|
23 | 23 | expand_globs, |
24 | 24 | expand_path, |
25 | 25 | ) |
26 | | -from vectorcode.common import get_client, get_collection, verify_ef |
| 26 | +from vectorcode.common import ( |
| 27 | + get_client, |
| 28 | + get_collection, |
| 29 | + list_collection_files, |
| 30 | + verify_ef, |
| 31 | +) |
27 | 32 |
|
28 | 33 | logger = logging.getLogger(name=__name__) |
29 | 34 |
|
@@ -155,6 +160,25 @@ async def chunked_add( |
155 | 160 | stats.add += 1 |
156 | 161 |
|
157 | 162 |
|
| 163 | +async def remove_orphanes( |
| 164 | + collection: AsyncCollection, |
| 165 | + collection_lock: Lock, |
| 166 | + stats: VectoriseStats, |
| 167 | + stats_lock: Lock, |
| 168 | +): |
| 169 | + async with collection_lock: |
| 170 | + paths = await list_collection_files(collection) |
| 171 | + orphans = set() |
| 172 | + for path in paths: |
| 173 | + if isinstance(path, str) and not os.path.isfile(path): |
| 174 | + orphans.add(path) |
| 175 | + async with stats_lock: |
| 176 | + stats.removed = len(orphans) |
| 177 | + if len(orphans): |
| 178 | + logger.info(f"Removing {len(orphans)} orphaned files from database.") |
| 179 | + await collection.delete(where={"path": {"$in": list(orphans)}}) |
| 180 | + |
| 181 | + |
158 | 182 | def show_stats(configs: Config, stats: VectoriseStats): |
159 | 183 | if configs.pipe: |
160 | 184 | print(stats.to_json()) |
@@ -284,19 +308,7 @@ async def vectorise(configs: Config) -> int: |
284 | 308 | print("Abort.", file=sys.stderr) |
285 | 309 | return 1 |
286 | 310 |
|
287 | | - async with collection_lock: |
288 | | - all_results = await collection.get(include=[IncludeEnum.metadatas]) |
289 | | - if all_results is not None and all_results.get("metadatas"): |
290 | | - paths = (meta["path"] for meta in (all_results["metadatas"] or [])) |
291 | | - orphans = set() |
292 | | - for path in paths: |
293 | | - if isinstance(path, str) and not os.path.isfile(path): |
294 | | - orphans.add(path) |
295 | | - async with stats_lock: |
296 | | - stats.removed = len(orphans) |
297 | | - if len(orphans): |
298 | | - logger.info(f"Removing {len(orphans)} orphaned files from database.") |
299 | | - await collection.delete(where={"path": {"$in": list(orphans)}}) |
| 311 | + await remove_orphanes(collection, collection_lock, stats, stats_lock) |
300 | 312 |
|
301 | 313 | show_stats(configs=configs, stats=stats) |
302 | 314 | return 0 |
0 commit comments