Skip to content

Commit

Permalink
Separate history tree from hyper tree
Browse files Browse the repository at this point in the history
Co-authored-by: Gabriel Díaz <[email protected]>
  • Loading branch information
aalda and gdiazlo committed Feb 25, 2019
1 parent 57ace16 commit 6445c72
Show file tree
Hide file tree
Showing 50 changed files with 2,741 additions and 1,677 deletions.
6 changes: 2 additions & 4 deletions balloon/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,15 @@
package cache

import (
"github.com/bbva/qed/balloon/navigator"
"github.com/bbva/qed/hashing"
"github.com/bbva/qed/storage"
)

type Cache interface {
Get(pos navigator.Position) (hashing.Digest, bool)
Get(key []byte) ([]byte, bool)
}

type ModifiableCache interface {
Put(pos navigator.Position, value hashing.Digest)
Put(key []byte, value []byte)
Fill(r storage.KVPairReader) error
Size() int
Cache
Expand Down
10 changes: 4 additions & 6 deletions balloon/cache/fast.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ package cache

import (
"github.com/VictoriaMetrics/fastcache"
"github.com/bbva/qed/balloon/navigator"
"github.com/bbva/qed/hashing"
"github.com/bbva/qed/storage"
)

Expand All @@ -32,16 +30,16 @@ func NewFastCache(maxBytes int64) *FastCache {
return &FastCache{cached: cache}
}

func (c FastCache) Get(pos navigator.Position) (hashing.Digest, bool) {
value := c.cached.Get(nil, pos.Bytes())
func (c FastCache) Get(key []byte) ([]byte, bool) {
value := c.cached.Get(nil, key)
if value == nil {
return nil, false
}
return value, true
}

func (c *FastCache) Put(pos navigator.Position, value hashing.Digest) {
c.cached.Set(pos.Bytes(), value)
func (c *FastCache) Put(key []byte, value []byte) {
c.cached.Set(key, value)
}

func (c *FastCache) Fill(r storage.KVPairReader) (err error) {
Expand Down
22 changes: 10 additions & 12 deletions balloon/cache/fast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,30 @@ package cache
import (
"testing"

"github.com/bbva/qed/balloon/navigator"
"github.com/bbva/qed/hashing"
"github.com/bbva/qed/util"
"github.com/stretchr/testify/require"
)

func TestFastCache(t *testing.T) {

testCases := []struct {
pos navigator.Position
value hashing.Digest
key []byte
value []byte
cached bool
}{
{&navigator.FakePosition{[]byte{0x0}, 0}, hashing.Digest{0x1}, true},
{&navigator.FakePosition{[]byte{0x1}, 0}, hashing.Digest{0x2}, true},
{&navigator.FakePosition{[]byte{0x2}, 0}, hashing.Digest{0x3}, false},
{[]byte{0x0, 0x0}, []byte{0x1}, true},
{[]byte{0x1, 0x0}, []byte{0x2}, true},
{[]byte{0x2, 0x0}, []byte{0x3}, false},
}

cache := NewFastCache(100 * 1024)

for i, c := range testCases {
if c.cached {
cache.Put(c.pos, c.value)
cache.Put(c.key, c.value)
}

cachedValue, ok := cache.Get(c.pos)
cachedValue, ok := cache.Get(c.key)

if c.cached {
require.Truef(t, ok, "The key should exists in cache in test case %d", i)
Expand All @@ -66,8 +64,8 @@ func TestFillFastCache(t *testing.T) {
require.Truef(t, reader.Remaining == 0, "All elements should be cached. Remaining: %d", reader.Remaining)

for i := uint64(0); i < numElems; i++ {
pos := &navigator.FakePosition{util.Uint64AsBytes(i), 0}
_, ok := cache.Get(pos)
require.Truef(t, ok, "The element in position %v should be in cache", pos)
key := util.Uint64AsBytes(i)
_, ok := cache.Get(key)
require.Truef(t, ok, "The element with key %v should be in cache", key)
}
}
10 changes: 4 additions & 6 deletions balloon/cache/free.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (
"bytes"
"runtime/debug"

"github.com/bbva/qed/balloon/navigator"
"github.com/bbva/qed/hashing"
"github.com/bbva/qed/storage"
"github.com/coocood/freecache"
)
Expand All @@ -36,16 +34,16 @@ func NewFreeCache(initialSize int) *FreeCache {
return &FreeCache{cached: cache}
}

func (c FreeCache) Get(pos navigator.Position) (hashing.Digest, bool) {
value, err := c.cached.Get(pos.Bytes())
func (c FreeCache) Get(key []byte) ([]byte, bool) {
value, err := c.cached.Get(key)
if err != nil {
return nil, false
}
return value, true
}

func (c *FreeCache) Put(pos navigator.Position, value hashing.Digest) {
c.cached.Set(pos.Bytes(), value, 0)
func (c *FreeCache) Put(key []byte, value []byte) {
c.cached.Set(key, value, 0)
}

func (c *FreeCache) Fill(r storage.KVPairReader) (err error) {
Expand Down
22 changes: 10 additions & 12 deletions balloon/cache/free_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,30 @@ package cache
import (
"testing"

"github.com/bbva/qed/balloon/navigator"
"github.com/bbva/qed/hashing"
"github.com/bbva/qed/util"
"github.com/stretchr/testify/require"
)

func TestFreeCache(t *testing.T) {

testCases := []struct {
pos navigator.Position
value hashing.Digest
key []byte
value []byte
cached bool
}{
{&navigator.FakePosition{[]byte{0x0}, 0}, hashing.Digest{0x1}, true},
{&navigator.FakePosition{[]byte{0x1}, 0}, hashing.Digest{0x2}, true},
{&navigator.FakePosition{[]byte{0x2}, 0}, hashing.Digest{0x3}, false},
{[]byte{0x0, 0x0}, []byte{0x1}, true},
{[]byte{0x1, 0x0}, []byte{0x2}, true},
{[]byte{0x2, 0x0}, []byte{0x3}, false},
}

cache := NewFreeCache(100 * 1024)

for i, c := range testCases {
if c.cached {
cache.Put(c.pos, c.value)
cache.Put(c.key, c.value)
}

cachedValue, ok := cache.Get(c.pos)
cachedValue, ok := cache.Get(c.key)

if c.cached {
require.Truef(t, ok, "The key should exists in cache in test case %d", i)
Expand All @@ -66,8 +64,8 @@ func TestFillFreeCache(t *testing.T) {
require.Truef(t, reader.Remaining == 0, "All elements should be cached. Remaining: %d", reader.Remaining)

for i := uint64(0); i < numElems; i++ {
pos := &navigator.FakePosition{util.Uint64AsBytes(i), 0}
_, ok := cache.Get(pos)
require.Truef(t, ok, "The element in position %v should be in cache", pos)
key := util.Uint64AsBytes(i)
_, ok := cache.Get(key)
require.Truef(t, ok, "The element with key %v should be in cache", key)
}
}
26 changes: 12 additions & 14 deletions balloon/cache/lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ package cache
import (
"container/list"

"github.com/bbva/qed/balloon/navigator"
"github.com/bbva/qed/hashing"
"github.com/bbva/qed/storage"
)

const lruKeySize = 10

type entry struct {
key [lruKeySize]byte
value hashing.Digest
value []byte
}

type LruReadThroughCache struct {
Expand All @@ -33,12 +31,12 @@ func NewLruReadThroughCache(prefix byte, store storage.Store, cacheSize uint16)
}
}

func (c LruReadThroughCache) Get(pos navigator.Position) (hashing.Digest, bool) {
var key [lruKeySize]byte
copy(key[:], pos.Bytes())
e, ok := c.items[key]
func (c LruReadThroughCache) Get(key []byte) ([]byte, bool) {
var k [lruKeySize]byte
copy(k[:], key)
e, ok := c.items[k]
if !ok {
pair, err := c.store.Get(c.prefix, pos.Bytes())
pair, err := c.store.Get(c.prefix, key)
if err != nil {
return nil, false
}
Expand All @@ -48,21 +46,21 @@ func (c LruReadThroughCache) Get(pos navigator.Position) (hashing.Digest, bool)
return e.Value.(*entry).value, ok
}

func (c *LruReadThroughCache) Put(pos navigator.Position, value hashing.Digest) {
var key [lruKeySize]byte
copy(key[:], pos.Bytes())
func (c *LruReadThroughCache) Put(key []byte, value []byte) {
var k [lruKeySize]byte
copy(k[:], key)
// check for existing item
if e, ok := c.items[key]; ok {
if e, ok := c.items[k]; ok {
// update value for specified key
c.evictList.MoveToFront(e)
e.Value.(*entry).value = value
return
}

// Add new item
e := &entry{key, value}
e := &entry{k, value}
entry := c.evictList.PushFront(e)
c.items[key] = entry
c.items[k] = entry

// Verify if eviction is needed
if c.evictList.Len() > c.size {
Expand Down
11 changes: 6 additions & 5 deletions balloon/cache/passthrough.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package cache

import (
"github.com/bbva/qed/balloon/navigator"
"github.com/bbva/qed/hashing"
"github.com/bbva/qed/storage"
)

Expand All @@ -28,11 +26,14 @@ type PassThroughCache struct {
}

func NewPassThroughCache(prefix byte, store storage.Store) *PassThroughCache {
return &PassThroughCache{prefix, store}
return &PassThroughCache{
prefix: prefix,
store: store,
}
}

func (c PassThroughCache) Get(pos navigator.Position) (hashing.Digest, bool) {
pair, err := c.store.Get(c.prefix, pos.Bytes())
func (c PassThroughCache) Get(key []byte) ([]byte, bool) {
pair, err := c.store.Get(c.prefix, key)
if err != nil {
return nil, false
}
Expand Down
16 changes: 7 additions & 9 deletions balloon/cache/passthrough_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ package cache
import (
"testing"

"github.com/bbva/qed/balloon/navigator"
"github.com/bbva/qed/hashing"
"github.com/stretchr/testify/require"

"github.com/bbva/qed/storage"
Expand All @@ -30,13 +28,13 @@ import (
func TestPassThroughCache(t *testing.T) {

testCases := []struct {
pos navigator.Position
value hashing.Digest
key []byte
value []byte
cached bool
}{
{&navigator.FakePosition{[]byte{0x0}, 0}, hashing.Digest{0x1}, true},
{&navigator.FakePosition{[]byte{0x1}, 0}, hashing.Digest{0x2}, true},
{&navigator.FakePosition{[]byte{0x2}, 0}, hashing.Digest{0x3}, false},
{[]byte{0x0, 0x0}, []byte{0x1}, true},
{[]byte{0x1, 0x0}, []byte{0x2}, true},
{[]byte{0x2, 0x0}, []byte{0x3}, false},
}

store, closeF := storage_utils.OpenBPlusTreeStore()
Expand All @@ -47,12 +45,12 @@ func TestPassThroughCache(t *testing.T) {
for i, c := range testCases {
if c.cached {
err := store.Mutate([]*storage.Mutation{
{prefix, c.pos.Bytes(), c.value},
{prefix, c.key, c.value},
})
require.NoError(t, err)
}

cachedValue, ok := cache.Get(c.pos)
cachedValue, ok := cache.Get(c.key)

if c.cached {
require.Truef(t, ok, "The key should exists in cache in test case %d", i)
Expand Down
24 changes: 11 additions & 13 deletions balloon/cache/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,30 @@ package cache
import (
"bytes"

"github.com/bbva/qed/balloon/navigator"
"github.com/bbva/qed/hashing"
"github.com/bbva/qed/storage"
)

const keySize = 34

type SimpleCache struct {
cached map[[keySize]byte]hashing.Digest
cached map[[keySize]byte][]byte
}

func NewSimpleCache(initialSize uint64) *SimpleCache {
return &SimpleCache{make(map[[keySize]byte]hashing.Digest, initialSize)}
return &SimpleCache{make(map[[keySize]byte][]byte, initialSize)}
}

func (c SimpleCache) Get(pos navigator.Position) (hashing.Digest, bool) {
var key [keySize]byte
copy(key[:], pos.Bytes())
digest, ok := c.cached[key]
return digest, ok
func (c SimpleCache) Get(key []byte) ([]byte, bool) {
var k [keySize]byte
copy(k[:], key)
value, ok := c.cached[k]
return value, ok
}

func (c *SimpleCache) Put(pos navigator.Position, value hashing.Digest) {
var key [keySize]byte
copy(key[:], pos.Bytes())
c.cached[key] = value
func (c *SimpleCache) Put(key []byte, value []byte) {
var k [keySize]byte
copy(k[:], key)
c.cached[k] = value
}

func (c *SimpleCache) Fill(r storage.KVPairReader) (err error) {
Expand Down
Loading

0 comments on commit 6445c72

Please sign in to comment.