Skip to content

Commit

Permalink
temp views are actually temp
Browse files Browse the repository at this point in the history
  • Loading branch information
calvinmetcalf committed Feb 8, 2016
1 parent 0f33fb8 commit 5d54960
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 86 deletions.
69 changes: 39 additions & 30 deletions create-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,23 @@ var createHash = require('create-hash');
var Promise = require('lie');

function hash(string) {
return createHash('sha224').update(string).digest('hex');
return createHash('sha224').update(string).digest('hex');
}


module.exports = function (sourceDB, viewCode, temporary, viewName) {

var viewSignature = (temporary ? 'temp' : hash(viewCode.toString()));
if (!temporary && sourceDB._cachedViews) {
if (temporary) {
return Promise.resolve({
db: {
temp: true,
get: function () {
return Promise.resolve({_id: '_local/gclastSeq', last_seq: 0});
}
}
});
}
var viewSignature = hash(viewCode.toString());
if (sourceDB._cachedViews) {
var cachedView = sourceDB._cachedViews[viewSignature];
if (cachedView) {
return Promise.resolve(cachedView);
Expand All @@ -28,7 +37,7 @@ module.exports = function (sourceDB, viewCode, temporary, viewName) {
function diffFunction(doc) {
doc.views = doc.views || {};
var fullViewName = viewSignature;

var depDbs = doc.views[fullViewName] = doc.views[fullViewName] || {};
/* istanbul ignore if */
if (depDbs[depDbName]) {
Expand All @@ -49,7 +58,7 @@ module.exports = function (sourceDB, viewCode, temporary, viewName) {
db.auto_compaction = true;
var view = {
name: depDbName,
db: db,
db: db,
sourceDB: sourceDB,
adapter: sourceDB.adapter
};
Expand All @@ -61,34 +70,34 @@ module.exports = function (sourceDB, viewCode, temporary, viewName) {
}).then(function (lastSeqDoc) {
view.seq = lastSeqDoc ? lastSeqDoc.seq : 0;
var viewID;
if (!temporary) {
sourceDB._cachedViews = sourceDB._cachedViews || {};
sourceDB._cachedViews[viewSignature] = view;
view.db.on('destroyed', function () {
delete sourceDB._cachedViews[viewSignature];
upsert(sourceDB, '_local/gcviews', cleanUpView);
});
viewID = '_design/' + viewName.split('/')[0];
sourceDB._viewListeners = sourceDB._viewListeners || {};
if (!sourceDB._viewListeners[viewID]) {
sourceDB.info().then(function (info) {
sourceDB._viewListeners[viewID] = sourceDB.changes({live: true, since: info.update_seq});
sourceDB._viewListeners[viewID].on('change', function (ch) {
if (ch.id !== viewID) {
return;
}
view.db.destroy();
sourceDB._viewListeners[viewID].cancel();
delete sourceDB._viewListeners[viewID];
delete sourceDB._cachedViews[viewSignature];
upsert(sourceDB, '_local/gcviews', cleanUpView);
});

sourceDB._cachedViews = sourceDB._cachedViews || {};
sourceDB._cachedViews[viewSignature] = view;
view.db.on('destroyed', function () {
delete sourceDB._cachedViews[viewSignature];
upsert(sourceDB, '_local/gcviews', cleanUpView);
});
viewID = '_design/' + viewName.split('/')[0];
sourceDB._viewListeners = sourceDB._viewListeners || {};
if (!sourceDB._viewListeners[viewID]) {
sourceDB.info().then(function (info) {
sourceDB._viewListeners[viewID] = sourceDB.changes({live: true, since: info.update_seq});
sourceDB._viewListeners[viewID].on('change', function (ch) {
if (ch.id !== viewID) {
return;
}
view.db.destroy();
sourceDB._viewListeners[viewID].cancel();
delete sourceDB._viewListeners[viewID];
delete sourceDB._cachedViews[viewSignature];
upsert(sourceDB, '_local/gcviews', cleanUpView);
});
}
});
}

return view;
});
});
});
});
};
};
66 changes: 36 additions & 30 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,14 @@ function spatial(fun, bbox, opts, cb, /*only needed if people use 2 bboxen-->*/c
throw err;
}
});

function updateIndex(func) {
return function (viewDB) {
//console.log(viewDB);
viewDB = viewDB.db;
if (viewDB._rStore) {
if (temporary) {
store = new RTree();
} else if (viewDB._rStore) {
store = viewDB._rStore;
} else {
store = viewDB._rStore = new RTree(new Store(viewDB));
Expand All @@ -76,31 +79,31 @@ function spatial(fun, bbox, opts, cb, /*only needed if people use 2 bboxen-->*/c
function fixMulti (doc) {
var type = doc.type;
switch (type) {
case 'MultiPoint':
return doc.coordinates.forEach(function (coord) {
emit({
type: 'Point',
coordinates: coord
});
});
case 'MultiLineString':
return doc.coordinates.forEach(function (coord) {
emit({
type: 'LineString',
coordinates: coord
});
});
case 'MultiPolygon':
return doc.coordinates.forEach(function (coord) {
emit({
type: 'Polygon',
coordinates: coord
});
});
case 'GeometryCollection':
return doc.geometries.forEach(fixMulti);
default:
return emit(doc);
case 'MultiPoint':
return doc.coordinates.forEach(function (coord) {
emit({
type: 'Point',
coordinates: coord
});
});
case 'MultiLineString':
return doc.coordinates.forEach(function (coord) {
emit({
type: 'LineString',
coordinates: coord
});
});
case 'MultiPolygon':
return doc.coordinates.forEach(function (coord) {
emit({
type: 'Polygon',
coordinates: coord
});
});
case 'GeometryCollection':
return doc.geometries.forEach(fixMulti);
default:
return emit(doc);
}
}
func(doc, fixMulti);
Expand Down Expand Up @@ -134,6 +137,9 @@ function spatial(fun, bbox, opts, cb, /*only needed if people use 2 bboxen-->*/c
return addDoc(doc.doc);
})).then(function () {
lastSeq.last_seq = res.last_seq;
if (temporary) {
return;
}
return upsert(viewDB, '_local/gclastSeq', function (doc) {
if (!doc.last_seq) {
return lastSeq;
Expand Down Expand Up @@ -177,12 +183,12 @@ function spatial(fun, bbox, opts, cb, /*only needed if people use 2 bboxen-->*/c
function makeFunc (db, fun) {
return new Promise (function (resolve, reject) {
if (typeof fun === 'function') {
return resolve(new Function ('doc', 'emit', 'var func = (' + fun.toString().replace(/;\s*$/,"") + ');func(doc);'));
return resolve(new Function ('doc', 'emit', 'var func = (' + fun.toString().replace(/;\s*$/,'') + ');func(doc);'));
}
var parts = fun.split('/');
resolve(db.get('_design/' + parts[0]).then(function (doc) {
var fun = doc.spatial[parts[1]];
return new Function ('doc', 'emit', 'var func = (' + fun.replace(/;\s*$/,"") + ');func(doc);');
return new Function ('doc', 'emit', 'var func = (' + fun.replace(/;\s*$/,'') + ');func(doc);');
}));
});
}
}
4 changes: 2 additions & 2 deletions store.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ Store.prototype.batch = function(array, cb) {
return self.del(item.key, callback);
}
return self.put(item.key, item.value, callback);
});
});
})).then(function () {
cb();
}, cb);
};
};
92 changes: 68 additions & 24 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,6 @@ function testit(name, opts) {
return db.spatial('foo/bar',[ -70.98495,42.24867, -70.98495,42.24867], {stale: true}).then(function (resp) {
resp.length.should.equal(0);
done();
}).catch(function (e) {
console.log(e);
done(e);
});
}).catch(done);
});
Expand Down Expand Up @@ -209,7 +206,7 @@ function testit(name, opts) {
}).catch(done);
});
it ('should allow updating the query designDoc', function (done) {
this.timeout(50000);
this.timeout(50000);
db.put({
_id: '_design/foo',
spatial: {
Expand Down Expand Up @@ -255,25 +252,72 @@ function testit(name, opts) {
}).catch(done);
});

it ('should work fast', function (done) {
db2.spatial('foo/bar',[[ -71.70639038085936,42.353469793490646], [-71.56219482421875, 42.461966608980134]]).then(function (resp) {
resp.length.should.equal(9);
var nr = resp.map(function(i) {
return i.id;
});
nr.sort();
nr.should.deep.equal([
'BERLIN',
'BOLTON',
'BOYLSTON',
'CLINTON',
'HARVARD',
'HUDSON',
'LANCASTER',
'MARLBOROUGH',
'NORTHBOROUGH' ], 'names');
done();
}).catch(done);
it ('should work fast', function (done) {
db2.spatial('foo/bar',[[ -71.70639038085936,42.353469793490646], [-71.56219482421875, 42.461966608980134]]).then(function (resp) {
resp.length.should.equal(9);
var nr = resp.map(function(i) {
return i.id;
});
nr.sort();
nr.should.deep.equal([
'BERLIN',
'BOLTON',
'BOYLSTON',
'CLINTON',
'HARVARD',
'HUDSON',
'LANCASTER',
'MARLBOROUGH',
'NORTHBOROUGH' ], 'names');
done();
}).catch(done);
});

it('should be able to close and then open', function (done) {
var db3;
db.bulkDocs([{
_id : 'eb46c0cc24eabb6427af7eac2b0012ac',
geometry : {
type : 'Point',
coordinates : [13.3971420055456, 52.5296601136334]
}
}, {
_id : 'eb46c0cc24eabb6427af7eac2b001c9f',
geometry : {
type : 'Point',
coordinates : [13.6384082053328, 52.418740058446]
}
}])
.then(()=>{
return db.spatial(function(doc){
emit(doc.geometry);
}, [[13.3971420055456,52.418740058446],[13.6384082053328,52.5296601136334]])
})
.then(resp=>{
resp.length.should.equal(2);
return db.close();
})
.then(()=>{
db3 = new Pouch('testy', opts);
return db3.allDocs();
}).then(resp=>{
resp.rows.length.should.equal(2);
}).then(()=> {
return db3.spatial(function(doc){
emit(doc.geometry);
}, [[13.3971420055456,52.418740058446],[13.6384082053328,52.5296601136334]])
}).then(function (resp) {
resp.length.should.equal(2);
var nr = resp.map(function(i) {
return i.id;
});
db = db3;
done();
})
.catch((e)=>{
db = db3;
done(e);
});
});
}
});
}

0 comments on commit 5d54960

Please sign in to comment.