/
helper.go
139 lines (112 loc) · 2.95 KB
/
helper.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
package main
import (
"bytes"
"unsafe"
"math/rand"
)
func beforeNull(b []byte) []byte {
/* Find the index of the first null character. Assembly in Go. */
i := bytes.IndexByte(b, 0)
if i == -1 {
/* Return `b` if no null character is found in `b`. */
return b
} else {
return b[: i]
}
}
/* Both `pickBetween` and `pickBetweenNonUniformly` are inclusive. */
func pickBetween(rd *rand.Rand, min, max uint32) uint32 {
n := rd.Uint32() % (max - min + 1) + min
return n
}
func pickBetweenNonUniformly(rd *rand.Rand, a, c, min, max uint32) uint32 {
/* See Silo tpcc.cc:L360. */
x := (pickBetween(rd, 0, a) | pickBetween(rd, min, max)) + c
y := x % (max - min + 1) + min
return y
}
func trueWithProb(rd *rand.Rand, prob uint32) bool {
b := rd.Uint32() % 100 < prob
return b
}
/**
* Assume:
* 1. nwh > 0
* 2. wid > 0
* 3. wid <= nwh
*/
func pickWarehouseIdExcept(rd *rand.Rand, nwh, wid uint8) uint8 {
if nwh == 1 {
return 1
}
widret := wid
for widret == wid {
widret = uint8(rd.Uint32() % uint32(nwh)) + 1
}
return widret
}
func pickNOrderLines(rd *rand.Rand) uint8 {
n := uint8(pickBetween(rd, uint32(OL_MIN_CNT), uint32(OL_MAX_CNT)))
return n
}
func pickQuantity(rd *rand.Rand) uint8 {
n := uint8(pickBetween(rd, 1, uint32(OL_MAX_QUANTITY)))
return n
}
/* TODO */
func getTime() uint32 {
return 0
}
func encodeBool(buf []byte, b bool, offset uint64) {
if b {
buf[offset] = 1
} else {
buf[offset] = 0
}
}
func encodeU8(buf []byte, n uint8, offset uint64) {
buf[offset] = n
}
func encodeU16(buf []byte, n uint16, offset uint64) {
copy(buf[offset :], unsafe.Slice((*byte)(unsafe.Pointer(&n)), 2))
}
func encodeU32(buf []byte, n uint32, offset uint64) {
copy(buf[offset :], unsafe.Slice((*byte)(unsafe.Pointer(&n)), 4))
}
func encodeU64(buf []byte, n uint64, offset uint64) {
copy(buf[offset :], unsafe.Slice((*byte)(unsafe.Pointer(&n)), 8))
}
func encodeF32(buf []byte, n float32, offset uint64) {
copy(buf[offset :], unsafe.Slice((*byte)(unsafe.Pointer(&n)), 4))
}
func encodeBytes(buf []byte, src []byte, offset uint64) {
copy(buf[offset :], src)
}
func decodeBool(ptr *bool, s string, offset uint64) {
if s[offset] == 1 {
*ptr = true
} else {
*ptr = false
}
}
func decodeU8(ptr *uint8, s string, offset uint64) {
copy(unsafe.Slice((*byte)(unsafe.Pointer(ptr)), 1), s[offset :])
}
func decodeU16(ptr *uint16, s string, offset uint64) {
copy(unsafe.Slice((*byte)(unsafe.Pointer(ptr)), 2), s[offset :])
}
func decodeU32(ptr *uint32, s string, offset uint64) {
copy(unsafe.Slice((*byte)(unsafe.Pointer(ptr)), 4), s[offset :])
}
func decodeU64(ptr *uint64, s string, offset uint64) {
copy(unsafe.Slice((*byte)(unsafe.Pointer(ptr)), 8), s[offset :])
}
func decodeString(buf []byte, s string, offset uint64) {
copy(buf, s[offset :])
}
func decodeF32(ptr *float32, s string, offset uint64) {
copy(unsafe.Slice((*byte)(unsafe.Pointer(ptr)), 4), s[offset :])
}
func bytesToString(bs []byte) string {
return *(*string)(unsafe.Pointer(&bs))
}