Commit 9baafc82 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 6f377311
......@@ -200,9 +200,16 @@ func (db *DB) watcher(watchq <-chan CommitEvent) { // XXX err ?
}
// forget older δtail entries
fmt.Println()
fmt.Printf("%s\n", db.δtail.Head().Time().Time)
fmt.Printf("%s\n", db.δtail.Head().Time().Add(-db.tδkeep))
tcut := db.δtail.Head().Time().Add(-db.tδkeep)
δcut := TidFromTime(tcut)
db.δtail.ForgetBefore(δcut)
fmt.Printf("db: watcher: δtail: = (%s, %s]\n", db.δtail.Tail(), db.δtail.Head())
fmt.Printf("db: watcher: forget <= %s\n", δcut)
db.δtail.ForgetPast(δcut)
fmt.Printf("db: watcher: δtail: -> (%s, %s]\n", db.δtail.Tail(), db.δtail.Head())
db.mu.Unlock()
......
......@@ -58,7 +58,8 @@ import (
// #blk - file block number, when ΔTail represents changes to a file.
type ΔTail struct {
head Tid
tailv []δRevEntry
tail Tid
tailv []δRevEntry // XXX -> revv ?
lastRevOf map[Oid]Tid // index for LastRevOf queries
// XXX -> lastRevOf = {} oid -> []rev↑ if linear scan in LastRevOf starts to eat cpu
......@@ -78,6 +79,7 @@ type δRevEntry struct {
func NewΔTail(at0 Tid) *ΔTail {
return &ΔTail{
head: at0,
tail: at0,
lastRevOf: make(map[Oid]Tid),
}
}
......@@ -98,10 +100,7 @@ func (δtail *ΔTail) Head() Tid {
//
// Tail is ↑= on Forget, even if δtail becomes empty.
func (δtail *ΔTail) Tail() Tid {
if len(δtail.tailv) == 0 {
return δtail.head
}
return δtail.tailv[0].rev-1
return δtail.tail
}
// SliceByRev returns δtail slice of elements with .rev ∈ (low, high].
......@@ -161,12 +160,17 @@ func (δtail *ΔTail) Append(rev Tid, changev []Oid) {
}
}
// ForgetBefore discards all δtail entries with rev < revCut.
func (δtail *ΔTail) ForgetBefore(revCut Tid) {
// ForgetPast discards all δtail entries with rev ≤ revCut.
func (δtail *ΔTail) ForgetPast(revCut Tid) {
// revCut ≤ tail: nothing to do; don't let .tail go ↓
if revCut <= δtail.tail {
return
}
icut := 0
for i, δ := range δtail.tailv {
rev := δ.rev
if rev >= revCut {
if rev > revCut {
break
}
icut = i+1
......@@ -186,6 +190,8 @@ func (δtail *ΔTail) ForgetBefore(revCut Tid) {
tailv := make([]δRevEntry, l)
copy(tailv, δtail.tailv[icut:])
δtail.tailv = tailv
δtail.tail = revCut
}
// LastRevOf tries to return what was the last revision that changed id as of at database state.
......
......@@ -26,7 +26,7 @@ import (
)
func TestΔTail(t *testing.T) {
δtail := NewΔTail(1)
var δtail *ΔTail
// R is syntactic sugar to create 1 δRevEntry
R := func(rev Tid, changev ...Oid) δRevEntry {
......@@ -39,7 +39,7 @@ func TestΔTail(t *testing.T) {
}
// δCheck verifies that δtail state corresponds to provided tailv
δCheck := func(head Tid, tailv ...δRevEntry) {
δCheck := func(tail, head Tid, tailv ...δRevEntry) {
t.Helper()
for i := 1; i < len(tailv); i++ {
......@@ -48,19 +48,11 @@ func TestΔTail(t *testing.T) {
}
}
// Len/Head/Tail
if l := δtail.Len(); l != len(tailv) {
t.Fatalf("Len() -> %d ; want %d", l, len(tailv))
}
// Head/Tail/Data
if h := δtail.Head(); h != head {
t.Fatalf("Head() -> %s ; want %s", h, head)
}
tail := head
if len(tailv) > 0 {
tail = tailv[0].rev-1
}
if tt := δtail.Tail(); tt != tail {
t.Fatalf("Tail() -> %s ; want %s", tt, tail)
}
......@@ -69,6 +61,11 @@ func TestΔTail(t *testing.T) {
t.Fatalf("tailv:\nhave: %v\nwant: %v", δtail.tailv, tailv)
}
if l := δtail.Len(); l != len(tailv) {
t.Fatalf("Len() -> %d ; want %d", l, len(tailv))
}
// SliceByRev
// check that δtail.SliceByRev(rlo, rhi) == tailv[ilo:ihi).
......@@ -170,39 +167,43 @@ func TestΔTail(t *testing.T) {
}
}
δtail = NewΔTail(3)
δCheck(1)
δCheck(3,3)
δCheckLastUP(4, 12, 12) // δtail = ø
δAppend(R(10, 3,5))
δCheck(10, R(10, 3,5))
δCheck(3,10, R(10, 3,5))
δCheckLastUP(3, 9, 9) // at < δtail
δCheckLastUP(3, 2, 2) // at < δtail
δCheckLastUP(3, 12, 12) // at > δtail
δCheckLastUP(4, 10, 10) // id ∉ δtail
δAppend(R(11, 7))
δCheck(11, R(10, 3,5), R(11, 7))
δCheck(3,11, R(10, 3,5), R(11, 7))
δAppend(R(12, 7))
δCheck(12, R(10, 3,5), R(11, 7), R(12, 7))
δCheck(3,12, R(10, 3,5), R(11, 7), R(12, 7))
δAppend(R(14, 3,8))
δCheck(14, R(10, 3,5), R(11, 7), R(12, 7), R(14, 3,8))
δCheck(3,14, R(10, 3,5), R(11, 7), R(12, 7), R(14, 3,8))
δCheckLastUP(8, 12, 10) // id ∈ δtail, but has no entry with rev ≤ at
δtail.ForgetBefore(10)
δCheck(14, R(10, 3,5), R(11, 7), R(12, 7), R(14, 3,8))
δtail.ForgetPast(9)
δCheck(9,14, R(10, 3,5), R(11, 7), R(12, 7), R(14, 3,8))
δtail.ForgetPast(10)
δCheck(10,14, R(11, 7), R(12, 7), R(14, 3,8))
δtail.ForgetBefore(11)
δCheck(14, R(11, 7), R(12, 7), R(14, 3,8))
δtail.ForgetPast(12)
δCheck(12,14, R(14, 3,8))
δtail.ForgetBefore(13)
δCheck(14, R(14, 3,8))
δtail.ForgetPast(14)
δCheck(14,14)
δtail.ForgetBefore(15)
δCheck(14)
δtail.ForgetPast(12)
δCheck(14,14) // .tail should not go ↓
// Append panics on non-↑ rev
......@@ -229,14 +230,14 @@ func TestΔTail(t *testing.T) {
// on !empty δtail
δAppend(R(15, 1))
δCheck(15, R(15, 1))
δCheck(14,15, R(15, 1))
δAppendPanic(15)
δAppendPanic(14)
// .tailv underlying storage is not kept after forget
δtail.ForgetBefore(16)
δCheck(15)
δtail.ForgetPast(15)
δCheck(15,15)
const N = 1E3
for rev, i := Tid(16), 0; i < N; i, rev = i+1, rev+1 {
......@@ -244,7 +245,7 @@ func TestΔTail(t *testing.T) {
}
capN := cap(δtail.tailv)
δtail.ForgetBefore(N)
δtail.ForgetPast(N)
if c := cap(δtail.tailv); !(c < capN/10) {
t.Fatalf("forget: tailv storage did not shrink: cap%v: %d -> cap: %d", N, capN, c)
}
......
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