8000 Initial work in the IndexedDB backend · antoniogarrote/rdfstore-js@24eecb2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 24eecb2

Browse files
Initial work in the IndexedDB backend
1 parent 93cd33c commit 24eecb2

File tree

2 files changed

+127
-1
lines changed

2 files changed

+127
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@
3333
"gulp-browserify": "^0.5.1",
3434
"gulp-closure-compiler": "^0.2.17",
3535
"gulp-jasmine": "^1.0.1",
36-
"moment": "^2.8.4",
3736
"pegjs": "^0.8.0",
3837
"vinyl-source-stream": "^1.0.0",
3938
"closure-compiler": "^0.2.6",
4039
"debowerify": "^1.2.0"
4140
},
4241
"dependencies": {
4342
"async": "^0.9.0",
43+
"indexeddb-js": "0.0.14",
4444
"jsonld": "^0.3.22",
4545
"lodash": "^2.4.1",
4646
"moment": "^2.9.0",

src/persistent_quad_backend.js

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
2+
// imports
3+
var async = require('async');
4+
var _ = require('lodash');
5+
6+
/*
7+
* "perfect" indices for RDF indexing
8+
*
9+
* SPOG (?, ?, ?, ?), (s, ?, ?, ?), (s, p, ?, ?), (s, p, o, ?), (s, p, o, g)
10+
* GP (?, ?, ?, g), (?, p, ?, g)
11+
* OGS (?, ?, o, ?), (?, ?, o, g), (s, ?, o, g)
12+
* POG (?, p, ?, ?), (?, p, o, ?), (?, p, o, g)
13+
* GSP (s, ?, ?, g), (s, p, ?, g)
14+
* OS (s, ?, o, ?)
15+
*
16+
* @param configuration['treeOrder'] Tree order for the indices that are going to be created
17+
* @return The newly created backend.
18+
*/
19+
QuadBackend = function (configuration, callback) {
20+
var that = this;
21+
22+
if (arguments !== 0) {
23+
24+
if(typeof(window) === 'undefined') {
25+
var indexeddbjs = require("indexeddb-js");
26+
var indexEngine = new sqlite3.Database(':memory:');
27+
that.indexedDB = new indexeddbjs.indexedDB('sqlite3', indexEngine);
28+
} else {
29+
// In the following line, you should include the prefixes of implementations you want to test.
30+
window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
31+
// DON'T use "var indexedDB = ..." if you're not in a function.
32+
// Moreover, you may need references to some window.IDB* objects:
33+
if (!window.indexedDB) {
34+
callback(null,new Error("The browser does not support IndexDB."));
35+
} else {
36+
that.indexedDB = window.indexedDB;
37+
}
38+
}
39+
40+
this.indexMap = {};
41+
this.treeOrder = configuration['treeOrder'];
42+
this.indices = ['SPOG', 'GP', 'OGS', 'POG', 'GSP', 'OS'];
43+
this.componentOrders = {
44+
SPOG:['subject', 'predicate', 'object', 'graph'],
45+
GP:['graph', 'predicate', 'subject', 'object'],
46+
OGS:['object', 'graph', 'subject', 'predicate'],
47+
POG:['predicate', 'object', 'graph', 'subject'],
48+
GSP:['graph', 'subject', 'predicate', 'object'],
49+
OS:['object', 'subject', 'predicate', 'graph']
50+
};
51+
52+
that.dbName = configuration['dbName'] || "rdfstorejs";
53+
var request = that.indexedDB.open(this.dbName, 1);
54+
request.onerror = function(event) {
55+
callback(null,new Error("Error opening IndexedDB: " + event.target.errorCode));
56+
};
57+
request.onsuccess = function(event) {
58+
that.db = event.target.result;
59+
callback(that);
60+
};
61+
request.onupgradeneeded = function(event) {
62+
var db = event.target.result;
63+
db.createObjectStore(index, { keyPath: 'SPOG'});
64+
_.each(that.indices, function(index){
65+
if(index !== 'SPOG') {
66+
db.createIndex(index,index,{unique: false});
67+
}
68+
});
69+
};
70+
}
71+
};
72+
73+
QuadBackend.prototype._genIndexKey = function(quad,index) {
74+
return _.map(indexComponents, function(component){
75+
return ""+(quad[component] || -1);
76+
}).join('.');
77+
};
78+
79+
QuadBackend.prototype.index = function (quad, callback) {
80+
var that = this;
81+
_.each(this.indices, function(index){
82+
quad[index] = that._genIndexKey(quad, that.componentOrders[index]);
83+
});
84+
85+
var transaction = that.db.transaction([that.dbName],"write");
86+
transaction.oncomplete = function(event) {
87+
callback(true)
88+
};
89+
transaction.onerror = function(event) {
90+
callback(null, new Error(event.target.statusCode));
91+
};
92+
var objectStore = transaction.objectStore(that.dbName);
93+
objectStore.add(quad);
94+
};
95+
96+
QuadBackend.prototype.range = function (pattern, callback) {
97+
var indexKey = this._indexForPattern(pattern);
98+
var index = this.indexMap[indexKey];
99+
index.range(pattern, function (quads) {
100+
callback(quads);
101+
});
102+
};
103+
104+
QuadBackend.prototype.search = function (quad, callback) {
105+
var index = this.indexMap['SPOG'];
106+
107+
index.search(quad, function (result) {
108+
callback(result != null);
109+
});
110+
};
111+
112+
113+
QuadBackend.prototype.delete = function (quad, callback) {
114+
var that = this;
115+
116+
async.eachSeries(this.indices, function(indexKey,k){
117+
var index = that.indexMap[indexKey];
118+
index.delete(quad, function(){
119+
k();
120+
})
121+
},function(){
122+
callback(that);
123+
});
124+
};
125+
126+
module.exports.QuadBackend = QuadBackend;

0 commit comments

Comments
 (0)
0