OLD | NEW |
1 package gkvlite | 1 package gkvlite |
2 | 2 |
3 import ( | 3 import ( |
4 "bytes" | 4 "bytes" |
5 "errors" | 5 "errors" |
6 "fmt" | 6 "fmt" |
7 "io" | 7 "io" |
8 "io/ioutil" | 8 "io/ioutil" |
9 "math/rand" | 9 "math/rand" |
10 "os" | 10 "os" |
(...skipping 1608 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1619 if m != 0 { | 1619 if m != 0 { |
1620 t.Errorf("expected 0 items, got: %v", m) | 1620 t.Errorf("expected 0 items, got: %v", m) |
1621 } | 1621 } |
1622 } | 1622 } |
1623 | 1623 |
1624 func TestItemCopy(t *testing.T) { | 1624 func TestItemCopy(t *testing.T) { |
1625 i := &Item{ | 1625 i := &Item{ |
1626 Key: []byte("hi"), | 1626 Key: []byte("hi"), |
1627 Val: []byte("world"), | 1627 Val: []byte("world"), |
1628 Priority: 1234, | 1628 Priority: 1234, |
1629 » » Transient: unsafe.Pointer(&node{}), | 1629 » » Transient: &node{}, |
1630 } | 1630 } |
1631 j := i.Copy() | 1631 j := i.Copy() |
1632 if !bytes.Equal(j.Key, i.Key) || !bytes.Equal(j.Val, i.Val) || | 1632 if !bytes.Equal(j.Key, i.Key) || !bytes.Equal(j.Val, i.Val) || |
1633 j.Priority != i.Priority || j.Transient != i.Transient { | 1633 j.Priority != i.Priority || j.Transient != i.Transient { |
1634 t.Errorf("expected item copy to work") | 1634 t.Errorf("expected item copy to work") |
1635 } | 1635 } |
1636 } | 1636 } |
1637 | 1637 |
1638 func TestCurFreeNodes(t *testing.T) { | 1638 func TestCurFreeNodes(t *testing.T) { |
1639 s, err := NewStore(nil) | 1639 s, err := NewStore(nil) |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1682 loadCollection(x, []string{"e", "d", "a", "c", "b", "c", "a"}) | 1682 loadCollection(x, []string{"e", "d", "a", "c", "b", "c", "a"}) |
1683 fmt.Printf("testing dump\n") | 1683 fmt.Printf("testing dump\n") |
1684 dump(s, x.root.root, 1) | 1684 dump(s, x.root.root, 1) |
1685 } | 1685 } |
1686 | 1686 |
1687 func TestStoreClose(t *testing.T) { | 1687 func TestStoreClose(t *testing.T) { |
1688 s, err := NewStore(nil) | 1688 s, err := NewStore(nil) |
1689 if err != nil || s == nil { | 1689 if err != nil || s == nil { |
1690 t.Errorf("expected memory-only NewStore to work") | 1690 t.Errorf("expected memory-only NewStore to work") |
1691 } | 1691 } |
1692 » if s.coll == unsafe.Pointer(nil) { | 1692 » if s.coll == nil { |
1693 t.Errorf("expected coll before Close()") | 1693 t.Errorf("expected coll before Close()") |
1694 } | 1694 } |
1695 s.Close() | 1695 s.Close() |
1696 » if s.coll != unsafe.Pointer(nil) { | 1696 » if s.coll != nil { |
1697 t.Errorf("expected no coll after Close()") | 1697 t.Errorf("expected no coll after Close()") |
1698 } | 1698 } |
1699 s.Close() | 1699 s.Close() |
1700 » if s.coll != unsafe.Pointer(nil) { | 1700 » if s.coll != nil { |
1701 t.Errorf("expected no coll after re-Close()") | 1701 t.Errorf("expected no coll after re-Close()") |
1702 } | 1702 } |
1703 s.Close() | 1703 s.Close() |
1704 » if s.coll != unsafe.Pointer(nil) { | 1704 » if s.coll != nil { |
1705 t.Errorf("expected no coll after re-Close()") | 1705 t.Errorf("expected no coll after re-Close()") |
1706 } | 1706 } |
1707 | 1707 |
1708 // Now, with a collection | 1708 // Now, with a collection |
1709 s, _ = NewStore(nil) | 1709 s, _ = NewStore(nil) |
1710 s.SetCollection("x", bytes.Compare) | 1710 s.SetCollection("x", bytes.Compare) |
1711 » if s.coll == unsafe.Pointer(nil) { | 1711 » if s.coll == nil { |
1712 t.Errorf("expected coll before Close()") | 1712 t.Errorf("expected coll before Close()") |
1713 } | 1713 } |
1714 s.Close() | 1714 s.Close() |
1715 » if s.coll != unsafe.Pointer(nil) { | 1715 » if s.coll != nil { |
1716 t.Errorf("expected no coll after Close()") | 1716 t.Errorf("expected no coll after Close()") |
1717 } | 1717 } |
1718 s.Close() | 1718 s.Close() |
1719 » if s.coll != unsafe.Pointer(nil) { | 1719 » if s.coll != nil { |
1720 t.Errorf("expected no coll after Close()") | 1720 t.Errorf("expected no coll after Close()") |
1721 } | 1721 } |
1722 } | 1722 } |
1723 | 1723 |
1724 func TestItemNumValBytes(t *testing.T) { | 1724 func TestItemNumValBytes(t *testing.T) { |
1725 var x *Collection | 1725 var x *Collection |
1726 var h *Item | 1726 var h *Item |
1727 s, err := NewStoreEx(nil, StoreCallbacks{ | 1727 s, err := NewStoreEx(nil, StoreCallbacks{ |
1728 ItemValLength: func(c *Collection, i *Item) int { | 1728 ItemValLength: func(c *Collection, i *Item) int { |
1729 if c != x { | 1729 if c != x { |
(...skipping 471 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2201 x.SetItem(&Item{ | 2201 x.SetItem(&Item{ |
2202 Key: []byte("a"), | 2202 Key: []byte("a"), |
2203 Val: []byte("aaa"), | 2203 Val: []byte("aaa"), |
2204 Priority: 100, | 2204 Priority: 100, |
2205 }) | 2205 }) |
2206 s.Flush() | 2206 s.Flush() |
2207 | 2207 |
2208 readShouldErr = true | 2208 readShouldErr = true |
2209 | 2209 |
2210 rnl := x.rootAddRef() | 2210 rnl := x.rootAddRef() |
2211 » rnl.root.node = unsafe.Pointer(nil) // Evict node from memory to force r
eading. | 2211 » rnl.root.node = nil // Evict node from memory to force reading. |
2212 | 2212 |
2213 _, _, _, _, err = numInfo(s, rnl.root, empty_nodeLoc) | 2213 _, _, _, _, err = numInfo(s, rnl.root, empty_nodeLoc) |
2214 if err == nil { | 2214 if err == nil { |
2215 t.Errorf("expected numInfo to error") | 2215 t.Errorf("expected numInfo to error") |
2216 } | 2216 } |
2217 _, _, _, _, err = numInfo(s, empty_nodeLoc, rnl.root) | 2217 _, _, _, _, err = numInfo(s, empty_nodeLoc, rnl.root) |
2218 if err == nil { | 2218 if err == nil { |
2219 t.Errorf("expected numInfo to error") | 2219 t.Errorf("expected numInfo to error") |
2220 } | 2220 } |
2221 } | 2221 } |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2343 Priority: 100, | 2343 Priority: 100, |
2344 }) | 2344 }) |
2345 rnl := x.rootAddRef() | 2345 rnl := x.rootAddRef() |
2346 | 2346 |
2347 writeShouldErr = true | 2347 writeShouldErr = true |
2348 if rnl.root.write(s) == nil { | 2348 if rnl.root.write(s) == nil { |
2349 t.Errorf("expected write node to fail") | 2349 t.Errorf("expected write node to fail") |
2350 } | 2350 } |
2351 writeShouldErr = false | 2351 writeShouldErr = false |
2352 | 2352 |
2353 » rnl.root.node = unsafe.Pointer(nil) // Force a nil node. | 2353 » rnl.root.node = nil // Force a nil node. |
2354 if rnl.root.write(s) != nil { | 2354 if rnl.root.write(s) != nil { |
2355 t.Errorf("expected write node on nil node to work") | 2355 t.Errorf("expected write node on nil node to work") |
2356 } | 2356 } |
2357 } | 2357 } |
2358 | 2358 |
2359 func TestStoreRefCount(t *testing.T) { | 2359 func TestStoreRefCount(t *testing.T) { |
2360 counts := map[string]int{} | 2360 counts := map[string]int{} |
2361 nadds := 0 | 2361 nadds := 0 |
2362 ndecs := 0 | 2362 ndecs := 0 |
2363 s, err := NewStoreEx(nil, StoreCallbacks{ | 2363 s, err := NewStoreEx(nil, StoreCallbacks{ |
(...skipping 436 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2800 if err != nil { | 2800 if err != nil { |
2801 t.Fatalf("expected nil error, got: %v", err) | 2801 t.Fatalf("expected nil error, got: %v", err) |
2802 } | 2802 } |
2803 } | 2803 } |
2804 mustJustOneRef(fmt.Sprintf("i: %d", i)) | 2804 mustJustOneRef(fmt.Sprintf("i: %d", i)) |
2805 } | 2805 } |
2806 } | 2806 } |
2807 | 2807 |
2808 // perm returns a random permutation of n Int items in the range [0, n). | 2808 // perm returns a random permutation of n Int items in the range [0, n). |
2809 func perm(n int) (out [][]byte) { | 2809 func perm(n int) (out [][]byte) { |
2810 for _, v := range rand.Perm(n) { | 2810 » for _, v := range rand.Perm(n) { |
2811 out = append(out, []byte(strconv.Itoa(v))) | 2811 » » out = append(out, []byte(strconv.Itoa(v))) |
2812 } | 2812 » } |
2813 return out | 2813 » return out |
2814 } | 2814 } |
2815 | 2815 |
2816 const benchmarkSize = 10000 | 2816 const benchmarkSize = 10000 |
2817 | 2817 |
2818 func BenchmarkRandInsert(b *testing.B) { | 2818 func BenchmarkRandInsert(b *testing.B) { |
2819 b.StopTimer() | 2819 b.StopTimer() |
2820 insertP := perm(benchmarkSize) | 2820 insertP := perm(benchmarkSize) |
2821 i := 0 | 2821 i := 0 |
2822 b.StartTimer() | 2822 b.StartTimer() |
2823 for i < b.N { | 2823 for i < b.N { |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2875 b.StartTimer() | 2875 b.StartTimer() |
2876 for _, item := range removeP { | 2876 for _, item := range removeP { |
2877 x.Get(item) | 2877 x.Get(item) |
2878 i++ | 2878 i++ |
2879 if i >= b.N { | 2879 if i >= b.N { |
2880 return | 2880 return |
2881 } | 2881 } |
2882 } | 2882 } |
2883 } | 2883 } |
2884 } | 2884 } |
OLD | NEW |