@typeberry/trie
Advanced tools
Comparing version 0.0.1-b19c732 to 0.0.1-bdc68c7
73
index.js
@@ -35,2 +35,7 @@ 'use strict'; | ||
} | ||
/** Create a new [`BytesBlob'] by converting given UTF-u encoded string into bytes. */ | ||
static fromString(v) { | ||
const encoder = new TextEncoder(); | ||
return BytesBlob.fromBlob(encoder.encode(v)); | ||
} | ||
/** Create a new [`BytesBlob`] from existing [`Uint8Array`]. */ | ||
@@ -398,2 +403,39 @@ static fromBlob(v) { | ||
/** A map which uses hashes as keys. */ | ||
class HashDictionary { | ||
constructor() { | ||
// TODO [ToDr] [crit] We can't use `TrieHash` directly in the map, | ||
// because of the way it's being compared. Hence having `string` here. | ||
// This has to be benchmarked and re-written to a custom map most likely. | ||
this.map = new Map(); | ||
} | ||
/** Return true if the key is present in the dictionary. */ | ||
has(key) { | ||
return this.map.has(key.toString()); | ||
} | ||
/** Get the value under given key or `null` if the value is not present. */ | ||
get(key) { | ||
return this.map.get(key.toString()); | ||
} | ||
/** Insert/overwrite the value at given key. */ | ||
set(key, value) { | ||
this.map.set(key.toString(), value); | ||
} | ||
/** Remove the key and any value from the dictionary. */ | ||
delete(key) { | ||
this.map.delete(key.toString()); | ||
} | ||
} | ||
/** A return value of some comparator. */ | ||
var Ordering; | ||
(function (Ordering) { | ||
/** `self < other` */ | ||
Ordering[Ordering["Less"] = -1] = "Less"; | ||
/** `self === other` */ | ||
Ordering[Ordering["Equal"] = 0] = "Equal"; | ||
/** `self > other` */ | ||
Ordering[Ordering["Greater"] = 1] = "Greater"; | ||
})(Ordering || (Ordering = {})); | ||
/** | ||
@@ -405,7 +447,8 @@ * An abstraction over read-only nodes storage. | ||
this.hasher = hasher; | ||
this.nodes = new Map(); | ||
this.nodes = new HashDictionary(); | ||
} | ||
get(hash) { | ||
const key = NodesDb.hashCompatStr(hash); | ||
return this.nodes.get(key) ?? null; | ||
return NodesDb.withHashCompat(hash, (key) => { | ||
return this.nodes.get(key) ?? null; | ||
}); | ||
} | ||
@@ -419,14 +462,14 @@ hashNode(n) { | ||
* Before calling `toString` the first bit is set to 0, to maintain compatibility | ||
* with branch nodes, which have the left subtree stripped out of the first bit (since it's | ||
* a branch node identifier). | ||
* with branch nodes, which have the left subtree stripped out of the first bit | ||
* (since it's a branch node identifier). | ||
* | ||
*/ | ||
static hashCompatStr(hash) { | ||
static withHashCompat(hash, exe) { | ||
const prevValue = hash.raw[0]; | ||
hash.raw[0] &= 0b1111_1110; | ||
const hashString = hash.toString(); | ||
const returnValue = exe(hash); | ||
// restore the original byte, so that we have correct value in case it | ||
// ends up in the right part of the subtree. | ||
hash.raw[0] = prevValue; | ||
return hashString; | ||
return returnValue; | ||
} | ||
@@ -439,9 +482,11 @@ } | ||
remove(hash) { | ||
const key = NodesDb.hashCompatStr(hash); | ||
this.nodes.delete(key); | ||
return NodesDb.withHashCompat(hash, (key) => { | ||
this.nodes.delete(key); | ||
}); | ||
} | ||
insert(node, hash) { | ||
const h = hash ?? this.hashNode(node); | ||
const key = NodesDb.hashCompatStr(h); | ||
this.nodes.set(key, node); | ||
NodesDb.withHashCompat(h, (key) => { | ||
this.nodes.set(key, node); | ||
}); | ||
return h; | ||
@@ -466,2 +511,6 @@ } | ||
} | ||
remove(_) { | ||
// TODO [ToDr] implement me. | ||
throw new Error("Removing from the trie not implemented yet."); | ||
} | ||
getRoot() { | ||
@@ -468,0 +517,0 @@ if (this.root === null) { |
{ | ||
"name": "@typeberry/trie", | ||
"version": "0.0.1-b19c732", | ||
"version": "0.0.1-bdc68c7", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "author": "Fluffy Labs", |
@@ -15,2 +15,4 @@ /** | ||
toString(): string; | ||
/** Create a new [`BytesBlob'] by converting given UTF-u encoded string into bytes. */ | ||
static fromString(v: string): BytesBlob; | ||
/** Create a new [`BytesBlob`] from existing [`Uint8Array`]. */ | ||
@@ -17,0 +19,0 @@ static fromBlob(v: Uint8Array): BytesBlob; |
@@ -0,1 +1,2 @@ | ||
import { HashDictionary } from "@typeberry/collections"; | ||
import type { TrieHash, TrieNode } from "./nodes"; | ||
@@ -13,3 +14,3 @@ /** | ||
readonly hasher: TrieHasher; | ||
protected readonly nodes: Map<string, TrieNode>; | ||
protected readonly nodes: HashDictionary<TrieHash, TrieNode>; | ||
constructor(hasher: TrieHasher); | ||
@@ -22,7 +23,7 @@ get(hash: TrieHash): TrieNode | null; | ||
* Before calling `toString` the first bit is set to 0, to maintain compatibility | ||
* with branch nodes, which have the left subtree stripped out of the first bit (since it's | ||
* a branch node identifier). | ||
* with branch nodes, which have the left subtree stripped out of the first bit | ||
* (since it's a branch node identifier). | ||
* | ||
*/ | ||
protected static hashCompatStr(hash: TrieHash): string; | ||
protected static withHashCompat<T>(hash: TrieHash, exe: (hash: TrieHash) => T): T; | ||
} | ||
@@ -29,0 +30,0 @@ /** |
@@ -11,4 +11,5 @@ import { type BytesBlob } from "@typeberry/bytes"; | ||
set(key: StateKey, value: BytesBlob, maybeValueHash?: TrieHash): void; | ||
remove(_: StateKey): void; | ||
getRoot(): TrieHash; | ||
toString(): string; | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
58811
19
1616
0