0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 package org.apache.spark.unsafe.array;
0019
0020 import org.apache.spark.unsafe.Platform;
0021 import org.apache.spark.unsafe.memory.MemoryBlock;
0022
0023
0024
0025
0026
0027
0028
0029
0030 public final class LongArray {
0031
0032
0033 private static final long WIDTH = 8;
0034
0035 private final MemoryBlock memory;
0036 private final Object baseObj;
0037 private final long baseOffset;
0038
0039 private final long length;
0040
0041 public LongArray(MemoryBlock memory) {
0042 assert memory.size() < (long) Integer.MAX_VALUE * 8: "Array size >= Integer.MAX_VALUE elements";
0043 this.memory = memory;
0044 this.baseObj = memory.getBaseObject();
0045 this.baseOffset = memory.getBaseOffset();
0046 this.length = memory.size() / WIDTH;
0047 }
0048
0049 public MemoryBlock memoryBlock() {
0050 return memory;
0051 }
0052
0053 public Object getBaseObject() {
0054 return baseObj;
0055 }
0056
0057 public long getBaseOffset() {
0058 return baseOffset;
0059 }
0060
0061
0062
0063
0064 public long size() {
0065 return length;
0066 }
0067
0068
0069
0070
0071 public void zeroOut() {
0072 for (long off = baseOffset; off < baseOffset + length * WIDTH; off += WIDTH) {
0073 Platform.putLong(baseObj, off, 0);
0074 }
0075 }
0076
0077
0078
0079
0080 public void set(int index, long value) {
0081 assert index >= 0 : "index (" + index + ") should >= 0";
0082 assert index < length : "index (" + index + ") should < length (" + length + ")";
0083 Platform.putLong(baseObj, baseOffset + index * WIDTH, value);
0084 }
0085
0086
0087
0088
0089 public long get(int index) {
0090 assert index >= 0 : "index (" + index + ") should >= 0";
0091 assert index < length : "index (" + index + ") should < length (" + length + ")";
0092 return Platform.getLong(baseObj, baseOffset + index * WIDTH);
0093 }
0094 }