Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,17 @@ default VALUE getReadCopy(KEY key) throws IOException {
TableIterator<KEY, ? extends KeyValue<KEY, VALUE>> iterator(KEY prefix)
throws IOException;

/**
* Returns a prefixed iterator for this metadata store with customized value codec.
* @param customValueCodec customized codec for value
* @param prefix key prefix
* @return MetaStoreIterator
*/
default TableIterator<KEY, ? extends KeyValue<KEY, VALUE>> iterator(Codec<VALUE> customValueCodec, KEY prefix)
throws IOException {
return iterator(prefix);
}

/**
* Returns the Name of this Table.
* @return - Table Name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,11 @@ private KEY decodeKey(byte[] key) throws IOException {
}

private VALUE decodeValue(byte[] value) throws IOException {
return value == null ? null : valueCodec.fromPersistedFormat(value);
return decodeValue(value, valueCodec);
}

private VALUE decodeValue(byte[] value, Codec<VALUE> customValueCodec) throws IOException {
return value == null ? null : customValueCodec.fromPersistedFormat(value);
}

@Override
Expand Down Expand Up @@ -420,10 +424,15 @@ public Table.KeyValueIterator<KEY, VALUE> iterator() throws IOException {
@Override
public Table.KeyValueIterator<KEY, VALUE> iterator(KEY prefix)
throws IOException {
return iterator(valueCodec, prefix);
}

public Table.KeyValueIterator<KEY, VALUE> iterator(Codec<VALUE> customValueCodec, KEY prefix) throws IOException {
Codec<VALUE> finalValueCodec = customValueCodec == null ? valueCodec : customValueCodec;
if (supportCodecBuffer) {
final CodecBuffer prefixBuffer = encodeKeyCodecBuffer(prefix);
try {
return newCodecBufferTableIterator(rawTable.iterator(prefixBuffer));
return newCodecBufferTableIterator(rawTable.iterator(prefixBuffer), finalValueCodec);
} catch (Throwable t) {
if (prefixBuffer != null) {
prefixBuffer.release();
Expand All @@ -432,7 +441,7 @@ public Table.KeyValueIterator<KEY, VALUE> iterator(KEY prefix)
}
} else {
final byte[] prefixBytes = encodeKey(prefix);
return new TypedTableIterator(rawTable.iterator(prefixBytes));
return new TypedTableIterator(rawTable.iterator(prefixBytes), finalValueCodec);
}
}

Expand Down Expand Up @@ -558,9 +567,15 @@ TableCache<KEY, VALUE> getCache() {
public final class TypedKeyValue implements KeyValue<KEY, VALUE> {

private final KeyValue<byte[], byte[]> rawKeyValue;
private final Codec<VALUE> customValueCodec;

private TypedKeyValue(KeyValue<byte[], byte[]> rawKeyValue) {
this(rawKeyValue, valueCodec);
}

private TypedKeyValue(KeyValue<byte[], byte[]> rawKeyValue, Codec<VALUE> customValueCodec) {
this.rawKeyValue = rawKeyValue;
this.customValueCodec = customValueCodec;
}

@Override
Expand All @@ -570,7 +585,7 @@ public KEY getKey() throws IOException {

@Override
public VALUE getValue() throws IOException {
return decodeValue(rawKeyValue.getValue());
return decodeValue(rawKeyValue.getValue(), customValueCodec);
}

public byte[] getRawKey() throws IOException {
Expand All @@ -583,7 +598,7 @@ public byte[] getRawValue() throws IOException {
}

RawIterator<CodecBuffer> newCodecBufferTableIterator(
TableIterator<CodecBuffer, KeyValue<CodecBuffer, CodecBuffer>> i) {
TableIterator<CodecBuffer, KeyValue<CodecBuffer, CodecBuffer>> i, Codec<VALUE> customValueCodec) {
return new RawIterator<CodecBuffer>(i) {
@Override
AutoCloseSupplier<CodecBuffer> convert(KEY key) throws IOException {
Expand All @@ -606,7 +621,7 @@ KeyValue<KEY, VALUE> convert(KeyValue<CodecBuffer, CodecBuffer> raw)
throws IOException {
final int rawSize = raw.getValue().readableBytes();
final KEY key = keyCodec.fromCodecBuffer(raw.getKey());
final VALUE value = valueCodec.fromCodecBuffer(raw.getValue());
final VALUE value = customValueCodec.fromCodecBuffer(raw.getValue());
return Table.newKeyValue(key, value, rawSize);
}
};
Expand All @@ -616,9 +631,10 @@ KeyValue<KEY, VALUE> convert(KeyValue<CodecBuffer, CodecBuffer> raw)
* Table Iterator implementation for strongly typed tables.
*/
public class TypedTableIterator extends RawIterator<byte[]> {
TypedTableIterator(
TableIterator<byte[], KeyValue<byte[], byte[]>> rawIterator) {
private final Codec<VALUE> customValueCodec;
TypedTableIterator(TableIterator<byte[], KeyValue<byte[], byte[]>> rawIterator, Codec<VALUE> customValueCodec) {
super(rawIterator);
this.customValueCodec = customValueCodec;
}

@Override
Expand All @@ -629,7 +645,7 @@ AutoCloseSupplier<byte[]> convert(KEY key) throws IOException {

@Override
KeyValue<KEY, VALUE> convert(KeyValue<byte[], byte[]> raw) {
return new TypedKeyValue(raw);
return new TypedKeyValue(raw, customValueCodec);
}
}

Expand Down