Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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 com.facebook.presto.redis;
import com.facebook.airlift.log.Logger;
import com.facebook.presto.common.type.Type;
import com.facebook.presto.decoder.DecoderColumnHandle;
import com.facebook.presto.decoder.FieldValueProvider;
import com.facebook.presto.decoder.RowDecoder;
import com.facebook.presto.spi.ColumnHandle;
import com.facebook.presto.spi.RecordCursor;
import io.airlift.slice.Slice;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.ScanResult;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import static com.facebook.presto.decoder.FieldValueProviders.booleanValueProvider;
import static com.facebook.presto.decoder.FieldValueProviders.bytesValueProvider;
import static com.facebook.presto.decoder.FieldValueProviders.longValueProvider;
import static com.google.common.base.Preconditions.checkArgument;
import static java.lang.String.format;
import static redis.clients.jedis.ScanParams.SCAN_POINTER_START;
public class RedisRecordCursor
implements RecordCursor
{
private static final Logger log = Logger.get(RedisRecordCursor.class);
private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
private final RowDecoder keyDecoder;
private final RowDecoder valueDecoder;
private final RedisSplit split;
private final List columnHandles;
private final RedisJedisManager redisJedisManager;
private final JedisPool jedisPool;
private final ScanParams scanParams;
private ScanResult redisCursor;
private Iterator keysIterator;
private final AtomicBoolean reported = new AtomicBoolean();
private FieldValueProvider[] fieldValueProviders;
private String valueString;
private Map valueMap;
private long totalBytes;
private long totalValues;
private final FieldValueProvider[] currentRowValues;
RedisRecordCursor(
RowDecoder keyDecoder,
RowDecoder valueDecoder,
RedisSplit split,
List columnHandles,
RedisJedisManager redisJedisManager)
{
this.keyDecoder = keyDecoder;
this.valueDecoder = valueDecoder;
this.split = split;
this.columnHandles = columnHandles;
this.redisJedisManager = redisJedisManager;
this.jedisPool = redisJedisManager.getJedisPool(split.getNodes().get(0));
this.scanParams = setScanParams();
this.currentRowValues = new FieldValueProvider[columnHandles.size()];
fetchKeys();
}
@Override
public long getCompletedBytes()
{
return totalBytes;
}
@Override
public long getReadTimeNanos()
{
return 0;
}
@Override
public Type getType(int field)
{
checkArgument(field < columnHandles.size(), "Invalid field index");
return columnHandles.get(field).getType();
}
public boolean hasUnscannedData()
{
if (redisCursor == null) {
return false;
}
// no more keys are unscanned when
// when redis scan command
// returns 0 string cursor
return (!redisCursor.getStringCursor().equals("0"));
}
@Override
public boolean advanceNextPosition()
{
while (!keysIterator.hasNext()) {
if (!hasUnscannedData()) {
return endOfData();
}
fetchKeys();
}
return nextRow(keysIterator.next());
}
private boolean endOfData()
{
if (!reported.getAndSet(true)) {
log.debug("Read a total of %d values with %d bytes.", totalValues, totalBytes);
}
return false;
}
private boolean nextRow(String keyString)
{
fetchData(keyString);
byte[] keyData = keyString.getBytes(StandardCharsets.UTF_8);
byte[] valueData = EMPTY_BYTE_ARRAY;
if (valueString != null) {
valueData = valueString.getBytes(StandardCharsets.UTF_8);
}
totalBytes += valueData.length;
totalValues++;
Optional