Skip to content

Commit fbc4c44

Browse files
committed
Check if a file ID already exists before creating a new record
1 parent ab2ee68 commit fbc4c44

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

jupyter_server_fileid/manager.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -311,19 +311,18 @@ def _from_normalized_path(self, path: Optional[str]) -> Optional[str]:
311311
return relpath
312312

313313
def _create(self, path: str) -> str:
314-
path = self._normalize_path(path)
315-
id = self._uuid()
316-
self.con.execute("INSERT INTO Files (id, path) VALUES (?, ?)", (id, path))
317-
return id
318-
319-
def index(self, path: str) -> str:
320314
path = self._normalize_path(path)
321315
row = self.con.execute("SELECT id FROM Files WHERE path = ?", (path,)).fetchone()
322316
existing_id = row and row[0]
323317

324318
if existing_id:
325319
return existing_id
320+
321+
id = self._uuid()
322+
self.con.execute("INSERT INTO Files (id, path) VALUES (?, ?)", (id, path))
323+
return id
326324

325+
def index(self, path: str) -> str:
327326
# create new record
328327
id = self._create(path)
329328
self.con.commit()
@@ -613,6 +612,7 @@ def _sync_file(self, path, stat_info):
613612
"SELECT id, path, crtime FROM Files WHERE ino = ?", (stat_info.ino,)
614613
).fetchone()
615614

615+
616616
# if ino is not in database, return None
617617
if src is None:
618618
return None
@@ -626,6 +626,7 @@ def _sync_file(self, path, stat_info):
626626
# otherwise update existing record with new path, moving any indexed
627627
# children if necessary. then return its id
628628
self._update(id, path=path)
629+
629630
if stat_info.is_dir and old_path != path:
630631
self._move_recursive(old_path, path)
631632
self._update_cursor = True
@@ -672,6 +673,17 @@ def _create(self, path, stat_info):
672673
dangerous and may throw a runtime error if the file is not guaranteed to
673674
have a unique `ino`.
674675
"""
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+
675687
id = self._uuid()
676688
self.con.execute(
677689
"INSERT INTO Files (id, path, ino, crtime, mtime, is_dir) VALUES (?, ?, ?, ?, ?, ?)",

0 commit comments

Comments
 (0)