diff --git a/client/api/controller.go b/client/api/controller.go index 7df3e356..281bceb8 100644 --- a/client/api/controller.go +++ b/client/api/controller.go @@ -1,40 +1,79 @@ package api +import ( + "context" + "github.com/anytypeio/go-anytype-infrastructure-experiments/client/clientspace" + "github.com/anytypeio/go-anytype-infrastructure-experiments/client/document" + "github.com/anytypeio/go-anytype-infrastructure-experiments/client/storage" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/account" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys/symmetric" + "golang.org/x/exp/rand" +) + type Controller interface { // DeriveSpace derives the space from current account DeriveSpace() (id string, err error) // CreateSpace creates new space with random data CreateSpace() (id string, err error) - GetAllSpacesIds() (ids []string, err error) + // AllSpaceIds returns ids of all spaces + AllSpaceIds(spaceId string) (ids []string, err error) // LoadSpace asks node to load a particular space LoadSpace(id string) (err error) + // CreateDocument creates new document in space CreateDocument(spaceId string) (id string, err error) - GetAllDocumentIds(spaceId string) (ids []string, err error) - AddText(documentId, text string) (err error) - DumpDocumentTree(documentId string) (err error) + // AllDocumentIds gets all ids of documents in space + AllDocumentIds(spaceId string) (ids []string, err error) + // AddText adds text to space document + AddText(spaceId, documentId, text string) (err error) + // DumpDocumentTree dumps the tree data into string + DumpDocumentTree(spaceId, documentId string) (dump string, err error) - GetValidInvites(spaceId string) (invites []string, err error) + ValidInvites(spaceId string) (invites []string, err error) GenerateInvite(spaceId string) (invite string, err error) JoinSpace(invite string) (err error) } type controller struct { + spaceService clientspace.Service + storageService storage.ClientStorage + docService document.Service + account account.Service } func (c *controller) DeriveSpace() (id string, err error) { - //TODO implement me - panic("implement me") + sp, err := c.spaceService.DeriveSpace(context.Background(), commonspace.SpaceDerivePayload{ + SigningKey: c.account.Account().SignKey, + EncryptionKey: c.account.Account().EncKey, + }) + if err != nil { + return + } + id = sp.Id() + return } func (c *controller) CreateSpace() (id string, err error) { - //TODO implement me - panic("implement me") + key, err := symmetric.NewRandom() + if err != nil { + return + } + sp, err := c.spaceService.CreateSpace(context.Background(), commonspace.SpaceCreatePayload{ + SigningKey: c.account.Account().SignKey, + EncryptionKey: c.account.Account().EncKey, + ReadKey: key.Bytes(), + ReplicationKey: rand.Uint64(), + }) + if err != nil { + return + } + id = sp.Id() + return } -func (c *controller) GetAllSpacesIds() (ids []string, err error) { - //TODO implement me - panic("implement me") +func (c *controller) AllSpaceIds(spaceId string) (ids []string, err error) { + return c.storageService.AllSpaceIds() } func (c *controller) LoadSpace(id string) (err error) { @@ -43,26 +82,22 @@ func (c *controller) LoadSpace(id string) (err error) { } func (c *controller) CreateDocument(spaceId string) (id string, err error) { - //TODO implement me - panic("implement me") + return c.docService.CreateDocument(spaceId) } -func (c *controller) GetAllDocumentIds(spaceId string) (ids []string, err error) { - //TODO implement me - panic("implement me") +func (c *controller) AllDocumentIds(spaceId string) (ids []string, err error) { + return c.docService.AllDocumentIds(spaceId) } -func (c *controller) AddText(documentId, text string) (err error) { - //TODO implement me - panic("implement me") +func (c *controller) AddText(spaceId, documentId, text string) (err error) { + return c.docService.AddText(spaceId, documentId, text) } -func (c *controller) DumpDocumentTree(documentId string) (err error) { - //TODO implement me - panic("implement me") +func (c *controller) DumpDocumentTree(spaceId, documentId string) (dump string, err error) { + return c.docService.DumpDocumentTree(spaceId, documentId) } -func (c *controller) GetValidInvites(spaceId string) (invites []string, err error) { +func (c *controller) ValidInvites(spaceId string) (invites []string, err error) { //TODO implement me panic("implement me") } diff --git a/client/clientspace/service.go b/client/clientspace/service.go index bb0bd1b3..876dcd3f 100644 --- a/client/clientspace/service.go +++ b/client/clientspace/service.go @@ -55,10 +55,6 @@ func (s *service) Name() (name string) { } func (s *service) Run(ctx context.Context) (err error) { - go func() { - time.Sleep(time.Second * 5) - _, _ = s.GetSpace(ctx, "testDSpace") - }() return } diff --git a/client/document/service.go b/client/document/service.go index 514bac94..47c2d250 100644 --- a/client/document/service.go +++ b/client/document/service.go @@ -17,7 +17,7 @@ type Service interface { app.Component updatelistener.UpdateListener CreateDocument(spaceId string) (id string, err error) - GetAllDocumentIds(spaceId string) (ids []string, err error) + AllDocumentIds(spaceId string) (ids []string, err error) AddText(spaceId, documentId, text string) (err error) DumpDocumentTree(spaceId, documentId string) (dump string, err error) } @@ -60,7 +60,7 @@ func (s *service) CreateDocument(spaceId string) (id string, err error) { return } -func (s *service) GetAllDocumentIds(spaceId string) (ids []string, err error) { +func (s *service) AllDocumentIds(spaceId string) (ids []string, err error) { space, err := s.spaceService.GetSpace(context.Background(), spaceId) if err != nil { return diff --git a/client/document/textdocument.go b/client/document/textdocument.go index 8f8633f3..1d6d3238 100644 --- a/client/document/textdocument.go +++ b/client/document/textdocument.go @@ -58,7 +58,7 @@ func (t *textDocument) Tree() tree.ObjectTree { } func (t *textDocument) AddText(text string) (err error) { - content := &testchanges.TextContentValueOfTextAppend{ + content := &testchanges.TextContent_TextAppend{ TextAppend: &testchanges.TextAppend{Text: text}, } change := &testchanges.TextData{ diff --git a/client/storage/keys.go b/client/storage/keys.go index 81ee43c6..05bf7e7f 100644 --- a/client/storage/keys.go +++ b/client/storage/keys.go @@ -65,7 +65,7 @@ type spaceKeys struct { func newSpaceKeys(spaceId string) spaceKeys { return spaceKeys{ - headerKey: storage.JoinStringsToBytes("space", spaceId), + headerKey: storage.JoinStringsToBytes("space", "header", spaceId), treePrefixKey: storage.JoinStringsToBytes("space", spaceId, "t", "rootId"), } } @@ -77,3 +77,15 @@ func (s spaceKeys) HeaderKey() []byte { func (s spaceKeys) TreeRootPrefix() []byte { return s.treePrefixKey } + +type storageServiceKeys struct { + spacePrefix []byte +} + +func newStorageServiceKeys() storageServiceKeys { + return storageServiceKeys{spacePrefix: []byte("space/header")} +} + +func (s storageServiceKeys) SpacePrefix() []byte { + return s.spacePrefix +} diff --git a/client/storage/storageservice.go b/client/storage/storageservice.go index 29082c92..57a8cc11 100644 --- a/client/storage/storageservice.go +++ b/client/storage/storageservice.go @@ -1,23 +1,30 @@ package storage import ( - "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app" "github.com/anytypeio/go-anytype-infrastructure-experiments/client/badgerprovider" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/storage" "github.com/dgraph-io/badger/v3" ) type storageService struct { - db *badger.DB + keys storageServiceKeys + db *badger.DB } -func New() storage.SpaceStorageProvider { +type ClientStorage interface { + storage.SpaceStorageProvider + AllSpaceIds() (ids []string, err error) +} + +func New() ClientStorage { return &storageService{} } func (s *storageService) Init(a *app.App) (err error) { provider := a.MustComponent(badgerprovider.CName).(badgerprovider.BadgerProvider) s.db = provider.Badger() + s.keys = newStorageServiceKeys() return } @@ -32,3 +39,26 @@ func (s *storageService) SpaceStorage(id string) (storage.SpaceStorage, error) { func (s *storageService) CreateSpaceStorage(payload storage.SpaceStorageCreatePayload) (storage.SpaceStorage, error) { return createSpaceStorage(s.db, payload) } + +func (s *storageService) AllSpaceIds() (ids []string, err error) { + err = s.db.View(func(txn *badger.Txn) error { + opts := badger.DefaultIteratorOptions + opts.PrefetchValues = false + opts.Prefix = s.keys.SpacePrefix() + + it := txn.NewIterator(opts) + defer it.Close() + + for it.Rewind(); it.Valid(); it.Next() { + item := it.Item() + id := item.Key() + if len(id) <= len(s.keys.SpacePrefix())+1 { + continue + } + id = id[len(s.keys.SpacePrefix())+1:] + ids = append(ids, string(id)) + } + return nil + }) + return +}