Skip to content

Commit da172ef

Browse files
committed
Preserve reader_protocol only
1 parent 0fff176 commit da172ef

5 files changed

Lines changed: 37 additions & 68 deletions

File tree

libCacheSim-python/libcachesim/__init__.pyi

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ from typing import bool, int, str, tuple
33
from collections.abc import Iterator
44

55
from .libcachesim_python import ReqOp, TraceType, SamplerType
6-
from .protocols import ReaderProtocol, CacheProtocol
6+
from .protocols import ReaderProtocol
77

88
class Request:
99
clock_time: int
@@ -59,8 +59,8 @@ class Cache:
5959
def get_n_obj(self) -> int: ...
6060
def print_cache(self) -> str: ...
6161

62-
class CacheBase(CacheProtocol):
63-
"""Base class implementing CacheProtocol"""
62+
class CacheBase:
63+
"""Base class for all cache implementations"""
6464
def __init__(self, _cache: Cache): ...
6565
def get(self, req: Request) -> bool: ...
6666
def find(self, req: Request, update_cache: bool = True) -> CacheObject: ...
@@ -219,6 +219,7 @@ def create_zipf_requests(
219219
start_obj_id: int = 0,
220220
seed: int | None = None,
221221
) -> Iterator[Request]: ...
222+
222223
def create_uniform_requests(
223224
num_objects: int,
224225
num_requests: int,
@@ -230,8 +231,9 @@ def create_uniform_requests(
230231

231232
# Analyzer
232233
class TraceAnalyzer:
233-
def __init__(self, analyzer): ...
234-
def analyze(self, reader: ReaderProtocol, output_path: str, analysis_param, analysis_option) -> None: ...
234+
def __init__(self, analyzer, reader: ReaderProtocol, output_path: str, analysis_param, analysis_option): ...
235+
def run(self) -> None: ...
236+
def cleanup(self) -> None: ...
235237

236238
# Utilities
237239
class Util:

libCacheSim-python/libcachesim/cache.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@
4040
c_process_trace,
4141
)
4242

43-
from .protocols import CacheProtocol, ReaderProtocol
43+
from .protocols import ReaderProtocol
4444

4545

46-
class CacheBase(CacheProtocol):
47-
"""Base class for all cache implementations that implements CacheProtocol"""
46+
class CacheBase(ABC):
47+
"""Base class for all cache implementations"""
4848

4949
_cache: Cache # Internal C++ cache object
5050

Lines changed: 16 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,34 @@
1-
from __future__ import annotations
2-
3-
from typing import Protocol, TYPE_CHECKING
4-
5-
if TYPE_CHECKING:
6-
from .libcachesim_python import Request, CacheObject, Reader, Analyzer
7-
8-
9-
class CacheProtocol(Protocol):
10-
def get(self, req: Request) -> bool: ...
11-
12-
def find(self, req: Request, update_cache: bool = True) -> CacheObject: ...
13-
14-
def can_insert(self, req: Request) -> bool: ...
1+
"""
2+
Reader protocol for libCacheSim Python bindings.
153
16-
def insert(self, req: Request) -> CacheObject: ...
4+
ReaderProtocol defines the interface contract for trace readers,
5+
enabling different implementations (Python/C++) to work interchangeably.
6+
"""
177

18-
def need_eviction(self, req: Request) -> bool: ...
19-
20-
def evict(self, req: Request) -> CacheObject: ...
21-
22-
def remove(self, obj_id: int) -> bool: ...
23-
24-
def to_evict(self, req: Request) -> CacheObject: ...
25-
26-
def get_occupied_byte(self) -> int: ...
27-
28-
def get_n_obj(self) -> int: ...
29-
30-
def print_cache(self) -> str: ...
8+
from __future__ import annotations
9+
from typing import Protocol, runtime_checkable, TYPE_CHECKING
3110

32-
def process_trace(self, reader: "ReaderProtocol", start_req: int = 0, max_req: int = -1) -> tuple[float, float]: ...
11+
if TYPE_CHECKING:
12+
from .libcachesim_python import Request
3313

34-
# Properties
35-
@property
36-
def cache_size(self) -> int: ...
3714

38-
@property
39-
def cache_name(self) -> str: ...
15+
@runtime_checkable
16+
class ReaderProtocol(Protocol):
17+
"""Protocol for trace readers
4018
19+
This protocol ensures that different reader implementations
20+
(SyntheticReader, TraceReader) can be used interchangeably.
21+
"""
4122

42-
class ReaderProtocol(Protocol):
4323
def get_num_of_req(self) -> int: ...
44-
4524
def read_one_req(self, req: Request) -> Request: ...
46-
4725
def reset(self) -> None: ...
48-
4926
def close(self) -> None: ...
50-
51-
def clone(self) -> ReaderProtocol: ...
52-
27+
def clone(self) -> "ReaderProtocol": ...
5328
def read_first_req(self, req: Request) -> Request: ...
54-
5529
def read_last_req(self, req: Request) -> Request: ...
56-
5730
def skip_n_req(self, n: int) -> int: ...
58-
5931
def read_one_req_above(self, req: Request) -> Request: ...
60-
6132
def go_back_one_req(self) -> None: ...
62-
6333
def set_read_pos(self, pos: float) -> None: ...
64-
6534
def get_read_pos(self) -> float: ...
66-
67-
68-
class AnalyzerProtocol(Protocol):
69-
def run(self) -> None: ...
70-
71-
def cleanup(self) -> None: ...

libCacheSim-python/libcachesim/trace_analyzer.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
"""Wrapper of Analyzer"""
2+
from __future__ import annotations
23

3-
from .protocols import ReaderProtocol, AnalyzerProtocol
4+
from typing import TYPE_CHECKING
5+
6+
if TYPE_CHECKING:
7+
from .protocols import ReaderProtocol
48

59
from .libcachesim_python import (
610
Analyzer,
@@ -9,7 +13,7 @@
913
)
1014

1115

12-
class TraceAnalyzer(AnalyzerProtocol):
16+
class TraceAnalyzer:
1317
_analyzer: Analyzer
1418

1519
def __init__(

libCacheSim-python/libcachesim/util.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
"""Wrapper misc functions"""
2+
from __future__ import annotations
23

34
from typing import TYPE_CHECKING
45

56
if TYPE_CHECKING:
6-
from .protocols import CacheProtocol, ReaderProtocol
7+
from .protocols import ReaderProtocol
8+
from .cache import CacheBase
79

810
from .libcachesim_python import convert_to_oracleGeneral, convert_to_lcs, c_process_trace
911

@@ -28,9 +30,7 @@ def convert_to_lcs(reader, ofilepath, output_txt=False, remove_size_change=False
2830
return convert_to_lcs(reader, ofilepath, output_txt, remove_size_change, lcs_ver)
2931

3032
@staticmethod
31-
def process_trace(
32-
cache: "CacheProtocol", reader: "ReaderProtocol", start_req: int = 0, max_req: int = -1
33-
) -> tuple[float, float]:
33+
def process_trace(cache: CacheBase, reader: ReaderProtocol, start_req: int = 0, max_req: int = -1) -> tuple[float, float]:
3434
"""
3535
Process a trace with a cache.
3636
@@ -44,7 +44,7 @@ def process_trace(
4444
tuple[float, float]: The object miss ratio and byte miss ratio.
4545
"""
4646
# Check if reader is C++ reader
47-
if not hasattr(reader, "c_reader") or not reader.c_reader:
47+
if not hasattr(reader, 'c_reader') or not reader.c_reader:
4848
raise ValueError("Reader must be a C++ reader")
4949

5050
return c_process_trace(cache._cache, reader._reader, start_req, max_req)

0 commit comments

Comments
 (0)