Skip to content

Commit 9fcc06b

Browse files
committed
address comment
1 parent 91ce4a5 commit 9fcc06b

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

common/unsafe/src/main/java/org/apache/spark/unsafe/types/ByteArray.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static long getPrefix(byte[] bytes) {
4949
return getPrefix(bytes, Platform.BYTE_ARRAY_OFFSET, bytes.length);
5050
}
5151

52-
protected static long getPrefix(Object base, long offset, int numBytes) {
52+
static long getPrefix(Object base, long offset, int numBytes) {
5353
// Since JVMs are either 4-byte aligned or 8-byte aligned, we check the size of the bytes.
5454
// If size is 0, just return 0.
5555
// If size is between 1 and 4 (inclusive), assume data is 4-byte aligned under the hood and
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.spark.unsafe.array;
19+
20+
import org.apache.spark.unsafe.Platform;
21+
import org.apache.spark.unsafe.types.ByteArray;
22+
import org.junit.Assert;
23+
import org.junit.Test;
24+
25+
public class ByteArraySuite {
26+
private long getPrefixByByte(byte[] bytes) {
27+
final int minLen = Math.min(bytes.length, 8);
28+
long p = 0;
29+
for (int i = 0; i < minLen; ++i) {
30+
p |= ((long) Platform.getByte(bytes, Platform.BYTE_ARRAY_OFFSET + i) & 0xff)
31+
<< (56 - 8 * i);
32+
}
33+
return p;
34+
}
35+
36+
@Test
37+
public void testGetPrefix() {
38+
for (int i = 0; i <= 9; i++) {
39+
byte[] bytes = new byte[i];
40+
int prefix = i - 1;
41+
while (prefix >= 0) {
42+
bytes[prefix] = (byte) prefix;
43+
prefix -= 1;
44+
}
45+
46+
long result = ByteArray.getPrefix(bytes);
47+
long expected = getPrefixByByte(bytes);
48+
Assert.assertEquals(result, expected);
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)