@@ -312,21 +312,21 @@ def _from_normalized_path(self, path: Optional[str]) -> Optional[str]:
312
312
313
313
def _create (self , path : str ) -> str :
314
314
path = self ._normalize_path (path )
315
+ row = self .con .execute ("SELECT id FROM Files WHERE path = ?" , (path ,)).fetchone ()
316
+ existing_id = row and row [0 ]
317
+
318
+ if existing_id :
319
+ return existing_id
320
+
315
321
id = self ._uuid ()
316
322
self .con .execute ("INSERT INTO Files (id, path) VALUES (?, ?)" , (id , path ))
317
323
return id
318
324
319
325
def index (self , path : str ) -> str :
326
+ # create new record
320
327
with self .con :
321
- path = self ._normalize_path (path )
322
- row = self .con .execute ("SELECT id FROM Files WHERE path = ?" , (path ,)).fetchone ()
323
- existing_id = row and row [0 ]
324
-
325
- if existing_id :
326
- return existing_id
327
-
328
- # create new record
329
328
id = self ._create (path )
329
+ self .con .commit ()
330
330
return id
331
331
332
332
def get_id (self , path : str ) -> Optional [str ]:
@@ -612,6 +612,7 @@ def _sync_file(self, path, stat_info):
612
612
"SELECT id, path, crtime FROM Files WHERE ino = ?" , (stat_info .ino ,)
613
613
).fetchone ()
614
614
615
+
615
616
# if ino is not in database, return None
616
617
if src is None :
617
618
return None
@@ -625,6 +626,7 @@ def _sync_file(self, path, stat_info):
625
626
# otherwise update existing record with new path, moving any indexed
626
627
# children if necessary. then return its id
627
628
self ._update (id , path = path )
629
+
628
630
if stat_info .is_dir and old_path != path :
629
631
self ._move_recursive (old_path , path )
630
632
self ._update_cursor = True
@@ -671,6 +673,17 @@ def _create(self, path, stat_info):
671
673
dangerous and may throw a runtime error if the file is not guaranteed to
672
674
have a unique `ino`.
673
675
"""
676
+ # If the path exists
677
+ existing_id , ino = None , None
678
+ row = self .con .execute ("SELECT id, ino FROM Files WHERE path = ?" , (path ,)).fetchone ()
679
+ if row :
680
+ existing_id , ino = row
681
+
682
+ # If the file ID already exists and the current file matches our records
683
+ # return the file ID instead of creating a new one.
684
+ if existing_id and stat_info .ino == ino :
685
+ return existing_id
686
+
674
687
id = self ._uuid ()
675
688
self .con .execute (
676
689
"INSERT INTO Files (id, path, ino, crtime, mtime, is_dir) VALUES (?, ?, ?, ?, ?, ?)" ,
0 commit comments