Skip to content

Commit

Permalink
temporary
Browse files Browse the repository at this point in the history
  • Loading branch information
amadolid committed Sep 11, 2024
1 parent 6d275a2 commit 1f4c4c2
Showing 1 changed file with 40 additions and 43 deletions.
83 changes: 40 additions & 43 deletions jac/jaclang/runtimelib/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@
from pickle import dumps
from shelve import Shelf, open
from typing import Callable, Generator, Generic, Iterable, TypeVar
from uuid import UUID

from .implementation import EdgeAnchor, JID, NodeAnchor, WalkerAnchor, _ANCHOR
from .architype import Anchor, NodeAnchor, Root, TANCH

ID = TypeVar("ID")


@dataclass
class Memory(Generic[ID, _ANCHOR]):
class Memory(Generic[ID, TANCH]):
"""Generic Memory Handler."""

__mem__: dict[ID, _ANCHOR] = field(default_factory=dict)
__gc__: set[_ANCHOR] = field(default_factory=set)
__mem__: dict[ID, TANCH] = field(default_factory=dict)
__gc__: set[TANCH] = field(default_factory=set)

def close(self) -> None:
"""Close memory handler."""
Expand All @@ -27,8 +28,8 @@ def close(self) -> None:
def find(
self,
ids: ID | Iterable[ID],
filter: Callable[[_ANCHOR], _ANCHOR] | None = None,
) -> Generator[_ANCHOR, None, None]:
filter: Callable[[TANCH], TANCH] | None = None,
) -> Generator[TANCH, None, None]:
"""Find anchors from memory by ids with filter."""
if not isinstance(ids, Iterable):
ids = [ids]
Expand All @@ -42,16 +43,16 @@ def find(
def find_one(
self,
ids: ID | Iterable[ID],
filter: Callable[[_ANCHOR], _ANCHOR] | None = None,
) -> _ANCHOR | None:
filter: Callable[[TANCH], TANCH] | None = None,
) -> TANCH | None:
"""Find one anchor from memory by ids with filter."""
return next(self.find(ids, filter), None)

def find_by_id(self, id: ID) -> _ANCHOR | None:
def find_by_id(self, id: ID) -> TANCH | None:
"""Find one by id."""
return self.__mem__.get(id)

def set(self, id: ID, data: _ANCHOR) -> None:
def set(self, id: ID, data: TANCH) -> None:
"""Save anchor to memory."""
self.__mem__[id] = data

Expand All @@ -66,10 +67,10 @@ def remove(self, ids: ID | Iterable[ID]) -> None:


@dataclass
class ShelfStorage(Memory[JID, EdgeAnchor | NodeAnchor | WalkerAnchor]):
class ShelfStorage(Memory[UUID, Anchor]):
"""Shelf Handler."""

__shelf__: Shelf[EdgeAnchor | NodeAnchor | WalkerAnchor] | None = None
__shelf__: Shelf[Anchor] | None = None

def __init__(self, session: str | None = None) -> None:
"""Initialize memory handler."""
Expand All @@ -84,48 +85,44 @@ def close(self) -> None:
root = Jac.get_root().__jac__

for anchor in self.__gc__:
self.__shelf__.pop(str(anchor.jid), None)
self.__mem__.pop(anchor.jid, None)
self.__shelf__.pop(str(anchor.id), None)
self.__mem__.pop(anchor.id, None)

for anchor in self.__mem__.values():
if anchor.persistent and anchor.hash != hash(dumps(anchor)):
jid = str(anchor.jid)
if source_anchor := self.__shelf__.get(jid):
for d in self.__mem__.values():
if d.persistent and d.hash != hash(dumps(d)):
_id = str(d.id)
if p_d := self.__shelf__.get(_id):
if (
isinstance(source_anchor, NodeAnchor)
and isinstance(anchor, NodeAnchor)
and source_anchor.edge_ids != anchor.edge_ids
and root.has_connect_access(anchor)
isinstance(p_d, NodeAnchor)
and isinstance(d, NodeAnchor)
and p_d.edges != d.edges
and root.has_connect_access(d)
):
if not anchor.edges:
self.__shelf__.pop(jid, None)
if not d.edges:
self.__shelf__.pop(_id, None)
continue
source_anchor.edges = anchor.edges

if root.has_write_access(anchor):
if hash(dumps(source_anchor.access)) != hash(
dumps(anchor.access)
):
source_anchor.access = anchor.access
if hash(dumps(anchor.architype)) != hash(
dumps(anchor.architype)
):
source_anchor.architype = anchor.architype

self.__shelf__[jid] = source_anchor
p_d.edges = d.edges

if root.has_write_access(d):
if hash(dumps(p_d.access)) != hash(dumps(d.access)):
p_d.access = d.access
if hash(dumps(d.architype)) != hash(dumps(d.architype)):
p_d.architype = d.architype

self.__shelf__[_id] = p_d
elif not (
isinstance(anchor, NodeAnchor)
and not isinstance(anchor.architype, Root)
and not anchor.edges
isinstance(d, NodeAnchor)
and not isinstance(d.architype, Root)
and not d.edges
):
self.__shelf__[jid] = anchor
self.__shelf__[_id] = d

self.__shelf__.close()
super().close()

def find(
self,
ids: JID | Iterable[JID],
ids: UUID | Iterable[UUID],
filter: Callable[[Anchor], Anchor] | None = None,
) -> Generator[Anchor, None, None]:
"""Find anchors from datasource by ids with filter."""
Expand All @@ -147,7 +144,7 @@ def find(
else:
yield from super().find(ids, filter)

def find_by_id(self, id: JID) -> Anchor | None:
def find_by_id(self, id: UUID) -> Anchor | None:
"""Find one by id."""
data = super().find_by_id(id)

Expand Down

0 comments on commit 1f4c4c2

Please sign in to comment.