Search in sources :

Example 11 with Binary

use of org.bson.types.Binary in project presto by prestodb.

the class MongoPageSink method getObjectValue.

private Object getObjectValue(Type type, Block block, int position) {
    if (block.isNull(position)) {
        if (type.equals(OBJECT_ID)) {
            return new ObjectId();
        }
        return null;
    }
    if (type.equals(OBJECT_ID)) {
        return new ObjectId(block.getSlice(position, 0, block.getSliceLength(position)).getBytes());
    }
    if (type.equals(BooleanType.BOOLEAN)) {
        return type.getBoolean(block, position);
    }
    if (type.equals(BigintType.BIGINT)) {
        return type.getLong(block, position);
    }
    if (type.equals(IntegerType.INTEGER)) {
        return (int) type.getLong(block, position);
    }
    if (type.equals(SmallintType.SMALLINT)) {
        return (short) type.getLong(block, position);
    }
    if (type.equals(TinyintType.TINYINT)) {
        return (byte) type.getLong(block, position);
    }
    if (type.equals(DoubleType.DOUBLE)) {
        return type.getDouble(block, position);
    }
    if (isVarcharType(type)) {
        return type.getSlice(block, position).toStringUtf8();
    }
    if (type.equals(VarbinaryType.VARBINARY)) {
        return new Binary(type.getSlice(block, position).getBytes());
    }
    if (type.equals(DateType.DATE)) {
        long days = type.getLong(block, position);
        return new Date(TimeUnit.DAYS.toMillis(days));
    }
    if (type.equals(TimeType.TIME)) {
        long millisUtc = type.getLong(block, position);
        return new Date(millisUtc);
    }
    if (type.equals(TimestampType.TIMESTAMP)) {
        long millisUtc = type.getLong(block, position);
        return new Date(millisUtc);
    }
    if (type.equals(TimestampWithTimeZoneType.TIMESTAMP_WITH_TIME_ZONE)) {
        long millisUtc = unpackMillisUtc(type.getLong(block, position));
        return new Date(millisUtc);
    }
    if (type instanceof DecimalType) {
        // TODO: decimal type might not support yet
        DecimalType decimalType = (DecimalType) type;
        BigInteger unscaledValue;
        if (decimalType.isShort()) {
            unscaledValue = BigInteger.valueOf(decimalType.getLong(block, position));
        } else {
            unscaledValue = Decimals.decodeUnscaledValue(decimalType.getSlice(block, position));
        }
        return new BigDecimal(unscaledValue);
    }
    if (isArrayType(type)) {
        Type elementType = type.getTypeParameters().get(0);
        Block arrayBlock = block.getObject(position, Block.class);
        List<Object> list = new ArrayList<>(arrayBlock.getPositionCount());
        for (int i = 0; i < arrayBlock.getPositionCount(); i++) {
            Object element = getObjectValue(elementType, arrayBlock, i);
            list.add(element);
        }
        return unmodifiableList(list);
    }
    if (isMapType(type)) {
        Type keyType = type.getTypeParameters().get(0);
        Type valueType = type.getTypeParameters().get(1);
        Block mapBlock = block.getObject(position, Block.class);
        // map type is converted into list of fixed keys document
        List<Object> values = new ArrayList<>(mapBlock.getPositionCount() / 2);
        for (int i = 0; i < mapBlock.getPositionCount(); i += 2) {
            Map<String, Object> mapValue = new HashMap<>();
            mapValue.put("key", getObjectValue(keyType, mapBlock, i));
            mapValue.put("value", getObjectValue(valueType, mapBlock, i + 1));
            values.add(mapValue);
        }
        return unmodifiableList(values);
    }
    if (isRowType(type)) {
        Block rowBlock = block.getObject(position, Block.class);
        List<Type> fieldTypes = type.getTypeParameters();
        if (fieldTypes.size() != rowBlock.getPositionCount()) {
            throw new PrestoException(StandardErrorCode.GENERIC_INTERNAL_ERROR, "Expected row value field count does not match type field count");
        }
        if (isImplicitRowType(type)) {
            List<Object> rowValue = new ArrayList<>();
            for (int i = 0; i < rowBlock.getPositionCount(); i++) {
                Object element = getObjectValue(fieldTypes.get(i), rowBlock, i);
                rowValue.add(element);
            }
            return unmodifiableList(rowValue);
        }
        Map<String, Object> rowValue = new HashMap<>();
        for (int i = 0; i < rowBlock.getPositionCount(); i++) {
            rowValue.put(type.getTypeSignature().getParameters().get(i).getNamedTypeSignature().getName(), getObjectValue(fieldTypes.get(i), rowBlock, i));
        }
        return unmodifiableMap(rowValue);
    }
    throw new PrestoException(NOT_SUPPORTED, "unsupported type: " + type);
}
Also used : ObjectId(org.bson.types.ObjectId) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PrestoException(com.facebook.presto.spi.PrestoException) Date(java.util.Date) BigDecimal(java.math.BigDecimal) IntegerType(com.facebook.presto.spi.type.IntegerType) DateType(com.facebook.presto.spi.type.DateType) TinyintType(com.facebook.presto.spi.type.TinyintType) DoubleType(com.facebook.presto.spi.type.DoubleType) DecimalType(com.facebook.presto.spi.type.DecimalType) TimestampType(com.facebook.presto.spi.type.TimestampType) SmallintType(com.facebook.presto.spi.type.SmallintType) TypeUtils.isArrayType(com.facebook.presto.mongodb.TypeUtils.isArrayType) VarbinaryType(com.facebook.presto.spi.type.VarbinaryType) TypeUtils.isRowType(com.facebook.presto.mongodb.TypeUtils.isRowType) Varchars.isVarcharType(com.facebook.presto.spi.type.Varchars.isVarcharType) TimeType(com.facebook.presto.spi.type.TimeType) Type(com.facebook.presto.spi.type.Type) BigintType(com.facebook.presto.spi.type.BigintType) TimestampWithTimeZoneType(com.facebook.presto.spi.type.TimestampWithTimeZoneType) BooleanType(com.facebook.presto.spi.type.BooleanType) TypeUtils.isMapType(com.facebook.presto.mongodb.TypeUtils.isMapType) DecimalType(com.facebook.presto.spi.type.DecimalType) BigInteger(java.math.BigInteger) Block(com.facebook.presto.spi.block.Block) Binary(org.bson.types.Binary)

Example 12 with Binary

use of org.bson.types.Binary in project mongo-java-driver by mongodb.

the class GridFSDownloadStreamImpl method getBufferFromChunk.

private byte[] getBufferFromChunk(final Document chunk, final int expectedChunkIndex) {
    if (chunk == null || chunk.getInteger("n") != expectedChunkIndex) {
        throw chunkNotFound(expectedChunkIndex);
    } else if (!(chunk.get("data") instanceof Binary)) {
        throw new MongoGridFSException("Unexpected data format for the chunk");
    }
    byte[] data = chunk.get("data", Binary.class).getData();
    long expectedDataLength = expectedChunkIndex + 1 == numberOfChunks ? fileInfo.getLength() - (expectedChunkIndex * (long) fileInfo.getChunkSize()) : fileInfo.getChunkSize();
    if (data.length != expectedDataLength) {
        throw new MongoGridFSException(format("Chunk size data length is not the expected size. " + "The size was %s for file_id: %s chunk index %s it should be %s bytes.", data.length, fileInfo.getId(), expectedChunkIndex, expectedDataLength));
    }
    return data;
}
Also used : MongoGridFSException(com.mongodb.MongoGridFSException) Binary(org.bson.types.Binary)

Example 13 with Binary

use of org.bson.types.Binary in project mongo-java-driver by mongodb.

the class GridFSDownloadStreamImpl method getBufferFromChunk.

private byte[] getBufferFromChunk(final Document chunk, final int expectedChunkIndex) {
    if (chunk == null || chunk.getInteger("n") != expectedChunkIndex) {
        throw new MongoGridFSException(format("Could not find file chunk for file_id: %s at chunk index %s.", fileId, expectedChunkIndex));
    }
    if (!(chunk.get("data") instanceof Binary)) {
        throw new MongoGridFSException("Unexpected data format for the chunk");
    }
    byte[] data = chunk.get("data", Binary.class).getData();
    long expectedDataLength = 0;
    boolean extraChunk = false;
    if (expectedChunkIndex + 1 > numberOfChunks) {
        extraChunk = true;
    } else if (expectedChunkIndex + 1 == numberOfChunks) {
        expectedDataLength = length - (expectedChunkIndex * (long) chunkSizeInBytes);
    } else {
        expectedDataLength = chunkSizeInBytes;
    }
    if (extraChunk && data.length > expectedDataLength) {
        throw new MongoGridFSException(format("Extra chunk data for file_id: %s. Unexpected chunk at chunk index %s." + "The size was %s and it should be %s bytes.", fileId, expectedChunkIndex, data.length, expectedDataLength));
    } else if (data.length != expectedDataLength) {
        throw new MongoGridFSException(format("Chunk size data length is not the expected size. " + "The size was %s for file_id: %s chunk index %s it should be %s bytes.", data.length, fileId, expectedChunkIndex, expectedDataLength));
    }
    return data;
}
Also used : MongoGridFSException(com.mongodb.MongoGridFSException) Binary(org.bson.types.Binary)

Example 14 with Binary

use of org.bson.types.Binary in project mongo-java-driver by mongodb.

the class JSONCallback method objectDone.

@Override
public Object objectDone() {
    String name = curName();
    Object o = super.objectDone();
    if (_lastArray) {
        return o;
    }
    BSONObject b = (BSONObject) o;
    // override the object if it's a special type
    if (b.containsField("$oid")) {
        o = new ObjectId((String) b.get("$oid"));
    } else if (b.containsField("$date")) {
        if (b.get("$date") instanceof Number) {
            o = new Date(((Number) b.get("$date")).longValue());
        } else {
            SimpleDateFormat format = new SimpleDateFormat(_msDateFormat);
            format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
            o = format.parse(b.get("$date").toString(), new ParsePosition(0));
            if (o == null) {
                // try older format with no ms
                format = new SimpleDateFormat(_secDateFormat);
                format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
                o = format.parse(b.get("$date").toString(), new ParsePosition(0));
            }
        }
    } else if (b.containsField("$regex")) {
        o = Pattern.compile((String) b.get("$regex"), BSON.regexFlags((String) b.get("$options")));
    } else if (b.containsField("$ts")) {
        //Legacy timestamp format
        Integer ts = ((Number) b.get("$ts")).intValue();
        Integer inc = ((Number) b.get("$inc")).intValue();
        o = new BSONTimestamp(ts, inc);
    } else if (b.containsField("$timestamp")) {
        BSONObject tsObject = (BSONObject) b.get("$timestamp");
        Integer ts = ((Number) tsObject.get("t")).intValue();
        Integer inc = ((Number) tsObject.get("i")).intValue();
        o = new BSONTimestamp(ts, inc);
    } else if (b.containsField("$code")) {
        if (b.containsField("$scope")) {
            o = new CodeWScope((String) b.get("$code"), (DBObject) b.get("$scope"));
        } else {
            o = new Code((String) b.get("$code"));
        }
    } else if (b.containsField("$ref")) {
        o = new DBRef((String) b.get("$ref"), b.get("$id"));
    } else if (b.containsField("$minKey")) {
        o = new MinKey();
    } else if (b.containsField("$maxKey")) {
        o = new MaxKey();
    } else if (b.containsField("$uuid")) {
        o = UUID.fromString((String) b.get("$uuid"));
    } else if (b.containsField("$binary")) {
        int type = (b.get("$type") instanceof String) ? Integer.valueOf((String) b.get("$type"), 16) : (Integer) b.get("$type");
        byte[] bytes = DatatypeConverter.parseBase64Binary((String) b.get("$binary"));
        o = new Binary((byte) type, bytes);
    } else if (b.containsField("$undefined") && b.get("$undefined").equals(true)) {
        o = new BsonUndefined();
    } else if (b.containsField("$numberLong")) {
        o = Long.valueOf((String) b.get("$numberLong"));
    } else if (b.containsField("$numberDecimal")) {
        o = Decimal128.parse((String) b.get("$numberDecimal"));
    }
    if (!isStackEmpty()) {
        _put(name, o);
    } else {
        o = !BSON.hasDecodeHooks() ? o : BSON.applyDecodingHooks(o);
        setRoot(o);
    }
    return o;
}
Also used : ObjectId(org.bson.types.ObjectId) BSONObject(org.bson.BSONObject) GregorianCalendar(java.util.GregorianCalendar) DBRef(com.mongodb.DBRef) MaxKey(org.bson.types.MaxKey) BSONTimestamp(org.bson.types.BSONTimestamp) Code(org.bson.types.Code) Date(java.util.Date) CodeWScope(org.bson.types.CodeWScope) MinKey(org.bson.types.MinKey) SimpleTimeZone(java.util.SimpleTimeZone) BasicDBObject(com.mongodb.BasicDBObject) BSONObject(org.bson.BSONObject) DBObject(com.mongodb.DBObject) Binary(org.bson.types.Binary) SimpleDateFormat(java.text.SimpleDateFormat) BsonUndefined(org.bson.BsonUndefined) ParsePosition(java.text.ParsePosition)

Example 15 with Binary

use of org.bson.types.Binary in project mongo-java-driver by mongodb.

the class LazyBSONObject method readValue.

Object readValue(final BsonBinaryReader reader) {
    switch(reader.getCurrentBsonType()) {
        case DOCUMENT:
            return readDocument(reader);
        case ARRAY:
            return readArray(reader);
        case DOUBLE:
            return reader.readDouble();
        case STRING:
            return reader.readString();
        case BINARY:
            byte binarySubType = reader.peekBinarySubType();
            if (BsonBinarySubType.isUuid(binarySubType) && reader.peekBinarySize() == 16) {
                return new UuidCodec().decode(reader, DecoderContext.builder().build());
            }
            BsonBinary binary = reader.readBinaryData();
            if (binarySubType == BINARY.getValue() || binarySubType == OLD_BINARY.getValue()) {
                return binary.getData();
            } else {
                return new Binary(binary.getType(), binary.getData());
            }
        case NULL:
            reader.readNull();
            return null;
        case UNDEFINED:
            reader.readUndefined();
            return null;
        case OBJECT_ID:
            return reader.readObjectId();
        case BOOLEAN:
            return reader.readBoolean();
        case DATE_TIME:
            return new Date(reader.readDateTime());
        case REGULAR_EXPRESSION:
            BsonRegularExpression regularExpression = reader.readRegularExpression();
            return Pattern.compile(regularExpression.getPattern(), BSON.regexFlags(regularExpression.getOptions()));
        case DB_POINTER:
            BsonDbPointer dbPointer = reader.readDBPointer();
            return callback.createDBRef(dbPointer.getNamespace(), dbPointer.getId());
        case JAVASCRIPT:
            return new Code(reader.readJavaScript());
        case SYMBOL:
            return new Symbol(reader.readSymbol());
        case JAVASCRIPT_WITH_SCOPE:
            return new CodeWScope(reader.readJavaScriptWithScope(), (BSONObject) readJavaScriptWithScopeDocument(reader));
        case INT32:
            return reader.readInt32();
        case TIMESTAMP:
            BsonTimestamp timestamp = reader.readTimestamp();
            return new BSONTimestamp(timestamp.getTime(), timestamp.getInc());
        case INT64:
            return reader.readInt64();
        case DECIMAL128:
            return reader.readDecimal128();
        case MIN_KEY:
            reader.readMinKey();
            return new MinKey();
        case MAX_KEY:
            reader.readMaxKey();
            return new MaxKey();
        default:
            throw new IllegalArgumentException("unhandled BSON type: " + reader.getCurrentBsonType());
    }
}
Also used : UuidCodec(org.bson.codecs.UuidCodec) Symbol(org.bson.types.Symbol) MaxKey(org.bson.types.MaxKey) BSONTimestamp(org.bson.types.BSONTimestamp) Code(org.bson.types.Code) Date(java.util.Date) CodeWScope(org.bson.types.CodeWScope) MinKey(org.bson.types.MinKey) Binary(org.bson.types.Binary)

Aggregations

Binary (org.bson.types.Binary)20 Date (java.util.Date)11 ObjectId (org.bson.types.ObjectId)10 Test (org.junit.Test)10 MaxKey (org.bson.types.MaxKey)8 MinKey (org.bson.types.MinKey)8 BSONTimestamp (org.bson.types.BSONTimestamp)7 Code (org.bson.types.Code)7 BasicDBObject (com.mongodb.BasicDBObject)5 DBRef (com.mongodb.DBRef)5 CodeWScope (org.bson.types.CodeWScope)5 Document (org.bson.Document)4 Symbol (org.bson.types.Symbol)3 MongoGridFSException (com.mongodb.MongoGridFSException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 ArrayList (java.util.ArrayList)2 Arrays.asList (java.util.Arrays.asList)2 GregorianCalendar (java.util.GregorianCalendar)2 List (java.util.List)2 Map (java.util.Map)2