Skip to content

Commit

Permalink
make UnorderedMap and Set helper methods private (#179)
Browse files Browse the repository at this point in the history
* make UnorderedMap and Set helper methods private

* address Austin's comment
  • Loading branch information
ailisp authored Aug 18, 2022
1 parent 2e9e197 commit d09554b
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 93 deletions.
3 changes: 0 additions & 3 deletions lib/collections/unordered-map.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ export declare class UnorderedMap {
constructor(prefix: Bytes);
len(): number;
isEmpty(): boolean;
serializeIndex(index: number): Bytes;
deserializeIndex(rawIndex: Bytes): number;
getIndexRaw(key: Bytes): Bytes;
get(key: Bytes): unknown | null;
set(key: Bytes, value: unknown): unknown | null;
remove(key: Bytes): unknown | null;
Expand Down
40 changes: 20 additions & 20 deletions lib/collections/unordered-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@ import * as near from "../api";
import { u8ArrayToBytes, bytesToU8Array } from "../utils";
import { Vector, VectorIterator } from "./vector";
const ERR_INCONSISTENT_STATE = "The collection is an inconsistent state. Did previous smart contract execution terminate unexpectedly?";
function serializeIndex(index) {
let data = new Uint32Array([index]);
let array = new Uint8Array(data.buffer);
return u8ArrayToBytes(array);
}
function deserializeIndex(rawIndex) {
let array = bytesToU8Array(rawIndex);
let data = new Uint32Array(array.buffer);
return data[0];
}
function getIndexRaw(keyIndexPrefix, key) {
let indexLookup = keyIndexPrefix + JSON.stringify(key);
let indexRaw = near.storageRead(indexLookup);
return indexRaw;
}
export class UnorderedMap {
constructor(prefix) {
this.length = 0;
Expand All @@ -28,25 +43,10 @@ export class UnorderedMap {
}
return keysIsEmpty;
}
serializeIndex(index) {
let data = new Uint32Array([index]);
let array = new Uint8Array(data.buffer);
return u8ArrayToBytes(array);
}
deserializeIndex(rawIndex) {
let array = bytesToU8Array(rawIndex);
let data = new Uint32Array(array.buffer);
return data[0];
}
getIndexRaw(key) {
let indexLookup = this.keyIndexPrefix + JSON.stringify(key);
let indexRaw = near.storageRead(indexLookup);
return indexRaw;
}
get(key) {
let indexRaw = this.getIndexRaw(key);
let indexRaw = getIndexRaw(this.keyIndexPrefix, key);
if (indexRaw) {
let index = this.deserializeIndex(indexRaw);
let index = deserializeIndex(indexRaw);
let value = this.values.get(index);
if (value) {
return value;
Expand All @@ -61,12 +61,12 @@ export class UnorderedMap {
let indexLookup = this.keyIndexPrefix + JSON.stringify(key);
let indexRaw = near.storageRead(indexLookup);
if (indexRaw) {
let index = this.deserializeIndex(indexRaw);
let index = deserializeIndex(indexRaw);
return this.values.replace(index, value);
}
else {
let nextIndex = this.len();
let nextIndexRaw = this.serializeIndex(nextIndex);
let nextIndexRaw = serializeIndex(nextIndex);
near.storageWrite(indexLookup, nextIndexRaw);
this.keys.push(key);
this.values.push(value);
Expand Down Expand Up @@ -97,7 +97,7 @@ export class UnorderedMap {
near.storageWrite(lastLookupKey, indexRaw);
}
}
let index = this.deserializeIndex(indexRaw);
let index = deserializeIndex(indexRaw);
this.keys.swapRemove(index);
return this.values.swapRemove(index);
}
Expand Down
2 changes: 0 additions & 2 deletions lib/collections/unordered-set.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ export declare class UnorderedSet {
constructor(prefix: Bytes);
len(): number;
isEmpty(): boolean;
serializeIndex(index: number): string;
deserializeIndex(rawIndex: Bytes): number;
contains(element: unknown): boolean;
set(element: unknown): boolean;
remove(element: unknown): boolean;
Expand Down
24 changes: 12 additions & 12 deletions lib/collections/unordered-set.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ import * as near from "../api";
import { u8ArrayToBytes, bytesToU8Array } from "../utils";
import { Vector } from "./vector";
const ERR_INCONSISTENT_STATE = "The collection is an inconsistent state. Did previous smart contract execution terminate unexpectedly?";
function serializeIndex(index) {
let data = new Uint32Array([index]);
let array = new Uint8Array(data.buffer);
return u8ArrayToBytes(array);
}
function deserializeIndex(rawIndex) {
let array = bytesToU8Array(rawIndex);
let data = new Uint32Array(array.buffer);
return data[0];
}
export class UnorderedSet {
constructor(prefix) {
this.length = 0;
Expand All @@ -16,16 +26,6 @@ export class UnorderedSet {
isEmpty() {
return this.elements.isEmpty();
}
serializeIndex(index) {
let data = new Uint32Array([index]);
let array = new Uint8Array(data.buffer);
return u8ArrayToBytes(array);
}
deserializeIndex(rawIndex) {
let array = bytesToU8Array(rawIndex);
let data = new Uint32Array(array.buffer);
return data[0];
}
contains(element) {
let indexLookup = this.elementIndexPrefix + JSON.stringify(element);
return near.storageHasKey(indexLookup);
Expand All @@ -37,7 +37,7 @@ export class UnorderedSet {
}
else {
let nextIndex = this.len();
let nextIndexRaw = this.serializeIndex(nextIndex);
let nextIndexRaw = serializeIndex(nextIndex);
near.storageWrite(indexLookup, nextIndexRaw);
this.elements.push(element);
return true;
Expand Down Expand Up @@ -67,7 +67,7 @@ export class UnorderedSet {
near.storageWrite(lastLookupElement, indexRaw);
}
}
let index = this.deserializeIndex(indexRaw);
let index = deserializeIndex(indexRaw);
this.elements.swapRemove(index);
return true;
}
Expand Down
48 changes: 24 additions & 24 deletions src/collections/unordered-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@ import { Vector, VectorIterator } from "./vector";
const ERR_INCONSISTENT_STATE =
"The collection is an inconsistent state. Did previous smart contract execution terminate unexpectedly?";

function serializeIndex(index: number): Bytes {
let data = new Uint32Array([index]);
let array = new Uint8Array(data.buffer);
return u8ArrayToBytes(array);
}

function deserializeIndex(rawIndex: Bytes) {
let array = bytesToU8Array(rawIndex);
let data = new Uint32Array(array.buffer);
return data[0];
}

function getIndexRaw(keyIndexPrefix: Bytes, key: Bytes): Bytes {
let indexLookup = keyIndexPrefix + JSON.stringify(key);
let indexRaw = near.storageRead(indexLookup);
return indexRaw;
}

export class UnorderedMap {
readonly length: number;
readonly prefix: Bytes;
Expand Down Expand Up @@ -40,28 +58,10 @@ export class UnorderedMap {
return keysIsEmpty;
}

serializeIndex(index: number): Bytes {
let data = new Uint32Array([index]);
let array = new Uint8Array(data.buffer);
return u8ArrayToBytes(array);
}

deserializeIndex(rawIndex: Bytes) {
let array = bytesToU8Array(rawIndex);
let data = new Uint32Array(array.buffer);
return data[0];
}

getIndexRaw(key: Bytes): Bytes {
let indexLookup = this.keyIndexPrefix + JSON.stringify(key);
let indexRaw = near.storageRead(indexLookup);
return indexRaw;
}

get(key: Bytes): unknown | null {
let indexRaw = this.getIndexRaw(key);
let indexRaw = getIndexRaw(this.keyIndexPrefix, key);
if (indexRaw) {
let index = this.deserializeIndex(indexRaw);
let index = deserializeIndex(indexRaw);
let value = this.values.get(index);
if (value) {
return value;
Expand All @@ -76,11 +76,11 @@ export class UnorderedMap {
let indexLookup = this.keyIndexPrefix + JSON.stringify(key);
let indexRaw = near.storageRead(indexLookup);
if (indexRaw) {
let index = this.deserializeIndex(indexRaw);
let index = deserializeIndex(indexRaw);
return this.values.replace(index, value);
} else {
let nextIndex = this.len();
let nextIndexRaw = this.serializeIndex(nextIndex);
let nextIndexRaw = serializeIndex(nextIndex);
near.storageWrite(indexLookup, nextIndexRaw);
this.keys.push(key);
this.values.push(value);
Expand Down Expand Up @@ -111,7 +111,7 @@ export class UnorderedMap {
near.storageWrite(lastLookupKey, indexRaw);
}
}
let index = this.deserializeIndex(indexRaw);
let index = deserializeIndex(indexRaw);
this.keys.swapRemove(index);
return this.values.swapRemove(index);
}
Expand Down Expand Up @@ -162,7 +162,7 @@ export class UnorderedMap {
// reconstruct values Vector
map.values = new Vector(data.prefix + "v");
map.values.length = data.values.length;
return map;
return map as UnorderedMap;
}
}

Expand Down
30 changes: 15 additions & 15 deletions src/collections/unordered-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ import { Mutable } from "../utils";
const ERR_INCONSISTENT_STATE =
"The collection is an inconsistent state. Did previous smart contract execution terminate unexpectedly?";

function serializeIndex(index: number) {
let data = new Uint32Array([index]);
let array = new Uint8Array(data.buffer);
return u8ArrayToBytes(array);
}

function deserializeIndex(rawIndex: Bytes): number {
let array = bytesToU8Array(rawIndex);
let data = new Uint32Array(array.buffer);
return data[0];
}

export class UnorderedSet {
readonly length: number;
readonly prefix: Bytes;
Expand All @@ -28,18 +40,6 @@ export class UnorderedSet {
return this.elements.isEmpty();
}

serializeIndex(index: number) {
let data = new Uint32Array([index]);
let array = new Uint8Array(data.buffer);
return u8ArrayToBytes(array);
}

deserializeIndex(rawIndex: Bytes): number {
let array = bytesToU8Array(rawIndex);
let data = new Uint32Array(array.buffer);
return data[0];
}

contains(element: unknown): boolean {
let indexLookup = this.elementIndexPrefix + JSON.stringify(element);
return near.storageHasKey(indexLookup);
Expand All @@ -51,7 +51,7 @@ export class UnorderedSet {
return false;
} else {
let nextIndex = this.len();
let nextIndexRaw = this.serializeIndex(nextIndex);
let nextIndexRaw = serializeIndex(nextIndex);
near.storageWrite(indexLookup, nextIndexRaw);
this.elements.push(element);
return true;
Expand Down Expand Up @@ -81,7 +81,7 @@ export class UnorderedSet {
near.storageWrite(lastLookupElement, indexRaw);
}
}
let index = this.deserializeIndex(indexRaw);
let index = deserializeIndex(indexRaw);
this.elements.swapRemove(index);
return true;
}
Expand Down Expand Up @@ -129,6 +129,6 @@ export class UnorderedSet {
let elementsPrefix = data.prefix + "e";
set.elements = new Vector(elementsPrefix);
set.elements.length = data.elements.length;
return set;
return set as UnorderedSet;
}
}
4 changes: 2 additions & 2 deletions tests/jsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"compilerOptions": {
"experimentalDecorators": true,
"experimentalDecorators": true
},
"exclude": [
"node_modules"
],
]
}
15 changes: 0 additions & 15 deletions tests/src/unordered-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,6 @@ class UnorderedMapTestContract extends NearContract {
return this.unorderedMap.isEmpty();
}

@view
serializeIndex({index}) {
return this.unorderedMap.serializeIndex(index);
}

@view
deserializeIndex({rawIndex}) {
return this.unorderedMap.deserializeIndex(rawIndex);
}

@view
getIndexRaw({key}) {
return this.unorderedMap.getIndexRaw(key);
}

@view
get({key}) {
return this.unorderedMap.get(key);
Expand Down

0 comments on commit d09554b

Please sign in to comment.