Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/mops-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ jobs:
- uses: actions/checkout@v4
- uses: ZenVoich/setup-mops@v1

- name: Install dependencies
run: mops install

- name: make sure moc is installed
run: mops toolchain bin moc || mops toolchain use moc latest

- name: run tests
run: mops test
run: mops test
8 changes: 8 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,11 @@ Before:

## Version 1.0.5
- The performance enhanced ( == less cycles needed for the operations. approx 30-40 %)

## Version 1.0.6

- updated dependencies

- removed dependency memory-buffer and StableTrieMap through copy and paste
the needed code directly.

16 changes: 8 additions & 8 deletions makefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
install-check:
install-check:

ifeq (, $(shell which curl))
@echo No curl is installed, curl will be installed now....
@echo No curl is installed, curl will be installed now....
@sudo apt-get install curl -y
endif

ifeq (,$(shell which $(HOME)/bin/dfx))
ifeq (,$(shell which $(HOME)/bin/dfx))
@echo No dfx is installed, dfx will be installed now....
curl -fsSL https://internetcomputer.org/install.sh -o install_dfx.sh
chmod +x install_dfx.sh
./install_dfx.sh
rm install_dfx.sh
rm install_dfx.sh
endif

ifeq (, $(shell which nodejs))
Expand All @@ -25,7 +25,7 @@ ifeq (, $(shell which mops))
sudo npm i -g ic-mops
endif

ifeq (, $(shell which $(HOME)/bin/vessel))
ifeq (, $(shell which $(HOME)/bin/vessel))
rm installvessel.sh -f
echo '#install vessel'>installvessel.sh
echo cd $(HOME)/bin>>installvessel.sh
Expand All @@ -35,12 +35,12 @@ ifeq (, $(shell which $(HOME)/bin/vessel))
chmod +x installvessel.sh
./installvessel.sh
rm installvessel.sh -f
endif
endif

mops init
mops add memory-buffer
mops add test
mops add memory-region
mops add bench --dev
mops update
dfx upgrade
# dfx upgrade
dfxvm update
18 changes: 8 additions & 10 deletions mops.toml
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
[package]
name = "memory-hashtable"
version = "1.0.5"
version = "1.0.6"
description = "Storing/updating/deletion/retrieving of a single blob-value per key. (url: https://github.com/fGhost713/memory-HashTable)"
repository = "https://github.com/fGhost713/memory-HashTable"
keywords = [ "storing", "memory", "hashlist", "hashmap", "datastructure" ]
keywords = [ "storing", "memory", "hashlist", "hashmap", "data-structure" ]
license = "MIT"

[dependencies]
base = "0.10.4"
memory-region = "0.2.0"
StableTrieMap = "https://github.com/NatLabs/StableTrieMap#main@4781cb03efd34b124c22396c69710b374366c797"
itertools = "0.2.1"
test = "1.2.0"
memory-buffer = "0.0.2"
base = "0.16.0"
memory-region = "1.3.2"
itertools = "0.2.2"
test = "2.1.1"

[toolchain]
wasmtime = "18.0.1"
wasmtime = "21.0.0"

[dev-dependencies]
bench = "1.0.0"
bench = "1.0.0"
182 changes: 182 additions & 0 deletions src/helpers/blobify.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@

/// Blobify is a module that provides a generic interface for converting
/// values to and from blobs. It is intended to be used for serializing
/// and deserializing values that will be stored in persistent stable memory.
///
/// The Blobify module provides a default implementation for the following
/// types:
/// - Nat
/// - Nat8
/// - Nat16
/// - Nat32
/// - Nat64
/// - Blob
/// - Bool
/// - Text
/// - Principal

import TextModule "mo:base/Text";
import BlobModule "mo:base/Blob";
import ArrayModule "mo:base/Array";
import NatModule "mo:base/Nat";
import Nat8Module "mo:base/Nat8";
import Nat16Module "mo:base/Nat16";
import Nat32Module "mo:base/Nat32";
import Nat64Module "mo:base/Nat64";
import PrincipalModule "mo:base/Principal";

import Debug "mo:base/Debug";

module {

let Base = {
Array = ArrayModule;
Blob = BlobModule;
Nat = NatModule;
Nat8 = Nat8Module;
Nat16 = Nat16Module;
Nat32 = Nat32Module;
Nat64 = Nat64Module;
Text = TextModule;
Principal = PrincipalModule;
};

public type Blobify<A> = {
to_blob : (A) -> Blob;
from_blob : (Blob) -> A;
};

// Default blobify helpers return blobs in little-endian format.
public let Nat : Blobify<Nat> = {
to_blob = func(n : Nat) : Blob {
if (n == 0) { return "\00" };
var num = n;
var nbytes = 0;

while (num > 0) {
num /= 255;
nbytes += 1;
};

num := n;

let arr = ArrayModule.tabulate(
nbytes,
func(_ : Nat) : Nat8 {
let tmp = num % 255;
num /= 255;
Nat8Module.fromNat(tmp);
},
);

Base.Blob.fromArray(arr);
};
from_blob = func(blob : Blob) : Nat {
var n = 0;
let bytes = Base.Blob.toArray(blob);

var j = bytes.size();

while (j > 0){
let byte = bytes.get(j - 1);
n *= 255;
n += Base.Nat8.toNat(byte);

j -= 1;
};

n;
};
};

public let Nat8 : Blobify<Nat8> = {
to_blob = func(n : Nat8) : Blob { Base.Blob.fromArray([n]) };
from_blob = func(blob : Blob) : Nat8 { Base.Blob.toArray(blob)[0] };
};

public let Nat16 : Blobify<Nat16> = {
to_blob = func(n : Nat16) : Blob {
Base.Blob.fromArray([
Base.Nat8.fromNat16(n & 0xff),
Base.Nat8.fromNat16(n >> 8),
])
};
from_blob = func(blob : Blob) : Nat16 {
let bytes = Base.Blob.toArray(blob);

let _n16 = Base.Nat16.fromNat8(bytes[1] << 8)
| Base.Nat16.fromNat8(bytes[0]);
};
};

public let Nat32 : Blobify<Nat32> = {
to_blob = func(n : Nat32) : Blob{
Base.Blob.fromArray([
Base.Nat8.fromNat(Base.Nat32.toNat(n & 0xff)),
Base.Nat8.fromNat(Base.Nat32.toNat((n >> 8) & 0xff)),
Base.Nat8.fromNat(Base.Nat32.toNat((n >> 16) & 0xff)),
Base.Nat8.fromNat(Base.Nat32.toNat(n >> 24)),
])
};
from_blob = func(blob : Blob) : Nat32 {
let bytes = Base.Blob.toArray(blob);

let _n32 = Base.Nat32.fromNat(Base.Nat8.toNat(bytes[3] << 24))
| Base.Nat32.fromNat(Base.Nat8.toNat(bytes[2] << 16))
| Base.Nat32.fromNat(Base.Nat8.toNat(bytes[1] << 8))
| Base.Nat32.fromNat(Base.Nat8.toNat(bytes[0]));
};
};

public let Nat64 : Blobify<Nat64> = {
to_blob = func(n : Nat64) : Blob {
Base.Blob.fromArray([
Base.Nat8.fromNat(Base.Nat64.toNat(n & 0xff)),
Base.Nat8.fromNat(Base.Nat64.toNat((n >> 8) & 0xff)),
Base.Nat8.fromNat(Base.Nat64.toNat((n >> 16) & 0xff)),
Base.Nat8.fromNat(Base.Nat64.toNat((n >> 24) & 0xff)),
Base.Nat8.fromNat(Base.Nat64.toNat((n >> 32) & 0xff)),
Base.Nat8.fromNat(Base.Nat64.toNat((n >> 40) & 0xff)),
Base.Nat8.fromNat(Base.Nat64.toNat((n >> 48) & 0xff)),
Base.Nat8.fromNat(Base.Nat64.toNat(n >> 56)),
])
};
from_blob = func(blob : Blob) : Nat64 {
let bytes = Base.Blob.toArray(blob);

let _n64 = Base.Nat64.fromNat(Base.Nat8.toNat(bytes[7] << 56))
| Base.Nat64.fromNat(Base.Nat8.toNat(bytes[6] << 48))
| Base.Nat64.fromNat(Base.Nat8.toNat(bytes[5] << 40))
| Base.Nat64.fromNat(Base.Nat8.toNat(bytes[4] << 32))
| Base.Nat64.fromNat(Base.Nat8.toNat(bytes[3] << 24))
| Base.Nat64.fromNat(Base.Nat8.toNat(bytes[2] << 16))
| Base.Nat64.fromNat(Base.Nat8.toNat(bytes[1] << 8))
| Base.Nat64.fromNat(Base.Nat8.toNat(bytes[0]));
};
};

public let Blob : Blobify<Blob> = {
to_blob = func(b : Blob) : Blob = b;
from_blob = func(blob : Blob) : Blob = blob;
};

public let Bool : Blobify<Bool> = {
to_blob = func(b : Bool) : Blob = Base.Blob.fromArray([if (b) 1 else 0]);
from_blob = func(blob : Blob) : Bool {
blob == Base.Blob.fromArray([1]);
};
};

public let Text : Blobify<Text> = {
to_blob = func(t : Text) : Blob = TextModule.encodeUtf8(t);
from_blob = func(blob : Blob) : Text {
let ?text = TextModule.decodeUtf8(blob) else Debug.trap("from_blob() on Blobify.Text failed to decodeUtf8");
text;
};
};

public let Principal : Blobify<Principal> = {
to_blob = func(p : Principal) : Blob { Base.Principal.toBlob(p) };
from_blob = func(blob : Blob) : Principal { Base.Principal.fromBlob(blob) };
};
};
Loading