syndtr / goleveldb

LevelDB key/value database in Go.
BSD 2-Clause "Simplified" License
6.15k stars 969 forks source link

memdb will be inserted in order ? #348

Open pqnguyen opened 3 years ago

pqnguyen commented 3 years ago

Hi all. Anyone understand about memdb. It seems like a new inserted element will be placed in order. ` func (p *DB) Put(key []byte, value []byte) error { p.mu.Lock() defer p.mu.Unlock()

if node, exact := p.findGE(key, true); exact {
    kvOffset := len(p.kvData)
    p.kvData = append(p.kvData, key...)
    p.kvData = append(p.kvData, value...)
    p.nodeData[node] = kvOffset
    m := p.nodeData[node+nVal]
    p.nodeData[node+nVal] = len(value)
    p.kvSize += len(value) - m
    return nil
}

h := p.randHeight()
if h > p.maxHeight {
    for i := p.maxHeight; i < h; i++ {
        p.prevNode[i] = 0
    }
    p.maxHeight = h
}

kvOffset := len(p.kvData)
p.kvData = append(p.kvData, key...)
p.kvData = append(p.kvData, value...)
// Node
node := len(p.nodeData)
p.nodeData = append(p.nodeData, kvOffset, len(key), len(value), h)
for i, n := range p.prevNode[:h] {
    m := n + nNext + i
    p.nodeData = append(p.nodeData, p.nodeData[m])
    p.nodeData[m] = node
}

p.kvSize += len(key) + len(value)
p.n++
return nil

} ` If it does. Which is its data structure/algorithm? any docs about this one. Thanks.

ktereyp commented 3 years ago

It is skiplist.