Skip to content

Commit 047ccd3

Browse files
authored
Merge pull request #30 from Terran-One/feat/immutable-store
Use immutable.js for storage backend
2 parents a03c16b + 2a7721e commit 047ccd3

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
"@types/secp256k1": "^4.0.3",
7070
"bech32": "^2.0.0",
7171
"elliptic": "^6.5.4",
72+
"immutable": "^4.1.0",
7273
"process": "^0.11.10",
7374
"secp256k1": "^4.0.3",
7475
"synchronized-promise": "^0.3.1",

src/backend/storage.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { fromBase64, toBase64 } from '@cosmjs/encoding';
22
import { compare } from '../helpers/byte-array';
3+
import Immutable from 'immutable';
34
import { MAX_LENGTH_DB_KEY } from '../instance';
45

56
export interface IStorage {
@@ -40,11 +41,11 @@ export interface IIterStorage {
4041

4142
export class BasicKVStorage implements IStorage {
4243
// TODO: Add binary uint / typed Addr maps for cw-storage-plus compatibility
43-
constructor(public dict: { [key: string]: string | undefined } = {}) {}
44+
constructor(public dict: Immutable.Map<string, string> = Immutable.Map()) {}
4445

4546
get(key: Uint8Array): Uint8Array | null {
4647
const keyStr = toBase64(key);
47-
const value = this.dict[keyStr];
48+
const value = this.dict.get(keyStr);
4849
if (value === undefined) {
4950
return null;
5051
}
@@ -54,7 +55,7 @@ export class BasicKVStorage implements IStorage {
5455

5556
set(key: Uint8Array, value: Uint8Array): void {
5657
const keyStr = toBase64(key);
57-
this.dict[keyStr] = toBase64(value);
58+
this.dict = this.dict.set(keyStr, toBase64(value));
5859
}
5960

6061
remove(key: Uint8Array): void {
@@ -63,7 +64,7 @@ export class BasicKVStorage implements IStorage {
6364
`Key length ${key.length} exceeds maximum length ${MAX_LENGTH_DB_KEY}.`
6465
);
6566
}
66-
this.dict[toBase64(key)] = undefined;
67+
this.dict = this.dict.remove(toBase64(key));
6768
}
6869
}
6970

@@ -119,11 +120,11 @@ export class BasicKVIterStorage extends BasicKVStorage implements IIterStorage {
119120
}
120121

121122
let data: Record[] = [];
122-
for (const key of Object.keys(this.dict)) {
123+
for (const key of this.dict.keys()) {
123124
if (start.length && compare(start, fromBase64(key)) === 1) continue;
124125
if (end.length && compare(fromBase64(key), end) > -1) break;
125126

126-
data.push({ key: fromBase64(key), value: fromBase64(this.dict[key]!) });
127+
data.push({ key: fromBase64(key), value: this.get(fromBase64(key))! });
127128
}
128129

129130
if (order === Order.Descending) {

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3881,6 +3881,11 @@ ignore@^5.1.1, ignore@^5.1.8, ignore@^5.2.0:
38813881
resolved "https://registry.npmjs.org/ignore/-/ignore-5.2.0.tgz"
38823882
integrity sha512-CmxgYGiEPCLhfLnpPp1MoRmifwEIOgjcHXxOBjv7mY96c+eWScsOP9c112ZyLdWHi0FxHjI+4uVhKYp/gcdRmQ==
38833883

3884+
immutable@^4.1.0:
3885+
version "4.1.0"
3886+
resolved "https://registry.yarnpkg.com/immutable/-/immutable-4.1.0.tgz#f795787f0db780183307b9eb2091fcac1f6fafef"
3887+
integrity sha512-oNkuqVTA8jqG1Q6c+UglTOD1xhC1BtjKI7XkCXRkZHrN5m18/XsnUp8Q89GkQO/z+0WjonSvl0FLhDYftp46nQ==
3888+
38843889
import-fresh@^3.0.0, import-fresh@^3.2.1:
38853890
version "3.3.0"
38863891
resolved "https://registry.npmjs.org/import-fresh/-/import-fresh-3.3.0.tgz"

0 commit comments

Comments
 (0)