Example #1
0
bsoncxx::document::view scoped_bson_t::view() {
    // if we were initialized with a view_or_value just use that view
    if (_doc) {
        return _doc->view();
    }
    // otherwise, if we were initialized from libmongoc, construct
    if (_is_initialized) {
        return bsoncxx::document::view{bson_get_data(bson()), bson()->len};
    }
    // otherwise, return an empty view
    return bsoncxx::document::view{};
}
Example #2
0
int Processer::svc(void)
{
	char ch[SIZE] = { };
	while(loop)
	{
		try
		{
			msg_ = 0;
			getq(msg_);

			//change to mongo::BSONObj
			//handle it
			if(msg_)
			{
				memset(ch,0,SIZE);
				memcpy(ch,msg_->rd_ptr(),SIZE);
				if(!(*ch))
					throw "EMPTY BSONOBJ";
				mongo::BSONObj bson(ch);
				std::cout << bson << std::endl;

				Handle(bson);

				delete [] msg_->rd_ptr();
				msg_ ->release();
			}
		}
		catch(const char* ch)
		{
			LOG4CXX_ERROR(log_,ch);
		}
	}

	return 0;
}
Status
TerarkDbRecordStore::updateRecord(OperationContext* txn,
								const RecordId& id,
								const char* data,
								int len,
								bool enforceQuota,
								UpdateNotifier* notifier) {
	CompositeTable* tab = m_table->m_tab.get();
	terark::db::IncrementGuard_size_t incrGuard(tab->m_inprogressWritingCount);
	llong recId = id.repr() - 1;
	{
		terark::db::MyRwLock lock(tab->m_rwMutex, false);
		size_t segIdx = tab->getSegmentIndexOfRecordIdNoLock(recId);
		if (segIdx >= tab->getSegNum()) {
			return {ErrorCodes::InvalidIdField, "record id is out of range"};
		}
		auto seg = tab->getSegmentPtr(segIdx);
		if (seg->m_isFreezed) {
			return {ErrorCodes::NeedsDocumentMove, "segment of record is frozen"};
		}
	}
    auto& td = m_table->getMyThreadData();
    BSONObj bson(data);
    td.m_coder.encode(&tab->rowSchema(), nullptr, bson, &td.m_buf);
	llong newRecId = tab->updateRow(recId, td.m_buf, &*td.m_dbCtx);
	invariant(newRecId == recId);
	return Status::OK();
}
Cursor CollectionView::get(Flags::Query flags) const
{
   bson_t q;
   bson_t f;

   BSON::BSONC bson("$query", _query);

   if (_sort) {
      bson.append("$orderby", _sort);
   }

   Utils::to_bson_t(bson, &q);

   if (_fields) {
      Utils::to_bson_t(_fields, &f);
   }

   mongoc_cursor_t *cursor = mongoc_collection_find(
      _collection->collection.get(),
      (mongoc_query_flags_t) flags,
      _skip,
      _limit,
      0,
      &q,
      (_fields ? &f : nullptr),
      nullptr
   );

   return Cursor(std::unique_ptr<CursorImpl>(new CursorImpl(_collection->client, cursor)));
}
Example #5
0
bsoncxx::document::value scoped_bson_t::steal() {
    if (!_is_initialized) {
        return bsoncxx::document::value{bsoncxx::document::view()};
    }

    std::uint32_t length;
    std::uint8_t* buff = bson_destroy_with_steal(bson(), true, &length);

    return bsoncxx::document::value(buff, length, bson_free_deleter);
}
StatusWith<RecordId> TerarkDbRecordStore::insertRecord(OperationContext* txn,
													 const char* data,
													 int len,
													 bool enforceQuota) {
	CompositeTable* tab = m_table->m_tab.get();
    auto& td = m_table->getMyThreadData();
    BSONObj bson(data);
	invariant(bson.objsize() == len);
    td.m_coder.encode(&tab->rowSchema(), nullptr, bson, &td.m_buf);
    llong recIdx = tab->insertRow(td.m_buf, &*td.m_dbCtx);
	return {RecordId(recIdx + 1)};
}
Status TerarkDbRecordStore::insertRecords(OperationContext* txn,
										std::vector<Record>* records,
										bool enforceQuota) {
	CompositeTable* tab = m_table->m_tab.get();
    auto& td = m_table->getMyThreadData();
    for (Record& rec : *records) {
    	BSONObj bson(rec.data.data());
    	td.m_coder.encode(&tab->rowSchema(), nullptr, bson, &td.m_buf);
    	rec.id = RecordId(1 + tab->insertRow(td.m_buf, &*td.m_dbCtx));
    }
    return Status::OK();
}
Example #8
0
    int KeyV1::dataSize() const { 
        const unsigned char *p = _keyData;
        if( !isCompactFormat() ) {
            return bson().objsize() + 1;
        }

        bool more;
        do { 
            unsigned z = sizeOfElement(p);
            more = (*p & cHASMORE) != 0;
            p += z;
        } while( more );
        return p - _keyData;
    }
bool QJsonPipe::send(const QJsonObject& object)
{
    Q_D(QJsonPipe);
    if (!d->mOut)
        return false;

    QJsonDocument document(object);

    switch (d->mFormat) {
    case FormatUndefined:
        d->mFormat = FormatQBJS;
        // Deliberate fall through
    case FormatQBJS:
        d->mOutBuffer.append(document.toBinaryData());
        break;
    case FormatUTF8:
        d->mOutBuffer.append(document.toJson());
        break;
    case FormatUTF16BE:
        d->mOutBuffer.append( QTextCodec::codecForName("UTF-16BE")->fromUnicode(QString::fromUtf8(document.toJson())).mid(2) );
        break;
    case FormatUTF16LE:
        d->mOutBuffer.append( QTextCodec::codecForName("UTF-16LE")->fromUnicode(QString::fromUtf8(document.toJson())).mid(2) );
        break;
    case FormatUTF32BE:
        d->mOutBuffer.append( QTextCodec::codecForName("UTF-32BE")->fromUnicode(QString::fromUtf8(document.toJson())).mid(4) );
        break;
    case FormatUTF32LE:
        d->mOutBuffer.append( QTextCodec::codecForName("UTF-32LE")->fromUnicode(QString::fromUtf8(document.toJson())).mid(4) );
        break;
    case FormatBSON:
    {
        BsonObject bson(document.toVariant().toMap());
        d->mOutBuffer.append("bson");
        d->mOutBuffer.append(bson.data());
        break;
    }
    }
    if (d->mOutBuffer.size())
        d->mOut->setEnabled(true);
    return true;
}
Example #10
0
size_t Mongo_Storage::write(const void* buf, size_t buf_size, size_t timeout)
{
    if (!buf || (buf_size == 0))
        return 0;

    if (((const char*)buf)[buf_size - 1] != 0)
        THROWM(fplog::exceptions::Write_Failed, "Only null-terminated strings are supported by the storage.");

    try
    {
        mongo::BSONObj bson(mongo::fromjson((const char*)buf));
        connection_->insert(collection_, bson);
    }
    catch (mongo::DBException& e)
    {
        THROWM(fplog::exceptions::Write_Failed, e.what());
    }

    return buf_size;
}
Example #11
0
    BSONObj KeyV1::toBson() const { 
        verify( _keyData != 0 );
        if( !isCompactFormat() )
            return bson();

        BSONObjBuilder b(512);
        const unsigned char *p = _keyData;
        while( 1 ) { 
            unsigned bits = *p++;

            switch( bits & 0x3f ) {
                case cminkey: b.appendMinKey(""); break;
                case cnull:   b.appendNull(""); break;
                case cfalse:  b.appendBool("", false); break;
                case ctrue:   b.appendBool("", true); break;
                case cmaxkey: 
                    b.appendMaxKey(""); 
                    break;
                case cstring:
                    {
                        unsigned sz = *p++;
                        // we build the element ourself as we have to null terminate it
                        BufBuilder &bb = b.bb();
                        bb.appendNum((char) String);
                        bb.appendUChar(0); // fieldname ""
                        bb.appendNum(sz+1);
                        bb.appendBuf(p, sz);
                        bb.appendUChar(0); // null char at end of string
                        p += sz;
                        break;
                    }
                case coid:
                    b.appendOID("", (OID *) p);
                    p += sizeof(OID);
                    break;
                case cbindata:
                    {
                        int len = binDataCodeToLength(*p);
                        int subtype = (*p) & BinDataTypeMask;
                        if( subtype & 0x8 ) { 
                            subtype = (subtype & 0x7) | 0x80;
                        }
                        b.appendBinData("", len, (BinDataType) subtype, ++p);
                        p += len;
                        break;
                    }
                case cdate:
                    b.appendDate("", (Date_t&) *p);
                    p += 8;
                    break;
                case cdouble:
                    b.append("", (double&) *p);
                    p += sizeof(double);
                    break;
                case cint:
                    b.append("", static_cast< int >((reinterpret_cast< const PackedDouble& >(*p)).d));
                    p += sizeof(double);
                    break;
                case clong:
                    b.append("", static_cast< long long>((reinterpret_cast< const PackedDouble& >(*p)).d));
                    p += sizeof(double);
                    break;
                default:
                    verify(false);
            }

            if( (bits & cHASMORE) == 0 )
                break;
        }
        return b.obj();
    }
Example #12
0
bsoncxx::document::view scoped_bson_t::view() {
    return _is_initialized ? bsoncxx::document::view(bson_get_data(bson()), bson()->len)
           : bsoncxx::document::view();
}