Search in sources :

Example 6 with BinaryHeapInputStream

use of org.apache.ignite.internal.binary.streams.BinaryHeapInputStream in project ignite by apache.

the class JdbcThinTcpIo method sendRequest.

/**
     * @param req Request.
     * @param cap Initial ouput stream capacity.
     * @return Server response.
     * @throws IOException On IO error.
     * @throws IgniteCheckedException On error.
     */
@SuppressWarnings("unchecked")
public <R extends JdbcResult> R sendRequest(JdbcRequest req, int cap) throws IOException, IgniteCheckedException {
    BinaryWriterExImpl writer = new BinaryWriterExImpl(null, new BinaryHeapOutputStream(cap), null, null);
    req.writeBinary(writer);
    send(writer.array());
    BinaryReaderExImpl reader = new BinaryReaderExImpl(null, new BinaryHeapInputStream(read()), null, null, false);
    JdbcResponse res = new JdbcResponse();
    res.readBinary(reader);
    if (res.status() != SqlListenerResponse.STATUS_SUCCESS)
        throw new IgniteCheckedException("Error server response: [req=" + req + ", resp=" + res + ']');
    return (R) res.response();
}
Also used : BinaryReaderExImpl(org.apache.ignite.internal.binary.BinaryReaderExImpl) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) BinaryHeapOutputStream(org.apache.ignite.internal.binary.streams.BinaryHeapOutputStream) BinaryHeapInputStream(org.apache.ignite.internal.binary.streams.BinaryHeapInputStream) JdbcResponse(org.apache.ignite.internal.processors.odbc.jdbc.JdbcResponse) BinaryWriterExImpl(org.apache.ignite.internal.binary.BinaryWriterExImpl)

Example 7 with BinaryHeapInputStream

use of org.apache.ignite.internal.binary.streams.BinaryHeapInputStream in project ignite by apache.

the class JdbcThinTcpIo method sendRequest.

/**
 * @param req Request.
 * @return Server response.
 * @throws IOException In case of IO error.
 */
@SuppressWarnings("unchecked")
JdbcResponse sendRequest(JdbcRequest req) throws IOException {
    int cap = guessCapacity(req);
    BinaryWriterExImpl writer = new BinaryWriterExImpl(null, new BinaryHeapOutputStream(cap), null, null);
    req.writeBinary(writer);
    send(writer.array());
    BinaryReaderExImpl reader = new BinaryReaderExImpl(null, new BinaryHeapInputStream(read()), null, null, false);
    JdbcResponse res = new JdbcResponse();
    res.readBinary(reader);
    return res;
}
Also used : BinaryReaderExImpl(org.apache.ignite.internal.binary.BinaryReaderExImpl) BinaryHeapOutputStream(org.apache.ignite.internal.binary.streams.BinaryHeapOutputStream) BinaryHeapInputStream(org.apache.ignite.internal.binary.streams.BinaryHeapInputStream) JdbcResponse(org.apache.ignite.internal.processors.odbc.jdbc.JdbcResponse) BinaryWriterExImpl(org.apache.ignite.internal.binary.BinaryWriterExImpl) IpcClientTcpEndpoint(org.apache.ignite.internal.util.ipc.loopback.IpcClientTcpEndpoint)

Example 8 with BinaryHeapInputStream

use of org.apache.ignite.internal.binary.streams.BinaryHeapInputStream in project ignite by apache.

the class OdbcMessageParser method decode.

/**
 * {@inheritDoc}
 */
@Override
public ClientListenerRequest decode(byte[] msg) {
    assert msg != null;
    BinaryInputStream stream = new BinaryHeapInputStream(msg);
    BinaryReaderExImpl reader = new BinaryReaderExImpl(marsh.context(), stream, ctx.config().getClassLoader(), true);
    byte cmd = reader.readByte();
    ClientListenerRequest res;
    switch(cmd) {
        case OdbcRequest.QRY_EXEC:
            {
                String schema = reader.readString();
                String sql = reader.readString();
                int paramNum = reader.readInt();
                Object[] params = readParameterRow(reader, paramNum);
                int timeout = 0;
                if (ver.compareTo(OdbcConnectionContext.VER_2_3_2) >= 0)
                    timeout = reader.readInt();
                res = new OdbcQueryExecuteRequest(schema, sql, params, timeout);
                break;
            }
        case OdbcRequest.QRY_EXEC_BATCH:
            {
                String schema = reader.readString();
                String sql = reader.readString();
                int paramRowLen = reader.readInt();
                int rowNum = reader.readInt();
                boolean last = reader.readBoolean();
                Object[][] params = new Object[rowNum][];
                for (int i = 0; i < rowNum; ++i) params[i] = readParameterRow(reader, paramRowLen);
                int timeout = 0;
                if (ver.compareTo(OdbcConnectionContext.VER_2_3_2) >= 0)
                    timeout = reader.readInt();
                res = new OdbcQueryExecuteBatchRequest(schema, sql, last, params, timeout);
                break;
            }
        case OdbcRequest.QRY_FETCH:
            {
                long queryId = reader.readLong();
                int pageSize = reader.readInt();
                res = new OdbcQueryFetchRequest(queryId, pageSize);
                break;
            }
        case OdbcRequest.QRY_CLOSE:
            {
                long queryId = reader.readLong();
                res = new OdbcQueryCloseRequest(queryId);
                break;
            }
        case OdbcRequest.META_COLS:
            {
                String schema = reader.readString();
                String table = reader.readString();
                String column = reader.readString();
                res = new OdbcQueryGetColumnsMetaRequest(schema, table, column);
                break;
            }
        case OdbcRequest.META_TBLS:
            {
                String catalog = reader.readString();
                String schema = reader.readString();
                String table = reader.readString();
                String tableType = reader.readString();
                res = new OdbcQueryGetTablesMetaRequest(catalog, schema, table, tableType);
                break;
            }
        case OdbcRequest.META_PARAMS:
            {
                String schema = reader.readString();
                String sqlQuery = reader.readString();
                res = new OdbcQueryGetParamsMetaRequest(schema, sqlQuery);
                break;
            }
        case OdbcRequest.MORE_RESULTS:
            {
                long queryId = reader.readLong();
                int pageSize = reader.readInt();
                res = new OdbcQueryMoreResultsRequest(queryId, pageSize);
                break;
            }
        default:
            throw new IgniteException("Unknown ODBC command: [cmd=" + cmd + ']');
    }
    return res;
}
Also used : BinaryReaderExImpl(org.apache.ignite.internal.binary.BinaryReaderExImpl) BinaryInputStream(org.apache.ignite.internal.binary.streams.BinaryInputStream) ClientListenerRequest(org.apache.ignite.internal.processors.odbc.ClientListenerRequest) IgniteException(org.apache.ignite.IgniteException) BinaryHeapInputStream(org.apache.ignite.internal.binary.streams.BinaryHeapInputStream)

Example 9 with BinaryHeapInputStream

use of org.apache.ignite.internal.binary.streams.BinaryHeapInputStream in project ignite by apache.

the class JdbcRequest method readRequestId.

/**
 * Reads JdbcRequest Id.
 *
 * @param msg Jdbc request as byte array.
 * @return Request Id.
 */
public static long readRequestId(byte[] msg) {
    BinaryInputStream stream = new BinaryHeapInputStream(msg);
    stream.position(1);
    return stream.readLong();
}
Also used : BinaryInputStream(org.apache.ignite.internal.binary.streams.BinaryInputStream) BinaryHeapInputStream(org.apache.ignite.internal.binary.streams.BinaryHeapInputStream)

Example 10 with BinaryHeapInputStream

use of org.apache.ignite.internal.binary.streams.BinaryHeapInputStream in project ignite by apache.

the class BinaryBuilderReader method parseValue.

/**
 * @return Parsed value.
 */
public Object parseValue() {
    int valPos = pos;
    byte type = arr[pos++];
    int plainLazyValLen;
    boolean modifiableLazyVal = false;
    switch(type) {
        case GridBinaryMarshaller.NULL:
            return null;
        case GridBinaryMarshaller.HANDLE:
            {
                int objStart = pos - 1 - readInt();
                Object res = objMap.get(objStart);
                if (res != null)
                    return res;
                // Read handle by position
                int savedPos = pos;
                pos = objStart;
                res = parseValue();
                pos = savedPos;
                objMap.put(objStart, res);
                return res;
            }
        case GridBinaryMarshaller.OBJ:
            {
                pos--;
                Object res = objMap.get(pos);
                if (res == null) {
                    res = new BinaryObjectBuilderImpl(new BinaryBuilderReader(this, pos), pos);
                    objMap.put(pos, res);
                }
                pos += readInt(GridBinaryMarshaller.TOTAL_LEN_POS);
                return res;
            }
        case GridBinaryMarshaller.BYTE:
            return arr[pos++];
        case GridBinaryMarshaller.SHORT:
            {
                Object res = BinaryPrimitives.readShort(arr, pos);
                pos += 2;
                return res;
            }
        case GridBinaryMarshaller.INT:
            return readInt();
        case GridBinaryMarshaller.LONG:
            plainLazyValLen = 8;
            break;
        case GridBinaryMarshaller.FLOAT:
            plainLazyValLen = 4;
            break;
        case GridBinaryMarshaller.DOUBLE:
            plainLazyValLen = 8;
            break;
        case GridBinaryMarshaller.CHAR:
            plainLazyValLen = 2;
            break;
        case GridBinaryMarshaller.BOOLEAN:
            return arr[pos++] != 0;
        case GridBinaryMarshaller.DECIMAL:
            plainLazyValLen = /**
             * scale
             */
            4 + /**
             * mag len
             */
            4 + /**
             * mag bytes count
             */
            readInt(4);
            break;
        case GridBinaryMarshaller.STRING:
            plainLazyValLen = 4 + readStringLength();
            break;
        case GridBinaryMarshaller.UUID:
            plainLazyValLen = 8 + 8;
            break;
        case GridBinaryMarshaller.DATE:
            plainLazyValLen = 8;
            break;
        case GridBinaryMarshaller.TIMESTAMP:
            plainLazyValLen = 8 + 4;
            break;
        case GridBinaryMarshaller.TIME:
            plainLazyValLen = 8;
            break;
        case GridBinaryMarshaller.BYTE_ARR:
            plainLazyValLen = 4 + readLength();
            modifiableLazyVal = true;
            break;
        case GridBinaryMarshaller.SHORT_ARR:
            plainLazyValLen = 4 + readLength() * 2;
            modifiableLazyVal = true;
            break;
        case GridBinaryMarshaller.INT_ARR:
            plainLazyValLen = 4 + readLength() * 4;
            modifiableLazyVal = true;
            break;
        case GridBinaryMarshaller.LONG_ARR:
            plainLazyValLen = 4 + readLength() * 8;
            modifiableLazyVal = true;
            break;
        case GridBinaryMarshaller.FLOAT_ARR:
            plainLazyValLen = 4 + readLength() * 4;
            modifiableLazyVal = true;
            break;
        case GridBinaryMarshaller.DOUBLE_ARR:
            plainLazyValLen = 4 + readLength() * 8;
            modifiableLazyVal = true;
            break;
        case GridBinaryMarshaller.CHAR_ARR:
            plainLazyValLen = 4 + readLength() * 2;
            modifiableLazyVal = true;
            break;
        case GridBinaryMarshaller.BOOLEAN_ARR:
            plainLazyValLen = 4 + readLength();
            modifiableLazyVal = true;
            break;
        case GridBinaryMarshaller.OBJ_ARR:
            return new BinaryObjectArrayLazyValue(this);
        case GridBinaryMarshaller.DATE_ARR:
            {
                int size = readInt();
                Date[] res = new Date[size];
                for (int i = 0; i < res.length; i++) {
                    byte flag = arr[pos++];
                    if (flag == GridBinaryMarshaller.NULL)
                        continue;
                    if (flag != GridBinaryMarshaller.DATE)
                        throw new BinaryObjectException("Invalid flag value: " + flag);
                    long time = BinaryPrimitives.readLong(arr, pos);
                    pos += 8;
                    res[i] = new Date(time);
                }
                return res;
            }
        case GridBinaryMarshaller.TIMESTAMP_ARR:
            {
                int size = readInt();
                Timestamp[] res = new Timestamp[size];
                for (int i = 0; i < res.length; i++) {
                    byte flag = arr[pos++];
                    if (flag == GridBinaryMarshaller.NULL)
                        continue;
                    if (flag != GridBinaryMarshaller.TIMESTAMP)
                        throw new BinaryObjectException("Invalid flag value: " + flag);
                    long time = BinaryPrimitives.readLong(arr, pos);
                    pos += 8;
                    int nano = BinaryPrimitives.readInt(arr, pos);
                    pos += 4;
                    Timestamp ts = new Timestamp(time);
                    ts.setNanos(ts.getNanos() + nano);
                    res[i] = ts;
                }
                return res;
            }
        case GridBinaryMarshaller.TIME_ARR:
            {
                int size = readInt();
                Time[] res = new Time[size];
                for (int i = 0; i < res.length; i++) {
                    byte flag = arr[pos++];
                    if (flag == GridBinaryMarshaller.NULL)
                        continue;
                    if (flag != GridBinaryMarshaller.TIME)
                        throw new BinaryObjectException("Invalid flag value: " + flag);
                    long time = BinaryPrimitives.readLong(arr, pos);
                    pos += 8;
                    res[i] = new Time(time);
                }
                return res;
            }
        case GridBinaryMarshaller.UUID_ARR:
        case GridBinaryMarshaller.STRING_ARR:
        case GridBinaryMarshaller.DECIMAL_ARR:
            {
                int size = readInt();
                for (int i = 0; i < size; i++) {
                    byte flag = arr[pos++];
                    if (flag == GridBinaryMarshaller.UUID)
                        pos += 8 + 8;
                    else if (flag == GridBinaryMarshaller.STRING)
                        pos += 4 + readStringLength();
                    else if (flag == GridBinaryMarshaller.DECIMAL) {
                        // scale value
                        pos += 4;
                        pos += 4 + readLength();
                    } else
                        assert flag == GridBinaryMarshaller.NULL;
                }
                return new BinaryModifiableLazyValue(this, valPos, pos - valPos);
            }
        case GridBinaryMarshaller.COL:
            {
                int size = readInt();
                byte colType = arr[pos++];
                Object res = objMap.get(valPos);
                Object parseRes;
                switch(colType) {
                    case GridBinaryMarshaller.USER_COL:
                    case GridBinaryMarshaller.ARR_LIST:
                        parseRes = new BinaryLazyArrayList(this, size);
                        break;
                    case GridBinaryMarshaller.LINKED_LIST:
                        parseRes = new BinaryLazyLinkedList(this, size);
                        break;
                    case GridBinaryMarshaller.HASH_SET:
                    case GridBinaryMarshaller.LINKED_HASH_SET:
                        parseRes = new BinaryLazySet(this, size);
                        break;
                    default:
                        throw new BinaryObjectException("Unknown collection type: " + colType);
                }
                if (res == null || res instanceof LazyCollection) {
                    objMap.put(valPos, parseRes);
                    res = parseRes;
                }
                return res;
            }
        case GridBinaryMarshaller.MAP:
            {
                Object res = objMap.get(valPos);
                Object parseRes = BinaryLazyMap.parseMap(this);
                if (res == null || res instanceof LazyCollection) {
                    objMap.put(valPos, parseRes);
                    res = parseRes;
                }
                return res;
            }
        case GridBinaryMarshaller.ENUM:
            return new BinaryBuilderEnum(this);
        case GridBinaryMarshaller.ENUM_ARR:
            return new BinaryEnumArrayLazyValue(this);
        case GridBinaryMarshaller.BINARY_OBJ:
            {
                int size = readInt();
                pos += size;
                int start = readInt();
                BinaryObjectImpl binaryObj = new BinaryObjectImpl(ctx, arr, pos - 4 - size + start);
                return new BinaryPlainBinaryObject(binaryObj);
            }
        case GridBinaryMarshaller.OPTM_MARSH:
            {
                final BinaryHeapInputStream bin = BinaryHeapInputStream.create(arr, pos);
                final Object obj = BinaryUtils.doReadOptimized(bin, ctx, U.resolveClassLoader(ctx.configuration()));
                pos = bin.position();
                return obj;
            }
        default:
            throw new BinaryObjectException("Invalid flag value: " + type);
    }
    BinaryAbstractLazyValue res;
    if (modifiableLazyVal)
        res = new BinaryModifiableLazyValue(this, valPos, 1 + plainLazyValLen);
    else
        res = new BinaryPlainLazyValue(this, valPos, 1 + plainLazyValLen);
    pos += plainLazyValLen;
    return res;
}
Also used : BinaryObjectImpl(org.apache.ignite.internal.binary.BinaryObjectImpl) Time(java.sql.Time) Timestamp(java.sql.Timestamp) Date(java.util.Date) BinaryHeapInputStream(org.apache.ignite.internal.binary.streams.BinaryHeapInputStream) BinaryObjectException(org.apache.ignite.binary.BinaryObjectException)

Aggregations

BinaryHeapInputStream (org.apache.ignite.internal.binary.streams.BinaryHeapInputStream)16 BinaryReaderExImpl (org.apache.ignite.internal.binary.BinaryReaderExImpl)13 BinaryHeapOutputStream (org.apache.ignite.internal.binary.streams.BinaryHeapOutputStream)9 BinaryWriterExImpl (org.apache.ignite.internal.binary.BinaryWriterExImpl)8 BinaryInputStream (org.apache.ignite.internal.binary.streams.BinaryInputStream)7 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)4 JdbcResponse (org.apache.ignite.internal.processors.odbc.jdbc.JdbcResponse)3 SQLException (java.sql.SQLException)2 IgniteException (org.apache.ignite.IgniteException)2 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)2 MarshallerContextImpl (org.apache.ignite.internal.MarshallerContextImpl)2 BinaryContext (org.apache.ignite.internal.binary.BinaryContext)2 BinaryMarshaller (org.apache.ignite.internal.binary.BinaryMarshaller)2 ClientListenerProtocolVersion (org.apache.ignite.internal.processors.odbc.ClientListenerProtocolVersion)2 ClientListenerRequest (org.apache.ignite.internal.processors.odbc.ClientListenerRequest)2 IgniteProductVersion (org.apache.ignite.lang.IgniteProductVersion)2 Time (java.sql.Time)1 Timestamp (java.sql.Timestamp)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1