Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Batch operation #241

Merged
merged 5 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/young-geese-teach.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"barnard59-base": patch
giacomociti marked this conversation as resolved.
Show resolved Hide resolved
---

added batch operation
26 changes: 26 additions & 0 deletions packages/base/batch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Transform } from 'readable-stream'

function batch(size = 0) {
let array = []
const maxSize = Number(size)
return new Transform({
readableObjectMode: true,
writableObjectMode: true,
readableHighWaterMark: maxSize,
writableHighWaterMark: maxSize,
transform(item, _encoding, callback) {
array.push(item)
if (array.length === maxSize) {
this.push(array)
array = []
}
callback()
},
flush(callback) {
if (array.length > 0) this.push(array)
callback()
},
})
}

export default batch
7 changes: 7 additions & 0 deletions packages/base/manifest.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@
code:link <node:barnard59-base/filter.js#default>
].

<batch> a p:Operation, p:WritableObjectMode, p:ReadableObjectMode;
rdfs:label "Batch";
rdfs:comment "Groups incoming items into arrays of the given size.";
code:implementedBy [ a code:EcmaScriptModule;
code:link <node:barnard59-base/batch.js#default>
].

<flatten> a p:Operation, p:WritableObjectMode, p:ReadableObjectMode;
rdfs:label "Flatten";
rdfs:comment "Separates incoming arrays into their elements and emits each element as a single chunk.";
Expand Down
59 changes: 59 additions & 0 deletions packages/base/test/batch.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { deepStrictEqual, strictEqual } from 'assert'
import { array } from 'get-stream'
import { isReadable, isWritable } from 'isstream'
import { Readable } from 'readable-stream'
import batch from '../batch.js'

describe('batch', () => {
it('should be a function', () => {
strictEqual(typeof batch, 'function')
})

it('should return a duplex stream', async () => {
const result = batch()

strictEqual(isWritable(result), true)
strictEqual(isReadable(result), true)
})

it('should do nothing if there are no input chunks', async () => {
const input = Readable({
objectMode: true,
read: () => {
input.push(null)
},
})

const result = await array(input.pipe(batch()))

deepStrictEqual(result, [])
})

it('should split input in batches', async () => {
const expected = [['a', 'b'], ['c', 'd'], ['e']]
const input = Readable({
objectMode: true,
read: () => {
['a', 'b', 'c', 'd', 'e', null].forEach(item => input.push(item))
},
})

const result = await array(input.pipe(batch(2)))

deepStrictEqual(result, expected)
})

it('should emit a single batch', async () => {
const expected = [['a', 'b', 'c', 'd', 'e']]
const input = Readable({
objectMode: true,
read: () => {
['a', 'b', 'c', 'd', 'e', null].forEach(item => input.push(item))
},
})

const result = await array(input.pipe(batch(0)))

deepStrictEqual(result, expected)
})
})
31 changes: 0 additions & 31 deletions packages/cube/lib/batch.js

This file was deleted.

13 changes: 2 additions & 11 deletions packages/cube/pipeline/check-observations.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ _:sortChunkSize a p:Variable ;
[ n3:parse () ]
_:sortBySubject
[ splitDataset:bySubject () ]
_:batch
[ base:batch ("batchSize"^^p:VariableName) ]
[ base:flatten () ]
tpluscode marked this conversation as resolved.
Show resolved Hide resolved
[ shacl:report
[code:name "shape" ; code:value _:getConstraint ] ,
[code:name "maxErrors" ; code:value "maxViolations"^^p:VariableName ]
Expand All @@ -56,16 +57,6 @@ _:sortChunkSize a p:Variable ;
]
.

_:batch a p:Step ;
code:implementedBy
[
a code:EcmaScriptModule ;
code:link <file:../lib/batch.js#batch> ;
] ;
code:arguments ("batchSize"^^p:VariableName)
.


_:getConstraint a p:Pipeline , p:ReadableObjectMode ;
p:variables [ p:variable _:constraint ] ;
p:steps
Expand Down