Commit 42324df2 by James Cropcho

Merge pull request #105 from DavidWittman/alt-results-database

Add support for external results database
parents 4066f2b2 b2a70ee6
...@@ -23,7 +23,8 @@ ...@@ -23,7 +23,8 @@
"outputFormat": true, "outputFormat": true,
"persistResults": true, "persistResults": true,
"plugins": true, "plugins": true,
"slaveOk" : true "slaveOk" : true,
"connect": true
}, },
"latedef": true, "latedef": true,
"singleGroups": true, "singleGroups": true,
......
...@@ -155,12 +155,23 @@ by setting the ```slaveOk``` property to ```true```: ...@@ -155,12 +155,23 @@ by setting the ```slaveOk``` property to ```true```:
$ mongo secondary.replicaset.member:31337/somedb --eval "var collection = 'users', slaveOk = true" variety.js $ mongo secondary.replicaset.member:31337/somedb --eval "var collection = 'users', slaveOk = true" variety.js
### Save Results in MongoDB For Future Use ### ### Save Results in MongoDB For Future Use ###
By default, Variety prints results only to standard output and does not store them in MongoDB itself. If you want to persist them automatically in database for later usage, you can set the parameter ```persistResults```. By default, Variety prints results only to standard output and does not store them in MongoDB itself. If you want to persist them automatically in MongoDB for later usage, you can set the parameter ```persistResults```.
Variety then stores result documents in database ```varietyResults``` and the collection name is derived from the source collection's name. Variety then stores result documents in database ```varietyResults``` and the collection name is derived from the source collection's name.
If the source collection's name is ```users```, Variety will store results in collection ```usersKeys``` under ```varietyResults``` database. If the source collection's name is ```users```, Variety will store results in collection ```usersKeys``` under ```varietyResults``` database.
$ mongo test --quiet --eval "var collection = 'users', persistResults=true" variety.js $ mongo test --quiet --eval "var collection = 'users', persistResults=true" variety.js
To persist to an alternate MongoDB database, you may specify the following parameters:
* `resultsDatabase` - The database to store Variety results in. Accepts either a database name or a `host[:port]/database` URL.
* `resultsCollection` - Collection to store Variety results in. **WARNING:** This collection is dropped before results are inserted.
* `resultsUser` - MongoDB username for results database
* `resultsPass` - MongoDB password for results database
```
$ mongo test --quiet --eval "var collection = 'users', persistResults=true, resultsDatabase='db.example.com/variety' variety.js
```
### Command Line Interface ### Command Line Interface
Variety itself is command line friendly, as shown on examples above. Variety itself is command line friendly, as shown on examples above.
But if you are a NPM and Node.js user, you could prefer the But if you are a NPM and Node.js user, you could prefer the
......
...@@ -77,6 +77,10 @@ var readConfig = function(configProvider) { ...@@ -77,6 +77,10 @@ var readConfig = function(configProvider) {
read('sort', {_id: -1}); read('sort', {_id: -1});
read('outputFormat', 'ascii'); read('outputFormat', 'ascii');
read('persistResults', false); read('persistResults', false);
read('resultsDatabase', 'varietyResults');
read('resultsCollection', collection + 'Keys');
read('resultsUser', null);
read('resultsPass', null);
return config; return config;
}; };
...@@ -297,11 +301,23 @@ var varietyResults = convertResults(interimResults, cursor.size()) ...@@ -297,11 +301,23 @@ var varietyResults = convertResults(interimResults, cursor.size())
.sort(comparator); .sort(comparator);
if(config.persistResults) { if(config.persistResults) {
var resultsDB = db.getMongo().getDB('varietyResults'); var resultsDB;
var resultsCollectionName = collection + 'Keys'; var resultsCollectionName = config.resultsCollection;
if (config.resultsDatabase.indexOf('/') === -1) {
// Local database; don't reconnect
resultsDB = db.getMongo().getDB(config.resultsDatabase);
} else {
// Remote database, establish new connection
resultsDB = connect(config.resultsDatabase);
}
if (config.resultsUser !== null && config.resultsPass !== null) {
resultsDB.auth(config.resultsUser, config.resultsPass);
}
// replace results collection // replace results collection
log('creating results collection: '+resultsCollectionName); log('replacing results collection: '+ resultsCollectionName);
resultsDB[resultsCollectionName].drop(); resultsDB[resultsCollectionName].drop();
resultsDB[resultsCollectionName].insert(varietyResults); resultsDB[resultsCollectionName].insert(varietyResults);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment