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