Skip to content

Commit

Permalink
Added support for filename being seperate to fileId, and removed uniq…
Browse files Browse the repository at this point in the history
…ue index on filename
  • Loading branch information
christkv committed Mar 7, 2012
1 parent 665e273 commit 6d9e7b4
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 39 deletions.
52 changes: 37 additions & 15 deletions lib/mongodb/gridfs/gridstore.js
Expand Up @@ -36,21 +36,47 @@ var REFERENCE_BY_FILENAME = 0,
*
* @class Represents the GridStore.
* @param {Db} db A database instance to interact with.
* @param {String|ObjectID} fileIdObject ObjectID or the name for the file.
* @param {ObjectID} id an unique ObjectID for this file
* @param {String} filename a non unique filename for this file
* @param {String} mode set the mode for this file.
* @param {Object} options optional properties to specify. Recognized keys:
* @return {GridStore}
*/
function GridStore(db, fileIdObject, mode, options) {
function GridStore(db, id, filename, mode, options) {
if(!(this instanceof GridStore)) return new GridStore(db, fileIdObject, mode, options);

var self = this;
this.db = db;
var _filename = filename;

// console.log("----------------------------------------------------------- PRE")
// console.dir(id)
// console.dir(filename)
// console.dir(mode)
// console.dir(options)

if(typeof filename == 'string' && typeof mode == 'string') {
_filename = filename;
} else if(typeof filename == 'string' && typeof mode == 'object' && mode != null) {
var _mode = mode;
mode = filename;
options = _mode;
_filename = id;
} else if(typeof filename == 'string' && mode == null) {
mode = filename;
_filename = id;
}

// console.log("----------------------------------------------------------- AFTER")
// console.dir(id)
// console.dir(filename)
// console.dir(mode)
// console.dir(options)

// set grid referencetype
this.referenceBy = typeof fileIdObject == 'string' ? 0 : 1;
this.filename = fileIdObject;
this.fileId = fileIdObject;
this.referenceBy = typeof id == 'string' ? 0 : 1;
this.filename = _filename;
this.fileId = id;

// Set up the rest
this.mode = mode == null ? "r" : mode;
Expand Down Expand Up @@ -117,15 +143,11 @@ GridStore.prototype.open = function(callback) {
if((self.mode == "w" || self.mode == "w+") && self.db.serverConfig.primary != null) {
// Get files collection
self.collection(function(err, collection) {
// Ensure index on files Collection
collection.ensureIndex([['filename', 1], ['uploadDate', -1]], function(err, index) {

// Get chunk collection
self.chunkCollection(function(err, chunkCollection) {
// Ensure index on chunk collection
chunkCollection.ensureIndex([['files_id', 1], ['n', 1]], function(err, index) {
_open(self, callback);
});
// Get chunk collection
self.chunkCollection(function(err, chunkCollection) {
// Ensure index on chunk collection
chunkCollection.ensureIndex([['files_id', 1], ['n', 1]], function(err, index) {
_open(self, callback);
});
});
});
Expand Down
110 changes: 86 additions & 24 deletions test/gridstore/grid_store_file_test.js
Expand Up @@ -926,30 +926,92 @@ exports.shouldCorrectlyRetrieveSingleCharacterUsingGetC = function(test) {
});
}

// /**
// * @ignore
// */
// exports.shouldNotThrowErrorOnClose = function(test) {
// var fieldId = new ObjectID();
// var gridStore = new GridStore(client, fieldId, "w", {root:'fs'});
// gridStore.chunkSize = 1024 * 256;
// gridStore.open(function(err, gridStore) {
// Step(
// function writeData() {
// var group = this.group();
// for(var i = 0; i < 1000000; i += 5000) {
// gridStore.write(new Buffer(5000), group());
// }
// },
//
// function doneWithWrite() {
// gridStore.close(function(err, result) {
// test.done();
// });
// }
// )
// });
// }
/**
* @ignore
*/
exports.shouldNotThrowErrorOnClose = function(test) {
var fieldId = new ObjectID();
var gridStore = new GridStore(client, fieldId, "w", {root:'fs'});
gridStore.chunkSize = 1024 * 256;
gridStore.open(function(err, gridStore) {
Step(
function writeData() {
var group = this.group();
for(var i = 0; i < 1000000; i += 5000) {
gridStore.write(new Buffer(5000), group());
}
},

function doneWithWrite() {
gridStore.close(function(err, result) {
test.done();
});
}
)
});
}

/**
* A simple example showing how to save a file with a filename allowing for multiple files with the same name
*
* @_class gridstore
* @_function open
* @ignore
*/
exports.shouldCorrectlyRetrieveSingleCharacterUsingGetC = function(test) {
var db = new Db('integration_tests', new Server("127.0.0.1", 27017,
{auto_reconnect: false, poolSize: 1, ssl:useSSL}), {native_parser: native_parser});

// Establish connection to db
db.open(function(err, db) {
// Create a file and open it
var gridStore = new GridStore(db, new ObjectID(), "test_gs_getc_file", "w");
gridStore.open(function(err, gridStore) {
// Write some content to the file
gridStore.write(new Buffer("hello, world!", "utf8"), function(err, gridStore) {
// Flush the file to GridFS
gridStore.close(function(err, fileData) {
test.equal(null, err);

// Create another file with same name and and save content to it
gridStore = new GridStore(db, new ObjectID(), "test_gs_getc_file", "w");
gridStore.open(function(err, gridStore) {
// Write some content to the file
gridStore.write(new Buffer("hello, world!", "utf8"), function(err, gridStore) {
// Flush the file to GridFS
gridStore.close(function(err, fileData) {
test.equal(null, err);

// Open the file in read mode using the filename
var gridStore2 = new GridStore(db, "test_gs_getc_file", "r");
gridStore2.open(function(err, gridStore) {

// Read first character and verify
gridStore.getc(function(err, chr) {
test.equal('h', chr);

// Open the file using an object id
gridStore2 = new GridStore(db, fileData._id, "r");
gridStore2.open(function(err, gridStore) {

// Read first character and verify
gridStore.getc(function(err, chr) {
test.equal('h', chr);

db.close();
test.done();
})
});
});
});
});
});
});
});
});
});
});
}

/**
* Retrieve the server information for the current
Expand Down

0 comments on commit 6d9e7b4

Please sign in to comment.