Repository navigation
Expand file tree
/
Copy pathMultiFileStore.js
More file actions
103 lines (78 loc) · 2.58 KB
/
Copy pathMultiFileStore.js
File metadata and controls
103 lines (78 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { promisify } from 'node:util'
import multistream from 'multistream'
import rdf from 'rdf-ext'
import FilterStream from 'rdf-stream-filter'
import byGraph from 'rdf-stream-to-dataset-stream/byGraph.js'
import { finished, Writable } from 'readable-stream'
import MultiFileDatasetStore from './lib/MultiFileDatasetStore.js'
import promiseToEvent from './lib/promiseToEvent.js'
class MultiFileStore {
constructor ({ factory = rdf, resolver }) {
this.datastore = new MultiFileDatasetStore({
factory,
resolver
})
}
match (subject, predicate, object, graph) {
let graphs = graph ? [graph] : null
return multistream.obj(async callback => {
if (!graphs) {
graphs = [...await this.datastore.graphs()]
}
const current = graphs.shift()
if (!current) {
return callback(null, null)
}
const dataset = await this.datastore.read(current)
callback(null, new FilterStream(dataset.toStream(), subject, predicate, object, current))
})
}
import (stream, { truncate = false } = {}) {
const writer = new Writable({
objectMode: true,
write: async (dataset, encoding, callback) => {
const graph = [...dataset][0].graph
if (!truncate) {
dataset.addAll(await this.datastore.read(graph))
}
await this.datastore.write(graph, dataset)
callback()
}
})
stream.pipe(byGraph()).pipe(writer)
return promiseToEvent(promisify(finished)(writer))
}
remove (stream) {
const writer = new Writable({
objectMode: true,
write: async (dataset, encoding, callback) => {
const graph = [...dataset][0].graph
const content = await this.datastore.read(graph)
for (const quad of dataset) {
if (content.has(quad)) {
content.delete(quad)
}
}
await this.datastore.write(graph, content)
callback()
}
})
stream.pipe(byGraph()).pipe(writer)
return promiseToEvent(promisify(finished)(writer))
}
removeMatches (subject, predicate, object, graph) {
return promiseToEvent((async () => {
const graphs = graph ? [graph] : [...await this.datastore.graphs()]
const current = graphs.shift()
const content = await this.datastore.read(current)
for (const quad of content.match(subject, predicate, object, current)) {
content.delete(quad)
}
await this.datastore.write(current, content)
})())
}
deleteGraph (graph) {
return promiseToEvent(this.datastore.write(graph, rdf.dataset()))
}
}
export default MultiFileStore