Commit 44357788 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent bbe11527
......@@ -26,6 +26,7 @@ import (
"sync"
"unsafe"
// "github.com/kylelemons/godebug/pretty"
"lab.nexedi.com/kirr/neo/go/zodb"
)
......@@ -103,7 +104,12 @@ type revCacheEntry struct {
// NewCache creates new cache backed up by loader
// XXX +sizeMax
func NewCache(loader storLoader) *Cache {
return &Cache{loader: loader, entryMap: make(map[zodb.Oid]*oidCacheEntry)}
c := &Cache{
loader: loader,
entryMap: make(map[zodb.Oid]*oidCacheEntry),
}
c.lru.Init()
return c
}
// newRevEntry creates new revCacheEntry with .before and inserts it into .rcev @i.
......@@ -182,6 +188,7 @@ func (c *Cache) Load(xid zodb.Xid) (data []byte, serial zodb.Tid, err error) {
// rce is already in cache - use it
if !rceNew {
//panic(0)
<-rce.ready
c.gcMu.Lock()
rce.inLRU.MoveBefore(&c.lru)
......@@ -375,7 +382,14 @@ func (c *Cache) loadRCE(rce *revCacheEntry, oid zodb.Oid) {
// update lru & cache size
c.gcMu.Lock()
//xv1 := map[string]interface{}{"lru": &c.lru, "rce": &rce.inLRU}
//fmt.Printf("aaa:\n%s\n", pretty.Sprint(xv1))
rce.inLRU.MoveBefore(&c.lru)
//xv2 := map[string]interface{}{"lru": &c.lru, "rce": &rce.inLRU}
//fmt.Printf("\n--------\n%s\n\n\n", pretty.Sprint(xv2))
//panic(1)
c.size += δsize
if c.size > c.sizeMax {
// XXX -> run gc
......
......@@ -108,6 +108,8 @@ func xideq(oid zodb.Oid, tid zodb.Tid) zodb.Xid {
func TestCache(t *testing.T) {
// XXX hack; place=ok?
pretty.CompareConfig.PrintStringers = true
debug := pretty.DefaultConfig
debug.IncludeUnexported = true
// XXX <100 <90 <80
// q<110 -> a) 110 <= cache.before b) otherwise
......@@ -172,19 +174,39 @@ func TestCache(t *testing.T) {
t.Helper()
oce := c.entryMap[oid]
if !reflect.DeepEqual(oce.rcev, rcev) {
t.Fatalf("oce(%v):\n%s", oid, pretty.Compare(rcev, oce.rcev))
t.Fatalf("oce(%v):\n%s\n", oid, pretty.Compare(rcev, oce.rcev))
}
}
checkMRU := func(mruvOk ...*revCacheEntry) {
t.Helper()
var mruv []*revCacheEntry
for hp, h := &c.lru, c.lru.prev; h != &c.lru; hp, h = h, h.prev {
xv := []interface{}{&c.lru, h.rceFromInLRU()}
debug.Print(xv) // &c.lru, h.rceFromInLRU())
if h.next != hp {
t.Fatalf("LRU list .next/.prev broken for\nh:\n%s\n\nhp:\n%s\n",
debug.Sprint(h), debug.Sprint(hp))
}
mruv = append(mruv, h.rceFromInLRU())
}
if !reflect.DeepEqual(mruv, mruvOk) {
t.Fatalf("MRU:\n%s\n", pretty.Compare(mruv, mruvOk))
}
}
// ---- verify cache behaviour for must be loaded/merged entries ----
// (this excercises mostly loadRCE/tryMerge)
checkMRU()
// load <3 -> new rce entry
checkLoad(xidlt(1,3), nil, 0, &zodb.ErrXidMissing{xidlt(1,3)})
oce1 := c.entryMap[1]
ok1(len(oce1.rcev) == 1)
rce1_b3 := oce1.rcev[0]
checkRCE(rce1_b3, 3, 0, nil, &zodb.ErrXidMissing{xidlt(1,3)})
checkMRU(rce1_b3)
// load <4 -> <3 merged with <4
checkLoad(xidlt(1,4), nil, 0, &zodb.ErrXidMissing{xidlt(1,4)})
......@@ -192,6 +214,7 @@ func TestCache(t *testing.T) {
rce1_b4 := oce1.rcev[0]
ok1(rce1_b4 != rce1_b3) // rce1_b3 was merged into rce1_b4
checkRCE(rce1_b4, 4, 0, nil, &zodb.ErrXidMissing{xidlt(1,4)})
checkMRU(rce1_b4)
// load <2 -> <2 merged with <4
checkLoad(xidlt(1,2), nil, 0, &zodb.ErrXidMissing{xidlt(1,2)})
......
......@@ -49,7 +49,7 @@ func (a *listHead) MoveBefore(b *listHead) {
a.Delete()
a.next = b
b.prev = a
a.prev = b.prev
b.prev = a
a.prev.next = a
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment