refactor: rzset - do not use rkey.DeleteType

This commit is contained in:
Anton
2024-04-28 07:21:01 +05:00
parent 4e8c59f5cf
commit c99c7e048d
3 changed files with 28 additions and 35 deletions

View File

@@ -38,11 +38,6 @@ const (
limit :n
)`
sqlDeleteType = `
delete from rkey where key in (:keys)
and (etime is null or etime > :now)
and type = :type`
sqlExpire = `
update rkey set etime = :at
where key = :key and (etime is null or etime > :now)`
@@ -349,20 +344,6 @@ func CountType(tx sqlx.Tx, typ core.TypeID, keys ...string) (int, error) {
return count, err
}
// DeleteType deletes keys of a specific type.
// Returns the number of deleted keys.
// Non-existing keys and keys of other types are ignored.
func DeleteType(tx sqlx.Tx, typ core.TypeID, keys ...string) (int, error) {
query, keyArgs := sqlx.ExpandIn(sqlDeleteType, ":keys", keys)
args := append(keyArgs, time.Now().UnixMilli(), typ)
res, err := tx.Exec(query, args...)
if err != nil {
return 0, err
}
affectedCount, _ := res.RowsAffected()
return int(affectedCount), nil
}
// Get returns the key data structure.
func Get(tx sqlx.Tx, key string) (core.Key, error) {
args := []any{key, time.Now().UnixMilli()}

View File

@@ -7,7 +7,6 @@ import (
"time"
"github.com/nalgeon/redka/internal/core"
"github.com/nalgeon/redka/internal/rkey"
"github.com/nalgeon/redka/internal/sqlx"
)
@@ -22,8 +21,18 @@ const (
order by sum(score), elem`
sqlInterStore = `
delete from rzset
where key_id = (
select id from rkey where key = :key
and (etime is null or etime > :now)
);
insert into rkey (key, type, version, mtime)
values (:key, :type, :version, :mtime);
values (:key, :type, :version, :mtime)
on conflict (key) do update set
version = version+1,
type = excluded.type,
mtime = excluded.mtime;
insert into rzset (key_id, elem, score)
select
@@ -147,15 +156,12 @@ func (c InterCmd) inter(tx sqlx.Tx) ([]SetItem, error) {
// store intersects multiple sets and stores the result in a new set.
func (c InterCmd) store(tx sqlx.Tx) (int, error) {
// Delete the destination key if it exists.
_, err := rkey.DeleteType(tx, core.TypeSortedSet, c.dest)
if err != nil {
return 0, err
}
// Insert the destination key and get its ID.
now := time.Now().UnixMilli()
args := []any{
// delete from rzset
c.dest, // key
now, // now
// insert into rkey
c.dest, // key
core.TypeSortedSet, // type

View File

@@ -6,7 +6,6 @@ import (
"time"
"github.com/nalgeon/redka/internal/core"
"github.com/nalgeon/redka/internal/rkey"
"github.com/nalgeon/redka/internal/sqlx"
)
@@ -20,8 +19,18 @@ const (
order by sum(score), elem`
sqlUnionStore = `
delete from rzset
where key_id = (
select id from rkey where key = :key
and (etime is null or etime > :now)
);
insert into rkey (key, type, version, mtime)
values (:key, :type, :version, :mtime);
values (:key, :type, :version, :mtime)
on conflict (key) do update set
version = version+1,
type = excluded.type,
mtime = excluded.mtime;
insert into rzset (key_id, elem, score)
select
@@ -143,15 +152,12 @@ func (c UnionCmd) union(tx sqlx.Tx) ([]SetItem, error) {
// store unions multiple sets and stores the result in a new set.
func (c UnionCmd) store(tx sqlx.Tx) (int, error) {
// Delete the destination key if it exists.
_, err := rkey.DeleteType(tx, core.TypeSortedSet, c.dest)
if err != nil {
return 0, err
}
// Union the sets and store the result.
now := time.Now().UnixMilli()
args := []any{
// delete from rzset
c.dest, // key
now, // now
// insert into rkey
c.dest, // key
core.TypeSortedSet, // type