Search in sources :

Example 1 with Type

use of com.cinchapi.concourse.thrift.Type in project concourse by cinchapi.

the class Value method fromByteBuffer.

/**
 * Return the Value encoded in {@code bytes} so long as those bytes adhere
 * to the format specified by the {@link #getBytes()} method.
 *
 * @param bytes
 * @return the Value
 */
public static Value fromByteBuffer(ByteBuffer bytes) {
    Type type = Type.values()[bytes.get()];
    TObject data = createTObject(bytes, type);
    return new Value(data);
}
Also used : Type(com.cinchapi.concourse.thrift.Type) TObject(com.cinchapi.concourse.thrift.TObject)

Example 2 with Type

use of com.cinchapi.concourse.thrift.Type in project concourse by cinchapi.

the class Value method equals.

@Override
public boolean equals(Object obj) {
    if (obj instanceof Value) {
        final Value other = (Value) obj;
        Type typeA = getType();
        Type typeB = other.getType();
        if (typeA != typeB && (isNumericType(typeA) && isNumericType(typeB))) {
            return Numbers.areEqual((Number) getObject(), (Number) other.getObject());
        } else {
            return data.equals(other.data);
        }
    }
    return false;
}
Also used : Type(com.cinchapi.concourse.thrift.Type)

Example 3 with Type

use of com.cinchapi.concourse.thrift.Type in project concourse by cinchapi.

the class Convert method javaToThrift.

/**
 * Return the Thrift Object that represents {@code object}.
 *
 * @param object
 * @return the TObject
 */
public static TObject javaToThrift(Object object) {
    if (object == null) {
        return TObject.NULL;
    } else {
        ByteBuffer bytes;
        Type type = null;
        if (object instanceof Boolean) {
            bytes = ByteBuffer.allocate(1);
            bytes.put((boolean) object ? (byte) 1 : (byte) 0);
            type = Type.BOOLEAN;
        } else if (object instanceof Double) {
            bytes = ByteBuffer.allocate(8);
            bytes.putDouble((double) object);
            type = Type.DOUBLE;
        } else if (object instanceof Float) {
            bytes = ByteBuffer.allocate(4);
            bytes.putFloat((float) object);
            type = Type.FLOAT;
        } else if (object instanceof Link) {
            bytes = ByteBuffer.allocate(8);
            bytes.putLong(((Link) object).longValue());
            type = Type.LINK;
        } else if (object instanceof Long) {
            bytes = ByteBuffer.allocate(8);
            bytes.putLong((long) object);
            type = Type.LONG;
        } else if (object instanceof Integer) {
            bytes = ByteBuffer.allocate(4);
            bytes.putInt((int) object);
            type = Type.INTEGER;
        } else if (object instanceof BigDecimal) {
            bytes = ByteBuffer.allocate(8);
            bytes.putDouble((double) ((BigDecimal) object).doubleValue());
            type = Type.DOUBLE;
        } else if (object instanceof Tag) {
            bytes = ByteBuffer.wrap(object.toString().getBytes(StandardCharsets.UTF_8));
            type = Type.TAG;
        } else if (object instanceof Timestamp) {
            try {
                bytes = ByteBuffer.allocate(8);
                bytes.putLong(((Timestamp) object).getMicros());
                type = Type.TIMESTAMP;
            } catch (IllegalStateException e) {
                throw new UnsupportedOperationException("Cannot convert string based Timestamp to a TObject");
            }
        } else if (object instanceof Function) {
            type = Type.FUNCTION;
            Function function = (Function) object;
            byte[] nameBytes = function.operation().getBytes(StandardCharsets.UTF_8);
            byte[] keyBytes = function.key().getBytes(StandardCharsets.UTF_8);
            if (function instanceof IndexFunction) {
                /*
                     * Schema:
                     * | type (1) | timestamp(8) | nameLength (4) | name
                     * (nameLength) | key |
                     */
                bytes = ByteBuffer.allocate(1 + 8 + 4 + nameBytes.length + keyBytes.length);
                bytes.put((byte) FunctionType.INDEX.ordinal());
                bytes.putLong(((TemporalFunction) function).timestamp());
                bytes.putInt(nameBytes.length);
                bytes.put(nameBytes);
                bytes.put(keyBytes);
            } else if (function instanceof KeyRecordsFunction) {
                /*
                     * Schema:
                     * | type (1) | timestamp(8) | nameLength (4) | name
                     * (nameLength) | keyLength (4) | key (keyLength) | records
                     * (8 each) |
                     */
                KeyRecordsFunction func = (KeyRecordsFunction) function;
                bytes = ByteBuffer.allocate(1 + 8 + 4 + nameBytes.length + 4 + keyBytes.length + 8 * func.source().size());
                bytes.put((byte) FunctionType.KEY_RECORDS.ordinal());
                bytes.putLong(((TemporalFunction) function).timestamp());
                bytes.putInt(nameBytes.length);
                bytes.put(nameBytes);
                bytes.putInt(keyBytes.length);
                bytes.put(keyBytes);
                for (long record : func.source()) {
                    bytes.putLong(record);
                }
            } else if (function instanceof KeyConditionFunction) {
                /*
                     * Schema:
                     * | type (1) | timestamp(8) | nameLength (4) | name
                     * (nameLength) | keyLength (4) | key (keyLength) |
                     * condition |
                     */
                KeyConditionFunction func = (KeyConditionFunction) function;
                String condition = ConcourseCompiler.get().tokenize(func.source()).stream().map(Symbol::toString).collect(Collectors.joining(" "));
                bytes = ByteBuffer.allocate(1 + 9 + 4 + nameBytes.length + 4 + keyBytes.length + condition.length());
                bytes.put((byte) FunctionType.KEY_CONDITION.ordinal());
                bytes.putLong(((TemporalFunction) function).timestamp());
                bytes.putInt(nameBytes.length);
                bytes.put(nameBytes);
                bytes.putInt(keyBytes.length);
                bytes.put(keyBytes);
                bytes.put(condition.getBytes(StandardCharsets.UTF_8));
            } else {
                throw new UnsupportedOperationException("Cannot convert the following function to a TObject: " + function);
            }
        } else {
            bytes = ByteBuffer.wrap(object.toString().getBytes(StandardCharsets.UTF_8));
            type = Type.STRING;
        }
        bytes.rewind();
        return new TObject(bytes, type).setJavaFormat(object);
    }
}
Also used : TObject(com.cinchapi.concourse.thrift.TObject) KeyRecordsFunction(com.cinchapi.ccl.type.function.KeyRecordsFunction) FunctionValueSymbol(com.cinchapi.ccl.grammar.FunctionValueSymbol) Symbol(com.cinchapi.ccl.grammar.Symbol) ByteBuffer(java.nio.ByteBuffer) Timestamp(com.cinchapi.concourse.Timestamp) BigDecimal(java.math.BigDecimal) TemporalFunction(com.cinchapi.ccl.type.function.TemporalFunction) IndexFunction(com.cinchapi.ccl.type.function.IndexFunction) TemporalFunction(com.cinchapi.ccl.type.function.TemporalFunction) KeyRecordsFunction(com.cinchapi.ccl.type.function.KeyRecordsFunction) Function(com.cinchapi.ccl.type.Function) KeyConditionFunction(com.cinchapi.ccl.type.function.KeyConditionFunction) Type(com.cinchapi.concourse.thrift.Type) IndexFunction(com.cinchapi.ccl.type.function.IndexFunction) Tag(com.cinchapi.concourse.Tag) KeyConditionFunction(com.cinchapi.ccl.type.function.KeyConditionFunction) Link(com.cinchapi.concourse.Link)

Example 4 with Type

use of com.cinchapi.concourse.thrift.Type in project concourse by cinchapi.

the class ObjectResultDataset method deserializeValue.

@Override
protected Object deserializeValue(Buffer buffer) {
    Type type = Type.values()[buffer.readByte()];
    int length = buffer.readInt();
    byte[] data = new byte[length];
    buffer.read(data);
    TObject value = new TObject(ByteBuffer.wrap(data), type);
    return Convert.thriftToJava(value);
}
Also used : Type(com.cinchapi.concourse.thrift.Type) TObject(com.cinchapi.concourse.thrift.TObject)

Example 5 with Type

use of com.cinchapi.concourse.thrift.Type in project concourse by cinchapi.

the class PluginSerializer method deserialize.

/**
 * Return the object represented by the serialized data in the
 * {@link ByteBuffer}.
 *
 * @param bytes the bytes from which the object is deserialized
 * @return the deserialized object
 */
@SuppressWarnings("unchecked")
public <T> T deserialize(ByteBuffer bytes) {
    Scheme scheme = Scheme.values()[bytes.get()];
    if (scheme == Scheme.PLUGIN_SERIALIZABLE) {
        Buffer buffer = HeapBuffer.wrap(ByteBuffers.getByteArray(bytes));
        Class<T> clazz = Reflection.getClassCasted(buffer.readUTF8());
        T instance = Reflection.newInstance(clazz);
        ((PluginSerializable) instance).deserialize(buffer);
        return instance;
    } else if (scheme == Scheme.REMOTE_MESSAGE) {
        Buffer buffer = HeapBuffer.wrap(ByteBuffers.getByteArray(bytes));
        T instance = (T) RemoteMessage.fromBuffer(buffer);
        return instance;
    } else if (scheme == Scheme.COMPLEX_TOBJECT) {
        ByteBuffer bytes0 = ByteBuffers.slice(bytes, 1, bytes.remaining());
        return (T) ComplexTObject.fromByteBuffer(bytes0);
    } else if (scheme == Scheme.TOBJECT) {
        ByteBuffer bytes0 = ByteBuffers.slice(bytes, 1, bytes.remaining());
        Type type = Type.values()[bytes0.get()];
        return (T) new TObject(ByteBuffers.slice(bytes0, bytes0.remaining()), type);
    } else if (scheme == Scheme.JAVA_SERIALIZABLE) {
        int classLength = bytes.getShort();
        byte[] className = new byte[classLength];
        bytes.get(className);
        Class<T> clazz = Reflection.getClassCasted(new String(className, StandardCharsets.UTF_8));
        bytes = ByteBuffers.get(bytes, bytes.remaining());
        Serializable instance = Serializables.read(bytes, (Class<? extends Serializable>) clazz);
        return (T) instance;
    } else {
        // blob from a blob that is really just a PluginSerializable object
        throw new IllegalStateException("Cannot plugin deserialize the provided byte stream");
    }
}
Also used : HeapBuffer(io.atomix.catalyst.buffer.HeapBuffer) Buffer(io.atomix.catalyst.buffer.Buffer) ByteBuffer(java.nio.ByteBuffer) ComplexTObject(com.cinchapi.concourse.thrift.ComplexTObject) TObject(com.cinchapi.concourse.thrift.TObject) Serializable(java.io.Serializable) ByteBuffer(java.nio.ByteBuffer) Type(com.cinchapi.concourse.thrift.Type)

Aggregations

Type (com.cinchapi.concourse.thrift.Type)6 TObject (com.cinchapi.concourse.thrift.TObject)5 ByteBuffer (java.nio.ByteBuffer)2 FunctionValueSymbol (com.cinchapi.ccl.grammar.FunctionValueSymbol)1 Symbol (com.cinchapi.ccl.grammar.Symbol)1 Function (com.cinchapi.ccl.type.Function)1 IndexFunction (com.cinchapi.ccl.type.function.IndexFunction)1 KeyConditionFunction (com.cinchapi.ccl.type.function.KeyConditionFunction)1 KeyRecordsFunction (com.cinchapi.ccl.type.function.KeyRecordsFunction)1 TemporalFunction (com.cinchapi.ccl.type.function.TemporalFunction)1 Link (com.cinchapi.concourse.Link)1 Tag (com.cinchapi.concourse.Tag)1 Timestamp (com.cinchapi.concourse.Timestamp)1 ComplexTObject (com.cinchapi.concourse.thrift.ComplexTObject)1 Buffer (io.atomix.catalyst.buffer.Buffer)1 HeapBuffer (io.atomix.catalyst.buffer.HeapBuffer)1 Serializable (java.io.Serializable)1 BigDecimal (java.math.BigDecimal)1