forked from attic-labs/noms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_set_test.go
150 lines (122 loc) · 4.05 KB
/
table_set_test.go
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright 2016 Attic Labs, Inc. All rights reserved.
// Licensed under the Apache License, version 2.0:
// http://www.apache.org/licenses/LICENSE-2.0
package nbs
import (
"testing"
"github.com/stretchr/testify/assert"
)
var testChunks = [][]byte{[]byte("hello2"), []byte("goodbye2"), []byte("badbye2")}
func TestTableSetPrependEmpty(t *testing.T) {
ts := newFakeTableSet().Prepend(newMemTable(testMemTableSize), &Stats{})
assert.Empty(t, ts.ToSpecs())
}
func TestTableSetPrepend(t *testing.T) {
assert := assert.New(t)
ts := newFakeTableSet()
assert.Empty(ts.ToSpecs())
mt := newMemTable(testMemTableSize)
mt.addChunk(computeAddr(testChunks[0]), testChunks[0])
ts = ts.Prepend(mt, &Stats{})
firstSpecs := ts.ToSpecs()
assert.Len(firstSpecs, 1)
mt = newMemTable(testMemTableSize)
mt.addChunk(computeAddr(testChunks[1]), testChunks[1])
mt.addChunk(computeAddr(testChunks[2]), testChunks[2])
ts = ts.Prepend(mt, &Stats{})
secondSpecs := ts.ToSpecs()
assert.Len(secondSpecs, 2)
assert.Equal(firstSpecs, secondSpecs[1:])
}
func TestTableSetToSpecsExcludesEmptyTable(t *testing.T) {
assert := assert.New(t)
ts := newFakeTableSet()
assert.Empty(ts.ToSpecs())
mt := newMemTable(testMemTableSize)
mt.addChunk(computeAddr(testChunks[0]), testChunks[0])
ts = ts.Prepend(mt, &Stats{})
mt = newMemTable(testMemTableSize)
ts = ts.Prepend(mt, &Stats{})
mt = newMemTable(testMemTableSize)
mt.addChunk(computeAddr(testChunks[1]), testChunks[1])
mt.addChunk(computeAddr(testChunks[2]), testChunks[2])
ts = ts.Prepend(mt, &Stats{})
specs := ts.ToSpecs()
assert.Len(specs, 2)
}
func TestTableSetFlattenExcludesEmptyTable(t *testing.T) {
assert := assert.New(t)
ts := newFakeTableSet()
assert.Empty(ts.ToSpecs())
mt := newMemTable(testMemTableSize)
mt.addChunk(computeAddr(testChunks[0]), testChunks[0])
ts = ts.Prepend(mt, &Stats{})
mt = newMemTable(testMemTableSize)
ts = ts.Prepend(mt, &Stats{})
mt = newMemTable(testMemTableSize)
mt.addChunk(computeAddr(testChunks[1]), testChunks[1])
mt.addChunk(computeAddr(testChunks[2]), testChunks[2])
ts = ts.Prepend(mt, &Stats{})
ts = ts.Flatten()
assert.EqualValues(ts.Size(), 2)
}
func TestTableSetExtract(t *testing.T) {
assert := assert.New(t)
ts := newFakeTableSet()
assert.Empty(ts.ToSpecs())
// Put in one table
mt := newMemTable(testMemTableSize)
mt.addChunk(computeAddr(testChunks[0]), testChunks[0])
ts = ts.Prepend(mt, &Stats{})
// Put in a second
mt = newMemTable(testMemTableSize)
mt.addChunk(computeAddr(testChunks[1]), testChunks[1])
mt.addChunk(computeAddr(testChunks[2]), testChunks[2])
ts = ts.Prepend(mt, &Stats{})
chunkChan := make(chan extractRecord)
go func() { defer close(chunkChan); ts.extract(chunkChan) }()
i := 0
for rec := range chunkChan {
a := computeAddr(testChunks[i])
assert.NotNil(rec.data, "Nothing for", a)
assert.Equal(testChunks[i], rec.data, "Item %d: %s != %s", i, string(testChunks[i]), string(rec.data))
assert.Equal(a, rec.a)
i++
}
}
func TestTableSetRebase(t *testing.T) {
assert := assert.New(t)
persister := newFakeTablePersister()
insert := func(ts tableSet, chunks ...[]byte) tableSet {
for _, c := range chunks {
mt := newMemTable(testMemTableSize)
mt.addChunk(computeAddr(c), c)
ts = ts.Prepend(mt, &Stats{})
}
return ts
}
fullTS := newTableSet(persister)
assert.Empty(fullTS.ToSpecs())
fullTS = insert(fullTS, testChunks...)
fullTS = fullTS.Flatten()
ts := newTableSet(persister)
ts = insert(ts, testChunks[0])
assert.Equal(1, ts.Size())
ts = ts.Flatten()
ts = insert(ts, []byte("novel"))
ts = ts.Rebase(fullTS.ToSpecs(), nil)
assert.Equal(4, ts.Size())
}
func TestTableSetPhysicalLen(t *testing.T) {
assert := assert.New(t)
ts := newFakeTableSet()
assert.Empty(ts.ToSpecs())
mt := newMemTable(testMemTableSize)
mt.addChunk(computeAddr(testChunks[0]), testChunks[0])
ts = ts.Prepend(mt, &Stats{})
mt = newMemTable(testMemTableSize)
mt.addChunk(computeAddr(testChunks[1]), testChunks[1])
mt.addChunk(computeAddr(testChunks[2]), testChunks[2])
ts = ts.Prepend(mt, &Stats{})
assert.True(ts.physicalLen() > indexSize(ts.count()))
}