Example #1
0
	void MongoSchema::process(){
		//std::cout << "Processing " << m_dbname << "." << m_col << std::endl;
		std::string querystr;
		querystr.clear();
		querystr.append(m_dbname);
		querystr.append(".");
		querystr.append(m_col);
		int recordscount = m_conn->count(querystr);
		
		//std::cout << "count:" <<  recordscount << std::endl;
		
		std::auto_ptr<mongo::DBClientCursor> cursor = m_conn->query(querystr, mongo::Query());
		//std::set<std::string> fields;
		while(cursor->more()){
			mongo::BSONObj bo = cursor->next();
			
			for( BSONObj::iterator i = bo.begin(); i.more(); ) { 
				BSONElement e = i.next();
				if(skipField(e.fieldName())){
					continue;
				}
				
				if(e.isSimpleType()){
					hashmap::const_iterator keyexsit = m_map.find(e.fieldName());
					SchemaModel* sm = new SchemaModel();
					if(keyexsit != m_map.end()){
							sm = &m_map[e.fieldName()];
							sm->count ++;
							
					}else{
							sm->count = 1;
							sm->datatype = getType(e);
							m_map[e.fieldName()] = *sm; 
					}
				}else if(e.isABSONObj()){
					int depth = 0;
					std::string parent = e.fieldName();
					extractBSON(e.Obj(), depth, parent);
				}
				
			}			
		}
		BSONObjBuilder bOb;
		BSONArrayBuilder bArr;
		std::tr1::hash<std::string> hashfunc = m_map.hash_function();
		for( hashmap::const_iterator i = m_map.begin(), e = m_map.end() ; i != e ; ++i ) {
			    SchemaModel sm = i->second;
				float percentage = (float)sm.count / (float)recordscount * 100.0;
				std::cout.precision(4);
				BSONObj bo = BSON( "field" << i->first << "percent" << percentage << "datatype" << sm.datatype );
				bArr.append(bo);
		        //std::cout << i->first << " -> "<< "Percent: "<< percentage << " (hash = " << hashfunc( i->first ) << ")" << "\r\n";
		}
		bOb.append(m_col, bArr.arr());
		m_schema = bOb.obj();
	}
/* ****************************************************************************
*
* compoundObjectResponse -
*/
void compoundObjectResponse(orion::CompoundValueNode* cvP, const BSONElement& be)
{
  BSONObj obj = be.embeddedObject();

  cvP->valueType = orion::ValueTypeObject;
  for (BSONObj::iterator i = obj.begin(); i.more();)
  {
    BSONElement e = i.next();
    addCompoundNode(cvP, e);
  }
}
Example #3
0
 BSONObj ls(const BSONObj& args) { 
     BSONObj o = listFiles(args);
     if( !o.isEmpty() ) {
         for( BSONObj::iterator i = o.firstElement().Obj().begin(); i.more(); ) { 
             BSONObj f = i.next().Obj();
             cout << f["name"].String();
             if( f["isDirectory"].trueValue() ) cout << '/';
             cout << '\n';
         }
         cout.flush();
     }
     return BSONObj();
 }
void MongoListModel::mongoHeader() const
{
    BSONObj objOne = mongoDatabase->findOne("mongolab_database.random_data",Query());
    mongoHeaderDataList.clear();
    for ( BSONObj::iterator iter = objOne.begin(); iter.more(); )
    {
        BSONElement element = iter.next();
        QString key = QString::fromStdString( element.fieldName() );
        if ( key == "_id" ) continue;
        mongoHeaderDataList.append(key);

    }
}
BSONObj ls(const BSONObj& args, void* data) {
    BSONArrayBuilder ret;
    BSONObj o = listFiles(args, data);
    if (!o.isEmpty()) {
        for (BSONObj::iterator i = o.firstElement().Obj().begin(); i.more();) {
            BSONObj f = i.next().Obj();
            string name = f["name"].String();
            if (f["isDirectory"].trueValue()) {
                name += '/';
            }
            ret << name;
        }
    }
    return BSON("" << ret.arr());
}
Example #6
0
int Meta2ServerInfo(const TMeta &m, ServerInfo &si)
{
	if(m.data.empty()){
		log(Warn, "meta id:%lld Meta2ServerInfo meta.data empty", m.serverId);
		return -1;
	}
	BSONObj bo(m.data.c_str());
	size_t idx = 0;
	for( BSONObj::iterator i = bo.begin(); i.more() && idx < sizeof(ServerInfoCodec) / sizeof(CodecPair<ServerInfo>); idx++) {
		BSONElement e = i.next();
		int ret = ServerInfoCodec[idx].codec->decode(&si, e);
		if(ret != 0){
			log(Warn, "decode ServerInfo name:%s error occure", ServerInfoCodec[idx].name);
			return -1;
		}
	}
	return 0;
}
Example #7
0
int unmarSessionData(const std::string &xlog, SessionData *info, bool checkName = true)
{
        BSONObj bo(xlog.c_str());
        size_t idx = 0;
        for( BSONObj::iterator i = bo.begin(); i.more() && idx < sizeof(sessionDataCodec) / sizeof(CodecPair<SessionData>); idx++) {
                BSONElement e = i.next();
                if(checkName){
                        if(strcmp(e.fieldName(), sessionDataCodec[idx].name) != 0){
                                myerrno = EDECFIELDMISS;
                                return -1;
                        }
                }
                int ret = sessionDataCodec[idx].codec->decode(info, e);
                if(ret != 0){
                        myerrno = EDECBJSONERR;
                        return -1;
                }
        }
        return 0;
}
/** Restore transforms from database.
 * @param start_msec start time of range to restore since the epoch in msec
 * @param end_msec end time of range to restore since the epoch in msec
 * @param new_start_msec the new start time since the epoch in msec to which the
 * transform times will be reset, i.e. from the transforms time stamp the
 * @p start time is subtracted and @p new_start is added.
 */
void
MongoDBTransformer::restore(long long start_msec, long long end_msec, long long new_start_msec)
{
  cache_time_ = (double)(end_msec - start_msec) / 1000.;

  if (new_start_msec == 0) {
    new_start_msec = start_msec;
  }

  std::list<std::string> collections =
    mongodb_client_->getCollectionNames(database_);
  
  std::auto_ptr<DBClientCursor> cursor;
  BSONObj doc;
  std::list<std::string>::iterator c;
  for (c = collections.begin(); c != collections.end(); ++c) {
    if ((c->find(database_ + ".TransformInterface.") != 0 ) &&
	(c->find(database_ + ".tf") != 0) )
    {
      continue;
    }

    cursor = mongodb_client_->query(*c,
	     QUERY("timestamp" << GTE << start_msec << LT << end_msec).sort("timestamp"));

    while (cursor->more()) {
      doc = cursor->next();
      if (doc.hasField("transforms")) {
	// multi transforms document
	BSONObj::iterator i = doc.getObjectField("transforms").begin();
	while (i.more()) {
	  BSONElement e = i.next();
	  BSONObj o = e.Obj();
	  restore_tf_doc(o, start_msec, new_start_msec);
	}
     } else {
	restore_tf_doc(doc, start_msec, new_start_msec);
      }
    }
  }
}
Example #9
0
	int MongoSchema::extractBSON(mongo::BSONObj bo, int& depth, std::string parent){

		if(depth >= m_depth){
			return 0;
		}
		depth++;
		
		for( BSONObj::iterator i = bo.begin(); i.more(); ) { 
			BSONElement e = i.next();
			
			if(skipField(e.fieldName())){
					continue;
				}

			std::string fieldname = parent ;
			fieldname.append(".");
			fieldname.append(e.fieldName());
			if(e.isSimpleType()){
					hashmap::const_iterator keyexsit = m_map.find(fieldname);
					SchemaModel* sm = new SchemaModel();
					if(keyexsit != m_map.end()){
							sm = &m_map[fieldname];
							sm->count ++;
							
					}else{
							sm->count = 1;
							sm->datatype = getType(e);
							m_map[fieldname] = *sm; 
					}
			 }else if(e.isABSONObj()){
					
					extractBSON(e.Obj(), depth, fieldname);
			}
				
		}
		
		return 0;		
	}
Example #10
0
    intrusive_ptr<Pipeline> Pipeline::parseCommand(
        string &errmsg, BSONObj &cmdObj,
        const intrusive_ptr<ExpressionContext> &pCtx) {
        intrusive_ptr<Pipeline> pPipeline(new Pipeline(pCtx));
        vector<BSONElement> pipeline;

        /* gather the specification for the aggregation */
        for(BSONObj::iterator cmdIterator = cmdObj.begin();
                cmdIterator.more(); ) {
            BSONElement cmdElement(cmdIterator.next());
            const char *pFieldName = cmdElement.fieldName();

            // ignore top-level fields prefixed with $. They are for the command processor, not us.
            if (pFieldName[0] == '$') {
                continue;
            }

            /* look for the aggregation command */
            if (!strcmp(pFieldName, commandName)) {
                pPipeline->collectionName = cmdElement.String();
                continue;
            }

            /* check for the collection name */
            if (!strcmp(pFieldName, pipelineName)) {
                pipeline = cmdElement.Array();
                continue;
            }

            /* check for explain option */
            if (!strcmp(pFieldName, explainName)) {
                pPipeline->explain = cmdElement.Bool();
                continue;
            }

            /* if the request came from the router, we're in a shard */
            if (!strcmp(pFieldName, fromRouterName)) {
                pCtx->setInShard(cmdElement.Bool());
                continue;
            }

            /* check for debug options */
            if (!strcmp(pFieldName, splitMongodPipelineName)) {
                pPipeline->splitMongodPipeline = true;
                continue;
            }

            /* we didn't recognize a field in the command */
            ostringstream sb;
            sb <<
               "unrecognized field \"" <<
               cmdElement.fieldName();
            errmsg = sb.str();
            return intrusive_ptr<Pipeline>();
        }

        /*
          If we get here, we've harvested the fields we expect for a pipeline.

          Set up the specified document source pipeline.
        */
        SourceContainer& sources = pPipeline->sources; // shorthand

        /* iterate over the steps in the pipeline */
        const size_t nSteps = pipeline.size();
        for(size_t iStep = 0; iStep < nSteps; ++iStep) {
            /* pull out the pipeline element as an object */
            BSONElement pipeElement(pipeline[iStep]);
            uassert(15942, str::stream() << "pipeline element " <<
                    iStep << " is not an object",
                    pipeElement.type() == Object);
            BSONObj bsonObj(pipeElement.Obj());

            // Parse a pipeline stage from 'bsonObj'.
            uassert(16435, "A pipeline stage specification object must contain exactly one field.",
                    bsonObj.nFields() == 1);
            BSONElement stageSpec = bsonObj.firstElement();
            const char* stageName = stageSpec.fieldName();

            // Create a DocumentSource pipeline stage from 'stageSpec'.
            StageDesc key;
            key.pName = stageName;
            const StageDesc* pDesc = (const StageDesc*)
                    bsearch(&key, stageDesc, nStageDesc, sizeof(StageDesc),
                            stageDescCmp);

            uassert(16436,
                    str::stream() << "Unrecognized pipeline stage name: '" << stageName << "'",
                    pDesc);
            intrusive_ptr<DocumentSource> stage = (*pDesc->pFactory)(&stageSpec, pCtx);
            verify(stage);
            stage->setPipelineStep(iStep);
            sources.push_back(stage);
        }

        /* if there aren't any pipeline stages, there's nothing more to do */
        if (sources.empty())
            return pPipeline;

        /*
          Move filters up where possible.

          CW TODO -- move filter past projections where possible, and noting
          corresponding field renaming.
        */

        /*
          Wherever there is a match immediately following a sort, swap them.
          This means we sort fewer items.  Neither changes the documents in
          the stream, so this transformation shouldn't affect the result.

          We do this first, because then when we coalesce operators below,
          any adjacent matches will be combined.
         */
        for (size_t srcn = sources.size(), srci = 1; srci < srcn; ++srci) {
            intrusive_ptr<DocumentSource> &pSource = sources[srci];
            if (dynamic_cast<DocumentSourceMatch *>(pSource.get())) {
                intrusive_ptr<DocumentSource> &pPrevious = sources[srci - 1];
                if (dynamic_cast<DocumentSourceSort *>(pPrevious.get())) {
                    /* swap this item with the previous */
                    intrusive_ptr<DocumentSource> pTemp(pPrevious);
                    pPrevious = pSource;
                    pSource = pTemp;
                }
            }
        }

        /* Move limits in front of skips. This is more optimal for sharding
         * since currently, we can only split the pipeline at a single source
         * and it is better to limit the results coming from each shard
         */
        for(int i = sources.size() - 1; i >= 1 /* not looking at 0 */; i--) {
            DocumentSourceLimit* limit =
                dynamic_cast<DocumentSourceLimit*>(sources[i].get());
            DocumentSourceSkip* skip =
                dynamic_cast<DocumentSourceSkip*>(sources[i-1].get());
            if (limit && skip) {
                // Increase limit by skip since the skipped docs now pass through the $limit
                limit->setLimit(limit->getLimit() + skip->getSkip());
                swap(sources[i], sources[i-1]);

                // Start at back again. This is needed to handle cases with more than 1 $limit
                // (S means skip, L means limit)
                //
                // These two would work without second pass (assuming back to front ordering)
                // SL   -> LS
                // SSL  -> LSS
                //
                // The following cases need a second pass to handle the second limit
                // SLL  -> LLS
                // SSLL -> LLSS
                // SLSL -> LLSS
                i = sources.size(); // decremented before next pass
            }
        }

        /*
          Coalesce adjacent filters where possible.  Two adjacent filters
          are equivalent to one filter whose predicate is the conjunction of
          the two original filters' predicates.  For now, capture this by
          giving any DocumentSource the option to absorb it's successor; this
          will also allow adjacent projections to coalesce when possible.

          Run through the DocumentSources, and give each one the opportunity
          to coalesce with its successor.  If successful, remove the
          successor.

          Move all document sources to a temporary list.
        */
        SourceContainer tempSources;
        sources.swap(tempSources);

        /* move the first one to the final list */
        sources.push_back(tempSources[0]);

        /* run through the sources, coalescing them or keeping them */
        for (size_t tempn = tempSources.size(), tempi = 1; tempi < tempn; ++tempi) {
            /*
              If we can't coalesce the source with the last, then move it
              to the final list, and make it the new last.  (If we succeeded,
              then we're still on the same last, and there's no need to move
              or do anything with the source -- the destruction of tempSources
              will take care of the rest.)
            */
            intrusive_ptr<DocumentSource> &pLastSource = sources.back();
            intrusive_ptr<DocumentSource> &pTemp = tempSources[tempi];
            verify(pTemp && pLastSource);
            if (!pLastSource->coalesce(pTemp))
                sources.push_back(pTemp);
        }

        /* optimize the elements in the pipeline */
        for(SourceContainer::iterator iter(sources.begin()),
                                      listEnd(sources.end());
                                    iter != listEnd;
                                    ++iter) {
            if (!*iter) {
                errmsg = "Pipeline received empty document as argument";
                return intrusive_ptr<Pipeline>();
            }

            (*iter)->optimize();
        }

        return pPipeline;
    }
Example #11
0
    intrusive_ptr<Pipeline> Pipeline::parseCommand(
        string &errmsg, BSONObj &cmdObj,
        const intrusive_ptr<ExpressionContext> &pCtx) {
        intrusive_ptr<Pipeline> pPipeline(new Pipeline(pCtx));
        vector<BSONElement> pipeline;

        /* gather the specification for the aggregation */
        for(BSONObj::iterator cmdIterator = cmdObj.begin();
                cmdIterator.more(); ) {
            BSONElement cmdElement(cmdIterator.next());
            const char *pFieldName = cmdElement.fieldName();

            /* look for the aggregation command */
            if (!strcmp(pFieldName, commandName)) {
                pPipeline->collectionName = cmdElement.String();
                continue;
            }

            /* check for the collection name */
            if (!strcmp(pFieldName, pipelineName)) {
                pipeline = cmdElement.Array();
                continue;
            }

            /* check for explain option */
            if (!strcmp(pFieldName, explainName)) {
                pPipeline->explain = cmdElement.Bool();
                continue;
            }

            /* if the request came from the router, we're in a shard */
            if (!strcmp(pFieldName, fromRouterName)) {
                pCtx->setInShard(cmdElement.Bool());
                continue;
            }

            /* check for debug options */
            if (!strcmp(pFieldName, splitMongodPipelineName)) {
                pPipeline->splitMongodPipeline = true;
                continue;
            }

            /* Ignore $auth information sent along with the command. The authentication system will
             * use it, it's not a part of the pipeline.
             */
            if (!strcmp(pFieldName, AuthenticationTable::fieldName.c_str())) {
                continue;
            }

            /* we didn't recognize a field in the command */
            ostringstream sb;
            sb <<
               "unrecognized field \"" <<
               cmdElement.fieldName();
            errmsg = sb.str();
            return intrusive_ptr<Pipeline>();
        }

        /*
          If we get here, we've harvested the fields we expect for a pipeline.

          Set up the specified document source pipeline.
        */
        SourceVector *pSourceVector = &pPipeline->sourceVector; // shorthand

        /* iterate over the steps in the pipeline */
        const size_t nSteps = pipeline.size();
        for(size_t iStep = 0; iStep < nSteps; ++iStep) {
            /* pull out the pipeline element as an object */
            BSONElement pipeElement(pipeline[iStep]);
            uassert(15942, str::stream() << "pipeline element " <<
                    iStep << " is not an object",
                    pipeElement.type() == Object);
            BSONObj bsonObj(pipeElement.Obj());

            // Parse a pipeline stage from 'bsonObj'.
            uassert(16435, "A pipeline stage specification object must contain exactly one field.",
                    bsonObj.nFields() == 1);
            BSONElement stageSpec = bsonObj.firstElement();
            const char* stageName = stageSpec.fieldName();

            // Create a DocumentSource pipeline stage from 'stageSpec'.
            StageDesc key;
            key.pName = stageName;
            const StageDesc* pDesc = (const StageDesc*)
                    bsearch(&key, stageDesc, nStageDesc, sizeof(StageDesc),
                            stageDescCmp);

            uassert(16436,
                    str::stream() << "Unrecognized pipeline stage name: '" << stageName << "'",
                    pDesc);
            intrusive_ptr<DocumentSource> stage = (*pDesc->pFactory)(&stageSpec, pCtx);
            verify(stage);
            stage->setPipelineStep(iStep);
            pSourceVector->push_back(stage);
        }

        /* if there aren't any pipeline stages, there's nothing more to do */
        if (!pSourceVector->size())
            return pPipeline;

        /*
          Move filters up where possible.

          CW TODO -- move filter past projections where possible, and noting
          corresponding field renaming.
        */

        /*
          Wherever there is a match immediately following a sort, swap them.
          This means we sort fewer items.  Neither changes the documents in
          the stream, so this transformation shouldn't affect the result.

          We do this first, because then when we coalesce operators below,
          any adjacent matches will be combined.
         */
        for(size_t srcn = pSourceVector->size(), srci = 1;
            srci < srcn; ++srci) {
            intrusive_ptr<DocumentSource> &pSource = pSourceVector->at(srci);
            if (dynamic_cast<DocumentSourceMatch *>(pSource.get())) {
                intrusive_ptr<DocumentSource> &pPrevious =
                    pSourceVector->at(srci - 1);
                if (dynamic_cast<DocumentSourceSort *>(pPrevious.get())) {
                    /* swap this item with the previous */
                    intrusive_ptr<DocumentSource> pTemp(pPrevious);
                    pPrevious = pSource;
                    pSource = pTemp;
                }
            }
        }

        /*
          Coalesce adjacent filters where possible.  Two adjacent filters
          are equivalent to one filter whose predicate is the conjunction of
          the two original filters' predicates.  For now, capture this by
          giving any DocumentSource the option to absorb it's successor; this
          will also allow adjacent projections to coalesce when possible.

          Run through the DocumentSources, and give each one the opportunity
          to coalesce with its successor.  If successful, remove the
          successor.

          Move all document sources to a temporary list.
        */
        SourceVector tempVector(*pSourceVector);
        pSourceVector->clear();

        /* move the first one to the final list */
        pSourceVector->push_back(tempVector[0]);

        /* run through the sources, coalescing them or keeping them */
        for(size_t tempn = tempVector.size(), tempi = 1;
            tempi < tempn; ++tempi) {
            /*
              If we can't coalesce the source with the last, then move it
              to the final list, and make it the new last.  (If we succeeded,
              then we're still on the same last, and there's no need to move
              or do anything with the source -- the destruction of tempVector
              will take care of the rest.)
            */
            intrusive_ptr<DocumentSource> &pLastSource = pSourceVector->back();
            intrusive_ptr<DocumentSource> &pTemp = tempVector.at(tempi);
            verify(pTemp && pLastSource);
            if (!pLastSource->coalesce(pTemp))
                pSourceVector->push_back(pTemp);
        }

        /* optimize the elements in the pipeline */
        for(SourceVector::iterator iter(pSourceVector->begin()),
                listEnd(pSourceVector->end()); iter != listEnd; ++iter) {
            if (!*iter) {
                errmsg = "Pipeline received empty document as argument";
                return intrusive_ptr<Pipeline>();
            }

            (*iter)->optimize();
        }

        return pPipeline;
    }