From 3dec56141f3b495f5dcd9193207772c3a6a19c02 Mon Sep 17 00:00:00 2001 From: mcrakhman Date: Fri, 2 Dec 2022 12:18:38 +0100 Subject: [PATCH] Add dump heads and new head when adding text --- client/api/controller.go | 11 +++++-- client/api/rpchandler.go | 12 ++++---- client/document/service.go | 15 ++++++++-- client/document/textdocument/textdocument.go | 10 +++++-- common/commonspace/diffservice/diffservice.go | 26 +++++++++++++++++ common/commonspace/space.go | 5 ++++ util/cmd/debug/api/service.go | 29 ++++++++++++++++++- 7 files changed, 94 insertions(+), 14 deletions(-) diff --git a/client/api/controller.go b/client/api/controller.go index 0e6b886c..94f7a690 100644 --- a/client/api/controller.go +++ b/client/api/controller.go @@ -7,6 +7,7 @@ import ( "github.com/anytypeio/go-anytype-infrastructure-experiments/client/storage" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/account" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/diffservice" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys/symmetric" "math/rand" ) @@ -27,8 +28,10 @@ type Controller interface { DeleteDocument(spaceId, documentId string) (err error) // AllDocumentIds gets all ids of documents in space AllDocumentIds(spaceId string) (ids []string, err error) + // AllDocumentHeads gets heads of all documents + AllDocumentHeads(spaceId string) (ids []diffservice.TreeHeads, err error) // AddText adds text to space document - AddText(spaceId, documentId, text string) (err error) + AddText(spaceId, documentId, text string) (head string, err error) // DumpDocumentTree dumps the tree data into string DumpDocumentTree(spaceId, documentId string) (dump string, err error) @@ -107,7 +110,11 @@ func (c *controller) AllDocumentIds(spaceId string) (ids []string, err error) { return c.docService.AllDocumentIds(spaceId) } -func (c *controller) AddText(spaceId, documentId, text string) (err error) { +func (c *controller) AllDocumentHeads(spaceId string) (ids []diffservice.TreeHeads, err error) { + return c.docService.AllDocumentHeads(spaceId) +} + +func (c *controller) AddText(spaceId, documentId, text string) (head string, err error) { return c.docService.AddText(spaceId, documentId, text) } diff --git a/client/api/rpchandler.go b/client/api/rpchandler.go index bdabe3d6..a0f4d4aa 100644 --- a/client/api/rpchandler.go +++ b/client/api/rpchandler.go @@ -55,13 +55,13 @@ func (r *rpcHandler) DeleteDocument(ctx context.Context, request *apiproto.Delet } func (r *rpcHandler) AddText(ctx context.Context, request *apiproto.AddTextRequest) (resp *apiproto.AddTextResponse, err error) { - err = r.controller.AddText(request.SpaceId, request.DocumentId, request.Text) + head, err := r.controller.AddText(request.SpaceId, request.DocumentId, request.Text) if err != nil { return } - // TODO: update controller to add head resp = &apiproto.AddTextResponse{ DocumentId: request.DocumentId, + HeadId: head, } return } @@ -78,15 +78,15 @@ func (r *rpcHandler) DumpTree(ctx context.Context, request *apiproto.DumpTreeReq } func (r *rpcHandler) AllTrees(ctx context.Context, request *apiproto.AllTreesRequest) (resp *apiproto.AllTreesResponse, err error) { - ids, err := r.controller.AllDocumentIds(request.SpaceId) + heads, err := r.controller.AllDocumentHeads(request.SpaceId) if err != nil { return } - // TODO: add getting heads to controller var trees []*apiproto.Tree - for _, id := range ids { + for _, head := range heads { trees = append(trees, &apiproto.Tree{ - Id: id, + Id: head.Id, + Heads: head.Heads, }) } resp = &apiproto.AllTreesResponse{Trees: trees} diff --git a/client/document/service.go b/client/document/service.go index d6f727ea..b03e2c61 100644 --- a/client/document/service.go +++ b/client/document/service.go @@ -8,6 +8,7 @@ import ( "github.com/anytypeio/go-anytype-infrastructure-experiments/common/account" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app/logger" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/diffservice" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/treegetter" ) @@ -16,7 +17,8 @@ type Service interface { CreateDocument(spaceId string) (id string, err error) DeleteDocument(spaceId, documentId string) (err error) AllDocumentIds(spaceId string) (ids []string, err error) - AddText(spaceId, documentId, text string) (err error) + AllDocumentHeads(spaceId string) (ids []diffservice.TreeHeads, err error) + AddText(spaceId, documentId, text string) (head string, err error) DumpDocumentTree(spaceId, documentId string) (dump string, err error) } @@ -75,7 +77,16 @@ func (s *service) AllDocumentIds(spaceId string) (ids []string, err error) { return } -func (s *service) AddText(spaceId, documentId, text string) (err error) { +func (s *service) AllDocumentHeads(spaceId string) (ids []diffservice.TreeHeads, err error) { + space, err := s.spaceService.GetSpace(context.Background(), spaceId) + if err != nil { + return + } + ids = space.DebugAllHeads() + return +} + +func (s *service) AddText(spaceId, documentId, text string) (head string, err error) { doc, err := s.cache.GetDocument(context.Background(), spaceId, documentId) if err != nil { return diff --git a/client/document/textdocument/textdocument.go b/client/document/textdocument/textdocument.go index 4c4abb44..c6ef7ed6 100644 --- a/client/document/textdocument/textdocument.go +++ b/client/document/textdocument/textdocument.go @@ -13,7 +13,7 @@ import ( type TextDocument interface { tree.ObjectTree InnerTree() tree.ObjectTree - AddText(text string) error + AddText(text string) (string, error) Text() (string, error) TreeDump() string Close() error @@ -60,7 +60,7 @@ func (t *textDocument) InnerTree() tree.ObjectTree { return t.ObjectTree } -func (t *textDocument) AddText(text string) (err error) { +func (t *textDocument) AddText(text string) (head string, err error) { content := &testchanges.TextContent_TextAppend{ TextAppend: &testchanges.TextAppend{Text: text}, } @@ -76,12 +76,16 @@ func (t *textDocument) AddText(text string) (err error) { } t.Lock() defer t.Unlock() - _, err = t.AddContent(context.Background(), tree.SignableChangeContent{ + addRes, err := t.AddContent(context.Background(), tree.SignableChangeContent{ Data: res, Key: t.account.Account().SignKey, Identity: t.account.Account().Identity, IsSnapshot: false, }) + if err != nil { + return + } + head = addRes.Heads[0] return } diff --git a/common/commonspace/diffservice/diffservice.go b/common/commonspace/diffservice/diffservice.go index ae3b5cb4..d3ac3211 100644 --- a/common/commonspace/diffservice/diffservice.go +++ b/common/commonspace/diffservice/diffservice.go @@ -15,11 +15,17 @@ import ( "strings" ) +type TreeHeads struct { + Id string + Heads []string +} + type DiffService interface { HeadNotifiable HandleRangeRequest(ctx context.Context, req *spacesyncproto.HeadSyncRequest) (resp *spacesyncproto.HeadSyncResponse, err error) RemoveObjects(ids []string) AllIds() []string + DebugAllHeads() (res []TreeHeads) Init(objectIds []string, deletionState deletionstate.DeletionState) Close() (err error) @@ -79,6 +85,18 @@ func (d *diffService) AllIds() []string { return d.diff.Ids() } +func (d *diffService) DebugAllHeads() (res []TreeHeads) { + els := d.diff.Elements() + for _, el := range els { + idHead := TreeHeads{ + Id: el.Id, + Heads: splitString(el.Head), + } + res = append(res, idHead) + } + return +} + func (d *diffService) RemoveObjects(ids []string) { d.syncer.RemoveObjects(ids) } @@ -122,3 +140,11 @@ func concatStrings(strs []string) string { } return b.String() } + +func splitString(str string) (res []string) { + const cidLen = 59 + for i := 0; i < len(str); i += cidLen { + res = append(res, str[i:i+cidLen]) + } + return +} diff --git a/common/commonspace/space.go b/common/commonspace/space.go index 7401f30c..3d1a9a24 100644 --- a/common/commonspace/space.go +++ b/common/commonspace/space.go @@ -69,6 +69,7 @@ type Space interface { Init(ctx context.Context) error StoredIds() []string + DebugAllHeads() []diffservice.TreeHeads Description() (SpaceDescription, error) SpaceSyncRpc() RpcHandler @@ -196,6 +197,10 @@ func (s *space) StoredIds() []string { return s.diffService.AllIds() } +func (s *space) DebugAllHeads() []diffservice.TreeHeads { + return s.diffService.DebugAllHeads() +} + func (s *space) DeriveTree(ctx context.Context, payload tree.ObjectTreeCreatePayload, listener updatelistener.UpdateListener) (tr tree.ObjectTree, err error) { if s.isClosed.Load() { err = ErrSpaceClosed diff --git a/util/cmd/debug/api/service.go b/util/cmd/debug/api/service.go index 4a8d019d..607509a1 100644 --- a/util/cmd/debug/api/service.go +++ b/util/cmd/debug/api/service.go @@ -165,7 +165,7 @@ func (s *service) registerClientCommands() { if err != nil { return } - res = resp.DocumentId + res = resp.DocumentId + "->" + resp.HeadId return }} s.clientCommands["load-space"] = Command{Cmd: func(server peers.Peer, params []string) (res string, err error) { @@ -182,6 +182,33 @@ func (s *service) registerClientCommands() { res = params[0] return }} + s.clientCommands["all-trees"] = Command{Cmd: func(server peers.Peer, params []string) (res string, err error) { + if len(params) != 1 { + err = ErrIncorrectParamsCount + return + } + resp, err := client.AllTrees(context.Background(), server.Address, &apiproto.AllTreesRequest{ + SpaceId: params[0], + }) + if err != nil { + return + } + for treeIdx, tree := range resp.Trees { + treeStr := tree.Id + ":[" + for headIdx, head := range tree.Heads { + treeStr += head + if headIdx != len(tree.Heads)-1 { + treeStr += "," + } + } + treeStr += "]" + res += treeStr + if treeIdx != len(resp.Trees)-1 { + res += "\n" + } + } + return + }} } func (s *service) registerNodeCommands() {