@@ -5,6 +5,7 @@ const inherits = require('inherits')
55const events = require ( 'events' )
66const SparqlIterator = require ( 'sparql-iterator' )
77
8+ const constants = require ( './lib/constants' )
89const utils = require ( './lib/utils' )
910const prefixes = require ( './lib/prefixes' )
1011const Variable = require ( './lib/Variable' )
@@ -26,21 +27,29 @@ function Graph (storage, key, opts) {
2627 events . EventEmitter . call ( this )
2728 this . db = hyperdb ( storage , key , opts )
2829
30+ // what are the default prefixes
31+ this . _prefixes = constants . DEFAULT_PREFIXES
32+ this . _indexes = ( opts && opts . index === 'small' )
33+ ? constants . HEXSTORE_INDEXES_REDUCED
34+ : constants . HEXSTORE_INDEXES
35+ this . _indexKeys = Object . keys ( this . _indexes )
36+
2937 this . db . on ( 'error' , ( e ) => {
3038 this . emit ( 'error' , e )
3139 } )
3240 this . db . on ( 'ready' , ( e ) => {
3341 this . emit ( 'ready' , e )
42+ // check prefixes
3443 } )
3544}
3645
3746inherits ( Graph , events . EventEmitter )
3847
3948Graph . prototype . v = ( name ) => new Variable ( name )
4049
41- Graph . prototype . prefixes = function ( callback ) {
50+ Graph . prototype . listPrefixes = function ( callback ) {
4251 // should cache this somehow
43- const prefixStream = this . db . createReadStream ( prefixes . PREFIX_KEY )
52+ const prefixStream = this . db . createReadStream ( constants . PREFIX_KEY )
4453 utils . collect ( prefixStream , ( err , data ) => {
4554 if ( err ) return callback ( err )
4655 var names = data . reduce ( ( p , nodes ) => {
@@ -57,8 +66,8 @@ Graph.prototype.addPrefix = function (prefix, uri, cb) {
5766}
5867
5968Graph . prototype . getStream = function ( triple , opts ) {
60- const stream = this . db . createReadStream ( utils . createQuery ( triple ) )
61- return stream . pipe ( new HyperdbReadTransform ( this . db , opts ) )
69+ const stream = this . db . createReadStream ( this . _createQuery ( triple ) )
70+ return stream . pipe ( new HyperdbReadTransform ( this . db , this . _prefixes , opts ) )
6271}
6372
6473Graph . prototype . get = function ( triple , opts , callback ) {
@@ -71,21 +80,22 @@ function doAction (action) {
7180 if ( ! triples ) return callback ( new Error ( 'Must pass triple' ) )
7281 let entries = ( ! triples . reduce ) ? [ triples ] : triples
7382 entries = entries . reduce ( ( prev , triple ) => {
74- return prev . concat ( this . generateBatch ( triple , action ) )
83+ return prev . concat ( this . _generateBatch ( triple , action ) )
7584 } , [ ] )
7685 this . db . batch ( entries . reverse ( ) , callback )
7786 }
7887}
7988
8089function doActionStream ( action ) {
8190 return function ( ) {
91+ const self = this
8292 const transform = new Transform ( {
8393 objectMode : true ,
8494 transform ( triples , encoding , done ) {
8595 if ( ! triples ) return done ( )
8696 let entries = ( ! triples . reduce ) ? [ triples ] : triples
8797 entries = entries . reduce ( ( prev , triple ) => {
88- return prev . concat ( utils . generateBatch ( triple , action ) )
98+ return prev . concat ( self . _generateBatch ( triple , action ) )
8999 } , [ ] )
90100 this . push ( entries . reverse ( ) )
91101 done ( )
@@ -150,10 +160,64 @@ Graph.prototype.query = function (query, callback) {
150160 utils . collect ( this . queryStream ( query ) , callback )
151161}
152162
153- Graph . prototype . generateBatch = utils . generateBatch
154-
155163Graph . prototype . close = function ( callback ) {
156164 callback ( )
157165}
158166
167+ /* PRIVATE FUNCTIONS */
168+
169+ Graph . prototype . _generateBatch = function ( triple , action ) {
170+ if ( ! action ) action = 'put'
171+ var data = null
172+ if ( action === 'put' ) {
173+ data = JSON . stringify ( utils . extraDataMask ( triple ) )
174+ }
175+ return this . _encodeKeys ( triple ) . map ( key => ( {
176+ type : 'put' , // no delete in hyperdb so just putting nulls
177+ key : key ,
178+ value : data
179+ } ) )
180+ }
181+
182+ Graph . prototype . _encodeKeys = function ( triple ) {
183+ const encodedTriple = utils . encodeTriple ( triple , this . _prefixes )
184+ return this . _indexKeys . map ( key => utils . encodeKey ( key , encodedTriple ) )
185+ }
186+
187+ Graph . prototype . _createQuery = function ( pattern , options ) {
188+ var types = utils . typesFromPattern ( pattern )
189+ var preferedIndex = options && options . index
190+ var index = this . _findIndex ( types , preferedIndex )
191+ const encodedTriple = utils . encodeTriple ( pattern , this . _prefixes )
192+ var key = utils . encodeKey ( index , encodedTriple )
193+ return key
194+ }
195+
196+ Graph . prototype . _possibleIndexes = function ( types ) {
197+ var result = this . _indexKeys . filter ( ( key ) => {
198+ var matches = 0
199+ return this . _indexes [ key ] . every ( function ( e , i ) {
200+ if ( types . indexOf ( e ) >= 0 ) {
201+ matches ++
202+ return true
203+ }
204+ if ( matches === types . length ) {
205+ return true
206+ }
207+ } )
208+ } )
209+
210+ result . sort ( )
211+
212+ return result
213+ }
214+
215+ Graph . prototype . _findIndex = function ( types , preferedIndex ) {
216+ var result = this . _possibleIndexes ( types )
217+ if ( preferedIndex && result . some ( r => r === preferedIndex ) ) {
218+ return preferedIndex
219+ }
220+ return result [ 0 ]
221+ }
222+
159223module . exports = Graph
0 commit comments