Skip to content

Commit

Permalink
Added function SyncIt.getFirstInDatasets()
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Forrester committed May 12, 2014
1 parent e223cfc commit 50d22b5
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 40 deletions.
38 changes: 25 additions & 13 deletions Path/AsyncLocalStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -1095,8 +1095,8 @@ Als.prototype.getDatakeysInDataset = function(dataset,next) {
* * **@param {String} `dataset`**
* * **@param {String} `datakey`**
*/
Als.prototype.findFirstDatasetDatakey = function(path,next) {
this._findFirstDatasetDatakeyReference(path,function(err,dataset,datakey) {
Als.prototype.findFirstDatasetDatakey = function(datasets,path,next) {
this._findFirstDatasetDatakeyReference(datasets,path,function(err,dataset,datakey) {
if (err !== ERROR.OK) {
return next(err);
}
Expand All @@ -1105,11 +1105,12 @@ Als.prototype.findFirstDatasetDatakey = function(path,next) {
};

/**
* ## SyncIt_Path_AsyncLocalStorage.getFirstPathitem()
* ## SyncIt_Path_AsyncLocalStorage.getFirstPathitemInDatasets()
*
* Will find the first Pathitem via the lowest Reference and return it along
* with the Dataset, Datakey and Pathitem.
*
* * **@param {Array|null} `datasets`** An array of datasets which are acceptable, or null for any.
* * **@param {Path} `path`**
* * **@param {Function} `next`** Signature: Function(err, dataset, datakey, reference, pathitem)
* * **@param {Errorcode} `err`**
Expand All @@ -1118,15 +1119,19 @@ Als.prototype.findFirstDatasetDatakey = function(path,next) {
* * **@param {String} `reference`**
* * **@param {Pathitem} `pathitem`**
*/
Als.prototype.getFirstPathitem = function(path,next) {
this._findFirstDatasetDatakeyReference(path,function(err,dataset,datakey,reference) {
if (err !== ERROR.OK) {
return next(err);
}
return this._getPathItem(dataset,datakey,reference,function(err,pathitem) {
next(err,dataset,datakey,reference,pathitem);
});
}.bind(this));
Als.prototype.getFirstPathitem = function(datasets,path,next) {
this._findFirstDatasetDatakeyReference(
datasets,
path,
function(err,dataset,datakey,reference) {
if (err !== ERROR.OK) {
return next(err);
}
return this._getPathItem(dataset,datakey,reference,function(err,pathitem) {
next(err,dataset,datakey,reference,pathitem);
});
}.bind(this)
);
};

/**
Expand All @@ -1135,11 +1140,12 @@ Als.prototype.getFirstPathitem = function(path,next) {
* Will find the lowest Reference and return it along with the Dataset, Datakey
* it was found at.
*
* * **@param {Array|null} `datasets`** An array of datasets which are acceptable, or null for any
* * **@param {Path} `path`**
* * **@param {Function} `next`** Signature: Function(err)
* * **@param {Errorcode} `err`**
*/
Als.prototype._findFirstDatasetDatakeyReference = function(path,next) {
Als.prototype._findFirstDatasetDatakeyReference = function(datasets,path,next) {
this._ls.findKeys('*.*.*',function(keys) {
keys = keys.sort(this._ed.sort);
var processOne = function() {
Expand All @@ -1160,6 +1166,12 @@ Als.prototype._findFirstDatasetDatakeyReference = function(path,next) {
if (err !== ERROR.OK) {
return next(err);
}
if (
(datasets !== null) &&
(datasets.indexOf(k[0]) == -1)
) {
return processOne();
}
if (
root.hasOwnProperty(path) &&
root[path].hasOwnProperty('_n') &&
Expand Down
11 changes: 8 additions & 3 deletions SyncIt.js
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,7 @@ SyncIt.prototype.advance = function(done) {
this._lockFor(LOCKING.ADVANCING);
var addedPathkey = '';

this._ps.findFirstDatasetDatakey('a',function(err,dataset,datakey) {
this._ps.findFirstDatasetDatakey(null,'a',function(err,dataset,datakey) {
if (err !== ERROR.OK) {
this._unlockFor(LOCKING.ADVANCING);
return done(err);
Expand Down Expand Up @@ -1062,20 +1062,22 @@ SyncIt.prototype.getVersion = function(dataset, datakey, whenVersionFound) {


/**
* ### SyncIt.getFirst()
* ### SyncIt.getFirstInDatasets()
*
* Gets the Pathitem which should be next uploaded to the server or will be advanced
* should `SyncIt.advance()` be called.
*
* #### Parameters
*
* * **@param {Array|null} `datasets`** An array of datasets which are acceptable, or null for any
* * **@param {Function} `done`** Callback. Signature: Function(err, pathitem)
* * **@param {ErrorCode} `done.err`** See SyncIt_Constant.Error.
* * **@param {Pathitem} `done.pathitem`** The Pathitem.
*/
SyncIt.prototype.getFirst = function(done) {
SyncIt.prototype.getFirstInDatasets = function(datasets, done) {

this._ps.getFirstPathitem(
null,
'a',
function(err,dataset,datakey,reference,queueitem) {

Expand Down Expand Up @@ -1104,6 +1106,9 @@ SyncIt.prototype.getFirst = function(done) {

};

SyncIt.prototype.getFirst = function(done) {
this.getFirstInDatasets(null, done);
};

/**
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sync-it",
"version": "0.9.10",
"version": "0.9.11",
"homepage": "https://github.com/forbesmyester/SyncIt",
"authors": [
"Matthew Forrester <[email protected]>"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sync-it",
"version": "0.9.10",
"version": "0.9.11",
"description": "Framework for data synchronization",
"author": "Matthew Forrester <[email protected]>",
"dependencies": {
Expand Down
65 changes: 43 additions & 22 deletions test/Path/AsyncLocalStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,7 @@ describe('SyncIt_Path_AsyncLocalStorage',function() {
});
});

it('can clean and get first queueitem',function(done) {
this.timeout(60000);
var setupForGetFirstTest = function(next) {
var localStorage = new SyncIt_FakeLocalStorage();
var asyncLocalStorage = new AsyncLocalStorage(
localStorage,
Expand Down Expand Up @@ -492,26 +491,7 @@ describe('SyncIt_Path_AsyncLocalStorage',function() {
{from:'bikes.harley:p',to:'_4'},
{from:'bikes.kawasaki:p',to:'_6'}
]);
pathStore.findFirstDatasetDatakey('p',function(err,dataset,datakey) {
expect(err).to.equal(ERROR.OK);
expect([dataset,datakey]).to.eql(['cars','subaru']);
pathStore.clean(function(err) {
expect(err).to.equal(SyncIt_Constant.Error.OK);
expect(
getPaths(localStorage,'aa','p')
).to.eql([
{from:'cars.subaru._2',to:'_3'},
{from:'cars.subaru._3',to:null},
{from:'bikes.harley._4',to:'_5'},
{from:'bikes.harley._5',to:null},
{from:'bikes.kawasaki._6',to:null},
{from:'cars.subaru:p',to:'_2'},
{from:'bikes.harley:p',to:'_4'},
{from:'bikes.kawasaki:p',to:'_6'}
]);
done();
});
});
next(localStorage,pathStore);
});
});
});
Expand All @@ -520,6 +500,47 @@ describe('SyncIt_Path_AsyncLocalStorage',function() {
});
});
});
};

it('can clean and get first queueitem in specific datasets', function(done) {
this.timeout(60000);
setupForGetFirstTest(function(localStorage,pathStore) {
pathStore.findFirstDatasetDatakey(['cars'],'p',function(err,dataset,datakey) {
expect(err).to.equal(ERROR.OK);
expect([dataset,datakey]).to.eql(['cars','subaru']);
pathStore.findFirstDatasetDatakey(['bikes'],'p',function(err,dataset,datakey) {
expect(err).to.equal(ERROR.OK);
expect([dataset,datakey]).to.eql(['bikes','harley']);
done();
});
});
});
});

it('can clean and get first queueitem', function(done) {
this.timeout(60000);
setupForGetFirstTest(function(localStorage,pathStore) {
pathStore.findFirstDatasetDatakey(null,'p',function(err,dataset,datakey) {
expect(err).to.equal(ERROR.OK);
expect([dataset,datakey]).to.eql(['cars','subaru']);
pathStore.clean(function(err) {
expect(err).to.equal(SyncIt_Constant.Error.OK);
expect(
getPaths(localStorage,'aa','p')
).to.eql([
{from:'cars.subaru._2',to:'_3'},
{from:'cars.subaru._3',to:null},
{from:'bikes.harley._4',to:'_5'},
{from:'bikes.harley._5',to:null},
{from:'bikes.kawasaki._6',to:null},
{from:'cars.subaru:p',to:'_2'},
{from:'bikes.harley:p',to:'_4'},
{from:'bikes.kawasaki:p',to:'_6'}
]);
done();
});
});
});
});

it('will exit when nothing to clean',function(done) {
Expand Down

0 comments on commit 50d22b5

Please sign in to comment.