Change thread to tree storage and other changes with protos
This commit is contained in:
36 files changed
+547
-536
No files matched your search
@@ -0,0 +1,167 @@
|
||||
package plaintextdocument
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/account"
|
||||
aclpb "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/aclchanges/pb"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/acltree"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/testutils/testchanges/pb"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/treestorage"
|
||||
|
||||
"github.com/gogo/protobuf/proto"
|
||||
)
|
||||
|
||||
type PlainTextDocument interface {
|
||||
Text() string
|
||||
AddText(text string) error
|
||||
}
|
||||
|
||||
type plainTextDocument struct {
|
||||
heads []string
|
||||
aclTree acltree.ACLTree
|
||||
state *DocumentState
|
||||
}
|
||||
|
||||
func (p *plainTextDocument) Text() string {
|
||||
if p.state != nil {
|
||||
return p.state.Text
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (p *plainTextDocument) AddText(text string) error {
|
||||
_, err := p.aclTree.AddContent(func(builder acltree.ChangeBuilder) error {
|
||||
builder.AddChangeContent(
|
||||
&pb.PlainTextChangeData{
|
||||
Content: []*pb.PlainTextChangeContent{
|
||||
createAppendTextChangeContent(text),
|
||||
},
|
||||
})
|
||||
return nil
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (p *plainTextDocument) Update(tree acltree.ACLTree) {
|
||||
p.aclTree = tree
|
||||
var err error
|
||||
defer func() {
|
||||
if err != nil {
|
||||
fmt.Println("rebuild has returned error:", err)
|
||||
}
|
||||
}()
|
||||
|
||||
prevHeads := p.heads
|
||||
p.heads = tree.Heads()
|
||||
startId := prevHeads[0]
|
||||
tree.IterateFrom(startId, func(change *acltree.Change) (isContinue bool) {
|
||||
if change.Id == startId {
|
||||
return true
|
||||
}
|
||||
if change.DecryptedDocumentChange != nil {
|
||||
p.state, err = p.state.ApplyChange(change.DecryptedDocumentChange, change.Id)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
})
|
||||
}
|
||||
|
||||
func (p *plainTextDocument) Rebuild(tree acltree.ACLTree) {
|
||||
p.aclTree = tree
|
||||
p.heads = tree.Heads()
|
||||
var startId string
|
||||
var err error
|
||||
defer func() {
|
||||
if err != nil {
|
||||
fmt.Println("rebuild has returned error:", err)
|
||||
}
|
||||
}()
|
||||
|
||||
rootChange := tree.Root()
|
||||
|
||||
if rootChange.DecryptedDocumentChange == nil {
|
||||
err = fmt.Errorf("root doesn't have decrypted change")
|
||||
return
|
||||
}
|
||||
|
||||
state, err := BuildDocumentStateFromChange(rootChange.DecryptedDocumentChange, rootChange.Id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
startId = rootChange.Id
|
||||
tree.Iterate(func(change *acltree.Change) (isContinue bool) {
|
||||
if startId == change.Id {
|
||||
return true
|
||||
}
|
||||
if change.DecryptedDocumentChange != nil {
|
||||
state, err = state.ApplyChange(change.DecryptedDocumentChange, change.Id)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
p.state = state
|
||||
}
|
||||
|
||||
func NewInMemoryPlainTextDocument(acc *account.AccountData, text string) (PlainTextDocument, error) {
|
||||
return NewPlainTextDocument(acc, treestorage.NewInMemoryTreeStorage, text)
|
||||
}
|
||||
|
||||
func NewPlainTextDocument(
|
||||
acc *account.AccountData,
|
||||
create func(change *treestorage.RawChange) (treestorage.TreeStorage, error),
|
||||
text string) (PlainTextDocument, error) {
|
||||
changeBuilder := func(builder acltree.ChangeBuilder) error {
|
||||
err := builder.UserAdd(acc.Identity, acc.EncKey.GetPublic(), aclpb.ACLChange_Admin)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
builder.AddChangeContent(createInitialChangeContent(text))
|
||||
return nil
|
||||
}
|
||||
t, err := acltree.BuildTreeStorageWithACL(
|
||||
acc,
|
||||
changeBuilder,
|
||||
create)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
doc := &plainTextDocument{
|
||||
heads: nil,
|
||||
aclTree: nil,
|
||||
state: nil,
|
||||
}
|
||||
tree, err := acltree.BuildACLTree(t, acc, doc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
doc.aclTree = tree
|
||||
return doc, nil
|
||||
}
|
||||
|
||||
func createInitialChangeContent(text string) proto.Marshaler {
|
||||
return &pb.PlainTextChangeData{
|
||||
Content: []*pb.PlainTextChangeContent{
|
||||
createAppendTextChangeContent(text),
|
||||
},
|
||||
Snapshot: &pb.PlainTextChangeSnapshot{Text: text},
|
||||
}
|
||||
}
|
||||
|
||||
func createAppendTextChangeContent(text string) *pb.PlainTextChangeContent {
|
||||
return &pb.PlainTextChangeContent{
|
||||
Value: &pb.PlainTextChangeContentValueOfTextAppend{
|
||||
TextAppend: &pb.PlainTextChangeTextAppend{
|
||||
Text: text,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package plaintextdocument
|
||||
|
||||
import (
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/account"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/testutils/treestoragebuilder"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/treestorage"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDocument_NewPlainTextDocument(t *testing.T) {
|
||||
keychain := treestoragebuilder.NewKeychain()
|
||||
keychain.AddSigningKey("A")
|
||||
keychain.AddEncryptionKey("A")
|
||||
data := &account.AccountData{
|
||||
Identity: keychain.GetIdentity("A"),
|
||||
SignKey: keychain.SigningKeys["A"],
|
||||
EncKey: keychain.EncryptionKeys["A"],
|
||||
}
|
||||
|
||||
doc, err := NewPlainTextDocument(data, treestorage.NewInMemoryTreeStorage, "Some text")
|
||||
if err != nil {
|
||||
t.Fatalf("should not create document with error: %v", err)
|
||||
}
|
||||
assert.Equal(t, doc.Text(), "Some text")
|
||||
}
|
||||
|
||||
func TestDocument_PlainTextDocument_AddText(t *testing.T) {
|
||||
keychain := treestoragebuilder.NewKeychain()
|
||||
keychain.AddSigningKey("A")
|
||||
keychain.AddEncryptionKey("A")
|
||||
data := &account.AccountData{
|
||||
Identity: keychain.GetIdentity("A"),
|
||||
SignKey: keychain.SigningKeys["A"],
|
||||
EncKey: keychain.EncryptionKeys["A"],
|
||||
}
|
||||
|
||||
doc, err := NewPlainTextDocument(data, treestorage.NewInMemoryTreeStorage, "Some text")
|
||||
if err != nil {
|
||||
t.Fatalf("should not create document with error: %v", err)
|
||||
}
|
||||
|
||||
err = doc.AddText("Next")
|
||||
if err != nil {
|
||||
t.Fatalf("should be able to add document: %v", err)
|
||||
}
|
||||
assert.Equal(t, doc.Text(), "Some text|Next")
|
||||
|
||||
err = doc.AddText("Shmext")
|
||||
if err != nil {
|
||||
t.Fatalf("should be able to add document: %v", err)
|
||||
}
|
||||
assert.Equal(t, doc.Text(), "Some text|Next|Shmext")
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package plaintextdocument
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/testutils/testchanges/pb"
|
||||
|
||||
"github.com/gogo/protobuf/proto"
|
||||
)
|
||||
|
||||
type DocumentState struct {
|
||||
LastChangeId string
|
||||
Text string
|
||||
}
|
||||
|
||||
func NewDocumentState(text string, id string) *DocumentState {
|
||||
return &DocumentState{
|
||||
LastChangeId: id,
|
||||
Text: text,
|
||||
}
|
||||
}
|
||||
|
||||
func BuildDocumentStateFromChange(change []byte, id string) (*DocumentState, error) {
|
||||
var changesData pb.PlainTextChangeData
|
||||
err := proto.Unmarshal(change, &changesData)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if changesData.GetSnapshot() == nil {
|
||||
return nil, fmt.Errorf("could not create state from empty snapshot")
|
||||
}
|
||||
return NewDocumentState(changesData.GetSnapshot().GetText(), id), nil
|
||||
}
|
||||
|
||||
func (p *DocumentState) ApplyChange(change []byte, id string) (*DocumentState, error) {
|
||||
var changesData pb.PlainTextChangeData
|
||||
err := proto.Unmarshal(change, &changesData)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, content := range changesData.GetContent() {
|
||||
err = p.applyChange(content)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
p.LastChangeId = id
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func (p *DocumentState) applyChange(ch *pb.PlainTextChangeContent) error {
|
||||
switch {
|
||||
case ch.GetTextAppend() != nil:
|
||||
text := ch.GetTextAppend().GetText()
|
||||
p.Text += "|" + text
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user