Skip to content

Commit

Permalink
Normalize fingerprint table (#752)
Browse files Browse the repository at this point in the history
  • Loading branch information
InfiniteStash authored Feb 25, 2024
1 parent f165079 commit 1645ef4
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 46 deletions.
2 changes: 1 addition & 1 deletion pkg/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/jmoiron/sqlx"
)

var appSchemaVersion uint = 33
var appSchemaVersion uint = 34

var databaseProviders map[string]databaseProvider

Expand Down
57 changes: 57 additions & 0 deletions pkg/database/migrations/postgres/34_fingerprints.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
CREATE TABLE "fingerprints" (
"id" SERIAL PRIMARY KEY,
"hash" VARCHAR(255) NOT NULL,
"algorithm" VARCHAR(20) NOT NULL,
UNIQUE ("hash", "algorithm")
);

INSERT INTO "fingerprints" (hash, algorithm)
SELECT hash, algorithm
FROM "scene_fingerprints"
GROUP BY hash, algorithm;

ALTER TABLE "scene_fingerprints" RENAME TO "_scene_fingerprints";

CREATE TABLE "scene_fingerprints" (
"fingerprint_id" INT NOT NULL,
"scene_id" UUID NOT NULL,
"user_id" UUID NOT NULL,
"duration" INT NOT NULL,
"created_at" TIMESTAMP NOT NULL DEFAULT now(),
FOREIGN KEY("fingerprint_id") REFERENCES "fingerprints"("id") ON DELETE CASCADE,
FOREIGN KEY("scene_id") REFERENCES "scenes"("id") ON DELETE CASCADE,
FOREIGN KEY("user_id") REFERENCES "users"("id") ON DELETE CASCADE,
UNIQUE ("scene_id", "fingerprint_id", "user_id")
);

INSERT INTO "scene_fingerprints"
SELECT F.id, scene_id, user_id, duration, created_at
FROM "_scene_fingerprints" SF
JOIN "fingerprints" F ON SF.hash = F.hash AND SF.algorithm = F.algorithm;

DROP TABLE "_scene_fingerprints";

CREATE INDEX "scene_fingerprints_fingerprint_idx" ON "scene_fingerprints" (fingerprint_id);
CREATE INDEX "scene_fingerprints_user_idx" on "scene_fingerprints" (user_id);
CREATE INDEX "scene_fingerprints_created_at" on "scene_fingerprints" (created_at, scene_id);


-- Create phash index if bktree is available
DO $$
DECLARE
extension pg_extension%rowtype;
BEGIN

SELECT *
INTO extension
FROM pg_extension
WHERE extname='bktree';

IF found THEN
CREATE INDEX fingerprints_phash_idx
ON fingerprints
USING spgist ((('x' || hash)::bit(64)::bigint) bktree_ops)
WHERE algorithm = 'PHASH';
END IF;

END$$;
29 changes: 26 additions & 3 deletions pkg/models/model_scene.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,6 @@ type SceneFingerprint struct {
Algorithm string `db:"algorithm" json:"algorithm"`
Duration int `db:"duration" json:"duration"`
CreatedAt time.Time `db:"created_at" json:"created_at"`
// unused fields
Submissions int `db:"submissions" json:"submissions"`
UpdatedAt time.Time `db:"updated_at" json:"updated_at"`
}

type SceneFingerprints []*SceneFingerprint
Expand All @@ -128,6 +125,32 @@ func (f *SceneFingerprints) Add(o interface{}) {
*f = append(*f, o.(*SceneFingerprint))
}

type DBSceneFingerprint struct {
SceneID uuid.UUID `db:"scene_id" json:"scene_id"`
UserID uuid.UUID `db:"user_id" json:"user_id"`
FingerprintID int `db:"fingerprint_id" json:"fingerprint_id"`
Duration int `db:"duration" json:"duration"`
CreatedAt time.Time `db:"created_at" json:"created_at"`
}

type DBSceneFingerprints []*DBSceneFingerprint

func (f DBSceneFingerprints) Each(fn func(interface{})) {
for _, v := range f {
fn(*v)
}
}

func (f DBSceneFingerprints) EachPtr(fn func(interface{})) {
for _, v := range f {
fn(v)
}
}

func (f *DBSceneFingerprints) Add(o interface{}) {
*f = append(*f, o.(*DBSceneFingerprint))
}

func CreateSceneFingerprints(sceneID uuid.UUID, fingerprints []*FingerprintEditInput) SceneFingerprints {
var ret SceneFingerprints

Expand Down
Loading

0 comments on commit 1645ef4

Please sign in to comment.