Skip to content

Commit

Permalink
memory management functionality update
Browse files Browse the repository at this point in the history
  • Loading branch information
lane-neuro committed Jul 29, 2024
1 parent e8914a0 commit 7b57b40
Show file tree
Hide file tree
Showing 9 changed files with 364 additions and 429 deletions.
14 changes: 13 additions & 1 deletion research_analytics_suite/data_engine/memory/DataCache.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ def get_key(self, key) -> any:
"""
return self._cache.get(key)

def cache_values(self) -> list:
"""
Retrieves all values from the cache.
Returns:
List[any]: The values stored in the cache.
"""
_cache_values = []
for key in self._cache.keys():
_cache_values.append(self._cache[key])
return _cache_values

def set(self, key, data):
"""
Stores data in the cache.
Expand Down Expand Up @@ -127,4 +139,4 @@ async def close(self):
Cleans up resources used by DataCache.
"""
# Logic to clean up the cache, if needed.
pass
pass
43 changes: 36 additions & 7 deletions research_analytics_suite/data_engine/memory/MemoryManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@
import asyncio
import os
import uuid

import aiosqlite

from research_analytics_suite.commands import link_class_commands, command
from research_analytics_suite.data_engine.memory.MemorySlot import MemorySlot
from research_analytics_suite.data_engine.memory.DataCache import DataCache
from research_analytics_suite.utils import CustomLogger
from research_analytics_suite.utils import Config


@link_class_commands
class MemoryManager:
"""
A class to manage memory slot collections within the workspace using a specified storage backend.
Expand Down Expand Up @@ -102,6 +107,7 @@ async def initialize(self):
self._logger.debug("MemoryManager initialized.")
self._initialized = True

@command
async def create_slot(self, name: str, data: any, db_path: str = None, file_path: str = None) -> str:
"""
Creates a new memory slot and stores it in both cache and SQLite storage.
Expand All @@ -125,9 +131,10 @@ async def create_slot(self, name: str, data: any, db_path: str = None, file_path
_id = uuid.uuid4().hex[:8]
memory_slot = MemorySlot(memory_id=_id, name=name, data=data, db_path=db_path, file_path=file_path)
await memory_slot.setup()
self._data_cache.set(key=memory_slot.memory_id, data=memory_slot)
self._data_cache.set(key=memory_slot.memory_id, data=self.get_slot(memory_id=_id))
return memory_slot.memory_id

@command
async def update_slot(self, memory_id: str, data: any) -> str:
"""
Updates the data in an existing memory slot.
Expand All @@ -146,9 +153,10 @@ async def update_slot(self, memory_id: str, data: any) -> str:
memory_slot = MemorySlot(memory_id=memory_id, name="", data=data, db_path=self._db_path)
await memory_slot.setup()
await memory_slot.set_data(data)
self._data_cache.set(key=memory_id, data=memory_slot)
self._data_cache.set(key=memory_id, data=self.get_slot(memory_id=memory_id))
return memory_id

@command
async def delete_slot(self, memory_id: str) -> None:
"""
Deletes a memory slot from both cache and SQLite storage.
Expand All @@ -163,14 +171,22 @@ async def delete_slot(self, memory_id: str) -> None:
os.remove(memory_slot.file_path) # Remove memory-mapped file if it exists
self._data_cache.delete(key=memory_id)

async def list_slots(self) -> dict:
@command
async def list_slots(self) -> list:
"""
Lists all memory slots stored in the SQLite storage.
Returns:
dict: A dictionary of memory slots.
list: A dictionary of memory slots.
"""
return await self._data_cache.list_keys()
memory_slots = []
for memory_slot in self._data_cache.cache_values():
if not isinstance(memory_slot, MemorySlot):
continue
print(memory_slot)
memory_slots.append(memory_slot)

return memory_slots

async def slot_data(self, memory_id: str) -> any:
"""
Expand All @@ -190,7 +206,7 @@ async def slot_data(self, memory_id: str) -> any:
await memory_slot.setup()
data = await memory_slot.data
if data:
self._data_cache.set(key=memory_id, data=memory_slot)
self._data_cache.set(key=memory_id, data=self.get_slot(memory_id=memory_id))
return data

async def slot_name(self, memory_id: str) -> str:
Expand All @@ -211,9 +227,22 @@ async def slot_name(self, memory_id: str) -> str:
await memory_slot.setup()
name = memory_slot.name
if name:
self._data_cache.set(key=memory_id, data=memory_slot)
self._data_cache.set(key=memory_id, data=self.get_slot(memory_id=memory_id))
return name

@command
def get_slot(self, memory_id: str) -> MemorySlot:
"""
Retrieves a MemorySlot instance from the cache.
Args:
memory_id (str): The unique identifier for the memory slot.
Returns:
MemorySlot: The MemorySlot instance.
"""
return self._data_cache.get_key(key=memory_id)

async def validate_slots(self, memory_ids: list, require_values: bool = True) -> (list, list):
"""
Validates a list of memory slot identifiers and returns the valid and invalid slots.
Expand Down
Loading

0 comments on commit 7b57b40

Please sign in to comment.