fix(simple8b): Fix incorrect encoding for a run of 119 or 239 1s

If 120th or 240th value is not a 1, k still passes the check in the
switch, causing the last value to be lost. If this value occurs at
the boundary of a block, the max time will be incorrect, resulting in
compaction failing to make forward progress.
pull/10616/head
Stuart Carnie 2018-11-13 20:31:04 -07:00 committed by Edd Robinson
parent b35533e7f7
commit bef0577206
2 changed files with 19 additions and 1 deletions

View File

@ -388,6 +388,7 @@ NEXTVALUE:
// try to pack run of 240 or 120 1s
if len(remaining) >= 120 {
// Invariant: len(a) is fixed to 120 or 240 values
var a []uint64
if len(remaining) >= 240 {
a = remaining[:240]
@ -395,24 +396,31 @@ NEXTVALUE:
a = remaining[:120]
}
// search for the longest sequence of 1s in a
// Postcondition: k equals the index of the last 1 or -1
k := 0
for k = range a {
if a[k] != 1 {
k--
break
}
}
v := uint64(0)
switch {
case k >= 239:
case k == 239:
// 240 1s
i += 240
case k >= 119:
// at least 120 1s
v = 1 << 60
i += 120
default:
goto CODES
}
dst[j] = v
j++
continue

View File

@ -123,6 +123,16 @@ func TestEncodeAll(t *testing.T) {
in[120] = 5
return in
}},
{name: "119 ones", fn: func() []uint64 {
in := ones(240)()
in[119] = 5
return in
}},
{name: "239 ones", fn: func() []uint64 {
in := ones(241)()
in[239] = 5
return in
}},
}
for _, test := range tests {