Add error in all methods of tree storage
This commit is contained in:
parent
acff22a19e
commit
90b884534c
@ -85,7 +85,11 @@ func BuildACLTree(
|
||||
return nil, err
|
||||
}
|
||||
aclTree.removeOrphans()
|
||||
t.SetHeads(aclTree.Heads())
|
||||
err = t.SetHeads(aclTree.Heads())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
listener.Rebuild(aclTree)
|
||||
|
||||
return aclTree, nil
|
||||
@ -121,11 +125,15 @@ func BuildACLTree(
|
||||
// return nil
|
||||
//}
|
||||
|
||||
func (a *aclTree) removeOrphans() {
|
||||
func (a *aclTree) removeOrphans() error {
|
||||
// removing attached or invalid orphans
|
||||
var toRemove []string
|
||||
|
||||
for _, orphan := range a.treeStorage.Orphans() {
|
||||
orphans, err := a.treeStorage.Orphans()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, orphan := range orphans {
|
||||
if _, exists := a.fullTree.attached[orphan]; exists {
|
||||
toRemove = append(toRemove, orphan)
|
||||
}
|
||||
@ -133,7 +141,7 @@ func (a *aclTree) removeOrphans() {
|
||||
toRemove = append(toRemove, orphan)
|
||||
}
|
||||
}
|
||||
a.treeStorage.RemoveOrphans(toRemove...)
|
||||
return a.treeStorage.RemoveOrphans(toRemove...)
|
||||
}
|
||||
|
||||
func (a *aclTree) rebuildFromStorage(fromStart bool) error {
|
||||
@ -219,7 +227,10 @@ func (a *aclTree) AddContent(build func(builder ChangeBuilder) error) (*Change,
|
||||
return nil, err
|
||||
}
|
||||
|
||||
a.treeStorage.SetHeads([]string{ch.Id})
|
||||
err = a.treeStorage.SetHeads([]string{ch.Id})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ch, nil
|
||||
}
|
||||
|
||||
@ -233,8 +244,17 @@ func (a *aclTree) AddChanges(changes ...*Change) (AddResult, error) {
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
a.removeOrphans()
|
||||
a.treeStorage.SetHeads(a.fullTree.Heads())
|
||||
|
||||
err = a.removeOrphans()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = a.treeStorage.SetHeads(a.fullTree.Heads())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
a.Unlock()
|
||||
switch mode {
|
||||
case Append:
|
||||
@ -251,7 +271,10 @@ func (a *aclTree) AddChanges(changes ...*Change) (AddResult, error) {
|
||||
if err != nil {
|
||||
return AddResult{}, err
|
||||
}
|
||||
a.treeStorage.AddOrphans(ch.Id)
|
||||
err = a.treeStorage.AddOrphans(ch.Id)
|
||||
if err != nil {
|
||||
return AddResult{}, err
|
||||
}
|
||||
}
|
||||
|
||||
prevHeads := a.fullTree.Heads()
|
||||
|
||||
@ -38,8 +38,16 @@ func (tb *aclTreeBuilder) Init() {
|
||||
|
||||
func (tb *aclTreeBuilder) Build() (*Tree, error) {
|
||||
var headsAndOrphans []string
|
||||
headsAndOrphans = append(headsAndOrphans, tb.treeStorage.Orphans()...)
|
||||
headsAndOrphans = append(headsAndOrphans, tb.treeStorage.Heads()...)
|
||||
orphans, err := tb.treeStorage.Orphans()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
heads, err := tb.treeStorage.Heads()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
headsAndOrphans = append(headsAndOrphans, orphans...)
|
||||
headsAndOrphans = append(headsAndOrphans, heads...)
|
||||
aclHeads, err := tb.getACLHeads(headsAndOrphans)
|
||||
|
||||
if err != nil {
|
||||
@ -94,7 +102,11 @@ func (tb *aclTreeBuilder) dfsFromStart(heads []string) (buf []*Change, root *Cha
|
||||
possibleRoots = append(possibleRoots, ch)
|
||||
}
|
||||
}
|
||||
header := tb.treeStorage.Header()
|
||||
header, err := tb.treeStorage.Header()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
for _, r := range possibleRoots {
|
||||
if r.Id == header.FirstChangeId {
|
||||
return buf, r, nil
|
||||
|
||||
@ -46,8 +46,16 @@ func (tb *treeBuilder) Init() {
|
||||
|
||||
func (tb *treeBuilder) Build(fromStart bool) (*Tree, error) {
|
||||
var headsAndOrphans []string
|
||||
headsAndOrphans = append(headsAndOrphans, tb.treeStorage.Orphans()...)
|
||||
headsAndOrphans = append(headsAndOrphans, tb.treeStorage.Heads()...)
|
||||
orphans, err := tb.treeStorage.Orphans()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
heads, err := tb.treeStorage.Heads()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
headsAndOrphans = append(headsAndOrphans, orphans...)
|
||||
headsAndOrphans = append(headsAndOrphans, heads...)
|
||||
|
||||
if fromStart {
|
||||
if err := tb.buildTreeFromStart(headsAndOrphans); err != nil {
|
||||
@ -109,7 +117,10 @@ func (tb *treeBuilder) dfsFromStart(heads []string) (buf []*Change, root *Change
|
||||
possibleRoots = append(possibleRoots, ch)
|
||||
}
|
||||
}
|
||||
header := tb.treeStorage.Header()
|
||||
header, err := tb.treeStorage.Header()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
for _, r := range possibleRoots {
|
||||
if r.Id == header.FirstChangeId {
|
||||
return buf, r, nil
|
||||
|
||||
@ -75,16 +75,16 @@ func NewTreeStorageBuilderFromFile(file string) (*TreeStorageBuilder, error) {
|
||||
return tb, nil
|
||||
}
|
||||
|
||||
func (t *TreeStorageBuilder) TreeID() string {
|
||||
return t.treeId
|
||||
func (t *TreeStorageBuilder) TreeID() (string, error) {
|
||||
return t.treeId, nil
|
||||
}
|
||||
|
||||
func (t *TreeStorageBuilder) GetKeychain() *Keychain {
|
||||
return t.keychain
|
||||
}
|
||||
|
||||
func (t *TreeStorageBuilder) Heads() []string {
|
||||
return t.heads
|
||||
func (t *TreeStorageBuilder) Heads() ([]string, error) {
|
||||
return t.heads, nil
|
||||
}
|
||||
|
||||
func (t *TreeStorageBuilder) AddRawChange(change *treestorage.RawChange) error {
|
||||
@ -118,8 +118,9 @@ func (t *TreeStorageBuilder) AddRawChange(change *treestorage.RawChange) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *TreeStorageBuilder) AddOrphans(orphans ...string) {
|
||||
func (t *TreeStorageBuilder) AddOrphans(orphans ...string) error {
|
||||
t.orphans = append(t.orphans, orphans...)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *TreeStorageBuilder) AddChange(change aclchanges.Change) error {
|
||||
@ -149,17 +150,19 @@ func (t *TreeStorageBuilder) AddChange(change aclchanges.Change) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *TreeStorageBuilder) Orphans() []string {
|
||||
return t.orphans
|
||||
func (t *TreeStorageBuilder) Orphans() ([]string, error) {
|
||||
return t.orphans, nil
|
||||
}
|
||||
|
||||
func (t *TreeStorageBuilder) SetHeads(heads []string) {
|
||||
func (t *TreeStorageBuilder) SetHeads(heads []string) error {
|
||||
// we should copy here instead of just setting the value
|
||||
t.heads = heads
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *TreeStorageBuilder) RemoveOrphans(orphans ...string) {
|
||||
func (t *TreeStorageBuilder) RemoveOrphans(orphans ...string) error {
|
||||
t.orphans = slice.Difference(t.orphans, orphans)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *TreeStorageBuilder) GetChange(ctx context.Context, recordID string) (*treestorage.RawChange, error) {
|
||||
@ -176,8 +179,8 @@ func (t *TreeStorageBuilder) GetUpdates(useCase string) []*treestorage.RawChange
|
||||
return res
|
||||
}
|
||||
|
||||
func (t *TreeStorageBuilder) Header() *storagepb.TreeHeader {
|
||||
return t.header
|
||||
func (t *TreeStorageBuilder) Header() (*storagepb.TreeHeader, error) {
|
||||
return t.header, nil
|
||||
}
|
||||
|
||||
func (t *TreeStorageBuilder) getChange(changeId string, m map[string]*treeChange) *treestorage.RawChange {
|
||||
|
||||
@ -48,31 +48,31 @@ func NewInMemoryTreeStorage(firstChange *RawChange) (TreeStorage, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (t *inMemoryTreeStorage) TreeID() string {
|
||||
func (t *inMemoryTreeStorage) TreeID() (string, error) {
|
||||
t.RLock()
|
||||
defer t.RUnlock()
|
||||
return t.id
|
||||
return t.id, nil
|
||||
}
|
||||
|
||||
func (t *inMemoryTreeStorage) Header() *pb.TreeHeader {
|
||||
func (t *inMemoryTreeStorage) Header() (*pb.TreeHeader, error) {
|
||||
t.RLock()
|
||||
defer t.RUnlock()
|
||||
return t.header
|
||||
return t.header, nil
|
||||
}
|
||||
|
||||
func (t *inMemoryTreeStorage) Heads() []string {
|
||||
func (t *inMemoryTreeStorage) Heads() ([]string, error) {
|
||||
t.RLock()
|
||||
defer t.RUnlock()
|
||||
return t.heads
|
||||
return t.heads, nil
|
||||
}
|
||||
|
||||
func (t *inMemoryTreeStorage) Orphans() []string {
|
||||
func (t *inMemoryTreeStorage) Orphans() ([]string, error) {
|
||||
t.RLock()
|
||||
defer t.RUnlock()
|
||||
return t.orphans
|
||||
return t.orphans, nil
|
||||
}
|
||||
|
||||
func (t *inMemoryTreeStorage) SetHeads(heads []string) {
|
||||
func (t *inMemoryTreeStorage) SetHeads(heads []string) error {
|
||||
t.Lock()
|
||||
defer t.Unlock()
|
||||
t.heads = t.heads[:0]
|
||||
@ -80,18 +80,21 @@ func (t *inMemoryTreeStorage) SetHeads(heads []string) {
|
||||
for _, h := range heads {
|
||||
t.heads = append(t.heads, h)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *inMemoryTreeStorage) RemoveOrphans(orphans ...string) {
|
||||
func (t *inMemoryTreeStorage) RemoveOrphans(orphans ...string) error {
|
||||
t.Lock()
|
||||
defer t.Unlock()
|
||||
t.orphans = slice.Difference(t.orphans, orphans)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *inMemoryTreeStorage) AddOrphans(orphans ...string) {
|
||||
func (t *inMemoryTreeStorage) AddOrphans(orphans ...string) error {
|
||||
t.Lock()
|
||||
defer t.Unlock()
|
||||
t.orphans = append(t.orphans, orphans...)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *inMemoryTreeStorage) AddRawChange(change *RawChange) error {
|
||||
|
||||
@ -6,16 +6,15 @@ import (
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/treestorage/pb"
|
||||
)
|
||||
|
||||
// TODO: change methods to have errors as a return parameter, because we will be dealing with a real database
|
||||
type TreeStorage interface {
|
||||
TreeID() string
|
||||
TreeID() (string, error)
|
||||
|
||||
Header() *pb.TreeHeader
|
||||
Heads() []string
|
||||
Orphans() []string
|
||||
SetHeads(heads []string)
|
||||
RemoveOrphans(orphan ...string)
|
||||
AddOrphans(orphan ...string)
|
||||
Header() (*pb.TreeHeader, error)
|
||||
Heads() ([]string, error)
|
||||
Orphans() ([]string, error)
|
||||
SetHeads(heads []string) error
|
||||
RemoveOrphans(orphan ...string) error
|
||||
AddOrphans(orphan ...string) error
|
||||
|
||||
AddRawChange(change *RawChange) error
|
||||
AddChange(change aclchanges.Change) error
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user