Change updates format in yaml

This commit is contained in:
mcrakhman 2022-07-12 16:33:22 +02:00 committed by Mikhail Iudin
parent 6490f09662
commit 029fead117
No known key found for this signature in database
GPG Key ID: FAAAA8BAABDFF1C0
7 changed files with 96 additions and 70 deletions

View File

@ -148,6 +148,10 @@ func (a *aclTree) rebuildFromThread(fromStart bool) error {
return a.rebuildFromThread(true) return a.rebuildFromThread(true)
} }
} }
// TODO: there is a question how we can validate not only that the full tree is built correctly
// but also that the ACL prev ids are not messed up. I think we should probably compare the resulting
// acl state with the acl state which is built in aclTreeFromStart
err = a.aclStateBuilder.Init(a.fullTree) err = a.aclStateBuilder.Init(a.fullTree)
if err != nil { if err != nil {
return err return err

View File

@ -2,6 +2,7 @@ package acltree
import ( import (
"fmt" "fmt"
"github.com/anytypeio/go-anytype-infrastructure-experiments/thread"
"github.com/gogo/protobuf/proto" "github.com/gogo/protobuf/proto"
"github.com/anytypeio/go-anytype-infrastructure-experiments/aclchanges/pb" "github.com/anytypeio/go-anytype-infrastructure-experiments/aclchanges/pb"
@ -46,6 +47,18 @@ func (ch *Change) IsACLChange() bool {
return ch.Content.GetAclData() != nil return ch.Content.GetAclData() != nil
} }
func NewFromRawChange(rawChange *thread.RawChange) (*Change, error) {
unmarshalled := &pb.ACLChange{}
err := proto.Unmarshal(rawChange.Payload, unmarshalled)
if err != nil {
return nil, err
}
ch := NewChange(rawChange.Id, unmarshalled)
ch.Sign = rawChange.Signature
return ch, nil
}
func NewChange(id string, ch *pb.ACLChange) *Change { func NewChange(id string, ch *pb.ACLChange) *Change {
return &Change{ return &Change{
Next: nil, Next: nil,

View File

@ -30,21 +30,25 @@ type threadChange struct {
changesDataDecrypted []byte changesDataDecrypted []byte
} }
type updateUseCase struct {
changes map[string]*threadChange
}
type ThreadBuilder struct { type ThreadBuilder struct {
threadId string threadId string
allChanges map[string]*threadChange allChanges map[string]*threadChange
updatedChanges map[string]*threadChange updates map[string]*updateUseCase
heads []string heads []string
orphans []string orphans []string
keychain *Keychain keychain *Keychain
header *threadpb.ThreadHeader header *threadpb.ThreadHeader
} }
func NewThreadBuilder(keychain *Keychain) *ThreadBuilder { func NewThreadBuilder(keychain *Keychain) *ThreadBuilder {
return &ThreadBuilder{ return &ThreadBuilder{
allChanges: make(map[string]*threadChange), allChanges: make(map[string]*threadChange),
updatedChanges: make(map[string]*threadChange), updates: make(map[string]*updateUseCase),
keychain: keychain, keychain: keychain,
} }
} }
@ -162,10 +166,11 @@ func (t *ThreadBuilder) GetChange(ctx context.Context, recordID string) (*thread
return t.getChange(recordID, t.allChanges), nil return t.getChange(recordID, t.allChanges), nil
} }
func (t *ThreadBuilder) GetUpdatedChanges() []*thread.RawChange { func (t *ThreadBuilder) GetUpdates(useCase string) []*thread.RawChange {
var res []*thread.RawChange var res []*thread.RawChange
for _, ch := range t.updatedChanges { update := t.updates[useCase]
rawCh := t.getChange(ch.id, t.updatedChanges) for _, ch := range update.changes {
rawCh := t.getChange(ch.id, update.changes)
res = append(res, rawCh) res = append(res, rawCh)
} }
return res return res
@ -216,14 +221,10 @@ func (t *ThreadBuilder) Parse(thread *YMLThread) {
t.allChanges[newChange.id] = newChange t.allChanges[newChange.id] = newChange
} }
for _, ch := range thread.UpdatedChanges {
newChange := t.parseChange(ch)
t.updatedChanges[newChange.id] = newChange
}
t.parseGraph(thread) t.parseGraph(thread)
t.parseOrphans(thread) t.parseOrphans(thread)
t.parseHeader(thread) t.parseHeader(thread)
t.parseUpdates(thread.Updates)
} }
func (t *ThreadBuilder) parseChange(ch *Change) *threadChange { func (t *ThreadBuilder) parseChange(ch *Change) *threadChange {
@ -500,6 +501,26 @@ func (t *ThreadBuilder) traverseFromHeads(f func(t *threadChange) error) error {
return nil return nil
} }
func (t *ThreadBuilder) parseUpdates(updates []*Update) {
for _, update := range updates {
useCase := &updateUseCase{
changes: map[string]*threadChange{},
}
for _, ch := range update.Changes {
newChange := t.parseChange(ch)
useCase.changes[newChange.id] = newChange
}
for _, node := range update.Graph {
rec := useCase.changes[node.Id]
rec.AclHeadIds = node.ACLHeads
rec.TreeHeadIds = node.TreeHeads
rec.SnapshotBaseId = node.BaseSnapshot
}
t.updates[update.UseCase] = useCase
}
}
func (t *ThreadBuilder) parseGraph(thread *YMLThread) { func (t *ThreadBuilder) parseGraph(thread *YMLThread) {
for _, node := range thread.Graph { for _, node := range thread.Graph {
rec := t.allChanges[node.Id] rec := t.allChanges[node.Id]
@ -507,13 +528,6 @@ func (t *ThreadBuilder) parseGraph(thread *YMLThread) {
rec.TreeHeadIds = node.TreeHeads rec.TreeHeadIds = node.TreeHeads
rec.SnapshotBaseId = node.BaseSnapshot rec.SnapshotBaseId = node.BaseSnapshot
} }
for _, node := range thread.UpdatedGraph {
rec := t.updatedChanges[node.Id]
rec.AclHeadIds = node.ACLHeads
rec.TreeHeadIds = node.TreeHeads
rec.SnapshotBaseId = node.BaseSnapshot
}
} }
func (t *ThreadBuilder) parseOrphans(thread *YMLThread) { func (t *ThreadBuilder) parseOrphans(thread *YMLThread) {

View File

@ -95,15 +95,20 @@ type Header struct {
IsWorkspace bool `yaml:"isWorkspace"` IsWorkspace bool `yaml:"isWorkspace"`
} }
type Update struct {
UseCase string `yaml:"useCase"`
Changes []*Change `yaml:"changes"`
Graph []*GraphNode `yaml:"graph"`
}
type YMLThread struct { type YMLThread struct {
Description *ThreadDescription `yaml:"thread"` Description *ThreadDescription `yaml:"thread"`
Changes []*Change `yaml:"changes"` Changes []*Change `yaml:"changes"`
UpdatedChanges []*Change `yaml:"updatedChanges"` Updates []*Update `yaml:"updates"`
Keys Keys `yaml:"keys"` Keys Keys `yaml:"keys"`
Graph []*GraphNode `yaml:"graph"` Graph []*GraphNode `yaml:"graph"`
UpdatedGraph []*GraphNode `yaml:"updatedGraph"`
Heads []string `yaml:"heads"` Heads []string `yaml:"heads"`
Orphans []string `yaml:"orphans"` Orphans []string `yaml:"orphans"`

View File

@ -6,7 +6,7 @@ import (
) )
func Test_YamlParse(t *testing.T) { func Test_YamlParse(t *testing.T) {
tb, _ := NewThreadBuilderWithTestName("userjoinexample.yml") tb, _ := NewThreadBuilderWithTestName("userjoinexampleupdate.yml")
gr, _ := tb.Graph() gr, _ := tb.Graph()
fmt.Println(gr) fmt.Println(gr)
} }

View File

@ -104,15 +104,3 @@ header:
isWorkspace: false isWorkspace: false
orphans: orphans:
- "A.1.3" - "A.1.3"
updatedChanges:
- id: B.1.3
identity: B
changes:
- textAppend:
text: "second"
readKey: key.Read.1
updatedGraph:
- id: B.1.3
baseSnapshot: A.1.1
aclHeads: [ B.1.1 ]
treeHeads: [ B.1.2 ]

View File

@ -106,31 +106,33 @@ header:
isWorkspace: false isWorkspace: false
orphans: orphans:
- "A.1.3" - "A.1.3"
updatedChanges: updates:
- id: B.1.3 - useCase: append
identity: B
changes: changes:
- textAppend: - id: B.1.3
text: "second" identity: B
readKey: key.Read.1 changes:
- id: A.1.4 - textAppend:
identity: A text: "second"
aclChanges: readKey: key.Read.1
- userAdd: - id: A.1.4
identity: D identity: A
permission: writer aclChanges:
encryptionKey: key.Enc.D - userAdd:
encryptedReadKeys: [ key.Read.1 ] identity: D
changes: permission: writer
- textAppend: encryptionKey: key.Enc.D
text: "second" encryptedReadKeys: [ key.Read.1 ]
readKey: key.Read.1 changes:
updatedGraph: - textAppend:
- id: B.1.3 text: "second"
baseSnapshot: A.1.1 readKey: key.Read.1
aclHeads: [ B.1.1 ] graph:
treeHeads: [ B.1.2 ] - id: B.1.3
- id: A.1.4 baseSnapshot: A.1.1
baseSnapshot: A.1.1 aclHeads: [ B.1.1 ]
aclHeads: [ B.1.1 ] treeHeads: [ B.1.2 ]
treeHeads: [ B.1.3 ] - id: A.1.4
baseSnapshot: A.1.1
aclHeads: [ B.1.1 ]
treeHeads: [ B.1.3 ]