Example usage for org.apache.cassandra.dht IPartitioner getTokenFactory

List of usage examples for org.apache.cassandra.dht IPartitioner getTokenFactory

Introduction

In this page you can find the example usage for org.apache.cassandra.dht IPartitioner getTokenFactory.

Prototype

public Token.TokenFactory getTokenFactory();

Source Link

Usage

From source file:com.stratio.deep.cassandra.cql.RangeUtils.java

License:Apache License

/**
 * Recursive function that splits a given token range to a given number of token ranges.
 *
 * @param range        the token range to be splitted.
 * @param partitioner  the cassandra partitioner.
 * @param bisectFactor the actual number of pieces the original token range will be splitted to.
 * @param accumulator  a token range accumulator (ne
 *///from ww w . j av  a2  s.  co m
private static void bisectTokeRange(DeepTokenRange range, final IPartitioner partitioner,
        final int bisectFactor, final List<DeepTokenRange> accumulator) {

    final AbstractType tkValidator = partitioner.getTokenValidator();

    Token leftToken = partitioner.getTokenFactory().fromByteArray(tkValidator.decompose(range.getStartToken()));
    Token rightToken = partitioner.getTokenFactory().fromByteArray(tkValidator.decompose(range.getEndToken()));
    Token midToken = partitioner.midpoint(leftToken, rightToken);

    Comparable midpoint = (Comparable) tkValidator.compose(tkValidator.fromString(midToken.toString()));

    DeepTokenRange left = new DeepTokenRange(range.getStartToken(), midpoint, range.getReplicas());
    DeepTokenRange right = new DeepTokenRange(midpoint, range.getEndToken(), range.getReplicas());

    if (bisectFactor / 2 <= 1) {
        accumulator.add(left);
        accumulator.add(right);
    } else {
        bisectTokeRange(left, partitioner, bisectFactor / 2, accumulator);
        bisectTokeRange(right, partitioner, bisectFactor / 2, accumulator);
    }
}

From source file:com.stratio.deep.cassandra.thrift.ThriftRangeUtils.java

License:Apache License

/**
 * Builds a new {@link ThriftRangeUtils}.
 *
 * @param partitioner  the partitioner.//www  .  ja v a 2s .  c  o m
 * @param host         the host address.
 * @param rpcPort      the host RPC port.
 * @param keyspace     the keyspace name.
 * @param columnFamily the column family name.
 * @param splitSize    the number of rows per split.
 */
public ThriftRangeUtils(IPartitioner partitioner, String host, int rpcPort, String keyspace,
        String columnFamily, int splitSize) {
    this.host = host;
    this.rpcPort = rpcPort;
    this.splitSize = splitSize;
    this.keyspace = keyspace;
    this.columnFamily = columnFamily;
    tokenType = partitioner.getTokenValidator();
    tokenFactory = partitioner.getTokenFactory();
    minToken = (Comparable) partitioner.getMinimumToken().token;
}

From source file:kina.cql.RangeUtils.java

License:Apache License

/**
 * Recursive function that splits a given token range to a given number of tolen ranges.
 *
 * @param range the token range to be splitted.
 * @param partitioner the cassandra partitioner.
 * @param bisectFactor the actual number of pieces the original token range will be splitted to.
 * @param accumulator a token range accumulator (ne
 *//*  w  ww  .j  av a 2 s . co m*/
private static void bisectTokeRange(Range range, final IPartitioner partitioner, final int bisectFactor,
        final List<Range> accumulator) {

    final AbstractType tkValidator = partitioner.getTokenValidator();

    Token leftToken = partitioner.getTokenFactory().fromByteArray(tkValidator.decompose(range.getStartToken()));
    Token rightToken = partitioner.getTokenFactory().fromByteArray(tkValidator.decompose(range.getEndToken()));
    Token midToken = partitioner.midpoint(leftToken, rightToken);

    Comparable midpoint = (Comparable) tkValidator.compose(tkValidator.fromString(midToken.toString()));

    Range left = new Range(range.getStartToken(), midpoint, range.getReplicas());
    Range right = new Range(midpoint, range.getEndToken(), range.getReplicas());

    if (bisectFactor / 2 <= 1) {
        accumulator.add(left);
        accumulator.add(right);
    } else {
        bisectTokeRange(left, partitioner, bisectFactor / 2, accumulator);
        bisectTokeRange(right, partitioner, bisectFactor / 2, accumulator);
    }
}

From source file:org.elasticsearch.common.io.stream.StreamOutput.java

License:Apache License

public void writeGenericValue(@Nullable Object value) throws IOException {
    if (value == null) {
        writeByte((byte) -1);
        return;/*from www  .ja  v  a  2  s.  co  m*/
    }
    Class type = value.getClass();
    if (type == String.class) {
        writeByte((byte) 0);
        writeString((String) value);
    } else if (type == Integer.class) {
        writeByte((byte) 1);
        writeInt((Integer) value);
    } else if (type == Long.class) {
        writeByte((byte) 2);
        writeLong((Long) value);
    } else if (type == Float.class) {
        writeByte((byte) 3);
        writeFloat((Float) value);
    } else if (type == Double.class) {
        writeByte((byte) 4);
        writeDouble((Double) value);
    } else if (type == Boolean.class) {
        writeByte((byte) 5);
        writeBoolean((Boolean) value);
    } else if (type == byte[].class) {
        writeByte((byte) 6);
        writeVInt(((byte[]) value).length);
        writeBytes(((byte[]) value));
    } else if (value instanceof List) {
        writeByte((byte) 7);
        List list = (List) value;
        writeVInt(list.size());
        for (Object o : list) {
            writeGenericValue(o);
        }
    } else if (value instanceof Object[]) {
        writeByte((byte) 8);
        Object[] list = (Object[]) value;
        writeVInt(list.length);
        for (Object o : list) {
            writeGenericValue(o);
        }
    } else if (value instanceof Map) {
        if (value instanceof LinkedHashMap) {
            writeByte((byte) 9);
        } else {
            writeByte((byte) 10);
        }
        @SuppressWarnings("unchecked")
        Map<String, Object> map = (Map<String, Object>) value;
        writeVInt(map.size());
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            writeString(entry.getKey());
            writeGenericValue(entry.getValue());
        }
    } else if (type == Byte.class) {
        writeByte((byte) 11);
        writeByte((Byte) value);
    } else if (type == Date.class) {
        writeByte((byte) 12);
        writeLong(((Date) value).getTime());
    } else if (value instanceof ReadableInstant) {
        writeByte((byte) 13);
        writeString(((ReadableInstant) value).getZone().getID());
        writeLong(((ReadableInstant) value).getMillis());
    } else if (value instanceof BytesReference) {
        writeByte((byte) 14);
        writeBytesReference((BytesReference) value);
    } else if (value instanceof Text) {
        writeByte((byte) 15);
        writeText((Text) value);
    } else if (type == Short.class) {
        writeByte((byte) 16);
        writeShort((Short) value);
    } else if (type == int[].class) {
        writeByte((byte) 17);
        writeIntArray((int[]) value);
    } else if (type == long[].class) {
        writeByte((byte) 18);
        writeLongArray((long[]) value);
    } else if (type == float[].class) {
        writeByte((byte) 19);
        writeFloatArray((float[]) value);
    } else if (type == double[].class) {
        writeByte((byte) 20);
        writeDoubleArray((double[]) value);
    } else if (value instanceof BytesRef) {
        writeByte((byte) 21);
        writeBytesRef((BytesRef) value);
    } else if (type == GeoPoint.class) {
        writeByte((byte) 22);
        writeGeoPoint((GeoPoint) value);
    } else if (value instanceof Token) {
        writeByte((byte) 64);
        IPartitioner p = StorageService.instance.getPartitioner();
        ByteBuffer b = p.getTokenFactory().toByteArray((Token) value);
        writeVInt(b.array().length);
        writeBytes(b.array());
    } else {
        throw new IOException("Can't write type [" + type + "]");
    }
}

From source file:org.janusgraph.diskstorage.cassandra.thrift.CassandraThriftStoreManager.java

License:Apache License

@Override
public List<KeyRange> getLocalKeyPartition() throws BackendException {
    CTConnection conn = null;//w  w  w .ja va2  s. c  o m
    IPartitioner partitioner = getCassandraPartitioner();

    if (!(partitioner instanceof AbstractByteOrderedPartitioner))
        throw new UnsupportedOperationException(
                "getLocalKeyPartition() only supported by byte ordered partitioner.");

    Token.TokenFactory tokenFactory = partitioner.getTokenFactory();

    try {
        // Resist the temptation to describe SYSTEM_KS.  It has no ring.
        // Instead, we'll create our own keyspace (or check that it exists), then describe it.
        ensureKeyspaceExists(keySpaceName);

        conn = pool.borrowObject(keySpaceName);
        List<TokenRange> ranges = conn.getClient().describe_ring(keySpaceName);
        List<KeyRange> keyRanges = new ArrayList<KeyRange>(ranges.size());

        for (TokenRange range : ranges) {
            if (!NetworkUtil.hasLocalAddress(range.endpoints))
                continue;

            keyRanges.add(CassandraHelper.transformRange(tokenFactory.fromString(range.start_token),
                    tokenFactory.fromString(range.end_token)));
        }

        return keyRanges;
    } catch (Exception e) {
        throw CassandraThriftKeyColumnValueStore.convertException(e);
    } finally {
        pool.returnObjectUnsafe(keySpaceName, conn);
    }
}