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