1
0
Fork 0
mirror of https://github.com/anyproto/any-sync.git synced 2025-06-08 05:57:03 +09:00

Create storage fixes

This commit is contained in:
mcrakhman 2024-11-25 11:57:13 +01:00
parent 471ebf73a4
commit 42639f17a1
No known key found for this signature in database
GPG key ID: DED12CFEF5B8396B
2 changed files with 37 additions and 35 deletions

View file

@ -306,7 +306,8 @@ func TestObjectTree(t *testing.T) {
IsEncrypted: true, IsEncrypted: true,
}, aAccount.Acl) }, aAccount.Acl)
require.NoError(t, err) require.NoError(t, err)
aStore, _ := createStorage(ctx, root, store) aStore, err := createStorage(ctx, root, store)
require.NoError(t, err)
aTree, err := BuildKeyFilterableObjectTree(aStore, aAccount.Acl) aTree, err := BuildKeyFilterableObjectTree(aStore, aAccount.Acl)
require.NoError(t, err) require.NoError(t, err)
err = aTree.Delete() err = aTree.Delete()

View file

@ -14,17 +14,16 @@ import (
) )
const ( const (
orderKey = "o" orderKey = "o"
headsKey = "c" headsKey = "h"
commonSnapshotKey = "s" commonSnapshotKey = "s"
idKey = "id" idKey = "id"
rawChangeKey = "r" rawChangeKey = "r"
snapshotCounterKey = "sc" snapshotCounterKey = "sc"
changeSizeKey = "sz" changeSizeKey = "sz"
snapshotIdKey = "i" snapshotIdKey = "i"
prevIdsKey = "p" prevIdsKey = "p"
headsCollectionName = "heads" headsCollectionName = "heads"
changesCollectionName = "changes"
) )
type StorageChange struct { type StorageChange struct {
@ -85,6 +84,16 @@ func createStorage(ctx context.Context, root *treechangeproto.RawTreeChangeWithI
OrderId: firstOrder, OrderId: firstOrder,
ChangeSize: len(root.RawChange), ChangeSize: len(root.RawChange),
} }
headsColl, err := store.Collection(ctx, headsCollectionName)
if err != nil {
return nil, err
}
st.headsColl = headsColl
changesColl, err := store.Collection(ctx, root.Id)
if err != nil {
return nil, err
}
st.changesColl = changesColl
orderIdx := anystore.IndexInfo{ orderIdx := anystore.IndexInfo{
Name: orderKey, Name: orderKey,
Fields: []string{orderKey}, Fields: []string{orderKey},
@ -94,16 +103,6 @@ func createStorage(ctx context.Context, root *treechangeproto.RawTreeChangeWithI
if err != nil { if err != nil {
return nil, err return nil, err
} }
headsColl, err := store.Collection(ctx, headsCollectionName)
if err != nil {
return nil, err
}
st.headsColl = headsColl
changesColl, err := store.Collection(ctx, changesCollectionName)
if err != nil {
return nil, err
}
st.changesColl = changesColl
st.arena = &anyenc.Arena{} st.arena = &anyenc.Arena{}
defer st.arena.Reset() defer st.arena.Reset()
doc := newStorageChangeValue(stChange, st.arena) doc := newStorageChangeValue(stChange, st.arena)
@ -119,12 +118,13 @@ func createStorage(ctx context.Context, root *treechangeproto.RawTreeChangeWithI
headsDoc := st.arena.NewObject() headsDoc := st.arena.NewObject()
headsDoc.Set(headsKey, newStringArrayValue([]string{root.Id}, st.arena)) headsDoc.Set(headsKey, newStringArrayValue([]string{root.Id}, st.arena))
headsDoc.Set(commonSnapshotKey, st.arena.NewString(root.Id)) headsDoc.Set(commonSnapshotKey, st.arena.NewString(root.Id))
err = st.headsColl.Insert(tx.Context(), doc) headsDoc.Set(idKey, st.arena.NewString(root.Id))
err = st.headsColl.Insert(tx.Context(), headsDoc)
if err != nil { if err != nil {
tx.Rollback() tx.Rollback()
return nil, err return nil, err
} }
return st, nil return st, tx.Commit()
} }
func newStorage(ctx context.Context, id string, store anystore.DB) (Storage, error) { func newStorage(ctx context.Context, id string, store anystore.DB) (Storage, error) {
@ -133,25 +133,25 @@ func newStorage(ctx context.Context, id string, store anystore.DB) (Storage, err
id: id, id: id,
store: store, store: store,
} }
orderIdx := anystore.IndexInfo{
Name: orderKey,
Fields: []string{orderKey},
Unique: true,
}
err := st.changesColl.EnsureIndex(ctx, orderIdx)
if err != nil {
return nil, err
}
headsColl, err := store.Collection(ctx, headsCollectionName) headsColl, err := store.Collection(ctx, headsCollectionName)
if err != nil { if err != nil {
return nil, err return nil, err
} }
st.headsColl = headsColl st.headsColl = headsColl
changesColl, err := store.Collection(ctx, changesCollectionName) changesColl, err := store.Collection(ctx, id)
if err != nil { if err != nil {
return nil, err return nil, err
} }
st.changesColl = changesColl st.changesColl = changesColl
orderIdx := anystore.IndexInfo{
Name: orderKey,
Fields: []string{orderKey},
Unique: true,
}
err = st.changesColl.EnsureIndex(ctx, orderIdx)
if err != nil {
return nil, err
}
st.arena = &anyenc.Arena{} st.arena = &anyenc.Arena{}
return st, nil return st, nil
} }
@ -251,7 +251,7 @@ func (s *storage) Delete() error {
tx.Rollback() tx.Rollback()
return fmt.Errorf("failed to remove document from heads collection: %w", err) return fmt.Errorf("failed to remove document from heads collection: %w", err)
} }
return nil return tx.Commit()
} }
func (s *storage) Id() string { func (s *storage) Id() string {
@ -315,6 +315,7 @@ func newStorageChangeValue(ch StorageChange, arena *anyenc.Arena) *anyenc.Value
newVal.Set(snapshotCounterKey, arena.NewNumberInt(ch.SnapshotCounter)) newVal.Set(snapshotCounterKey, arena.NewNumberInt(ch.SnapshotCounter))
newVal.Set(snapshotIdKey, arena.NewString(ch.SnapshotId)) newVal.Set(snapshotIdKey, arena.NewString(ch.SnapshotId))
newVal.Set(changeSizeKey, arena.NewNumberInt(ch.ChangeSize)) newVal.Set(changeSizeKey, arena.NewNumberInt(ch.ChangeSize))
newVal.Set(idKey, arena.NewString(ch.Id))
if len(ch.PrevIds) != 0 { if len(ch.PrevIds) != 0 {
newVal.Set(prevIdsKey, newStringArrayValue(ch.PrevIds, arena)) newVal.Set(prevIdsKey, newStringArrayValue(ch.PrevIds, arena))
} }