Skip to content

Commit

Permalink
Updated crstore to v19
Browse files Browse the repository at this point in the history
  • Loading branch information
Azarattum committed Jan 5, 2024
1 parent 16c98c3 commit e9b414f
Show file tree
Hide file tree
Showing 16 changed files with 309 additions and 130 deletions.
2 changes: 1 addition & 1 deletion apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
"devDependencies": {
"@sveltejs/adapter-static": "2.0.3",
"crstore": "^0.18.0",
"crstore": "^0.19.3",
"murmurhash": "^2.0.1"
}
}
6 changes: 3 additions & 3 deletions packages/crdata/operations/cte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ const source = (qc: QueryCreator<Schema>) =>
.selectFrom("sources")
.select(["owner", "source"])
.orderBy("sources.primary", "desc")
.as("ordered")
.as("ordered"),
)
.select(["owner", (qb) => group(qb, "source").as("sources")])
.groupBy("owner")
.$castTo<SourceCTE["source"]>();

const asset = (qc: QueryCreator<Schema>) =>
const asset = (qc: QueryCreator<Schema & SourceCTE>) =>
qc
.selectFrom((qb) =>
qb
.selectFrom("assets")
.select(["owner", "art", "thumbnail"])
.orderBy("assets.primary", "desc")
.as("ordered")
.as("ordered"),
)
.select([
"owner",
Expand Down
14 changes: 8 additions & 6 deletions packages/crdata/operations/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { ExpressionBuilder } from "crstore";
import type { Schema } from "../data/schema";
import { sql } from "crstore";

const localDevice = sql<Uint8Array>`crsql_siteid()`;
const localDevice = sql<Uint8Array>`crsql_site_id()`;
const uuid = () => (Math.random() * 2 ** 32) >>> 0;
const sanitize = (query: string) =>
query
Expand All @@ -26,7 +26,7 @@ const position = {
.select(sql`position + 1`.as("position"))
.where("id", "=", id),
"=",
"position"
"position",
)
.limit(1),
next: (qb: ExpressionBuilder<Schema, "playback">) =>
Expand All @@ -51,13 +51,15 @@ const position = {
.select(["id", "position"])
.$if(!!ids.length, (qb) =>
qb.unionAll(
sql`VALUES ${sql.raw(ids.map((x) => `(${x}, 1)`).join(","))}`
)
sql<any>`VALUES ${sql.raw(
ids.map((x) => `(${x}, 1)`).join(","),
)}`,
),
)
.unionAll(
sql`SELECT null,1 WHERE NOT EXISTS (SELECT 1 FROM queue WHERE position >= 0)`
sql<any>`SELECT null,1 WHERE NOT EXISTS (SELECT 1 FROM queue WHERE position >= 0)`,
)
.as("data")
.as("data"),
)
.select("id")
.where("position", ">=", 0)
Expand Down
2 changes: 1 addition & 1 deletion packages/crdata/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"dependencies": {
"@amadeus-music/protocol": "workspace:*",
"crstore": "^0.18.0",
"crstore": "^0.19.3",
"superstruct": "^1.0.3"
}
}
16 changes: 8 additions & 8 deletions packages/crdata/sql/playback.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
INSERT OR IGNORE INTO devices VALUES (crsql_siteid(), -1, 0, 0, 0, 0);
INSERT OR IGNORE INTO devices VALUES (crsql_site_id(), -1, 0, 0, 0, 0);

CREATE VIEW IF NOT EXISTS queue AS
WITH ordered AS
Expand All @@ -9,7 +9,7 @@ CREATE VIEW IF NOT EXISTS queue AS
CASE WHEN "direction" != 1 THEN "playback"."id" ELSE NULL END ASC,
CASE WHEN "direction" = 1 THEN "playback"."id" ELSE NULL END DESC
) as position FROM devices INNER JOIN playback ON playback.device = devices.id
WHERE devices.id = crsql_siteid())
WHERE devices.id = crsql_site_id())
SELECT id, device, track,
(position -
(SELECT position FROM ordered WHERE id = (SELECT playback FROM devices WHERE id = device))
Expand All @@ -19,15 +19,15 @@ CREATE VIEW IF NOT EXISTS queue AS
CREATE TRIGGER IF NOT EXISTS playback_started
BEFORE INSERT ON playback
FOR EACH ROW
WHEN NEW.device = crsql_siteid()
WHEN NEW.device = crsql_site_id()
BEGIN
UPDATE devices SET playback = NEW.id WHERE id = NEW.device AND playback IS -1;
END;

CREATE TRIGGER IF NOT EXISTS playback_finised
BEFORE UPDATE OF progress ON devices
FOR EACH ROW
WHEN NEW.progress >= 1 AND NEW.id = crsql_siteid()
WHEN NEW.progress >= 1 AND NEW.id = crsql_site_id()
BEGIN
INSERT INTO library SELECT ABS(RANDOM() % 4294967296), -1, track, UNIXEPOCH(), -1 FROM playback WHERE id = NEW.playback;
UPDATE devices SET
Expand All @@ -44,7 +44,7 @@ END;
CREATE TRIGGER IF NOT EXISTS playback_backtracked
BEFORE UPDATE OF progress ON devices
FOR EACH ROW
WHEN NEW.progress < 0 AND NEW.id = crsql_siteid()
WHEN NEW.progress < 0 AND NEW.id = crsql_site_id()
BEGIN
UPDATE devices SET
playback = IFNULL((SELECT id FROM queue WHERE position = -1), playback),
Expand All @@ -56,7 +56,7 @@ END;
CREATE TRIGGER IF NOT EXISTS playback_shuffled
AFTER UPDATE OF direction ON devices
FOR EACH ROW
WHEN NEW.direction = 2 AND NEW.id = crsql_siteid()
WHEN NEW.direction = 2 AND NEW.id = crsql_site_id()
BEGIN
UPDATE playback SET "temp"="order" WHERE
(SELECT id FROM queue WHERE position = 0) != id AND device = NEW.id;
Expand All @@ -68,7 +68,7 @@ END;
CREATE TRIGGER IF NOT EXISTS playback_unshuffled
AFTER UPDATE OF direction ON devices
FOR EACH ROW
WHEN OLD.direction = 2 AND NEW.direction != 2 AND NEW.id = crsql_siteid()
WHEN OLD.direction = 2 AND NEW.direction != 2 AND NEW.id = crsql_site_id()
BEGIN
UPDATE playback SET
"order"="temp",
Expand All @@ -79,7 +79,7 @@ END;
CREATE TRIGGER IF NOT EXISTS playback_purged
BEFORE DELETE ON playback
FOR EACH ROW
WHEN (OLD.id = (SELECT playback FROM devices WHERE id = crsql_siteid()))
WHEN (OLD.id = (SELECT playback FROM devices WHERE id = crsql_site_id()))
BEGIN
UPDATE devices SET
playback = (SELECT id FROM queue WHERE position = 1),
Expand Down
24 changes: 12 additions & 12 deletions packages/crdata/stores/playback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ export const preceding = ({ store }: DB) =>
.with("update", (qb) =>
qb
.selectFrom("playback") // ↓ We need this to trigger updates from `playback` & `devices`
.select((qb) => qb.selectFrom("devices").select("id").as("_"))
.select((qb) => qb.selectFrom("devices").select("id").as("_")),
)
.selectFrom("queue")
.innerJoin("track", "track.id", "queue.track")
.where("position", "<", 0)
.select("queue.id as entry")
.select(fields)
.$castTo<Track & { entry: number }>()
.$castTo<Track & { entry: number }>(),
);

export const upcoming = ({ store }: DB) =>
Expand All @@ -41,14 +41,14 @@ export const upcoming = ({ store }: DB) =>
.with("update", (qb) =>
qb
.selectFrom("playback") // ↓ We need this to trigger updates from `playback` & `devices`
.select((qb) => qb.selectFrom("devices").select("id").as("_"))
.select((qb) => qb.selectFrom("devices").select("id").as("_")),
)
.selectFrom("queue")
.innerJoin("track", "track.id", "queue.track")
.where("position", ">", 0)
.select("queue.id as entry")
.select(fields)
.$castTo<Track & { entry: number }>()
.$castTo<Track & { entry: number }>(),
);

export const playback = ({ store }: DB) =>
Expand All @@ -73,16 +73,16 @@ export const playback = ({ store }: DB) =>
album: "track.album",
artists: "track.artists",
sources: "track.sources",
}).as("track")
}).as("track"),
)
.select(sql<boolean>`device = crsql_siteid()`.as("local"))
.select(sql<boolean>`device = crsql_site_id()`.as("local"))
.select(fields)
.$castTo<Playback>(),
{
async push(
db,
tracks: Track[],
at: "first" | "next" | "last" | "random" | number = "next"
at: "first" | "next" | "last" | "random" | number = "next",
) {
if (!tracks.length) return;
await pushTracks(db, tracks);
Expand Down Expand Up @@ -123,7 +123,7 @@ export const playback = ({ store }: DB) =>
: i > 0 && direction != 1
? ids[i - 1]
: order,
}))
})),
)
.execute();
if (!~playback && at === "random") {
Expand Down Expand Up @@ -245,20 +245,20 @@ export const playback = ({ store }: DB) =>
.whereRef("id", "=", "playback.device")
.select("devices.playback"),
"=",
qb.ref("playback.id")
qb.ref("playback.id"),
)
.then(id)
.else(sql`ABS(RANDOM() % 4294967296)`)
.end()
.as("id")
.as("id"),
)
.select(sql`${localDevice}`.as("device"))
.select(["track", "order", "temp"])
.where("device", "=", device)
.where("device", "=", device),
)
.execute();
},
}
},
);

const fields = [
Expand Down
Binary file modified plugins/sqlite/assets/arm64/crsqlite.dylib
Binary file not shown.
Binary file modified plugins/sqlite/assets/arm64/sqlite.darwin.node
Binary file not shown.
Binary file modified plugins/sqlite/assets/x64/crsqlite.dll
100644 → 100755
Binary file not shown.
Binary file added plugins/sqlite/assets/x64/crsqlite.dylib
Binary file not shown.
Binary file modified plugins/sqlite/assets/x64/crsqlite.so
100644 → 100755
Binary file not shown.
Binary file modified plugins/sqlite/assets/x64/sqlite.linux.node
Binary file not shown.
Binary file modified plugins/sqlite/assets/x64/sqlite.win32.node
Binary file not shown.
8 changes: 4 additions & 4 deletions plugins/telegram/handlers/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ function handleChanges(user: string) {
}

function parseChanges(changes: any) {
let target: string | null = null;
let target: string | undefined;
let consequent = 0;
let last = "";

const library = new Map<string, Set<number>>();

for (const { table, cid, val, pk } of decode(changes)) {
if (table !== "library") continue;
if (last === pk) consequent++;
if (last === pk.toString()) consequent++;
else consequent = 1;
last = pk;
if (cid === "playlist") target = val;
last = pk.toString();
if (cid === "playlist") target = val?.toString();
// 4 is the number of columns other than the primary one
if (consequent !== 4 || !target) continue;
const entry = +(pk || NaN);
Expand Down
2 changes: 1 addition & 1 deletion plugins/telegram/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"@amadeus-music/core": "workspace:*",
"@amadeus-music/protocol": "workspace:*",
"@amadeus-music/util": "workspace:*",
"crstore": "^0.18.0"
"crstore": "^0.19.3"
},
"devDependencies": {
"vite": "4.5.0"
Expand Down
Loading

0 comments on commit e9b414f

Please sign in to comment.