BSONの基本操作テスト.


#include <iostream>
#include <string>
#include <vector>
#include <stdio.h>
#include <fstream>
#include <list>
#include <set>
#include "bson.h"

using namespace mongo;
using namespace bson;
using namespace std;

const string bob::numStrs[] = {
	"0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",  "8",  "9",
	"10", "11", "12", "13", "14", "15", "16", "17", "18", "19",
	"20", "21", "22", "23", "24", "25", "26", "27", "28", "29",
	"30", "31", "32", "33", "34", "35", "36", "37", "38", "39",
	"40", "41", "42", "43", "44", "45", "46", "47", "48", "49",
	"50", "51", "52", "53", "54", "55", "56", "57", "58", "59",
	"60", "61", "62", "63", "64", "65", "66", "67", "68", "69",
	"70", "71", "72", "73", "74", "75", "76", "77", "78", "79",
	"80", "81", "82", "83", "84", "85", "86", "87", "88", "89",
	"90", "91", "92", "93", "94", "95", "96", "97", "98", "99",
};

//This is to ensure that BSONObjBuilder doesn't try to use numStrs before the strings have been constructed
//	I've tested just making numStrs a char[][], but the overhead of constructing the strings each time was too high
//	numStrsReady will be 0 until after numStrs is initialized because it is a static variable

bool bob::numStrsReady = (numStrs[0].size() > 0);


void iter(bo o) {
	/* iterator example */
	cout << "
iter()
"; for( bo::iterator i(o); i.more(); ) { cout << ' ' << i.next().toString() << '
'; } } void iter2(bo myObj) { for( BSONObj::iterator i = myObj.begin(); i.more(); ) { BSONElement e = i.next(); } } int main() { //BSONObj /* a bson object defaults on construction to { } */ bo empty; cout << "empty: " << empty << endl; /* make a simple { name : 'joe', age : 33.7 } object */ { bob b; b.append("name", "joe"); b.append("age", 33.7); b.obj(); } /* make { name : 'joe', age : 33.7 } with a more compact notation. */ bo x = bob().append("name", "joe").append("age", 33.7).obj(); /* convert from bson to json */ string json = x.toString(); cout << "json for x:" << json << endl; /* access some fields of bson object x */ cout << "Some x things: " << x["name"] << ' ' << x["age"]<< ' ' << x.isEmpty() << endl; /* make a bit more complex object with some nesting { x : 'asdf', y : true, subobj : { z : 3, q : 4 } } /*/ bo y = BSON( "x" << "asdf" << "y" << true << "subobj" << BSON( "z" << 3 << "q" << 4 ) ); cout << "y==" << y << endl; // bob obj_total; obj_total.append("name","fuli-test"); obj_total.obj(); cout<<"**************"<<endl; // BSONObj BSONObj y_re=y.removeField("x"); cout << "y_re==" << y_re << endl; cout<<"nfield=="<<y.nFields()<<endl; /* reach in and get subobj.z */ cout << "subobj.z: " << y.getFieldDotted("subobj.z").Number() << endl; /* alternate syntax: */ cout << "subobj.z: " << y["subobj"]["z"].Number() << endl; /* fetch all *top level* elements from object y into a vector */ vector<be> v; y.elems(v); cout<<"---------------------"<<endl; cout << "v[0]=="<<v[0] << endl; cout << "v[1]=="<<v[1] << endl; cout << "v[2]=="<<v[2] << endl; cout << "v[2][0]=="<<v[2]["z"]<<endl; cout<<"v[0].fieldName()=="<<v[0].fieldName()<<endl; cout<<"v[1].fieldName()=="<<v[1].fieldName()<<endl; cout<<"v[2].fieldName()=="<<v[2].fieldName()<<endl; // Test if the element is a BSONobj,use isABSONObj() cout<<"isABSONObj v[0]: "<<v[0].isABSONObj()<<endl; cout<<"isABSONObj v[1]: "<<v[1].isABSONObj()<<endl; cout<<"isABSONObj v[2]: "<<v[2].isABSONObj()<<endl; //differ the String() & toString() string v0=v[0].String(); string v00=v[0].toString(); cout<<"v00=="<<v00<<endl; cout<<"v0=="<<v0<<endl; //handle sub-object bo sub = y["subobj"].Obj(); vector<be> w; sub.elems(w); cout<<"w[0]=="<<w[0]<<endl; cout<<"w[1]=="<<w[1]<<endl; cout<<"w[0].fieldName()=="<<w[0].fieldName()<<" "<<w[0].Number()<<endl; cout<<"w[1].fieldName()=="<<w[1].fieldName()<<" "<<w[1].Number()<<endl; /** raw data of the element's value (so be careful). */ /*use value()*/ cout<<"w[0].value()=="<<w[0].value()<<endl; /* into an array */ list<be> L; y.elems(L); //bo sub = y["subobj"].Obj(); /* grab all the int's that were in subobj. if it had elements that were not ints, we throw an exception (capital V on Vals() means exception if wrong type found */ vector<int> myints; sub.Vals(myints); cout << "my ints: " << myints[0] << ' ' << myints[1] << endl; /* grab all the string values from x. if the field isn't of string type, just skip it -- lowercase v on vals() indicates skip don't throw. */ vector<string> strs; x.vals(strs); cout << strs.size() << " strings, first one: " << strs[0] << endl; iter(y); bo an_obj; /** transform a BSON array into a vector of BSONElements. we match array # positions with their vector position, and ignore any fields with non-numeric field names. */ //vector<be> a = an_obj["x"].Array(); //be array = an_obj["x"]; // Use BSON_ARRAY macro like BSON macro, but without keys BSONArray arr = BSON_ARRAY( "hello" << 1 << BSON( "foo" << BSON_ARRAY( "bar" << "baz" << "qux" ) ) ); cout<<"arr=="<<arr<<endl; for(int i=0;i<arr.nFields();i++) { cout<<"arr["<<i<<"]=="<<arr[i]<<endl; } BSONArray carr=BSON_ARRAY(1<<2<<3<<BSON("name"<<"fuli")<<BSON("AGE"<<BSON_ARRAY(12<<13<<14))); cout<<"carr=="<<carr<<endl; cout<<"arr.isEmpty()=="<<arr.isEmpty()<<endl; // ARRAY for(BSONArray::iterator it=carr.begin(); it.more();) { be b=it.next(); cout<<"b=="<<b<<endl; } // ARRAY for(int j=0; j<carr.nFields(); j++) { cout<<"carr[j]=="<<carr[j]<<endl; } BSONArrayBuilder barr; //barr.append(1).append(2).arr(); BSONArray AA=barr.append(1).append(2).arr(); cout<<AA<<endl; BSONArrayBuilder barr1; //BSONArray ar=barr.append(1).append(2).arr(); bo xx= BSON("NAME"<<"FULI"); barr1.append(xx); barr1.append(xx); BSONArray yy=barr1.arr(); cout<<yy<<endl; bob xy; xy.append("anme","mom"); xy.append("age",12); /////Test read and write operation bo test=BSON("name"<<"ming"); BSONArrayBuilder test_arr; test_arr.append(1); test_arr.append(test); BSONArray test_arr1=test_arr.arr(); BSONObjBuilder test_bo; test_bo.append("testname","testming"); test_bo.append("dataarr",test_arr1); bo test1=test_bo.obj(); cout<<"test1=="<<test1<<endl; const char *p=test1.objdata(); int size=test1.objsize(); fstream outfile; outfile.open("a.txt",ios::binary | ios::out); if(!outfile) { perror("can not open file
"); return -1; } for(int i=0; i<size; i++) { outfile<<*p; p++; } outfile.close(); FILE *fp=fopen("a.txt","rb"); if(fp==NULL) { printf("Can not open infile
"); return-1; } char *pl= (char *)malloc(1000); fread(pl,size,1,fp); bob readin; readin.appendObject("data",pl,size); bo readbo=readin.obj(); cout<<"readbo=="<<readbo<<endl; ////Test BSONElement bo bo_ele=BSON("name"<<"elemet"); BSONElement element1=bo_ele["name"]; cout<<element1<<endl; cout<<element1.type()<<endl; cout<<"bo_ele=="<<bo_ele<<endl; cout<<bo_ele.getStringField("name")<<endl ; bo getbo=bo_ele.getObjectField("name").getOwned(); cout<<getbo<<endl; //test sting type BSONElement bo str_bo=BSON("name"<<"ming"); cout<<"type is: "<<str_bo["name"].type()<<endl; system("pause"); return 0; }