@@ -80,7 +80,19 @@ class Graph {
8080 }
8181 insert ( records ) {
8282 for ( const record of records ) {
83- this . add ( record ) ;
83+ const vertex = this . add ( record ) ;
84+ const keys = Object . keys ( record ) ;
85+ for ( const [ key , idx ] of this . indices ) {
86+ if ( keys . includes ( key ) ) {
87+ const value = record [ key ] ;
88+ let records = idx . get ( value ) ;
89+ if ( ! records ) {
90+ records = new Set ( ) ;
91+ idx . set ( value , records ) ;
92+ }
93+ records . add ( vertex ) ;
94+ }
95+ }
8496 }
8597 }
8698 index ( key ) {
@@ -104,7 +116,7 @@ class Graph {
104116
105117// Usage
106118
107- const graph = new Graph ( 'name' ) ;
119+ const graph = new Graph ( 'name' ) . index ( 'city' ) ;
108120
109121graph . insert ( [
110122 { name : 'Marcus Aurelius' , city : 'Rome' , born : 121 , dynasty : 'Antonine' } ,
@@ -114,6 +126,8 @@ graph.insert([
114126 { name : 'Trajan' , city : 'Sevilla' , born : 98 , dynasty : 'Nerva–Trajan' }
115127] ) ;
116128
129+ graph . index ( 'dynasty' ) ;
130+
117131graph . link ( 'Marcus Aurelius' ) . to ( 'Lucius Verus' ) ;
118132graph . link ( 'Lucius Verus' ) . to ( 'Trajan' , 'Marcus Aurelius' , 'Marcus Aurelius' ) ;
119133graph . link ( 'Antoninus Pius' ) . to ( 'Marcus Aurelius' , 'Lucius Verus' ) ;
@@ -123,12 +137,9 @@ graph.link('Trajan').to('Lucius Verus', 'Marcus Aurelius');
123137console . dir ( { graph } , { depth : null } ) ;
124138
125139const res = graph
126- . index ( 'city' ) . index ( 'dynasty' )
127140 . select ( { city : 'Rome' , dynasty : 'Antonine' } )
128141 . linked ( 'Trajan' ) ;
129142
130- console . dir ( { graph } , { depth : null } ) ;
131-
132143console . log ( '\nQuery result:\n' ) ;
133144for ( const item of res . vertices ) {
134145 console . dir ( item . data ) ;
0 commit comments