Skip to content

Commit bd693d2

Browse files
committed
Speed up startup by improving optimizing database opening
Do not open all shards for a SymbolIndex if we want to just get the binary dir of it. * bdx/cli.py (guess_index_path_for_directory): Do not open the SymbolIndex, instead call the new static `get_binary_dir' function to try to get the binary directory for the index at given path. * bdx/index.py (DB_SUBDIR): New helper var. (SymbolIndex.open): Use it. (SymbolIndex.open_shard): Use it and remove the `readonly' argument as shards should only be used for writing. (SymbolIndex.get_binary_dir): New staticmethod.
1 parent d7a46ef commit bd693d2

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

bdx/cli.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,12 @@ def guess_index_path_for_directory(directory: str | Path) -> Path:
100100

101101
for index_path in cache_dir.iterdir():
102102
try:
103-
with SymbolIndex.open(index_path, readonly=True) as index:
104-
binary_dir = index.binary_dir()
105-
if binary_dir is None:
106-
continue
107-
if binary_dir == directory:
108-
return index_path
109-
if binary_dir in directory.parents:
110-
return index_path
111-
except SymbolIndex.Error:
103+
binary_dir = SymbolIndex.get_binary_dir(index_path)
104+
if binary_dir == directory:
105+
return index_path
106+
if binary_dir in directory.parents:
107+
return index_path
108+
except Exception:
112109
pass
113110

114111
return SymbolIndex.default_path(directory)

bdx/index.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,9 @@ def index_document(self, document: xapian.Document, **data: str):
507507
handler.index(document, val)
508508

509509

510+
DB_SUBDIR = "db"
511+
512+
510513
class SymbolIndex:
511514
"""Easy interface for a xapian database, with schema support."""
512515

@@ -644,7 +647,9 @@ def open(directory: Path | str, readonly: bool = False) -> "SymbolIndex":
644647
645648
"""
646649
index = SymbolIndex(
647-
Path(directory) / "db", readonly=readonly, is_shard=False
650+
Path(directory) / DB_SUBDIR,
651+
readonly=readonly,
652+
is_shard=False,
648653
)
649654

650655
debug("Opened index: {}", index.path)
@@ -655,13 +660,13 @@ def open(directory: Path | str, readonly: bool = False) -> "SymbolIndex":
655660
return index
656661

657662
@staticmethod
658-
def open_shard(
659-
directory: Path | str, readonly: bool = False
660-
) -> "SymbolIndex":
663+
def open_shard(directory: Path | str) -> "SymbolIndex":
661664
"""Open a writable shard for index in given directory."""
662-
for path in SymbolIndex.generate_shard_paths(Path(directory) / "db"):
665+
for path in SymbolIndex.generate_shard_paths(
666+
Path(directory) / DB_SUBDIR
667+
):
663668
try:
664-
return SymbolIndex(path, readonly=readonly, is_shard=True)
669+
return SymbolIndex(path, readonly=False, is_shard=True)
665670
except Exception:
666671
pass
667672

@@ -681,6 +686,16 @@ def generate_shard_paths(directory: Path | str) -> Iterator[Path]:
681686
yield directory.parent / f"{directory.name}.{i:0>3}"
682687
i += 1
683688

689+
@staticmethod
690+
def get_binary_dir(index_path: Path | str) -> Optional[Path]:
691+
"""Try to get the saved binary directory for given index path."""
692+
with SymbolIndex(
693+
Path(index_path) / DB_SUBDIR,
694+
readonly=True,
695+
is_shard=True,
696+
) as index:
697+
return index.binary_dir()
698+
684699
def shards(self) -> Iterator[Path]:
685700
"""Yield the shards of this database."""
686701
for x in self.generate_shard_paths(self.path):

0 commit comments

Comments
 (0)