modify cache db schema

This commit is contained in:
julian 2024-05-30 17:17:35 -06:00
parent e5d8dff6cd
commit 0454c88c4a

View file

@ -177,6 +177,7 @@ abstract class _FiroCache {
blockHash TEXT NOT NULL, blockHash TEXT NOT NULL,
setHash TEXT NOT NULL, setHash TEXT NOT NULL,
groupId INTEGER NOT NULL, groupId INTEGER NOT NULL,
timestampUTC INTEGER NOT NULL,
UNIQUE (blockHash, setHash, groupId) UNIQUE (blockHash, setHash, groupId)
); );
@ -190,7 +191,6 @@ abstract class _FiroCache {
CREATE TABLE SparkSetCoins ( CREATE TABLE SparkSetCoins (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
timestampUTC INTEGER NOT NULL,
setId INTEGER NOT NULL, setId INTEGER NOT NULL,
coinId INTEGER NOT NULL, coinId INTEGER NOT NULL,
FOREIGN KEY (setId) REFERENCES SparkSet(id), FOREIGN KEY (setId) REFERENCES SparkSet(id),
@ -215,15 +215,15 @@ abstract class _FiroCache {
int? newerThanTimeStamp, int? newerThanTimeStamp,
}) async { }) async {
String query = """ String query = """
SELECT sc.id, sc.serialized, sc.txHash, sc.context SELECT sc.serialized, sc.txHash, sc.context
FROM SparkSetCoins AS ssc FROM SparkSet AS ss
JOIN SparkSet AS ss ON ssc.setId = ss.id JOIN SparkSetCoins AS ssc ON ss.id = ssc.setId
JOIN SparkCoin AS sc ON ssc.coinId = sc.id JOIN SparkCoin AS sc ON ssc.coinId = sc.id
WHERE ss.groupId = $groupId WHERE ss.groupId = $groupId
"""; """;
if (newerThanTimeStamp != null) { if (newerThanTimeStamp != null) {
query += " AND ssc.timestampUTC" query += " AND ss.timestampUTC"
" > $newerThanTimeStamp"; " > $newerThanTimeStamp";
} }
@ -234,11 +234,10 @@ abstract class _FiroCache {
int groupId, int groupId,
) async { ) async {
final query = """ final query = """
SELECT ss.blockHash, ss.setHash, ssc.timestampUTC SELECT ss.blockHash, ss.setHash, ss.timestampUTC
FROM SparkSet ss FROM SparkSet ss
JOIN SparkSetCoins ssc ON ss.id = ssc.setId
WHERE ss.groupId = $groupId WHERE ss.groupId = $groupId
ORDER BY ssc.timestampUTC DESC ORDER BY ss.timestampUTC DESC
LIMIT 1; LIMIT 1;
"""; """;
@ -479,16 +478,17 @@ abstract class _FiroCache {
try { try {
db.execute( db.execute(
""" """
INSERT INTO SparkSet (blockHash, setHash, groupId) INSERT INTO SparkSet (blockHash, setHash, groupId, timestampUTC)
VALUES (?, ?, ?); VALUES (?, ?, ?, ?);
""", """,
[blockHash, setHash, groupId], [blockHash, setHash, groupId, timestamp],
); );
final setId = db.lastInsertRowId; final setId = db.lastInsertRowId;
for (final coin in coins) { for (final coin in coins) {
int coinId; int coinId;
try { try {
// try to insert and get row id
db.execute( db.execute(
""" """
INSERT INTO SparkCoin (serialized, txHash, context) INSERT INTO SparkCoin (serialized, txHash, context)
@ -498,6 +498,8 @@ abstract class _FiroCache {
); );
coinId = db.lastInsertRowId; coinId = db.lastInsertRowId;
} on SqliteException catch (e) { } on SqliteException catch (e) {
// if there already is a matching coin in the db
// just grab its row id
if (e.extendedResultCode == 2067) { if (e.extendedResultCode == 2067) {
final result = db.select( final result = db.select(
""" """
@ -513,12 +515,13 @@ abstract class _FiroCache {
} }
} }
// finally add the row id to the newly added set
db.execute( db.execute(
""" """
INSERT INTO SparkSetCoins (timestampUTC, setId, coinId) INSERT INTO SparkSetCoins (setId, coinId)
VALUES (?, ?, ?); VALUES (?, ?);
""", """,
[timestamp, setId, coinId], [setId, coinId],
); );
} }