Skip to content

Commit

Permalink
Doc: Add godoc to cache and metrics packages.
Browse files Browse the repository at this point in the history
Refers to #40
  • Loading branch information
Jose Luis Lucas committed Jun 17, 2019
1 parent f90cb31 commit db255a3
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 4 deletions.
2 changes: 2 additions & 0 deletions balloon/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
limitations under the License.
*/

// Package cache implements the interface to interact with a cache.
// There are 4 implemented caches: freecache, lru, passthrough, and simple cache.
package cache

import (
Expand Down
13 changes: 11 additions & 2 deletions balloon/cache/free.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@ type FreeCache struct {
cached *freecache.Cache
}

// NewFreeCache funtion returns a new cache with a parametrized size.
func NewFreeCache(initialSize int) *FreeCache {
cache := freecache.NewCache(initialSize)
debug.SetGCPercent(20)
return &FreeCache{cached: cache}
}

// Get function returns the value of a given key in cache, and a boolean showing if
// the key is or is not present.
func (c FreeCache) Get(key []byte) ([]byte, bool) {
value, err := c.cached.Get(key)
if err != nil {
Expand All @@ -42,10 +45,12 @@ func (c FreeCache) Get(key []byte) ([]byte, bool) {
return value, true
}

// Put function adds a new key/value pair to the cache.
func (c *FreeCache) Put(key []byte, value []byte) {
c.cached.Set(key, value, 0)
_ = c.cached.Set(key, value, 0)
}

// Fill function inserts a bulk of key/value elements into the cache.
func (c *FreeCache) Fill(r storage.KVPairReader) (err error) {
defer r.Close()
for {
Expand All @@ -56,17 +61,21 @@ func (c *FreeCache) Fill(r storage.KVPairReader) (err error) {
}
for _, entry := range entries {
if entry != nil {
c.cached.Set(entry.Key, entry.Value, 0)
_ = c.cached.Set(entry.Key, entry.Value, 0)
}
}
}
return nil
}

// Size function returns the number of items currently in the cache.
func (c FreeCache) Size() int {
return int(c.cached.EntryCount())
}

// Equal function checks if every element from current cache (C) exists
// in the cache to compare (O). It does not check that every element from (O)
// exists in current cache (C).
func (c FreeCache) Equal(o *FreeCache) bool {
it := c.cached.NewIterator()
entry := it.Next()
Expand Down
10 changes: 10 additions & 0 deletions balloon/cache/lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ type entry struct {
value []byte
}

// LruReadThroughCache implemets a "Last Recent Used" cache with a storage backend.
// Therefore, "writes" are done in-memory, but "reads" are done checking the storage.
// It uses an "evictList" for the LRU functionality.
type LruReadThroughCache struct {
table storage.Table
store storage.Store
Expand All @@ -21,6 +24,7 @@ type LruReadThroughCache struct {
evictList *list.List
}

// NewLruReadThroughCache returns a new cacheSize cache with an asociated storage.
func NewLruReadThroughCache(table storage.Table, store storage.Store, cacheSize uint16) *LruReadThroughCache {
return &LruReadThroughCache{
table: table,
Expand All @@ -31,6 +35,8 @@ func NewLruReadThroughCache(table storage.Table, store storage.Store, cacheSize
}
}

// Get function returns the value of a given key in cache. If it is not present, it looks
// on the storage. Finally it returns a boolean showing if the key is or is not present.
func (c LruReadThroughCache) Get(key []byte) ([]byte, bool) {
var k [lruKeySize]byte
copy(k[:], key)
Expand All @@ -46,6 +52,8 @@ func (c LruReadThroughCache) Get(key []byte) ([]byte, bool) {
return e.Value.(*entry).value, ok
}

// Put function adds a new key/value element to the in-memory cache, or updates it if it
// exists. It also updates the LRU eviction list.
func (c *LruReadThroughCache) Put(key []byte, value []byte) {
var k [lruKeySize]byte
copy(k[:], key)
Expand All @@ -68,6 +76,7 @@ func (c *LruReadThroughCache) Put(key []byte, value []byte) {
}
}

// Fill function inserts a bulk of key/value elements into the cache.
func (c *LruReadThroughCache) Fill(r storage.KVPairReader) (err error) {
defer r.Close()
for {
Expand Down Expand Up @@ -103,6 +112,7 @@ func (c *LruReadThroughCache) Fill(r storage.KVPairReader) (err error) {
return nil
}

// Size function returns the number of items currently in the cache.
func (c *LruReadThroughCache) Size() int {
return c.evictList.Len()
}
Expand Down
4 changes: 4 additions & 0 deletions balloon/cache/passthrough.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,22 @@ import (
"github.com/bbva/qed/storage"
)

// PassThroughCache is not a cache itself. It stores data directly on disk.
type PassThroughCache struct {
table storage.Table
store storage.Store
}

// NewPassThroughCache initializes a cache with the given underlaying storage.
func NewPassThroughCache(table storage.Table, store storage.Store) *PassThroughCache {
return &PassThroughCache{
table: table,
store: store,
}
}

// Get function returns the value of a given key by looking for it on storage.
// It also returns a boolean showing if the key is or is not present.
func (c PassThroughCache) Get(key []byte) ([]byte, bool) {
pair, err := c.store.Get(c.table, key)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion balloon/cache/passthrough_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestPassThroughCache(t *testing.T) {
for i, c := range testCases {
if c.cached {
err := store.Mutate([]*storage.Mutation{
{table, c.key, c.value},
{Table: table, Key: c.key, Value: c.value},
})
require.NoError(t, err)
}
Expand Down
10 changes: 10 additions & 0 deletions balloon/cache/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,33 @@ import (

const keySize = 34

// SimpleCache is a fixed size in-memory map of byte array as key and values.
type SimpleCache struct {
cached map[[keySize]byte][]byte
}

// NewSimpleCache returns an empty SimpleCache of 'initialSize' size.
func NewSimpleCache(initialSize uint64) *SimpleCache {
return &SimpleCache{make(map[[keySize]byte][]byte, initialSize)}
}

// Get function returns the value of a given key in cache, and a boolean showing if
// the key is or is not present.
func (c SimpleCache) Get(key []byte) ([]byte, bool) {
var k [keySize]byte
copy(k[:], key)
value, ok := c.cached[k]
return value, ok
}

// Put function adds a key/value element to the SimpleCache.
func (c *SimpleCache) Put(key []byte, value []byte) {
var k [keySize]byte
copy(k[:], key)
c.cached[k] = value
}

// Fill function inserts a bulk of key/value elements into the cache.
func (c *SimpleCache) Fill(r storage.KVPairReader) (err error) {
defer r.Close()
for {
Expand All @@ -64,10 +70,14 @@ func (c *SimpleCache) Fill(r storage.KVPairReader) (err error) {
return nil
}

// Size function returns the number of items currently in the cache.
func (c SimpleCache) Size() int {
return len(c.cached)
}

// Equal function checks if every element from current cache (C) exists
// in the cache to compare (O). It does not check that every element from (O)
// exists in current cache (C).
func (c SimpleCache) Equal(o *SimpleCache) bool {
for k, v1 := range c.cached {
v2, ok := o.cached[k]
Expand Down
5 changes: 4 additions & 1 deletion balloon/cache/test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ func NewFakeKVPairReader(numElems uint64) *FakeKVPairReader {

func (r *FakeKVPairReader) Read(buffer []*storage.KVPair) (n int, err error) {
for n = 0; r.Remaining > 0 && n < len(buffer); n++ {
buffer[n] = &storage.KVPair{util.Uint64AsBytes(r.index), rand.Bytes(8)}
buffer[n] = &storage.KVPair{
Key: util.Uint64AsBytes(r.index),
Value: rand.Bytes(8),
}
r.Remaining--
r.index++
}
Expand Down
2 changes: 2 additions & 0 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
limitations under the License.
*/

// Package metrics implements an HTTP metrics server along with its
// life cycle: start, shutdown, and register metrics.
package metrics

import (
Expand Down

0 comments on commit db255a3

Please sign in to comment.