Commit ed7ffaa2 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 94e427fe
...@@ -68,7 +68,7 @@ type oidCacheEntry struct { ...@@ -68,7 +68,7 @@ type oidCacheEntry struct {
// XXX or? // XXX or?
// cached revisions in descending order // cached revisions in descending order
// .before > .serial >= next.before > next.serial ? // .before > .serial >= next.before > next.serial ?
revv []*revCacheEntry // XXX -> rcev ? rcev []*revCacheEntry
} }
// revCacheEntry is information about 1 cached oid revision // revCacheEntry is information about 1 cached oid revision
...@@ -104,8 +104,8 @@ func NewCache(loader storLoader) *Cache { ...@@ -104,8 +104,8 @@ func NewCache(loader storLoader) *Cache {
return &Cache{loader: loader, entryMap: make(map[zodb.Oid]*oidCacheEntry)} return &Cache{loader: loader, entryMap: make(map[zodb.Oid]*oidCacheEntry)}
} }
// newReveEntry creates new revCacheEntry with .before and inserts it into .revv @i // newReveEntry creates new revCacheEntry with .before and inserts it into .rcev @i
// (if i == len(oce.revv) - entry is appended) // (if i == len(oce.rcev) - entry is appended)
func (oce *oidCacheEntry) newRevEntry(i int, before zodb.Tid) *revCacheEntry { func (oce *oidCacheEntry) newRevEntry(i int, before zodb.Tid) *revCacheEntry {
rce := &revCacheEntry{ rce := &revCacheEntry{
parent: oce, parent: oce,
...@@ -115,17 +115,17 @@ func (oce *oidCacheEntry) newRevEntry(i int, before zodb.Tid) *revCacheEntry { ...@@ -115,17 +115,17 @@ func (oce *oidCacheEntry) newRevEntry(i int, before zodb.Tid) *revCacheEntry {
} }
rce.inLRU.Init() rce.inLRU.Init()
oce.revv = append(oce.revv, nil) oce.rcev = append(oce.rcev, nil)
copy(oce.revv[i+1:], oce.revv[i:]) copy(oce.rcev[i+1:], oce.rcev[i:])
oce.revv[i] = rce oce.rcev[i] = rce
return rce return rce
} }
// find finds rce under oce and returns its index in oce.revv. // find finds rce under oce and returns its index in oce.rcev.
// not found -> -1. // not found -> -1.
func (oce *oidCacheEntry) find(rce *revCacheEntry) int { func (oce *oidCacheEntry) find(rce *revCacheEntry) int {
for i, r := range oce.revv { for i, r := range oce.rcev {
if r == rce { if r == rce {
return i return i
} }
...@@ -134,12 +134,12 @@ func (oce *oidCacheEntry) find(rce *revCacheEntry) int { ...@@ -134,12 +134,12 @@ func (oce *oidCacheEntry) find(rce *revCacheEntry) int {
} }
func (oce *oidCacheEntry) deli(i int) { func (oce *oidCacheEntry) deli(i int) {
n := len(oce.revv) - 1 n := len(oce.rcev) - 1
copy(oce.revv[i:], oce.revv[i+1:]) copy(oce.rcev[i:], oce.rcev[i+1:])
// release ptr to revCacheEntry so it won't confusingly stay live when // release ptr to revCacheEntry so it won't confusingly stay live when
// its turn to be deleted come. // its turn to be deleted come.
oce.revv[n] = nil oce.rcev[n] = nil
oce.revv = oce.revv[:n] oce.rcev = oce.rcev[:n]
} }
// XXX doc; must be called with oce lock held // XXX doc; must be called with oce lock held
...@@ -200,9 +200,9 @@ func (c *Cache) Load(xid zodb.Xid) (data []byte, tid zodb.Tid, err error) { ...@@ -200,9 +200,9 @@ func (c *Cache) Load(xid zodb.Xid) (data []byte, tid zodb.Tid, err error) {
var rceNew bool // whether we created rce anew var rceNew bool // whether we created rce anew
if xid.TidBefore { if xid.TidBefore {
l := len(oce.revv) l := len(oce.rcev)
i := sort.Search(l, func(i int) bool { i := sort.Search(l, func(i int) bool {
before := oce.revv[i].before before := oce.rcev[i].before
if before == zodb.TidMax { if before == zodb.TidMax {
before = cacheBefore before = cacheBefore
} }
...@@ -210,7 +210,7 @@ func (c *Cache) Load(xid zodb.Xid) (data []byte, tid zodb.Tid, err error) { ...@@ -210,7 +210,7 @@ func (c *Cache) Load(xid zodb.Xid) (data []byte, tid zodb.Tid, err error) {
}) })
switch { switch {
// not found - tid > max(revv.before) - insert new max entry // not found - tid > max(rcev.before) - insert new max entry
case i == l: case i == l:
rce = oce.newRevEntry(i, xid.Tid) rce = oce.newRevEntry(i, xid.Tid)
if rce.before == cacheBefore { if rce.before == cacheBefore {
...@@ -221,17 +221,17 @@ func (c *Cache) Load(xid zodb.Xid) (data []byte, tid zodb.Tid, err error) { ...@@ -221,17 +221,17 @@ func (c *Cache) Load(xid zodb.Xid) (data []byte, tid zodb.Tid, err error) {
rceNew = true rceNew = true
// found: // found:
// tid <= revv[i].before // tid <= rcev[i].before
// tid > revv[i-1].before // tid > rcev[i-1].before
// exact match - we already have entry for this before // exact match - we already have entry for this before
case xid.Tid == oce.revv[i].before: case xid.Tid == oce.rcev[i].before:
rce = oce.revv[i] rce = oce.rcev[i]
// non-exact match - same entry if inside (serial, before] // non-exact match - same entry if inside (serial, before]
// XXX do we need `oce.revv[i].serial != 0` check vvv ? // XXX do we need `oce.rcev[i].serial != 0` check vvv ?
case oce.revv[i].loaded() && oce.revv[i].serial != 0 && oce.revv[i].serial < xid.Tid: case oce.rcev[i].loaded() && oce.rcev[i].serial != 0 && oce.rcev[i].serial < xid.Tid:
rce = oce.revv[i] rce = oce.rcev[i]
// otherwise - insert new entry // otherwise - insert new entry
default: default:
...@@ -288,8 +288,8 @@ func (c *Cache) Load(xid zodb.Xid) (data []byte, tid zodb.Tid, err error) { ...@@ -288,8 +288,8 @@ func (c *Cache) Load(xid zodb.Xid) (data []byte, tid zodb.Tid, err error) {
} }
// if rce & rceNext cover the same range -> drop rce // if rce & rceNext cover the same range -> drop rce
if i + 1 < len(oce.revv) { if i + 1 < len(oce.rcev) {
rceNext := oce.revv[i+1] rceNext := oce.rcev[i+1]
if rceNext.loaded() && tryMerge(rce, rceNext, rce, xid.Oid) { if rceNext.loaded() && tryMerge(rce, rceNext, rce, xid.Oid) {
// not δsize -= len(rce.data) // not δsize -= len(rce.data)
// tryMerge can change rce.data if consistency is broken // tryMerge can change rce.data if consistency is broken
...@@ -300,7 +300,7 @@ func (c *Cache) Load(xid zodb.Xid) (data []byte, tid zodb.Tid, err error) { ...@@ -300,7 +300,7 @@ func (c *Cache) Load(xid zodb.Xid) (data []byte, tid zodb.Tid, err error) {
// if rcePrev & rce cover the same range -> drop rcePrev // if rcePrev & rce cover the same range -> drop rcePrev
if i > 0 { if i > 0 {
rcePrev := oce.revv[i-1] rcePrev := oce.rcev[i-1]
if rcePrev.loaded() && tryMerge(rcePrev, rce, rce, xid.Oid) { if rcePrev.loaded() && tryMerge(rcePrev, rce, rce, xid.Oid) {
δsize -= len(rcePrev.data) δsize -= len(rcePrev.data)
} }
...@@ -323,7 +323,7 @@ func (c *Cache) Load(xid zodb.Xid) (data []byte, tid zodb.Tid, err error) { ...@@ -323,7 +323,7 @@ func (c *Cache) Load(xid zodb.Xid) (data []byte, tid zodb.Tid, err error) {
// tryMerge tries to merge rce prev into next // tryMerge tries to merge rce prev into next
// //
// both prev and next must be already loaded. // both prev and next must be already loaded.
// prev and next must come adjacent to each other in parent.revv with // prev and next must come adjacent to each other in parent.rcev with
// prev.before < next.before . // prev.before < next.before .
// //
// cur must be one of either prev or next and indicates which rce is current // cur must be one of either prev or next and indicates which rce is current
......
...@@ -115,9 +115,7 @@ func TestCache(t *testing.T) { ...@@ -115,9 +115,7 @@ func TestCache(t *testing.T) {
// XXX cases when .serial=0 (not yet determined - 1st loadBefore is in progress) // XXX cases when .serial=0 (not yet determined - 1st loadBefore is in progress)
// XXX for every serial check before = (s-1, s, s+1) // XXX for every serial check before = (s-1, s, s+1)
// merge: rce + rceNext // merge: rcePrev + (rce + rceNext) ?
// rcePrev + rce
// rcePrev + (rce + rceNext)
tc := Checker{t} tc := Checker{t}
ok1 := func(v bool) { t.Helper(); tc.ok1(v) } ok1 := func(v bool) { t.Helper(); tc.ok1(v) }
...@@ -171,75 +169,75 @@ func TestCache(t *testing.T) { ...@@ -171,75 +169,75 @@ func TestCache(t *testing.T) {
checkOCE := func(oid zodb.Oid, rcev ...*revCacheEntry) { checkOCE := func(oid zodb.Oid, rcev ...*revCacheEntry) {
t.Helper() t.Helper()
oce := c.entryMap[oid] oce := c.entryMap[oid]
if !reflect.DeepEqual(oce.revv, rcev) { if !reflect.DeepEqual(oce.rcev, rcev) {
t.Fatalf("oce(%v):\n%s", oid, pretty.Compare(rcev, oce.revv)) t.Fatalf("oce(%v):\n%s", oid, pretty.Compare(rcev, oce.rcev))
} }
} }
// load <3 -> new rce entry // load <3 -> new rce entry
checkLoad(xidlt(1,3), nil, 0, &zodb.ErrXidMissing{xidlt(1,3)}) checkLoad(xidlt(1,3), nil, 0, &zodb.ErrXidMissing{xidlt(1,3)})
oce1 := c.entryMap[1] oce1 := c.entryMap[1]
ok1(len(oce1.revv) == 1) ok1(len(oce1.rcev) == 1)
rce1_b3 := oce1.revv[0] rce1_b3 := oce1.rcev[0]
checkRCE(rce1_b3, 3, 0, nil, &zodb.ErrXidMissing{xidlt(1,3)}) checkRCE(rce1_b3, 3, 0, nil, &zodb.ErrXidMissing{xidlt(1,3)})
// load <4 -> <3 merged with <4 // load <4 -> <3 merged with <4
checkLoad(xidlt(1,4), nil, 0, &zodb.ErrXidMissing{xidlt(1,4)}) checkLoad(xidlt(1,4), nil, 0, &zodb.ErrXidMissing{xidlt(1,4)})
ok1(len(oce1.revv) == 1) ok1(len(oce1.rcev) == 1)
rce1_b4 := oce1.revv[0] rce1_b4 := oce1.rcev[0]
ok1(rce1_b4 != rce1_b3) // rce1_b3 was merged into rce1_b4 ok1(rce1_b4 != rce1_b3) // rce1_b3 was merged into rce1_b4
checkRCE(rce1_b4, 4, 0, nil, &zodb.ErrXidMissing{xidlt(1,4)}) checkRCE(rce1_b4, 4, 0, nil, &zodb.ErrXidMissing{xidlt(1,4)})
// load <2 -> <2 merged with <4 // load <2 -> <2 merged with <4
checkLoad(xidlt(1,2), nil, 0, &zodb.ErrXidMissing{xidlt(1,2)}) checkLoad(xidlt(1,2), nil, 0, &zodb.ErrXidMissing{xidlt(1,2)})
ok1(len(oce1.revv) == 1) ok1(len(oce1.rcev) == 1)
ok1(oce1.revv[0] == rce1_b4) ok1(oce1.rcev[0] == rce1_b4)
checkRCE(rce1_b4, 4, 0, nil, &zodb.ErrXidMissing{xidlt(1,4)}) checkRCE(rce1_b4, 4, 0, nil, &zodb.ErrXidMissing{xidlt(1,4)})
// load <6 -> new rce entry with data // load <6 -> new rce entry with data
checkLoad(xidlt(1,6), hello, 4, nil) checkLoad(xidlt(1,6), hello, 4, nil)
ok1(len(oce1.revv) == 2) ok1(len(oce1.rcev) == 2)
rce1_b6 := oce1.revv[1] rce1_b6 := oce1.rcev[1]
checkRCE(rce1_b6, 6, 4, hello, nil) checkRCE(rce1_b6, 6, 4, hello, nil)
checkOCE(1, rce1_b4, rce1_b6) checkOCE(1, rce1_b4, rce1_b6)
// load <5 -> merged with <6 // load <5 -> <5 merged with <6
checkLoad(xidlt(1,5), hello, 4, nil) checkLoad(xidlt(1,5), hello, 4, nil)
checkOCE(1, rce1_b4, rce1_b6) checkOCE(1, rce1_b4, rce1_b6)
// load <7 -> <6 merged -> <7 // load <7 -> <6 merged with <7
checkLoad(xidlt(1,7), hello, 4, nil) checkLoad(xidlt(1,7), hello, 4, nil)
ok1(len(oce1.revv) == 2) ok1(len(oce1.rcev) == 2)
rce1_b7 := oce1.revv[1] rce1_b7 := oce1.rcev[1]
ok1(rce1_b7 != rce1_b6) ok1(rce1_b7 != rce1_b6)
checkRCE(rce1_b7, 7, 4, hello, nil) checkRCE(rce1_b7, 7, 4, hello, nil)
checkOCE(1, rce1_b4, rce1_b7) checkOCE(1, rce1_b4, rce1_b7)
// load <8 -> ioerr + new rce // load <8 -> ioerr + new rce
checkLoad(xidlt(1,8), nil, 0, ioerr) checkLoad(xidlt(1,8), nil, 0, ioerr)
ok1(len(oce1.revv) == 3) ok1(len(oce1.rcev) == 3)
rce1_b8 := oce1.revv[2] rce1_b8 := oce1.rcev[2]
checkRCE(rce1_b8, 8, 0, nil, ioerr) checkRCE(rce1_b8, 8, 0, nil, ioerr)
checkOCE(1, rce1_b4, rce1_b7, rce1_b8) checkOCE(1, rce1_b4, rce1_b7, rce1_b8)
// load <9 -> ioerr + new rce (IO errors are not merged) // load <9 -> ioerr + new rce (IO errors are not merged)
checkLoad(xidlt(1,9), nil, 0, ioerr) checkLoad(xidlt(1,9), nil, 0, ioerr)
ok1(len(oce1.revv) == 4) ok1(len(oce1.rcev) == 4)
rce1_b9 := oce1.revv[3] rce1_b9 := oce1.rcev[3]
checkRCE(rce1_b9, 9, 0, nil, ioerr) checkRCE(rce1_b9, 9, 0, nil, ioerr)
checkOCE(1, rce1_b4, rce1_b7, rce1_b8, rce1_b9) checkOCE(1, rce1_b4, rce1_b7, rce1_b8, rce1_b9)
// load <10 -> new data rce, not merged with ioerr @<9 // load <10 -> new data rce, not merged with ioerr @<9
checkLoad(xidlt(1,10), world, 9, nil) checkLoad(xidlt(1,10), world, 9, nil)
ok1(len(oce1.revv) == 5) ok1(len(oce1.rcev) == 5)
rce1_b10 := oce1.revv[4] rce1_b10 := oce1.rcev[4]
checkRCE(rce1_b10, 10, 9, world, nil) checkRCE(rce1_b10, 10, 9, world, nil)
checkOCE(1, rce1_b4, rce1_b7, rce1_b8, rce1_b9, rce1_b10) checkOCE(1, rce1_b4, rce1_b7, rce1_b8, rce1_b9, rce1_b10)
// load <12 -> <10 merged -> <12 // load <12 -> <10 merged with <12
checkLoad(xidlt(1,12), world, 9, nil) checkLoad(xidlt(1,12), world, 9, nil)
ok1(len(oce1.revv) == 5) ok1(len(oce1.rcev) == 5)
rce1_b12 := oce1.revv[4] rce1_b12 := oce1.rcev[4]
ok1(rce1_b12 != rce1_b10) ok1(rce1_b12 != rce1_b10)
checkRCE(rce1_b12, 12, 9, world, nil) checkRCE(rce1_b12, 12, 9, world, nil)
checkOCE(1, rce1_b4, rce1_b7, rce1_b8, rce1_b9, rce1_b12) checkOCE(1, rce1_b4, rce1_b7, rce1_b8, rce1_b9, rce1_b12)
......
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