-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentity_database_service.py
More file actions
561 lines (452 loc) · 18.7 KB
/
Copy pathentity_database_service.py
File metadata and controls
561 lines (452 loc) · 18.7 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
"""
Service layer for entity database file-based operations
This module provides high-level services for loading, managing, and accessing
manually curated entity reference images from the filesystem.
Implements async-first operations following the MemecoinService pattern.
"""
import asyncio
import base64
import logging
import time
from pathlib import Path
from typing import Dict, List, Optional, Tuple
from src.constants import ENTITY_DATABASE_ROOT
from src.models.entity.entity_database_models import DatabaseEntity, EntityDatabaseSnapshot
from src.models.entity.entity_models import EntityType
logger = logging.getLogger(__name__)
class EntityDatabaseService:
"""
Service for filesystem-based entity database operations
This service handles loading entity data from the filesystem, managing
the entity_database directory structure, and providing caching for
performance optimization.
Implements async-first operations with true async methods.
All blocking I/O wrapped with asyncio.to_thread().
Directory structure expected:
res/entity_database/
{category_name}/
{entity_name}/
example_001.jpg
example_002.jpg
...
Example:
res/entity_database/
logo/
bnb_logo/
example_001.jpg
example_002.jpg
person/
vitalik_buterin/
example_001.jpg
"""
def __init__(
self,
database_root: Path = ENTITY_DATABASE_ROOT,
cache_timeout: int = 60,
):
"""
Initialize the entity database service
Args:
database_root: Root directory for entity database (default from constants)
cache_timeout: Cache timeout in seconds (default 60)
"""
self.database_root = database_root
self.cache_timeout = cache_timeout
self._lock = asyncio.Lock()
self._snapshot_cache: Optional[Tuple[EntityDatabaseSnapshot, float]] = None
async def _ensure_database_directory(self) -> Path:
"""
Ensure entity database directory exists
Returns:
Path to entity database root
Raises:
PermissionError: If directory cannot be created or accessed
"""
try:
# Check if directory exists (blocking I/O)
dir_exists = await asyncio.to_thread(self.database_root.exists)
if not dir_exists:
logger.warning(
f"Entity database directory does not exist: {self.database_root}"
)
logger.info("Creating entity database directory...")
await asyncio.to_thread(self.database_root.mkdir, parents=True, exist_ok=True)
# Verify it's a directory
is_dir = await asyncio.to_thread(self.database_root.is_dir)
if not is_dir:
raise PermissionError(
f"Entity database path exists but is not a directory: {self.database_root}"
)
return self.database_root
except Exception as e:
logger.error(f"Failed to ensure entity database directory: {e}")
raise
def _is_cache_valid(
self, cache_entry: Optional[Tuple[EntityDatabaseSnapshot, float]]
) -> bool:
"""
Check if cache entry is still valid
Args:
cache_entry: Tuple of (snapshot, timestamp) or None
Returns:
True if cache is valid, False otherwise
"""
if cache_entry is None:
return False
_, timestamp = cache_entry
return time.time() - timestamp < self.cache_timeout
async def _scan_category_directory(
self, category_path: Path, category: EntityType
) -> List[DatabaseEntity]:
"""
Scan a category directory and load all entities (supports recursive subdirectories)
Handles both flat and hierarchical directory structures:
- Flat: logo/bnb_logo/example_001.jpg
- Hierarchical: logo/crypto/bnb_chain/bnb_chain_001.jpg
Args:
category_path: Path to category directory
category: EntityType for this category
Returns:
List of DatabaseEntity objects for this category
"""
def _scan_entity_directory(
entity_dir: Path, subcategory: str | None = None
) -> DatabaseEntity | None:
"""
Scan a single entity directory for images
Args:
entity_dir: Path to entity directory
subcategory: Optional subcategory name (e.g., 'crypto', 'brand')
Returns:
DatabaseEntity if images found, None otherwise
"""
entity_name = entity_dir.name
display_name = entity_name.replace("_", " ").title()
# Find all image files (ANY .jpg or .png, not just "example_*")
example_paths = []
# Glob for .jpg files
for image_file in entity_dir.glob("*.jpg"):
if image_file.is_file():
example_paths.append(image_file)
# Glob for .png files
for image_file in entity_dir.glob("*.png"):
if image_file.is_file():
example_paths.append(image_file)
# Sort for consistent ordering
example_paths.sort()
# Skip entities with no images
if not example_paths:
logger.debug(
f"No images found for entity: {entity_name} in {category.value}"
+ (f"/{subcategory}" if subcategory else "")
)
return None
# Create database entity with subcategory
entity = DatabaseEntity(
name=entity_name,
display_name=display_name,
category=category,
subcategory=subcategory,
example_image_paths=example_paths,
aliases=[], # Could be loaded from metadata file in future
)
logger.debug(
f"Loaded entity: {display_name} ({len(example_paths)} images)"
+ (f" [subcategory: {subcategory}]" if subcategory else "")
)
return entity
def _scan_sync() -> List[DatabaseEntity]:
"""Synchronous filesystem scanning (wrapped in to_thread)"""
entities = []
if not category_path.exists() or not category_path.is_dir():
logger.debug(
f"Category directory not found or not a directory: {category_path}"
)
return entities
# Iterate through items in category directory
for item in category_path.iterdir():
if not item.is_dir():
continue
# Check if this is a subcategory directory or entity directory
# Heuristic: If directory contains subdirectories, it's a subcategory
# Otherwise, it's an entity directory
subdirs = [d for d in item.iterdir() if d.is_dir()]
if subdirs:
# This is a subcategory directory (e.g., logo/crypto/)
subcategory_name = item.name
logger.debug(
f"Found subcategory: {category.value}/{subcategory_name}"
)
# Scan all entity directories in this subcategory
for entity_dir in subdirs:
entity = _scan_entity_directory(entity_dir, subcategory_name)
if entity:
entities.append(entity)
else:
# This is a direct entity directory (e.g., person/vitalik_buterin/)
entity = _scan_entity_directory(item, subcategory=None)
if entity:
entities.append(entity)
return entities
try:
entities = await asyncio.to_thread(_scan_sync)
logger.info(
f"Loaded {len(entities)} entities from category: {category.value}"
)
return entities
except Exception as e:
logger.error(f"Failed to scan category {category.value}: {e}")
return []
async def load_database_snapshot(
self, use_cache: bool = True
) -> EntityDatabaseSnapshot:
"""
Load complete entity database snapshot from filesystem
Args:
use_cache: Whether to use cached snapshot if available
Returns:
EntityDatabaseSnapshot with all entities organized by category
Raises:
PermissionError: If database directory cannot be accessed
"""
async with self._lock:
# Use cache if valid
if use_cache and self._is_cache_valid(self._snapshot_cache):
logger.debug("Returning cached entity database snapshot")
return self._snapshot_cache[0]
logger.info("Loading entity database from filesystem...")
# Ensure database directory exists
await self._ensure_database_directory()
# Scan all category directories
entities_by_category: Dict[EntityType, List[DatabaseEntity]] = {}
total_entity_count = 0
total_image_count = 0
categories_loaded = []
for entity_type in EntityType:
category_path = self.database_root / entity_type.value
# Scan this category
entities = await self._scan_category_directory(category_path, entity_type)
if entities:
entities_by_category[entity_type] = entities
categories_loaded.append(entity_type)
total_entity_count += len(entities)
# Count images
for entity in entities:
total_image_count += len(entity.example_image_paths)
# Create snapshot
snapshot = EntityDatabaseSnapshot(
entities_by_category=entities_by_category,
total_entity_count=total_entity_count,
total_image_count=total_image_count,
categories_loaded=categories_loaded,
)
# Update cache
self._snapshot_cache = (snapshot, time.time())
logger.info(
f"✅ Loaded entity database: {total_entity_count} entities, "
f"{total_image_count} images across {len(categories_loaded)} categories"
)
return snapshot
async def get_all_entities_by_category(
self, use_cache: bool = True
) -> Dict[EntityType, List[DatabaseEntity]]:
"""
Get all entities organized by category
Args:
use_cache: Whether to use cached data if available
Returns:
Dictionary mapping EntityType to list of DatabaseEntity objects
"""
snapshot = await self.load_database_snapshot(use_cache=use_cache)
return snapshot.entities_by_category
async def get_entity_by_name(
self,
name: str,
category: EntityType = None,
use_cache: bool = True,
) -> Optional[DatabaseEntity]:
"""
Find entity by name or alias (case-insensitive)
Args:
name: Entity name to search for
category: Optional category to narrow search
use_cache: Whether to use cached data
Returns:
DatabaseEntity if found, None otherwise
"""
snapshot = await self.load_database_snapshot(use_cache=use_cache)
return snapshot.find_entity_by_name(name, category)
async def find_entity_by_display_name(
self,
display_name: str,
category: EntityType = None,
use_cache: bool = True,
) -> Optional[DatabaseEntity]:
"""
Find entity by display name (case-insensitive)
Used for resolving LLM-returned display names back to database keys.
Searches only the display_name field, not aliases or directory names.
Args:
display_name: Display name to search for (e.g., "Bnb Chain", "Vitalik Buterin")
category: Optional category to narrow search
use_cache: Whether to use cached data
Returns:
DatabaseEntity if found by display_name, None otherwise
Example:
entity = await service.find_entity_by_display_name("Bnb Chain", EntityType.BRAND_LOGO)
if entity:
print(entity.name) # "bnb_chain"
"""
snapshot = await self.load_database_snapshot(use_cache=use_cache)
search_lower = display_name.lower().strip()
if category:
# Search only in specified category
entities = snapshot.get_entities_by_category(category)
for entity in entities:
if entity.display_name.lower().strip() == search_lower:
return entity
else:
# Search all categories
for entities in snapshot.entities_by_category.values():
for entity in entities:
if entity.display_name.lower().strip() == search_lower:
return entity
return None
async def get_entities_by_category(
self,
category: EntityType,
use_cache: bool = True,
) -> List[DatabaseEntity]:
"""
Get all entities for a specific category
Args:
category: EntityType to retrieve
use_cache: Whether to use cached data
Returns:
List of DatabaseEntity objects for this category
"""
snapshot = await self.load_database_snapshot(use_cache=use_cache)
return snapshot.get_entities_by_category(category)
async def get_entity_image_paths(
self,
entity_name: str,
category: EntityType = None,
use_cache: bool = True,
) -> List[Path]:
"""
Get file paths for entity's example images
Args:
entity_name: Entity name to search for
category: Optional category to narrow search
use_cache: Whether to use cached data
Returns:
List of Path objects to example images, empty list if not found
"""
entity = await self.get_entity_by_name(entity_name, category, use_cache)
if entity:
return entity.example_image_paths
else:
return []
async def get_entity_images_as_base64(
self,
entity_name: str,
category: EntityType = None,
use_cache: bool = True,
) -> List[str]:
"""
Get entity's example images as base64-encoded strings
Args:
entity_name: Entity name to search for
category: Optional category to narrow search
use_cache: Whether to use cached data
Returns:
List of base64-encoded image strings, empty list if not found
"""
image_paths = await self.get_entity_image_paths(entity_name, category, use_cache)
if not image_paths:
return []
def _load_and_encode_images() -> List[str]:
"""Load images and encode to base64 (blocking I/O)"""
encoded_images = []
for image_path in image_paths:
try:
if not image_path.exists() or not image_path.is_file():
logger.warning(f"Image file not found: {image_path}")
continue
with open(image_path, "rb") as f:
image_data = f.read()
encoded = base64.b64encode(image_data).decode("utf-8")
encoded_images.append(encoded)
except Exception as e:
logger.error(f"Failed to load image {image_path}: {e}")
continue
return encoded_images
try:
encoded_images = await asyncio.to_thread(_load_and_encode_images)
logger.debug(
f"Loaded and encoded {len(encoded_images)} images for entity: {entity_name}"
)
return encoded_images
except Exception as e:
logger.error(
f"Failed to load images for entity {entity_name}: {e}"
)
return []
async def refresh_cache(self) -> None:
"""
Force full refresh of cached data
Reloads entire database from filesystem, bypassing cache.
Use sparingly - only when filesystem changes are detected.
"""
logger.info("🔄 Refreshing entity database cache from filesystem")
async with self._lock:
self._snapshot_cache = None
# Pre-load cache
await self.load_database_snapshot(use_cache=False)
async def get_database_stats(self) -> Dict[str, any]:
"""
Get statistics about the entity database
Returns:
Dictionary with statistics:
- total_entities: Total number of entities
- total_images: Total number of example images
- categories: Number of categories with entities
- entities_by_category: Breakdown per category
"""
snapshot = await self.load_database_snapshot(use_cache=True)
return {
"total_entities": snapshot.total_entity_count,
"total_images": snapshot.total_image_count,
"categories": len(snapshot.categories_loaded),
"entities_by_category": snapshot.get_category_stats(),
"database_root": str(self.database_root),
}
async def has_entity(
self,
entity_name: str,
category: EntityType = None,
use_cache: bool = True,
) -> bool:
"""
Check if entity exists in database
Args:
entity_name: Entity name to check
category: Optional category to narrow search
use_cache: Whether to use cached data
Returns:
True if entity exists, False otherwise
"""
entity = await self.get_entity_by_name(entity_name, category, use_cache)
return entity is not None
# Global service instance (singleton pattern)
_entity_database_service_instance: Optional[EntityDatabaseService] = None
def get_entity_database_service() -> EntityDatabaseService:
"""
Get the global entity database service instance
Returns:
EntityDatabaseService instance (singleton)
"""
global _entity_database_service_instance
if _entity_database_service_instance is None:
_entity_database_service_instance = EntityDatabaseService()
return _entity_database_service_instance