forked from vearch/vearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.go
More file actions
123 lines (106 loc) · 2.34 KB
/
common.go
File metadata and controls
123 lines (106 loc) · 2.34 KB
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
// Copyright 2019 The Vearch Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package util
import (
"encoding/binary"
"fmt"
"math"
"strings"
)
func BuildAddr(ip string, port uint16) string {
return fmt.Sprintf("%s:%d", ip, port)
}
func BuildAddrBothString(ip string, port string) string {
return fmt.Sprintf("%s:%s", ip, port)
}
func ParseAddr(addr string) []string {
pair := strings.Split(addr, ":")
if len(pair) != 2 {
return nil
}
return pair
}
func BytesPrefix(prefix []byte) ([]byte, []byte) {
var limit []byte
for i := len(prefix) - 1; i >= 0; i-- {
c := prefix[i]
if c < 0xff {
limit = make([]byte, i+1)
copy(limit, prefix)
limit[i] = c + 1
break
}
}
return prefix, limit
}
func SlotSplit(start, end uint32, n uint64) []uint32 {
if n <= 0 {
return nil
}
if uint64(end-start)+1 < (n) {
return nil
}
var min, max uint32
if start <= end {
min = start
max = end
} else {
min = end
max = start
}
ret := make([]uint32, 0)
switch n {
case 1:
ret = append(ret, min)
case 2:
ret = append(ret, min)
ret = append(ret, max)
default:
step := (max - min) / uint32(n-1)
ret = append(ret, min)
for i := uint64(1); i < n-1; i++ {
ret = append(ret, min+uint32(i)*step)
}
ret = append(ret, max)
}
return ret
}
func BytesToUint32(b []byte) uint32 {
if len(b) != 4 {
return 0
}
return binary.BigEndian.Uint32(b)
}
func Uint32ToBytes(v uint32) []byte {
b := make([]byte, 4)
binary.BigEndian.PutUint32(b, v)
return b
}
func BytesToUint64(b []byte) uint64 {
if len(b) != 8 {
return 0
}
return binary.BigEndian.Uint64(b)
}
func Uint64ToBytes(v uint64) []byte {
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, v)
return b
}
func Float32ToByte(f float32) []byte {
bits := math.Float32bits(f)
bytes := make([]byte, 4)
binary.LittleEndian.PutUint32(bytes, bits)
return bytes
}