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