Skip to content

Commit

Permalink
doc GridFS
Browse files Browse the repository at this point in the history
  • Loading branch information
gerald-lindsly committed Nov 21, 2011
1 parent e117f59 commit b15f42a
Showing 1 changed file with 109 additions and 4 deletions.
113 changes: 109 additions & 4 deletions GridFS.pas
@@ -1,3 +1,16 @@
{ GridFS Unit - The classes in this unit are used to store and/or
access a "Grid File System" (GridFS) on a MongoDB server.
While primarily intended to store large documents that
won't fit on the server as a single BSON object,
GridFS may also be used to store large numbers of smaller files.
See http://www.mongodb.org/display/DOCS/GridFS and
http://www.mongodb.org/display/DOCS/When+to+use+GridFS.
Objects of class TGridFS represent the interface to the GridFS.
Objects of class TGridfile are used to access gridfiles and read from them.
Objects of class TGridfileWriter are used to write buffered data to the GridFS.
}
unit GridFS; unit GridFS;


interface interface
Expand All @@ -9,55 +22,147 @@ TGridfile = class;
TGridfileWriter = class; TGridfileWriter = class;


TGridFS = class(TObject) TGridFS = class(TObject)
private
var var
{ Pointer to externally managed data representing the GridFS }
handle : Pointer; handle : Pointer;
{ Holds a reference to the TMongo object used in construction.
Prevents release until the TGridFS is destroyed. }
conn : TMongo; conn : TMongo;
public
{ Create a TGridFS object for accessing the GridFS on the MongoDB server.
Parameter mongo is an already established connection object to the
server; db is the name of the database in which to construct the GridFS.
The prefix defaults to 'fs'.}
constructor Create(mongo : TMongo; db : string); overload; constructor Create(mongo : TMongo; db : string); overload;
{ Create a TGridFS object for accessing the GridFS on the MongoDB server.
Parameter mongo is an already established connection object to the
server; db is the name of the database in which to construct the GridFS.
prefix is appended to the database name for the collections that represent
the GridFS: 'db.prefix.files' & 'db.prefix.chunks'. }
constructor Create(mongo : TMongo; db : string; prefix : string); overload; constructor Create(mongo : TMongo; db : string; prefix : string); overload;
{ Store a file on the GridFS. filename is the path to the file.
Returns True if successful; otherwise, False. }
function storeFile(filename : string) : Boolean; overload; function storeFile(filename : string) : Boolean; overload;
{ Store a file on the GridFS. filename is the path to the file.
remoteName is the name that the file will be known as within the GridFS.
Returns True if successful; otherwise, False. }
function storeFile(filename : string; remoteName : string) : Boolean; overload; function storeFile(filename : string; remoteName : string) : Boolean; overload;
{ Store a file on the GridFS. filename is the path to the file.
remoteName is the name that the file will be known as within the GridFS.
contentType is the MIME-type content type of the file.
Returns True if successful; otherwise, False. }
function storeFile(filename : string; remoteName : string; contentType : string) : Boolean; overload; function storeFile(filename : string; remoteName : string; contentType : string) : Boolean; overload;
{ Remove a file from the GridFS. }
procedure removeFile(remoteName : string); procedure removeFile(remoteName : string);
{ Store data as a GridFS file. Pointer is the address of the data and length
is its size. remoteName is the name that the file will be known as within the GridFS.
Returns True if successful; otherwise, False. }
function store(p : Pointer; length : Int64; remoteName : string) : Boolean; overload; function store(p : Pointer; length : Int64; remoteName : string) : Boolean; overload;
{ Store data as a GridFS file. Pointer is the address of the data and length
is its size. remoteName is the name that the file will be known as within the GridFS.
contentType is the MIME-type content type of the file.
Returns True if successful; otherwise, False. }
function store(p : Pointer; length : Int64; remoteName : string; contentType : string) : Boolean; overload; function store(p : Pointer; length : Int64; remoteName : string; contentType : string) : Boolean; overload;
{ Create a TGridfileWriter object for writing buffered data to a GridFS file.
remoteName is the name that the file will be known as within the GridFS. }
function writerCreate(remoteName : string) : TGridfileWriter; overload; function writerCreate(remoteName : string) : TGridfileWriter; overload;
{ Create a TGridfileWriter object for writing buffered data to a GridFS file.
remoteName is the name that the file will be known as within the GridFS.
contentType is the MIME-type content type of the file. }
function writerCreate(remoteName : string; contentType : string) : TGridfileWriter; overload; function writerCreate(remoteName : string; contentType : string) : TGridfileWriter; overload;
{ Locate a GridFS file by its remoteName and return a TGridfile object for
accessing it. }
function find(remoteName : string) : TGridfile; overload; function find(remoteName : string) : TGridfile; overload;
{ Locate a GridFS file by an TBson query document on the GridFS file descriptors.
Returns a TGridfile object for accessing it. }
function find(query : TBson) : TGridfile; overload; function find(query : TBson) : TGridfile; overload;
{ Destroy this GridFS object. Releases external resources. }
destructor Destroy(); override; destructor Destroy(); override;
end; end;


{ Objects of class TGridfile are used to access gridfiles and read from them. }
TGridfile = class(TObject) TGridfile = class(TObject)
private
var var
{ Pointer to externally managed data representing the gridfile }
handle : Pointer; handle : Pointer;
{ Hold a reference to the TGridFS object used in construction of this
TGridfile. Prevents release until this TGridfile is destroyed. }
gfs : TGridFS; gfs : TGridFS;
private { Create a TGridfile object. Internal use only by TGridFS.find(). }
constructor Create(gridfs : TGridFS); constructor Create(gridfs : TGridFS);
public public
{ Get the filename (remoteName) of this gridfile. }
function getFilename() : string; function getFilename() : string;
{ Get the size of the chunks into which the file is divided. }
function getChunkSize() : Integer; function getChunkSize() : Integer;
{ Get the length of this gridfile. }
function getLength() : Int64; function getLength() : Int64;
{ Get the content type of this gridfile. }
function getContentType() : string; function getContentType() : string;
{ Get the upload date of this gridfile. }
function getUploadDate() : TDateTime; function getUploadDate() : TDateTime;
{ Get the MD5 hash of this gridfile. This is a 16-digit hex string. }
function getMD5() : string; function getMD5() : string;
{ Get any metadata associated with this gridfile as a TBson document.
Returns nil if there is none. }
function getMetadata() : TBson; function getMetadata() : TBson;
{ Get the number of chunks into which the file is divided. }
function getChunkCount() : Integer; function getChunkCount() : Integer;
{ Get the descriptor of this gridfile as a TBson document. }
function getDescriptor() : TBson; function getDescriptor() : TBson;
{ Get the Ith chunk of this gridfile. The content of the chunk is
in the 'data' field of the returned TBson document. Returns nil
if i is not in the range 0 to getChunkCount() - 1. }
function getChunk(i : Integer) : TBson; function getChunk(i : Integer) : TBson;
{ Get a cursor for stepping through a range of chunks of this gridfile.
i is the index of the first chunk to be returned. count is the number
of chunks to return. Returns nil if there are no chunks in the
specified range. }
function getChunks(i : Integer; count : Integer) : TMongoCursor; function getChunks(i : Integer; count : Integer) : TMongoCursor;
{ Read data from this gridfile. The gridfile maintains a current position
so that successive reads will return consecutive data. The data is
read to the address indicated by p and length bytes are read. The size
of the data read is returned and can be less than length if there was
not enough data remaining to be read. }
function read(p : Pointer; length : Int64) : Int64; function read(p : Pointer; length : Int64) : Int64;
{ Seek to a specified offset within the gridfile. read() will then
return data starting at that location. Returns the position that
was set. This can be at the end of the gridfile if offset is greater
the length of this gridfile. }
function seek(offset : Int64) : Int64; function seek(offset : Int64) : Int64;
{ Destroy this TGridfile object. Releases external resources. }
destructor Destroy(); override; destructor Destroy(); override;
end; end;


{ Objects of class TGridfileWriter are used to write buffered data to the GridFS. }
TGridfileWriter = class(TObject) TGridfileWriter = class(TObject)
private
var var
{ Holds a pointer to externally managed data representing the TGridfileWriter. }
handle : Pointer; handle : Pointer;
{ Holds a reference to the TGridFS object used in construction.
Prevents release of the TGridFS until this TGridfileWriter is destroyed. }
gfs : TGridFS; gfs : TGridFS;
public
{ Create a TGridfile writer on the given TGridFS that will write data to
the given remoteName. }
constructor Create(gridfs : TGridFS; remoteName : string); overload; constructor Create(gridfs : TGridFS; remoteName : string); overload;
{ Create a TGridfile writer on the given TGridFS that will write data to
the given remoteName. contentType is the MIME-type content type of the gridfile
to be written. }
constructor Create(gridfs : TGridFS; remoteName : string; contentType : string); overload; constructor Create(gridfs : TGridFS; remoteName : string; contentType : string); overload;
{ Write data to this TGridfileWriter. p is the address of the data and length
is its size. Multiple calls to write() may be made to append successive
data. }
procedure write(p : Pointer; length : Int64); procedure write(p : Pointer; length : Int64);
{ Finish with this TGridfileWriter. Flushes any data remaining to be written
to a chunk and posts the 'directory' information of the gridfile to the
GridFS. Returns True if successful; otherwise, False. }
function finish() : Boolean; function finish() : Boolean;
{ Destroy this TGridfileWriter. Calls finish() if necessary and releases
external resources. }
destructor Destroy(); override; destructor Destroy(); override;
end; end;


Expand Down Expand Up @@ -263,7 +368,7 @@ implementation
begin begin
Result := Int64ToDouble(gridfile_get_uploaddate(handle)) / (1000 * 24 * 60 * 60) + 25569; Result := Int64ToDouble(gridfile_get_uploaddate(handle)) / (1000 * 24 * 60 * 60) + 25569;
end; end;

function TGridfile.getMD5() : string; function TGridfile.getMD5() : string;
begin begin
Result := string(gridfile_get_md5(handle)); Result := string(gridfile_get_md5(handle));
Expand All @@ -290,7 +395,7 @@ implementation
begin begin
Result := gridfile_get_numchunks(handle); Result := gridfile_get_numchunks(handle);
end; end;

function TGridfile.getDescriptor() : TBson; function TGridfile.getDescriptor() : TBson;
var var
b : Pointer; b : Pointer;
Expand Down Expand Up @@ -338,5 +443,5 @@ implementation
Result := gridfile_seek(handle, offset); Result := gridfile_seek(handle, offset);
end; end;



end. end.

0 comments on commit b15f42a

Please sign in to comment.