Add synchronization to inmemory store

This commit is contained in:
mcrakhman 2022-07-12 09:01:06 +02:00 committed by Mikhail Iudin
parent 67dba37ea3
commit af653405d0
No known key found for this signature in database
GPG Key ID: FAAAA8BAABDFF1C0
2 changed files with 23 additions and 3 deletions

View File

@ -52,7 +52,7 @@ func (c *changeBuilder) SetMakeSnapshot(b bool) {
c.makeSnapshot = b c.makeSnapshot = b
} }
func (c *changeBuilder) UserAdd(identity string, encryptionKey keys.EncryptionPubKey) { func (c *changeBuilder) UserAdd(identity string, encryptionKey keys.EncryptionPubKey, permissions pb.ACLChangeUserPermissions) {
//TODO implement me //TODO implement me
panic("implement me") panic("implement me")
} }

View File

@ -18,7 +18,7 @@ type inMemoryThread struct {
orphans []string orphans []string
changes map[string]*RawChange changes map[string]*RawChange
sync.Mutex sync.RWMutex
} }
func NewInMemoryThread(firstChange *RawChange) (Thread, error) { func NewInMemoryThread(firstChange *RawChange) (Thread, error) {
@ -44,27 +44,37 @@ func NewInMemoryThread(firstChange *RawChange) (Thread, error) {
heads: []string{firstChange.Id}, heads: []string{firstChange.Id},
orphans: nil, orphans: nil,
changes: changes, changes: changes,
Mutex: sync.Mutex{}, RWMutex: sync.RWMutex{},
}, nil }, nil
} }
func (t *inMemoryThread) ID() string { func (t *inMemoryThread) ID() string {
t.RLock()
defer t.RUnlock()
return t.id return t.id
} }
func (t *inMemoryThread) Header() *pb.ThreadHeader { func (t *inMemoryThread) Header() *pb.ThreadHeader {
t.RLock()
defer t.RUnlock()
return t.header return t.header
} }
func (t *inMemoryThread) Heads() []string { func (t *inMemoryThread) Heads() []string {
t.RLock()
defer t.RUnlock()
return t.heads return t.heads
} }
func (t *inMemoryThread) Orphans() []string { func (t *inMemoryThread) Orphans() []string {
t.RLock()
defer t.RUnlock()
return t.orphans return t.orphans
} }
func (t *inMemoryThread) SetHeads(heads []string) { func (t *inMemoryThread) SetHeads(heads []string) {
t.Lock()
defer t.Unlock()
t.heads = t.heads[:0] t.heads = t.heads[:0]
for _, h := range heads { for _, h := range heads {
@ -73,20 +83,28 @@ func (t *inMemoryThread) SetHeads(heads []string) {
} }
func (t *inMemoryThread) RemoveOrphans(orphans ...string) { func (t *inMemoryThread) RemoveOrphans(orphans ...string) {
t.Lock()
defer t.Unlock()
t.orphans = slice.Difference(t.orphans, orphans) t.orphans = slice.Difference(t.orphans, orphans)
} }
func (t *inMemoryThread) AddOrphans(orphans ...string) { func (t *inMemoryThread) AddOrphans(orphans ...string) {
t.Lock()
defer t.Unlock()
t.orphans = append(t.orphans, orphans...) t.orphans = append(t.orphans, orphans...)
} }
func (t *inMemoryThread) AddRawChange(change *RawChange) error { func (t *inMemoryThread) AddRawChange(change *RawChange) error {
t.Lock()
defer t.Unlock()
// TODO: better to do deep copy // TODO: better to do deep copy
t.changes[change.Id] = change t.changes[change.Id] = change
return nil return nil
} }
func (t *inMemoryThread) AddChange(change aclchanges.Change) error { func (t *inMemoryThread) AddChange(change aclchanges.Change) error {
t.Lock()
defer t.Unlock()
signature := change.Signature() signature := change.Signature()
id := change.CID() id := change.CID()
aclChange := change.ProtoChange() aclChange := change.ProtoChange()
@ -105,6 +123,8 @@ func (t *inMemoryThread) AddChange(change aclchanges.Change) error {
} }
func (t *inMemoryThread) GetChange(ctx context.Context, changeId string) (*RawChange, error) { func (t *inMemoryThread) GetChange(ctx context.Context, changeId string) (*RawChange, error) {
t.RLock()
defer t.RUnlock()
if res, exists := t.changes[changeId]; exists { if res, exists := t.changes[changeId]; exists {
return res, nil return res, nil
} }