Ejemplo n.º 1
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 ;
   }
Ejemplo n.º 2
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 ;
   }