本文整理汇总了C++中BSONObjBuilder::appendBinData方法的典型用法代码示例。如果您正苦于以下问题:C++ BSONObjBuilder::appendBinData方法的具体用法?C++ BSONObjBuilder::appendBinData怎么用?C++ BSONObjBuilder::appendBinData使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BSONObjBuilder
的用法示例。
在下文中一共展示了BSONObjBuilder::appendBinData方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: msg_callback
void msg_callback(const sensor_msgs::Image::ConstPtr& msg)
{
BSONObjBuilder document;
Date_t stamp = msg->header.stamp.sec * 1000 + msg->header.stamp.nsec / 1000000;
document.append("header", BSON( "seq" << msg->header.seq
<< "stamp" << stamp
<< "frame_id" << msg->header.frame_id));
document.append("height", msg->height);
document.append("width", msg->width);
document.append("encoding", msg->encoding);
document.append("is_bigendian", msg->is_bigendian);
document.append("step", msg->step);
document.appendBinData("data", msg->data.size(), BinDataGeneral,
const_cast<unsigned char*>(&msg->data[0]));
mongodb_conn->insert(collection, document.obj());
// If we'd get access to the message queue this could be more useful
// https://code.ros.org/trac/ros/ticket/744
pthread_mutex_lock(&in_counter_mutex);
++in_counter;
pthread_mutex_unlock(&in_counter_mutex);
pthread_mutex_lock(&out_counter_mutex);
++out_counter;
pthread_mutex_unlock(&out_counter_mutex);
}
示例2:
GridFSChunk::GridFSChunk( BSONObj fileObject , int chunkNumber , const char * data , int len ) {
BSONObjBuilder b;
b.appendAs( fileObject["_id"] , "files_id" );
b.append( "n" , chunkNumber );
b.appendBinData( "data" , len, BinDataGeneral, data );
_data = b.obj();
}
示例3: run
void run(){
Scope * s = globalScriptEngine->createScope();
s->localConnect( "asd" );
const char * foo = "asdasdasdasd";
BSONObj in;
{
BSONObjBuilder b;
b.append( "a" , 7 );
b.appendBinData( "b" , strlen( foo ) , ByteArray , foo );
in = b.obj();
s->setObject( "x" , in );
}
s->invokeSafe( "myb = x.b; print( myb ); printjson( myb );" , BSONObj() );
s->invokeSafe( "y = { c : myb };" , BSONObj() );
BSONObj out = s->getObject( "y" );
ASSERT_EQUALS( BinData , out["c"].type() );
//blah( "in " , in["b"] );
//blah( "out" , out["c"] );
ASSERT_EQUALS( 0 , in["b"].woCompare( out["c"] , false ) );
delete s;
}
示例4: InsertBig
InsertBig() {
char buf[200000];
BSONObjBuilder b;
b.append("x", 99);
b.appendBinData("bin", 200000, (BinDataType) 129, buf);
x = b.obj();
}
示例5: handlePossibleShardedMessage
bool handlePossibleShardedMessage( Message &m, DbResponse &dbresponse ){
if ( shardConfigServer.empty() ){
return false;
}
int op = m.data->operation();
if ( op < 2000 || op >= 3000 )
return false;
const char *ns = m.data->_data + 4;
string errmsg;
if ( shardVersionOk( ns , errmsg ) ){
return false;
}
log() << "shardVersionOk failed ns:" << ns << " " << errmsg << endl;
if ( doesOpGetAResponse( op ) ){
BufBuilder b( 32768 );
b.skip( sizeof( QueryResult ) );
{
BSONObj obj = BSON( "$err" << errmsg );
b.append( obj.objdata() , obj.objsize() );
}
QueryResult *qr = (QueryResult*)b.buf();
qr->_resultFlags() = QueryResult::ResultFlag_ErrSet | QueryResult::ResultFlag_ShardConfigStale;
qr->len = b.len();
qr->setOperation( opReply );
qr->cursorId = 0;
qr->startingFrom = 0;
qr->nReturned = 1;
b.decouple();
Message * resp = new Message();
resp->setData( qr , true );
dbresponse.response = resp;
dbresponse.responseTo = m.data->id;
return true;
}
OID * clientID = clientServerIds.get();
massert( 10422 , "write with bad shard config and no server id!" , clientID );
log() << "got write with an old config - writing back" << endl;
BSONObjBuilder b;
b.appendBool( "writeBack" , true );
b.append( "ns" , ns );
b.appendBinData( "msg" , m.data->len , bdtCustom , (char*)(m.data) );
log() << "writing back msg with len: " << m.data->len << " op: " << m.data->_operation << endl;
clientQueues[clientID->str()]->push( b.obj() );
return true;
}
示例6: createBSONMetricChunkDocument
BSONObj createBSONMetricChunkDocument(ConstDataRange buf, Date_t date) {
BSONObjBuilder builder;
builder.appendDate(kFTDCIdField, date);
builder.appendNumber(kFTDCTypeField, static_cast<int>(FTDCType::kMetricChunk));
builder.appendBinData(kFTDCDataField, buf.length(), BinDataType::BinDataGeneral, buf.data());
return builder.obj();
}
示例7: run
void run() {
{
BSONObjBuilder b;
b.genOID();
b.append("files_id", 0);
b.append("n", 0);
b.appendBinData("data", 6, BinDataGeneral, "hello ");
db.insert(ns(), b.obj());
}
{
BSONObjBuilder b;
b.genOID();
b.append("files_id", 0);
b.append("n", 1);
b.appendBinData("data", 5, BinDataGeneral, "world");
db.insert(ns(), b.obj());
}
BSONObj result;
ASSERT( db.runCommand("test", BSON("filemd5" << 0), result) );
ASSERT_EQUALS( string("5eb63bbbe01eeed093cb22bb8f5acdc3") , result["md5"].valuestr() );
}
示例8: run
void run(){
Scope * s = globalScriptEngine->newScope();
s->localConnect( "asd" );
const char * foo = "asdas\0asdasd";
const char * base64 = "YXNkYXMAYXNkYXNk";
BSONObj in;
{
BSONObjBuilder b;
b.append( "a" , 7 );
b.appendBinData( "b" , 12 , BinDataGeneral , foo );
in = b.obj();
s->setObject( "x" , in );
}
s->invokeSafe( "myb = x.b; print( myb ); printjson( myb );" , BSONObj() );
s->invokeSafe( "y = { c : myb };" , BSONObj() );
BSONObj out = s->getObject( "y" );
ASSERT_EQUALS( BinData , out["c"].type() );
// pp( "in " , in["b"] );
// pp( "out" , out["c"] );
ASSERT_EQUALS( 0 , in["b"].woCompare( out["c"] , false ) );
// check that BinData js class is utilized
s->invokeSafe( "q = x.b.toString();", BSONObj() );
stringstream expected;
expected << "BinData(" << BinDataGeneral << ",\"" << base64 << "\")";
ASSERT_EQUALS( expected.str(), s->getString( "q" ) );
stringstream scriptBuilder;
scriptBuilder << "z = { c : new BinData( " << BinDataGeneral << ", \"" << base64 << "\" ) };";
string script = scriptBuilder.str();
s->invokeSafe( script.c_str(), BSONObj() );
out = s->getObject( "z" );
// pp( "out" , out["c"] );
ASSERT_EQUALS( 0 , in["b"].woCompare( out["c"] , false ) );
s->invokeSafe( "a = { f: new BinData( 128, \"\" ) };", BSONObj() );
out = s->getObject( "a" );
int len = -1;
out[ "f" ].binData( len );
ASSERT_EQUALS( 0, len );
ASSERT_EQUALS( 128, out[ "f" ].binDataType() );
delete s;
}
示例9: insert_user
/**
* @brief insert_user
* insert a new user in database
* @param username
* user name
* @param key
* XOR key to encrypt password
* @param role
* user role (0 = normal / 1 = admin)
* @return
* success status
*/
bool Database::insert_user(std::string username,std::string key_data,int role) {
QDateTime currentDate = QDateTime::currentDateTime();
std::string hash = username + ":" + realm + ":" + key_data;
QByteArray convert_hash;
if (digest_algo==ALGO_MD5)
convert_hash = QCryptographicHash::hash(hash.data(), QCryptographicHash::Md5).toHex();
else if (digest_algo==ALGO_SHA1)
convert_hash = QCryptographicHash::hash(hash.data(), QCryptographicHash::Sha1).toHex();
char * crypted_data = new char[convert_hash.length()];
encrypt((char*)convert_hash.data(),convert_hash.length(),key,crypted_data);
BSONObjBuilder generatedCert;
generatedCert.append(USER_USERNAME ,username);
generatedCert.append(USER_ROLE ,role);
generatedCert.appendBinData(USER_KEY ,convert_hash.length(),BinDataGeneral,crypted_data);
generatedCert.append(USER_CREATION ,Date_t(currentDate.toMSecsSinceEpoch()));
generatedCert.append(USER_LAST_LOGIN ,Date_t(currentDate.toMSecsSinceEpoch()));
delete[] crypted_data;
BSONObj userObj = generatedCert.obj();
con.insert("ssldashboard.users", userObj);
string e = con.getLastError();
if( !e.empty() ) {
cerr << e << endl;
return false;
}
return true;
}
示例10: while
const char *GridFileBuilder::privateAppendChunk(const char *data,
size_t length,
bool pending_insert) {
size_t chunk_len;
char const * const end = data + length;
while (data < end) {
chunk_len = min(_chunkSize, (size_t)(end-data));
// the last chunk needs to be stored as pending_data
if (chunk_len < _chunkSize && !pending_insert) break;
/* from gridfs.cpp at https://github.com/mongodb/mongo-cxx-driver/blob/legacy/src/mongo/client/gridfs.cpp */
BSONObjBuilder b;
b.appendAs( _file_id["_id"] , "files_id" );
b.append( "n" , _current_chunk );
b.appendBinData( "data" , chunk_len, BinDataGeneral, data );
BSONObj chunk_data = b.obj();
/************************************************************************/
++_current_chunk;
_client->insert( _chunkNS.c_str(), chunk_data );
data += chunk_len;
_file_length += chunk_len;
}
return data;
}
示例11: insertGeneratedCert
/**
* @brief insertGeneratedCert
* insert a new certificate in database
*
* @param isCaCert
* cert is CA or not
* @param certPublic
* public key value
* @param certPrivate
* private key value
* @param certP12
* p12 cert value
* @param start_date
* cert starting date
* @param end_date
* cert ending date
* @param serial
* cert serial number
* @param commonName
* cert common name
* @param signBySerialNum
* cert is sign with a given cert identified by serial number (-1 if self signed)
* @return
* success status
*/
bool Database::insertGeneratedCert(bool isCaCert,
std::string certPublic,
std::string certPrivate,
std::vector<char> certP12,
unsigned long long start_date,
unsigned long long end_date,
int serial,
std::string commonName,
int signBySerialNum
){
QDateTime currentDate = QDateTime::currentDateTime();
BSONObjBuilder generatedCert;
generatedCert.append(CERT_SEQ_NUM ,serial);
generatedCert.append(CERT_COMMON_NAME ,commonName);
generatedCert.append(CERT_IS_CA ,isCaCert);
generatedCert.append(CERT_SIGN_BY_SERIAL ,signBySerialNum);
generatedCert.append(CERT_START_DATE ,Date_t(start_date));
generatedCert.append(CERT_END_DATE ,Date_t(end_date));
generatedCert.append(CERT_RECORD_DATE_FIELD ,Date_t(currentDate.toMSecsSinceEpoch()));
generatedCert.append(CERT_PUBLIC_BODY ,certPublic);
generatedCert.append(CERT_PRIVATE_BODY ,certPrivate);
generatedCert.appendBinData(CERT_P12 ,certP12.size() ,BinDataGeneral,certP12.data());
BSONObj generatedCertObj = generatedCert.obj();
con.insert("ssldashboard.generatedcerts", generatedCertObj);
string e = con.getLastError();
if( !e.empty() ) {
cerr << e << endl;
return false;
}
return true;
}
示例12: if
void v8ToMongoElement( BSONObjBuilder & b , v8::Handle<v8::String> name , const string sname , v8::Handle<v8::Value> value ){
if ( value->IsString() ){
b.append( sname.c_str() , toSTLString( value ).c_str() );
return;
}
if ( value->IsFunction() ){
b.appendCode( sname.c_str() , toSTLString( value ).c_str() );
return;
}
if ( value->IsNumber() ){
if ( value->IsInt32() )
b.append( sname.c_str(), int( value->ToInt32()->Value() ) );
else
b.append( sname.c_str() , value->ToNumber()->Value() );
return;
}
if ( value->IsArray() ){
BSONObj sub = v8ToMongo( value->ToObject() );
b.appendArray( sname.c_str() , sub );
return;
}
if ( value->IsDate() ){
b.appendDate( sname.c_str() , Date_t(v8::Date::Cast( *value )->NumberValue()) );
return;
}
if ( value->IsExternal() )
return;
if ( value->IsObject() ){
// The user could potentially modify the fields of these special objects,
// wreaking havoc when we attempt to reinterpret them. Not doing any validation
// for now...
Local< v8::Object > obj = value->ToObject();
if ( obj->InternalFieldCount() && obj->GetInternalField( 0 )->IsNumber() ) {
switch( obj->GetInternalField( 0 )->ToInt32()->Value() ) { // NOTE Uint32's Value() gave me a linking error, so going with this instead
case Timestamp:
b.appendTimestamp( sname.c_str(),
Date_t( v8::Date::Cast( *obj->Get( v8::String::New( "time" ) ) )->NumberValue() ),
obj->Get( v8::String::New( "i" ) )->ToInt32()->Value() );
return;
case MinKey:
b.appendMinKey( sname.c_str() );
return;
case MaxKey:
b.appendMaxKey( sname.c_str() );
return;
default:
assert( "invalid internal field" == 0 );
}
}
string s = toSTLString( value );
if ( s.size() && s[0] == '/' ){
s = s.substr( 1 );
string r = s.substr( 0 , s.rfind( "/" ) );
string o = s.substr( s.rfind( "/" ) + 1 );
b.appendRegex( sname.c_str() , r.c_str() , o.c_str() );
}
else if ( value->ToObject()->GetPrototype()->IsObject() &&
value->ToObject()->GetPrototype()->ToObject()->HasRealNamedProperty( v8::String::New( "isObjectId" ) ) ){
OID oid;
oid.init( toSTLString( value ) );
b.appendOID( sname.c_str() , &oid );
}
else if ( !value->ToObject()->GetHiddenValue( v8::String::New( "__NumberLong" ) ).IsEmpty() ) {
// TODO might be nice to potentially speed this up with an indexed internal
// field, but I don't yet know how to use an ObjectTemplate with a
// constructor.
unsigned long long val =
( (unsigned long long)( value->ToObject()->Get( v8::String::New( "top" ) )->ToInt32()->Value() ) << 32 ) +
(unsigned)( value->ToObject()->Get( v8::String::New( "bottom" ) )->ToInt32()->Value() );
b.append( sname.c_str(), (long long)val );
}
else if ( !value->ToObject()->GetHiddenValue( v8::String::New( "__DBPointer" ) ).IsEmpty() ) {
OID oid;
oid.init( toSTLString( value->ToObject()->Get( v8::String::New( "id" ) ) ) );
string ns = toSTLString( value->ToObject()->Get( v8::String::New( "ns" ) ) );
b.appendDBRef( sname.c_str(), ns.c_str(), oid );
}
else if ( !value->ToObject()->GetHiddenValue( v8::String::New( "__BinData" ) ).IsEmpty() ) {
int len = obj->Get( v8::String::New( "len" ) )->ToInt32()->Value();
v8::String::Utf8Value data( obj->Get( v8::String::New( "data" ) ) );
const char *dataArray = *data;
assert( data.length() == len );
b.appendBinData( sname.c_str(),
len,
mongo::BinDataType( obj->Get( v8::String::New( "type" ) )->ToInt32()->Value() ),
dataArray );
} else {
BSONObj sub = v8ToMongo( value->ToObject() );
b.append( sname.c_str() , sub );
}
return;
}
//.........这里部分代码省略.........
示例13: saslClientAuthenticate
Status saslClientAuthenticate(Gsasl *gsasl,
DBClientWithCommands* client,
const BSONObj& saslParameters,
void* sessionHook) {
GsaslSession session;
int saslLogLevel = getSaslClientLogLevel(saslParameters);
Status status = configureSession(gsasl, client, saslParameters, sessionHook, &session);
if (!status.isOK())
return status;
std::string targetDatabase;
status = bsonExtractStringFieldWithDefault(saslParameters,
saslCommandPrincipalSourceFieldName,
saslDefaultDBName,
&targetDatabase);
if (!status.isOK())
return status;
BSONObj saslFirstCommandPrefix = BSON(
saslStartCommandName << 1 <<
saslCommandMechanismFieldName << session.getMechanism());
BSONObj saslFollowupCommandPrefix = BSON(saslContinueCommandName << 1);
BSONObj saslCommandPrefix = saslFirstCommandPrefix;
BSONObj inputObj = BSON(saslCommandPayloadFieldName << "");
bool isServerDone = false;
while (!session.isDone()) {
std::string payload;
BSONType type;
status = saslExtractPayload(inputObj, &payload, &type);
if (!status.isOK())
return status;
LOG(saslLogLevel) << "sasl client input: " << base64::encode(payload) << endl;
std::string responsePayload;
status = session.step(payload, &responsePayload);
if (!status.isOK())
return status;
LOG(saslLogLevel) << "sasl client output: " << base64::encode(responsePayload) << endl;
BSONObjBuilder commandBuilder;
commandBuilder.appendElements(saslCommandPrefix);
commandBuilder.appendBinData(saslCommandPayloadFieldName,
int(responsePayload.size()),
BinDataGeneral,
responsePayload.c_str());
BSONElement conversationId = inputObj[saslCommandConversationIdFieldName];
if (!conversationId.eoo())
commandBuilder.append(conversationId);
// Server versions 2.3.2 and earlier may return "ok: 1" with a non-zero "code" field,
// indicating a failure. Subsequent versions should return "ok: 0" on failure with a
// non-zero "code" field to indicate specific failure. In all versions, ok: 1, code: >0
// and ok: 0, code optional, indicate failure.
bool ok = client->runCommand(targetDatabase, commandBuilder.obj(), inputObj);
ErrorCodes::Error code = ErrorCodes::fromInt(
inputObj[saslCommandCodeFieldName].numberInt());
if (!ok || code != ErrorCodes::OK) {
if (code == ErrorCodes::OK)
code = ErrorCodes::UnknownError;
return Status(code, inputObj[saslCommandErrmsgFieldName].str());
}
isServerDone = inputObj[saslCommandDoneFieldName].trueValue();
saslCommandPrefix = saslFollowupCommandPrefix;
}
if (!isServerDone)
return Status(ErrorCodes::ProtocolError, "Client finished before server.");
return Status::OK();
}
示例14:
void SHA1Block::appendAsBinData(BSONObjBuilder& builder, StringData fieldName) const {
builder.appendBinData(fieldName, _hash.size(), BinDataGeneral, _hash.data());
}
示例15: _handlePossibleShardedMessage
bool _handlePossibleShardedMessage( Message &m, DbResponse* dbresponse ) {
DEV assert( shardingState.enabled() );
int op = m.operation();
if ( op < 2000
|| op >= 3000
|| op == dbGetMore // cursors are weird
)
return false;
DbMessage d(m);
const char *ns = d.getns();
string errmsg;
// We don't care about the version here, since we're returning it later in the writeback
ConfigVersion received, wanted;
if ( shardVersionOk( ns , errmsg, received, wanted ) ) {
return false;
}
LOG(1) << "connection meta data too old - will retry ns:(" << ns << ") op:(" << opToString(op) << ") " << errmsg << endl;
if ( doesOpGetAResponse( op ) ) {
assert( dbresponse );
BufBuilder b( 32768 );
b.skip( sizeof( QueryResult ) );
{
BSONObj obj = BSON( "$err" << errmsg << "ns" << ns );
b.appendBuf( obj.objdata() , obj.objsize() );
}
QueryResult *qr = (QueryResult*)b.buf();
qr->_resultFlags() = ResultFlag_ErrSet | ResultFlag_ShardConfigStale;
qr->len = b.len();
qr->setOperation( opReply );
qr->cursorId = 0;
qr->startingFrom = 0;
qr->nReturned = 1;
b.decouple();
Message * resp = new Message();
resp->setData( qr , true );
dbresponse->response = resp;
dbresponse->responseTo = m.header()->id;
return true;
}
uassert( 9517 , "writeback" , ( d.reservedField() & DbMessage::Reserved_FromWriteback ) == 0 );
OID writebackID;
writebackID.init();
lastError.getSafe()->writeback( writebackID );
const OID& clientID = ShardedConnectionInfo::get(false)->getID();
massert( 10422 , "write with bad shard config and no server id!" , clientID.isSet() );
LOG(1) << "got write with an old config - writing back ns: " << ns << endl;
LOG(1) << m.toString() << endl;
BSONObjBuilder b;
b.appendBool( "writeBack" , true );
b.append( "ns" , ns );
b.append( "id" , writebackID );
b.append( "connectionId" , cc().getConnectionId() );
b.append( "instanceIdent" , prettyHostName() );
b.appendTimestamp( "version" , shardingState.getVersion( ns ) );
ShardedConnectionInfo* info = ShardedConnectionInfo::get( false );
b.appendTimestamp( "yourVersion" , info ? info->getVersion(ns) : (ConfigVersion)0 );
b.appendBinData( "msg" , m.header()->len , bdtCustom , (char*)(m.singleData()) );
LOG(2) << "writing back msg with len: " << m.header()->len << " op: " << m.operation() << endl;
writeBackManager.queueWriteBack( clientID.str() , b.obj() );
return true;
}