Ejemplo n.º 1
0
INT32 queryCommand::convertRequest( mongoParser &parser,
                                    std::vector<msgBuffer*> &sdbMsgs )
{
   INT32 rc           = SDB_OK ;
   INT32 nToSkip      = 0 ;
   INT32 nToReturn    = 0 ;
   MsgHeader *header  = NULL ;
   MsgOpQuery *query  = NULL ;
   const CHAR *cmdStr = NULL ;
   command* cmd       = NULL ;
   bson::BSONObj cond ;
   bson::BSONObj selector ;
   bson::BSONObj orderby ;
   bson::BSONObj hint ;
   bson::BSONObj fieldToReturn ;
   msgBuffer *sdbMsg = SDB_OSS_NEW msgBuffer() ;
   if ( NULL == sdbMsg )
   {
      rc = SDB_OOM ;
      goto error ;
   }

   parser.opType = OP_QUERY ;
   sdbMsg->reverse( sizeof ( MsgOpQuery ) ) ;
   sdbMsg->advance( sizeof ( MsgOpQuery ) - 4 ) ;

   header = ( MsgHeader * )sdbMsg->data() ;
   header->opCode = MSG_BS_QUERY_REQ ;
   header->TID = 0 ;
   header->routeID.value = 0 ;
   header->requestID = parser.id ;

   query = ( MsgOpQuery * )sdbMsg->data() ;
   query->version = 0 ;
   query->w = 0 ;
   query->padding = 0 ;
   query->flags = 0 ;

   query->nameLength = parser.nsLen ;
   parser.skip( query->nameLength + 1 ) ;

   if ( parser.reservedFlags & QUERY_CURSOR_TAILABLE )
   {
   }

   if ( parser.reservedFlags & QUERY_SLAVE_OK )
   {
      query->flags |= FLG_QUERY_SLAVEOK ;
   }

   if ( parser.reservedFlags & QUERY_OPLOG_REPLAY )
   {
      query->flags |= FLG_QUERY_OPLOGREPLAY ;
   }

   if ( parser.reservedFlags & QUERY_NO_CURSOR_TIMEOUT )
   {
      query->flags |= FLG_QUERY_NOCONTEXTTIMEOUT ;
   }

   if ( parser.reservedFlags & QUERY_AWAIT_DATA )
   {
      query->flags |= FLG_QUERY_AWAITDATA ;
   }

   if ( parser.reservedFlags & QUERY_EXHAUST )
   {
   }

   if ( parser.reservedFlags & QUERY_PARTIAL_RESULTS )
   {
      query->flags |= FLG_QUERY_PARTIALREAD ;
   }

   parser.readNumber( sizeof( INT32 ), ( CHAR * )&nToSkip ) ;
   query->numToSkip = nToSkip ;
   parser.readNumber( sizeof( INT32 ), ( CHAR * )&nToReturn ) ;
   query->numToReturn = nToReturn ;

   if ( parser.more() )
   {
      parser.nextObj( cond ) ;
   }

   if ( parser.withCmd )
   {
      cmdStr = cond.firstElementFieldName() ;
      cmd = commandMgr::instance()->findCommand( cmdStr ) ;
      if ( NULL == cmd )
      {
         rc = SDB_OPTION_NOT_SUPPORT ;
         parser.opType = OP_CMD_NOT_SUPPORTED ;
         goto error ;
      }
      parser.reparse() ;
      sdbMsg->zero() ;

      SDB_OSS_DEL sdbMsg ;
      sdbMsg = NULL ;

      rc = cmd->convertRequest( parser, sdbMsgs ) ;
      goto done ;
   }

   orderby = cond.getObjectField( "orderby" ) ;
   orderby = removeField( orderby, "$hint" ) ;

   if ( parser.more() )
   {
      parser.nextObj( fieldToReturn ) ;
   }

   sdbMsg->write( parser.fullName, query->nameLength + 1, TRUE ) ;
   sdbMsg->write( cond, TRUE ) ;
   sdbMsg->write( fieldToReturn, TRUE ) ;
   sdbMsg->write( orderby, TRUE ) ;
   sdbMsg->write( hint, TRUE ) ;

   header->messageLength = sdbMsg->size() ;
   sdbMsgs.push_back( sdbMsg ) ;

done:
   return rc ;
error:
   goto done ;
}
Ejemplo n.º 2
0
INT32 queryCommand::convertRequest( mongoParser &parser, msgBuffer &sdbMsg )
{
    INT32 rc           = SDB_OK ;
    INT32 nToSkip      = 0 ;
    INT32 nToReturn    = -1 ;
    MsgHeader *header  = NULL ;
    MsgOpQuery *query  = NULL ;
    const CHAR *cmdStr = NULL ;
    command* cmd       = NULL ;
    bson::BSONObj all ;
    bson::BSONObj cond ;
    bson::BSONObj selector ;
    bson::BSONObj orderby ;
    bson::BSONObj hint ;
    bson::BSONObj fieldToReturn ;

    parser.opType = OP_QUERY ;
    sdbMsg.reverse( sizeof ( MsgOpQuery ) ) ;
    sdbMsg.advance( sizeof ( MsgOpQuery ) - 4 ) ;

    header = ( MsgHeader * )sdbMsg.data() ;
    header->opCode = MSG_BS_QUERY_REQ ;
    header->TID = 0 ;
    header->routeID.value = 0 ;
    header->requestID = parser.id ;

    query = ( MsgOpQuery * )sdbMsg.data() ;
    query->version = 0 ;
    query->w = 0 ;
    query->padding = 0 ;
    query->flags = 0 ;
    fap::setQueryFlags( parser.reservedFlags, query->flags ) ;

    query->nameLength = parser.nsLen ;
    parser.skip( query->nameLength + 1 ) ;

    parser.readNumber( sizeof( INT32 ), ( CHAR * )&nToSkip ) ;
    query->numToSkip = nToSkip ;
    parser.readNumber( sizeof( INT32 ), ( CHAR * )&nToReturn ) ;
    query->numToReturn = (0 == nToReturn ? -1 : nToReturn) ;
    if ( 0 > nToReturn )
    {
        query->numToReturn = -nToReturn ;
    }

    if ( parser.more() )
    {
        parser.nextObj( all ) ;
    }

    if ( parser.withIndex )
    {
        cmd = commandMgr::instance()->findCommand( "listIndexes" ) ;
        if ( NULL == cmd )
        {
            rc = SDB_OPTION_NOT_SUPPORT ;
            parser.opType = OP_CMD_NOT_SUPPORTED ;
            goto error ;
        }
        parser.reparse() ;
        sdbMsg.zero() ;

        rc = cmd->convertRequest( parser, sdbMsg ) ;
        goto done ;
    }

    if ( parser.withCmd )
    {
        cmdStr = all.firstElementFieldName() ;
        cmd = commandMgr::instance()->findCommand( cmdStr ) ;
        if ( NULL == cmd )
        {
            rc = SDB_OPTION_NOT_SUPPORT ;
            parser.opType = OP_CMD_NOT_SUPPORTED ;
            parser.cmdName = cmdStr ;
            goto error ;
        }
        parser.reparse() ;
        sdbMsg.zero() ;

        rc = cmd->convertRequest( parser, sdbMsg ) ;
        goto done ;
    }

    cond = fap::getCondObj( all ) ;
    orderby = all.getObjectField( "orderby" ) ;
    hint    = cond.getObjectField( "$hint" ) ;

    if ( all.hasField( "limit" ) )
    {
        query->numToReturn = cond.getIntField( "limit" ) ;
    }

    if ( all.hasField( "skip" ) )
    {
        query->numToSkip   = cond.getIntField( "skip" ) ;
    }

    if ( cond.getBoolField("$explain") )
    {
        query->flags |= FLG_QUERY_EXPLAIN ;
    }

    if ( parser.more() )
    {
        parser.nextObj( fieldToReturn ) ;
    }

    sdbMsg.write( parser.fullName, query->nameLength + 1, TRUE ) ;
    sdbMsg.write( cond, TRUE ) ;
    sdbMsg.write( fieldToReturn, TRUE ) ;
    sdbMsg.write( orderby, TRUE ) ;
    sdbMsg.write( hint, TRUE ) ;

    sdbMsg.doneLen() ;

done:
    return rc ;
error:
    goto done ;
}