Add dump heads and new head when adding text
This commit is contained in:
parent
c5a958df12
commit
3dec56141f
@ -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)
|
||||
}
|
||||
|
||||
|
||||
@ -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}
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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() {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user