Skip to content

Commit

Permalink
Fixed issue in GridStore seek and GridStore read to correctly work on…
Browse files Browse the repository at this point in the history
… multiple seeks (Issue #895)
  • Loading branch information
christkv committed Mar 14, 2013
1 parent 76bb1ae commit 85d0f7d
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
1 change: 1 addition & 0 deletions HISTORY
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Fixed issues related to node 0.10 and process.nextTick now correctly using setImmediate where needed on node 0.10
- Added support for maxMessageSizeBytes if available (DRIVERS-1)
- Added support for authSource (2.4) to MongoClient URL and db.authenticate method (DRIVER-69/NODE-34)
- Fixed issue in GridStore seek and GridStore read to correctly work on multiple seeks (Issue #895)

1.2.13 2013-02-22
-----------------
Expand Down
7 changes: 6 additions & 1 deletion lib/mongodb/gridfs/gridstore.js
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ GridStore.prototype.read = function(length, buffer, callback) {
// Copy content to final buffer
slice.copy(finalBuffer, finalBuffer._index);
// Update internal position
self.position = finalBuffer.length;
self.position = self.position + finalBuffer.length;
// Check if we don't have a file at all
if(finalLength == 0 && finalBuffer.length == 0) return callback(new Error("File does not exist"), null);
// Else return data
Expand Down Expand Up @@ -818,6 +818,10 @@ GridStore.prototype.seek = function(position, seekLocation, callback) {
var seekLocationFinal = seekLocation == null ? exports.GridStore.IO_SEEK_SET : seekLocation;
var finalPosition = position;
var targetPosition = 0;

// console.dir(targetPosition)

// Calculate the position
if(seekLocationFinal == exports.GridStore.IO_SEEK_CUR) {
targetPosition = self.position + finalPosition;
} else if(seekLocationFinal == exports.GridStore.IO_SEEK_END) {
Expand All @@ -826,6 +830,7 @@ GridStore.prototype.seek = function(position, seekLocation, callback) {
targetPosition = finalPosition;
}

// Get the chunk
var newChunkNumber = Math.floor(targetPosition/self.chunkSize);
if(newChunkNumber != self.currentChunk.chunkNumber) {
var seekChunk = function() {
Expand Down
78 changes: 78 additions & 0 deletions test/tests/functional/gridstore/gridstore_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1845,6 +1845,84 @@ exports.shouldCorrectlyHandleSeekIntoSecondChunkWithStream = function(configurat
});
}

/**
* @ignore
*/
exports['Should correctly handle multiple seeks'] = function(configuration, test) {
var GridStore = configuration.getMongoPackage().GridStore
, ObjectID = configuration.getMongoPackage().ObjectID;
var db = configuration.db();

var gridStore = new GridStore(db, "test_gs_seek_with_buffer", "w");
gridStore.open(function(err, gridStore) {
gridStore.write(new Buffer("012345678901234567890", "utf8"), function(err, gridStore) {
gridStore.close(function(result) {
var gridStore2 = new GridStore(db, "test_gs_seek_with_buffer", "r");
gridStore2.open(function(err, gridStore2) {

gridStore2.read( 5, function(err, data) {
test.equal("01234", data.toString());

gridStore2.seek(-2, GridStore.IO_SEEK_CUR, function(err, gridStore2) {

gridStore2.read( 5, function(err, data) {
test.equal("34567", data.toString());

gridStore2.seek(-2, GridStore.IO_SEEK_CUR, function(err, gridStore2) {

gridStore2.read( 5, function(err, data) {
test.equal("67890", data.toString());
test.done();
});
});
});
});
});
});
});
});
});
}

/**
* @ignore
*/
exports['Should correctly handle multiple seeks over several chunks'] = function(configuration, test) {
var GridStore = configuration.getMongoPackage().GridStore
, ObjectID = configuration.getMongoPackage().ObjectID;
var db = configuration.db();

var gridStore = new GridStore(db, "test_gs_seek_with_buffer", "w", {chunk_size:4});
gridStore.open(function(err, gridStore) {
gridStore.write(new Buffer("012345678901234567890", "utf8"), function(err, gridStore) {
gridStore.close(function(result) {
var gridStore2 = new GridStore(db, "test_gs_seek_with_buffer", "r");
gridStore2.open(function(err, gridStore2) {

gridStore2.read( 5, function(err, data) {
test.equal("01234", data.toString());

gridStore2.seek(-2, GridStore.IO_SEEK_CUR, function(err, gridStore2) {

gridStore2.read( 5, function(err, data) {
test.equal("34567", data.toString());

gridStore2.seek(-2, GridStore.IO_SEEK_CUR, function(err, gridStore2) {

gridStore2.read( 5, function(err, data) {
test.equal("67890", data.toString());
test.done();
});
});
});
});
});
});
});
});
});
}

/**
* @ignore
*/
Expand Down

0 comments on commit 85d0f7d

Please sign in to comment.