Skip to content
This repository has been archived by the owner on Jan 2, 2025. It is now read-only.

Commit

Permalink
feat(backend): implement backend for variants
Browse files Browse the repository at this point in the history
  • Loading branch information
burdiyan committed Dec 1, 2023
1 parent 3f93778 commit 41a4b09
Show file tree
Hide file tree
Showing 63 changed files with 1,272 additions and 895 deletions.
4 changes: 2 additions & 2 deletions backend/cmd/mintter-site/sites/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
accounts "mintter/backend/genproto/accounts/v1alpha"
"mintter/backend/hyper"
"mintter/backend/mttnet"
"mintter/backend/pkg/colx"
"mintter/backend/pkg/future"
"mintter/backend/pkg/libp2px"
"mintter/backend/pkg/slicex"
"net/url"

"crawshaw.io/sqlite/sqlitex"
Expand Down Expand Up @@ -44,7 +44,7 @@ func Load(ctx context.Context, address string, cfg config.Config, dir *storage.D
return nil, fmt.Errorf("address URL must not have a path: %s", address)
}

cfg.P2P.AnnounceAddrs, err = slicex.MapE(libp2px.DefaultListenAddrsDNS(u.Hostname(), cfg.P2P.Port), multiaddr.NewMultiaddr)
cfg.P2P.AnnounceAddrs, err = colx.SliceMapErr(libp2px.DefaultListenAddrsDNS(u.Hostname(), cfg.P2P.Port), multiaddr.NewMultiaddr)
if err != nil {
panic(fmt.Errorf("failed to parse announce addresses: %w", err))
}
Expand Down
4 changes: 2 additions & 2 deletions backend/cmd/mintter-site/sites/sites.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"mintter/backend/hyper"
"mintter/backend/hyper/hypersql"
"mintter/backend/mttnet"
"mintter/backend/pkg/colx"
"mintter/backend/pkg/future"
"mintter/backend/pkg/slicex"
"net/http"
"sync"

Expand Down Expand Up @@ -137,7 +137,7 @@ func (ws *Website) GetSiteInfo(ctx context.Context, in *groups.GetSiteInfoReques
PeerInfo: &groups.PeerInfo{
PeerId: ai.ID.String(),
AccountId: n.ID().Account().Principal().String(),
Addrs: slicex.Map(ai.Addrs, multiaddr.Multiaddr.String),
Addrs: colx.SliceMap(ai.Addrs, multiaddr.Multiaddr.String),
},
GroupId: groupID,
}
Expand Down
2 changes: 1 addition & 1 deletion backend/crdt2/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package crdt2

import (
"math"
"mintter/backend/pkg/maps"
"sort"

"github.com/tidwall/btree"
"golang.org/x/exp/maps"
)

type Map struct {
Expand Down
2 changes: 1 addition & 1 deletion backend/daemon/api/accounts/v1alpha/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ func (srv *Server) ListAccounts(ctx context.Context, in *accounts.ListAccountsRe
return nil, err
}

entities, err := srv.blobs.ListEntities(ctx, "hm://a/")
entities, err := srv.blobs.ListEntities(ctx, "hm://a/*")
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion backend/daemon/api/daemon/v1alpha/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func Register(ctx context.Context, bs *hyper.Storage, account core.KeyPair, devi
}

if err = bs.SetAccountTrust(ctx, account.Principal()); err != nil {
return blob.CID, fmt.Errorf("Could not set own account to trusted: " + err.Error())
return blob.CID, fmt.Errorf("could not set own account to trusted: " + err.Error())
}
return blob.CID, nil
}
Expand Down
6 changes: 3 additions & 3 deletions backend/daemon/api/documents/v1alpha/document_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
documents "mintter/backend/genproto/documents/v1alpha"
"mintter/backend/hlc"
"mintter/backend/hyper"
"mintter/backend/pkg/maputil"
"mintter/backend/pkg/colx"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -225,7 +225,7 @@ func (dm *docModel) ReplaceBlock(blk *documents.Block) error {
return err
}

maputil.Set(dm.patch, []string{"blocks", blk.Id, "#map"}, v)
colx.ObjectSet(dm.patch, []string{"blocks", blk.Id, "#map"}, v)

return nil
}
Expand Down Expand Up @@ -336,7 +336,7 @@ func (dm *docModel) cleanupPatch() {
for blk := range dm.deletedBlocks {
_, mustIgnore := dm.createdBlocks[blk]
if mustIgnore {
maputil.Delete(dm.patch, []string{"blocks", blk})
colx.ObjectDelete(dm.patch, []string{"blocks", blk})
continue
}

Expand Down
38 changes: 23 additions & 15 deletions backend/daemon/api/documents/v1alpha/documents.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func (api *Server) GetDraft(ctx context.Context, in *documents.GetDraftRequest)

// ListDrafts implements the corresponding gRPC method.
func (api *Server) ListDrafts(ctx context.Context, in *documents.ListDraftsRequest) (*documents.ListDraftsResponse, error) {
entities, err := api.blobs.ListEntities(ctx, "hm://d/")
entities, err := api.blobs.ListEntities(ctx, "hm://d/*")
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -344,7 +344,7 @@ func (api *Server) GetPublication(ctx context.Context, in *documents.GetPublicat
eid := hyper.EntityID(in.DocumentId)
version := hyper.Version(in.Version)

pub, err := api.loadPublication(ctx, eid, version, in.TrustedOnly)
pub, err := api.loadPublication(ctx, eid, version)
if err == nil {
return pub, nil
}
Expand Down Expand Up @@ -376,10 +376,10 @@ func (api *Server) GetPublication(ctx context.Context, in *documents.GetPublicat
return nil, status.Errorf(codes.NotFound, "failed to discover object %q at version %q: %s", eid, version, err.Error())
}

return api.loadPublication(ctx, eid, version, in.TrustedOnly)
return api.loadPublication(ctx, eid, version)
}

func (api *Server) loadPublication(ctx context.Context, docid hyper.EntityID, version hyper.Version, trustedOnly bool) (docpb *documents.Publication, err error) {
func (api *Server) loadPublication(ctx context.Context, docid hyper.EntityID, version hyper.Version) (docpb *documents.Publication, err error) {
var entity *hyper.Entity
if version != "" {
heads, err := hyper.Version(version).Parse()
Expand All @@ -392,11 +392,7 @@ func (api *Server) loadPublication(ctx context.Context, docid hyper.EntityID, ve
return nil, err
}
} else {
if trustedOnly {
entity, err = api.blobs.LoadTrustedEntity(ctx, docid)
} else {
entity, err = api.blobs.LoadEntity(ctx, docid)
}
entity, err = api.blobs.LoadEntity(ctx, docid)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -449,21 +445,33 @@ func (api *Server) DeletePublication(ctx context.Context, in *documents.DeletePu

// ListPublications implements the corresponding gRPC method.
func (api *Server) ListPublications(ctx context.Context, in *documents.ListPublicationsRequest) (*documents.ListPublicationsResponse, error) {
entities, err := api.blobs.ListEntities(ctx, "hm://d/")
if err != nil {
return nil, err
var (
entities []hyper.EntityID
err error
)
if in.TrustedOnly {
entities, err = api.blobs.ListTrustedEntities(ctx, "hm://d/*")
if err != nil {
return nil, err
}
} else {
entities, err = api.blobs.ListEntities(ctx, "hm://d/*")
if err != nil {
return nil, err
}
}

resp := &documents.ListPublicationsResponse{
Publications: make([]*documents.Publication, 0, len(entities)),
}

// TODO(burdiyan): this is very inefficient. Index the attributes necessary for listing,
// and use the database without loading the changes from disk all the time one by one.
for _, e := range entities {
docid := string(e)
pub, err := api.GetPublication(ctx, &documents.GetPublicationRequest{
DocumentId: docid,
LocalOnly: true,
TrustedOnly: in.TrustedOnly,
DocumentId: docid,
LocalOnly: true,
})
if err != nil {
continue
Expand Down
4 changes: 0 additions & 4 deletions backend/daemon/api/documents/v1alpha/documents_bugs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,6 @@ func TestBug_DraftsInTrustedPublicationList(t *testing.T) {
require.Error(t, err, "must fail asking for doc ID that only has a draft")
require.Nil(t, pub)

pub, err = api.GetPublication(ctx, &documents.GetPublicationRequest{DocumentId: draft.Id, TrustedOnly: true})
require.Error(t, err, "must fail asking for doc ID that only has a draft when requested trusted version")
require.Nil(t, pub)

pubs, err := api.ListPublications(ctx, &documents.ListPublicationsRequest{TrustedOnly: false})
require.NoError(t, err)
require.Len(t, pubs.Publications, 0, "must have no publications")
Expand Down
Loading

0 comments on commit 41a4b09

Please sign in to comment.