Last active
September 21, 2015 02:51
-
-
Save slrz/a80bdbc7af5c0753091f to your computer and use it in GitHub Desktop.
Go program demonstrating the issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"bytes" | |
"crypto/sha1" | |
"fmt" | |
"io" | |
"log" | |
"os" | |
"sort" | |
"github.com/cznic/kv" | |
) | |
type key [sha1.Size]byte | |
func main() { | |
if len(os.Args) < 2 { | |
log.Fatal("usage: repro dbfile") | |
} | |
keys := mustLoadFortunes() | |
db, err := kv.Create(os.Args[1], new(kv.Options)) | |
if err != nil { | |
log.Fatal(err) | |
} | |
const ( | |
// n = 795 // ok | |
n = 796 // fails | |
// n = 797 // ok | |
) | |
expected := 0 | |
for i, k := range keys { | |
if i == n { | |
break | |
} | |
if k[0] >= 0x10 { | |
expected++ | |
} | |
if err := db.Set(k[:], []byte("doesn't matter")); err != nil { | |
log.Fatal(err) | |
} | |
} | |
k := key{0x10} | |
e, _, err := db.Seek(k[:]) | |
if err != nil { | |
log.Fatal(err) | |
} | |
got := 0 | |
for { | |
_, _, err := e.Next() | |
if err != nil { | |
if err == io.EOF { | |
break | |
} | |
log.Fatal(err) | |
} | |
got++ | |
} | |
fmt.Printf("got: %d (expected: %d)\n", got, expected) | |
if err := db.Close(); err != nil { | |
log.Fatal(err) | |
} | |
} | |
func mustLoadFortunes() []key { | |
f, err := os.Open("/usr/local/plan9/lib/fortunes") | |
if err != nil { | |
panic("misfortune!") | |
} | |
defer f.Close() | |
s := bufio.NewScanner(f) | |
var ks []key | |
for s.Scan() { | |
k := sha1.Sum(s.Bytes()) | |
ks = append(ks, k) | |
} | |
if err := s.Err(); err != nil && err != io.EOF { | |
panic(err) | |
} | |
sort.Sort(withBytesCompare(ks)) | |
return ks | |
} | |
type withBytesCompare []key | |
func (a withBytesCompare) Len() int { return len(a) } | |
func (a withBytesCompare) Less(i, j int) bool { return bytes.Compare(a[i][:], a[j][:]) < 0 } | |
func (a withBytesCompare) Swap(i, j int) { a[i], a[j] = a[j], a[i] } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment