@@ -13,7 +13,7 @@ var _ = require('lodash');
13
13
* GSP (s, ?, ?, g), (s, p, ?, g)
14
14
* OS (s, ?, o, ?)
15
15
*
16
- * @param configuration['treeOrder '] Tree order for the indices that are going to be created
16
+ * @param configuration['dbName '] Name for the IndexedDB
17
17
* @return The newly created backend.
18
18
*/
19
19
QuadBackend = function ( configuration , callback ) {
@@ -22,23 +22,27 @@ QuadBackend = function (configuration, callback) {
22
22
if ( arguments !== 0 ) {
23
23
24
24
if ( typeof ( window ) === 'undefined' ) {
25
+ var sqlite3 = require ( 'sqlite3' )
25
26
var indexeddbjs = require ( "indexeddb-js" ) ;
26
- var indexEngine = new sqlite3 . Database ( ':memory:' ) ;
27
- that . indexedDB = new indexeddbjs . indexedDB ( 'sqlite3' , indexEngine ) ;
27
+ var engine = new sqlite3 . Database ( ':memory:' ) ;
28
+ var scope = indexeddbjs . makeScope ( 'sqlite3' , engine ) ;
29
+ that . indexedDB = scope . indexedDB ;
30
+ that . IDBKeyRange = scope . IDBKeyRange ;
28
31
} else {
29
32
// In the following line, you should include the prefixes of implementations you want to test.
30
33
window . indexedDB = window . indexedDB || window . mozIndexedDB || window . webkitIndexedDB || window . msIndexedDB ;
34
+ window . IDBKeyRange = window . IDBKeyRange || window . webkitIDBKeyRange || window . msIDBKeyRange ;
31
35
// DON'T use "var indexedDB = ..." if you're not in a function.
32
36
// Moreover, you may need references to some window.IDB* objects:
33
37
if ( ! window . indexedDB ) {
34
38
callback ( null , new Error ( "The browser does not support IndexDB." ) ) ;
35
39
} else {
36
40
that . indexedDB = window . indexedDB ;
41
+ that . IDBKeyRange = window . IDBKeyRange ;
37
42
}
38
43
}
39
44
40
45
this . indexMap = { } ;
41
- this . treeOrder = configuration [ 'treeOrder' ] ;
42
46
this . indices = [ 'SPOG' , 'GP' , 'OGS' , 'POG' , 'GSP' , 'OS' ] ;
43
47
this . componentOrders = {
44
48
SPOG :[ 'subject' , 'predicate' , 'object' , 'graph' ] ,
@@ -60,67 +64,146 @@ QuadBackend = function (configuration, callback) {
60
64
} ;
61
65
request . onupgradeneeded = function ( event ) {
62
66
var db = event . target . result ;
63
- db . createObjectStore ( index , { keyPath : 'SPOG' } ) ;
67
+ var objectStore = db . createObjectStore ( that . dbName , { keyPath : 'SPOG' } ) ;
64
68
_ . each ( that . indices , function ( index ) {
65
69
if ( index !== 'SPOG' ) {
66
- db . createIndex ( index , index , { unique : false } ) ;
70
+ objectStore . createIndex ( index , index , { unique : false } ) ;
67
71
}
68
72
} ) ;
69
73
} ;
70
74
}
71
75
} ;
72
76
73
- QuadBackend . prototype . _genIndexKey = function ( quad , index ) {
74
- return _ . map ( indexComponents , function ( component ) {
75
- return "" + ( quad [ component ] || - 1 ) ;
76
- } ) . join ( '.' ) ;
77
- } ;
78
77
79
78
QuadBackend . prototype . index = function ( quad , callback ) {
80
79
var that = this ;
81
80
_ . each ( this . indices , function ( index ) {
82
- quad [ index ] = that . _genIndexKey ( quad , that . componentOrders [ index ] ) ;
81
+ quad [ index ] = that . _genMinIndexKey ( quad , index ) ;
83
82
} ) ;
84
83
85
- var transaction = that . db . transaction ( [ that . dbName ] , "write " ) ;
84
+ var transaction = that . db . transaction ( [ that . dbName ] , "readwrite " ) ;
86
85
transaction . oncomplete = function ( event ) {
87
- callback ( true )
86
+ // callback(true)
88
87
} ;
89
88
transaction . onerror = function ( event ) {
90
89
callback ( null , new Error ( event . target . statusCode ) ) ;
91
90
} ;
92
91
var objectStore = transaction . objectStore ( that . dbName ) ;
93
- objectStore . add ( quad ) ;
92
+ var request = objectStore . add ( quad ) ;
93
+ request . onsuccess = function ( event ) {
94
+ // event.target.result == customerData[i].ssn;
95
+ callback ( true )
96
+ } ;
94
97
} ;
95
98
96
99
QuadBackend . prototype . range = function ( pattern , callback ) {
100
+ var that = this ;
101
+ var objectStore = that . db . transaction ( [ that . dbName ] ) . objectStore ( that . dbName ) ;
97
102
var indexKey = this . _indexForPattern ( pattern ) ;
98
- var index = this . indexMap [ indexKey ] ;
99
- index . range ( pattern , function ( quads ) {
100
- callback ( quads ) ;
101
- } ) ;
103
+ var minIndexKeyValue = this . _genMinIndexKey ( pattern , indexKey ) ;
104
+ var maxIndexKeyValue = this . _genMaxIndexKey ( pattern , indexKey ) ;
105
+ var keyRange = that . IDBKeyRange . bound ( minIndexKeyValue , maxIndexKeyValue , false , false ) ;
106
+ var quads = [ ] ;
107
+ var cursorSource ;
108
+
109
+ if ( indexKey === 'SPOG' ) {
110
+ cursorSource = objectStore ;
111
+ } else {
112
+ cursorSource = objectStore . index ( indexKey ) ;
113
+ }
114
+
115
+ cursorSource . openCursor ( keyRange ) . onsuccess = function ( event ) {
116
+ var cursor = event . target . result ;
117
+ if ( cursor ) {
118
+ quads . push ( cursor . value ) ;
119
+ cursor . continue ( ) ;
120
+ } else {
121
+ callback ( quads ) ;
122
+ }
123
+ }
102
124
} ;
103
125
104
126
QuadBackend . prototype . search = function ( quad , callback ) {
105
- var index = this . indexMap [ 'SPOG' ] ;
106
-
107
- index . search ( quad , function ( result ) {
108
- callback ( result != null ) ;
109
- } ) ;
127
+ var objectStore = that . db . transaction ( [ this . dbName ] ) . objectStore ( that . dbName ) ;
128
+ var indexKey = that . _genMinIndexKey ( quad , 'SPOG' ) ;
129
+ var request = objectStore . get ( indexKey ) ;
130
+ request . onerror = function ( event ) {
131
+ callback ( null , new Error ( event . target . statusCode ) ) ;
132
+ } ;
133
+ request . onsuccess = function ( event ) {
134
+ callback ( event . target . result != null ) ;
135
+ } ;
110
136
} ;
111
137
112
138
113
139
QuadBackend . prototype . delete = function ( quad , callback ) {
114
140
var that = this ;
141
+ var indexKey = that . _genMinIndexKey ( quad , 'SPOG' ) ;
142
+ var request = that . db . transaction ( [ that . dbName ] , "readwrite" )
143
+ . objectStore ( that . dbName )
144
+ . delete ( indexKey ) ;
145
+ request . onsuccess = function ( ) {
146
+ callback ( true ) ;
147
+ } ;
148
+ request . onerror = function ( event ) {
149
+ callback ( null , new Error ( event . target . statusCode ) ) ;
150
+ } ;
151
+ } ;
115
152
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
- } ) ;
153
+ QuadBackend . prototype . _genMinIndexKey = function ( quad , index ) {
154
+ var indexComponents = this . componentOrders [ index ] ;
155
+ return _ . map ( indexComponents , function ( component ) {
156
+ if ( typeof ( quad [ component ] ) === 'string' || quad [ component ] == null ) {
157
+ return "-1" ;
158
+ } else {
159
+ return "" + quad [ component ] ;
160
+ }
161
+ } ) . join ( '.' ) ;
162
+ } ;
163
+
164
+ QuadBackend . prototype . _genMaxIndexKey = function ( quad , index ) {
165
+ var indexComponents = this . componentOrders [ index ] ;
166
+ var acum = [ ] ;
167
+ var foundFirstMissing = false ;
168
+ for ( var i = 0 ; i < indexComponents . length ; i ++ ) {
169
+ var component = indexComponents [ i ] ;
170
+ var componentValue = quad [ component ] ;
171
+ if ( typeof ( componentValue ) === 'string' ) {
172
+ if ( foundFirstMissing === false ) {
173
+ foundFirstMissing = true ;
174
+ if ( i - 1 >= 0 ) {
175
+ acum [ i - 1 ] = acum [ i - 1 ] + 1
176
+ }
177
+ }
178
+ acum [ i ] = - 1 ;
179
+ } else {
180
+ acum [ i ] = componentValue ;
181
+ }
182
+ }
183
+ return _ . map ( acum , function ( componentValue ) {
184
+ return "" + componentValue
185
+ } ) . join ( '.' ) ;
124
186
} ;
125
187
188
+
189
+ QuadBackend . prototype . _indexForPattern = function ( pattern ) {
190
+ var indexKey = pattern . indexKey ;
191
+
192
+ for ( var i = 0 ; i < this . indices . length ; i ++ ) {
193
+ var index = this . indices [ i ] ;
194
+ var indexComponents = this . componentOrders [ index ] ;
195
+ for ( var j = 0 ; j < indexComponents . length ; j ++ ) {
196
+ if ( _ . include ( indexKey , indexComponents [ j ] ) === false ) {
197
+ break ;
198
+ }
199
+ if ( j == indexKey . length - 1 ) {
200
+ return index ;
201
+ }
202
+ }
203
+ }
204
+
205
+ return 'SPOG' ; // If no other match, we return the more generic index
206
+ } ;
207
+
208
+
126
209
module . exports . QuadBackend = QuadBackend ;
0 commit comments