55import time
66from collections import defaultdict
77from contextlib import contextmanager
8- from typing import TYPE_CHECKING , Self
8+ from typing import TYPE_CHECKING , Any , Self
99
1010from zarr .abc .store import AccessMode , ByteRangeRequest , Store
1111from zarr .core .buffer import Buffer
@@ -75,22 +75,24 @@ def _default_handler(self) -> logging.Handler:
7575 return handler
7676
7777 @contextmanager
78- def log (self ) -> Generator [None , None , None ]:
78+ def log (self , hint : Any = "" ) -> Generator [None , None , None ]:
7979 """Context manager to log method calls
8080
8181 Each call to the wrapped store is logged to the configured logger and added to
8282 the counter dict.
8383 """
8484 method = inspect .stack ()[2 ].function
8585 op = f"{ type (self ._store ).__name__ } .{ method } "
86+ if hint :
87+ op += f"({ hint } )"
8688 self .logger .info (f"Calling { op } " )
8789 start_time = time .time ()
8890 try :
8991 self .counter [method ] += 1
9092 yield
9193 finally :
9294 end_time = time .time ()
93- self .logger .info (f"Finished { op } in { end_time - start_time :.2f} seconds " )
95+ self .logger .info (f"Finished { op } [ { end_time - start_time :.2f} s] " )
9496
9597 @property
9698 def supports_writes (self ) -> bool :
@@ -118,10 +120,15 @@ def _mode(self) -> AccessMode: # type: ignore[override]
118120 return self ._store ._mode
119121
120122 @property
121- def _is_open (self ) -> bool : # type: ignore[override]
123+ def _is_open (self ) -> bool :
122124 with self .log ():
123125 return self ._store ._is_open
124126
127+ @_is_open .setter
128+ def _is_open (self , value : bool ) -> None :
129+ with self .log (value ):
130+ self ._store ._is_open = value
131+
125132 async def _open (self ) -> None :
126133 with self .log ():
127134 return await self ._store ._open ()
@@ -147,7 +154,7 @@ def __repr__(self) -> str:
147154 return f"LoggingStore({ repr (self ._store )!r} )"
148155
149156 def __eq__ (self , other : object ) -> bool :
150- with self .log ():
157+ with self .log (other ):
151158 return self ._store == other
152159
153160 async def get (
@@ -157,7 +164,7 @@ async def get(
157164 byte_range : tuple [int | None , int | None ] | None = None ,
158165 ) -> Buffer | None :
159166 # docstring inherited
160- with self .log ():
167+ with self .log (key ):
161168 return await self ._store .get (key = key , prototype = prototype , byte_range = byte_range )
162169
163170 async def get_partial_values (
@@ -166,34 +173,36 @@ async def get_partial_values(
166173 key_ranges : Iterable [tuple [str , ByteRangeRequest ]],
167174 ) -> list [Buffer | None ]:
168175 # docstring inherited
169- with self .log ():
176+ keys = "," .join ([k [0 ] for k in key_ranges ])
177+ with self .log (keys ):
170178 return await self ._store .get_partial_values (prototype = prototype , key_ranges = key_ranges )
171179
172180 async def exists (self , key : str ) -> bool :
173181 # docstring inherited
174- with self .log ():
182+ with self .log (key ):
175183 return await self ._store .exists (key )
176184
177185 async def set (self , key : str , value : Buffer ) -> None :
178186 # docstring inherited
179- with self .log ():
187+ with self .log (key ):
180188 return await self ._store .set (key = key , value = value )
181189
182190 async def set_if_not_exists (self , key : str , value : Buffer ) -> None :
183191 # docstring inherited
184- with self .log ():
192+ with self .log (key ):
185193 return await self ._store .set_if_not_exists (key = key , value = value )
186194
187195 async def delete (self , key : str ) -> None :
188196 # docstring inherited
189- with self .log ():
197+ with self .log (key ):
190198 return await self ._store .delete (key = key )
191199
192200 async def set_partial_values (
193201 self , key_start_values : Iterable [tuple [str , int , bytes | bytearray | memoryview ]]
194202 ) -> None :
195203 # docstring inherited
196- with self .log ():
204+ keys = "," .join ([k [0 ] for k in key_start_values ])
205+ with self .log (keys ):
197206 return await self ._store .set_partial_values (key_start_values = key_start_values )
198207
199208 async def list (self ) -> AsyncGenerator [str , None ]:
@@ -204,19 +213,19 @@ async def list(self) -> AsyncGenerator[str, None]:
204213
205214 async def list_prefix (self , prefix : str ) -> AsyncGenerator [str , None ]:
206215 # docstring inherited
207- with self .log ():
216+ with self .log (prefix ):
208217 async for key in self ._store .list_prefix (prefix = prefix ):
209218 yield key
210219
211220 async def list_dir (self , prefix : str ) -> AsyncGenerator [str , None ]:
212221 # docstring inherited
213- with self .log ():
222+ with self .log (prefix ):
214223 async for key in self ._store .list_dir (prefix = prefix ):
215224 yield key
216225
217226 def with_mode (self , mode : AccessModeLiteral ) -> Self :
218227 # docstring inherited
219- with self .log ():
228+ with self .log (mode ):
220229 return type (self )(
221230 self ._store .with_mode (mode ),
222231 log_level = self .log_level ,
0 commit comments