本文整理汇总了C++中BSONObj::add方法的典型用法代码示例。如果您正苦于以下问题:C++ BSONObj::add方法的具体用法?C++ BSONObj::add怎么用?C++ BSONObj::add使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BSONObj
的用法示例。
在下文中一共展示了BSONObj::add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: insert
bool DjondbConnection::insert(const char* db, const char* ns, const BSONObj& bson) {
if (_logger->isDebug()) _logger->debug(2, "Insert command. db: %s, ns: %s", db, ns);
if (!isOpen()) {
throw DjondbException(D_ERROR_CONNECTION, "Not connected to any server");
}
BSONObj* obj = new BSONObj(bson);
InsertCommand cmd;
cmd.setDB(db);
if (!obj->has("_id")) {
std::string* id = uuid();
obj->add("_id", const_cast<char*>(id->c_str()));
delete id;
}
if (!obj->has("_revision")) {
std::string* rev = uuid();
obj->add("_revision", const_cast<char*>(rev->c_str()));
delete rev;
}
cmd.setBSON(obj);
cmd.setNameSpace(ns);
prepareOptions((Command*)&cmd);
_commandWriter->writeCommand(&cmd);
cmd.readResult(_inputStream);
int hasResults = false; //_inputStream->readInt();
if (hasResults) {
// When the bson didnt contain an id the server will return a bson with it
// At this moment this will never occur, but I will leave this code for later
}
return true;
}
示例2: commandClients
// this method is executed in a thread to simulate multiple clients
void* commandClients(void* arg) {
NetworkOutputStream* nos = new NetworkOutputStream();
int socket = nos->open("localhost", _port);
NetworkInputStream* nis = new NetworkInputStream(socket);
Logger* log = getLogger(NULL);
CommandWriter* writer = new CommandWriter(nos);
for (int x = 0; x < 10; x++) {
log->info("client: preparing insert");
InsertCommand* cmd = new InsertCommand();
cmd->setDB("db1");
cmd->setNameSpace("ns");
BSONObj* o = new BSONObj();
std::string* id = uuid();
o->add("_id", id->c_str());
delete id;
std::string* rev = uuid();
o->add("_revision", rev->c_str());
delete rev;
o->add("name", "John");
cmd->setBSON(o);
BSONObj* options = new BSONObj();
cmd->setOptions(options);
log->info("client: writing insert command");
writer->writeCommand(cmd);
log->info("client: insert command sent");
delete cmd;
log->info("client: preparing showdbs command");
ShowdbsCommand* showCmd = new ShowdbsCommand();
BSONObj* options2 = new BSONObj();
showCmd->setOptions(options2);
log->info("client: sending showCmd");
writer->writeCommand(showCmd);
log->info("client: waiting showDbs answer");
int dbs = nis->readInt();
EXPECT_EQ(dbs, 3);
char* db1 = nis->readChars();
EXPECT_TRUE(strcmp(db1, "db1") == 0);
char* db2 = nis->readChars();
EXPECT_TRUE(strcmp(db2, "db2") == 0);
char* db3 = nis->readChars();
EXPECT_TRUE(strcmp(db3, "db3") == 0);
log->info("client: showDbs received");
free(db1);
free(db2);
free(db3);
}
log->info("client: sending ShutdownCommand");
ShutdownCommand* shut = new ShutdownCommand();
writer->writeCommand(shut);
log->info("client: shutdown sent");
nis->close();
}
示例3: remove
void DBController::remove(const char* db, const char* ns, const char* documentId, const char* revision, const BSONObj* options) {
if (_logger->isDebug()) _logger->debug(2, "DBController::update db: %s, ns: %s, documentId: %s, revision: %s", db, ns, documentId, revision);
StreamType* streamData = StreamManager::getStreamManager()->open(db, ns, DATA_FTYPE);
IndexAlgorithm* impl = IndexFactory::indexFactory.index(db, ns, "_id");
BSONObj indexBSON;
indexBSON.add("_id", documentId);
Index* index = impl->find(&indexBSON);
if (index != NULL) {
// TODO check the revision id
StreamType* out = StreamManager::getStreamManager()->open(db, ns, DATA_FTYPE);
out->flush();
long currentPos = out->currentPos();
out->seek(index->posData);
BSONObj* obj = readBSON(out);
obj->add("_status", 2); // DELETED
// Get back to the record start
out->seek(index->posData);
writeBSON(out, obj);
// restores the last position
out->seek(currentPos);
//std::string id = obj->getDJString("_id");
//CacheManager::objectCache()->remove(id);
delete obj;
}
}
示例4: FileOutputStream
TEST(TestCommand, testInsertCommand) {
cout << "testInsertCommand" << endl;
FileOutputStream* fos = new FileOutputStream("test.dat", "wb");
CommandWriter* commandWriter = new CommandWriter(fos);
InsertCommand cmd;
cmd.setDB("testdb");
cmd.setNameSpace("test.namespace.db");
BSONObj* obj = new BSONObj();
obj->add("name", "Cross");
obj->add("age", 18);
cmd.setBSON(obj);
commandWriter->writeCommand(&cmd);
fos->close();
delete fos;
delete commandWriter;
FileInputStream* fis = new FileInputStream("test.dat", "rb");
CommandReader* reader = new CommandReader(fis);
InsertCommand* rdCmd = (InsertCommand*) reader->readCommand();
EXPECT_TRUE(rdCmd != NULL);
EXPECT_TRUE(rdCmd->nameSpace()->compare("test.namespace.db") == 0);
EXPECT_TRUE(rdCmd->DB()->compare("testdb") == 0);
const BSONObj* objResult = rdCmd->bson();
EXPECT_TRUE(objResult != NULL);
EXPECT_TRUE(objResult->has("name"));
EXPECT_TRUE(objResult->getString("name").compare("Cross") == 0);
}
示例5: testToChar
void testToChar()
{
cout << "testToChar" << endl;
BSONObj obj;
obj.add("int", 1);
obj.add("string", (char*)"test");
obj.add("char*", (char*)"char*");
obj.add("long", (__int64)1L);
obj.add("double", 1.1);
char* json = obj.toChar();
int res = strcmp(json, "{ \"char*\" : \"char*\", \"double\" : 1.100000, \"int\" : 1, \"long\" : 1, \"string\" : \"test\"}");
TEST_ASSERT(res == 0);
if (res != 0) {
cout << "\nResult: " << json << endl;
}
free(json);
BSONObj inner;
inner.add("int", 1);
inner.add("string", (char*)"test");
inner.add("char*", (char*)"char*");
inner.add("long", (__int64)1L);
inner.add("double", 1.1);
obj.add("inner", inner);
json = obj.toChar();
cout << "\nResult: " << json << endl;
free(json);
}
示例6: producer
void* producer(void* arg) {
NetworkOutputStream* nos = new NetworkOutputStream();
int socket = nos->open("localhost", _port);
printf("Producer started\n");
Logger* log = getLogger(NULL);
log->info("Producer starter");
log->startTimeRecord();
if (socket > 0) {
NetworkInputStream* nis = new NetworkInputStream(socket);
std::auto_ptr<CommandWriter> writer(new CommandWriter(nos));
for (int x = 0; x < MAX_INSERT; x++) {
std::auto_ptr<InsertCommand> cmd(new InsertCommand());
BSONObj* obj = new BSONObj();
std::auto_ptr<std::string> guid(uuid());
obj->add("_id", guid->c_str());
char* temp = (char*)malloc(2000);
memset(temp, 0, 2000);
memset(temp, 'a', 1999);
int len = strlen(temp);
obj->add("content", temp);
free(temp);
cmd->setBSON(obj);
std::string db("mydb");
cmd->setDB(db);
std::string ns("myns");
cmd->setNameSpace(ns);
cmd->setOptions(new BSONObj());
writer->writeCommand(cmd.get());
int result = nis->readInt();
EXPECT_EQ(result, 1);
if (result != 1) {
break;
}
}
nis->close();
} else {
printf("Socket is 0");
}
log->info("Producer end");
log->stopTimeRecord();
DTime time = log->recordedTime();
if (time.totalSecs() > 0) {
log->info("Producer time: %d secs. Operations per sec: %d", time.totalSecs(), MAX_INSERT / time.totalSecs());
} else {
EXPECT_TRUE(false) << "Something was wrong network could not execute " << MAX_INSERT << " in 0 secs.";
}
}
示例7: getLogger
TEST(testIndexP, testRecoverNames) {
Logger* log = getLogger(NULL);
std::set<std::string> keys;
keys.insert("_id");
BPlusIndexP* index = new BPlusIndexP("testIndexNames");
index->setKeys(keys);
FileInputStream* fis = new FileInputStream("names.txt", "r");
cout << "Adding names to the index" << endl;
std::vector<std::string> names;
int x = 0;
while (true) {
if (fis->eof()) {
cout << "No more names" << endl;
break;
}
if (x >= 100) {
break;
}
x++;
BSONObj o;
std::string* name = fis->readString();
o.add("_id", name->c_str());
char* temp = strcpy(const_cast<char*>(name->c_str()), name->length());
index->add(o, djondb::string(temp, name->length()), 100);
index->debug();
if (log->isDebug()) log->debug("===============================================================================");
names.push_back(*name);
delete name;
}
index->debug();
delete index;
cout << "Finding names from the index" << endl;
index = new BPlusIndexP("testIndexNames");
index->setKeys(keys);
index->debug();
for (std::vector<std::string>::iterator i = names.begin(); i != names.end(); i++) {
std::string name = *i;
BSONObj o;
o.add("_id", name.c_str());
Index* idx = index->find(&o);
ASSERT_TRUE(idx != NULL) << "_id " << name.c_str() << " not found";
ASSERT_TRUE(idx->key->has("_id")) << "Retrieved index for _id " << name.c_str() << " does not returned the _id";
EXPECT_TRUE(idx->key->getString("_id").compare(name) == 0) << "Recovered a wrong key, expected: " << name.c_str() << " and retrived: " << idx->key->getString("_id").c_str();
}
delete index;
}
示例8: update
void DBController::update(const char* db, const char* ns, BSONObj* obj, const BSONObj* options) {
if (_logger->isDebug()) _logger->debug(2, "DBController::update ns: %s, bson: %s", ns, obj->toChar());
StreamType* streamData = StreamManager::getStreamManager()->open(db, ns, DATA_FTYPE);
Index* index = findIndex(db, ns, obj);
long currentPos = streamData->currentPos();
// Moves to the record to update
streamData->seek(index->posData);
BSONObj* previous = readBSON(streamData);
previous->add("_status", 3); // Updated
streamData->seek(index->posData);
writeBSON(streamData, previous);
// Back to the end of the stream
streamData->seek(currentPos);
updateIndex(db, ns, obj, streamData->currentPos());
obj->add("_status", 1); // Active
writeBSON(streamData, obj);
//std::string id = obj->getDJString("_id");
//CacheManager::objectCache()->add(id, new BSONObj(*obj));
}
示例9: updateIndex
void DBController::updateIndex(const char* db, const char* ns, BSONObj* bson, long filePos) {
BSONObj indexBSON;
djondb::string id = bson->getDJString("_id");
indexBSON.add("_id", (char*)id);
IndexAlgorithm* impl = IndexFactory::indexFactory.index(db, ns, "_id");
impl->update(indexBSON, id, filePos);
}
示例10: findIndex
Index* DBController::findIndex(const char* db, const char* ns, BSONObj* bson) {
IndexAlgorithm* impl = IndexFactory::indexFactory.index(db, ns, "_id");
BSONObj indexBSON;
indexBSON.add("_id", (const char*)bson->getDJString("_id"));
Index* index = impl->find(&indexBSON);
return index;
}
示例11: testTransactionManager
void testTransactionManager() {
printf("%s\n", "testTransactionManager");
// Insert a document in wal
BaseTransaction* wal = new BaseTransaction(_controller);
wal->dropNamespace("db", "mtx");
TransactionManager* manager = new TransactionManager(wal);
BSONObj testA;
testA.add("cod", 1);
testA.add("name", "William");
wal->insert("db", "mtx", &testA);
std::string* t1 = uuid();
StdTransaction* transaction = manager->getTransaction(*t1);
std::auto_ptr<BSONArrayObj> array(transaction->find("db", "mtx", "*", "$'cod' == 1"));
BSONObj* obj1up = array->get(0);
obj1up->add("lastName", "Shakespeare");
transaction->update("db", "mtx", obj1up);
std::auto_ptr<BSONArrayObj> array0(wal->find("db", "mtx", "*", "$'cod' == 1"));
BSONObj* origin1 = array0->get(0);
TEST_ASSERT(!origin1->has("lastName"));
std::auto_ptr<BSONArrayObj> array1(transaction->find("db", "mtx", "*", "$'cod' == 1"));
BSONObj* objtx1 = array1->get(0);
TEST_ASSERT(objtx1->has("lastName"));
transaction->commit();
manager->dropTransaction(*t1);
std::auto_ptr<BSONArrayObj> array2(wal->find("db", "mtx", "*", "$'cod' == 1"));
BSONObj* origin2 = array2->get(0);
TEST_ASSERT(origin2->has("lastName"));
delete t1;
delete manager;
printf("%s\n", "~testTransactionManager");
}
示例12: testTransactionMergedData
void testTransactionMergedData()
{
printf("%s\n", "testTransactionMergedData");
BaseTransaction* tx = new BaseTransaction(_controller);
tx->dropNamespace("db", "testcommit");
BSONObj ooutTX;
std::string* idOut = uuid();
ooutTX.add("_id", const_cast<char*>(idOut->c_str()));
ooutTX.add("name", "JohnOut");
tx->insert("db", "testcommit", &ooutTX);
// Insert out of the transaction
std::string* tuid = uuid();
StdTransaction* stx = new StdTransaction(tx, *tuid);
BSONObj o;
std::string* id = uuid();
o.add("_id", const_cast<char*>(id->c_str()));
o.add("name", "John");
stx->insert("db", "testcommit", &o);
BSONArrayObj* res = stx->find("db", "testcommit", "*", "");
TEST_ASSERT(res->length() == 2);
BSONArrayObj* resOut = tx->find("db", "testcommit", "*", "");
TEST_ASSERT(resOut->length() == 1);
stx->commit();
delete stx;
BSONArrayObj* resOut2 = tx->find("db", "testcommit", "*", "");
TEST_ASSERT(resOut2->length() == 2);
delete tx;
delete res;
delete resOut;
delete resOut2;
delete tuid;
printf("%s\n", "~testTransactionMergedData");
}
示例13: testCopyBSON
void testCopyBSON()
{
cout << "testCopyBSON" << endl;
BSONObj* objOrig = new BSONObj();
// Add in
objOrig->add("int", 1);
objOrig->add("string", (char*)"test");
objOrig->add("long", (__int64)1L);
objOrig->add("double", 1.1);
BSONObj rel;
rel.add("innertext", (char*)"inner text");
objOrig->add("rel1", rel);
BSONArrayObj array;
BSONObj b1;
b1.add("b1", "test");
array.add(b1);
BSONObj b2;
b2.add("b1", "test2");
array.add(b2);
objOrig->add("array", array);
BSONObj* obj = new BSONObj(*objOrig);
delete objOrig;
objOrig = NULL;
TEST_ASSERT(obj->has("int"));
TEST_ASSERT(obj->getInt("int") == 1);
TEST_ASSERT(strcmp(obj->getString("string"), "test") == 0);
TEST_ASSERT(obj->has("long"));
TEST_ASSERT(obj->getLong("long") == 1L);
TEST_ASSERT(obj->has("double"));
TEST_ASSERT(obj->getDouble("double") == 1.1);
BSONObj* temp = obj->getBSON("rel1");
TEST_ASSERT(temp != NULL);
TEST_ASSERT(strcmp(obj->getBSON("rel1")->getString("innertext"), "inner text") == 0);
TEST_ASSERT(obj->getBSONArray("array") != NULL);
BSONArrayObj* arrayR = obj->getBSONArray("array");
TEST_ASSERT(arrayR != NULL);
TEST_ASSERT(arrayR->length() == 2);
BSONObj* el1 = arrayR->get(0);
TEST_ASSERT(el1 != NULL);
BSONObj* el2 = arrayR->get(1);
TEST_ASSERT(el2 != NULL);
delete obj;
}
示例14: testAutocasting
void testAutocasting() {
cout << "testAutocasting" << endl;
BSONObj o;
o.add("long", (__int64)1L);
o.add("double", 2.0);
o.add("int", 1);
o.add("char*", (char*)"Test");
BSONObj inner;
inner.add("text", "text");
o.add("inner", inner);
TEST_ASSERT(o.getContent("long") != NULL);
TEST_ASSERT((__int64)*o.getContent("long") == 1L);
TEST_ASSERT(o.getContent("double") != NULL);
TEST_ASSERT((double)*o.getContent("double") == 2.0);
TEST_ASSERT(o.getContent("int") != NULL);
TEST_ASSERT((int)*o.getContent("int") == 1);
TEST_ASSERT(o.getContent("inner") != NULL);
BSONObj* obj = *o.getContent("inner");
TEST_ASSERT(strcmp(obj->getString("text"), "text") == 0);
}
示例15: BPlusIndexP
TEST(testIndexP, testRecoverRandom) {
std::set<std::string> keys;
keys.insert("_id");
BPlusIndexP* index = new BPlusIndexP("testIndex2");
index->setKeys(keys);
std::vector<std::string> ids;
for (int x = 0; x < 10; x++) {
BSONObj o;
std::stringstream ss;
ss << x;
std::string id = ss.str();
o.add("_id", id.c_str());
int n = rand() % 100;
if (n > 0) {
ids.push_back(id);
}
char* temp = strcpy(const_cast<char*>(id.c_str()), id.length());
index->add(o, djondb::string(temp, id.length()), 100);
index->debug();
}
index->debug();
delete index;
index = new BPlusIndexP("testIndex2");
index->setKeys(keys);
index->debug();
for (std::vector<std::string>::iterator i = ids.begin(); i != ids.end(); i++) {
std::string guid = *i;
BSONObj o;
o.add("_id", guid.c_str());
Index* idx = index->find(&o);
ASSERT_TRUE(idx != NULL);
ASSERT_TRUE(idx->key->has("_id"));
EXPECT_TRUE(idx->key->getString("_id").compare(guid) == 0);
}
delete index;
}