-
Notifications
You must be signed in to change notification settings - Fork 1
/
pon.go
174 lines (162 loc) · 3.48 KB
/
pon.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
// Copyright (c) 2023, donnie <donnie4w@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.
//
// github.com/donnie4w/tldb
package level1
import (
"sort"
"strings"
"github.com/donnie4w/tldb/sys"
. "github.com/donnie4w/tldb/util"
)
type pon struct {
store_num int
uuids []int64
}
func Pon() *pon {
return &pon{sys.STORENODENUM, nodeWare.GetAllRunUUID()}
}
func PonUUIDS(uuids []int64) *pon {
return &pon{sys.STORENODENUM, uuids}
}
func (this *pon) ExcuNode(key string) int64 {
if sys.IsStandAlone() {
return sys.UUID
}
umap := make(map[uint32]int64, 0)
keylen := len([]byte(key))
for _, uuid := range this.uuids {
bs := make([]byte, keylen+8)
copy(bs[0:keylen], []byte(key))
copy(bs[keylen:], Int64ToBytes(uuid))
umap[CRC32(bs)] = uuid
}
return max(umap)
}
func (this *pon) ExcuNodes(key string) (ids []int64) {
length := this.store_num
if len(this.uuids) < length || length == 0 {
length = len(this.uuids)
}
umap := make(map[uint32]int64, 0)
keybslen := len([]byte(key))
for _, uuid := range this.uuids {
bs := make([]byte, keybslen+8)
copy(bs[0:keybslen], []byte(key))
copy(bs[keybslen:], Int64ToBytes(uuid))
umap[CRC32(bs)] = uuid
}
return sortArr(umap, length)
}
func (this *pon) ExcuBatchNodes(kvmap map[string][]byte, delarr []string) (ids []int64) {
length := this.store_num
if len(this.uuids) <= length || length == 0 {
return this.uuids
}
keylen := 0
if kvmap != nil {
keylen = keylen + len(kvmap)
}
if delarr != nil {
keylen = keylen + len(delarr)
}
keys := make([]string, keylen)
i := 0
if kvmap != nil {
for k := range kvmap {
keys[i] = k
i++
}
}
if delarr != nil {
for _, k := range delarr {
keys[i] = k
i++
}
}
sort.Strings(keys)
key := strings.Join(keys, "")
umap := make(map[uint32]int64, 0)
keybslen := len([]byte(key))
for _, uuid := range this.uuids {
bs := make([]byte, keybslen+8)
copy(bs[0:keybslen], []byte(key))
copy(bs[keybslen:], Int64ToBytes(uuid))
umap[CRC32(bs)] = uuid
}
return sortArr(umap, length)
}
// func (this *pon) checkuuid(key string, checkUUids []int64) bool {
// uuid := this.ExcuNode(key)
// for _, v := range checkUUids {
// if uuid == v {
// return true
// }
// }
// return false
// }
// func (this *pon) compareArrHalf(u1, u2 []int64) bool {
// if len(u1) > len(u2) {
// return this.compareArrHalf(u2, u1)
// }
// f := 0
// _u2 := make([]int64, len(u2))
// copy(_u2, u2)
// for _, v := range u1 {
// for i, v2 := range _u2 {
// if v == v2 {
// _u2[i] = 17
// f++
// break
// }
// }
// }
// return f >= len(u1)/2+1
// }
// func (this *pon) compareArrALL(u1, u2 []int64) bool {
// if len(u1) != len(u2) {
// return false
// }
// _u2 := make([]int64, len(u2))
// copy(_u2, u2)
// for _, v := range u1 {
// f := false
// for i, v2 := range _u2 {
// if v == v2 {
// _u2[i] = 17
// f = true
// break
// }
// }
// if !f {
// return false
// }
// }
// return true
// }
func sortArr(umap map[uint32]int64, length int) (uuids []int64) {
ustrs := make([]uint32, len(umap))
i := 0
for n := range umap {
ustrs[i] = n
i++
}
sort.Slice(ustrs, func(i, j int) bool { return ustrs[i] > ustrs[j] })
uuids = make([]int64, length)
for i := 0; i < length; i++ {
uuids[i] = umap[ustrs[i]]
}
return
}
func max(umap map[uint32]int64) (uuid int64) {
var _r uint32
for u := range umap {
if _r < u {
_r = u
}
}
uuid = umap[_r]
return
}