git ssb

0+

cel / kvset



Commit 4bca6cd77b9f7e49c4f0b9c9419f10f9c94e02b2

Initial commit

cel committed on 11/1/2016, 12:17:47 AM

Files changed

README.mdadded
index.jsadded
package.jsonadded
test.jsadded
README.mdView
@@ -1,0 +1,30 @@
1 +# kvset
2 +
3 +A set that can be updated using key-value links to indicate causal order.
4 +Intended to be used with hash-link systems, like [secure-scuttlebutt][].
5 +
6 +See also [hyperlog][], [kvgraph][]
7 +
8 +## API
9 +
10 +```
11 +var KVSet = require('kvset')
12 +var set = new KVSet()
13 +```
14 +
15 +### `set.add(key, value)`
16 +
17 +Add a value to the set with the given key.
18 +
19 +### `set.remove(key)`
20 +
21 +Remove the value for the given key from the set.
22 +
23 +### `set.replace(links, key, value)`
24 +
25 +Replace values pointed to by keys in `links` with `value` at `key`.
26 +Equivalent to calling `add(key, value)` and `remove(link)` for each link in `links`
27 +
28 +[hyperlog]: https://github.com/mafintosh/hyperlog
29 +[kvgraph]: https://git-ssb.celehner.com/%25oJnY1QfgTgg%2BQhpa7COv5WGWZLohz74%2B77thqH6tzqU%3D.sha256
30 +[secure-scuttlebutt]: https://git-ssb.celehner.com/%25iljFzUwTYposC7vs2V6AZgObPqwRVNAXjxYVVUoG4tU%3D.sha256
index.jsView
@@ -1,0 +1,22 @@
1 +module.exports = KVSet
2 +
3 +function KVSet() {
4 + if (!(this instanceof KVSet)) return new Set()
5 + this.heads = {}
6 + this.tails = {}
7 +}
8 +
9 +KVSet.prototype.add = function (key, value) {
10 + if (key in this.tails) delete this.tails[key]
11 + else this.heads[key] = value
12 +}
13 +
14 +KVSet.prototype.replace = function (links, key, value) {
15 + for (var i = 0; i < links.length; i++) this.remove(links[i])
16 + this.add(key, value)
17 +}
18 +
19 +KVSet.prototype.remove = function (key) {
20 + if (key in this.heads) delete this.heads[key]
21 + else this.tails[key] = true
22 +}
package.jsonView
@@ -1,0 +1,15 @@
1 +{
2 + "name": "kvset",
3 + "version": "0.0.0",
4 + "description": "set updatable with key-value causal links",
5 + "main": "index.js",
6 + "scripts": {
7 + "test": "node test.js"
8 + },
9 + "devDependencies": {
10 + "tape": "^4.6.0"
11 + },
12 + "repository": "https://git-ssb.celehner.com/%25jjC5eRj%2BPtp30xQV2W0aSCiKwSNQR5nnVcckDtjjPC0%3D.sha256",
13 + "author": "cel",
14 + "license": "Fair"
15 +}
test.jsView
@@ -1,0 +1,27 @@
1 +var test = require('tape')
2 +var Set = require('.')
3 +
4 +test('kvset', function (t) {
5 + var set = new Set()
6 + set.add(1, 'foo')
7 + t.deepEqual(set.heads, {1: 'foo'}, 'set head')
8 + set.add(2, 'bar')
9 + set.add(3, 'qux')
10 + t.deepEqual(set.heads, {1: 'foo', 2: 'bar', 3: 'qux'}, 'add more heads')
11 + t.deepEqual(set.tails, {}, 'no tails')
12 + set.remove(1)
13 + t.deepEqual(set.heads, {2: 'bar', 3: 'qux'}, 'remove a value')
14 + set.add(4, 'baz')
15 + t.deepEqual(set.heads, {2: 'bar', 3: 'qux', 4: 'baz'}, 'add a value')
16 + set.replace([4, 2], 5, 'blah')
17 + t.deepEqual(set.heads, {3: 'qux', 5: 'blah'}, 'replace values')
18 + set.replace([3, 8], 7, 'thing')
19 + t.deepEqual(set.heads, {5: 'blah', 7: 'thing'}, 'add thing with unknown link')
20 + t.ok(8 in set.tails, 'wait for tail')
21 + set.add(8, 'nop')
22 + t.deepEqual(set.heads, {5: 'blah', 7: 'thing'}, 'tail does not become a head')
23 + t.deepEqual(set.tails, {}, 'no tails')
24 + set.replace([7], 9, 'ok')
25 + t.deepEqual(set.heads, {5: 'blah', 9: 'ok'}, 'replace a thing')
26 + t.end()
27 +})

Built with git-ssb-web