From 5acaeaa21ef72afb600f21bbffdc30e56db3b20e Mon Sep 17 00:00:00 2001 From: Anton Date: Sat, 4 May 2024 01:24:50 +0500 Subject: [PATCH] refactor: sqlx - rename key_id to kid --- internal/rhash/tx.go | 20 ++++++------- internal/rlist/tx.go | 64 ++++++++++++++++++++-------------------- internal/rstring/tx.go | 12 ++++---- internal/rzset/delete.go | 4 +-- internal/rzset/inter.go | 10 +++---- internal/rzset/range.go | 6 ++-- internal/rzset/tx.go | 24 +++++++-------- internal/rzset/union.go | 6 ++-- internal/sqlx/schema.sql | 56 +++++++++++++++++------------------ 9 files changed, 101 insertions(+), 101 deletions(-) diff --git a/internal/rhash/tx.go b/internal/rhash/tx.go index 480ed8f..67fbae9 100644 --- a/internal/rhash/tx.go +++ b/internal/rhash/tx.go @@ -11,12 +11,12 @@ import ( const ( sqlCount = ` select count(field) - from rhash join rkey on key_id = rkey.id and type = 4 + from rhash join rkey on kid = rkey.id and type = 4 where key = ? and (etime is null or etime > ?) and field in (:fields)` sqlDelete1 = ` delete from rhash - where key_id = ( + where kid = ( select id from rkey where key = ? and type = 4 and (etime is null or etime > ?) ) and field in (:fields)` @@ -30,22 +30,22 @@ const ( sqlFields = ` select field - from rhash join rkey on key_id = rkey.id and type = 4 + from rhash join rkey on kid = rkey.id and type = 4 where key = ? and (etime is null or etime > ?)` sqlGet = ` select value - from rhash join rkey on key_id = rkey.id and type = 4 + from rhash join rkey on kid = rkey.id and type = 4 where key = ? and (etime is null or etime > ?) and field = ?` sqlGetMany = ` select field, value - from rhash join rkey on key_id = rkey.id and type = 4 + from rhash join rkey on kid = rkey.id and type = 4 where key = ? and (etime is null or etime > ?) and field in (:fields)` sqlItems = ` select field, value - from rhash join rkey on key_id = rkey.id and type = 4 + from rhash join rkey on kid = rkey.id and type = 4 where key = ? and (etime is null or etime > ?)` sqlLen = ` @@ -54,7 +54,7 @@ const ( sqlScan = ` select rhash.rowid, field, value - from rhash join rkey on key_id = rkey.id and type = 4 + from rhash join rkey on kid = rkey.id and type = 4 where key = ? and (etime is null or etime > ?) and rhash.rowid > ? and field glob ? @@ -71,14 +71,14 @@ const ( returning id` sqlSet2 = ` - insert into rhash (key_id, field, value) + insert into rhash (kid, field, value) values (?, ?, ?) - on conflict (key_id, field) do update + on conflict (kid, field) do update set value = excluded.value;` sqlValues = ` select value - from rhash join rkey on key_id = rkey.id and type = 4 + from rhash join rkey on kid = rkey.id and type = 4 where key = ? and (etime is null or etime > ?)` ) diff --git a/internal/rlist/tx.go b/internal/rlist/tx.go index 07e56c8..7eebb2c 100644 --- a/internal/rlist/tx.go +++ b/internal/rlist/tx.go @@ -12,7 +12,7 @@ import ( const ( sqlDelete = ` delete from rlist - where key_id = ( + where kid = ( select id from rkey where key = ? and type = 2 and (etime is null or etime > ?) ) and elem = ?` @@ -20,7 +20,7 @@ const ( sqlDeleteBack = ` with ids as ( select rlist.rowid - from rlist join rkey on key_id = rkey.id and type = 2 + from rlist join rkey on kid = rkey.id and type = 2 where key = ? and (etime is null or etime > ?) and elem = ? order by pos desc limit ? @@ -31,7 +31,7 @@ const ( sqlDeleteFront = ` with ids as ( select rlist.rowid - from rlist join rkey on key_id = rkey.id and type = 2 + from rlist join rkey on kid = rkey.id and type = 2 where key = ? and (etime is null or etime > ?) and elem = ? order by pos limit ? @@ -42,7 +42,7 @@ const ( sqlGet = ` with elems as ( select elem, row_number() over (order by pos asc) as rownum - from rlist join rkey on key_id = rkey.id and type = 2 + from rlist join rkey on kid = rkey.id and type = 2 where key = ? and (etime is null or etime > ?) ) select elem @@ -60,11 +60,11 @@ const ( sqlInsertAfter = ` with elprev as ( select min(pos) as pos from rlist - where key_id = ? and elem = ? + where kid = ? and elem = ? ), elnext as ( select min(pos) as pos from rlist - where key_id = ? and pos > (select pos from elprev) + where kid = ? and pos > (select pos from elprev) ), newpos as ( select @@ -74,20 +74,20 @@ const ( end as pos from elprev, elnext ) - insert into rlist (key_id, pos, elem) + insert into rlist (kid, pos, elem) select ?, (select pos from newpos), ? from rlist - where key_id = ? + where kid = ? limit 1` sqlInsertBefore = ` with elnext as ( select min(pos) as pos from rlist - where key_id = ? and elem = ? + where kid = ? and elem = ? ), elprev as ( select max(pos) as pos from rlist - where key_id = ? and pos < (select pos from elnext) + where kid = ? and pos < (select pos from elnext) ), newpos as ( select @@ -97,10 +97,10 @@ const ( end as pos from elprev, elnext ) - insert into rlist (key_id, pos, elem) + insert into rlist (kid, pos, elem) select ?, (select pos from newpos), ? from rlist - where key_id = ? + where kid = ? limit 1` sqlLen = ` @@ -108,30 +108,30 @@ const ( where key = ? and type = 2 and (etime is null or etime > ?)` sqlPopBack = ` - with keyid as ( + with curkey as ( select id from rkey where key = ? and type = 2 and (etime is null or etime > ?) ) delete from rlist where - key_id = (select id from keyid) + kid = (select id from curkey) and pos = ( select max(pos) from rlist - where key_id = (select id from keyid) + where kid = (select id from curkey) ) returning elem` sqlPopFront = ` - with keyid as ( + with curkey as ( select id from rkey where key = ? and type = 2 and (etime is null or etime > ?) ) delete from rlist where - key_id = (select id from keyid) + kid = (select id from curkey) and pos = ( select min(pos) from rlist - where key_id = (select id from keyid) + where kid = (select id from curkey) ) returning elem` @@ -147,25 +147,25 @@ const ( returning id, len` sqlPushBack = ` - insert into rlist (key_id, pos, elem) + insert into rlist (kid, pos, elem) select ?, coalesce(max(pos)+1, 0), ? from rlist - where key_id = ?` + where kid = ?` sqlPushFront = ` - insert into rlist (key_id, pos, elem) + insert into rlist (kid, pos, elem) select ?, coalesce(min(pos)-1, 0), ? from rlist - where key_id = ?` + where kid = ?` sqlRange = ` - with keyid as ( + with curkey as ( select id from rkey where key = ? and type = 2 and (etime is null or etime > ?) ), counts as ( select len from rkey - where id = (select id from keyid) + where id = (select id from curkey) ), bounds as ( select @@ -180,34 +180,34 @@ const ( ) select elem from rlist - where key_id = (select id from keyid) + where kid = (select id from curkey) order by pos limit (select start from bounds), ((select stop from bounds) - (select start from bounds) + 1)` sqlSet = ` - with keyid as ( + with curkey as ( select id from rkey where key = ? and type = 2 and (etime is null or etime > ?) ), elems as ( select pos, row_number() over (order by pos asc) as rownum from rlist - where key_id = (select id from keyid) + where kid = (select id from curkey) ) update rlist set elem = ? - where key_id = (select id from keyid) + where kid = (select id from curkey) and pos = (select pos from elems where rownum = ? + 1)` sqlTrim = ` - with keyid as ( + with curkey as ( select id from rkey where key = ? and type = 2 and (etime is null or etime > ?) ), counts as ( select len from rkey - where id = (select id from keyid) + where id = (select id from curkey) ), bounds as ( select @@ -222,7 +222,7 @@ const ( ), remain as ( select rowid from rlist - where key_id = (select id from keyid) + where kid = (select id from curkey) order by pos limit (select start from bounds), @@ -230,7 +230,7 @@ const ( ) delete from rlist where - key_id = (select id from keyid) + kid = (select id from curkey) and rowid not in (select rowid from remain)` ) diff --git a/internal/rstring/tx.go b/internal/rstring/tx.go index 4a28aac..42bb26d 100644 --- a/internal/rstring/tx.go +++ b/internal/rstring/tx.go @@ -11,13 +11,13 @@ import ( const ( sqlGet = ` select value - from rstring join rkey on key_id = rkey.id and type = 1 + from rstring join rkey on kid = rkey.id and type = 1 where key = ? and (etime is null or etime > ?)` sqlGetMany = ` select key, value from rstring - join rkey on key_id = rkey.id and type = 1 + join rkey on kid = rkey.id and type = 1 where key in (:keys) and (etime is null or etime > ?)` sqlSet1 = ` @@ -31,9 +31,9 @@ const ( returning id` sqlSet2 = ` - insert into rstring (key_id, value) + insert into rstring (kid, value) values (?, ?) - on conflict (key_id) do update + on conflict (kid) do update set value = excluded.value` sqlUpdate1 = ` @@ -46,9 +46,9 @@ const ( returning id` sqlUpdate2 = ` - insert into rstring (key_id, value) + insert into rstring (kid, value) values (?, ?) - on conflict (key_id) do update + on conflict (kid) do update set value = excluded.value` ) diff --git a/internal/rzset/delete.go b/internal/rzset/delete.go index b29a77f..3349530 100644 --- a/internal/rzset/delete.go +++ b/internal/rzset/delete.go @@ -11,7 +11,7 @@ const ( with ranked as ( select rowid, elem, score from rzset - where key_id = ( + where kid = ( select id from rkey where key = ? and type = 5 and (etime is null or etime > ?) ) @@ -23,7 +23,7 @@ const ( sqlDeleteScore = ` delete from rzset - where key_id = ( + where kid = ( select id from rkey where key = ? and type = 5 and (etime is null or etime > ?) ) and score between ? and ?` diff --git a/internal/rzset/inter.go b/internal/rzset/inter.go index 9c5e057..3430de3 100644 --- a/internal/rzset/inter.go +++ b/internal/rzset/inter.go @@ -12,10 +12,10 @@ import ( const ( sqlInter = ` select elem, sum(score) as score - from rzset join rkey on key_id = rkey.id and type = 5 + from rzset join rkey on kid = rkey.id and type = 5 where key in (:keys) and (etime is null or etime > ?) group by elem - having count(distinct key_id) = ? + having count(distinct kid) = ? order by sum(score), elem` sqlInterStore1 = sqlDeleteAll @@ -23,12 +23,12 @@ const ( sqlInterStore2 = sqlAdd1 sqlInterStore3 = ` - insert into rzset (key_id, elem, score) + insert into rzset (kid, elem, score) select ?, elem, sum(score) as score - from rzset join rkey on key_id = rkey.id and type = 5 + from rzset join rkey on kid = rkey.id and type = 5 where key in (:keys) and (etime is null or etime > ?) group by elem - having count(distinct key_id) = ? + having count(distinct kid) = ? order by sum(score), elem;` ) diff --git a/internal/rzset/range.go b/internal/rzset/range.go index 706aa48..c7dc6de 100644 --- a/internal/rzset/range.go +++ b/internal/rzset/range.go @@ -11,9 +11,9 @@ const ( sqlRangeRank = ` with ranked as ( select elem, score, (row_number() over w - 1) as rank - from rzset join rkey on key_id = rkey.id and type = 5 + from rzset join rkey on kid = rkey.id and type = 5 where key = ? and (etime is null or etime > ?) - window w as (partition by key_id order by score asc, elem asc) + window w as (partition by kid order by score asc, elem asc) ) select elem, score from ranked @@ -22,7 +22,7 @@ const ( sqlRangeScore = ` select elem, score - from rzset join rkey on key_id = rkey.id and type = 5 + from rzset join rkey on kid = rkey.id and type = 5 where key = ? and (etime is null or etime > ?) and score between ? and ? order by score asc, elem asc` diff --git a/internal/rzset/tx.go b/internal/rzset/tx.go index 021a766..df823c2 100644 --- a/internal/rzset/tx.go +++ b/internal/rzset/tx.go @@ -21,24 +21,24 @@ const ( returning id` sqlAdd2 = ` - insert into rzset (key_id, elem, score) + insert into rzset (kid, elem, score) values (?, ?, ?) - on conflict (key_id, elem) do update + on conflict (kid, elem) do update set score = excluded.score` sqlCount = ` select count(elem) - from rzset join rkey on key_id = rkey.id and type = 5 + from rzset join rkey on kid = rkey.id and type = 5 where key = ? and (etime is null or etime > ?) and elem in (:elems)` sqlCountScore = ` select count(elem) - from rzset join rkey on key_id = rkey.id and type = 5 + from rzset join rkey on kid = rkey.id and type = 5 where key = ? and (etime is null or etime > ?) and score between ? and ?` sqlDelete1 = ` delete from rzset - where key_id = ( + where kid = ( select id from rkey where key = ? and type = 5 and (etime is null or etime > ?) ) and elem in (:elems)` @@ -52,7 +52,7 @@ const ( sqlDeleteAll = ` delete from rzset - where key_id = ( + where kid = ( select id from rkey where key = ? and type = 5 and (etime is null or etime > ?) ); @@ -66,9 +66,9 @@ const ( sqlGetRank = ` with ranked as ( select elem, score, (row_number() over w - 1) as rank - from rzset join rkey on key_id = rkey.id and type = 5 + from rzset join rkey on kid = rkey.id and type = 5 where key = ? and (etime is null or etime > ?) - window w as (partition by key_id order by score asc, elem asc) + window w as (partition by kid order by score asc, elem asc) ) select rank, score from ranked @@ -76,15 +76,15 @@ const ( sqlGetScore = ` select score - from rzset join rkey on key_id = rkey.id and type = 5 + from rzset join rkey on kid = rkey.id and type = 5 where key = ? and (etime is null or etime > ?) and elem = ?` sqlIncr1 = sqlAdd1 sqlIncr2 = ` - insert into rzset (key_id, elem, score) + insert into rzset (kid, elem, score) values (?, ?, ?) - on conflict (key_id, elem) do update + on conflict (kid, elem) do update set score = score + excluded.score returning score` @@ -94,7 +94,7 @@ const ( sqlScan = ` select rzset.rowid, elem, score - from rzset join rkey on key_id = rkey.id and type = 5 + from rzset join rkey on kid = rkey.id and type = 5 where key = ? and (etime is null or etime > ?) and rzset.rowid > ? and elem glob ? diff --git a/internal/rzset/union.go b/internal/rzset/union.go index 93c0a35..f7caf2a 100644 --- a/internal/rzset/union.go +++ b/internal/rzset/union.go @@ -12,7 +12,7 @@ import ( const ( sqlUnion = ` select elem, sum(score) as score - from rzset join rkey on key_id = rkey.id and type = 5 + from rzset join rkey on kid = rkey.id and type = 5 where key in (:keys) and (etime is null or etime > ?) group by elem order by sum(score), elem` @@ -22,9 +22,9 @@ const ( sqlUnionStore2 = sqlAdd1 sqlUnionStore3 = ` - insert into rzset (key_id, elem, score) + insert into rzset (kid, elem, score) select ?, elem, sum(score) as score - from rzset join rkey on key_id = rkey.id and type = 5 + from rzset join rkey on kid = rkey.id and type = 5 where key in (:keys) and (etime is null or etime > ?) group by elem order by sum(score), elem;` diff --git a/internal/sqlx/schema.sql b/internal/sqlx/schema.sql index b7828a3..c0ac31a 100644 --- a/internal/sqlx/schema.sql +++ b/internal/sqlx/schema.sql @@ -32,23 +32,23 @@ where etime is not null; -- └───────────────┘ create table if not exists rstring ( - key_id integer not null, + kid integer not null, value blob not null, - foreign key (key_id) references rkey (id) + foreign key (kid) references rkey (id) on delete cascade ) strict; create unique index if not exists -rstring_pk_idx on rstring (key_id); +rstring_pk_idx on rstring (kid); create view if not exists vstring as select - rkey.id as key_id, rkey.key, rstring.value, + rkey.id as kid, rkey.key, rstring.value, datetime(etime/1000, 'unixepoch') as etime, datetime(mtime/1000, 'unixepoch') as mtime -from rkey join rstring on rkey.id = rstring.key_id +from rkey join rstring on rkey.id = rstring.kid where rkey.type = 1 and (rkey.etime is null or rkey.etime > unixepoch('subsec')); @@ -57,16 +57,16 @@ where rkey.type = 1 -- └───────────────┘ create table if not exists rlist ( - key_id integer not null, + kid integer not null, pos real not null, elem blob not null, - foreign key (key_id) references rkey (id) + foreign key (kid) references rkey (id) on delete cascade ) strict; create unique index if not exists -rlist_pk_idx on rlist (key_id, pos); +rlist_pk_idx on rlist (kid, pos); create trigger if not exists rlist_on_update @@ -76,7 +76,7 @@ begin update rkey set version = version + 1, mtime = unixepoch('subsec') * 1000 - where id = old.key_id; + where id = old.kid; end; create trigger if not exists @@ -88,36 +88,36 @@ begin version = version + 1, mtime = unixepoch('subsec') * 1000, len = len - 1 - where id = old.key_id; + where id = old.kid; end; create view if not exists vlist as select - rkey.id as key_id, rkey.key, + rkey.id as kid, rkey.key, row_number() over w as idx, rlist.elem, datetime(etime/1000, 'unixepoch') as etime, datetime(mtime/1000, 'unixepoch') as mtime -from rkey join rlist on rkey.id = rlist.key_id +from rkey join rlist on rkey.id = rlist.kid where rkey.type = 2 and (rkey.etime is null or rkey.etime > unixepoch('subsec')) -window w as (partition by key_id order by pos); +window w as (partition by kid order by pos); -- ┌───────────────┐ -- │ Hashes │ -- └───────────────┘ create table if not exists rhash ( - key_id integer not null, + kid integer not null, field text not null, value blob not null, - foreign key (key_id) references rkey (id) + foreign key (kid) references rkey (id) on delete cascade ) strict; create unique index if not exists -rhash_pk_idx on rhash (key_id, field); +rhash_pk_idx on rhash (kid, field); create trigger if not exists rhash_on_insert @@ -125,21 +125,21 @@ before insert on rhash for each row when ( select count(*) from rhash - where key_id = new.key_id and field = new.field + where kid = new.kid and field = new.field ) = 0 begin update rkey set len = len + 1 - where id = new.key_id; + where id = new.kid; end; create view if not exists vhash as select - rkey.id as key_id, rkey.key, rhash.field, rhash.value, + rkey.id as kid, rkey.key, rhash.field, rhash.value, datetime(etime/1000, 'unixepoch') as etime, datetime(mtime/1000, 'unixepoch') as mtime -from rkey join rhash on rkey.id = rhash.key_id +from rkey join rhash on rkey.id = rhash.kid where rkey.type = 4 and (rkey.etime is null or rkey.etime > unixepoch('subsec')); @@ -148,19 +148,19 @@ where rkey.type = 4 -- └───────────────┘ create table if not exists rzset ( - key_id integer not null, + kid integer not null, elem blob not null, score real not null, - foreign key (key_id) references rkey (id) + foreign key (kid) references rkey (id) on delete cascade ) strict; create unique index if not exists -rzset_pk_idx on rzset (key_id, elem); +rzset_pk_idx on rzset (kid, elem); create index if not exists -rzset_score_idx on rzset (key_id, score, elem); +rzset_score_idx on rzset (kid, score, elem); create trigger if not exists rzset_on_insert @@ -168,20 +168,20 @@ before insert on rzset for each row when ( select count(*) from rzset - where key_id = new.key_id and elem = new.elem + where kid = new.kid and elem = new.elem ) = 0 begin update rkey set len = len + 1 - where id = new.key_id; + where id = new.kid; end; create view if not exists vzset as select - rkey.id as key_id, rkey.key, rzset.elem, rzset.score, + rkey.id as kid, rkey.key, rzset.elem, rzset.score, datetime(etime/1000, 'unixepoch') as etime, datetime(mtime/1000, 'unixepoch') as mtime -from rkey join rzset on rkey.id = rzset.key_id +from rkey join rzset on rkey.id = rzset.kid where rkey.type = 5 and (rkey.etime is null or rkey.etime > unixepoch('subsec'));