Skip to content

Commit

Permalink
ARCO-184: save callback duplicates; change callbacks table from heap …
Browse files Browse the repository at this point in the history
…to clustered
  • Loading branch information
arkadiuszos4chain committed Sep 3, 2024
1 parent 790793d commit 39c3783
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
DROP INDEX IF EXISTS callbacker.ux_callbacker_callbacks_unique;

DROP TABLE IF EXISTS callbacker.callbacks;

DROP SCHEMA IF EXISTS callbacker;
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
CREATE SCHEMA callbacker;

CREATE TABLE callbacker.callbacks (
id BIGSERIAL PRIMARY KEY,
url TEXT NOT NULL,
token TEXT NOT NULL,
tx_id TEXT NOT NULL,
Expand All @@ -13,4 +14,3 @@ CREATE TABLE callbacker.callbacks (
timestamp TIMESTAMPTZ NOT NULL
);

CREATE UNIQUE INDEX ux_callbacker_callbacks_unique ON callbacker.callbacks (url, token, tx_id, tx_status);
6 changes: 3 additions & 3 deletions internal/callbacker/store/postgresql/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ func (p *PostgreSQL) PopMany(ctx context.Context, limit int) (res []*store.Callb
}()

const q = `DELETE FROM callbacker.callbacks
WHERE ctid IN (
SELECT ctid FROM callbacker.callbacks
ORDER BY timestamp
WHERE id IN (
SELECT id FROM callbacker.callbacks
ORDER BY id
LIMIT $1
FOR UPDATE
)
Expand Down
31 changes: 6 additions & 25 deletions internal/callbacker/store/postgresql/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,44 +195,25 @@ func TestPostgresDBt(t *testing.T) {
// then
require.NoError(t, err)

// check if every UNIQUE record has been saved
uniqueRecords := make(map[*store.CallbackData]struct{})

for _, dr := range data {
duplicate := false

for ur := range uniqueRecords {
if tutils.CallbackRecordEqual(ur, dr) {
duplicate = true
break
}
}

if !duplicate {
uniqueRecords[dr] = struct{}{}
}
}
require.NotEmpty(t, uniqueRecords)

// read all from db
dbCallbacks := tutils.ReadAllCallbacks(t, postgresDB.db)
for _, c := range dbCallbacks {
// check if exists in unique records
for ur := range uniqueRecords {
found := false
for i, ur := range data {
if ur == nil {
continue
}

if tutils.CallbackRecordEqual(ur, c) {
// remove if found
delete(uniqueRecords, ur)
data[i] = nil
found = true
break
}
}
}

// uniqueRecords map should be empty if all entries have been visited
require.Empty(t, uniqueRecords)
require.True(t, found)
}
})

t.Run("pop many", func(t *testing.T) {
Expand Down

0 comments on commit 39c3783

Please sign in to comment.