Skip to content

Commit

Permalink
api: names are locally caches
Browse files Browse the repository at this point in the history
It improve speed because it avoid doing requests to the DB
  • Loading branch information
ebrocas committed Dec 12, 2023
1 parent cf55db3 commit 50b5b11
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/numbat/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def __init__(self, database: sqlite3.Connection, path: Path, logger: logging.Log
self.logger = logging.getLogger()
else:
self.logger = logger
self.name_cache = dict()

# ------------------------------------------------------------------------ #
# Database file management functions #
Expand All @@ -80,6 +81,19 @@ def __uniformize_path(cls, path: Path | str) -> Path:
path = path.with_suffix(cls.SOURCETRAIL_DB_EXT)
return path.absolute()

@classmethod
def exists(cls, path: Path | str) -> bool:
"""
This method check if there is a Sourcetrail db with the given path.
If the provided path does not end with the sourcetrail db correct
suffix. It will be added.
:param path: The path to test
:return: a bool
"""
path = cls.__uniformize_path(path)
return path.exists()

@classmethod
def open(cls, path: Path | str, clear: bool = False) -> 'SourcetrailDB':
"""
Expand Down Expand Up @@ -229,8 +243,7 @@ def __add_if_not_existing(self, name: str, type_: NodeType) -> int:
the existing one
"""

node = NodeDAO.get_by_name(self.database, name)
if not node:
if name not in self.name_cache:
elem = Element()
elem.id = ElementDAO.new(self.database, elem)

Expand All @@ -240,9 +253,10 @@ def __add_if_not_existing(self, name: str, type_: NodeType) -> int:
name
))

self.name_cache[name] = elem.id
return elem.id
else:
return node.id
return self.name_cache[name]

def _record_symbol(self, hierarchy: NameHierarchy) -> int:
"""
Expand Down

0 comments on commit 50b5b11

Please sign in to comment.