-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathkeys.go
129 lines (106 loc) · 2.1 KB
/
keys.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
package ddtxn
import (
"fmt"
"strconv"
)
type KeyGenFunc func(uint64) Key
func CKey(x uint64, ch rune) Key {
var b [16]byte
var i uint64
for i = 0; i < 8; i++ {
b[i] = byte((x >> (i * 8)))
}
b[8] = byte(ch)
return Key(b)
}
func UndoCKey(k Key) (uint64, rune) {
b := [16]byte(k)
var x uint64
var i uint64
for i = 0; i < 8; i++ {
v := uint32(b[i])
x = x + uint64(v<<(i*8))
}
return x, rune(b[8])
}
func (k Key) String() string {
x, y := UndoCKey(k)
return fmt.Sprintf("[%v%v]", strconv.QuoteRuneToASCII(y), x)
}
func TKey(x uint64, y uint64) Key {
var b [16]byte
var i uint64
for i = 0; i < 8; i++ {
b[i] = byte((x >> (i * 8)))
}
for i = 8; i < 16; i++ {
b[i] = byte((y >> (i * 8)))
}
return Key(b)
}
func SKey(s string) Key {
var b [16]byte
end := 16
if len(s) < 16 {
end = len(s)
}
for i := 0; i < end; i++ {
b[i] = s[i]
}
return Key(b)
}
func UserKey(bidder uint64) Key {
return CKey(uint64(bidder), 'u')
}
func NicknameKey(bidder uint64) Key {
return CKey(uint64(bidder), 'd')
}
func BidKey(id uint64) Key {
return CKey(id, 'b')
}
func PairKey(x uint32, y uint32, ch rune) Key {
var b [16]byte
var i uint64
for i = 0; i < 4; i++ {
b[i] = byte((x >> (i * 8)))
b[i+4] = byte((y >> (i * 8)))
}
b[8] = byte(ch)
return Key(b)
}
func PairBidKey(bidder uint64, product uint64) Key {
return PairKey(uint32(bidder), uint32(product), 'z')
}
func ItemKey(item uint64) Key {
return CKey(item, 'i')
}
func ProductKey(product int) Key {
return CKey(uint64(product), 'p')
}
func MaxBidKey(item uint64) Key {
return CKey(item, 'm')
}
func NumBidsKey(item uint64) Key {
return CKey(item, 'n')
}
func BidsPerItemKey(item uint64) Key {
return CKey(item, 'p')
}
func MaxBidBidderKey(item uint64) Key {
return CKey(item, 'a')
}
func BuyNowKey(item uint64) Key {
return CKey(item, 'k')
}
func CommentKey(item uint64) Key {
return CKey(item, 'c')
}
func ItemsByCatKey(item uint64) Key {
return CKey(item, 't')
}
func ItemsByRegKey(region uint64, categ uint64) Key {
return PairKey(uint32(region), uint32(categ), 'r')
}
func RatingKey(user uint64) Key {
return CKey(user, 's')
}