|
| 1 | +import sqlite3 |
| 2 | +import glob |
| 3 | +import os |
| 4 | +import base64 |
| 5 | +from tqdm import tqdm |
| 6 | + |
| 7 | +# Define database file pattern |
| 8 | +db_files = glob.glob("../data/Wikidata/sqlite_cacheembeddings_*.db") |
| 9 | + |
| 10 | +# Define the target merged database |
| 11 | +merged_db = "../data/Wikidata/sqlite_cacheembeddings_merged.db" |
| 12 | +TABLE_NAME = "wikidata_prototype" |
| 13 | + |
| 14 | +# Batch size for processing |
| 15 | +BATCH_SIZE = 1000 # Adjust based on performance needs |
| 16 | + |
| 17 | +# Create the merged database connection |
| 18 | +conn_merged = sqlite3.connect(merged_db) |
| 19 | +cursor_merged = conn_merged.cursor() |
| 20 | + |
| 21 | +# Create table in the merged database if it doesn't exist |
| 22 | +cursor_merged.execute(f""" |
| 23 | +CREATE TABLE IF NOT EXISTS {TABLE_NAME} ( |
| 24 | + id TEXT PRIMARY KEY, |
| 25 | + embedding TEXT |
| 26 | +); |
| 27 | +""") |
| 28 | +conn_merged.commit() |
| 29 | + |
| 30 | +# Helper function to check if a string is a valid Base64 encoding |
| 31 | +def is_valid_base64(s): |
| 32 | + try: |
| 33 | + if not s or not isinstance(s, str): |
| 34 | + return False |
| 35 | + base64.b64decode(s, validate=True) |
| 36 | + return True |
| 37 | + except Exception: |
| 38 | + return False |
| 39 | + |
| 40 | +# Loop through all source databases |
| 41 | +for db_file in db_files: |
| 42 | + print(f"Processing {db_file}...") |
| 43 | + |
| 44 | + # Connect to the current database |
| 45 | + conn_src = sqlite3.connect(db_file) |
| 46 | + cursor_src = conn_src.cursor() |
| 47 | + |
| 48 | + # Get total record count for progress tracking |
| 49 | + cursor_src.execute(f"SELECT COUNT(*) FROM {TABLE_NAME}") |
| 50 | + total_records = cursor_src.fetchone()[0] |
| 51 | + |
| 52 | + # Fetch records in batches |
| 53 | + offset = 0 |
| 54 | + with tqdm(total=total_records, |
| 55 | + desc=f"Merging {db_file}", unit="records") as pbar: |
| 56 | + while True: |
| 57 | + cursor_src.execute(f"SELECT id, embedding FROM {TABLE_NAME} LIMIT {BATCH_SIZE} OFFSET {offset}") |
| 58 | + records = cursor_src.fetchall() |
| 59 | + if not records: |
| 60 | + break # No more records to process |
| 61 | + |
| 62 | + # Prepare batch for insertion |
| 63 | + batch_data = [] |
| 64 | + for id_, embedding in records: |
| 65 | + if embedding and embedding.strip() and is_valid_base64(embedding): |
| 66 | + cursor_merged.execute(f"SELECT embedding FROM {TABLE_NAME} WHERE id = ?", (id_,)) |
| 67 | + existing = cursor_merged.fetchone() |
| 68 | + |
| 69 | + if existing is None or not is_valid_base64(existing[0]): |
| 70 | + batch_data.append((id_, embedding, embedding)) # Prepare for bulk insert |
| 71 | + |
| 72 | + # Perform batch insert/update |
| 73 | + if batch_data: |
| 74 | + cursor_merged.executemany( |
| 75 | + f""" |
| 76 | + INSERT INTO {TABLE_NAME} (id, embedding) |
| 77 | + VALUES (?, ?) |
| 78 | + ON CONFLICT(id) DO UPDATE SET embedding = ? |
| 79 | + """, |
| 80 | + batch_data |
| 81 | + ) |
| 82 | + conn_merged.commit() |
| 83 | + |
| 84 | + offset += BATCH_SIZE # Move to the next batch |
| 85 | + pbar.update(len(records)) # Update tqdm progress bar |
| 86 | + |
| 87 | + conn_src.close() |
| 88 | + |
| 89 | +# Close merged database connection |
| 90 | +conn_merged.close() |
| 91 | +print(f"Merge completed! Combined database saved as {merged_db}") |
0 commit comments