Skip to content

Commit

Permalink
Merge pull request #4 from SimplyEdit/sharedArrayBuffer
Browse files Browse the repository at this point in the history
Shared array buffer implementation
  • Loading branch information
poef authored Feb 19, 2024
2 parents 25cabb9 + c26835e commit 252c700
Show file tree
Hide file tree
Showing 23 changed files with 8,494 additions and 520 deletions.
5 changes: 5 additions & 0 deletions example/commands.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
addPerson: (dataspace, command, request, meta) => {
dataspace.people.push(command.value)
}
}
4 changes: 3 additions & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"dependencies": {
"@muze-nl/jsontag": "^0.8.6",
"@muze-nl/jsontag-types": "^0.1.1",
"@muze-nl/simplystore": "file:../"
"@muze-nl/simplystore": "file:../",
"eslint": "^8.56.0",
"uuid": "^9.0.1"
}
}
36 changes: 36 additions & 0 deletions example/run-command.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { v4 as uuid } from 'uuid'
import JSONTag from '@muze-nl/jsontag'

// run command to localhost:3000

let id = uuid()
let commandStr = `{
"id": "${id}",
"name": "addPerson",
"value": {
"name": "Some Stormtrooper",
"gender": "male",
"homeworld": <link>"http://swapi.co/api/planets/1/"
}
}`


async function main() {
let response = await fetch('http://localhost:3000/command', {
method: 'POST',
headers: {
'Accept': 'application/jsontag',
'Content-Type': 'application/jsontag'
},
body: commandStr
})
if (!response.ok) {
let text = await response.text()
console.error(response.status+': '+response.statusText, text)
} else {
let data = await response.json()
console.log('response:', data)
}
}

main()
14 changes: 5 additions & 9 deletions example/server.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import SimplyStore from '@muze-nl/simplystore'
import fs from 'fs'
import JSONTag from '@muze-nl/jsontag'

let str = fs.readFileSync(process.cwd()+'/data.jsontag','utf-8')
const data = JSONTag.parse(str)
import SimplyStore from '../src/server.mjs'

SimplyStore.run({
dataspace: data
})

datafile: process.cwd()+'/data.jsontag',
commandsFile: process.cwd()+'/commands.mjs',
commandLog: process.cwd()+'/command-log.jsontag',
})
7,186 changes: 7,186 additions & 0 deletions example/swdb.jsontag

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"json-pointer": "^0.6.2",
"jsonpath-plus": "^7.2.0",
"piscina": "^4.1.0",
"vm2": "^3.9.13"
"vm2": "^3.9.13",
"write-file-atomic": "^5.0.1"
},
"devDependencies": {
"eslint": "^8.48.0",
Expand Down
25 changes: 25 additions & 0 deletions scripts/convert.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import JSONTag from '@muze-nl/jsontag'
import fastStringify from '../src/fastStringify.mjs'
import fs from 'node:fs'

if (process.argv.length<=3) {
console.log('usage: node ./convert.mjs {inputfile} {outputfile}')
process.exit()
}

// parse command line
let inputFile = process.argv[2]
let outputFile = process.argv[3]

// load file
let input = fs.readFileSync(inputFile, 'utf-8')

// parse jsontag
let data = JSONTag.parse(input)

// write resultset to output
let strData = fastStringify(data)

fs.writeFileSync(outputFile, strData)

console.log('Converted data written to ',outputFile)
100 changes: 100 additions & 0 deletions src/command-worker-module.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import JSONTag from '@muze-nl/jsontag'
import {source} from './symbols.mjs'
import fastParse from './fastParse.mjs'
import {stringToSAB,resultSetStringify} from './fastStringify.mjs'
import writeFileAtomic from 'write-file-atomic'

let commands = {}
let resultSet = []
let dataspace
let datafile
let meta = {}
let metaProxy = {
index: {
}
}

export const metaIdProxy = {
forEach: (callback) => {
meta.index.id.forEach((ref,id) => {
callback({
deref: () => {
return resultSet[ref]
}
},id)
})
},
set: (id,ref) => {
//FICME: is this correct?
meta.index.id.set(id, resultSet.length-1)
},
get: (id) => {
let index = meta.index.id.get(id)
if (index || index===0) {
return {
deref: () => {
return resultSet[index]
}
}
}
},
has: (id) => {
return meta.index.id.has(id)
}
}

export const FastJSONTag = {
getType: (obj) => JSONTag.getType(obj[source]),
getAttribute: (obj, attr) => JSONTag.getAttribute(obj[source],attr),
setAttribute: (obj, attr, value) => JSONTag.setAttribute(obj[source], attr, value),
getAttributes: (obj) => JSONTag.getAttributes(obj[source]),
getAttributeString: (obj) => JSONTag.getAttributesString(obj[source]),
getTypeString: (obj) => JSONTag.getTypeString(obj[source])
}

export async function initialize(task) {
resultSet = fastParse(task.data, task.meta, false) // false means mutable
dataspace = resultSet[0]
meta = task.meta
metaProxy.index.id = metaIdProxy
datafile = task.datafile
commands = await import(task.commandsFile).then(mod => {
return mod.default
})
}

export default async function runCommand(commandStr, request) {
let task = JSONTag.parse(commandStr, null, metaProxy)
if (!task.id) { throw new Error('missing command id')}
if (!task.name) { throw new Error('missing command name parameter')}
let response = {
jsontag: true
}
if (commands[task.name]) {
try {
commands[task.name](dataspace, task, request, metaProxy)
FastJSONTag.setAttribute(dataspace, 'command', task.id)

const strData = resultSetStringify(resultSet)
const uint8sab = stringToSAB(strData)
response.data = uint8sab
response.meta = {
index: {
id: meta.index.id
}
}

await writeFileAtomic(datafile, uint8sab)
} catch(err) {
console.error('error',err)
response.code = 422;
response.body = '<object class="Error">{"message":'+JSON.stringify(''+err)+',"code":422}'
}
} else {
console.error('Command not found', task.name, commands)
response.code = 404
response.body = '<object class="Error">{"message":"Command '+task.name+' not found","code":404}'
}
return response
}

13 changes: 13 additions & 0 deletions src/command-worker.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { parentPort } from 'node:worker_threads'
import runCommand, { initialize } from '../src/command-worker-module.mjs'

parentPort.on('message', async data => {
let result
try {
await initialize(data)
result = await runCommand(data.command)
} catch(err) {
result = { error: err.message }
}
parentPort.postMessage(result)
})
Loading

0 comments on commit 252c700

Please sign in to comment.