Example #1
0
INT32 _msgBuffer::write( const bson::BSONObj &obj, BOOLEAN align, INT32 bytes )
{
   INT32 rc   = SDB_OK ;
   INT32 size = 0 ;        // new size to realloc
   INT32 num  = 0 ;        // number of memory block
   UINT32 objsize = obj.objsize() ;
   if( objsize > _capacity - _size )
   {
      num = ( objsize + _size ) / MEMERY_BLOCK_SIZE + 1 ;
      size = num * MEMERY_BLOCK_SIZE ;

      rc = realloc( _data, size ) ;
      if( SDB_OK != rc )
      {
         goto error ;
      }
   }

   ossMemcpy( _data + _size, obj.objdata(), objsize ) ;
   if ( align )
   {
      _size += ossRoundUpToMultipleX( objsize, bytes ) ;
   }
   else
   {
      _size += objsize ;
   }

done:
   return rc ;
error:
   goto done ;
}
Example #2
0
bson::BSONObj getCondObj( const bson::BSONObj& all )
{
    bson::BSONObj cond ;
    const char* cmdStr = all.firstElementFieldName() ;
    if ( all.hasField( "$query" ) )
    {
        cond = all.getObjectField( "$query" ) ;
    }
    else if ( all.hasField( "query" ) )
    {
        cond = all.getObjectField( "query" ) ;
    }
    else
    {
        cond = all ;
    }

    return cond ;
}
 qgmPlMthMatcherScan::qgmPlMthMatcherScan( const qgmDbAttr &collection,
                                           const qgmOPFieldVec &selector,
                                           const bson::BSONObj &orderby,
                                           const bson::BSONObj &hint,
                                           INT64 numSkip,
                                           INT64 numReturn,
                                           const qgmField &alias,
                                           const bson::BSONObj &matcher )
 : _qgmPlScan( collection, selector, orderby, hint, numSkip, numReturn,
             alias, NULL )
 {
    _condition = matcher.copy();
 }
Example #4
0
   // PD_TRACE_DECLARE_FUNCTION ( SDB__MTHSELECTOR_SELECT, "_mthSelector::select" )
   INT32 _mthSelector::select( const bson::BSONObj &source,
                               bson::BSONObj &target )
   {
      INT32 rc = SDB_OK ;
      PD_TRACE_ENTRY( SDB__MTHSELECTOR_SELECT ) ;
      SDB_ASSERT(_init, "The selector has not been initialized, please "
                         "call 'loadPattern' before using it" ) ;
      BSONObj obj ;
      if ( !_init )
      {
         target = source.copy() ;
         goto done ;
      }

      rc = _matrix.select( source, obj ) ;
      if ( SDB_OK != rc )
      {
         PD_LOG( PDERROR, "failed to select columns:%d", rc ) ;
         goto error ;
      }

      if ( !_stringOutput )
      {
         target = obj ;
      }
      else
      {
         BSONObj resorted ;
         rc = _resortObj( _matrix.getPattern(),
                          obj,
                          resorted ) ;
         if ( SDB_OK != rc )
         {
            PD_LOG( PDERROR, "failed to resort obj:%d", rc ) ;
            goto error ;
         }

         rc = _buildCSV( resorted, target ) ;
         if ( SDB_OK != rc )
         {
            PD_LOG( PDERROR, "failed to build csv:%d", rc ) ;
            goto error ;
         }
      }
   done:
      PD_TRACE_EXITRC( SDB__MTHSELECTOR_SELECT, rc ) ;
      return rc ;
   error:
      goto done ;
   }
Example #5
0
   // PD_TRACE_DECLARE_FUNCTION ( SDB__MTHSELECTOR__RESORTOBJ, "_mthSelector::_resortObj" )
   INT32 _mthSelector::_resortObj( const bson::BSONObj &pattern,
                                   const bson::BSONObj &src,
                                   bson::BSONObj &obj )
   {
      
      INT32 rc = SDB_OK ;
      PD_TRACE_ENTRY( SDB__MTHSELECTOR__RESORTOBJ ) ;
      BSONObjBuilder builder ;
      hash_map fMap ;
                                                    
      BSONObjIterator i( src ) ;
      while ( i.more() )
      {
         BSONElement e = i.next() ;
         fMap.insert( std::make_pair( e.fieldName(), e ) ) ;
      }

      BSONObjIterator j( pattern ) ;
      while ( j.more() )
      {
         BSONElement e = j.next() ;
         hash_map::const_iterator itr = fMap.find( e.fieldName() ) ;
         if ( fMap.end() != itr )
         {
            builder.append( itr->second ) ;
         }
         else
         {
            PD_LOG( PDWARNING, "field[%s] in pattern does not exist in src[%s]",
                    e.fieldName(), src.toString( FALSE, TRUE ).c_str() ) ;
         }
      }

      obj = builder.obj() ;
      PD_TRACE_EXITRC( SDB__MTHSELECTOR__RESORTOBJ, rc ) ;
      return rc ;
   }
Example #6
0
   INT32 utilJsonFile::write( ossFile& file, bson::BSONObj& data )
   {
      INT32 rc = SDB_OK ;

      SDB_ASSERT( file.isOpened(), "file must be opened" ) ;

      string json = data.toString() ;

      rc = file.truncate( 0 ) ;
      if ( SDB_OK != rc )
      {
         PD_LOG( PDERROR, "Failed to truncate json file, rc=%d", rc );
         goto error ;
      }

      rc = file.seekAndWriteN( 0, json.c_str(), json.size() ) ;
      if ( SDB_OK != rc )
      {
         PD_LOG( PDERROR, "Failed to write json to file, " \
                 "json size=%d, rc=%d",
                 json.size(), rc );
         goto error ;
      }

      rc = file.sync() ;
      if ( SDB_OK != rc )
      {
         PD_LOG( PDERROR, "Failed to sync json file, rc=%d", rc ) ;
         goto error ;
      }

   done:
      return rc ;
   error:
      goto done ;
   }
Example #7
0
int MongoBase::Insert(std::string name,bson::BSONObj& doc,int flag /* = 0 */)
{
	return this->doInsert(name.c_str(),name.size(),flag,doc.objdata(),doc.objsize());
}
Example #8
0
int MongoBase::Update(std::string name,int flag,bson::BSONObj& selector,bson::BSONObj& updator)
{
	return this->doUpdate(name.c_str(),name.size(),flag,selector.objdata(),selector.objsize(),updator.objdata(),updator.objsize());
}
Example #9
0
int MongoBase::Query(MongoQuery* reply,std::string name,bson::BSONObj& query,bson::BSONObj& selector,int flag /* = 0 */,int skip /* = 0 */,int number /* = 100 */)
{
	int session = this->doQuery(name.c_str(),name.size(),query.objdata(),query.objsize(),selector.objdata(),selector.objsize(),flag,skip,number);
	_queryCallBack[session] = boost::bind(&MongoBase::QueryReply,this,reply,_1,_2);
	return session;
}
Example #10
0
   // PD_TRACE_DECLARE_FUNCTION ( SDB__MTHSELECTOR__BUILDCSV, "_mthSelector::_buildCSV" )
   INT32 _mthSelector::_buildCSV( const bson::BSONObj &obj,
                                  bson::BSONObj &csv )
   {
      INT32 rc = SDB_OK ;
      PD_TRACE_ENTRY( SDB__MTHSELECTOR__BUILDCSV ) ;
      BOOLEAN result = FALSE ;
      INT32 stringLength = 0 ;

      // in the first round, let's allocate memory
      if ( 0 == _stringOutputBufferSize )
      {
         rc = mthDoubleBufferSize ( &_stringOutputBuffer,
                                     _stringOutputBufferSize ) ;
         PD_RC_CHECK ( rc, PDERROR,
                       "Failed to append string, rc = %d", rc ) ;
      }
      _stringOutputBuffer[FIRST_ELEMENT_STARTING_POS-6] = String ;
      _stringOutputBuffer[FIRST_ELEMENT_STARTING_POS-5] = '\0' ;

      while ( _stringOutputBufferSize < MAX_SELECTOR_BUFFER_THRESHOLD )
      {
         result = rawbson2csv ( obj.objdata(),
               &_stringOutputBuffer[FIRST_ELEMENT_STARTING_POS],
                _stringOutputBufferSize-FIRST_ELEMENT_STARTING_POS ) ;
         if ( result )
         {
            break ;
         }
         else
         {
            rc = mthDoubleBufferSize ( &_stringOutputBuffer,
                                       _stringOutputBufferSize ) ;
            PD_RC_CHECK ( rc, PDERROR,
                          "Failed to double buffer, rc = %d", rc ) ;
         }
      }

      if ( _stringOutputBufferSize >= MAX_SELECTOR_BUFFER_THRESHOLD )
      {
         PD_LOG ( PDERROR,
                  "string output buffer size is greater than threshold" ) ;
         rc = SDB_INVALIDARG ;
         goto error ;
      }

      stringLength =
               ossStrlen ( &_stringOutputBuffer[FIRST_ELEMENT_STARTING_POS] ) ;
      // assign object length, 1 for 0 at the end, 1 for the eoo
      *(INT32*)_stringOutputBuffer = FIRST_ELEMENT_STARTING_POS + 2 +
                                        stringLength ;
      _stringOutputBuffer[ *(INT32*)_stringOutputBuffer -1 ] = EOO ;
      // assign string length, 1 for 0 at the end
      *(INT32*)(&_stringOutputBuffer[FIRST_ELEMENT_STARTING_POS-4]) =
               stringLength + 1 ;
      // it should not cause memory leak even if there's previous owned
      // buffer because _stringOutputBuffer is owned by context, and we don't
      // touch holder in BSONObj, so smart pointer should still holding the
      // original buffer it owns
      csv.init ( _stringOutputBuffer ) ;
   done:
      PD_TRACE_EXITRC( SDB__MTHSELECTOR__BUILDCSV, rc ) ;
      return rc ;
   error:
      goto done ;
   }
Example #11
0
   INT32 _sptUsrSsh::construct( const _sptArguments &arg,
                                _sptReturnVal &rval,
                                bson::BSONObj &detail)
   {
      INT32 rc = SDB_OK ;
      string passwd ;
      string errmsg ;
      INT32 port = SPT_SSH_PORT ;

      rc = arg.getString( 0, _host ) ;
      if ( SDB_OUT_OF_BOUND == rc )
      {
         detail = BSON( SPT_ERR << "hostname must be config" ) ;
      }
      else if ( rc )
      {
         detail = BSON( SPT_ERR << "hostname must be string" ) ;
      }
      PD_RC_CHECK( rc, PDERROR, "Failed to get hostname, rc: %d", rc ) ;

      rc = arg.getString( 1, _user ) ;
      if ( rc && SDB_OUT_OF_BOUND != rc )
      {
         detail = BSON( SPT_ERR << "user must be string" ) ;
         PD_RC_CHECK( rc, PDERROR, "Failed to get user, rc: %d", rc ) ;
      }

      rc = arg.getString( 2, passwd ) ;
      if ( rc && SDB_OUT_OF_BOUND != rc )
      {
         detail = BSON( SPT_ERR << "password must be string" ) ;
         PD_RC_CHECK( rc, PDERROR, "Failed to get password, rc: %d", rc ) ;
      }

      rc = arg.getNative( 3, (void*)&port, SPT_NATIVE_INT32 ) ;
      if ( rc && SDB_OUT_OF_BOUND != rc )
      {
         detail = BSON( SPT_ERR << "port must be uint or int" ) ;
         PD_RC_CHECK( rc, PDERROR, "Failed to get port, rc: %d", rc ) ;
      }

      _session = SDB_OSS_NEW _sptLibssh2Session( _host.c_str(),
                                                 _user.c_str(),
                                                 passwd.c_str(),
                                                 &port ) ;
      if ( NULL == _session )
      {
         PD_LOG( PDERROR, "failed to allocate mem." ) ;
         rc = SDB_OOM ;
         goto error ;
      }

      rc = _session->open() ;
      if ( SDB_OK != rc )
      {
         PD_LOG( PDERROR, "failed to open ssh session:%d", rc ) ;
         _session->getLastError( errmsg ) ;
         goto error ;
      }
      _localIP = _session->getLocalIPAddr() ;
      _peerIP = _session->getPeerIPAddr() ;

      rval.addSelfProperty("_host")->setValue( _host ) ;
      rval.addSelfProperty("_port")->setValue( port ) ;
      rval.addSelfProperty("_usrname")->setValue( _user ) ;

   done:
      return rc ;
   error:
      SAFE_OSS_DELETE( _session ) ;
      if ( detail.isEmpty() )
      {
         if ( !errmsg.empty() )
         {
            detail = BSON( SPT_ERR << errmsg ) ;
         }
         else
         {
            detail = BSON( SPT_ERR << "failed to ssh to specified host" ) ;
         }
      }
      goto done ;
   }
Example #12
0
   INT32 aggrProjectParser::addFunc( const CHAR *pAlias, const bson::BSONObj &funcObj,
                                    const CHAR *pCLName, qgmOPFieldVec &selectorVec,
                                    _qgmPtrTable *pTable )
   {
      INT32 rc = SDB_OK;
      CHAR *pFuncBuf = NULL;
      try
      {
         const CHAR *pFuncName = funcObj.firstElementFieldName();
         PD_CHECK( AGGR_KEYWORD_PREFIX == pFuncName[0], SDB_INVALIDARG, error, PDERROR,
                  "failed to parse selector field(%s), function name must begin with \"$\"",
                  pFuncName );
         const CHAR *pFuncParam = funcObj.firstElement().valuestr();
         PD_CHECK( AGGR_KEYWORD_PREFIX == pFuncParam[0], SDB_INVALIDARG, error, PDERROR,
                  "failed to parse selector field(%s), parameter must begin with \"$\"",
                  pFuncParam );

         qgmField slValAttr;
         qgmField slValRelegation;

         UINT32 nameLen = ossStrlen(&(pFuncName[1]));
         UINT32 paramLen = ossStrlen(AGGR_CL_DEFAULT_ALIAS) + 1 + ossStrlen(&(pFuncParam[1]));
         UINT32 curPos = 0;
         pFuncBuf = ( CHAR * )SDB_OSS_MALLOC( nameLen + 3 + paramLen );
         PD_CHECK( pFuncBuf != NULL, SDB_OOM, error, PDERROR,
                  "malloc failed(size=%d)", (nameLen + 3 + paramLen) );
         ossStrcpy( pFuncBuf, &(pFuncName[1]) );
         pFuncBuf[ nameLen ]='(';
         curPos = nameLen + 1;

         ossStrcpy( ( pFuncBuf + curPos), AGGR_CL_DEFAULT_ALIAS );
         curPos += ossStrlen(AGGR_CL_DEFAULT_ALIAS);
         pFuncBuf[ curPos ] = '.';
         curPos += 1;

         ossStrcpy( ( pFuncBuf + curPos ), &(pFuncParam[1]) );
         curPos += ossStrlen(&(pFuncParam[1]));
         pFuncBuf[ curPos ] = ')';
          curPos += 1;
         pFuncBuf[ curPos ] = 0;
         rc = pTable->getOwnField( pFuncBuf, slValAttr );
         PD_RC_CHECK( rc, PDERROR,
                     "failed to get the field(%s)",
                     pFuncBuf );
         qgmDbAttr slVal( slValRelegation, slValAttr );
         qgmField slAlias;
         rc = pTable->getOwnField( pAlias, slAlias );
         qgmOpField selector;
         selector.alias = slAlias;
         selector.value = slVal;
         selector.type = SQL_GRAMMAR::FUNC;
         selectorVec.push_back( selector );
      }
      catch ( std::exception &e )
      {
         PD_CHECK( SDB_INVALIDARG, SDB_INVALIDARG, error, PDERROR,
                  "failed to add function-field, received unexpected error:%s",
                  e.what() );
      }
   done:
      if ( pFuncBuf != NULL )
      {
         SDB_OSS_FREE( pFuncBuf );
      }
      return rc;
   error:
      goto done;
   }
Example #13
0
   void _sptInvoker::_reportError( JSContext *cx,
                                   INT32 rc,
                                   const bson::BSONObj &detail )
   {
      sdbSetErrno( rc ) ;

      if ( SDB_OK != rc )
      {
         stringstream ss ;
         BSONObjIterator itr( detail) ;
         INT32 fieldNum = detail.nFields() ;
         INT32 count = 0 ;
         while ( itr.more() )
         {
            if ( count > 0 )
            {
               ss << ", " ;
            }
            BSONElement e = itr.next() ;
            if ( fieldNum > 1 ||
                 0 != ossStrcmp( SPT_ERR, e.fieldName() ) )
            {
               ss << e.fieldName() << ": " ;
            }

            if ( String == e.type() )
            {
               ss << e.valuestr() ;
            }
            else if ( NumberInt == e.type() )
            {
               ss << e.numberInt() ;
            }
            else if ( NumberLong == e.type() )
            {
               ss << e.numberLong() ;
            }
            else if ( NumberDouble == e.type() )
            {
               ss << e.numberDouble() ;
            }
            else if ( Bool == e.type() )
            {
               ss << ( e.boolean() ? "true" : "false" ) ;
            }
            else
            {
               ss << e.toString( false, false ) ;
            }
            ++count ;
         }
         sdbSetErrMsg( ss.str().c_str() ) ;

         if ( sdbIsErrMsgEmpty() )
         {
            sdbSetErrMsg( getErrDesp( rc ) ) ;
         }

         JS_SetPendingException( cx , INT_TO_JSVAL( rc ) ) ;
      }
      else
      {
         sdbSetErrMsg( NULL ) ;
      }

      return ;
   }