MCPcopy Create free account
hub / github.com/Open-Bee/DataStudio / _get_env

Method _get_env

datastudio/utils/database.py:195–234  ·  view source on GitHub ↗

Lazy-load shard environment with LRU eviction (thread-safe). Args: shard_id: Shard ID to load. Returns: lmdb.Environment for the shard.

(self, shard_id: int)

Source from the content-addressed store, hash-verified

193 return get_shard_id(image_path, self.num_shards)
194
195 def _get_env(self, shard_id: int) -> lmdb.Environment:
196 """Lazy-load shard environment with LRU eviction (thread-safe).
197
198 Args:
199 shard_id: Shard ID to load.
200
201 Returns:
202 lmdb.Environment for the shard.
203 """
204 with self._lock:
205 if shard_id in self._envs:
206 # Move to end to mark as recently used
207 self._envs.move_to_end(shard_id)
208 return self._envs[shard_id]
209
210 # Evict oldest shard if at capacity
211 while len(self._envs) >= self.max_open_shards:
212 oldest_shard_id, oldest_env = self._envs.popitem(last=False)
213 try:
214 oldest_env.close()
215 except Exception:
216 pass
217
218 shard_path = self._get_shard_path(shard_id)
219
220 # Check existing file size, dynamically adjust map_size
221 data_file = os.path.join(shard_path, "data.mdb")
222 actual_map_size = self.map_size_per_shard
223 if os.path.exists(data_file):
224 actual_size = os.path.getsize(data_file)
225 if actual_size > self.map_size_per_shard:
226 actual_map_size = actual_size * 2
227
228 self._envs[shard_id] = init_sharded_lmdb(
229 shard_path,
230 map_size=actual_map_size,
231 readonly=self.readonly,
232 lock=self.lock,
233 )
234 return self._envs[shard_id]
235
236 def _get_key(self, image_path: str) -> bytes:
237 """Get LMDB key for an image path."""

Callers 5

existsMethod · 0.95
putMethod · 0.95
getMethod · 0.95
_check_shardMethod · 0.95

Calls 4

_get_shard_pathMethod · 0.95
init_sharded_lmdbFunction · 0.85
closeMethod · 0.80
existsMethod · 0.80

Tested by

no test coverage detected