forked from naver/arcus-java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBTreeUtil.java
More file actions
104 lines (85 loc) · 2.82 KB
/
BTreeUtil.java
File metadata and controls
104 lines (85 loc) · 2.82 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
/*
* arcus-java-client : Arcus Java client
* Copyright 2010-2014 NAVER Corp.
* Copyright 2014-2022 JaM2in Co., Ltd.
*
* 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 net.spy.memcached.util;
import net.spy.memcached.CachedData;
import net.spy.memcached.collection.BKeyObject;
import net.spy.memcached.collection.Element;
import net.spy.memcached.transcoders.Transcoder;
public final class BTreeUtil {
private static final String HEXES = "0123456789ABCDEF";
private BTreeUtil() {
}
public static String toHex(byte[] byteArray) {
if (byteArray == null) {
return null;
}
final StringBuilder hex = new StringBuilder(2 * byteArray.length + 2);
hex.append("0x");
for (final byte b : byteArray) {
hex.append(HEXES.charAt((b & 0xF0) >> 4));
hex.append(HEXES.charAt((b & 0x0F)));
}
return hex.toString();
}
public static byte[] hexStringToByteArrays(String str) {
if (str == null) {
return null;
}
if (str.startsWith("0x")) {
str = str.substring(2);
}
if (str.length() == 0) {
return new byte[0];
}
if (str.length() % 2 != 0) {
throw new IllegalArgumentException("Invalid hex string.");
}
int len = str.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(str.charAt(i), 16) << 4) + Character
.digit(str.charAt(i + 1), 16));
}
return data;
}
public static int compareByteArraysInLexOrder(byte[] array1, byte[] array2) {
int diff;
for (int i = 0; i < array1.length && i < array2.length; i++) {
diff = (array1[i] & 0xFF) - (array2[i] & 0xFF);
if (diff != 0) {
return diff;
}
}
return array1.length - array2.length;
}
public static <T> Element<T> makeBTreeElement(BKeyObject bkey,
CachedData cachedData,
Transcoder<T> tc) {
Element<T> element = null;
T value = tc.decode(cachedData);
switch (bkey.getType()) {
case LONG:
element = new Element<>(bkey.getLongBKey(), value, cachedData.getEFlag());
break;
case BYTEARRAY:
element = new Element<>(bkey.getByteArrayBKeyRaw(), value, cachedData.getEFlag());
break;
}
return element;
}
}