Skip to content

Commit

Permalink
Convert into skip list (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
Preetam authored Jun 27, 2017
1 parent 110bbf2 commit beec538
Show file tree
Hide file tree
Showing 3 changed files with 312 additions and 370 deletions.
55 changes: 22 additions & 33 deletions cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (c *Collection) NewCursor() (*Cursor, error) {

c.metaLock.RLock()
defer c.metaLock.RUnlock()
if c.Head == 0 {
if c.Next[0] == 0 {
return &Cursor{
collection: c,
current: nil,
Expand All @@ -29,7 +29,7 @@ func (c *Collection) NewCursor() (*Cursor, error) {
}, nil
}

head, err := c.readRecord(c.Head)
head, err := c.readRecord(c.Next[0])
if err != nil {
return nil, err
}
Expand All @@ -44,7 +44,7 @@ func (c *Collection) NewCursor() (*Cursor, error) {
cur.current.lock.RLock()
for (cur.current.Deleted != 0 && cur.current.Deleted <= cur.snapshot) ||
(cur.current.Offset >= cur.snapshot) {
rec, err = cur.collection.readRecord(atomic.LoadInt64(&cur.current.Next))
rec, err = cur.collection.readRecord(atomic.LoadInt64(&cur.current.Next[0]))
if err != nil {
cur.current.lock.RUnlock()
cur.current = nil
Expand Down Expand Up @@ -85,10 +85,10 @@ func (c *Cursor) Next() bool {
}

c.current.lock.RLock()
rec, err := c.collection.readRecord(atomic.LoadInt64(&c.current.Next))
rec, err := c.collection.readRecord(atomic.LoadInt64(&c.current.Next[0]))
if err != nil {
c.current.lock.RUnlock()
if atomic.LoadInt64(&c.current.Next) != 0 {
if atomic.LoadInt64(&c.current.Next[0]) != 0 {
c.err = err
}
c.current = nil
Expand All @@ -100,10 +100,10 @@ func (c *Cursor) Next() bool {
c.current.lock.RLock()
for (c.current.Deleted != 0 && c.current.Deleted <= c.snapshot) ||
(c.current.Offset >= c.snapshot) {
rec, err = c.collection.readRecord(atomic.LoadInt64(&c.current.Next))
rec, err = c.collection.readRecord(atomic.LoadInt64(&c.current.Next[0]))
if err != nil {
c.current.lock.RUnlock()
if atomic.LoadInt64(&c.current.Next) != 0 {
if atomic.LoadInt64(&c.current.Next[0]) != 0 {
c.err = err
}
c.current = nil
Expand Down Expand Up @@ -145,38 +145,27 @@ func (c *Cursor) Seek(key string) {

var rec *record
var err error
offset := c.collection.cache.findLastLessThan(key)
if offset == 0 {
c.collection.metaLock.RLock()
rec, err = c.collection.readRecord(c.collection.Head)
c.collection.metaLock.RUnlock()
if err != nil {
if c.current != nil && atomic.LoadInt64(&c.current.Next) != 0 {
c.err = err
}
c.current = nil
return
}
} else {
rec, err = c.collection.readRecord(offset)
if err != nil {
if atomic.LoadInt64(&c.current.Next) != 0 {
c.err = err
}
c.current = nil
return
c.collection.metaLock.RLock()
rec, err = c.collection.readRecord(c.collection.Next[0])
c.collection.metaLock.RUnlock()
if err != nil {
if c.current != nil && atomic.LoadInt64(&c.current.Next[0]) != 0 {
c.err = err
}
c.current = nil
return
}

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)
rec, err = c.collection.nextRecord(rec, 0)
if err != nil {
if atomic.LoadInt64(&c.current.Next) != 0 {
if atomic.LoadInt64(&c.current.Next[0]) != 0 {
c.err = err
}
c.current = nil
Expand All @@ -191,9 +180,9 @@ func (c *Cursor) Seek(key string) {
}
if (rec.Deleted > 0 && rec.Deleted <= c.snapshot) || (rec.Offset >= c.snapshot) {
oldRec := rec
rec, err = c.collection.nextRecord(rec)
rec, err = c.collection.nextRecord(rec, 0)
if err != nil {
if atomic.LoadInt64(&c.current.Next) != 0 {
if atomic.LoadInt64(&c.current.Next[0]) != 0 {
c.err = err
}
c.current = nil
Expand All @@ -206,9 +195,9 @@ func (c *Cursor) Seek(key string) {
c.current = rec
}
oldRec := rec
rec, err = c.collection.nextRecord(rec)
rec, err = c.collection.nextRecord(rec, 0)
if err != nil {
if atomic.LoadInt64(&c.current.Next) != 0 {
if atomic.LoadInt64(&c.current.Next[0]) != 0 {
c.err = err
}
c.current = nil
Expand Down
Loading

0 comments on commit beec538

Please sign in to comment.