Files
mongo/client/gridfs.h

206 lines
5.8 KiB
C
Raw Normal View History

/** @file gridfs.h */
2009-02-02 17:19:55 -05:00
/* Copyright 2009 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2009-02-02 17:19:55 -05:00
#pragma once
#include "dbclient.h"
#include "redef_macros.h"
2009-02-02 17:19:55 -05:00
namespace mongo {
2009-02-09 15:38:26 -05:00
typedef unsigned long long gridfs_offset;
2009-02-02 17:19:55 -05:00
class GridFS;
class GridFile;
2010-07-02 11:29:33 -04:00
class GridFSChunk {
2009-02-02 17:19:55 -05:00
public:
2010-07-02 11:29:33 -04:00
GridFSChunk( BSONObj data );
GridFSChunk( BSONObj fileId , int chunkNumber , const char * data , int len );
2009-08-06 14:12:32 -04:00
int len() const {
2009-02-02 17:19:55 -05:00
int len;
2010-06-29 13:58:16 -04:00
_data["data"].binDataClean( len );
return len;
2009-02-02 17:19:55 -05:00
}
2009-08-06 14:12:32 -04:00
const char * data( int & len ) const {
2010-06-29 13:58:16 -04:00
return _data["data"].binDataClean( len );
2009-02-02 17:19:55 -05:00
}
2009-08-06 14:12:32 -04:00
2009-02-02 17:19:55 -05:00
private:
BSONObj _data;
friend class GridFS;
};
/**
2010-12-12 19:45:47 -05:00
GridFS is for storing large file-style objects in MongoDB.
@see http://www.mongodb.org/display/DOCS/GridFS+Specification
2009-02-02 17:19:55 -05:00
*/
2010-12-12 19:45:47 -05:00
class GridFS {
2009-02-02 17:19:55 -05:00
public:
/**
* @param client - db connection
* @param dbName - root database name
* @param prefix - if you want your data somewhere besides <dbname>.fs
2009-02-02 17:19:55 -05:00
*/
GridFS( DBClientBase& client , const string& dbName , const string& prefix="fs" );
2009-02-02 17:19:55 -05:00
~GridFS();
/**
* @param
*/
void setChunkSize(unsigned int size);
2009-02-02 17:19:55 -05:00
/**
* puts the file reference by fileName into the db
* @param fileName local filename relative to process
* @param remoteName optional filename to use for file stored in GridFS
* (default is to use fileName parameter)
* @param contentType optional MIME type for this object.
* (default is to omit)
* @return the file object
2009-02-02 17:19:55 -05:00
*/
BSONObj storeFile( const string& fileName , const string& remoteName="" , const string& contentType="");
2009-08-06 14:12:32 -04:00
/**
* puts the file represented by data into the db
* @param data pointer to buffer to store in GridFS
* @param length length of buffer
* @param remoteName optional filename to use for file stored in GridFS
* (default is to use fileName parameter)
* @param contentType optional MIME type for this object.
* (default is to omit)
* @return the file object
*/
BSONObj storeFile( const char* data , size_t length , const string& remoteName , const string& contentType="");
2010-12-12 19:45:47 -05:00
/**
* removes file referenced by fileName from the db
* @param fileName filename (in GridFS) of the file to remove
* @return the file object
*/
void removeFile( const string& fileName );
2009-08-06 14:12:32 -04:00
2009-02-02 17:19:55 -05:00
/**
* returns a file object matching the query
*/
GridFile findFile( BSONObj query ) const;
2009-02-02 17:19:55 -05:00
/**
* equiv to findFile( { filename : filename } )
*/
GridFile findFile( const string& fileName ) const;
2009-02-02 17:19:55 -05:00
/**
* convenience method to get all the files
*/
auto_ptr<DBClientCursor> list() const;
2009-02-02 17:19:55 -05:00
2009-02-02 22:16:39 -05:00
/**
* convenience method to get all the files with a filter
*/
auto_ptr<DBClientCursor> list( BSONObj query ) const;
2009-02-02 22:16:39 -05:00
2009-02-02 17:19:55 -05:00
private:
DBClientBase& _client;
string _dbName;
2009-02-18 13:11:22 -05:00
string _prefix;
2009-02-02 17:19:55 -05:00
string _filesNS;
string _chunksNS;
unsigned int _chunkSize;
2009-02-02 17:19:55 -05:00
// insert fileobject. All chunks must be in DB.
BSONObj insertFile(const string& name, const OID& id, gridfs_offset length, const string& contentType);
2009-02-02 17:19:55 -05:00
friend class GridFile;
};
2009-08-06 14:12:32 -04:00
2009-02-02 17:19:55 -05:00
/**
wrapper for a file stored in the Mongo database
2009-02-02 17:19:55 -05:00
*/
class GridFile {
public:
/**
* @return whether or not this file exists
* findFile will always return a GriFile, so need to check this
*/
bool exists() const {
2009-02-02 17:19:55 -05:00
return ! _obj.isEmpty();
}
2009-08-06 14:12:32 -04:00
string getFilename() const {
2009-02-02 17:19:55 -05:00
return _obj["filename"].str();
}
2009-08-06 14:12:32 -04:00
int getChunkSize() const {
2009-02-02 17:19:55 -05:00
return (int)(_obj["chunkSize"].number());
}
gridfs_offset getContentLength() const {
2009-02-02 17:19:55 -05:00
return (gridfs_offset)(_obj["length"].number());
}
string getContentType() const {
return _obj["contentType"].valuestr();
}
Date_t getUploadDate() const {
return _obj["uploadDate"].date();
}
string getMD5() const {
return _obj["md5"].str();
}
BSONElement getFileField( const string& name ) const {
return _obj[name];
}
BSONObj getMetadata() const;
int getNumChunks() const {
return (int) ceil( (double)getContentLength() / (double)getChunkSize() );
2009-02-02 17:19:55 -05:00
}
GridFSChunk getChunk( int n ) const;
2009-02-02 17:19:55 -05:00
/**
write the file to the output stream
2009-02-02 17:19:55 -05:00
*/
gridfs_offset write( ostream & out ) const;
2009-02-02 17:19:55 -05:00
/**
write the file to this filename
*/
gridfs_offset write( const string& where ) const;
2009-08-06 14:12:32 -04:00
2009-02-02 17:19:55 -05:00
private:
GridFile(const GridFS * grid , BSONObj obj );
2009-02-02 17:19:55 -05:00
void _exists() const;
2009-08-06 14:12:32 -04:00
const GridFS * _grid;
BSONObj _obj;
2009-02-02 17:19:55 -05:00
friend class GridFS;
};
}
2009-08-06 14:12:32 -04:00
#include "undef_macros.h"