| import sqlite3, time
|
| from collections import OrderedDict
|
|
|
| MAIN_DB = "fully_merged_database.sqlite"
|
| PRINT_INTERVAL = 0.1
|
|
|
|
|
| class Element:
|
| __slots__ = ("name", "emoji", "database_id")
|
| def __init__(self, name, emoji=None, database_id=None):
|
| self.name, self.emoji, self.database_id = name, emoji, database_id
|
| def __hash__(self): return hash(self.name)
|
| def __eq__(self, other): return self.name == other.name
|
| def __str__(self): return f"{self.emoji or ''} {self.name}"
|
|
|
|
|
| class ElementCache:
|
| def __init__(self, conn):
|
| self.conn, self.cache = conn, OrderedDict()
|
| for row in conn.execute("SELECT id, name, emoji FROM element"):
|
| self.cache[row[1]] = Element(row[1], row[2], row[0])
|
|
|
| def get_or_create(self, elem: Element):
|
| if elem.name in self.cache:
|
| return self.cache[elem.name].database_id
|
|
|
| cur = self.conn.execute(
|
| "INSERT INTO element (name, emoji) VALUES (?, ?) "
|
| "ON CONFLICT(name) DO UPDATE SET emoji=excluded.emoji",
|
| (elem.name, elem.emoji),
|
| )
|
| rowid = cur.lastrowid
|
| if rowid != 0:
|
| elem.database_id = rowid
|
| else:
|
| elem.database_id = self.conn.execute(
|
| "SELECT id FROM element WHERE name=?", (elem.name,)
|
| ).fetchone()[0]
|
| self.cache[elem.name] = elem
|
| return elem.database_id
|
|
|
|
|
| def connect():
|
| conn = sqlite3.connect(MAIN_DB)
|
| conn.execute("PRAGMA synchronous = OFF")
|
| conn.execute("PRAGMA journal_mode = MEMORY")
|
| conn.execute("PRAGMA temp_store = MEMORY")
|
| conn.execute("PRAGMA locking_mode = EXCLUSIVE")
|
| conn.execute("PRAGMA cache_size = -200000")
|
| return conn
|
|
|
|
|
| def init_db(conn):
|
| conn.execute("""
|
| CREATE TABLE IF NOT EXISTS element (
|
| id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| first_created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
| name TEXT UNIQUE,
|
| emoji TEXT
|
| )
|
| """)
|
| conn.execute("""
|
| CREATE TABLE IF NOT EXISTS pair (
|
| id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
|
| first_element_id INTEGER,
|
| second_element_id INTEGER,
|
| result_element_id INTEGER,
|
| is_discovery INTEGER,
|
| FOREIGN KEY (first_element_id) REFERENCES element (id),
|
| FOREIGN KEY (second_element_id) REFERENCES element (id),
|
| FOREIGN KEY (result_element_id) REFERENCES element (id),
|
| UNIQUE(first_element_id, second_element_id)
|
| )
|
| """)
|
| conn.commit()
|
|
|
|
|
| def merge_one_file(dbfile, conn, cache, start_count=0):
|
| src = sqlite3.connect(dbfile, uri=True)
|
| src.execute("PRAGMA synchronous = OFF")
|
| src.execute("PRAGMA journal_mode = MEMORY")
|
| src.execute("PRAGMA temp_store = MEMORY")
|
| src.commit()
|
| conn.execute("BEGIN IMMEDIATE")
|
| id_to_element = {row[0]: Element(row[1], row[2]) for row in src.execute("SELECT id, name, emoji FROM element")}
|
| pps = 0
|
| count, estim = start_count, 0
|
| last_time = time.perf_counter()
|
|
|
| for first_id, second_id, result_id, is_disc in src.execute(
|
| "SELECT first_element_id, second_element_id, result_element_id, is_discovery FROM pair"
|
| ):
|
| try:
|
| first = id_to_element[first_id]
|
| second = id_to_element[second_id]
|
| result = id_to_element[result_id]
|
| except KeyError:
|
| continue
|
|
|
|
|
| for e in (first, second, result):
|
| cache.get_or_create(e)
|
|
|
| conn.execute("""
|
| INSERT INTO pair (first_element_id, second_element_id, result_element_id, is_discovery)
|
| VALUES (?, ?, ?, ?)
|
| ON CONFLICT(first_element_id, second_element_id) DO UPDATE SET
|
| result_element_id=excluded.result_element_id,
|
| is_discovery=MAX(is_discovery, excluded.is_discovery)
|
| """, (first.database_id, second.database_id, result.database_id, is_disc))
|
|
|
| count += 1
|
| estim += 1
|
| now = time.perf_counter()
|
| if now - last_time >= PRINT_INTERVAL:
|
| pps = estim / (now - last_time)
|
| last_time, estim = now, 0
|
| print(f"(#{count:,} | PPS: {pps:,.0f}/s) Adding pair: {first} + {second} = {result}", end="\r")
|
|
|
| src.close()
|
| print('\nCommiting...')
|
| conn.commit()
|
| print(f"Finished merging {dbfile}, total so far: {count:,}")
|
| return count
|
|
|
|
|
| def merge_multiple(files):
|
| with connect() as conn:
|
| init_db(conn)
|
| cache = ElementCache(conn)
|
|
|
| count = 0
|
| for f in files:
|
| count = merge_one_file(f, conn, cache, start_count=count)
|
|
|
| print(f"\n✅ All done! {count:,} pairs merged.")
|
|
|
|
|
| if __name__ == "__main__":
|
| files = [
|
| "cache (2).sqlite",
|
| "cache (3).sqlite",
|
| "cache (4).sqlite",
|
| "cache.sqlite",
|
| "expitau_test.sqlite",
|
| "infinite_craft - Copy.sqlite",
|
| "infinite_craft.sqlite",
|
| ]
|
| merge_multiple(files)
|
|
|