From ca57e05698948ca21209bcfa7ba9e7f42f6dab3c Mon Sep 17 00:00:00 2001 From: juligasa <11684004+juligasa@users.noreply.github.com> Date: Wed, 24 Jul 2024 20:41:51 +0200 Subject: [PATCH] fix(daemon): search API --- .../daemon/api/entities/v1alpha/entities.go | 11 +++++- backend/daemon/index/index.go | 20 ++++++++++ backend/daemon/storage2/schema.gen.go | 19 ++++++++++ backend/daemon/storage2/schema.gensum | 4 +- backend/daemon/storage2/schema.sql | 38 +++++++++++++++++++ backend/daemon/storage2/storage_migrations.go | 2 +- 6 files changed, 89 insertions(+), 5 deletions(-) diff --git a/backend/daemon/api/entities/v1alpha/entities.go b/backend/daemon/api/entities/v1alpha/entities.go index d9035b8c..467f3718 100644 --- a/backend/daemon/api/entities/v1alpha/entities.go +++ b/backend/daemon/api/entities/v1alpha/entities.go @@ -379,9 +379,16 @@ func (api *Server) SearchEntities(ctx context.Context, in *entities.SearchEntiti var iris []string var owners []string const limit = 30 + type meta struct { + Title string `json:"title"` + } if err := api.blobs.Query(ctx, func(conn *sqlite.Conn) error { return sqlitex.Exec(conn, qGetEntityTitles(), func(stmt *sqlite.Stmt) error { - titles = append(titles, stmt.ColumnText(0)) + var attr meta + if err := json.Unmarshal(stmt.ColumnBytes(0), &attr); err != nil { + return err + } + titles = append(titles, attr.Title) iris = append(iris, stmt.ColumnText(1)) ownerID := core.Principal(stmt.ColumnBytes(2)).String() owners = append(owners, ownerID) @@ -528,7 +535,7 @@ func (api *Server) ListDeletedEntities(ctx context.Context, _ *entities.ListDele } var qGetEntityTitles = dqb.Str(` - SELECT meta, iri, principal + SELECT extra_attrs, iri, principal FROM meta_view;`) // ListEntityMentions implements listing mentions of an entity in other resources. diff --git a/backend/daemon/index/index.go b/backend/daemon/index/index.go index e298b207..4afd49e2 100644 --- a/backend/daemon/index/index.go +++ b/backend/daemon/index/index.go @@ -234,7 +234,27 @@ func (idx *Index) indexChange(ictx *indexingCtx, id int64, c cid.Cid, v *Change) } } } + type meta struct { + Title string `json:"title"` + } + attrs, ok := v.Payload["metadata"].(map[string]any) + if ok { + title, ok := attrs["title"] + if !ok { + alias, ok := attrs["alias"] + if ok { + sb.Meta = meta{Title: alias.(string)} + } else { + name, ok := attrs["name"] + if ok { + sb.Meta = meta{Title: name.(string)} + } + } + } else { + sb.Meta = meta{Title: title.(string)} + } + } return ictx.SaveBlob(id, sb) } diff --git a/backend/daemon/storage2/schema.gen.go b/backend/daemon/storage2/schema.gen.go index 017261bc..77c74670 100644 --- a/backend/daemon/storage2/schema.gen.go +++ b/backend/daemon/storage2/schema.gen.go @@ -76,6 +76,22 @@ const ( C_KVValue = "kv.value" ) +// Table meta_view. +const ( + MetaView sqlitegen.Table = "meta_view" + MetaViewExtraAttrs sqlitegen.Column = "meta_view.extra_attrs" + MetaViewIRI sqlitegen.Column = "meta_view.iri" + MetaViewPrincipal sqlitegen.Column = "meta_view.principal" +) + +// Table meta_view. Plain strings. +const ( + T_MetaView = "meta_view" + C_MetaViewExtraAttrs = "meta_view.extra_attrs" + C_MetaViewIRI = "meta_view.iri" + C_MetaViewPrincipal = "meta_view.principal" +) + // Table peers. const ( Peers sqlitegen.Table = "peers" @@ -250,6 +266,9 @@ var Schema = sqlitegen.Schema{ DeletedResourcesReason: {Table: DeletedResources, SQLType: "TEXT"}, KVKey: {Table: KV, SQLType: "TEXT"}, KVValue: {Table: KV, SQLType: "TEXT"}, + MetaViewExtraAttrs: {Table: MetaView, SQLType: "JSONB"}, + MetaViewIRI: {Table: MetaView, SQLType: "TEXT"}, + MetaViewPrincipal: {Table: MetaView, SQLType: "BLOB"}, PeersAddresses: {Table: Peers, SQLType: "TEXT"}, PeersID: {Table: Peers, SQLType: "INTEGER"}, PeersPid: {Table: Peers, SQLType: "TEXT"}, diff --git a/backend/daemon/storage2/schema.gensum b/backend/daemon/storage2/schema.gensum index 7428c8a7..672dd500 100644 --- a/backend/daemon/storage2/schema.gensum +++ b/backend/daemon/storage2/schema.gensum @@ -1,2 +1,2 @@ -srcs: dba4052e80113c39e4f99eb663119794 -outs: e4cb326951b65d31953285d919d1fb5c +srcs: c5fe02946c7607fa321b28f35f3d23ad +outs: 5a09c1032083d5e19160acd676cd6c6d diff --git a/backend/daemon/storage2/schema.sql b/backend/daemon/storage2/schema.sql index 347e26e1..3701982b 100644 --- a/backend/daemon/storage2/schema.sql +++ b/backend/daemon/storage2/schema.sql @@ -61,6 +61,44 @@ CREATE TABLE structural_blobs ( -- TODO(hm24): Create necessary table for the feed API. + +-- View blobs metadata It returns the latest non null title or the +-- latest blob in case of untitled meta. +CREATE VIEW meta_view AS +WITH RankedBlobs AS ( + SELECT + sb.id, + sb.extra_attrs, + sb.author, + sb.resource, + sb.ts, + ROW_NUMBER() OVER ( + PARTITION BY sb.resource + ORDER BY + (CASE WHEN sb.extra_attrs IS NOT NULL THEN 0 ELSE 1 END), + sb.ts DESC + ) AS rank + FROM structural_blobs sb + WHERE sb.type = 'Change' +), +LatestBlobs AS ( + SELECT + rb.id, + rb.extra_attrs, + rb.author, + rb.resource, + rb.ts + FROM RankedBlobs rb + WHERE rb.rank = 1 +) +SELECT + lb.extra_attrs, + res.iri, + pk.principal +FROM LatestBlobs lb +JOIN resources res ON res.id = (SELECT resource from structural_blobs where ts = lb.ts and resource IS NOT NULL LIMIT 1) +JOIN public_keys pk ON pk.id = lb.author; + -- Stores hypermedia resources. -- All resources are identified by an IRI[iri], -- might have an owner identified by a public key. diff --git a/backend/daemon/storage2/storage_migrations.go b/backend/daemon/storage2/storage_migrations.go index bd811a1c..c069ffc1 100644 --- a/backend/daemon/storage2/storage_migrations.go +++ b/backend/daemon/storage2/storage_migrations.go @@ -48,7 +48,7 @@ type migration struct { var migrations = []migration{ // New beginning. While we're doing the HM24 migration we can still make some breaking changes. // TODO(burdiyan): add a real version when we are ready to release. - {Version: "2024-07-21.hm24-dev-1", Run: func(d *Store, conn *sqlite.Conn) error { + {Version: "2024-07-24.hm24-dev-1", Run: func(d *Store, conn *sqlite.Conn) error { return nil }}, }