Search in sources :

Example 1 with ImmutableRTreeObjectStrategy

use of org.apache.druid.segment.data.ImmutableRTreeObjectStrategy in project druid by druid-io.

the class DictionaryEncodedColumnPartSerde method getDeserializer.

@Override
public Deserializer getDeserializer() {
    return new Deserializer() {

        @Override
        public void read(ByteBuffer buffer, ColumnBuilder builder, ColumnConfig columnConfig) {
            final VERSION rVersion = VERSION.fromByte(buffer.get());
            final int rFlags;
            if (rVersion.compareTo(VERSION.COMPRESSED) >= 0) {
                rFlags = buffer.getInt();
            } else {
                rFlags = rVersion.equals(VERSION.UNCOMPRESSED_MULTI_VALUE) ? Feature.MULTI_VALUE.getMask() : NO_FLAGS;
            }
            final boolean hasMultipleValues = Feature.MULTI_VALUE.isSet(rFlags) || Feature.MULTI_VALUE_V3.isSet(rFlags);
            // Duplicate the first buffer since we are reading the dictionary twice.
            final GenericIndexed<String> rDictionary = GenericIndexed.read(buffer.duplicate(), GenericIndexed.STRING_STRATEGY, builder.getFileMapper());
            final GenericIndexed<ByteBuffer> rDictionaryUtf8 = GenericIndexed.read(buffer, GenericIndexed.BYTE_BUFFER_STRATEGY, builder.getFileMapper());
            builder.setType(ValueType.STRING);
            final WritableSupplier<ColumnarInts> rSingleValuedColumn;
            final WritableSupplier<ColumnarMultiInts> rMultiValuedColumn;
            if (hasMultipleValues) {
                rMultiValuedColumn = readMultiValuedColumn(rVersion, buffer, rFlags);
                rSingleValuedColumn = null;
            } else {
                rSingleValuedColumn = readSingleValuedColumn(rVersion, buffer);
                rMultiValuedColumn = null;
            }
            final String firstDictionaryEntry = rDictionary.get(0);
            DictionaryEncodedColumnSupplier dictionaryEncodedColumnSupplier = new DictionaryEncodedColumnSupplier(rDictionary, rDictionaryUtf8, rSingleValuedColumn, rMultiValuedColumn, columnConfig.columnCacheSizeBytes());
            builder.setHasMultipleValues(hasMultipleValues).setHasNulls(firstDictionaryEntry == null).setDictionaryEncodedColumnSupplier(dictionaryEncodedColumnSupplier);
            if (!Feature.NO_BITMAP_INDEX.isSet(rFlags)) {
                GenericIndexed<ImmutableBitmap> rBitmaps = GenericIndexed.read(buffer, bitmapSerdeFactory.getObjectStrategy(), builder.getFileMapper());
                builder.setBitmapIndex(new StringBitmapIndexColumnPartSupplier(bitmapSerdeFactory.getBitmapFactory(), rBitmaps, rDictionary));
            }
            if (buffer.hasRemaining()) {
                ImmutableRTree rSpatialIndex = new ImmutableRTreeObjectStrategy(bitmapSerdeFactory.getBitmapFactory()).fromByteBufferWithSize(buffer);
                builder.setSpatialIndex(new SpatialIndexColumnPartSupplier(rSpatialIndex));
            }
        }

        private WritableSupplier<ColumnarInts> readSingleValuedColumn(VERSION version, ByteBuffer buffer) {
            switch(version) {
                case UNCOMPRESSED_SINGLE_VALUE:
                case UNCOMPRESSED_WITH_FLAGS:
                    return VSizeColumnarInts.readFromByteBuffer(buffer);
                case COMPRESSED:
                    return CompressedVSizeColumnarIntsSupplier.fromByteBuffer(buffer, byteOrder);
                default:
                    throw new IAE("Unsupported single-value version[%s]", version);
            }
        }

        private WritableSupplier<ColumnarMultiInts> readMultiValuedColumn(VERSION version, ByteBuffer buffer, int flags) {
            switch(version) {
                case UNCOMPRESSED_MULTI_VALUE:
                    {
                        return VSizeColumnarMultiInts.readFromByteBuffer(buffer);
                    }
                case UNCOMPRESSED_WITH_FLAGS:
                    {
                        if (Feature.MULTI_VALUE.isSet(flags)) {
                            return VSizeColumnarMultiInts.readFromByteBuffer(buffer);
                        } else {
                            throw new IAE("Unrecognized multi-value flag[%d] for version[%s]", flags, version);
                        }
                    }
                case COMPRESSED:
                    {
                        if (Feature.MULTI_VALUE.isSet(flags)) {
                            return CompressedVSizeColumnarMultiIntsSupplier.fromByteBuffer(buffer, byteOrder);
                        } else if (Feature.MULTI_VALUE_V3.isSet(flags)) {
                            return V3CompressedVSizeColumnarMultiIntsSupplier.fromByteBuffer(buffer, byteOrder);
                        } else {
                            throw new IAE("Unrecognized multi-value flag[%d] for version[%s]", flags, version);
                        }
                    }
                default:
                    throw new IAE("Unsupported multi-value version[%s]", version);
            }
        }
    };
}
Also used : ColumnConfig(org.apache.druid.segment.column.ColumnConfig) ImmutableBitmap(org.apache.druid.collections.bitmap.ImmutableBitmap) ColumnarInts(org.apache.druid.segment.data.ColumnarInts) VSizeColumnarInts(org.apache.druid.segment.data.VSizeColumnarInts) IAE(org.apache.druid.java.util.common.IAE) ByteBuffer(java.nio.ByteBuffer) ColumnarMultiInts(org.apache.druid.segment.data.ColumnarMultiInts) VSizeColumnarMultiInts(org.apache.druid.segment.data.VSizeColumnarMultiInts) ImmutableRTree(org.apache.druid.collections.spatial.ImmutableRTree) ImmutableRTreeObjectStrategy(org.apache.druid.segment.data.ImmutableRTreeObjectStrategy) ColumnBuilder(org.apache.druid.segment.column.ColumnBuilder)

Example 2 with ImmutableRTreeObjectStrategy

use of org.apache.druid.segment.data.ImmutableRTreeObjectStrategy in project druid by druid-io.

the class ImmutableRTreeTest method testToBytes.

@Test
public void testToBytes() {
    BitmapFactory bf = new RoaringBitmapFactory();
    ImmutableRTreeObjectStrategy rTreeObjectStrategy = new ImmutableRTreeObjectStrategy(bf);
    RTree rTree = new RTree(2, new LinearGutmanSplitStrategy(0, 50, bf), bf);
    rTree.insert(new float[] { 0, 0 }, 1);
    ImmutableRTree immutableRTree = ImmutableRTree.newImmutableFromMutable(rTree);
    byte[] bytes1 = immutableRTree.toBytes();
    GenericIndexed<ImmutableRTree> genericIndexed = GenericIndexed.fromIterable(Arrays.asList(immutableRTree, immutableRTree), rTreeObjectStrategy);
    ImmutableRTree deserializedTree = genericIndexed.get(0);
    byte[] bytes2 = deserializedTree.toBytes();
    org.junit.Assert.assertEquals(Bytes.asList(bytes1), Bytes.asList(bytes2));
}
Also used : ImmutableRTreeObjectStrategy(org.apache.druid.segment.data.ImmutableRTreeObjectStrategy) BitmapFactory(org.apache.druid.collections.bitmap.BitmapFactory) ConciseBitmapFactory(org.apache.druid.collections.bitmap.ConciseBitmapFactory) RoaringBitmapFactory(org.apache.druid.collections.bitmap.RoaringBitmapFactory) LinearGutmanSplitStrategy(org.apache.druid.collections.spatial.split.LinearGutmanSplitStrategy) RoaringBitmapFactory(org.apache.druid.collections.bitmap.RoaringBitmapFactory) Test(org.junit.Test)

Aggregations

ImmutableRTreeObjectStrategy (org.apache.druid.segment.data.ImmutableRTreeObjectStrategy)2 ByteBuffer (java.nio.ByteBuffer)1 BitmapFactory (org.apache.druid.collections.bitmap.BitmapFactory)1 ConciseBitmapFactory (org.apache.druid.collections.bitmap.ConciseBitmapFactory)1 ImmutableBitmap (org.apache.druid.collections.bitmap.ImmutableBitmap)1 RoaringBitmapFactory (org.apache.druid.collections.bitmap.RoaringBitmapFactory)1 ImmutableRTree (org.apache.druid.collections.spatial.ImmutableRTree)1 LinearGutmanSplitStrategy (org.apache.druid.collections.spatial.split.LinearGutmanSplitStrategy)1 IAE (org.apache.druid.java.util.common.IAE)1 ColumnBuilder (org.apache.druid.segment.column.ColumnBuilder)1 ColumnConfig (org.apache.druid.segment.column.ColumnConfig)1 ColumnarInts (org.apache.druid.segment.data.ColumnarInts)1 ColumnarMultiInts (org.apache.druid.segment.data.ColumnarMultiInts)1 VSizeColumnarInts (org.apache.druid.segment.data.VSizeColumnarInts)1 VSizeColumnarMultiInts (org.apache.druid.segment.data.VSizeColumnarMultiInts)1 Test (org.junit.Test)1