示例#1
0
文件: gridfs.cpp 项目: weakwater/easy
    GridFS::GridFS( DBClientBase& client , const string& dbName , const string& prefix ) : _client( client ) , _dbName( dbName ) , _prefix( prefix ) {
        _filesNS = dbName + "." + prefix + ".files";
        _chunksNS = dbName + "." + prefix + ".chunks";
        _chunkSize = DEFAULT_CHUNK_SIZE;

        client.createIndex( _filesNS , BSON( "filename" << 1 ) );
        client.createIndex( _chunksNS , IndexSpec().addKeys(BSON( "files_id" << 1 << "n" << 1 )).unique() );
    }
/* ****************************************************************************
*
* collectionCreateIndex -
*/
bool collectionCreateIndex
(
  const std::string&  col,
  const BSONObj&      indexes,
  std::string*        err
)
{
  TIME_STAT_MONGO_COMMAND_WAIT_START();
  DBClientBase* connection = getMongoConnection();

  if (connection == NULL)
  {
    TIME_STAT_MONGO_COMMAND_WAIT_STOP();

    LM_E(("Fatal Error (null DB connection)"));
    return false;
  }

  LM_T(LmtMongo, ("createIndex() in '%s' collection: '%s'", col.c_str(), indexes.toString().c_str()));

  try
  {
    connection->createIndex(col.c_str(), indexes);
    releaseMongoConnection(connection);
    TIME_STAT_MONGO_COMMAND_WAIT_STOP();
    LM_I(("Database Operation Successful (createIndex: %s)", indexes.toString().c_str()));
  }
  catch (const std::exception &e)
  {
    releaseMongoConnection(connection);
    TIME_STAT_MONGO_COMMAND_WAIT_STOP();

    std::string msg = std::string("collection: ") + col.c_str() +
      " - createIndex(): " + indexes.toString() +
      " - exception: " + e.what();
    *err = "Database Error (" + msg + ")";
    LM_E((err->c_str()));
    return false;
  }
  catch (...)
  {
    releaseMongoConnection(connection);
    TIME_STAT_MONGO_COMMAND_WAIT_STOP();

    std::string msg = std::string("collection: ") + col.c_str() +
      " - createIndex(): " + indexes.toString() +
      " - exception: generic";
    *err = "Database Error (" + msg + ")";
    LM_E((err->c_str()));
    return false;
  }

  return true;
}