-
Notifications
You must be signed in to change notification settings - Fork 20
/
map-index.js
61 lines (58 loc) · 1.78 KB
/
map-index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* This is a wrapper store that adds indexing through map functions
*/
var when = require("promised-io/promise").when;
module.exports = function(store, options){
options = options || {};
var IndexConstructor = options.IndexConstructor || require("../stores").DefaultStore;
store.deriveView = function deriveView(map){
var index = new IndexConstructor();
store.setPath(map.toString());
var getRevision = index.getRevision || function(){
return index.get("__revision__");
};
var getRevision = index.setRevision || function(revision){
return index.put("__revision__", revision);
};
var getRevisions = derivedFrom.getRevisions || store.getRevisions || function(from){
return store.query("revisions(" + from + ")");
};
var revision;
return {
cursor: function(){
var storeRevision = store.getRevision();
// TODO: Might use a vclock type of strategy to determine if we really need to update
if(storeRevision > getRevision()){
var transaction = index.transaction(true);
// make sure we still need to update after getting the lock
if(storeRevision > getRevision()){
getRevisions(getRevisition()).forEach(function(object){
var old = store.getPrevisionVersion(object);
if(old){
map(old, function(key, value){
index.remove(key + '/' + old.id);
});
}
if(object){
if(maybeAlreadyApplied){
map(object, function(key, value){
index.remove(key + '/' + object.id);
});
}
map(object, function(key, value){
index.put(key + '/' + object.id, value);
});
}
});
}
setRevision(storeRevision);
transaction.commit();
}
return index.cursor();
},
deriveView: function(){
}
}
};
return store;
};