1- const PassThrough = require ( 'readable-stream' ) . PassThrough
2- const Transform = require ( 'readable-stream' ) . Transform
1+ const hyperdb = require ( 'hyperdb' )
2+ const stream = require ( 'readable-stream' )
33const pump = require ( 'pump' )
4+ const inherits = require ( 'inherits' )
5+ const events = require ( 'events' )
46
57const utils = require ( './lib/utils' )
68const Variable = require ( './lib/Variable' )
7- const HyperdbDiffTransform = require ( './lib/HyperdbDiffTransform ' )
9+ const HyperdbReadTransform = require ( './lib/HyperdbReadTransform ' )
810const JoinStream = require ( './lib/JoinStream' )
911const planner = require ( './lib/planner' )
12+ const attachCreateReadStream = require ( './lib/hyperdbModifier' ) . attachCreateReadStream
1013
11- function Graph ( db , opts ) {
12- if ( ! ( this instanceof Graph ) ) return new Graph ( db , opts )
13- this . db = db
14+ const Transform = stream . Transform
15+ const PassThrough = stream . PassThrough
16+
17+ // temporarily augment hyperdb prototype to include createReadStream
18+ if ( ! hyperdb . createReadStream ) {
19+ attachCreateReadStream ( hyperdb )
1420}
1521
22+ function Graph ( storage , key , opts ) {
23+ if ( ! ( this instanceof Graph ) ) return new Graph ( storage , key , opts )
24+ events . EventEmitter . call ( this )
25+ this . db = hyperdb ( storage , key , opts )
26+
27+ this . db . on ( 'error' , ( e ) => {
28+ this . emit ( 'error' , e )
29+ } )
30+ this . db . on ( 'ready' , ( e ) => {
31+ this . emit ( 'ready' , e )
32+ } )
33+ }
34+
35+ inherits ( Graph , events . EventEmitter )
36+
1637Graph . prototype . v = ( name ) => new Variable ( name )
1738
18- Graph . prototype . getStream = function ( triple ) {
19- const stream = this . db . createDiffStream ( utils . createQuery ( triple ) )
20- return stream . pipe ( new HyperdbDiffTransform ( this . db ) )
39+ Graph . prototype . getStream = function ( triple , opts ) {
40+ const stream = this . db . createReadStream ( utils . createQuery ( triple ) )
41+ return stream . pipe ( new HyperdbReadTransform ( this . db , opts ) )
2142}
2243
23- Graph . prototype . get = function ( triple , callback ) {
24- utils . collect ( this . getStream ( triple ) , callback )
44+ Graph . prototype . get = function ( triple , opts , callback ) {
45+ if ( typeof opts === 'function' ) return this . get ( triple , undefined , opts )
46+ utils . collect ( this . getStream ( triple , opts ) , callback )
2547}
2648
2749function doAction ( action ) {
@@ -55,12 +77,11 @@ function doActionStream (action) {
5577 }
5678}
5779
58- // this is not implemented in hyperdb yet
59- // for now we just put a null value in the db
60-
6180Graph . prototype . put = doAction ( 'put' )
6281Graph . prototype . putStream = doActionStream ( 'put' )
6382
83+ // this is not implemented in hyperdb yet
84+ // for now we just put a null value in the db
6485Graph . prototype . del = doAction ( 'del' )
6586Graph . prototype . delStream = doActionStream ( 'del' )
6687
@@ -75,8 +96,14 @@ Graph.prototype.searchStream = function (query, options) {
7596 }
7697 const plannedQuery = planner ( query )
7798
78- var streams = plannedQuery . map ( ( triple ) => {
79- return new JoinStream ( { triple, db : this } )
99+ var streams = plannedQuery . map ( ( triple , i ) => {
100+ const limit = ( options && i === plannedQuery . length - 1 ) ? options . limit : undefined
101+ return new JoinStream ( {
102+ triple : utils . filterTriple ( triple ) ,
103+ filter : triple . filter ,
104+ db : this ,
105+ limit
106+ } )
80107 } )
81108
82109 streams [ 0 ] . start = true
@@ -87,8 +114,12 @@ Graph.prototype.searchStream = function (query, options) {
87114 return result
88115}
89116
90- Graph . prototype . search = function ( query , callback ) {
91- utils . collect ( this . searchStream ( query ) , callback )
117+ Graph . prototype . search = function ( query , options , callback ) {
118+ if ( typeof options === 'function' ) {
119+ callback = options
120+ options = undefined
121+ }
122+ utils . collect ( this . searchStream ( query , options ) , callback )
92123}
93124
94125Graph . prototype . generateBatch = utils . generateBatch
0 commit comments