Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -87,25 +87,32 @@ class ModelDatabase:
|
|
| 87 |
# Ensure the file exists before trying to connect
|
| 88 |
if not os.path.exists(db_path):
|
| 89 |
raise FileNotFoundError(f"Database file not found at {db_path}")
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
disk_conn.execute("PRAGMA synchronous = OFF")
|
| 94 |
-
#
|
| 95 |
-
disk_conn.execute("PRAGMA cache_size = -2097152")
|
|
|
|
| 96 |
disk_conn.execute("PRAGMA temp_store = MEMORY")
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
# 2. Connect to the target in-memory database
|
| 100 |
print("Creating in-memory database...")
|
| 101 |
-
# Increase timeout for potential long operations on the in-memory DB too
|
| 102 |
self.conn = sqlite3.connect(':memory:', check_same_thread=False, timeout=120)
|
| 103 |
-
self.conn.row_factory = sqlite3.Row
|
| 104 |
|
| 105 |
# 3. Backup data from disk to memory
|
| 106 |
print("Starting database backup from disk to memory (this may take a while)...")
|
| 107 |
start_backup = time.time()
|
| 108 |
-
# Use a context manager for the destination connection to handle commits/rollbacks
|
| 109 |
with self.conn:
|
| 110 |
disk_conn.backup(self.conn)
|
| 111 |
end_backup = time.time()
|
|
@@ -113,35 +120,30 @@ class ModelDatabase:
|
|
| 113 |
|
| 114 |
# 4. Apply PRAGMAs suitable for the in-memory database
|
| 115 |
print("Applying PRAGMAs to in-memory database...")
|
| 116 |
-
# temp_store=MEMORY is default for :memory:, but explicit is fine
|
| 117 |
self.conn.execute("PRAGMA temp_store = MEMORY")
|
| 118 |
-
#
|
| 119 |
-
# self.conn.execute("PRAGMA cache_size = -4194304") # e.g., 4GB
|
| 120 |
|
| 121 |
# 5. Ensure indices exist on the in-memory database *after* data loading
|
| 122 |
print("Creating indices on in-memory database...")
|
| 123 |
start_index = time.time()
|
| 124 |
-
self._ensure_indices() #
|
| 125 |
end_index = time.time()
|
| 126 |
print(f"Index creation completed in {end_index - start_index:.2f} seconds.")
|
| 127 |
|
| 128 |
except sqlite3.Error as e:
|
| 129 |
print(f"SQLite error during database initialization: {e}")
|
| 130 |
-
if self.conn:
|
| 131 |
-
|
| 132 |
-
self.conn = None
|
| 133 |
-
raise # Re-raise the exception to signal failure
|
| 134 |
except FileNotFoundError as e:
|
| 135 |
print(f"Error: {e}")
|
| 136 |
raise
|
| 137 |
except Exception as e:
|
| 138 |
print(f"Unexpected error during database initialization: {e}")
|
| 139 |
-
if self.conn:
|
| 140 |
-
self.conn.close()
|
| 141 |
-
self.conn = None
|
| 142 |
raise
|
| 143 |
finally:
|
| 144 |
-
# 6. Close the disk connection
|
| 145 |
if disk_conn:
|
| 146 |
disk_conn.close()
|
| 147 |
print("Closed connection to disk database.")
|
|
@@ -149,6 +151,7 @@ class ModelDatabase:
|
|
| 149 |
if self.conn:
|
| 150 |
print("In-memory database initialized successfully.")
|
| 151 |
else:
|
|
|
|
| 152 |
print("Error: In-memory database connection failed.")
|
| 153 |
raise RuntimeError("Failed to establish in-memory database connection.")
|
| 154 |
|
|
|
|
| 87 |
# Ensure the file exists before trying to connect
|
| 88 |
if not os.path.exists(db_path):
|
| 89 |
raise FileNotFoundError(f"Database file not found at {db_path}")
|
| 90 |
+
# Use a longer timeout just in case connection takes time, e.g. on network drives
|
| 91 |
+
disk_conn = sqlite3.connect(f'file:{db_path}?mode=ro', uri=True, check_same_thread=False, timeout=120)
|
| 92 |
+
|
| 93 |
+
# --- Adjusted PRAGMAs for disk_conn (read-only source) ---
|
| 94 |
+
# Apply PRAGMAs that are safe for read-only and might speed up the backup read
|
| 95 |
+
print("Applying safe PRAGMAs to source connection for potentially faster read...")
|
| 96 |
+
# synchronous=OFF is generally safe for read-only access
|
| 97 |
disk_conn.execute("PRAGMA synchronous = OFF")
|
| 98 |
+
# Set a large cache size to potentially speed up reading from disk during backup
|
| 99 |
+
disk_conn.execute("PRAGMA cache_size = -2097152") # ~2GB cache, adjust if needed
|
| 100 |
+
# temp_store=MEMORY is safe and might help if temporary tables are used internally
|
| 101 |
disk_conn.execute("PRAGMA temp_store = MEMORY")
|
| 102 |
+
# Removed PRAGMA journal_mode = OFF as it caused the disk I/O error
|
| 103 |
+
# Removed PRAGMA locking_mode = EXCLUSIVE as it's not needed for read-only source
|
| 104 |
+
print("Read-safe PRAGMAs applied to source connection.")
|
| 105 |
+
# --- End of Adjusted PRAGMAs ---
|
| 106 |
+
|
| 107 |
|
| 108 |
# 2. Connect to the target in-memory database
|
| 109 |
print("Creating in-memory database...")
|
|
|
|
| 110 |
self.conn = sqlite3.connect(':memory:', check_same_thread=False, timeout=120)
|
| 111 |
+
self.conn.row_factory = sqlite3.Row
|
| 112 |
|
| 113 |
# 3. Backup data from disk to memory
|
| 114 |
print("Starting database backup from disk to memory (this may take a while)...")
|
| 115 |
start_backup = time.time()
|
|
|
|
| 116 |
with self.conn:
|
| 117 |
disk_conn.backup(self.conn)
|
| 118 |
end_backup = time.time()
|
|
|
|
| 120 |
|
| 121 |
# 4. Apply PRAGMAs suitable for the in-memory database
|
| 122 |
print("Applying PRAGMAs to in-memory database...")
|
|
|
|
| 123 |
self.conn.execute("PRAGMA temp_store = MEMORY")
|
| 124 |
+
# Optional: Set cache size for in-memory DB if desired, though less critical
|
| 125 |
+
# self.conn.execute("PRAGMA cache_size = -4194304") # e.g., 4GB
|
| 126 |
|
| 127 |
# 5. Ensure indices exist on the in-memory database *after* data loading
|
| 128 |
print("Creating indices on in-memory database...")
|
| 129 |
start_index = time.time()
|
| 130 |
+
self._ensure_indices() # Operates on self.conn (memory DB)
|
| 131 |
end_index = time.time()
|
| 132 |
print(f"Index creation completed in {end_index - start_index:.2f} seconds.")
|
| 133 |
|
| 134 |
except sqlite3.Error as e:
|
| 135 |
print(f"SQLite error during database initialization: {e}")
|
| 136 |
+
if self.conn: self.conn.close(); self.conn = None
|
| 137 |
+
raise # Re-raise to signal failure
|
|
|
|
|
|
|
| 138 |
except FileNotFoundError as e:
|
| 139 |
print(f"Error: {e}")
|
| 140 |
raise
|
| 141 |
except Exception as e:
|
| 142 |
print(f"Unexpected error during database initialization: {e}")
|
| 143 |
+
if self.conn: self.conn.close(); self.conn = None
|
|
|
|
|
|
|
| 144 |
raise
|
| 145 |
finally:
|
| 146 |
+
# 6. Close the disk connection
|
| 147 |
if disk_conn:
|
| 148 |
disk_conn.close()
|
| 149 |
print("Closed connection to disk database.")
|
|
|
|
| 151 |
if self.conn:
|
| 152 |
print("In-memory database initialized successfully.")
|
| 153 |
else:
|
| 154 |
+
# This path should ideally not be reached if exceptions are raised properly
|
| 155 |
print("Error: In-memory database connection failed.")
|
| 156 |
raise RuntimeError("Failed to establish in-memory database connection.")
|
| 157 |
|