Skip to content
Browse files

Fixed issue in GridStore seek and GridStore read to correctly work on…

… multiple seeks (Issue #895)
  • Loading branch information...
1 parent 76bb1ae commit 85d0f7da21c3bcc1dfc2d4a3f91a8df8102daacc @christkv christkv committed Mar 14, 2013
Showing with 85 additions and 1 deletion.
  1. +1 −0 HISTORY
  2. +6 −1 lib/mongodb/gridfs/gridstore.js
  3. +78 −0 test/tests/functional/gridstore/gridstore_tests.js
View
1 HISTORY
@@ -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
-----------------
View
7 lib/mongodb/gridfs/gridstore.js
@@ -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
@@ -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) {
@@ -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() {
View
78 test/tests/functional/gridstore/gridstore_tests.js
@@ -1848,6 +1848,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
+ */
exports.shouldWriteFileWithMongofilesAndReadWithNodeJS = function(configuration, test) {
if(configuration.db().serverConfig instanceof configuration.getMongoPackage().ReplSet) return test.done();
var GridStore = configuration.getMongoPackage().GridStore

0 comments on commit 85d0f7d

Please sign in to comment.
Something went wrong with that request. Please try again.