-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
155 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
Copyright 2018 Banco Bilbao Vizcaya Argentaria, S.A. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package cache | ||
|
||
import ( | ||
"github.com/VictoriaMetrics/fastcache" | ||
"github.com/bbva/qed/balloon/navigator" | ||
"github.com/bbva/qed/hashing" | ||
"github.com/bbva/qed/storage" | ||
) | ||
|
||
type FastCache struct { | ||
cached *fastcache.Cache | ||
} | ||
|
||
func NewFastCache(maxBytes int64) *FastCache { | ||
cache := fastcache.New(int(maxBytes)) | ||
return &FastCache{cached: cache} | ||
} | ||
|
||
func (c FastCache) Get(pos navigator.Position) (hashing.Digest, bool) { | ||
value := c.cached.Get(nil, pos.Bytes()) | ||
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) Fill(r storage.KVPairReader) (err error) { | ||
defer r.Close() | ||
for { | ||
entries := make([]*storage.KVPair, 100) | ||
n, err := r.Read(entries) | ||
if err != nil || n == 0 { | ||
break | ||
} | ||
for _, entry := range entries { | ||
if entry != nil { | ||
c.cached.Set(entry.Key, entry.Value) | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (c FastCache) Size() int { | ||
var s fastcache.Stats | ||
c.cached.UpdateStats(&s) | ||
return int(s.EntriesCount) | ||
} | ||
|
||
func (c FastCache) Equal(o *FastCache) bool { | ||
// can only check size and entries count | ||
var stats, oStats fastcache.Stats | ||
c.cached.UpdateStats(&stats) | ||
o.cached.UpdateStats(&oStats) | ||
return stats.BytesSize == oStats.BytesSize && stats.EntriesCount == oStats.EntriesCount | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
Copyright 2018 Banco Bilbao Vizcaya Argentaria, S.A. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
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 | ||
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}, | ||
} | ||
|
||
cache := NewFastCache(100 * 1024) | ||
|
||
for i, c := range testCases { | ||
if c.cached { | ||
cache.Put(c.pos, c.value) | ||
} | ||
|
||
cachedValue, ok := cache.Get(c.pos) | ||
|
||
if c.cached { | ||
require.Truef(t, ok, "The key should exists in cache in test case %d", i) | ||
require.Equalf(t, c.value, cachedValue, "The cached value should be equal to stored value in test case %d", i) | ||
} else { | ||
require.Falsef(t, ok, "The key should not exist in cache in test case %d", i) | ||
} | ||
} | ||
} | ||
|
||
func TestFillFastCache(t *testing.T) { | ||
|
||
numElems := uint64(10000) | ||
cache := NewFastCache(10000 * 1024) | ||
reader := NewFakeKVPairReader(numElems) | ||
|
||
err := cache.Fill(reader) | ||
|
||
require.NoError(t, err) | ||
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters