-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
256 lines (212 loc) · 8.92 KB
/
database.py
File metadata and controls
256 lines (212 loc) · 8.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
"""
Database connection and management for PubLog Application
Uses DuckDB for fast analytical queries on large CSV files
"""
import duckdb
import os
from pathlib import Path
from typing import Optional, List, Dict, Any
import logging
from config import DB_PATH, DATA_FILES, PRIORITY_TABLES, LARGE_TABLES
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class PubLogDatabase:
"""Manages DuckDB database for PubLog data"""
_instance: Optional['PubLogDatabase'] = None
_connection: Optional[duckdb.DuckDBPyConnection] = None
def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance
def __init__(self):
if self._connection is None:
self._connection = duckdb.connect(str(DB_PATH))
self._setup_extensions()
def _setup_extensions(self):
"""Setup DuckDB extensions for better CSV handling"""
try:
self._connection.execute("INSTALL httpfs; LOAD httpfs;")
except:
pass # Extension might already be installed
@property
def conn(self) -> duckdb.DuckDBPyConnection:
return self._connection
def get_all_data_files(self) -> Dict[str, Path]:
"""Get flat dictionary of all data files"""
all_files = {}
for category, files in DATA_FILES.items():
all_files.update(files)
return all_files
def is_table_indexed(self, table_name: str) -> bool:
"""Check if a table is already indexed in DuckDB"""
try:
# DuckDB stores table names - check both upper and lower case
result = self._connection.execute(
"SELECT COUNT(*) FROM information_schema.tables WHERE UPPER(table_name) = UPPER(?)",
[table_name]
).fetchone()
return result[0] > 0
except Exception as e:
logger.error(f"Error checking table {table_name}: {e}")
return False
def get_indexed_tables(self) -> List[str]:
"""Get list of all indexed tables"""
try:
result = self._connection.execute(
"SELECT table_name FROM information_schema.tables WHERE table_schema = 'main'"
).fetchall()
return [row[0] for row in result]
except Exception as e:
logger.error(f"Error getting indexed tables: {e}")
return []
def index_csv_file(self, table_name: str, file_path: Path, force: bool = False) -> bool:
"""Index a CSV file into DuckDB"""
if not file_path.exists():
logger.warning(f"File not found: {file_path}")
return False
if self.is_table_indexed(table_name) and not force:
logger.info(f"Table {table_name} already indexed, skipping")
return True
try:
# Drop existing table if force
if force:
self._connection.execute(f"DROP TABLE IF EXISTS {table_name}")
# Get file size for logging
file_size_mb = file_path.stat().st_size / (1024 * 1024)
logger.info(f"Indexing {table_name} ({file_size_mb:.1f} MB)...")
# Create table from CSV
self._connection.execute(f"""
CREATE TABLE {table_name} AS
SELECT * FROM read_csv_auto('{file_path}',
header=true,
quote='"',
escape='"',
ignore_errors=true,
sample_size=10000
)
""")
# Get row count
count = self._connection.execute(f"SELECT COUNT(*) FROM {table_name}").fetchone()[0]
logger.info(f"Indexed {table_name}: {count:,} rows")
return True
except Exception as e:
logger.error(f"Error indexing {table_name}: {e}")
return False
def create_indexes(self, table_name: str, columns: List[str]):
"""Create indexes on specific columns for faster lookups"""
for col in columns:
try:
index_name = f"idx_{table_name}_{col}".lower()
self._connection.execute(
f"CREATE INDEX IF NOT EXISTS {index_name} ON {table_name}({col})"
)
logger.info(f"Created index {index_name}")
except Exception as e:
logger.warning(f"Could not create index on {table_name}.{col}: {e}")
def index_priority_tables(self, force: bool = False) -> Dict[str, bool]:
"""Index priority (smaller) tables first"""
results = {}
all_files = self.get_all_data_files()
for table_name in PRIORITY_TABLES:
if table_name in all_files:
results[table_name] = self.index_csv_file(
table_name, all_files[table_name], force
)
return results
def index_large_tables(self, force: bool = False) -> Dict[str, bool]:
"""Index large tables (may take longer)"""
results = {}
all_files = self.get_all_data_files()
for table_name in LARGE_TABLES:
if table_name in all_files:
results[table_name] = self.index_csv_file(
table_name, all_files[table_name], force
)
return results
def index_all_tables(self, force: bool = False) -> Dict[str, bool]:
"""Index all available tables"""
results = {}
all_files = self.get_all_data_files()
# Index priority tables first
results.update(self.index_priority_tables(force))
# Then large tables
results.update(self.index_large_tables(force))
# Any remaining tables
for table_name, file_path in all_files.items():
if table_name not in results:
results[table_name] = self.index_csv_file(table_name, file_path, force)
return results
def query(self, sql: str, params: Optional[List] = None) -> List[Dict[str, Any]]:
"""Execute a query and return results as list of dicts"""
try:
if params:
result = self._connection.execute(sql, params)
else:
result = self._connection.execute(sql)
columns = [desc[0] for desc in result.description]
rows = result.fetchall()
return [dict(zip(columns, row)) for row in rows]
except Exception as e:
logger.error(f"Query error: {e}")
raise
def query_df(self, sql: str, params: Optional[List] = None):
"""Execute a query and return results as DataFrame"""
try:
if params:
return self._connection.execute(sql, params).fetchdf()
else:
return self._connection.execute(sql).fetchdf()
except Exception as e:
logger.error(f"Query error: {e}")
raise
def get_table_info(self, table_name: str) -> Dict[str, Any]:
"""Get information about a table"""
try:
# Get column info
columns = self._connection.execute(
f"DESCRIBE {table_name}"
).fetchall()
# Get row count
count = self._connection.execute(
f"SELECT COUNT(*) FROM {table_name}"
).fetchone()[0]
return {
"table_name": table_name,
"row_count": count,
"columns": [
{"name": col[0], "type": col[1]}
for col in columns
]
}
except Exception as e:
logger.error(f"Error getting table info for {table_name}: {e}")
return {}
def get_database_stats(self) -> Dict[str, Any]:
"""Get overall database statistics"""
tables = self.get_indexed_tables()
total_rows = 0
table_stats = []
for table in tables:
try:
count = self._connection.execute(
f"SELECT COUNT(*) FROM {table}"
).fetchone()[0]
total_rows += count
table_stats.append({"table": table, "rows": count})
except:
pass
return {
"total_tables": len(tables),
"total_rows": total_rows,
"tables": sorted(table_stats, key=lambda x: x["rows"], reverse=True),
"db_file_size_mb": DB_PATH.stat().st_size / (1024 * 1024) if DB_PATH.exists() else 0
}
def close(self):
"""Close database connection"""
if self._connection:
self._connection.close()
self._connection = None
PubLogDatabase._instance = None
# Singleton accessor
def get_db() -> PubLogDatabase:
return PubLogDatabase()