mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Make hash and size a lazy property (#1837)
* use inspect data for hash and file size * make hash and cached_size lazy properties * move hash property near size
This commit is contained in:
parent
6e788b49ed
commit
2ea60e12d9
1 changed files with 24 additions and 12 deletions
|
|
@ -39,7 +39,7 @@ class Database:
|
||||||
self.memory_name = memory_name
|
self.memory_name = memory_name
|
||||||
if memory_name is not None:
|
if memory_name is not None:
|
||||||
self.is_memory = True
|
self.is_memory = True
|
||||||
self.hash = None
|
self.cached_hash = None
|
||||||
self.cached_size = None
|
self.cached_size = None
|
||||||
self._cached_table_counts = None
|
self._cached_table_counts = None
|
||||||
self._write_thread = None
|
self._write_thread = None
|
||||||
|
|
@ -47,14 +47,6 @@ class Database:
|
||||||
# These are used when in non-threaded mode:
|
# These are used when in non-threaded mode:
|
||||||
self._read_connection = None
|
self._read_connection = None
|
||||||
self._write_connection = None
|
self._write_connection = None
|
||||||
if not self.is_mutable and not self.is_memory:
|
|
||||||
if self.ds.inspect_data and self.ds.inspect_data.get(self.name):
|
|
||||||
self.hash = self.ds.inspect_data[self.name]["hash"]
|
|
||||||
self.cached_size = self.ds.inspect_data[self.name]["size"]
|
|
||||||
else:
|
|
||||||
p = Path(path)
|
|
||||||
self.hash = inspect_hash(p)
|
|
||||||
self.cached_size = p.stat().st_size
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def cached_table_counts(self):
|
def cached_table_counts(self):
|
||||||
|
|
@ -266,14 +258,34 @@ class Database:
|
||||||
results = await self.execute_fn(sql_operation_in_thread)
|
results = await self.execute_fn(sql_operation_in_thread)
|
||||||
return results
|
return results
|
||||||
|
|
||||||
|
@property
|
||||||
|
def hash(self):
|
||||||
|
if self.cached_hash is not None:
|
||||||
|
return self.cached_hash
|
||||||
|
elif self.is_mutable or self.is_memory:
|
||||||
|
return None
|
||||||
|
elif self.ds.inspect_data and self.ds.inspect_data.get(self.name):
|
||||||
|
self.cached_hash = self.ds.inspect_data[self.name]["hash"]
|
||||||
|
return self.cached_hash
|
||||||
|
else:
|
||||||
|
p = Path(self.path)
|
||||||
|
self.cached_hash = inspect_hash(p)
|
||||||
|
return self.cached_hash
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def size(self):
|
def size(self):
|
||||||
if self.is_memory:
|
|
||||||
return 0
|
|
||||||
if self.cached_size is not None:
|
if self.cached_size is not None:
|
||||||
return self.cached_size
|
return self.cached_size
|
||||||
else:
|
elif self.is_memory:
|
||||||
|
return 0
|
||||||
|
elif self.is_mutable:
|
||||||
return Path(self.path).stat().st_size
|
return Path(self.path).stat().st_size
|
||||||
|
elif self.ds.inspect_data and self.ds.inspect_data.get(self.name):
|
||||||
|
self.cached_size = self.ds.inspect_data[self.name]["size"]
|
||||||
|
return self.cached_size
|
||||||
|
else:
|
||||||
|
self.cached_size = Path(self.path).stat().st_size
|
||||||
|
return self.cached_size
|
||||||
|
|
||||||
async def table_counts(self, limit=10):
|
async def table_counts(self, limit=10):
|
||||||
if not self.is_mutable and self.cached_table_counts is not None:
|
if not self.is_mutable and self.cached_table_counts is not None:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue