MCPcopy Create free account
hub / github.com/PyTables/PyTables / NodeManager

Class NodeManager

tables/file.py:369–601  ·  view source on GitHub ↗

Node manager.

Source from the content-addressed store, hash-verified

367
368
369class NodeManager:
370 """Node manager."""
371
372 def __init__(self, nslots: int = 64, node_factory=None) -> None:
373 super().__init__()
374
375 self.registry = weakref.WeakValueDictionary()
376
377 if nslots > 0:
378 cache = lrucacheextension.NodeCache(nslots)
379 elif nslots == 0:
380 cache = _NoCache()
381 else:
382 # nslots < 0
383 cache = _DictCache(-nslots)
384
385 self.cache = cache
386
387 # node_factory(node_path)
388 self.node_factory = node_factory
389
390 def register_node(self, node: Node, key: str | None) -> None:
391 """Register a node."""
392 if key is None:
393 key = node._v_pathname
394
395 if key in self.registry:
396 if not self.registry[key]._v_isopen:
397 del self.registry[key]
398 self.registry[key] = node
399 elif self.registry[key] is not node:
400 raise RuntimeError(
401 "trying to register a node with an "
402 "existing key: ``%s``" % key
403 )
404 else:
405 self.registry[key] = node
406
407 def cache_node(self, node: Node, key: str | None = None) -> None:
408 """Create a node."""
409 if key is None:
410 key = node._v_pathname
411
412 self.register_node(node, key)
413 if key in self.cache:
414 oldnode = self.cache.pop(key)
415 if oldnode is not node and oldnode._v_isopen:
416 raise RuntimeError(
417 "trying to cache a node with an "
418 "existing key: ``%s``" % key
419 )
420
421 self.cache[key] = node
422
423 def get_node(self, key: str) -> Node:
424 """Return a node matching the input key."""
425 node = self.cache.pop(key, None)
426 if node is not None:

Callers 1

__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected