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/client/storage"
|
||||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/account"
|
"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"
|
||||||
|
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/diffservice"
|
||||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys/symmetric"
|
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys/symmetric"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
)
|
)
|
||||||
@ -27,8 +28,10 @@ type Controller interface {
|
|||||||
DeleteDocument(spaceId, documentId string) (err error)
|
DeleteDocument(spaceId, documentId string) (err error)
|
||||||
// AllDocumentIds gets all ids of documents in space
|
// AllDocumentIds gets all ids of documents in space
|
||||||
AllDocumentIds(spaceId string) (ids []string, err error)
|
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 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 dumps the tree data into string
|
||||||
DumpDocumentTree(spaceId, documentId string) (dump string, err error)
|
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)
|
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)
|
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) {
|
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 {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// TODO: update controller to add head
|
|
||||||
resp = &apiproto.AddTextResponse{
|
resp = &apiproto.AddTextResponse{
|
||||||
DocumentId: request.DocumentId,
|
DocumentId: request.DocumentId,
|
||||||
|
HeadId: head,
|
||||||
}
|
}
|
||||||
return
|
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) {
|
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 {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// TODO: add getting heads to controller
|
|
||||||
var trees []*apiproto.Tree
|
var trees []*apiproto.Tree
|
||||||
for _, id := range ids {
|
for _, head := range heads {
|
||||||
trees = append(trees, &apiproto.Tree{
|
trees = append(trees, &apiproto.Tree{
|
||||||
Id: id,
|
Id: head.Id,
|
||||||
|
Heads: head.Heads,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
resp = &apiproto.AllTreesResponse{Trees: trees}
|
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/account"
|
||||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/app"
|
"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/app/logger"
|
||||||
|
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/diffservice"
|
||||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/treegetter"
|
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/treegetter"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -16,7 +17,8 @@ type Service interface {
|
|||||||
CreateDocument(spaceId string) (id string, err error)
|
CreateDocument(spaceId string) (id string, err error)
|
||||||
DeleteDocument(spaceId, documentId string) (err error)
|
DeleteDocument(spaceId, documentId string) (err error)
|
||||||
AllDocumentIds(spaceId string) (ids []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)
|
DumpDocumentTree(spaceId, documentId string) (dump string, err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,7 +77,16 @@ func (s *service) AllDocumentIds(spaceId string) (ids []string, err error) {
|
|||||||
return
|
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)
|
doc, err := s.cache.GetDocument(context.Background(), spaceId, documentId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
|
|||||||
@ -13,7 +13,7 @@ import (
|
|||||||
type TextDocument interface {
|
type TextDocument interface {
|
||||||
tree.ObjectTree
|
tree.ObjectTree
|
||||||
InnerTree() tree.ObjectTree
|
InnerTree() tree.ObjectTree
|
||||||
AddText(text string) error
|
AddText(text string) (string, error)
|
||||||
Text() (string, error)
|
Text() (string, error)
|
||||||
TreeDump() string
|
TreeDump() string
|
||||||
Close() error
|
Close() error
|
||||||
@ -60,7 +60,7 @@ func (t *textDocument) InnerTree() tree.ObjectTree {
|
|||||||
return t.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{
|
content := &testchanges.TextContent_TextAppend{
|
||||||
TextAppend: &testchanges.TextAppend{Text: text},
|
TextAppend: &testchanges.TextAppend{Text: text},
|
||||||
}
|
}
|
||||||
@ -76,12 +76,16 @@ func (t *textDocument) AddText(text string) (err error) {
|
|||||||
}
|
}
|
||||||
t.Lock()
|
t.Lock()
|
||||||
defer t.Unlock()
|
defer t.Unlock()
|
||||||
_, err = t.AddContent(context.Background(), tree.SignableChangeContent{
|
addRes, err := t.AddContent(context.Background(), tree.SignableChangeContent{
|
||||||
Data: res,
|
Data: res,
|
||||||
Key: t.account.Account().SignKey,
|
Key: t.account.Account().SignKey,
|
||||||
Identity: t.account.Account().Identity,
|
Identity: t.account.Account().Identity,
|
||||||
IsSnapshot: false,
|
IsSnapshot: false,
|
||||||
})
|
})
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
head = addRes.Heads[0]
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -15,11 +15,17 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type TreeHeads struct {
|
||||||
|
Id string
|
||||||
|
Heads []string
|
||||||
|
}
|
||||||
|
|
||||||
type DiffService interface {
|
type DiffService interface {
|
||||||
HeadNotifiable
|
HeadNotifiable
|
||||||
HandleRangeRequest(ctx context.Context, req *spacesyncproto.HeadSyncRequest) (resp *spacesyncproto.HeadSyncResponse, err error)
|
HandleRangeRequest(ctx context.Context, req *spacesyncproto.HeadSyncRequest) (resp *spacesyncproto.HeadSyncResponse, err error)
|
||||||
RemoveObjects(ids []string)
|
RemoveObjects(ids []string)
|
||||||
AllIds() []string
|
AllIds() []string
|
||||||
|
DebugAllHeads() (res []TreeHeads)
|
||||||
|
|
||||||
Init(objectIds []string, deletionState deletionstate.DeletionState)
|
Init(objectIds []string, deletionState deletionstate.DeletionState)
|
||||||
Close() (err error)
|
Close() (err error)
|
||||||
@ -79,6 +85,18 @@ func (d *diffService) AllIds() []string {
|
|||||||
return d.diff.Ids()
|
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) {
|
func (d *diffService) RemoveObjects(ids []string) {
|
||||||
d.syncer.RemoveObjects(ids)
|
d.syncer.RemoveObjects(ids)
|
||||||
}
|
}
|
||||||
@ -122,3 +140,11 @@ func concatStrings(strs []string) string {
|
|||||||
}
|
}
|
||||||
return b.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
|
Init(ctx context.Context) error
|
||||||
|
|
||||||
StoredIds() []string
|
StoredIds() []string
|
||||||
|
DebugAllHeads() []diffservice.TreeHeads
|
||||||
Description() (SpaceDescription, error)
|
Description() (SpaceDescription, error)
|
||||||
|
|
||||||
SpaceSyncRpc() RpcHandler
|
SpaceSyncRpc() RpcHandler
|
||||||
@ -196,6 +197,10 @@ func (s *space) StoredIds() []string {
|
|||||||
return s.diffService.AllIds()
|
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) {
|
func (s *space) DeriveTree(ctx context.Context, payload tree.ObjectTreeCreatePayload, listener updatelistener.UpdateListener) (tr tree.ObjectTree, err error) {
|
||||||
if s.isClosed.Load() {
|
if s.isClosed.Load() {
|
||||||
err = ErrSpaceClosed
|
err = ErrSpaceClosed
|
||||||
|
|||||||
@ -165,7 +165,7 @@ func (s *service) registerClientCommands() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
res = resp.DocumentId
|
res = resp.DocumentId + "->" + resp.HeadId
|
||||||
return
|
return
|
||||||
}}
|
}}
|
||||||
s.clientCommands["load-space"] = Command{Cmd: func(server peers.Peer, params []string) (res string, err error) {
|
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]
|
res = params[0]
|
||||||
return
|
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() {
|
func (s *service) registerNodeCommands() {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user