本文整理汇总了C++中BSONElementSet::begin方法的典型用法代码示例。如果您正苦于以下问题:C++ BSONElementSet::begin方法的具体用法?C++ BSONElementSet::begin怎么用?C++ BSONElementSet::begin使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BSONElementSet
的用法示例。
在下文中一共展示了BSONElementSet::begin方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: _match
bool AllMatchExpression::_match( const BSONElementSet& all ) const {
if ( all.size() == 0 )
return _arrayEntries.singleNull();
const BSONElementSet& equalities = _arrayEntries.equalities();
for ( BSONElementSet::const_iterator i = equalities.begin(); i != equalities.end(); ++i ) {
BSONElement foo = *i;
if ( all.count( foo ) == 0 )
return false;
}
for ( size_t i = 0; i < _arrayEntries.numRegexes(); i++ ) {
bool found = false;
for ( BSONElementSet::const_iterator j = all.begin(); j != all.end(); ++j ) {
BSONElement bar = *j;
if ( _arrayEntries.regex(i)->matchesSingleElement( bar ) ) {
found = true;
break;
}
}
if ( ! found )
return false;
}
return true;
}
示例2: uassert
// Get the index keys for elements that are GeoJSON.
void S2AccessMethod::getGeoKeys(const BSONElementSet& elements, BSONObjSet* out) const {
for (BSONElementSet::iterator i = elements.begin(); i != elements.end(); ++i) {
uassert(16754, "Can't parse geometry from element: " + i->toString(),
i->isABSONObj());
const BSONObj &obj = i->Obj();
vector<string> cells;
bool succeeded = S2SearchUtil::getKeysForObject(obj, _params, &cells);
uassert(16755, "Can't extract geo keys from object, malformed geometry?:"
+ obj.toString(), succeeded);
uassert(16756, "Unable to generate keys for (likely malformed) geometry: "
+ obj.toString(),
cells.size() > 0);
for (vector<string>::const_iterator it = cells.begin(); it != cells.end(); ++it) {
BSONObjBuilder b;
b.append("", *it);
out->insert(b.obj());
}
}
if (0 == out->size()) {
BSONObjBuilder b;
b.appendNull("");
out->insert(b.obj());
}
}
示例3: getKeys
void getKeys( const BSONObj &obj, BSONObjSet &keys ) const {
BSONElement loc = obj.getFieldDotted( _geo );
if ( loc.eoo() )
return;
uassert( 13323 , "latlng not an array" , loc.isABSONObj() );
string root;
{
BSONObjIterator i( loc.Obj() );
BSONElement x = i.next();
BSONElement y = i.next();
root = makeString( hash(x) , hash(y) );
}
verify( _other.size() == 1 );
BSONElementSet all;
obj.getFieldsDotted( _other[0] , all );
if ( all.size() == 0 ) {
_add( obj , root , BSONElement() , keys );
}
else {
for ( BSONElementSet::iterator i=all.begin(); i!=all.end(); ++i ) {
_add( obj , root , *i , keys );
}
}
}
示例4: getLiteralKeys
// elements is a non-geo field. Add the values literally, expanding arrays.
void getLiteralKeys(const BSONElementSet &elements, BSONObjSet *out) const {
if (0 == elements.size()) {
BSONObjBuilder b;
b.appendNull("");
out->insert(b.obj());
} else if (1 == elements.size()) {
BSONObjBuilder b;
b.appendAs(*elements.begin(), "");
out->insert(b.obj());
} else {
BSONArrayBuilder aBuilder;
for (BSONElementSet::iterator i = elements.begin(); i != elements.end(); ++i) {
aBuilder.append(*i);
}
BSONObjBuilder b;
b.append("", aBuilder.arr());
out->insert(b.obj());
}
}
示例5: getLiteralKeys
// elements is a non-geo field. Add the values literally, expanding arrays.
void getLiteralKeys(const BSONElementSet &elements, BSONObjSet *out) const {
if (0 == elements.size()) {
// Missing fields are indexed as null.
BSONObjBuilder b;
b.appendNull("");
out->insert(b.obj());
} else {
for (BSONElementSet::iterator i = elements.begin(); i != elements.end(); ++i) {
getOneLiteralKey(*i, out);
}
}
}
示例6: matches
bool AllElemMatchOp::matches( const BSONObj& doc, MatchDetails* details ) const {
BSONElementSet all;
doc.getFieldsDotted( _path, all, false );
for ( BSONElementSet::const_iterator i = all.begin(); i != all.end(); ++i ) {
BSONElement sub = *i;
if ( sub.type() != Array )
continue;
if ( _allMatch( sub.Obj() ) ) {
return true;
}
}
return false;
}
示例7: getHaystackKeys
// static
void ExpressionKeysPrivate::getHaystackKeys(const BSONObj& obj,
const std::string& geoField,
const std::vector<std::string>& otherFields,
double bucketSize,
BSONObjSet* keys) {
BSONElement loc = obj.getFieldDotted(geoField);
if (loc.eoo()) {
return;
}
// NOTE: We explicitly test nFields >= 2 to support legacy users who may have indexed
// (intentionally or unintentionally) objects/arrays with more than two fields.
uassert(16775,
str::stream() << "cannot extract [lng, lat] array or object from " << obj,
loc.isABSONObj() && loc.Obj().nFields() >= 2);
string root;
{
BSONObjIterator i(loc.Obj());
BSONElement x = i.next();
BSONElement y = i.next();
root = makeHaystackString(hashHaystackElement(x, bucketSize),
hashHaystackElement(y, bucketSize));
}
verify(otherFields.size() == 1);
BSONElementSet all;
// This is getFieldsDotted (plural not singular) since the object we're indexing
// may be an array.
obj.getFieldsDotted(otherFields[0], all);
if (all.size() == 0) {
// We're indexing a document that doesn't have the secondary non-geo field present.
// XXX: do we want to add this even if all.size() > 0? result:empty search terms
// match everything instead of only things w/empty search terms)
addKey(root, BSONElement(), keys);
} else {
// Ex:If our secondary field is type: "foo" or type: {a:"foo", b:"bar"},
// all.size()==1. We can query on the complete field.
// Ex: If our secondary field is type: ["A", "B"] all.size()==2 and all has values
// "A" and "B". The query looks for any of the fields in the array.
for (BSONElementSet::iterator i = all.begin(); i != all.end(); ++i) {
addKey(root, *i, keys);
}
}
}
示例8: parent
// Get the index keys for elements that are GeoJSON.
void S2AccessMethod::getGeoKeys(const BSONElementSet& elements, BSONObjSet* out) const {
S2RegionCoverer coverer;
_params.configureCoverer(&coverer);
// See here for GeoJSON format: geojson.org/geojson-spec.html
for (BSONElementSet::iterator i = elements.begin(); i != elements.end(); ++i) {
uassert(16754, "Can't parse geometry from element: " + i->toString(),
i->isABSONObj());
const BSONObj &obj = i->Obj();
vector<string> cells;
S2Polyline line;
S2Cell point;
// We only support GeoJSON polygons. Why?:
// 1. we don't automagically do WGS84/flat -> WGS84, and
// 2. the old polygon format must die.
if (GeoParser::isGeoJSONPolygon(obj)) {
S2Polygon polygon;
GeoParser::parseGeoJSONPolygon(obj, &polygon);
keysFromRegion(&coverer, polygon, &cells);
} else if (GeoParser::parseLineString(obj, &line)) {
keysFromRegion(&coverer, line, &cells);
} else if (GeoParser::parsePoint(obj, &point)) {
S2CellId parent(point.id().parent(_params.finestIndexedLevel));
cells.push_back(parent.toString());
} else {
uasserted(16755, "Can't extract geo keys from object, malformed geometry?:"
+ obj.toString());
}
uassert(16756, "Unable to generate keys for (likely malformed) geometry: "
+ obj.toString(),
cells.size() > 0);
for (vector<string>::const_iterator it = cells.begin(); it != cells.end(); ++it) {
BSONObjBuilder b;
b.append("", *it);
out->insert(b.obj());
}
}
if (0 == out->size()) {
BSONObjBuilder b;
b.appendNull("");
out->insert(b.obj());
}
}
示例9: uassert
// This is the actual search.
bool S2Cursor::advance() {
if (_numToReturn <= 0) { return false; }
for (; _btreeCursor->ok(); _btreeCursor->advance()) {
++_nscanned;
if (_seen.end() != _seen.find(_btreeCursor->currLoc())) { continue; }
_seen.insert(_btreeCursor->currLoc());
++_matchTested;
MatchDetails details;
bool matched = _matcher->matchesCurrent(_btreeCursor.get(), &details);
if (!matched) { continue; }
const BSONObj &indexedObj = _btreeCursor->currLoc().obj();
++_geoTested;
size_t geoFieldsMatched = 0;
// OK, cool, non-geo match satisfied. See if the object actually overlaps w/the geo
// query fields.
for (size_t i = 0; i < _fields.size(); ++i) {
BSONElementSet geoFieldElements;
indexedObj.getFieldsDotted(_fields[i].getField(), geoFieldElements, false);
if (geoFieldElements.empty()) { continue; }
bool match = false;
for (BSONElementSet::iterator oi = geoFieldElements.begin();
!match && (oi != geoFieldElements.end()); ++oi) {
if (!oi->isABSONObj()) { continue; }
const BSONObj &geoObj = oi->Obj();
GeometryContainer geoContainer;
uassert(16698, "malformed geometry: " + geoObj.toString(),
geoContainer.parseFrom(geoObj));
match = _fields[i].satisfiesPredicate(geoContainer);
}
if (match) { ++geoFieldsMatched; }
}
if (geoFieldsMatched == _fields.size()) {
// We have a winner! And we point at it.
--_numToReturn;
return true;
}
}
return false;
}
示例10: extractGeometries
/**
* Find and parse all geometry elements on the appropriate field path from the document.
*/
static void extractGeometries(const BSONObj& doc,
const string& path,
vector<StoredGeometry*>* geometries) {
BSONElementSet geomElements;
// NOTE: Annoyingly, we cannot just expand arrays b/c single 2d points are arrays, we need
// to manually expand all results to check if they are geometries
doc.getFieldsDotted(path, geomElements, false /* expand arrays */);
for (BSONElementSet::iterator it = geomElements.begin(); it != geomElements.end(); ++it) {
const BSONElement& el = *it;
auto_ptr<StoredGeometry> stored(StoredGeometry::parseFrom(el));
if (stored.get()) {
// Valid geometry element
geometries->push_back(stored.release());
}
else if (el.type() == Array) {
// Many geometries may be in an array
BSONObjIterator arrIt(el.Obj());
while (arrIt.more()) {
const BSONElement nextEl = arrIt.next();
stored.reset(StoredGeometry::parseFrom(nextEl));
if (stored.get()) {
// Valid geometry element
geometries->push_back(stored.release());
}
else {
warning() << "geoNear stage read non-geometry element " << nextEl.toString()
<< " in array " << el.toString();
}
}
}
else {
warning() << "geoNear stage read non-geometry element " << el.toString();
}
}
}
示例11: getGeoKeys
// Get the index keys for elements that are GeoJSON.
void getGeoKeys(const BSONElementSet &elements, BSONObjSet *out) const {
S2RegionCoverer coverer;
_params.configureCoverer(&coverer);
// See here for GeoJSON format: geojson.org/geojson-spec.html
for (BSONElementSet::iterator i = elements.begin(); i != elements.end(); ++i) {
if (!i->isABSONObj()) { continue; } // error?
const BSONObj &obj = i->Obj();
vector<string> cells;
S2Polygon polygon;
S2Polyline line;
S2Cell point;
if (GeoParser::parsePolygon(obj, &polygon)) {
keysFromRegion(&coverer, polygon, &cells);
} else if (GeoParser::parseLineString(obj, &line)) {
keysFromRegion(&coverer, line, &cells);
} else if (GeoParser::parsePoint(obj, &point)) {
keysFromRegion(&coverer, point, &cells);
} else {
uasserted(16572, "Can't extract geo keys from object, malformed geometry?:"
+ obj.toString());
}
for (vector<string>::const_iterator it = cells.begin(); it != cells.end(); ++it) {
BSONObjBuilder b;
b.append("", *it);
out->insert(b.obj());
}
}
if (0 == out->size()) {
BSONObjBuilder b;
b.appendNull("");
out->insert(b.obj());
}
}
示例12: getKeys
void getKeys(const BSONObj &obj, BSONObjSet &keys) const {
BSONElement loc = obj.getFieldDotted(_geoField);
if (loc.eoo())
return;
uassert(13323, "latlng not an array", loc.isABSONObj());
string root;
{
BSONObjIterator i(loc.Obj());
BSONElement x = i.next();
BSONElement y = i.next();
root = makeString(hash(x), hash(y));
}
verify(_otherFields.size() == 1);
BSONElementSet all;
// This is getFieldsDotted (plural not singular) since the object we're indexing
// may be an array.
obj.getFieldsDotted(_otherFields[0], all);
if (all.size() == 0) {
// We're indexing a document that doesn't have the secondary non-geo field present.
// XXX: do we want to add this even if all.size() > 0? result:empty search terms
// match everything instead of only things w/empty search terms)
addKey(root, BSONElement(), keys);
} else {
// Ex:If our secondary field is type: "foo" or type: {a:"foo", b:"bar"},
// all.size()==1. We can query on the complete field.
// Ex: If our secondary field is type: ["A", "B"] all.size()==2 and all has values
// "A" and "B". The query looks for any of the fields in the array.
for (BSONElementSet::iterator i = all.begin(); i != all.end(); ++i) {
addKey(root, *i, keys);
}
}
}
示例13: run
bool run(const string& dbname, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool fromRepl ){
string ns = dbname + '.' + cmdObj.firstElement().valuestr();
string key = cmdObj["key"].valuestrsafe();
BSONObj keyPattern = BSON( key << 1 );
BSONObj query = getQuery( cmdObj );
BSONElementSet values;
shared_ptr<Cursor> cursor = bestGuessCursor(ns.c_str() , query , BSONObj() );
scoped_ptr<ClientCursor> cc (new ClientCursor(QueryOption_NoCursorTimeout, cursor, ns));
while ( cursor->ok() ){
if ( !cursor->matcher() || cursor->matcher()->matchesCurrent( cursor.get() ) ){
BSONObj o = cursor->current();
o.getFieldsDotted( key, values );
}
cursor->advance();
if (!cc->yieldSometimes())
break;
RARELY killCurrentOp.checkForInterrupt();
}
BSONArrayBuilder b( result.subarrayStart( "values" ) );
for ( BSONElementSet::iterator i = values.begin() ; i != values.end(); i++ ){
b.append( *i );
}
BSONObj arr = b.done();
uassert(10044, "distinct too big, 4mb cap", arr.objsize() < BSONObjMaxUserSize );
return true;
}
示例14: frs
// Fill _results with all of the results in the annulus defined by _innerRadius and
// _outerRadius. If no results are found, grow the annulus and repeat until success (or
// until the edge of the world).
void S2NearIndexCursor::fillResults() {
verify(_results.empty());
if (_innerRadius >= _outerRadius) {
return;
}
if (_innerRadius > _maxDistance) {
return;
}
// We iterate until 1. our search radius is too big or 2. we find results.
do {
// Some of these arguments are opaque, look at the definitions of the involved classes.
FieldRangeSet frs(_descriptor->parentNS().c_str(), makeFRSObject(), false, false);
shared_ptr<FieldRangeVector> frv(new FieldRangeVector(frs, _specForFRV, 1));
scoped_ptr<BtreeCursor> cursor(BtreeCursor::make(nsdetails(_descriptor->parentNS()),
_descriptor->getOnDisk(), frv, 0, 1));
// The cursor may return the same obj more than once for a given
// FRS, so we make sure to only consider it once in any given annulus.
//
// We don't want this outside of the 'do' loop because the covering
// for an annulus may return an object whose distance to the query
// point is actually contained in a subsequent annulus. If we
// didn't consider every object in a given annulus we might miss
// the point.
//
// We don't use a global 'seen' because we get that by requiring
// the distance from the query point to the indexed geo to be
// within our 'current' annulus, and I want to dodge all yield
// issues if possible.
unordered_set<DiskLoc, DiskLoc::Hasher> seen;
LOG(1) << "looking at annulus from " << _innerRadius << " to " << _outerRadius << endl;
LOG(1) << "Total # returned: " << _stats._numReturned << endl;
// Do the actual search through this annulus.
for (; cursor->ok(); cursor->advance()) {
// Don't bother to look at anything we've returned.
if (_returned.end() != _returned.find(cursor->currLoc())) {
++_stats._returnSkip;
continue;
}
++_stats._nscanned;
if (seen.end() != seen.find(cursor->currLoc())) {
++_stats._btreeDups;
continue;
}
// Get distance interval from our query point to the cell.
// If it doesn't overlap with our current shell, toss.
BSONObj currKey(cursor->currKey());
BSONObjIterator it(currKey);
BSONElement geoKey;
for (int i = 0; i <= _nearFieldIndex; ++i) {
geoKey = it.next();
}
S2Cell keyCell = S2Cell(S2CellId::FromString(geoKey.String()));
if (!_annulus.MayIntersect(keyCell)) {
++_stats._keyGeoSkip;
continue;
}
// We have to add this document to seen *AFTER* the key intersection test.
// A geometry may have several keys, one of which may be in our search shell and one
// of which may be outside of it. We don't want to ignore a document just because
// one of its covers isn't inside this annulus.
seen.insert(cursor->currLoc());
// At this point forward, we will not examine the document again in this annulus.
const BSONObj& indexedObj = cursor->currLoc().obj();
// Match against indexed geo fields.
++_stats._geoMatchTested;
size_t geoFieldsMatched = 0;
// See if the object actually overlaps w/the geo query fields.
for (size_t i = 0; i < _indexedGeoFields.size(); ++i) {
BSONElementSet geoFieldElements;
indexedObj.getFieldsDotted(_indexedGeoFields[i].getField(), geoFieldElements,
false);
if (geoFieldElements.empty()) {
continue;
}
bool match = false;
for (BSONElementSet::iterator oi = geoFieldElements.begin();
!match && (oi != geoFieldElements.end()); ++oi) {
if (!oi->isABSONObj()) {
continue;
}
const BSONObj &geoObj = oi->Obj();
GeometryContainer geoContainer;
uassert(16762, "ill-formed geometry: " + geoObj.toString(),
geoContainer.parseFrom(geoObj));
match = _indexedGeoFields[i].satisfiesPredicate(geoContainer);
//.........这里部分代码省略.........
示例15: run
bool run(const string& dbname, BSONObj& cmdObj, int, string& errmsg, BSONObjBuilder& result, bool fromRepl ) {
Timer t;
string ns = dbname + '.' + cmdObj.firstElement().valuestr();
string key = cmdObj["key"].valuestrsafe();
BSONObj keyPattern = BSON( key << 1 );
BSONObj query = getQuery( cmdObj );
int bufSize = BSONObjMaxUserSize - 4096;
BufBuilder bb( bufSize );
char * start = bb.buf();
BSONArrayBuilder arr( bb );
BSONElementSet values;
long long nscanned = 0; // locations looked at
long long nscannedObjects = 0; // full objects looked at
long long n = 0; // matches
MatchDetails md;
Collection *cl = getCollection( ns );
if ( ! cl ) {
result.appendArray( "values" , BSONObj() );
result.append( "stats" , BSON( "n" << 0 << "nscanned" << 0 << "nscannedObjects" << 0 ) );
return true;
}
shared_ptr<Cursor> cursor;
if ( ! query.isEmpty() ) {
cursor = getOptimizedCursor(ns.c_str() , query , BSONObj() );
}
else {
// query is empty, so lets see if we can find an index
// with the key so we don't have to hit the raw data
for (int i = 0; i < cl->nIndexes(); i++) {
IndexDetails &idx = cl->idx(i);
if (cl->isMultikey(i)) {
continue;
}
if ( idx.inKeyPattern( key ) ) {
cursor = getBestGuessCursor( ns.c_str() ,
BSONObj() ,
idx.keyPattern() );
if( cursor.get() ) break;
}
}
if ( ! cursor.get() ) {
cursor = getOptimizedCursor(ns.c_str() , query , BSONObj() );
}
}
verify( cursor );
string cursorName = cursor->toString();
auto_ptr<ClientCursor> cc (new ClientCursor(QueryOption_NoCursorTimeout, cursor, ns));
for ( ; cursor->ok(); cursor->advance() ) {
nscanned++;
bool loadedRecord = false;
if ( cursor->currentMatches( &md ) && !cursor->getsetdup( cursor->currPK() ) ) {
n++;
BSONObj holder;
BSONElementSet temp;
loadedRecord = ! cc->getFieldsDotted( key , temp, holder );
for ( BSONElementSet::iterator i=temp.begin(); i!=temp.end(); ++i ) {
BSONElement e = *i;
if ( values.count( e ) )
continue;
int now = bb.len();
uassert(10044, "distinct too big, 16mb cap", ( now + e.size() + 1024 ) < bufSize );
arr.append( e );
BSONElement x( start + now );
values.insert( x );
}
}
if ( loadedRecord || md.hasLoadedRecord() )
nscannedObjects++;
RARELY killCurrentOp.checkForInterrupt();
}
verify( start == bb.buf() );
result.appendArray( "values" , arr.done() );
//.........这里部分代码省略.........