aboutsummaryrefslogtreecommitdiffstats
path: root/Godeps/_workspace/src/github.com/syndtr/goleveldb/leveldb/table/table_test.go
blob: d7d3b2a4bfd698219d133a90cade2487e48a5742 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright (c) 2014, Suryandaru Triandana <syndtr@gmail.com>
// All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package table

import (
    "bytes"

    . "github.com/onsi/ginkgo"
    . "github.com/onsi/gomega"

    "github.com/syndtr/goleveldb/leveldb/iterator"
    "github.com/syndtr/goleveldb/leveldb/opt"
    "github.com/syndtr/goleveldb/leveldb/testutil"
    "github.com/syndtr/goleveldb/leveldb/util"
)

type tableWrapper struct {
    *Reader
}

func (t tableWrapper) TestFind(key []byte) (rkey, rvalue []byte, err error) {
    return t.Reader.Find(key, nil)
}

func (t tableWrapper) TestGet(key []byte) (value []byte, err error) {
    return t.Reader.Get(key, nil)
}

func (t tableWrapper) TestNewIterator(slice *util.Range) iterator.Iterator {
    return t.Reader.NewIterator(slice, nil)
}

var _ = testutil.Defer(func() {
    Describe("Table", func() {
        Describe("approximate offset test", func() {
            var (
                buf = &bytes.Buffer{}
                o   = &opt.Options{
                    BlockSize:   1024,
                    Compression: opt.NoCompression,
                }
            )

            // Building the table.
            tw := NewWriter(buf, o)
            tw.Append([]byte("k01"), []byte("hello"))
            tw.Append([]byte("k02"), []byte("hello2"))
            tw.Append([]byte("k03"), bytes.Repeat([]byte{'x'}, 10000))
            tw.Append([]byte("k04"), bytes.Repeat([]byte{'x'}, 200000))
            tw.Append([]byte("k05"), bytes.Repeat([]byte{'x'}, 300000))
            tw.Append([]byte("k06"), []byte("hello3"))
            tw.Append([]byte("k07"), bytes.Repeat([]byte{'x'}, 100000))
            err := tw.Close()

            It("Should be able to approximate offset of a key correctly", func() {
                Expect(err).ShouldNot(HaveOccurred())

                tr := NewReader(bytes.NewReader(buf.Bytes()), int64(buf.Len()), nil, o)
                CheckOffset := func(key string, expect, threshold int) {
                    offset, err := tr.OffsetOf([]byte(key))
                    Expect(err).ShouldNot(HaveOccurred())
                    Expect(offset).Should(BeNumerically("~", expect, threshold), "Offset of key %q", key)
                }

                CheckOffset("k0", 0, 0)
                CheckOffset("k01a", 0, 0)
                CheckOffset("k02", 0, 0)
                CheckOffset("k03", 0, 0)
                CheckOffset("k04", 10000, 1000)
                CheckOffset("k04a", 210000, 1000)
                CheckOffset("k05", 210000, 1000)
                CheckOffset("k06", 510000, 1000)
                CheckOffset("k07", 510000, 1000)
                CheckOffset("xyz", 610000, 2000)
            })
        })

        Describe("read test", func() {
            Build := func(kv testutil.KeyValue) testutil.DB {
                o := &opt.Options{
                    BlockSize:            512,
                    BlockRestartInterval: 3,
                }
                buf := &bytes.Buffer{}

                // Building the table.
                tw := NewWriter(buf, o)
                kv.Iterate(func(i int, key, value []byte) {
                    tw.Append(key, value)
                })
                tw.Close()

                // Opening the table.
                tr := NewReader(bytes.NewReader(buf.Bytes()), int64(buf.Len()), nil, o)
                return tableWrapper{tr}
            }
            Test := func(kv *testutil.KeyValue, body func(r *Reader)) func() {
                return func() {
                    db := Build(*kv)
                    if body != nil {
                        body(db.(tableWrapper).Reader)
                    }
                    testutil.KeyValueTesting(nil, db, *kv)
                }
            }

            testutil.AllKeyValueTesting(nil, Build)
            Describe("with one key per block", Test(testutil.KeyValue_Generate(nil, 9, 1, 10, 512, 512), func(r *Reader) {
                It("should have correct blocks number", func() {
                    Expect(r.indexBlock.restartsLen).Should(Equal(9))
                })
            }))
        })
    })
})