-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerate.go
276 lines (236 loc) · 6.69 KB
/
generate.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package main
import "fmt"
import "strconv"
import "math/rand"
const minkeysize = 16
// Generate presorted load, always return unique key,
// return nil after `n` keys.
func Generateloads(klen, vlen, n int64) func(k, v []byte) ([]byte, []byte) {
var textint [1024]byte
keynum := int64(0)
return func(key, value []byte) ([]byte, []byte) {
if keynum >= n {
return nil, nil
}
ascii := strconv.AppendInt(textint[:0], int64(keynum), 10)
// create key
key = Fixbuffer(key, int64(klen))
copy(key, zeros)
copy(key[klen-int64(len(ascii)):klen], ascii)
if value != nil { // create value
value = Fixbuffer(value, int64(vlen))
copytovalue(value, ascii, klen, vlen)
}
keynum++
return key, value
}
}
// Generate unsorted load, always return unique key,
// return nill after `n` keys.
func Generateloadr(
klen, vlen, n, seed int64) func(k, v []byte) ([]byte, []byte) {
var text [1024]byte
intn := n * rndscale
rnd := rand.New(rand.NewSource(seed))
bitmap := make([]byte, ((intn / 8) + 1))
count := int64(0)
return func(key, value []byte) ([]byte, []byte) {
if count >= n {
return nil, nil
}
ascii, key := makeuniquekey(rnd, bitmap, 0, intn, klen, text[:0], key)
//fmt.Printf("load %q\n", key)
value = makevalue(vlen, ascii, value)
count++
return key, value
}
}
// Generate keys greater than loadn, always return unique keys.
func Generatecreate(
klen, vlen, loadn, insertn,
seed int64) func(k, v []byte) ([]byte, []byte) {
var text [1024]byte
loadn = int64(loadn * rndscale)
intn := (insertn * rndscale)
rnd := rand.New(rand.NewSource(seed))
bitmap := make([]byte, ((intn / 8) + 1))
return func(key, value []byte) ([]byte, []byte) {
ascii, key := makeuniquekey(rnd, bitmap, loadn, intn, klen, text[:0], key)
//fmt.Printf("create %q\n", key)
value = makevalue(vlen, ascii, value)
return key, value
}
}
func Generateupdate(
klen, vlen, loadn, insertn,
seedl, seedc, mod int64) func(k, v []byte) ([]byte, []byte) {
var textint [1024]byte
var getkey func() int64
loadn1 := loadn * rndscale
intn := insertn * rndscale
rndl := rand.New(rand.NewSource(seedl))
rndc := rand.New(rand.NewSource(seedc))
lcount := int64(0)
getkey = func() (keynum int64) {
if lcount < loadn { // from load pool, headstart
keynum = int64(rndl.Intn(int(loadn1)))
} else if (lcount % 3) == 0 { // from create pool
keynum = loadn1 + int64(rndc.Intn(int(intn)))
} else { // from load pool
keynum = int64(rndl.Intn(int(loadn1)))
}
lcount++
if lcount >= loadn && (lcount%loadn) == 0 {
rndl = rand.New(rand.NewSource(seedl))
}
if mod >= 0 && (keynum%2) != mod {
return getkey()
}
return keynum
}
return func(key, value []byte) ([]byte, []byte) {
keynum := getkey()
ascii, key := makekey(keynum, klen, textint[:0], key)
//fmt.Printf("update %q\n", key)
value = makevalue(vlen, ascii, value)
return key, value
}
}
func Generateread(
klen, loadn, insertn, seedl, seedc int64) func([]byte, int64) []byte {
var textint [1024]byte
var getkey func(int64) int64
loadn1 := loadn * rndscale
intn := insertn * rndscale
rndl := rand.New(rand.NewSource(seedl))
rndc := rand.New(rand.NewSource(seedc))
lcount := int64(0)
getkey = func(mod int64) (keynum int64) {
if lcount < loadn { // from load pool, headstart
keynum = int64(rndl.Intn(int(loadn1)))
} else if mod > 0 && (lcount%mod) != 0 { // from create pool
keynum = loadn1 + int64(rndc.Intn(int(intn)))
} else { // from load pool
keynum = int64(rndl.Intn(int(loadn1)))
}
lcount++
if lcount >= loadn && (lcount%loadn) == 0 {
rndl = rand.New(rand.NewSource(seedl))
rndc = rand.New(rand.NewSource(seedc))
}
return keynum
}
return func(key []byte, ncreates int64) []byte {
keynum := getkey(ncreates / loadn)
_, key = makekey(keynum, klen, textint[:0], key)
//fmt.Printf("read %q\n", key)
return key
}
}
func Generatereadseq(klen, loadn, seedl int64) func([]byte, int64) []byte {
var textint [1024]byte
var getkey func(int64) int64
rndl := rand.New(rand.NewSource(seedl))
lcount := int64(0)
getkey = func(mod int64) (keynum int64) {
keynum = int64(rndl.Intn(int(loadn)))
lcount++
return keynum
}
return func(key []byte, ncreates int64) []byte {
keynum := getkey(ncreates / loadn)
_, key = makekey(keynum, klen, textint[:0], key)
return key
}
}
func Generatedelete(
klen, vlen,
loadn, insertn,
seedl, seedc, mod int64) func(k, v []byte) ([]byte, []byte) {
var textint [1024]byte
var getkey func() int64
loadn1 := loadn * rndscale
intn := insertn * rndscale
rndl := rand.New(rand.NewSource(seedl))
rndc := rand.New(rand.NewSource(seedc))
lcount := int64(0)
getkey = func() (keynum int64) {
if lcount < loadn { // from load pool, headstart
keynum = int64(rndl.Intn(int(loadn1)))
} else if (lcount % 3) == 0 { // from create pool
keynum = loadn1 + int64(rndc.Intn(int(intn)))
} else { // from load pool
keynum = int64(rndl.Intn(int(loadn1)))
}
lcount++
if lcount >= loadn && (lcount%loadn) == 0 {
rndl = rand.New(rand.NewSource(seedl))
}
if mod >= 0 && (keynum%2) != mod {
return getkey()
}
return keynum
}
return func(key, value []byte) ([]byte, []byte) {
keynum := getkey()
ascii, key := makekey(keynum, klen, textint[:0], key)
//fmt.Printf("delete %q\n", key)
value = makevalue(vlen, ascii, value)
return key, value
}
}
var rndscale = int64(3)
var bitmask = [8]byte{1, 2, 4, 8, 16, 32, 64, 128}
var zeros = make([]byte, 4096)
func makeuniquekey(
rnd *rand.Rand, bitmap []byte, offset, intn int64,
klen int64, textint, key []byte) ([]byte, []byte) {
for true {
keynum := int64(rnd.Intn(int(intn)))
if (bitmap[keynum/8] & bitmask[keynum%8]) == 0 {
bitmap[keynum/8] |= bitmask[keynum%8]
keynum += offset
ascii := strconv.AppendInt(textint[:0], keynum, 10)
// create key
key = Fixbuffer(key, int64(klen))
copy(key, zeros)
copy(key[klen-int64(len(ascii)):klen], ascii)
return ascii, key
}
}
panic(fmt.Errorf("unreachable code"))
}
func makekey(keynum, klen int64, textint, key []byte) ([]byte, []byte) {
ascii := strconv.AppendInt(textint[:0], keynum, 10)
// create key
key = Fixbuffer(key, int64(klen))
copy(key, zeros)
copy(key[klen-int64(len(ascii)):klen], ascii)
return ascii, key
}
func makevalue(vlen int64, ascii, value []byte) []byte {
if vlen == 0 {
return value
}
if vlen < int64(len(ascii)) {
vlen = minkeysize
}
value = Fixbuffer(value, vlen)
copy(value, zeros)
copy(value[vlen-int64(len(ascii)):vlen], ascii)
return value
}
func copytovalue(value, ascii []byte, klen, vlen int64) []byte {
if vlen <= klen {
copy(value, zeros)
} else {
copy(value[vlen-klen:vlen], zeros)
}
copy(value[vlen-int64(len(ascii)):vlen], ascii)
return value
}
func init() {
for i := range zeros {
zeros[i] = '0'
}
}