-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #262 from zazuko/sparql-oxigraph
add in-memory SPARQL
- Loading branch information
Showing
8 changed files
with
221 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"barnard59-sparql": minor | ||
--- | ||
|
||
Add In-Memory SPARQL operations (closes #255) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Transform } from 'readable-stream' | ||
import oxigraph from 'oxigraph' | ||
|
||
export const update = sparql => | ||
new Transform({ | ||
objectMode: true, | ||
transform: (chunk, encoding, callback) => { | ||
const store = new oxigraph.Store([...chunk]) | ||
store.update(sparql) | ||
callback(null, store.match()) | ||
}, | ||
}) | ||
|
||
export const query = sparql => | ||
new Transform({ | ||
objectMode: true, | ||
transform: (chunk, encoding, callback) => { | ||
const store = new oxigraph.Store([...chunk]) | ||
const result = store.query(sparql) | ||
callback(null, result) | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { ok, strictEqual } from 'assert' | ||
import getStream from 'get-stream' | ||
import { isReadableStream, isWritableStream } from 'is-stream' | ||
import { Readable } from 'readable-stream' | ||
import rdf from '@zazuko/env' | ||
import { query, update } from '../inMemory.js' | ||
import * as ns from './support/namespaces.js' | ||
|
||
describe('query', () => { | ||
it('should be a function', () => { | ||
strictEqual(typeof query, 'function') | ||
}) | ||
|
||
it('should return a readable and witable stream', () => { | ||
const construct = 'CONSTRUCT { ?s ?p ?o } WHERE { ?s ?p ?o }' | ||
|
||
const result = query(construct) | ||
|
||
strictEqual(isReadableStream(result), true) | ||
strictEqual(isWritableStream(result), true) | ||
}) | ||
|
||
it('should CONSTRUCT quads', async () => { | ||
const chunk1 = [ | ||
rdf.quad(ns.ex.s1, ns.ex.p, ns.ex.o1), | ||
rdf.quad(ns.ex.s2, ns.ex.p, ns.ex.s2), | ||
] | ||
const chunk2 = [ | ||
rdf.quad(ns.ex.s3, ns.ex.p, ns.ex.o3), | ||
] | ||
|
||
const construct = query('CONSTRUCT { ?s ?p "ok" } WHERE { ?s ?p ?s }') | ||
const pipeline = Readable.from([chunk1, chunk2]).pipe(construct) | ||
const result = await getStream.array(pipeline) | ||
const dataset = rdf.dataset(result.flat()) | ||
|
||
strictEqual(result.length, 2) | ||
strictEqual(dataset.size, 1) | ||
ok(dataset.has(rdf.quad(ns.ex.s2, ns.ex.p, rdf.literal('ok')))) | ||
}) | ||
}) | ||
|
||
describe('update', () => { | ||
it('should be a function', () => { | ||
strictEqual(typeof update, 'function') | ||
}) | ||
|
||
it('should return a readable and witable stream', () => { | ||
const construct = 'DELETE { ?s ?p ?o } WHERE { ?s ?p ?o }' | ||
|
||
const result = update(construct) | ||
|
||
strictEqual(isReadableStream(result), true) | ||
strictEqual(isWritableStream(result), true) | ||
}) | ||
it('should UPDATE quads', async () => { | ||
const chunk1 = [ | ||
rdf.quad(ns.ex.s1, ns.ex.p, ns.ex.o1), | ||
rdf.quad(ns.ex.s2, ns.ex.p, ns.ex.s2), | ||
] | ||
const chunk2 = [ | ||
rdf.quad(ns.ex.s3, ns.ex.p, ns.ex.o3), | ||
] | ||
|
||
const command = update('DELETE { ?s ?p ?s } WHERE { ?s ?p ?s }') | ||
const pipeline = Readable.from([chunk1, chunk2]).pipe(command) | ||
const result = await getStream.array(pipeline) | ||
|
||
strictEqual(result.length, 2) | ||
const dataset = rdf.dataset(result.flat()) | ||
strictEqual(dataset.size, 2) | ||
ok(dataset.has(rdf.quad(ns.ex.s1, ns.ex.p, ns.ex.o1))) | ||
ok(dataset.has(rdf.quad(ns.ex.s3, ns.ex.p, ns.ex.o3))) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters