-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
ByteBufferKeyValuePartitionReader.java
139 lines (120 loc) · 3.86 KB
/
ByteBufferKeyValuePartitionReader.java
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
/*
* The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
* (the “License”). You may not use this work except in compliance with the License, which is
* available at www.apache.org/licenses/LICENSE-2.0
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied, as more fully set forth in the License.
*
* See the NOTICE file distributed with this work for information regarding copyright ownership.
*/
package alluxio.client.keyvalue;
import alluxio.Constants;
import alluxio.exception.AlluxioException;
import alluxio.util.io.BufferUtils;
import alluxio.util.io.ByteIOUtils;
import com.google.common.base.Preconditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Iterator;
import java.util.NoSuchElementException;
import javax.annotation.concurrent.NotThreadSafe;
/**
* Reader that implements {@link KeyValuePartitionReader} to access a key-value file using random
* access API.
*/
@NotThreadSafe
public final class ByteBufferKeyValuePartitionReader implements KeyValuePartitionReader {
private static final Logger LOG = LoggerFactory.getLogger(Constants.LOGGER_TYPE);
private Index mIndex;
private PayloadReader mPayloadReader;
private ByteBuffer mBuf;
private int mBufferLength;
/** Whether this writer is closed. */
private boolean mClosed;
/**
* Constructs {@link ByteBufferKeyValuePartitionReader}.
*
* @param fileBytes the byte buffer as underline storage to read from
*/
public ByteBufferKeyValuePartitionReader(ByteBuffer fileBytes) {
mBuf = Preconditions.checkNotNull(fileBytes);
mBufferLength = mBuf.remaining();
mIndex = createIndex();
mPayloadReader = createPayloadReader();
mClosed = false;
}
private Index createIndex() {
int indexOffset = ByteIOUtils.readInt(mBuf, mBufferLength - 4);
ByteBuffer indexBytes =
BufferUtils.sliceByteBuffer(mBuf, indexOffset, mBufferLength - 4 - indexOffset);
return LinearProbingIndex.loadFromByteArray(indexBytes);
}
private PayloadReader createPayloadReader() {
return new BasePayloadReader(mBuf);
}
/**
* {@inheritDoc}
* <p>
* This could be slow when value size is large, use this cautiously or {@link #get(ByteBuffer)}
* which may avoid copying data.
*/
@Override
public byte[] get(byte[] key) throws IOException {
ByteBuffer valueBuffer = get(ByteBuffer.wrap(key));
if (valueBuffer == null) {
return null;
}
return BufferUtils.newByteArrayFromByteBuffer(valueBuffer);
}
@Override
public ByteBuffer get(ByteBuffer key) throws IOException {
Preconditions.checkState(!mClosed);
LOG.trace("get: key");
return mIndex.get(key, mPayloadReader);
}
@Override
public void close() {
if (mClosed) {
return;
}
mClosed = true;
}
@Override
public KeyValueIterator iterator() {
return new KeyValueIterator() {
private Iterator<ByteBuffer> mKeyIterator = mIndex.keyIterator(mPayloadReader);
@Override
public boolean hasNext() {
return mKeyIterator.hasNext();
}
@Override
public KeyValuePair next() throws IOException, AlluxioException {
if (!hasNext()) {
throw new NoSuchElementException();
}
ByteBuffer key = mKeyIterator.next();
ByteBuffer value = get(key);
return new KeyValuePair(key, value);
}
};
}
@Override
public int size() throws IOException, AlluxioException {
return mIndex.keyCount();
}
/**
* @return the {@link Index} reconstructed from the byte buffer
*/
public Index getIndex() {
return mIndex;
}
/**
* @return the {@link PayloadReader} for reading payloads from the byte buffer
*/
public PayloadReader getPayloadReader() {
return mPayloadReader;
}
}