@@ -37,8 +37,9 @@ function Graph (storage, key, opts) {
3737 if ( ! opts ) opts = { }
3838 this . db = hyperdb ( storage , key , opts )
3939 this . _prefixes = Object . assign ( { } , opts . prefixes || constants . DEFAULT_PREFIXES )
40- this . _basename = opts . base || constants . DEFAULT_BASE
40+ this . _basename = opts . name || constants . DEFAULT_BASE
4141 this . _prefixes . _ = this . _basename
42+ this . _indexType = opts . index === 'tri' ? 'tri' : 'hex'
4243 this . _indexes = opts . index === 'tri'
4344 ? constants . HEXSTORE_INDEXES_REDUCED
4445 : constants . HEXSTORE_INDEXES
@@ -54,7 +55,10 @@ function Graph (storage, key, opts) {
5455 this . emit ( 'ready' , e )
5556 } )
5657 } else {
57- this . emit ( 'ready' , e )
58+ this . _onInit ( ( err ) => {
59+ if ( err ) return this . emit ( 'error' , err )
60+ this . emit ( 'ready' , e )
61+ } )
5862 }
5963 } )
6064}
@@ -65,7 +69,7 @@ Graph.prototype._onNew = function (cb) {
6569 this . _version = pkg . version
6670 const metadata = [
6771 [ '@version' , pkg . version ] ,
68- [ '@index' , Object . keys ( this . _indexes ) . length === 3 ? 'tri' : 'hex' ] ,
72+ [ '@index' , this . _indexType ] ,
6973 [ '@name' , this . _basename ]
7074 ]
7175 Object . keys ( this . _prefixes ) . forEach ( ( key ) => {
@@ -76,9 +80,78 @@ Graph.prototype._onNew = function (cb) {
7680 utils . put ( this . db , metadata , cb )
7781}
7882
83+ Graph . prototype . _onInit = function ( cb ) {
84+ // get and set graph version
85+ this . _version = null
86+ this . _basename = null
87+ this . _indexType = null
88+
89+ let missing = 4
90+ let error = null
91+ // get and set version
92+ this . graphVersion ( ( err , version ) => {
93+ if ( err ) error = err
94+ this . _version = version
95+ maybeDone ( )
96+ } )
97+ // get and set graph name
98+ this . name ( ( err , name ) => {
99+ if ( err ) error = err
100+ this . _basename = name || constants . DEFAULT_BASE
101+ // modify prefixes to ensure correct namespacing
102+ this . _prefixes . _ = this . _basename
103+ maybeDone ( )
104+ } )
105+ // get and set graph indexation
106+ this . indexType ( ( err , index ) => {
107+ if ( err ) error = err
108+ this . _indexType = index || 'hex'
109+ this . _indexes = index === 'tri'
110+ ? constants . HEXSTORE_INDEXES_REDUCED
111+ : constants . HEXSTORE_INDEXES
112+ this . _indexKeys = Object . keys ( this . _indexes )
113+ maybeDone ( )
114+ } )
115+ // get and set prefixes
116+ this . prefixes ( ( err , prefixes ) => {
117+ if ( err ) error = err
118+ this . _prefixes = Object . assign ( { _ : this . _prefixes } , prefixes )
119+ maybeDone ( )
120+ } )
121+ function maybeDone ( ) {
122+ missing --
123+ if ( ! missing ) {
124+ cb ( error )
125+ }
126+ }
127+ }
128+
79129Graph . prototype . v = ( name ) => new Variable ( name )
80130
81- Graph . prototype . listPrefixes = function ( callback ) {
131+ function returnValueAsString ( cb ) {
132+ return ( err , nodes ) => {
133+ if ( err ) return cb ( err )
134+ if ( ! nodes ) return cb ( null , null )
135+ cb ( null , nodes [ 0 ] . value . toString ( ) )
136+ }
137+ }
138+
139+ Graph . prototype . graphVersion = function ( cb ) {
140+ if ( this . _version ) return cb ( null , this . _version )
141+ this . db . get ( '@version' , returnValueAsString ( cb ) )
142+ }
143+
144+ Graph . prototype . name = function ( cb ) {
145+ if ( this . _basename ) return cb ( null , this . _basename )
146+ this . db . get ( '@name' , returnValueAsString ( cb ) )
147+ }
148+
149+ Graph . prototype . indexType = function ( cb ) {
150+ if ( this . _indexType ) return cb ( null , this . _indexType )
151+ this . db . get ( '@index' , returnValueAsString ( cb ) )
152+ }
153+
154+ Graph . prototype . prefixes = function ( callback ) {
82155 // should cache this somehow
83156 const prefixStream = this . db . createReadStream ( constants . PREFIX_KEY )
84157 utils . collect ( prefixStream , ( err , data ) => {
0 commit comments