@@ -2,11 +2,13 @@ var Datasource = require('./Datasource'),
2
2
N3 = require ( 'n3' ) ,
3
3
path = require ( 'path' ) ,
4
4
fs = require ( 'fs' ) ,
5
- Bloem = require ( 'bloem' ) . Bloem ;
5
+ Bloem = require ( 'bloem' ) . Bloem ,
6
+ chokidar = require ( 'chokidar' ) ;
6
7
7
8
var ACCEPT = 'text/turtle;q=1.0,application/n-triples;q=0.7,text/n3;q=0.6' ;
8
9
9
- var DCT = 'http://purl.org/dc/terms/' ,
10
+ var RDF = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#' ,
11
+ DCT = 'http://purl.org/dc/terms/' ,
10
12
AMF = 'http://semweb.mmlab.be/ns/membership#' ,
11
13
DS = 'http://semweb.mmlab.be/ns/summaries#' ;
12
14
@@ -17,66 +19,103 @@ function SummaryDatasource(options) {
17
19
Datasource . call ( this , options ) ;
18
20
19
21
// Settings for data summaries
20
- this . _summariesFolder = options . dir || '../../summaries' ;
22
+ this . _summariesFolder = ( options . dir && path . isAbsolute ( options . dir ) ) ? options . dir : path . join ( __dirname , options . dir || '../../summaries' ) ;
21
23
}
22
24
Datasource . extend ( SummaryDatasource , [ 'triplePattern' , 'limit' , 'offset' ] ) ;
23
25
24
26
// Prepares the datasource for querying
25
27
SummaryDatasource . prototype . _initialize = function ( done ) {
26
- var tripleStore = this . _tripleStore = new N3 . Store ( ) ;
28
+ this . _tripleStore = new N3 . Store ( ) ;
29
+ // var parser = new N3.Parser();
30
+ // var self = this;
31
+
32
+ // If summaryDir does not exist, create it
33
+ if ( ! fs . existsSync ( this . _summariesFolder ) ) {
34
+ fs . mkdirSync ( this . _summariesFolder , { recursive : true } ) ;
35
+ }
36
+
37
+ // Initialize watcher.
38
+ const watcher = chokidar . watch ( this . _summariesFolder , {
39
+ ignored : / ( ^ | [ \/ \\ ] ) \. ./ ,
40
+ persistent : true
41
+ } ) ;
42
+
43
+ console . log ( `Watching ${ this . _summariesFolder } ` )
44
+ watcher . on ( 'add' , ( summaryFile ) => this . _storeFile ( summaryFile , err => console . log ( err ) ) ) ;
45
+ done ( ) ;
46
+ } ;
47
+
48
+ SummaryDatasource . prototype . _storeFile = function ( summaryFile , callback ) {
49
+ console . log ( `Adding ${ summaryFile } to store` )
27
50
var parser = new N3 . Parser ( ) ;
51
+
52
+ var document = this . _fetch ( { url : summaryFile , headers : { accept : ACCEPT } } , callback ) ;
53
+ var tripleStore = this . _tripleStore ;
28
54
var self = this ;
55
+ // N3Parser._resetBlankNodeIds();
29
56
30
- fs . readdir ( this . _summariesFolder , function ( err , items ) {
31
- if ( err ) {
32
- done ( err ) ;
57
+ var graph = decodeURIComponent ( summaryFile ) ;
58
+ parser . parse ( document , function ( error , triple ) {
59
+ if ( error ) {
60
+ callback && callback ( error ) ;
33
61
return ;
34
62
}
35
-
36
- // Store every summary in the store
37
- items . forEach ( function ( summaryFile ) {
38
- var document = self . _fetch ( { url : path . join ( self . _summariesFolder , summaryFile ) , headers : { accept : ACCEPT } } , done ) ;
39
- // N3Parser._resetBlankNodeIds();
40
- parser . parse ( document , function ( error , triple ) {
41
- if ( error ) {
42
- done ( error ) ;
43
- return ;
44
- }
45
-
46
- if ( triple )
47
- tripleStore . addTriple ( triple . subject , triple . predicate , triple . object , summaryFile ) ;
48
- else {
49
- // Create AMFs
50
- self . _storeFilters ( ) ;
51
- done ( ) ;
52
- }
53
- } ) ;
54
- } ) ;
63
+
64
+ if ( triple )
65
+ tripleStore . addTriple ( triple . subject , triple . predicate , triple . object , graph ) ;
66
+ else {
67
+ tripleStore . addTriple ( graph , RDF + 'type' , DS + 'Summary' , graph ) ;
68
+ // Create AMFs
69
+ console . log ( `Storing filters of ${ graph } ` )
70
+ self . _storeFilter ( graph ) ;
71
+ callback && callback ( null ) ;
72
+ }
55
73
} ) ;
56
- } ;
74
+ }
57
75
58
- SummaryDatasource . prototype . _storeFilters = function ( ) {
76
+ SummaryDatasource . prototype . _storeFilter = function ( graph ) {
59
77
var filters = this . _filters = { } ;
60
78
var tripleStore = this . _tripleStore ;
61
79
62
- var filterQuads = tripleStore . find ( null , DS + 'objFilter' , null ) ;
63
-
80
+ var filterQuads = tripleStore . find ( null , DS + 'objFilter' , null , graph ) ;
81
+ console . log ( `Found ${ filterQuads . length } filters for ${ graph } ` )
64
82
filterQuads . forEach ( function ( quad ) {
65
- var bitsString = tripleStore . find ( quad . object , AMF + 'bits' , null ) [ 0 ] . object ;
66
- var bits = parseInt ( N3 . Util . getLiteralValue ( bitsString ) , 10 ) ;
83
+ try {
84
+ var bitsMatches = tripleStore . find ( quad . object , AMF + 'bits' , null , graph ) ;
85
+
86
+ if ( bitsMatches . length === 0 ) return ;
87
+
88
+ var bitsString = bitsMatches [ 0 ] . object ;
89
+ var bits = parseInt ( N3 . Util . getLiteralValue ( bitsString ) , 10 ) ;
90
+
91
+ var hashesMatches = tripleStore . find ( quad . object , AMF + 'hashes' , null , graph ) ;
92
+
93
+ if ( hashesMatches . length === 0 ) return ;
67
94
68
- var hashesString = tripleStore . find ( quad . object , AMF + 'hashes' , null ) [ 0 ] . object ;
69
- var hashes = parseInt ( N3 . Util . getLiteralValue ( hashesString ) , 10 ) ;
95
+ var hashesString = hashesMatches [ 0 ] . object ;
96
+ var hashes = parseInt ( N3 . Util . getLiteralValue ( hashesString ) , 10 ) ;
70
97
71
- // Decode filter
72
- var filterString = tripleStore . find ( quad . object , AMF + 'filter' , null ) [ 0 ] . object ;
73
- var filter = Buffer . from ( N3 . Util . getLiteralValue ( filterString ) , 'base64' ) ;
98
+ // Decode filter
99
+ var filterMatches = tripleStore . find ( quad . object , AMF + 'filter' , null , graph ) ;
74
100
75
- // Find predicate
76
- var predicate = tripleStore . find ( quad . subject , DS + 'predicate' , null , quad . graph ) [ 0 ] . object ;
101
+ if ( filterMatches . length === 0 ) return ;
77
102
78
- filters [ predicate ] = filters [ predicate ] || { } ; // create entry for predicate if not exists
79
- filters [ predicate ] [ quad . graph ] = new Bloem ( bits , hashes , filter ) ; // add filter for the summary
103
+ var filterString = filterMatches [ 0 ] . object ;
104
+ var filter = Buffer . from ( N3 . Util . getLiteralValue ( filterString ) , 'base64' ) ;
105
+
106
+ // Find predicate
107
+ var predicateMatches = tripleStore . find ( quad . subject , DS + 'predicate' , null , quad . graph ) ;
108
+
109
+ if ( predicateMatches . length === 0 ) return ;
110
+
111
+ var predicate = predicateMatches [ 0 ] . object ;
112
+
113
+ filters [ predicate ] = filters [ predicate ] || { } ; // create entry for predicate if not exists
114
+ filters [ predicate ] [ quad . graph ] = new Bloem ( bits , hashes , filter ) ; // add filter for the summary
115
+ } catch ( er ) {
116
+ console . log ( er )
117
+ console . log ( filterMatches )
118
+ }
80
119
} ) ;
81
120
} ;
82
121
0 commit comments