Skip to content

Commit

Permalink
fix circular imports
Browse files Browse the repository at this point in the history
  • Loading branch information
ekorman committed Jul 23, 2024
1 parent 002e3b8 commit 9db3b88
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 30 deletions.
3 changes: 2 additions & 1 deletion affine/engine/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from .base import Engine
from .local import LocalEngine

try:
from .qdrant import QdrantEngine
except ModuleNotFoundError:
pass

__all__ = ["LocalEngine", "QdrantEngine"]
__all__ = ["Engine", "LocalEngine", "QdrantEngine"]
31 changes: 31 additions & 0 deletions affine/engine/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from abc import ABC, abstractmethod

from affine.collection import Collection, FilterSet


class Engine(ABC):
@abstractmethod
def query(self, filter_set: FilterSet) -> list[Collection]:
pass

@abstractmethod
def insert(self, record: Collection) -> int:
pass

@abstractmethod
def delete(self, collection: type, id_: int) -> None:
pass

@abstractmethod
def get_elements_by_ids(
self, collection: type, ids: list[int]
) -> list[Collection]:
pass

def get_element_by_id(self, collection: type, id_: int) -> Collection:
ret = self.get_elements_by_ids(collection, [id_])
if len(ret) == 0:
raise ValueError(f"No record found with id {id_}")
if len(ret) > 1:
raise ValueError(f"Multiple records found with id {id_}")
return ret[0]
30 changes: 1 addition & 29 deletions affine/engine/local.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import pickle
from abc import ABC, abstractmethod
from collections import defaultdict
from pathlib import Path
from typing import BinaryIO

import numpy as np

from affine.collection import Collection, Filter, FilterSet
from affine.engine import Engine


def apply_filter_to_record(filter_: Filter, record: Collection) -> bool:
Expand Down Expand Up @@ -72,34 +72,6 @@ def apply_filters_to_records(
return records


class Engine(ABC):
@abstractmethod
def query(self, filter_set: FilterSet) -> list[Collection]:
pass

@abstractmethod
def insert(self, record: Collection) -> int:
pass

@abstractmethod
def delete(self, collection: type, id_: int) -> None:
pass

@abstractmethod
def get_elements_by_ids(
self, collection: type, ids: list[int]
) -> list[Collection]:
pass

def get_element_by_id(self, collection: type, id_: int) -> Collection:
ret = self.get_elements_by_ids(collection, [id_])
if len(ret) == 0:
raise ValueError(f"No record found with id {id_}")
if len(ret) > 1:
raise ValueError(f"Multiple records found with id {id_}")
return ret[0]


class LocalEngine(Engine):
def __init__(self) -> None: # maybe add option to the init for ANN algo
self.records: dict[str, list[Collection]] = defaultdict(list)
Expand Down

0 comments on commit 9db3b88

Please sign in to comment.