Create space with settings
This commit is contained in:
1 parent
5d3e79e506
commit
bd3aff19e7
14 files changed
+621
-256
No files matched your search
@@ -51,7 +51,10 @@ func newSpaceKeys(spaceId string) spaceKeys {
|
||||
return spaceKeys{headerKey: storage.JoinStringsToBytes("s", spaceId)}
|
||||
}
|
||||
|
||||
var spaceIdKey = []byte("spaceId")
|
||||
var (
|
||||
spaceIdKey = []byte("spaceId")
|
||||
spaceSettingsIdKey = []byte("spaceSettingsId")
|
||||
)
|
||||
|
||||
func (s spaceKeys) SpaceIdKey() []byte {
|
||||
return spaceIdKey
|
||||
@@ -61,7 +64,11 @@ func (s spaceKeys) HeaderKey() []byte {
|
||||
return s.headerKey
|
||||
}
|
||||
|
||||
func isRootIdKey(key string) bool {
|
||||
func (s spaceKeys) SpaceSettingsIdKey() []byte {
|
||||
return spaceSettingsIdKey
|
||||
}
|
||||
|
||||
func isTreeHeadsKey(key string) bool {
|
||||
return strings.HasPrefix(key, "t/") && strings.HasSuffix(key, "/heads")
|
||||
}
|
||||
|
||||
|
||||
@@ -5,26 +5,28 @@ import (
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/app/logger"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/spacesyncproto"
|
||||
spacestorage "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/storage"
|
||||
storage "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/storage"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/storage"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/treechangeproto"
|
||||
"go.uber.org/zap"
|
||||
"path"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var defPogrebOptions = &pogreb.Options{
|
||||
BackgroundCompactionInterval: time.Minute * 5,
|
||||
}
|
||||
|
||||
var log = logger.NewNamed("storage.spacestorage")
|
||||
var (
|
||||
defPogrebOptions = &pogreb.Options{BackgroundCompactionInterval: time.Minute * 5}
|
||||
log = logger.NewNamed("storage.spacestorage")
|
||||
spaceValidationFunc = spacestorage.ValidateSpaceStorageCreatePayload
|
||||
)
|
||||
|
||||
type spaceStorage struct {
|
||||
spaceId string
|
||||
objDb *pogreb.DB
|
||||
keys spaceKeys
|
||||
aclStorage storage.ListStorage
|
||||
header *spacesyncproto.RawSpaceHeaderWithId
|
||||
mx sync.Mutex
|
||||
spaceId string
|
||||
spaceSettingsId string
|
||||
objDb *pogreb.DB
|
||||
keys spaceKeys
|
||||
aclStorage storage.ListStorage
|
||||
header *spacesyncproto.RawSpaceHeaderWithId
|
||||
mx sync.Mutex
|
||||
}
|
||||
|
||||
func newSpaceStorage(rootPath string, spaceId string) (store spacestorage.SpaceStorage, err error) {
|
||||
@@ -61,15 +63,25 @@ func newSpaceStorage(rootPath string, spaceId string) (store spacestorage.SpaceS
|
||||
return
|
||||
}
|
||||
|
||||
spaceSettingsId, err := objDb.Get(keys.SpaceSettingsIdKey())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if spaceSettingsId == nil {
|
||||
err = spacestorage.ErrSpaceStorageMissing
|
||||
return
|
||||
}
|
||||
|
||||
aclStorage, err := newListStorage(objDb)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
store = &spaceStorage{
|
||||
spaceId: spaceId,
|
||||
objDb: objDb,
|
||||
keys: keys,
|
||||
spaceId: spaceId,
|
||||
spaceSettingsId: string(spaceSettingsId),
|
||||
objDb: objDb,
|
||||
keys: keys,
|
||||
header: &spacesyncproto.RawSpaceHeaderWithId{
|
||||
RawHeader: header,
|
||||
Id: spaceId,
|
||||
@@ -88,8 +100,8 @@ func createSpaceStorage(rootPath string, payload spacestorage.SpaceStorageCreate
|
||||
}
|
||||
|
||||
defer func() {
|
||||
log.With(zap.String("id", payload.SpaceHeaderWithId.Id), zap.Error(err)).Warn("failed to create storage")
|
||||
if err != nil {
|
||||
log.With(zap.String("id", payload.SpaceHeaderWithId.Id), zap.Error(err)).Warn("failed to create storage")
|
||||
db.Close()
|
||||
}
|
||||
}()
|
||||
@@ -103,8 +115,35 @@ func createSpaceStorage(rootPath string, payload spacestorage.SpaceStorageCreate
|
||||
err = spacesyncproto.ErrSpaceExists
|
||||
return
|
||||
}
|
||||
err = spaceValidationFunc(payload)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
aclStorage, err := createListStorage(db, payload.RecWithId)
|
||||
aclStorage, err := createListStorage(db, payload.AclWithId)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
store = &spaceStorage{
|
||||
spaceId: payload.SpaceHeaderWithId.Id,
|
||||
objDb: db,
|
||||
keys: keys,
|
||||
aclStorage: aclStorage,
|
||||
spaceSettingsId: payload.SpaceSettingsWithId.Id,
|
||||
header: payload.SpaceHeaderWithId,
|
||||
}
|
||||
|
||||
_, err = store.CreateTreeStorage(storage.TreeStorageCreatePayload{
|
||||
RootRawChange: payload.SpaceSettingsWithId,
|
||||
Changes: []*treechangeproto.RawTreeChangeWithId{payload.SpaceSettingsWithId},
|
||||
Heads: []string{payload.SpaceHeaderWithId.Id},
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = db.Put(keys.SpaceSettingsIdKey(), []byte(payload.SpaceSettingsWithId.Id))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -119,13 +158,6 @@ func createSpaceStorage(rootPath string, payload spacestorage.SpaceStorageCreate
|
||||
return
|
||||
}
|
||||
|
||||
store = &spaceStorage{
|
||||
spaceId: payload.SpaceHeaderWithId.Id,
|
||||
objDb: db,
|
||||
keys: keys,
|
||||
aclStorage: aclStorage,
|
||||
header: payload.SpaceHeaderWithId,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -133,6 +165,10 @@ func (s *spaceStorage) Id() string {
|
||||
return s.spaceId
|
||||
}
|
||||
|
||||
func (s *spaceStorage) SpaceSettingsId() string {
|
||||
return s.spaceSettingsId
|
||||
}
|
||||
|
||||
func (s *spaceStorage) TreeStorage(id string) (storage.TreeStorage, error) {
|
||||
return newTreeStorage(s.objDb, id)
|
||||
}
|
||||
@@ -156,13 +192,13 @@ func (s *spaceStorage) SpaceHeader() (header *spacesyncproto.RawSpaceHeaderWithI
|
||||
func (s *spaceStorage) StoredIds() (ids []string, err error) {
|
||||
index := s.objDb.Items()
|
||||
|
||||
key, val, err := index.Next()
|
||||
key, _, err := index.Next()
|
||||
for err == nil {
|
||||
strKey := string(key)
|
||||
if isRootIdKey(strKey) {
|
||||
ids = append(ids, string(val))
|
||||
if isTreeHeadsKey(strKey) {
|
||||
ids = append(ids, getRootId(strKey))
|
||||
}
|
||||
key, val, err = index.Next()
|
||||
key, _, err = index.Next()
|
||||
}
|
||||
|
||||
if err != pogreb.ErrIterationDone {
|
||||
|
||||
@@ -20,7 +20,7 @@ func spaceTestPayload() spacestorage.SpaceStorageCreatePayload {
|
||||
Id: "aclRootId",
|
||||
}
|
||||
return spacestorage.SpaceStorageCreatePayload{
|
||||
RecWithId: aclRoot,
|
||||
AclWithId: aclRoot,
|
||||
SpaceHeaderWithId: header,
|
||||
}
|
||||
}
|
||||
@@ -32,7 +32,7 @@ func testSpace(t *testing.T, store spacestorage.SpaceStorage, payload spacestora
|
||||
|
||||
aclStorage, err := store.ACLStorage()
|
||||
require.NoError(t, err)
|
||||
testList(t, aclStorage, payload.RecWithId, payload.RecWithId.Id)
|
||||
testList(t, aclStorage, payload.AclWithId, payload.AclWithId.Id)
|
||||
}
|
||||
|
||||
func TestSpaceStorage_Create(t *testing.T) {
|
||||
|
||||
Reference in new issue
Block a user