Add space and document creation logic for client
This commit is contained in:
14 files changed
+583
-376
No files matched your search
@@ -3,9 +3,10 @@ package clientcache
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"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/common/app"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/app/logger"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/client/clientspace"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/cache"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/ocache"
|
||||
"time"
|
||||
@@ -21,6 +22,7 @@ const spaceKey ctxKey = 0
|
||||
type treeCache struct {
|
||||
gcttl int
|
||||
cache ocache.OCache
|
||||
docService document.Service
|
||||
clientService clientspace.Service
|
||||
}
|
||||
|
||||
@@ -40,14 +42,16 @@ func (c *treeCache) Close(ctx context.Context) (err error) {
|
||||
|
||||
func (c *treeCache) Init(a *app.App) (err error) {
|
||||
c.clientService = a.MustComponent(clientspace.CName).(clientspace.Service)
|
||||
c.docService = a.MustComponent(document.CName).(document.Service)
|
||||
c.cache = ocache.New(
|
||||
func(ctx context.Context, id string) (value ocache.Object, err error) {
|
||||
spaceId := ctx.Value(spaceKey).(string)
|
||||
space, err := c.clientService.GetSpace(ctx, spaceId)
|
||||
container, err := c.clientService.GetSpace(ctx, spaceId)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return space.BuildTree(ctx, id, nil)
|
||||
defer container.Release()
|
||||
return document.NewTextDocument(context.Background(), container.Object, id, c.docService)
|
||||
},
|
||||
ocache.WithLogger(log.Sugar()),
|
||||
ocache.WithGCPeriod(time.Minute),
|
||||
|
||||
@@ -2,6 +2,7 @@ package clientspace
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/client/util"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/app"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/app/logger"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace"
|
||||
@@ -22,9 +23,9 @@ func New() Service {
|
||||
}
|
||||
|
||||
type Service interface {
|
||||
GetSpace(ctx context.Context, id string) (commonspace.Space, error)
|
||||
CreateSpace(ctx context.Context, payload commonspace.SpaceCreatePayload) (commonspace.Space, error)
|
||||
DeriveSpace(ctx context.Context, payload commonspace.SpaceDerivePayload) (commonspace.Space, error)
|
||||
GetSpace(ctx context.Context, id string) (util.ReleaseContainer[commonspace.Space], error)
|
||||
CreateSpace(ctx context.Context, payload commonspace.SpaceCreatePayload) (util.ReleaseContainer[commonspace.Space], error)
|
||||
DeriveSpace(ctx context.Context, payload commonspace.SpaceDerivePayload) (util.ReleaseContainer[commonspace.Space], error)
|
||||
app.ComponentRunnable
|
||||
}
|
||||
|
||||
@@ -63,38 +64,53 @@ func (s *service) Run(ctx context.Context) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (s *service) CreateSpace(ctx context.Context, payload commonspace.SpaceCreatePayload) (space commonspace.Space, err error) {
|
||||
func (s *service) CreateSpace(ctx context.Context, payload commonspace.SpaceCreatePayload) (container util.ReleaseContainer[commonspace.Space], err error) {
|
||||
id, err := s.commonSpace.CreateSpace(ctx, payload)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
obj, err := s.commonSpace.GetSpace(ctx, id)
|
||||
obj, err := s.spaceCache.Get(ctx, id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return obj.(commonspace.Space), nil
|
||||
return util.ReleaseContainer[commonspace.Space]{
|
||||
Object: obj.(commonspace.Space),
|
||||
Release: func() {
|
||||
s.spaceCache.Release(id)
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *service) DeriveSpace(ctx context.Context, payload commonspace.SpaceDerivePayload) (space commonspace.Space, err error) {
|
||||
func (s *service) DeriveSpace(ctx context.Context, payload commonspace.SpaceDerivePayload) (container util.ReleaseContainer[commonspace.Space], err error) {
|
||||
id, err := s.commonSpace.DeriveSpace(ctx, payload)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
obj, err := s.commonSpace.GetSpace(ctx, id)
|
||||
obj, err := s.spaceCache.Get(ctx, id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return obj.(commonspace.Space), nil
|
||||
return util.ReleaseContainer[commonspace.Space]{
|
||||
Object: obj.(commonspace.Space),
|
||||
Release: func() {
|
||||
s.spaceCache.Release(id)
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *service) GetSpace(ctx context.Context, id string) (commonspace.Space, error) {
|
||||
func (s *service) GetSpace(ctx context.Context, id string) (container util.ReleaseContainer[commonspace.Space], err error) {
|
||||
v, err := s.spaceCache.Get(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return
|
||||
}
|
||||
return v.(commonspace.Space), nil
|
||||
return util.ReleaseContainer[commonspace.Space]{
|
||||
Object: v.(commonspace.Space),
|
||||
Release: func() {
|
||||
s.spaceCache.Release(id)
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *service) Close(ctx context.Context) (err error) {
|
||||
|
||||
@@ -1,13 +1,97 @@
|
||||
package document
|
||||
|
||||
import "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app"
|
||||
import (
|
||||
"context"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/client/clientspace"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/account"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/app"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/app/logger"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/cache"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/synctree/updatelistener"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/tree"
|
||||
)
|
||||
|
||||
type Service interface {
|
||||
app.Component
|
||||
updatelistener.UpdateListener
|
||||
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)
|
||||
AddText(spaceId, documentId, text string) (err error)
|
||||
DumpDocumentTree(spaceId, documentId string) (dump string, err error)
|
||||
}
|
||||
|
||||
const CName = "client.document"
|
||||
|
||||
var log = logger.NewNamed(CName)
|
||||
|
||||
type service struct {
|
||||
account account.Service
|
||||
spaceService clientspace.Service
|
||||
cache cache.TreeCache
|
||||
}
|
||||
|
||||
func New() Service {
|
||||
return &service{}
|
||||
}
|
||||
|
||||
func (s *service) Init(a *app.App) (err error) {
|
||||
s.account = a.MustComponent(account.CName).(account.Service)
|
||||
s.spaceService = a.MustComponent(clientspace.CName).(clientspace.Service)
|
||||
s.cache = a.MustComponent(cache.CName).(cache.TreeCache)
|
||||
return
|
||||
}
|
||||
|
||||
func (s *service) Name() (name string) {
|
||||
return CName
|
||||
}
|
||||
|
||||
func (s *service) CreateDocument(spaceId string) (id string, err error) {
|
||||
spaceRef, err := s.spaceService.GetSpace(context.Background(), spaceId)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer spaceRef.Release()
|
||||
doc, err := createTextDocument(context.Background(), spaceRef.Object, s.account, s)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
id = doc.Tree().ID()
|
||||
return
|
||||
}
|
||||
|
||||
func (s *service) GetAllDocumentIds(spaceId string) (ids []string, err error) {
|
||||
spaceRef, err := s.spaceService.GetSpace(context.Background(), spaceId)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer spaceRef.Release()
|
||||
ids = spaceRef.Object.StoredIds()
|
||||
return
|
||||
}
|
||||
|
||||
func (s *service) AddText(spaceId, documentId, text string) (err error) {
|
||||
doc, err := s.cache.GetTree(context.Background(), spaceId, documentId)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer doc.Release()
|
||||
return doc.TreeContainer.(TextDocument).AddText(text)
|
||||
}
|
||||
|
||||
func (s *service) DumpDocumentTree(spaceId, documentId string) (dump string, err error) {
|
||||
doc, err := s.cache.GetTree(context.Background(), spaceId, documentId)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer doc.Release()
|
||||
return doc.TreeContainer.Tree().DebugDump()
|
||||
}
|
||||
|
||||
func (s *service) Update(tree tree.ObjectTree) {
|
||||
|
||||
}
|
||||
|
||||
func (s *service) Rebuild(tree tree.ObjectTree) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
package document
|
||||
|
||||
import (
|
||||
"context"
|
||||
"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/commonspace/cache"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/synctree/updatelistener"
|
||||
testchanges "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/testutils/testchanges/proto"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/tree"
|
||||
"github.com/gogo/protobuf/proto"
|
||||
)
|
||||
|
||||
type TextDocument interface {
|
||||
cache.TreeContainer
|
||||
AddText(text string) error
|
||||
Text() (string, error)
|
||||
TreeDump() string
|
||||
Close() error
|
||||
}
|
||||
|
||||
type textDocument struct {
|
||||
objTree tree.ObjectTree
|
||||
account account.Service
|
||||
}
|
||||
|
||||
func createTextDocument(
|
||||
ctx context.Context,
|
||||
space commonspace.Space,
|
||||
account account.Service,
|
||||
listener updatelistener.UpdateListener) (doc TextDocument, err error) {
|
||||
payload := tree.ObjectTreeCreatePayload{
|
||||
SignKey: account.Account().SignKey,
|
||||
SpaceId: space.Id(),
|
||||
Identity: account.Account().Identity,
|
||||
}
|
||||
t, err := space.CreateTree(ctx, payload, listener)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return &textDocument{
|
||||
objTree: t,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func NewTextDocument(ctx context.Context, space commonspace.Space, id string, listener updatelistener.UpdateListener) (doc TextDocument, err error) {
|
||||
t, err := space.BuildTree(ctx, id, listener)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return &textDocument{
|
||||
objTree: t,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (t *textDocument) Tree() tree.ObjectTree {
|
||||
return t.objTree
|
||||
}
|
||||
|
||||
func (t *textDocument) AddText(text string) (err error) {
|
||||
content := &testchanges.TextContentValueOfTextAppend{
|
||||
TextAppend: &testchanges.TextAppend{Text: text},
|
||||
}
|
||||
change := &testchanges.TextData{
|
||||
Content: []*testchanges.TextContent{
|
||||
{content},
|
||||
},
|
||||
Snapshot: nil,
|
||||
}
|
||||
res, err := change.Marshal()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
_, err = t.objTree.AddContent(context.Background(), tree.SignableChangeContent{
|
||||
Data: res,
|
||||
Key: t.account.Account().SignKey,
|
||||
Identity: t.account.Account().Identity,
|
||||
IsSnapshot: false,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (t *textDocument) Text() (text string, err error) {
|
||||
t.objTree.RLock()
|
||||
defer t.objTree.RUnlock()
|
||||
|
||||
err = t.objTree.Iterate(
|
||||
func(decrypted []byte) (any, error) {
|
||||
textChange := &testchanges.TextData{}
|
||||
err = proto.Unmarshal(decrypted, textChange)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, cnt := range textChange.Content {
|
||||
if cnt.GetTextAppend() != nil {
|
||||
text += cnt.GetTextAppend().Text
|
||||
}
|
||||
}
|
||||
return textChange, nil
|
||||
}, func(change *tree.Change) bool {
|
||||
return true
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (t *textDocument) TreeDump() string {
|
||||
return t.TreeDump()
|
||||
}
|
||||
|
||||
func (t *textDocument) Close() error {
|
||||
return nil
|
||||
}
|
||||
+61
-1
@@ -2,10 +2,70 @@ module github.com/anytypeio/go-anytype-infrastructure-experiments/client
|
||||
|
||||
replace github.com/anytypeio/go-anytype-infrastructure-experiments/common => ../common
|
||||
|
||||
|
||||
go 1.19
|
||||
|
||||
require (
|
||||
github.com/anytypeio/go-anytype-infrastructure-experiments/common v0.0.0-00010101000000-000000000000
|
||||
github.com/dgraph-io/badger/v3 v3.2103.3
|
||||
github.com/gogo/protobuf v1.3.2
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/anytypeio/go-chash v0.0.0-20220629194632-4ad1154fe232 // indirect
|
||||
github.com/beorn7/perks v1.0.1 // indirect
|
||||
github.com/cespare/xxhash v1.1.0 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.1.2 // indirect
|
||||
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 // indirect
|
||||
github.com/dgraph-io/ristretto v0.1.1 // indirect
|
||||
github.com/dustin/go-humanize v1.0.0 // indirect
|
||||
github.com/fogleman/gg v1.3.0 // indirect
|
||||
github.com/goccy/go-graphviz v0.0.9 // indirect
|
||||
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
|
||||
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect
|
||||
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
|
||||
github.com/golang/protobuf v1.5.2 // indirect
|
||||
github.com/golang/snappy v0.0.3 // indirect
|
||||
github.com/google/flatbuffers v1.12.1 // indirect
|
||||
github.com/huandu/skiplist v1.2.0 // indirect
|
||||
github.com/ipfs/go-cid v0.3.2 // indirect
|
||||
github.com/ipfs/go-log/v2 v2.5.1 // indirect
|
||||
github.com/klauspost/compress v1.15.10 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.1.1 // indirect
|
||||
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
|
||||
github.com/libp2p/go-libp2p v0.23.2 // indirect
|
||||
github.com/libp2p/go-libp2p-core v0.20.1 // indirect
|
||||
github.com/libp2p/go-openssl v0.1.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.16 // indirect
|
||||
github.com/mattn/go-pointer v0.0.1 // indirect
|
||||
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
|
||||
github.com/minio/sha256-simd v1.0.0 // indirect
|
||||
github.com/mr-tron/base58 v1.2.0 // indirect
|
||||
github.com/multiformats/go-base32 v0.1.0 // indirect
|
||||
github.com/multiformats/go-base36 v0.1.0 // indirect
|
||||
github.com/multiformats/go-multiaddr v0.7.0 // indirect
|
||||
github.com/multiformats/go-multibase v0.1.1 // indirect
|
||||
github.com/multiformats/go-multicodec v0.6.0 // indirect
|
||||
github.com/multiformats/go-multihash v0.2.1 // indirect
|
||||
github.com/multiformats/go-varint v0.0.6 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/prometheus/client_golang v1.13.0 // indirect
|
||||
github.com/prometheus/client_model v0.2.0 // indirect
|
||||
github.com/prometheus/common v0.37.0 // indirect
|
||||
github.com/prometheus/procfs v0.8.0 // indirect
|
||||
github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572 // indirect
|
||||
github.com/spaolacci/murmur3 v1.1.0 // indirect
|
||||
github.com/zeebo/blake3 v0.2.3 // indirect
|
||||
github.com/zeebo/errs v1.3.0 // indirect
|
||||
go.opencensus.io v0.23.0 // indirect
|
||||
go.uber.org/atomic v1.10.0 // indirect
|
||||
go.uber.org/multierr v1.8.0 // indirect
|
||||
go.uber.org/zap v1.23.0 // indirect
|
||||
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // indirect
|
||||
golang.org/x/image v0.0.0-20200119044424-58c23975cae1 // indirect
|
||||
golang.org/x/net v0.0.0-20220920183852-bf014ff85ad5 // indirect
|
||||
golang.org/x/sys v0.0.0-20221010170243-090e33056c14 // indirect
|
||||
google.golang.org/protobuf v1.28.1 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
lukechampine.com/blake3 v1.1.7 // indirect
|
||||
storj.io/drpc v0.0.32 // indirect
|
||||
)
|
||||
@@ -0,0 +1,6 @@
|
||||
package util
|
||||
|
||||
type ReleaseContainer[T any] struct {
|
||||
Object T
|
||||
Release func()
|
||||
}
|
||||
Reference in new issue
Block a user