| 1 |
package diskv |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"reflect" |
| 6 |
"testing" |
| 7 |
"time" |
| 8 |
) |
| 9 |
|
| 10 |
func strLess(a, b string) bool { return a < b } |
| 11 |
|
| 12 |
func cmpStrings(a, b []string) bool { |
| 13 |
if len(a) != len(b) { |
| 14 |
return false |
| 15 |
} |
| 16 |
for i := 0; i < len(a); i++ { |
| 17 |
if a[i] != b[i] { |
| 18 |
return false |
| 19 |
} |
| 20 |
} |
| 21 |
return true |
| 22 |
} |
| 23 |
|
| 24 |
func (d *Diskv) isIndexed(key string) bool { |
| 25 |
if d.Index == nil { |
| 26 |
return false |
| 27 |
} |
| 28 |
|
| 29 |
for _, got := range d.Index.Keys("", 1000) { |
| 30 |
if got == key { |
| 31 |
return true |
| 32 |
} |
| 33 |
} |
| 34 |
return false |
| 35 |
} |
| 36 |
|
| 37 |
func TestIndexOrder(t *testing.T) { |
| 38 |
d := New(Options{ |
| 39 |
BasePath: "index-test", |
| 40 |
CacheSizeMax: 1024, |
| 41 |
Index: &BTreeIndex{}, |
| 42 |
IndexLess: strLess, |
| 43 |
}) |
| 44 |
defer d.EraseAll() |
| 45 |
|
| 46 |
v := []byte{'1', '2', '3'} |
| 47 |
d.Write("a", v) |
| 48 |
if !d.isIndexed("a") { |
| 49 |
t.Fatalf("'a' not indexed after write") |
| 50 |
} |
| 51 |
d.Write("1", v) |
| 52 |
d.Write("m", v) |
| 53 |
d.Write("-", v) |
| 54 |
d.Write("A", v) |
| 55 |
|
| 56 |
expectedKeys := []string{"-", "1", "A", "a", "m"} |
| 57 |
keys := []string{} |
| 58 |
for _, key := range d.Index.Keys("", 100) { |
| 59 |
keys = append(keys, key) |
| 60 |
} |
| 61 |
|
| 62 |
if !cmpStrings(keys, expectedKeys) { |
| 63 |
t.Fatalf("got %s, expected %s", keys, expectedKeys) |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
func TestIndexLoad(t *testing.T) { |
| 68 |
d1 := New(Options{ |
| 69 |
BasePath: "index-test", |
| 70 |
CacheSizeMax: 1024, |
| 71 |
}) |
| 72 |
defer d1.EraseAll() |
| 73 |
|
| 74 |
val := []byte{'1', '2', '3'} |
| 75 |
keys := []string{"a", "b", "c", "d", "e", "f", "g"} |
| 76 |
for _, key := range keys { |
| 77 |
d1.Write(key, val) |
| 78 |
} |
| 79 |
|
| 80 |
d2 := New(Options{ |
| 81 |
BasePath: "index-test", |
| 82 |
CacheSizeMax: 1024, |
| 83 |
Index: &BTreeIndex{}, |
| 84 |
IndexLess: strLess, |
| 85 |
}) |
| 86 |
defer d2.EraseAll() |
| 87 |
|
| 88 |
// check d2 has properly loaded existing d1 data |
| 89 |
for _, key := range keys { |
| 90 |
if !d2.isIndexed(key) { |
| 91 |
t.Fatalf("key '%s' not indexed on secondary", key) |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
// cache one |
| 96 |
if readValue, err := d2.Read(keys[0]); err != nil { |
| 97 |
t.Fatalf("%s", err) |
| 98 |
} else if bytes.Compare(val, readValue) != 0 { |
| 99 |
t.Fatalf("%s: got %s, expected %s", keys[0], readValue, val) |
| 100 |
} |
| 101 |
|
| 102 |
// make sure it got cached |
| 103 |
for i := 0; i < 10 && !d2.isCached(keys[0]); i++ { |
| 104 |
time.Sleep(10 * time.Millisecond) |
| 105 |
} |
| 106 |
if !d2.isCached(keys[0]) { |
| 107 |
t.Fatalf("key '%s' not cached", keys[0]) |
| 108 |
} |
| 109 |
|
| 110 |
// kill the disk |
| 111 |
d1.EraseAll() |
| 112 |
|
| 113 |
// cached value should still be there in the second |
| 114 |
if readValue, err := d2.Read(keys[0]); err != nil { |
| 115 |
t.Fatalf("%s", err) |
| 116 |
} else if bytes.Compare(val, readValue) != 0 { |
| 117 |
t.Fatalf("%s: got %s, expected %s", keys[0], readValue, val) |
| 118 |
} |
| 119 |
|
| 120 |
// but not in the original |
| 121 |
if _, err := d1.Read(keys[0]); err == nil { |
| 122 |
t.Fatalf("expected error reading from flushed store") |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
func TestIndexKeysEmptyFrom(t *testing.T) { |
| 127 |
d := New(Options{ |
| 128 |
BasePath: "index-test", |
| 129 |
CacheSizeMax: 1024, |
| 130 |
Index: &BTreeIndex{}, |
| 131 |
IndexLess: strLess, |
| 132 |
}) |
| 133 |
defer d.EraseAll() |
| 134 |
|
| 135 |
for _, k := range []string{"a", "c", "z", "b", "x", "b", "y"} { |
| 136 |
d.Write(k, []byte("1")) |
| 137 |
} |
| 138 |
|
| 139 |
want := []string{"a", "b", "c", "x", "y", "z"} |
| 140 |
have := d.Index.Keys("", 99) |
| 141 |
if !reflect.DeepEqual(want, have) { |
| 142 |
t.Errorf("want %v, have %v", want, have) |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
func TestBadKeys(t *testing.T) { |
| 147 |
d := New(Options{ |
| 148 |
BasePath: "index-test", |
| 149 |
CacheSizeMax: 1024, |
| 150 |
Index: &BTreeIndex{}, |
| 151 |
IndexLess: strLess, |
| 152 |
}) |
| 153 |
defer d.EraseAll() |
| 154 |
|
| 155 |
for _, k := range []string{"a/a"} { |
| 156 |
err := d.Write(k, []byte("1")) |
| 157 |
if err != errBadKey { |
| 158 |
t.Errorf("Expected bad key error, got: %v", err) |
| 159 |
} |
| 160 |
} |
| 161 |
} |