-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocal_grid_file.cpp
More file actions
432 lines (366 loc) · 14.3 KB
/
local_grid_file.cpp
File metadata and controls
432 lines (366 loc) · 14.3 KB
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
#include "local_grid_file.h"
#include "fs_logger.h"
#include "fs_options.h"
#include "utils.h"
#include <cerrno>
#include <cstring>
#include <mongo/client/gridfs.h>
#include <mongo/client/connpool.h>
using namespace mongo;
using namespace mgridfs;
using namespace std;
LocalGridFile::LocalGridFile()
: _size(0), _capacity(0), _readOnly(false), _dirty(false), _filename("") {
}
LocalGridFile::LocalGridFile(const string& filename)
: _size(0), _capacity(0), _readOnly(false), _dirty(false), _filename(filename) {
}
LocalGridFile::~LocalGridFile() {
}
LocalMemoryGridFile::LocalMemoryGridFile()
: _chunkSize(globalFSOptions._memChunkSize) {
}
LocalMemoryGridFile::LocalMemoryGridFile(const string& filename)
: LocalGridFile(filename), _chunkSize(globalFSOptions._memChunkSize) {
}
LocalMemoryGridFile::~LocalMemoryGridFile() {
// This should be implcitly thread-safe. Should be called only once from only one
// thread, hence does not need to be thread-safe explicitly
if (_dirty) {
warn() << "Flushing file data called on in-memory file delete {filname: " << _filename
<< ", size: " << _size << ", capacity: " << _capacity << ", dirty: " << _dirty
<< ", readOnly: " << _readOnly << ", chunkSize: " << _chunkSize
<< ", chunks: " << _chunks.size() << "}. "
<< "This should have happened on file close rather than memory object deletion."
<< endl;
flush();
}
if (_chunks.size()) {
// If chunks contain data elements, delete those memory blocks as well
for (vector<char*>::iterator pIt = _chunks.begin(); pIt != _chunks.end(); ++pIt) {
delete[] (*pIt);
}
}
}
void LocalMemoryGridFile::setDirty(bool flag) {
if (!_readOnly) {
_dirty = flag;
}
}
bool LocalMemoryGridFile::setCapacity(size_t capacity) {
trace() << " -> LocalMemoryGridFile::setCapacity {file: " << _filename << ", capacity: {old: " << _capacity
<< ", new: " << capacity << "} }" << endl;
//TODO: make setCapacity to see what all it affects
return false;
}
bool LocalMemoryGridFile::setSize(size_t size) {
trace() << " -> LocalMemoryGridFile::setSize {file: " << _filename << ", size: {old: " << _size
<< ", new: " << size << "} }" << endl;
//TODO: Make setSize smarter on when-all it can change size and in which direction
if (size <= _size) {
// The file is being truncated to smaller / equal size
_size = size;
_dirty = true;
return true;
}
// The file is being expanded to larger size
if (size < _capacity) {
// Size requested is within the allocated capacity, so nothing extra
// to do
_size = size;
_dirty = true;
return true;
}
// Capacity will need to grow beyond current capacity
if (size >= globalFSOptions._maxMemFileSize) {
error() << "Memory size requested is beyond max capacity for in-memory files {filename: "
<< _filename << ", size: " << _size << ", requested-size: " << size
<< "}, will not increase the capacity." << endl;
return false;
}
size_t currentChunks = (_capacity + _chunkSize - 1) / _chunkSize;
size_t newChunks = (size + _chunkSize - 1) / _chunkSize;
if (currentChunks > newChunks) {
error() << "Something is wrong with capapcity / chunk calculations {filename: " << _filename
<< ", capacity: " << _capacity << ", size: {old: " << _size << ", old: " << size
<< "}, chunks: {old: " << currentChunks << ", new: " << newChunks << "}, chunkSize: "
<< _chunkSize << "}, will not increase capacity." << endl;
return false;
}
size_t diffChunks = newChunks - currentChunks;
for (size_t i = 0; i < diffChunks; ++i) {
char* tempData = new (nothrow) char[_chunkSize];
if (!tempData) {
error() << "Failed to allocate memory {filename: " << _filename << ", chunkSize: " << _chunkSize
<< ", diffChunks: " << diffChunks << ", AllocatedChunks: " << i
<< "}, will not continue further." << endl;
return false;
}
_chunks.push_back(tempData);
_capacity += _chunkSize;
_size = (_capacity < size) ? _capacity : size;
_dirty = true;
trace() << "Added chunk {total: " << _chunks.size() << ", capacity: " << _capacity
<< ", size: " << _size << ", requested-size: " << size << "}" << endl;
}
return true;
}
bool LocalMemoryGridFile::setReadOnly() {
trace() << " -> LocalMemoryGridFile::setReadonly {file: " << _filename << "}" << endl;
if (_dirty) {
return false;
}
_readOnly = true;
return true;
}
bool LocalMemoryGridFile::setFilename(const string& filename) {
trace() << " -> LocalMemoryGridFile::setFilename {old: " << _filename << ", new: " << filename << "}" << endl;
//TODO: check for invalid instances
if (!_filename.empty() || _dirty) {
warn() << "setFilename called on local file object when filename already exists or "
<< "is dirty {old: " << _filename << ", new: " << filename << ", isDirty: " << _dirty
<< "}, will not be updating filename." << endl;
return false;
}
//TODO: Check flows that the handles and the LocalGridFS mappings are
//being changed appropriately
_filename = filename;
return true;
}
int LocalMemoryGridFile::openRemote(int fileFlags) {
trace() << " -> LocalMemoryGridFile::openRemote {fileFlags: " << fileFlags << "}" << endl;
try {
ScopedDbConnection dbc(globalFSOptions._connectString);
GridFS gridFS(dbc.conn(), globalFSOptions._db, globalFSOptions._collPrefix);
GridFile origGridFile = gridFS.findFile(BSON("filename" << _filename));
if (!origGridFile.exists()) {
dbc.done();
error() << "Requested file not found for opening from remote {file: " << _filename << "}" << endl;
return -EBADF;
}
if (origGridFile.getContentLength() > globalFSOptions._maxMemFileSize) {
// Don't support opening files of size > MAX_MEMORY_FILE_CAPACITY in R/W mode
error() << "Requested file length is beyond supported length for in-memory files {file: "
<< _filename << ", length: {requested: " << origGridFile.getContentLength()
<< ", max-supported: " << globalFSOptions._maxMemFileSize << "} }" << endl;
return -EROFS;
}
if (!initLocalBuffers(origGridFile)) {
error() << "Failed to initialize local buffers {file: " << _filename << "}" << endl;
return -EIO;
}
_dirty = false;
dbc.done();
} catch (DBException& e) {
// Something failed in getting the file from GridFS
error() << "Caught exception in getting remote file for flush {filename: " << _filename
<< ", code: " << e.getCode() << ", what: " << e.what() << ", exception: " << e.toString()
<< "}" << endl;
return -EIO;
}
return 0;
}
int LocalMemoryGridFile::write(const char *data, size_t len, off_t offset) {
trace() << " -> LocalMemoryGridFile::write {len: " << len << ", offset: " << offset << "}" << endl;
if (_readOnly) {
debug() << "Encountered write call on a _readOnly file" << endl;
return -EROFS;
}
if (len == 0) {
return 0;
}
size_t updatedSize = (offset + len);
if (_capacity > updatedSize) {
// Nothing to be done, the size remains as it was before this
// Change size only if it is expanding
_size = (updatedSize > _size) ? updatedSize : _size;
} else if (!setSize(updatedSize)) {
return -ENOMEM;
}
return _write(data, len, offset);
}
int LocalMemoryGridFile::_write(const char *data, size_t len, off_t offset) {
// Assumes the appropriate space is available and theh chunks have been allocated
// appropriately
size_t whichChunk = (offset + 1) / _chunkSize;
size_t offsetInChunk = offset % _chunkSize;
size_t bytesWritten = 0;
char* dest = NULL;
while (bytesWritten < len) {
// TODO: Check for the data / size of the file / offset consistency / chunk tracking etc.
size_t bytesPending = len - bytesWritten;
size_t n = ((bytesPending > (_chunkSize - offsetInChunk)) ? (_chunkSize - offsetInChunk) : bytesPending);
trace() << "Writing to chunk {chunk: " << whichChunk << ", input-offset: " << bytesWritten
<< ", chunk-offset: " << offsetInChunk
<< ", n: " << n << ", bytesPending: " << bytesPending << "}" << endl;
dest = _chunks[whichChunk];
memcpy(dest + offsetInChunk, (data + bytesWritten), n);
bytesWritten += n;
offsetInChunk = 0;
++whichChunk;
}
_dirty = true;
return len;
}
int LocalMemoryGridFile::read(char *data, size_t len, off_t offset) const {
trace() << " -> LocalMemoryGridFile::read {len: " << len << ", offset: " << offset << "}" << endl;
if (offset >= (off_t)_size) {
return -EOF;
}
if (len == 0) {
return 0;
}
// Read from the chunks of in-memory grid file to buffer
unsigned int numChunks = (_size + 1) / _chunkSize;
//TODO: Implement the file offset tracking for the file
unsigned int activeChunkNum = offset / _chunkSize;
size_t bytesRead = 0;
while (bytesRead < len && activeChunkNum < numChunks) {
int bytesToRead = 0;
//TODO: Change the chunkLen to be correct length in case of last chunk
int chunkLen = _chunkSize;
const char* chunkData = _chunks[activeChunkNum];
if (!chunkData) {
warn() << "Encountered NULL chunk data while reading in-memory chunk {activeChunkNum: " << activeChunkNum
<< "}, will return IO error to the reader." << endl;
return -EIO;
}
if (bytesRead) {
// If we are in-between reading data, check on what data is left to be read
bytesToRead = min((size_t)chunkLen, (len - bytesRead));
memcpy(data + bytesRead, chunkData, bytesToRead);
} else {
// This is the first chunk we are reading and could be starting from in-between offset of the
// chunk that was previously read partially
bytesToRead = min((size_t)(chunkLen - (offset % _chunkSize)), (size_t)(len - bytesRead));
memcpy(data + bytesRead, chunkData + (offset % _chunkSize), bytesToRead);
}
bytesRead += bytesToRead;
++activeChunkNum;
}
return bytesRead;
}
int LocalMemoryGridFile::flush() {
trace() << " -> LocalMemoryGridFile::flush {file: " << _filename << "}" << endl;
if (!_dirty) {
// Since, there are no dirty chunks, this does not need a flush
info() << "buffers are not dirty.. need not flush {filename: " << _filename << "}" << endl;
return 0;
}
size_t bufferLen = 0;
boost::shared_array<char> buffer = createFlushBuffer(bufferLen);
if (!buffer.get() && bufferLen > 0) {
// Failed to create flush buffer
return -ENOMEM;
}
// Get the existing gridfile from GridFS to get metadata and delete the
// file from the system
try {
ScopedDbConnection dbc(globalFSOptions._connectString);
GridFS gridFS(dbc.conn(), globalFSOptions._db, globalFSOptions._collPrefix);
GridFile origGridFile = gridFS.findFile(BSON("filename" << _filename));
if (!origGridFile.exists()) {
dbc.done();
warn() << "Requested file not found for flushing back data {file: " << _filename << "}" << endl;
return -EBADF;
}
//TODO: Make checks for appropriate object correctness
//i.e. do not update anything that is not a Regular File
//Check what happens in case of a link
gridFS.removeFile(_filename);
trace() << "Removing the current file from GridFS {file: " << _filename << "}" << endl;
//TODO: Check for remove status if that was successfull or not
//TODO: Rather have an update along with active / passive flag for the
//file
try {
GridFS gridFS(dbc.conn(), globalFSOptions._db, globalFSOptions._collPrefix);
// Create an empty file to signify the file creation and open a local file for the same
trace() << "Adding new file to GridFS {file: " << _filename << "}" << endl;
BSONObj fileObj = gridFS.storeFile(buffer.get(), bufferLen, _filename);
if (!fileObj.isValid()) {
warn() << "Failed to save file object in data flush {file: " << _filename << "}" << std::endl;
dbc.done();
return -EBADF;
}
// Update the last updated date for the document
BSONObj metadata = origGridFile.getMetadata();
BSONElement fileObjId = fileObj.getField("_id");
dbc->update(globalFSOptions._filesNS, BSON("_id" << fileObjId.OID()),
BSON("$set" << BSON(
"uploadDate" << origGridFile.getUploadDate()
<< "metadata.type" << "file"
<< "metadata.filename" << mgridfs::getPathBasename(_filename)
<< "metadata.directory" << mgridfs::getPathDirname(_filename)
<< "metadata.lastUpdated" << jsTime()
<< "metadata.uid" << metadata["uid"]
<< "metadata.gid" << metadata["gid"]
<< "metadata.mode" << metadata["mode"]
)
)
);
} catch (DBException& e) {
error() << "Caught exception in saving remote file in flush {code: " << e.getCode() << ", what: " << e.what()
<< ", exception: " << e.toString() << "}" << endl;
return -EIO;
}
dbc.done();
} catch (DBException& e) {
// Something failed in getting the file from GridFS
error() << "Caught exception in getting remote file for flush {code: " << e.getCode() << ", what: " << e.what()
<< ", exception: " << e.toString() << "}" << endl;
return -EIO;
}
_dirty = false;
debug() << "Completed flushing the file content to GridFS {file: " << _filename << "}" << endl;
return 0;
}
boost::shared_array<char> LocalMemoryGridFile::createFlushBuffer(size_t& bufferLen) const {
bufferLen = _size;
boost::shared_array<char> tempBuffer;
if (!bufferLen) {
// If the buffer is 0 length, then return NULL with bufferLen = 0
return tempBuffer;
}
tempBuffer.reset(new (nothrow) char[bufferLen]);
if (!tempBuffer.get()) {
// Failed to allocate memory, return
error() << "Failed to allocate temp buffer for full file {filename: " << _filename
<< ", size: " << _size << "}, will return empty buffer without data."
<< endl;
return tempBuffer;
}
// If the buffers are allocated, copy the data from memory chunks to
// the temporary flush buffer
size_t pending = _size;
size_t offset = 0;
for (size_t i = 0; i < _chunks.size() && pending > 0; ++i) {
size_t bytesToCopy = (pending >= _chunkSize ? _chunkSize : pending);
memcpy(tempBuffer.get() + offset, _chunks[i], bytesToCopy);
pending -= bytesToCopy;
offset += bytesToCopy;
}
return tempBuffer;
}
bool LocalMemoryGridFile::initLocalBuffers(GridFile& gridFile) {
if (!setSize(gridFile.getContentLength())) {
return false;
}
//TODO: Check for size compared to the content length that was specified for consistency
off_t offset = 0;
int chunkCount = gridFile.getNumChunks();
for (int i = 0; i < chunkCount; ++i) {
//TODO: Error checking or getChunk and related handling
GridFSChunk chunk = gridFile.getChunk(i);
int chunkLen = 0;
const char* data = chunk.data(chunkLen);
if (!data) {
error() << "Failed to get data from expected chunk {file: " << _filename
<< ", chunkOffset: " << i << ", totalChunks: " << chunkCount
<< "}, will stop in-between and return error." << endl;
return false;
}
_write(data, chunkLen, offset);
offset += chunkLen;
}
return true;
}