forked from gerald-lindsly/mongo-delphi-driver
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGridFS.pas
739 lines (666 loc) · 24.1 KB
/
GridFS.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
{
Copyright 2009-2011 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.
}
{ 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;
interface
uses
MongoDB, MongoBson, MongoApi;
const
GRIDFILE_DEFAULT = 0;
GRIDFILE_NOMD5 = 1;
GRIDFILE_COMPRESS = 2;
GRIDFILE_ENCRYPT = 4;
E_GridFSHandleIsNil = 90200;
E_GridFileHandleIsNil = 90201;
E_InternalErrorOIDDescriptorOfFile = 90202;
E_UnableToCreateGridFS = 90203;
type
IGridfile = interface;
IGridfileWriter = interface;
TGridFS = class(TMongoObject)
private
{ Pointer to externally managed data representing the GridFS }
Handle: Pointer;
{ Holds a reference to the TMongo object used in construction.
Prevents release until the TGridFS is destroyed. }
conn: TMongo;
fdb : UTF8String;
FPrefix: UTF8String;
procedure CheckHandle;
procedure setAutoCheckLastError(value : Boolean);
function getAutoCheckLastError : Boolean;
function GetCaseInsensitiveFileNames: Boolean;
procedure SetCaseInsensitiveFileNames(const Value: Boolean);
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; const db: UTF8String); 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; const db, prefix: UTF8String); overload;
{ Store a file on the GridFS. filename is the path to the file.
Returns True if successful; otherwise, False. }
function storeFile(const FileName: UTF8String; Flags: Integer =
GRIDFILE_DEFAULT): 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(const FileName, remoteName: UTF8String; Flags: Integer =
GRIDFILE_DEFAULT): 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(const FileName, remoteName, contentType: UTF8String; Flags:
Integer = GRIDFILE_DEFAULT): Boolean; overload;
{ Remove a file from the GridFS. }
procedure removeFile(const remoteName: UTF8String);
{ 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; const remoteName: UTF8String; Flags:
Integer = GRIDFILE_DEFAULT): 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; const remoteName, contentType:
UTF8String; Flags: Integer = GRIDFILE_DEFAULT): 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(const remoteName: UTF8String; Flags: Integer =
GRIDFILE_DEFAULT): IGridfileWriter; 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(const remoteName, contentType: UTF8String; Flags: Integer
= GRIDFILE_DEFAULT): IGridfileWriter; overload;
{ Locate a GridFS file by its remoteName and return a TGridfile object for
accessing it. }
function find(const remoteName: UTF8String; AWriteMode: Boolean): IGridfile;
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: IBson; AWriteMode: Boolean): IGridfile; overload;
{ Destroy this GridFS object. Releases external resources. }
function createGridFile: IGridFile;
destructor Destroy; override;
procedure removeFS;
property AutoCheckLastError: Boolean read getAutoCheckLastError write setAutoCheckLastError;
property CaseInsensitiveFileNames: Boolean read GetCaseInsensitiveFileNames
write SetCaseInsensitiveFileNames;
property Mongo: TMongo read conn;
end;
IGridFile = interface
['{DA93414C-1D0B-4F08-A78A-F1914A2E214C}']
{ 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): IBson;
{ Get the number of chunks into which the file is divided. }
function getChunkCount: Integer;
{ Get the number of chunks into which the file is divided. }
function getStoredChunkCount: UInt64;
{ 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): IMongoCursor;
{ Get the size of the chunks into which the file is divided. }
function getChunkSize: Integer;
{ Get the content type of this gridfile. }
function getContentType: UTF8String;
{ Get the descriptor of this gridfile as a TBson document. }
function getDescriptor: IBson;
{ Get the filename (remoteName) of this gridfile. }
function getFilename: UTF8String;
{ Get the length of this gridfile. }
function getLength: UInt64;
{ Get the MD5 hash of this gridfile. This is a 16-digit hex string. }
function getMD5: UTF8String;
{ Get any metadata associated with this gridfile as a TBson document.
Returns nil if there is none. }
function getMetadata: IBson;
{ Get the upload date of this gridfile. }
function getUploadDate: TDateTime;
{ 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: UInt64): UInt64;
{ 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: UInt64): UInt64;
function getID : IBsonOID;
function Handle : Pointer;
end;
IGridfileWriter = interface(IGridFile)
['{1BD1F2BA-C045-47CD-B1C7-611A77BCB590}']
{ Finish with this IGridfileWriter. 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;
{ Write data to this IGridfileWriter. p is the address of the data and length
is its size. Multiple calls to write() may be made to append successive
data. }
function Write(p: Pointer; Length: UInt64): UInt64;
function truncate(newSize: UInt64): UInt64;
function expand(bytesToExpand: UInt64): UInt64;
function setSize(newSize: UInt64): UInt64;
end;
implementation
uses
SysUtils;
// START resource string wizard section
const
SFiles_id = 'files_id';
SChunks = '.chunks';
SFs = 'fs';
SFilename = 'filename';
// END resource string wizard section
// START resource string wizard section
resourcestring
SGridFSHandleIsNil = 'GridFS Handle is nil (D%d)';
SGridFileHandleIsNil = 'GridFile Handle is nil (D%d)';
SUnableToCreateGridFS = 'Unable to create GridFS (D%d)';
// END resource string wizard section
type
{ Objects of class TGridfile are used to access gridfiles and read from them. }
TGridfile = class(TMongoInterfacedObject, IGridFile)
private
procedure CheckHandle;
protected
{ Pointer to externally managed data representing the gridfile }
FHandle: Pointer;
{ Hold a reference to the TGridFS object used in construction of this
TGridfile. Prevents release until this TGridfile is destroyed. }
gfs: TGridFS;
{ Create a TGridfile object. Internal use only by TGridFS.find(). }
constructor Create(gridfs: TGridFS);
procedure DestroyGridFile;
public
function getStoredChunkCount: UInt64;
{ Get the filename (remoteName) of this gridfile. }
function getFilename: UTF8String;
{ Get the size of the chunks into which the file is divided. }
function getChunkSize: Integer;
{ Get the length of this gridfile. }
function getLength: UInt64;
{ Get the content type of this gridfile. }
function getContentType: UTF8String;
{ Get the upload date of this gridfile. }
function getUploadDate: TDateTime;
{ Get the MD5 hash of this gridfile. This is a 16-digit hex string. }
function getMD5: UTF8String;
{ Get any metadata associated with this gridfile as a TBson document.
Returns nil if there is none. }
function getMetadata: IBson;
{ Get the number of chunks into which the file is divided. }
function getChunkCount: Integer;
{ Get the descriptor of this gridfile as a TBson document. }
function getDescriptor: IBson;
{ 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): IBson;
{ 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): IMongoCursor;
{ 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: UInt64): UInt64;
{ 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: UInt64): UInt64;
function Handle: Pointer;
function getID: IBsonOID;
{ Destroy this TGridfile object. Releases external resources. }
destructor Destroy; override;
end;
type
{ Objects of class TGridfileWriter are used to write buffered data to the GridFS. }
TGridfileWriter = class(TGridFile, IGridfileWriter)
private
FInited: Boolean;
public
{ Create a TGridfile writer on the given TGridFS that will write data to
the given remoteName. }
constructor Create(gridfs: TGridFS; remoteName: UTF8String; AInit: Boolean;
AMeta: Pointer; Flags: Integer); 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; const remoteName, contentType: UTF8String;
AInit: Boolean; AMeta: Pointer; Flags: Integer); 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. }
function write(p: Pointer; Length: UInt64): UInt64;
{ 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 truncate(newSize: UInt64): UInt64;
function expand(bytesToExpand: UInt64): UInt64;
{ setSize supercedes truncate in the sense it can change the file size up or down. If making
file size larger, file will be zero filled }
function setSize(newSize: UInt64): UInt64;
function finish: Boolean;
{ Destroy this TGridfileWriter. Calls finish() if necessary and releases
external resources. }
destructor Destroy; override;
end;
{ TGridFS }
constructor TGridFS.Create(mongo: TMongo; const db, prefix: UTF8String);
begin
inherited Create;
{$IFDEF OnDemandMongoCLoad}
InitMongoDBLibrary;
{$ENDIF}
FPrefix := prefix;
fdb := db;
conn := mongo;
Handle := gridfs_alloc;
if gridfs_init(mongo.Handle, PAnsiChar(fdb),
PAnsiChar(prefix), Handle) <> 0 then
begin
gridfs_dealloc(Handle);
raise EMongo.Create(SUnableToCreateGridFS, E_UnableToCreateGridFS);
end;
AutoCheckLastError := True;
end;
constructor TGridFS.Create(mongo: TMongo; const db: UTF8String);
begin
inherited Create;
Create(mongo, db, SFs);
end;
destructor TGridFS.Destroy;
begin
if Handle <> nil then
begin
gridfs_destroy(Handle);
gridfs_dealloc(Handle);
Handle := nil;
end;
inherited;
end;
procedure TGridFS.CheckHandle;
begin
if Handle = nil then
raise EMongo.Create(SGridFSHandleIsNil, E_GridFSHandleIsNil);
end;
function TGridFS.storeFile(const FileName, remoteName, contentType: UTF8String;
Flags: Integer = GRIDFILE_DEFAULT): Boolean;
var
RetVal : Integer;
begin
CheckHandle;
conn.autoCmdResetLastError(fdb, False);
RetVal := gridfs_store_file(Handle, PAnsiChar(FileName), PAnsiChar(remoteName), PAnsiChar(contentType), Flags);
Result := RetVal = 0;
conn.autoCheckCmdLastError(fdb, False);
end;
function TGridFS.storeFile(const FileName, remoteName: UTF8String; Flags:
Integer = GRIDFILE_DEFAULT): Boolean;
begin
Result := storeFile(FileName, remoteName, '', Flags);
end;
function TGridFS.storeFile(const FileName: UTF8String; Flags: Integer =
GRIDFILE_DEFAULT): Boolean;
begin
Result := storeFile(FileName, FileName, '', Flags);
end;
procedure TGridFS.removeFile(const remoteName: UTF8String);
begin
CheckHandle;
gridfs_remove_filename(Handle, PAnsiChar(remoteName));
end;
procedure TGridFS.setAutoCheckLastError(value: Boolean);
begin
conn.AutoCheckLastError := Value;
end;
function TGridFS.store(p: Pointer; Length: Int64; const remoteName,
contentType: UTF8String; Flags: Integer = GRIDFILE_DEFAULT): Boolean;
begin
CheckHandle;
Result := (gridfs_store_buffer(Handle, p, Length, PAnsiChar(remoteName), PAnsiChar(contentType), Flags) = 0);
end;
function TGridFS.store(p: Pointer; Length: Int64; const remoteName: UTF8String;
Flags: Integer = GRIDFILE_DEFAULT): Boolean;
begin
Result := store(p, Length, remoteName, '', Flags);
end;
function TGridFS.writerCreate(const remoteName, contentType: UTF8String; Flags:
Integer = GRIDFILE_DEFAULT): IGridfileWriter;
begin
CheckHandle;
Result := TGridfileWriter.Create(Self, remoteName, contentType, True, bsonEmpty.Handle, Flags);
end;
function TGridFS.writerCreate(const remoteName: UTF8String; Flags: Integer =
GRIDFILE_DEFAULT): IGridfileWriter;
begin
Result := writerCreate(remoteName, '', Flags);
end;
function TGridFS.createGridFile: IGridFile;
begin
CheckHandle;
Result := TGridFile.Create(Self);
end;
function TGridFS.find(query: IBson; AWriteMode: Boolean): IGridfile;
var
gf: IGridfile;
AHandle : Pointer;
meta : Pointer;
begin
CheckHandle;
gf := nil;
if not AWriteMode then
begin
gf := TGridfile.Create(Self);
AHandle := gf.Handle;
end
else AHandle := gridfile_create;
try
if gridfs_find_query(Handle, query.Handle, AHandle) = 0 then
begin
if AWriteMode then
begin
meta := bson_create;
try
gridfile_get_descriptor( AHandle, meta );
gf := TGridfileWriter.Create(Self, gridfile_get_filename(AHandle), True, Meta, GRIDFILE_DEFAULT);
gridfile_destroy(AHandle);
finally
bson_dealloc(meta); // Dont' call Destroy for this object, data is owned by gridfile
end;
end;
Result := gf;
end
else Result := nil;
finally
if AWriteMode then
gridfile_dealloc(AHandle);
end;
end;
function TGridFS.getAutoCheckLastError: Boolean;
begin
Result := conn.AutoCheckLastError;
end;
function TGridFS.find(const remoteName: UTF8String; AWriteMode: Boolean):
IGridfile;
begin
if CaseInsensitiveFileNames then
Result := find(BSON([SFilename, UpperCase(remoteName)]), AWriteMode)
else Result := find(BSON([SFilename, remoteName]), AWriteMode);
end;
function TGridFS.GetCaseInsensitiveFileNames: Boolean;
begin
CheckHandle;
Result := gridfs_get_caseInsensitive(Handle);
end;
procedure TGridFS.removeFS;
begin
conn.drop(fdb + '.' + FPrefix + '.files');
conn.drop(fdb + '.' + FPrefix + '.chunks');
end;
procedure TGridFS.SetCaseInsensitiveFileNames(const Value: Boolean);
begin
CheckHandle;
gridfs_set_caseInsensitive(Handle, Value);
end;
{ TGridfileWriter }
constructor TGridfileWriter.Create(gridfs: TGridFS; const remoteName,
contentType: UTF8String; AInit: Boolean; AMeta: Pointer; Flags: Integer);
begin
inherited Create(gridfs);
if AInit then
begin
gridfile_init(gfs.Handle, AMeta, Handle);
FInited := True;
end;
gridfile_writer_init(Handle, gridfs.Handle, PAnsiChar(remoteName), PAnsiChar(contentType), Flags);
end;
constructor TGridfileWriter.Create(gridfs: TGridFS; remoteName: UTF8String;
AInit: Boolean; AMeta: Pointer; Flags: Integer);
begin
Create(gridfs, remoteName, '', AInit, AMeta, Flags);
end;
function TGridfileWriter.write(p: Pointer; Length: UInt64): UInt64;
begin
CheckHandle;
Result := gridfile_write_buffer(Handle, p, Length);
end;
function TGridfileWriter.finish: Boolean;
begin
if Handle = nil then
Result := true
else
begin
Result := (gridfile_writer_done(Handle) = 0);
if FInited then
DestroyGridFile
else
begin
gridfile_dealloc(Handle);
FHandle := nil;
end;
end;
end;
destructor TGridfileWriter.Destroy;
begin
finish;
inherited;
end;
function TGridfileWriter.expand(bytesToExpand: UInt64): UInt64;
begin
CheckHandle;
Result := gridfile_expand(Handle, bytesToExpand);
end;
function TGridfileWriter.setSize(newSize: UInt64): UInt64;
begin
CheckHandle;
Result := gridfile_set_size(Handle, newSize);
end;
function TGridfileWriter.truncate(newSize: UInt64): UInt64;
begin
CheckHandle;
Result := gridfile_truncate(Handle, newSize);
end;
{ TGridfile }
constructor TGridfile.Create(gridfs: TGridFS);
begin
inherited Create;
gfs := gridfs;
FHandle := gridfile_create;
end;
destructor TGridfile.Destroy;
begin
DestroyGridFile;
inherited;
end;
procedure TGridfile.CheckHandle;
begin
if FHandle = nil then
raise EMongo.Create(SGridFileHandleIsNil, E_GridFileHandleIsNil);
end;
procedure TGridfile.DestroyGridFile;
begin
if FHandle <> nil then
begin
gridfile_destroy(FHandle);
gridfile_dealloc(FHandle);
FHandle := nil;
end;
end;
function TGridfile.getFilename: UTF8String;
begin
CheckHandle;
Result := UTF8String(gridfile_get_filename(FHandle));
end;
function TGridfile.getChunkSize: Integer;
begin
CheckHandle;
Result := gridfile_get_chunksize(FHandle);
end;
function TGridfile.getLength: UInt64;
begin
CheckHandle;
Result := gridfile_get_contentlength(FHandle);
end;
function TGridfile.getContentType: UTF8String;
begin
CheckHandle;
Result := UTF8String(gridfile_get_contenttype(FHandle));
end;
function TGridfile.getUploadDate: TDateTime;
begin
CheckHandle;
Result := Int64toDouble(gridfile_get_uploaddate(FHandle)) / (1000 * 24 * 60 * 60) + 25569;
end;
function TGridfile.getMD5: UTF8String;
begin
CheckHandle;
Result := UTF8String(gridfile_get_md5(FHandle));
end;
function TGridfile.getMetadata: IBson;
var
b: Pointer;
begin
CheckHandle;
b := bson_create;
try
gridfile_get_metadata(FHandle, b, True);
if bson_size(b) <= 5 then
Result := nil
else
Result := NewBsonCopy(b);
finally
bson_dealloc(b); // Dont' call Destroy for this object, data is owned by gridfile
end;
end;
function TGridfile.getChunkCount: Integer;
begin
CheckHandle;
Result := gridfile_get_numchunks(FHandle);
end;
function TGridfile.getDescriptor: IBson;
var
b : Pointer;
begin
CheckHandle;
b := bson_create;
try
gridfile_get_descriptor(FHandle, b);
Result := NewBsonCopy(b);
finally
bson_dealloc(b); // Dont' call Destroy for this object, data is owned by gridfile
end;
end;
function TGridfile.getChunk(i: Integer): IBson;
var
b: IBson;
h : Pointer;
begin
CheckHandle;
h := bson_create;
try
b := NewBson(h);
except
bson_dealloc_and_destroy(h);
raise;
end;
gridfile_get_chunk(FHandle, i, b.Handle);
if b.size <= 5 then
Result := nil
else
Result := b;
end;
function TGridfile.getChunks(i: Integer; Count: Integer): IMongoCursor;
var
Cursor: IMongoCursor;
begin
CheckHandle;
Cursor := NewMongoCursor;
Cursor.Handle := gridfile_get_chunks(FHandle, i, Count);
if Cursor.Handle = nil then
Result := nil
else
begin
Cursor.FindCalled;
Result := Cursor;
end;
end;
function TGridfile.getID: IBsonOID;
var
oid : TBsonOIDValue;
begin
CheckHandle;
oid := gridfile_get_id(FHandle);
Result := NewBsonOID;
Result.setValue(oid);
end;
function TGridfile.getStoredChunkCount: UInt64;
var
buf : IBsonBuffer;
q : IBson;
id : TBsonOIDValue;
oid : IBsonOID;
begin
CheckHandle;
id := gridfile_get_id(FHandle);
oid := NewBsonOID;
oid.setValue(id);
buf := NewBsonBuffer;
buf.Append(SFiles_id, oid);
q := buf.finish;
Result := Trunc(gfs.conn.count(gfs.fdb + '.' + gfs.FPrefix + SChunks, q));
end;
function TGridfile.Handle: Pointer;
begin
Result := FHandle;
end;
function TGridfile.read(p: Pointer; Length: UInt64): UInt64;
begin
CheckHandle;
Result := gridfile_read_buffer(FHandle, p, Length);
end;
function TGridfile.seek(offset: UInt64): UInt64;
begin
CheckHandle;
Result := gridfile_seek(FHandle, offset);
end;
end.