Skip to content

Commit

Permalink
CXX-603 Fix for dropping parts of blocks appended to a gridfs file wi…
Browse files Browse the repository at this point in the history
…th appendChunk

and add unit test for gridfs GridFileBuilder to check it handles block boundries correctly

Signed-off-by: Andrew Morrow <acm@mongodb.com>
  • Loading branch information
craftit authored and acmorrow committed May 19, 2015
1 parent ef3ed2f commit 9980a0b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
9 changes: 8 additions & 1 deletion src/mongo/client/gridfs.cpp
Expand Up @@ -321,7 +321,14 @@ namespace mongo {
invariant( _pendingDataSize <= _chunkSize );
if (_pendingDataSize == _chunkSize) {
_appendPendingData();
_appendChunk( data + size, length - size, false );
const char* const end = data + length;
data = _appendChunk( data + size, length - size, false );
if (data != end) {
invariant(data < end);
size_t nsize = static_cast<size_t>(end - data);
memcpy( _pendingData.get() + _pendingDataSize, data, nsize );
_pendingDataSize += nsize;
}
}
}
else {
Expand Down
32 changes: 28 additions & 4 deletions src/mongo/integration/standalone/gridfs_test.cpp
Expand Up @@ -226,21 +226,45 @@ namespace {

TEST_F(GridFSTest, GridFileBuilder) {
GridFileBuilder gfb(_gfs.get());
for (int i=0; i<DATA_LEN; i+=2)
gfb.appendChunk(DATA + i, min(2, DATA_LEN - i));
size_t totalSize = 0;
for (int i=0; i<DATA_LEN; i+=2) {
size_t chunkSize = min(2, DATA_LEN - i);
gfb.appendChunk(DATA + i, chunkSize);
totalSize += chunkSize;
}
gfb.buildFile(DATA_NAME);
GridFile gf = _gfs->findFileByName(DATA_NAME);
ASSERT_EQUALS(gf.getNumChunks(), 1);
ASSERT_EQUALS(gf.getContentLength(), totalSize);
}

TEST_F(GridFSTest, GridFileBuilderMultipleChunks) {
_gfs->setChunkSize(1);
GridFileBuilder gfb(_gfs.get());
for (int i=0; i<DATA_LEN; i+=2)
gfb.appendChunk(DATA + i, min(2, DATA_LEN - i));
size_t total_size = 0;
for (int i=0; i<DATA_LEN; i+=2) {
size_t chunk_size = min(2, DATA_LEN - i);
gfb.appendChunk(DATA + i, chunk_size);
total_size += chunk_size;
}
gfb.buildFile(DATA_NAME);
GridFile gf = _gfs->findFileByName(DATA_NAME);
ASSERT_EQUALS(gf.getNumChunks(), DATA_LEN);
ASSERT_EQUALS(gf.getContentLength(), total_size);
}

TEST_F(GridFSTest, GridFileBuilderAcrossChunkBoundry) {
_gfs->setChunkSize(11);
GridFileBuilder gfb(_gfs.get());
size_t total_size = 0;
for (int i=0; i<DATA_LEN; i+=2) {
size_t chunk_size = min(2, DATA_LEN - i);
gfb.appendChunk(DATA + i, chunk_size);
total_size += chunk_size;
}
gfb.buildFile(DATA_NAME);
GridFile gf = _gfs->findFileByName(DATA_NAME);
ASSERT_EQUALS(gf.getContentLength(), total_size);
}

} // namespace

0 comments on commit 9980a0b

Please sign in to comment.