Skip to content

Commit bea8dd4

Browse files
committed
optimize _index_all() to not commit transactions
1 parent 695de46 commit bea8dd4

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

jupyter_server/services/contents/fileidmanager.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def __init__(self, *args, **kwargs):
6363
"is_dir TINYINT NOT NULL"
6464
")"
6565
)
66-
self._index_dir_recursively(self.root_dir)
66+
self._index_all()
6767
self.con.execute("CREATE INDEX IF NOT EXISTS ix_Files_path ON Files (path)")
6868
self.con.execute("CREATE INDEX IF NOT EXISTS ix_Files_ino ON Files (ino)")
6969
self.con.execute("CREATE INDEX IF NOT EXISTS ix_Files_is_dir ON Files (is_dir)")
@@ -77,15 +77,18 @@ def _validate_abspath_traits(self, proposal):
7777
raise TraitError("FileIdManager : %s must be an absolute path" % proposal["trait"].name)
7878
return self._normalize_path(proposal["value"])
7979

80-
def _index_dir_recursively(self, dir_path):
81-
"""Recursively indexes all directories under a given path. Used in
82-
__init__() to recursively index all directories under the server
83-
root."""
84-
self.index(dir_path)
80+
def _index_all(self):
81+
"""Recursively indexes all directories under the server root."""
82+
self._index_dir_recursively(self.root_dir, self._stat(self.root_dir))
83+
84+
def _index_dir_recursively(self, dir_path, stat_info):
85+
"""Recursively indexes all directories under a given path."""
86+
self.index(dir_path, stat_info=stat_info, commit=False)
87+
8588
with os.scandir(dir_path) as scan_iter:
8689
for entry in scan_iter:
8790
if entry.is_dir():
88-
self._index_dir_recursively(entry.path)
91+
self._index_dir_recursively(entry.path, self._parse_raw_stat(entry.stat()))
8992
scan_iter.close()
9093

9194
def _sync_all(self):
@@ -183,12 +186,11 @@ def _parse_raw_stat(self, raw_stat, stat_info=None):
183186

184187
return stat_info
185188

186-
def _stat(self, path, stat_info=None):
189+
def _stat(self, path):
187190
"""Returns stat info on a path in a StatStruct object. Writes to
188191
`stat_info` StatStruct arg if passed. Returns None if file does not
189192
exist at path."""
190-
if stat_info is None:
191-
stat_info = StatStruct()
193+
stat_info = StatStruct()
192194

193195
try:
194196
raw_stat = os.stat(path)
@@ -225,11 +227,11 @@ def _update(self, id, stat_info, path=None):
225227
(stat_info.ino, stat_info.crtime, stat_info.mtime, id),
226228
)
227229

228-
def index(self, path):
230+
def index(self, path, stat_info=None, commit=True):
229231
"""Returns the file ID for the file at `path`, creating a new file ID if
230232
one does not exist. Returns None only if file does not exist at path."""
231233
path = self._normalize_path(path)
232-
stat_info = self._stat(path)
234+
stat_info = stat_info or self._stat(path)
233235
if not stat_info:
234236
return None
235237

@@ -240,7 +242,8 @@ def index(self, path):
240242

241243
# otherwise, create a new record and return the file ID
242244
id = self._create(path, stat_info)
243-
self.con.commit()
245+
if commit:
246+
self.con.commit()
244247
return id
245248

246249
def get_id(self, path):

0 commit comments

Comments
 (0)