Skip to content

Commit f389f09

Browse files
author
Zhe Yu
committed
feat(db): Implement orphan removal functionality in the database connector
1 parent 4d32898 commit f389f09

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

src/vectorcode/database/base.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import asyncio
22
import logging
3+
import os
34
from abc import ABC, abstractmethod
45
from typing import Optional, Self, Sequence
56

@@ -155,3 +156,21 @@ def replace_config(self, new_config: Config) -> Self:
155156
)
156157
self._configs = new_config
157158
return self
159+
160+
async def check_orphanes(self) -> int:
161+
"""
162+
Check for files that are in the database, but no longer on the disk, and remove them.
163+
"""
164+
165+
orphanes: list[str] = []
166+
database_files = (await self.list(ResultType.document)).files
167+
for file in database_files:
168+
path = file.path
169+
if not os.path.isfile(path):
170+
orphanes.append(path)
171+
logger.debug(f"Discovered orphaned file: {path}")
172+
173+
self.update_config(Config(rm_paths=orphanes))
174+
await self.delete()
175+
176+
return len(orphanes)

0 commit comments

Comments
 (0)