Skip to content

Commit

Permalink
remove unnecessary rwlock (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
Preetam committed Jul 17, 2018
1 parent 7fd809a commit 9b26c20
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 33 deletions.
23 changes: 1 addition & 22 deletions cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,16 @@ func (c *Collection) NewCursor() (*Cursor, error) {
}

var rec *record
cur.current.lock.RLock()
for (cur.current.Deleted != 0 && cur.current.Deleted <= cur.snapshot) ||
for (atomic.LoadInt64(&cur.current.Deleted) != 0 && atomic.LoadInt64(&cur.current.Deleted) <= cur.snapshot) ||
(cur.current.Offset >= cur.snapshot) {
rec, err = cur.collection.readRecord(atomic.LoadInt64(&cur.current.Next[0]), false)
if err != nil {
cur.current.lock.RUnlock()
cur.current = nil
cur.err = err
return cur, nil
}
cur.current.lock.RUnlock()
cur.current = rec
cur.current.lock.RLock()
}
cur.current.lock.RUnlock()

return cur, nil
}
Expand Down Expand Up @@ -84,36 +79,28 @@ func (c *Cursor) Next() bool {
return true
}

c.current.lock.RLock()
rec, err := c.collection.readRecord(atomic.LoadInt64(&c.current.Next[0]), false)
if err != nil {
c.current.lock.RUnlock()
if atomic.LoadInt64(&c.current.Next[0]) != 0 {
c.err = err
}
c.current = nil
return false
}
c.current.lock.RUnlock()
c.current = rec

c.current.lock.RLock()
for (atomic.LoadInt64(&c.current.Deleted) != 0 && atomic.LoadInt64(&c.current.Deleted) <= c.snapshot) ||
(c.current.Offset >= c.snapshot) {
rec, err = c.collection.readRecord(atomic.LoadInt64(&c.current.Next[0]), false)
if err != nil {
c.current.lock.RUnlock()
if atomic.LoadInt64(&c.current.Next[0]) != 0 {
c.err = err
}
c.current = nil
return false
}
c.current.lock.RUnlock()
c.current = rec
c.current.lock.RLock()
}
c.current.lock.RUnlock()

return true
}
Expand Down Expand Up @@ -171,10 +158,8 @@ func (c *Cursor) Seek(key string) {
c.current = rec
c.first = true
for rec != nil {
rec.lock.RLock()
if rec.Key >= key {
if (rec.Deleted > 0 && rec.Deleted <= c.snapshot) || (rec.Offset >= c.snapshot) {
oldRec := rec
rec, err = c.collection.nextRecord(rec, 0, false)
if err != nil {
if atomic.LoadInt64(&c.current.Next[0]) != 0 {
Expand All @@ -183,15 +168,12 @@ func (c *Cursor) Seek(key string) {
c.current = nil
return
}
oldRec.lock.RUnlock()
c.current = rec
continue
}
rec.lock.RUnlock()
break
}
if (rec.Deleted > 0 && rec.Deleted <= c.snapshot) || (rec.Offset >= c.snapshot) {
oldRec := rec
rec, err = c.collection.nextRecord(rec, 0, false)
if err != nil {
if atomic.LoadInt64(&c.current.Next[0]) != 0 {
Expand All @@ -200,13 +182,11 @@ func (c *Cursor) Seek(key string) {
c.current = nil
return
}
oldRec.lock.RUnlock()
continue
}
if rec.Key < key {
c.current = rec
}
oldRec := rec
rec, err = c.collection.nextRecord(rec, 0, false)
if err != nil {
if atomic.LoadInt64(&c.current.Next[0]) != 0 {
Expand All @@ -215,7 +195,6 @@ func (c *Cursor) Seek(key string) {
c.current = nil
return
}
oldRec.lock.RUnlock()
}
}

Expand Down
2 changes: 0 additions & 2 deletions lm2.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,6 @@ type record struct {
Offset int64
Key string
Value string

lock sync.RWMutex
}

func generateLevel() int {
Expand Down
9 changes: 0 additions & 9 deletions update.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,7 @@ func (c *Collection) findLastLessThanOrEqual(key string, startingOffset int64, l
}

for rec != nil {
rec.lock.RLock()
if (!equal && rec.Key == key) || rec.Key > key {
rec.lock.RUnlock()
break
}
offset = rec.Offset
Expand All @@ -98,7 +96,6 @@ func (c *Collection) findLastLessThanOrEqual(key string, startingOffset int64, l
if err != nil {
return 0, err
}
oldRec.lock.RUnlock()
}

return offset, nil
Expand Down Expand Up @@ -209,9 +206,7 @@ KEYS_LOOP:
rollbackErr = err
break KEYS_LOOP
}
readRec.lock.RLock()
*prevRec = *readRec
readRec.lock.RUnlock()
}
atomic.StoreInt64(&rec.Next[level], prevRec.Next[level])
atomic.StoreInt64(&prevRec.Next[level], newRecordOffset)
Expand Down Expand Up @@ -292,9 +287,7 @@ KEYS_LOOP:
rollbackErr = err
goto ROLLBACK
}
readRec.lock.RLock()
*rec = *readRec
readRec.lock.RUnlock()
}
if rec.Key != key {
continue
Expand All @@ -318,9 +311,7 @@ KEYS_LOOP:
rollbackErr = err
goto ROLLBACK
}
readRec.lock.RLock()
*rec = *readRec
readRec.lock.RUnlock()
}
atomic.StoreInt64(&rec.Deleted, currentOffset)
c.setDirty(rec.Offset, rec)
Expand Down

0 comments on commit 9b26c20

Please sign in to comment.