Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quick Switcher #1662

Merged
merged 19 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions backend/daemon/api/entities/v1alpha/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ import (
"mintter/backend/pkg/colx"
"mintter/backend/pkg/dqb"
"mintter/backend/pkg/errutil"
"sort"
"strconv"
"strings"

"github.com/lithammer/fuzzysearch/fuzzy"

"crawshaw.io/sqlite"
"crawshaw.io/sqlite/sqlitex"
"github.com/ipfs/go-cid"
Expand Down Expand Up @@ -355,3 +358,54 @@ func (api *Server) DiscoverEntity(ctx context.Context, in *entities.DiscoverEnti

return &entities.DiscoverEntityResponse{}, nil
}

// SearchEntities implements the Fuzzy search of entities.
func (api *Server) SearchEntities(ctx context.Context, in *entities.SearchEntitiesRequest) (*entities.SearchEntitiesResponse, error) {
burdiyan marked this conversation as resolved.
Show resolved Hide resolved
var titles []string
var iris []string
var owners []string
const limit = 30
if err := api.blobs.Query(ctx, func(conn *sqlite.Conn) error {
err := sqlitex.Exec(conn, qGetEntityTitles(), func(stmt *sqlite.Stmt) error {
titles = append(titles, stmt.ColumnText(0))
iris = append(iris, stmt.ColumnText(1))
ownerID := core.Principal(stmt.ColumnBytes(2)).String()
owners = append(owners, ownerID)
return nil
})
if err != nil {
return err
}
return nil
}); err != nil {
return nil, err
}
ranks := fuzzy.RankFindNormalizedFold(in.Query, titles)
sort.Slice(ranks, func(i, j int) bool {
return ranks[i].Distance < ranks[j].Distance
})
matchingEntities := []*entities.Entity{}
for i, rank := range ranks {
if i >= limit {
break
}
matchingEntities = append(matchingEntities, &entities.Entity{
Id: iris[rank.OriginalIndex],
Title: rank.Target,
Owner: owners[rank.OriginalIndex]})
}
return &entities.SearchEntitiesResponse{Entities: matchingEntities}, nil
}

var qGetEntityTitles = dqb.Str(`
SELECT sb.meta, resources.iri, public_keys.principal
FROM structural_blobs sb
JOIN public_keys ON public_keys.id = sb.author
JOIN resources ON resources.id = sb.resource
JOIN (
SELECT resource, MAX(ts) AS max_ts
FROM structural_blobs
WHERE type='Change' AND meta IS NOT NULL
GROUP BY resource
) AS latest_blobs ON sb.resource = latest_blobs.resource AND sb.ts = latest_blobs.max_ts;
`)
20 changes: 20 additions & 0 deletions backend/daemon/storage/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,26 @@ var migrations = []migration{
PRAGMA integrity_check;
`))
}},
{Version: "2024-02-23.01", Run: func(_ *Dir, conn *sqlite.Conn) error {
return sqlitex.ExecScript(conn, sqlfmt(`
DROP TABLE IF EXISTS structural_blobs;
DROP INDEX IF EXISTS structural_blobs_by_author;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@juligasa you don't need to drop indexes if you drop the parent table.

DROP INDEX IF EXISTS structural_blobs_by_resource;
DROP INDEX IF EXISTS structural_blobs_by_ts;
CREATE TABLE structural_blobs (
id INTEGER PRIMARY KEY REFERENCES blobs (id) ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
type TEXT NOT NULL,
ts INTEGER,
author INTEGER REFERENCES public_keys (id),
resource INTEGER REFERENCES resources (id),
meta TEXT
) WITHOUT ROWID;
CREATE INDEX structural_blobs_by_author ON structural_blobs (author, resource) WHERE author IS NOT NULL;
CREATE INDEX structural_blobs_by_resource ON structural_blobs (resource, author) WHERE resource IS NOT NULL;
CREATE INDEX structural_blobs_by_ts ON structural_blobs(ts, resource) WHERE ts IS NOT NULL;
DELETE FROM kv WHERE key = 'last_reindex_time';
`))
}},
}

const (
Expand Down
3 changes: 3 additions & 0 deletions backend/daemon/storage/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/storage/schema.gensum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
srcs: abf797c70a3e68986dd8fdf74b362fc0
outs: 6efa4c17e6260aa5e774b25347df82d2
srcs: d768fdb89d3eb5d4bee55227ac36c42d
outs: 6e8f9aaea92a324bcd6776afeaa0230c
6 changes: 5 additions & 1 deletion backend/daemon/storage/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,15 @@ CREATE TABLE structural_blobs (
author INTEGER REFERENCES public_keys (id),
-- For blobs that refer to some hypermedia resource
-- this is the reference to the resource.
resource INTEGER REFERENCES resources (id)
resource INTEGER REFERENCES resources (id),
-- Metadata about the content of the blob.
-- The title of the document or group. The bio of the Account.
meta TEXT
) WITHOUT ROWID;

CREATE INDEX structural_blobs_by_author ON structural_blobs (author, resource) WHERE author IS NOT NULL;
CREATE INDEX structural_blobs_by_resource ON structural_blobs (resource, author) WHERE resource IS NOT NULL;
CREATE INDEX structural_blobs_by_ts ON structural_blobs(ts, resource) WHERE ts IS NOT NULL;

-- View of structural blobs with dereferences foreign keys.
CREATE VIEW structural_blobs_view AS
Expand Down
Loading
Loading