mirror of
https://github.com/anyproto/any-sync.git
synced 2025-06-10 18:10:54 +09:00
Add read and write methods for treecache
This commit is contained in:
parent
588bab92a0
commit
3eedbf4188
4 changed files with 25 additions and 8 deletions
|
@ -79,7 +79,7 @@ func (s *service) treeDump(w http.ResponseWriter, req *http.Request) {
|
|||
dump string
|
||||
err error
|
||||
)
|
||||
err = s.treeCache.Do(context.Background(), treeId, func(tree acltree.ACLTree) error {
|
||||
err = s.treeCache.DoWrite(context.Background(), treeId, func(tree acltree.ACLTree) error {
|
||||
dump, err = tree.DebugDump()
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
@ -71,7 +71,7 @@ func (s *service) UpdateDocument(ctx context.Context, id, text string) (err erro
|
|||
log.With(zap.String("id", id), zap.String("text", text)).
|
||||
Debug("updating document")
|
||||
|
||||
err = s.treeCache.Do(ctx, id, func(tree acltree.ACLTree) error {
|
||||
err = s.treeCache.DoWrite(ctx, id, func(tree acltree.ACLTree) error {
|
||||
ch, err = tree.AddContent(ctx, func(builder acltree.ChangeBuilder) error {
|
||||
builder.AddChangeContent(
|
||||
&testchangepb.PlainTextChangeData{
|
||||
|
|
|
@ -79,7 +79,7 @@ func (r *requestHandler) HandleHeadUpdate(ctx context.Context, senderId string,
|
|||
log.With(zap.String("peerId", senderId), zap.String("treeId", update.TreeId)).
|
||||
Debug("processing head update")
|
||||
|
||||
err = r.treeCache.Do(ctx, update.TreeId, func(tree acltree.ACLTree) error {
|
||||
err = r.treeCache.DoWrite(ctx, update.TreeId, func(tree acltree.ACLTree) error {
|
||||
// TODO: check if we already have those changes
|
||||
result, err = tree.AddRawChanges(ctx, update.Changes...)
|
||||
if err != nil {
|
||||
|
@ -133,7 +133,7 @@ func (r *requestHandler) HandleFullSyncRequest(ctx context.Context, senderId str
|
|||
log.With(zap.String("peerId", senderId), zap.String("treeId", request.TreeId)).
|
||||
Debug("processing full sync request")
|
||||
|
||||
err = r.treeCache.Do(ctx, request.TreeId, func(tree acltree.ACLTree) error {
|
||||
err = r.treeCache.DoWrite(ctx, request.TreeId, func(tree acltree.ACLTree) error {
|
||||
// TODO: check if we already have those changes
|
||||
// if we have non-empty request
|
||||
if len(request.Heads) != 0 {
|
||||
|
@ -177,7 +177,7 @@ func (r *requestHandler) HandleFullSyncResponse(ctx context.Context, senderId st
|
|||
log.With(zap.String("peerId", senderId), zap.String("treeId", response.TreeId)).
|
||||
Debug("processing full sync response")
|
||||
|
||||
err = r.treeCache.Do(ctx, response.TreeId, func(tree acltree.ACLTree) error {
|
||||
err = r.treeCache.DoWrite(ctx, response.TreeId, func(tree acltree.ACLTree) error {
|
||||
// TODO: check if we already have those changes
|
||||
result, err = tree.AddRawChanges(ctx, response.Changes...)
|
||||
if err != nil {
|
||||
|
|
|
@ -22,7 +22,8 @@ type ChangeBuildFunc = func(builder acltree.ChangeBuilder) error
|
|||
var log = logger.NewNamed("treecache")
|
||||
|
||||
type Service interface {
|
||||
Do(ctx context.Context, treeId string, f ACLTreeFunc) error
|
||||
DoWrite(ctx context.Context, treeId string, f ACLTreeFunc) error
|
||||
DoRead(ctx context.Context, treeId string, f ACLTreeFunc) error
|
||||
Add(ctx context.Context, treeId string, header *treepb.TreeHeader, changes []*aclpb.RawChange, f ACLTreeFunc) error
|
||||
Create(ctx context.Context, build ChangeBuildFunc, f ACLTreeFunc) error
|
||||
}
|
||||
|
@ -49,10 +50,10 @@ func (s *service) Create(ctx context.Context, build ChangeBuildFunc, f ACLTreeFu
|
|||
return err
|
||||
}
|
||||
|
||||
return s.Do(ctx, id, f)
|
||||
return s.DoWrite(ctx, id, f)
|
||||
}
|
||||
|
||||
func (s *service) Do(ctx context.Context, treeId string, f ACLTreeFunc) error {
|
||||
func (s *service) DoWrite(ctx context.Context, treeId string, f ACLTreeFunc) error {
|
||||
log.
|
||||
With(zap.String("treeId", treeId)).
|
||||
Debug("requesting tree from cache to perform operation")
|
||||
|
@ -68,6 +69,22 @@ func (s *service) Do(ctx context.Context, treeId string, f ACLTreeFunc) error {
|
|||
return f(tree.(acltree.ACLTree))
|
||||
}
|
||||
|
||||
func (s *service) DoRead(ctx context.Context, treeId string, f ACLTreeFunc) error {
|
||||
log.
|
||||
With(zap.String("treeId", treeId)).
|
||||
Debug("requesting tree from cache to perform operation")
|
||||
|
||||
tree, err := s.cache.Get(ctx, treeId)
|
||||
defer s.cache.Release(treeId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
aclTree := tree.(acltree.ACLTree)
|
||||
aclTree.RLock()
|
||||
defer aclTree.RUnlock()
|
||||
return f(tree.(acltree.ACLTree))
|
||||
}
|
||||
|
||||
func (s *service) Add(ctx context.Context, treeId string, header *treepb.TreeHeader, changes []*aclpb.RawChange, f ACLTreeFunc) error {
|
||||
log.
|
||||
With(zap.String("treeId", treeId), zap.Int("len(changes)", len(changes))).
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue