Skip to content

Commit

Permalink
fix(daemon): search API
Browse files Browse the repository at this point in the history
  • Loading branch information
juligasa committed Jul 24, 2024
1 parent b7eec91 commit ca57e05
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 5 deletions.
11 changes: 9 additions & 2 deletions backend/daemon/api/entities/v1alpha/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
20 changes: 20 additions & 0 deletions backend/daemon/index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
19 changes: 19 additions & 0 deletions backend/daemon/storage2/schema.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions backend/daemon/storage2/schema.gensum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
srcs: dba4052e80113c39e4f99eb663119794
outs: e4cb326951b65d31953285d919d1fb5c
srcs: c5fe02946c7607fa321b28f35f3d23ad
outs: 5a09c1032083d5e19160acd676cd6c6d
38 changes: 38 additions & 0 deletions backend/daemon/storage2/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion backend/daemon/storage2/storage_migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}},
}
Expand Down

0 comments on commit ca57e05

Please sign in to comment.