Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammad Alian committed Sep 23, 2020
1 parent 8e77663 commit 12ce219
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
1 change: 1 addition & 0 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ type entry struct {
expiresAt int64
kind string
tags []uint64
deleted bool
}
20 changes: 11 additions & 9 deletions pot.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,13 @@ func (p *pot) DropTags(tags ...string) {
func (p *pot) Drop(keys ...string) {
for _, key := range keys {
e, ok := p.getEntry(key)
if ok {
p.dropEntry(e)
if !ok {
continue
}
e.deleted = true
p.tags.drop(e)
p.shards[e.shard].DropEntry(e.key)
e = nil
}
}

Expand Down Expand Up @@ -84,7 +88,7 @@ func (p *pot) getEntry(key string) (*entry, bool) {

func (p *pot) Get(key string, i interface{}) (err error) {
ent, ok := p.getEntry(key)
if !ok {
if !ok || ent.deleted {
return NotFoundErr
}

Expand Down Expand Up @@ -144,6 +148,7 @@ func (p *pot) dropExpiredEntries() {
}
if now > entry.expiresAt {
expiredWindows++
entry.deleted = true
expiredEntries = append(expiredEntries, entry)
} else {
break
Expand All @@ -157,14 +162,11 @@ func (p *pot) dropExpiredEntries() {

func (p *pot) dropEntries(entries ...*entry) {
for _, entry := range entries {
if !entry.deleted {
continue
}
p.tags.drop(entry)
p.shards[entry.shard].DropEntry(entry.key)
entry = nil
}
}

func (p *pot) dropEntry(e *entry) {
p.tags.drop(e)
p.shards[e.shard].DropEntry(e.key)
e = nil
}
7 changes: 6 additions & 1 deletion shard.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ func (s *shard) GetEntry(key uint64) (ent *entry, ok bool) {

func (s *shard) SetEntry(key uint64, ent *entry) {
s.rw.Lock()
s.entries[key] = ent
e, ok := s.entries[key]
if ok {
*e = *ent
} else {
s.entries[key] = ent
}
s.rw.Unlock()
}

Expand Down
13 changes: 12 additions & 1 deletion tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,18 @@ func (t *tags) getEntries(tag uint64) (entries entrySlice) {

func (t *tags) dropTags(tags ...uint64) {
for _, tag := range tags {
entries := t.getEntries(tag)
var entries entrySlice
t.rw.RLock()
if _, ok := t.pairs[tag]; !ok {
t.rw.RUnlock()
continue
}
for _, e := range t.pairs[tag] {
e.deleted = true
entries = append(entries, e)
}
t.rw.RUnlock()

t.rw.Lock()
delete(t.pairs, tag)
t.rw.Unlock()
Expand Down

0 comments on commit 12ce219

Please sign in to comment.