Skip to content

Commit

Permalink
finish to implementing, python kvruntime
Browse files Browse the repository at this point in the history
  • Loading branch information
j03-dev committed Aug 1, 2024
1 parent 7caee01 commit 4ba2191
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
8 changes: 4 additions & 4 deletions typegate/src/runtimes/kv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ export class KvRuntime extends Runtime {
}

if (name == "kv_keys") {
return async (filter: string) => {
return await this.redis.keys(filter);
return async (filter: string | null) => {
return await this.redis.keys(filter ?? "*");
};
}

if (name == "kv_all") {
return async (filter: string) => {
return await this.redis.hgetall(filter);
return async (filter: string | null) => {
return await this.redis.hgetall(filter ?? "*");
};
}
};
Expand Down
1 change: 1 addition & 0 deletions typegraph/python/typegraph/runtimes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
from typegraph.runtimes.python import PythonRuntime # noqa
from typegraph.runtimes.random import RandomRuntime # noqa
from typegraph.runtimes.wasm import WasmRuntime # noqa
from typegraph.runtimes.kv import KvRuntime # noqa
57 changes: 54 additions & 3 deletions typegraph/python/typegraph/runtimes/kv.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@
# SPDX-License-Identifier: MPL-2.0

from typing import Optional
from dataclasses import dataclass

from typegraph.gen.exports.runtimes import KvRuntimeData
from typegraph.runtimes.base import Runtime
from typegraph.gen.types import Err
from typegraph.gen.exports.runtimes import (
BaseMaterializer,
Effect,
KvRuntimeData,
KvMaterializer,
)
from typegraph import fx, t
from typegraph.runtimes.base import Materializer, Runtime
from typegraph.wit import runtimes, store


Expand All @@ -14,7 +22,7 @@ class KvRuntime(Runtime):
db_number: Optional[int]
password: Optional[str]

def __ini__(
def __init__(
self,
host: str,
port: Optional[str],
Expand All @@ -25,8 +33,51 @@ def __ini__(
host=host, port=port, db_number=db_number, password=password
)
runtime_id = runtimes.register_kv_runtime(store, data)
if isinstance(runtime_id, Err):
raise Exception(runtime_id.value)

super().__init__(runtime_id.value)
self.host = host
self.port = port
self.db_number = db_number
self.password = password

def get(self):
mat = self.__operation(KvMaterializer.GET, fx.read())
return t.func(t.struct({"key": t.string()}), t.string(), mat)

def set(self):
mat = self.__operation(KvMaterializer.SET, fx.write())
return t.func(
t.struct({"key": t.string(), "value": t.string()}), t.string(), mat
)

def delete(self, key: str):
mat = self.__operation(KvMaterializer.DELETE, fx.write())
return t.func(t.struct({"key": t.string()}), t.string(), mat)

def keys(self):
mat = self.__operation(KvMaterializer.KEYS, fx.read())
return t.func(t.struct({"filter": Optional[t.string()]}), list[t.string()], mat)

def all(self):
mat = self.__operation(KvMaterializer.ALL, fx.read())
return t.func(
t.struct({"filter": Optional[t.string()]}),
list[tuple[t.string(), t.string()]],
mat,
)

def __operation(self, operation: KvMaterializer, effect: Effect):
mat_id = runtimes.kv_operation(
store, BaseMaterializer(self.id, effect), operation
)
if isinstance(mat_id, Err):
raise Exception(mat_id.value)

return KvOperationMat(mat_id.value, effect=effect, operation=operation)


@dataclass
class KvOperationMat(Materializer):
operation: KvMaterializer

0 comments on commit 4ba2191

Please sign in to comment.