Skip to content

Commit

Permalink
Add transactions, optimizations, bench. Fix node/bun ci.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexagon committed May 7, 2024
1 parent 6bcc13c commit c218906
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 194 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ jobs:
bun_ci:
uses: cross-org/workflows/.github/workflows/bun-ci.yml@main
with:
jsr_dependencies: "@cross/test @cross/fs @std/assert @std/path cbor-x"
jsr_dependencies: "@cross/test @cross/fs @cross/runtime @std/assert @std/path"
npm_dependencies: "cbor-x"
node_ci:
uses: cross-org/workflows/.github/workflows/node-ci.yml@main
with:
jsr_dependencies: "@cross/test @cross/fs @std/assert @std/path cbor-x"
jsr_dependencies: "@cross/test @cross/fs @cross/runtime @std/assert @std/path"
npm_dependencies: "cbor-x"
test_target: "src/*.test.ts"
116 changes: 4 additions & 112 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,127 +16,19 @@ pids
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
# Node / NPM
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional stylelint cache
.stylelintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# vuepress v2.x temp and cache directory
.temp
.cache

# Docusaurus cache and generated files
.docusaurus

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# Deno / JSR
deno.lock

# NPM
.npmrc
package-lock.json
package.json

# Deno / JSR
deno.lock

# Bun
bunfig.toml

Expand Down
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
**cross/kv**

A cross-platform, hierarchical Key/Value database for JavaScript and TypeScript,
that work in all major runtimes (Node.js, Deno and Bun).
designed to work in all major runtimes (Node.js, Deno, and Bun).

### **Features**

Expand All @@ -21,13 +21,14 @@ Full installation instructions available at <https://jsr.io/@cross/kv>

```bash
# Using npm
npx jsr i @cross/kv
npx jsr add @cross/kv

# Using Deno
deno add @cross/kv
```

Replace `<version>` with the desired version of the package.
# Using bun
bunx jsr add @cross/kv
```

### **Simple Usage**

Expand All @@ -42,7 +43,7 @@ await kvStore.set(["data", "username"], "Alice");

// Get a value
const username = await kvStore.get(["data", "username"]);
console.log(username); // Output: 'Alice'
console.log(username); // Output: { ts: <numeric timestamp>, data "Alice" }

// Delete a key
await kvStore.delete(["data", "username"]);
Expand Down Expand Up @@ -110,11 +111,12 @@ await kvStore.close();

- `CrossKV` class
- `open(filepath)`
- `set(key, value)`
- `set(key, value, overwrite?)`
- `get(key)`
- `getMany(key)`
- `delete(key)`
- `sync()`
- `beginTransaction()`
- `endTransaction()`
- `close()`
- `KVKey` class (Detail the constructor and methods)
- `KVKeyRange` interface
Expand Down
4 changes: 2 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cross/kv",
"version": "0.0.4",
"version": "0.0.5",
"exports": {
".": "./mod.ts"
},
Expand All @@ -13,7 +13,7 @@
"cbor-x": "npm:cbor-x@^1.5.9"
},
"publish": {
"exclude": [".github", "*.test.ts", "*.bench.ts"]
"exclude": [".github", "**/*.test.ts", "**/*.bench.ts"]
},
"tasks": {
"check": "deno fmt --check && deno lint && deno check mod.ts && deno doc --lint mod.ts && deno run -A mod.ts --slim --ignore-unused",
Expand Down
1 change: 0 additions & 1 deletion mod.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from "./src/kv.ts";
export * from "./src/key.ts";
export * from "./src/index.ts";
21 changes: 21 additions & 0 deletions src/cbor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { addExtension, Decoder, Encoder } from "cbor-x";
import { KVKey } from "./key.ts";

addExtension({
Class: KVKey,
tag: 43331, // register our own extension code (a tag code)
//@ts-ignore external
encode(instance, encode) {
// define how your custom class should be encoded
// @ts-ignore external
encode(instance.get()); // return a buffer
},
//@ts-ignore external
decode(data) {
// @ts-ignore external
return new KVKey(data as (string | number)[]); // decoded value from buffer
},
});

export const extEncoder = new Encoder();
export const extDecoder = new Decoder();
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ export class KVIndex {
if (childNode) {
recurse(childNode, keyIndex + 1);
}
} else if (typeof keyPart === "object" && keyPart.from && keyPart.to) {
} else if (
typeof keyPart === "object" &&
(keyPart.from !== undefined || keyPart.to !== undefined)
) {
// Key range
const range = keyPart as KVKeyRange;
for (const [index, childNode] of node.children.entries()) {
Expand Down
4 changes: 2 additions & 2 deletions src/key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ export class KVKey {
}

if (typeof element === "object") {
if (!element.from || !element.to) {
if (!(element.from || element.to)) {
throw new TypeError(
'Ranges must have both "from" and "to" properties',
'Ranges must have one or both of "from" and "to"',
);
}
}
Expand Down
14 changes: 10 additions & 4 deletions src/kv.bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,25 @@ let crossIter = 0;
let denoIter = 0;

await Deno.bench("cross_kv_set", async () => {
await crossStore.set(["testKey", crossIter++], { data: "testData" });
await crossStore.set(["testKey", crossIter++], {
data: "testData",
ts: new Date(),
});
});

await Deno.bench("deno_kv_set", async () => {
await denoStore.set(["testKey", denoIter++], { data: "testData" });
await denoStore.set(["testKey", denoIter++], {
data: "testData",
ts: new Date(),
});
});

await Deno.bench("cross_kv_get", async () => {
await crossStore.get(["testKey", crossIter--]);
await crossStore.get(["testKey", 3]);
});

await Deno.bench("deno_kv_get", async () => {
await denoStore.get(["testKey", crossIter--]);
await denoStore.get(["testKey", 3]);
});

//await cleanup();
Loading

0 comments on commit c218906

Please sign in to comment.