diff --git a/processing/src/main/java/io/druid/query/metadata/SegmentAnalyzer.java b/processing/src/main/java/io/druid/query/metadata/SegmentAnalyzer.java index 406740636376..a3f42341d9a0 100644 --- a/processing/src/main/java/io/druid/query/metadata/SegmentAnalyzer.java +++ b/processing/src/main/java/io/druid/query/metadata/SegmentAnalyzer.java @@ -306,35 +306,36 @@ private ColumnAnalysis analyzeComplexColumn( final String typeName ) { - final ComplexColumn complexColumn = column != null ? column.getComplexColumn() : null; - final boolean hasMultipleValues = capabilities != null && capabilities.hasMultipleValues(); - long size = 0; + try (final ComplexColumn complexColumn = column != null ? column.getComplexColumn() : null) { + final boolean hasMultipleValues = capabilities != null && capabilities.hasMultipleValues(); + long size = 0; + + if (analyzingSize() && complexColumn != null) { + final ComplexMetricSerde serde = ComplexMetrics.getSerdeForType(typeName); + if (serde == null) { + return ColumnAnalysis.error(String.format("unknown_complex_%s", typeName)); + } - if (analyzingSize() && complexColumn != null) { - final ComplexMetricSerde serde = ComplexMetrics.getSerdeForType(typeName); - if (serde == null) { - return ColumnAnalysis.error(String.format("unknown_complex_%s", typeName)); - } + final Function inputSizeFn = serde.inputSizeFn(); + if (inputSizeFn == null) { + return new ColumnAnalysis(typeName, hasMultipleValues, 0, null, null, null, null); + } - final Function inputSizeFn = serde.inputSizeFn(); - if (inputSizeFn == null) { - return new ColumnAnalysis(typeName, hasMultipleValues, 0, null, null, null, null); + final int length = column.getLength(); + for (int i = 0; i < length; ++i) { + size += inputSizeFn.apply(complexColumn.getRowValue(i)); + } } - final int length = column.getLength(); - for (int i = 0; i < length; ++i) { - size += inputSizeFn.apply(complexColumn.getRowValue(i)); - } + return new ColumnAnalysis( + typeName, + hasMultipleValues, + size, + null, + null, + null, + null + ); } - - return new ColumnAnalysis( - typeName, - hasMultipleValues, - size, - null, - null, - null, - null - ); } } diff --git a/processing/src/main/java/io/druid/query/search/SearchQueryRunner.java b/processing/src/main/java/io/druid/query/search/SearchQueryRunner.java index a690cfaf092d..a6a0265389d3 100644 --- a/processing/src/main/java/io/druid/query/search/SearchQueryRunner.java +++ b/processing/src/main/java/io/druid/query/search/SearchQueryRunner.java @@ -121,18 +121,22 @@ public Sequence> run( if (!interval.contains(segment.getDataInterval())) { MutableBitmap timeBitmap = bitmapFactory.makeEmptyMutableBitmap(); final Column timeColumn = index.getColumn(Column.TIME_COLUMN_NAME); - final GenericColumn timeValues = timeColumn.getGenericColumn(); + try (final GenericColumn timeValues = timeColumn.getGenericColumn()) { - int startIndex = Math.max(0, getStartIndexOfTime(timeValues, interval.getStartMillis(), true)); - int endIndex = Math.min(timeValues.length() - 1, getStartIndexOfTime(timeValues, interval.getEndMillis(), false)); + int startIndex = Math.max(0, getStartIndexOfTime(timeValues, interval.getStartMillis(), true)); + int endIndex = Math.min( + timeValues.length() - 1, + getStartIndexOfTime(timeValues, interval.getEndMillis(), false) + ); - for (int i = startIndex; i <= endIndex; i++) { - timeBitmap.add(i); - } + for (int i = startIndex; i <= endIndex; i++) { + timeBitmap.add(i); + } - final ImmutableBitmap finalTimeBitmap = bitmapFactory.makeImmutableBitmap(timeBitmap); - timeFilteredBitmap = - (baseFilter == null) ? finalTimeBitmap : finalTimeBitmap.intersection(baseFilter); + final ImmutableBitmap finalTimeBitmap = bitmapFactory.makeImmutableBitmap(timeBitmap); + timeFilteredBitmap = + (baseFilter == null) ? finalTimeBitmap : finalTimeBitmap.intersection(baseFilter); + } } else { timeFilteredBitmap = baseFilter; } diff --git a/processing/src/main/java/io/druid/segment/ColumnSelectorBitmapIndexSelector.java b/processing/src/main/java/io/druid/segment/ColumnSelectorBitmapIndexSelector.java index ccfbfee55996..be46e159e7cb 100644 --- a/processing/src/main/java/io/druid/segment/ColumnSelectorBitmapIndexSelector.java +++ b/processing/src/main/java/io/druid/segment/ColumnSelectorBitmapIndexSelector.java @@ -98,14 +98,9 @@ public Iterator iterator() @Override public int getNumRows() { - GenericColumn column = null; - try { - column = index.getColumn(Column.TIME_COLUMN_NAME).getGenericColumn(); + try (final GenericColumn column = index.getColumn(Column.TIME_COLUMN_NAME).getGenericColumn()) { return column.length(); } - finally { - CloseQuietly.close(column); - } } @Override diff --git a/processing/src/main/java/io/druid/segment/QueryableIndexIndexableAdapter.java b/processing/src/main/java/io/druid/segment/QueryableIndexIndexableAdapter.java index a33e64f5f673..4d1d4a9ac900 100644 --- a/processing/src/main/java/io/druid/segment/QueryableIndexIndexableAdapter.java +++ b/processing/src/main/java/io/druid/segment/QueryableIndexIndexableAdapter.java @@ -24,6 +24,7 @@ import com.google.common.collect.FluentIterable; import com.google.common.collect.Lists; import com.google.common.collect.Sets; +import com.google.common.io.Closer; import com.metamx.common.ISE; import com.metamx.common.guava.CloseQuietly; import com.metamx.common.logger.Logger; @@ -178,8 +179,9 @@ public Iterator iterator() return new Iterator() { final GenericColumn timestamps = input.getColumn(Column.TIME_COLUMN_NAME).getGenericColumn(); - final Object[] metrics; + final Closeable[] metrics; final Closeable[] columns; + final Closer closer = Closer.create(); final int numMetrics = getMetricNames().size(); @@ -190,6 +192,8 @@ public Iterator iterator() boolean done = false; { + closer.register(timestamps); + handlerSet.toArray(handlers); this.columns = FluentIterable .from(handlerSet) @@ -204,9 +208,12 @@ public Closeable apply(DimensionHandler handler) } } ).toArray(Closeable.class); + for (Closeable column : columns) { + closer.register(column); + } final Indexed availableMetrics = getMetricNames(); - metrics = new Object[availableMetrics.size()]; + metrics = new Closeable[availableMetrics.size()]; for (int i = 0; i < metrics.length; ++i) { final Column column = input.getColumn(availableMetrics.get(i)); final ValueType type = column.getCapabilities().getType(); @@ -222,6 +229,9 @@ public Closeable apply(DimensionHandler handler) throw new ISE("Cannot handle type[%s]", type); } } + for (Closeable metricColumn : metrics) { + closer.register(metricColumn); + } } @Override @@ -229,15 +239,7 @@ public boolean hasNext() { final boolean hasNext = currRow < numRows; if (!hasNext && !done) { - CloseQuietly.close(timestamps); - for (Object metric : metrics) { - if (metric instanceof Closeable) { - CloseQuietly.close((Closeable) metric); - } - } - for (Closeable dimension : columns) { - CloseQuietly.close(dimension); - } + CloseQuietly.close(closer); done = true; } return hasNext; @@ -315,8 +317,11 @@ public String getMetricType(String metric) return "float"; case LONG: return "long"; - case COMPLEX: - return column.getComplexColumn().getTypeName(); + case COMPLEX: { + try (ComplexColumn complexColumn = column.getComplexColumn() ) { + return complexColumn.getTypeName(); + } + } default: throw new ISE("Unknown type[%s]", type); } diff --git a/processing/src/main/java/io/druid/segment/QueryableIndexStorageAdapter.java b/processing/src/main/java/io/druid/segment/QueryableIndexStorageAdapter.java index db724373a51a..6a0940d2afa6 100644 --- a/processing/src/main/java/io/druid/segment/QueryableIndexStorageAdapter.java +++ b/processing/src/main/java/io/druid/segment/QueryableIndexStorageAdapter.java @@ -28,10 +28,8 @@ import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.google.common.collect.Sets; +import com.google.common.io.Closer; import com.metamx.collections.bitmap.ImmutableBitmap; -import com.metamx.common.IAE; -import com.metamx.common.UOE; -import com.metamx.common.guava.CloseQuietly; import com.metamx.common.guava.Sequence; import com.metamx.common.guava.Sequences; import io.druid.granularity.QueryGranularity; @@ -135,27 +133,17 @@ public int getNumRows() @Override public DateTime getMinTime() { - GenericColumn column = null; - try { - column = index.getColumn(Column.TIME_COLUMN_NAME).getGenericColumn(); + try (final GenericColumn column = index.getColumn(Column.TIME_COLUMN_NAME).getGenericColumn()) { return new DateTime(column.getLongSingleValueRow(0)); } - finally { - CloseQuietly.close(column); - } } @Override public DateTime getMaxTime() { - GenericColumn column = null; - try { - column = index.getColumn(Column.TIME_COLUMN_NAME).getGenericColumn(); + try (final GenericColumn column = index.getColumn(Column.TIME_COLUMN_NAME).getGenericColumn()) { return new DateTime(column.getLongSingleValueRow(column.length() - 1)); } - finally { - CloseQuietly.close(column); - } } @Override @@ -202,8 +190,9 @@ public Map getDimensionHandlers() public String getColumnTypeName(String columnName) { final Column column = index.getColumn(columnName); - final ComplexColumn complexColumn = column.getComplexColumn(); - return complexColumn != null ? complexColumn.getTypeName() : column.getCapabilities().getType().toString(); + try (final ComplexColumn complexColumn = column.getComplexColumn()) { + return complexColumn != null ? complexColumn.getTypeName() : column.getCapabilities().getType().toString(); + } } @Override @@ -382,11 +371,13 @@ public Sequence build() final Map dictionaryColumnCache = Maps.newHashMap(); final Map genericColumnCache = Maps.newHashMap(); - final Map complexColumnCache = Maps.newHashMap(); final Map objectColumnCache = Maps.newHashMap(); final GenericColumn timestamps = index.getColumn(Column.TIME_COLUMN_NAME).getGenericColumn(); + final Closer closer = Closer.create(); + closer.register(timestamps); + Iterable iterable = gran.iterable(interval.getStartMillis(), interval.getEndMillis()); if (descending) { iterable = Lists.reverse(ImmutableList.copyOf(iterable)); @@ -471,6 +462,7 @@ private DimensionSelector makeDimensionSelectorUndecorated( DictionaryEncodedColumn cachedColumn = dictionaryColumnCache.get(dimension); if (cachedColumn == null) { cachedColumn = columnDesc.getDictionaryEncoding(); + closer.register(cachedColumn); dictionaryColumnCache.put(dimension, cachedColumn); } @@ -592,6 +584,7 @@ public FloatColumnSelector makeFloatColumnSelector(String columnName) if (holder != null && (holder.getCapabilities().getType() == ValueType.FLOAT || holder.getCapabilities().getType() == ValueType.LONG)) { cachedMetricVals = holder.getGenericColumn(); + closer.register(cachedMetricVals); genericColumnCache.put(columnName, cachedMetricVals); } } @@ -628,6 +621,7 @@ public LongColumnSelector makeLongColumnSelector(String columnName) if (holder != null && (holder.getCapabilities().getType() == ValueType.LONG || holder.getCapabilities().getType() == ValueType.FLOAT)) { cachedMetricVals = holder.getGenericColumn(); + closer.register(cachedMetricVals); genericColumnCache.put(columnName, cachedMetricVals); } } @@ -676,6 +670,7 @@ public ObjectColumnSelector makeObjectColumnSelector(String column) } if (cachedColumnVals != null) { + closer.register((Closeable) cachedColumnVals); objectColumnCache.put(column, cachedColumnVals); } } @@ -953,28 +948,7 @@ public void reset() } } ), - new Closeable() - { - @Override - public void close() throws IOException - { - CloseQuietly.close(timestamps); - for (DictionaryEncodedColumn column : dictionaryColumnCache.values()) { - CloseQuietly.close(column); - } - for (GenericColumn column : genericColumnCache.values()) { - CloseQuietly.close(column); - } - for (ComplexColumn complexColumn : complexColumnCache.values()) { - CloseQuietly.close(complexColumn); - } - for (Object column : objectColumnCache.values()) { - if (column instanceof Closeable) { - CloseQuietly.close((Closeable) column); - } - } - } - } + closer ); } } diff --git a/processing/src/main/java/io/druid/segment/column/ComplexColumn.java b/processing/src/main/java/io/druid/segment/column/ComplexColumn.java index ba68baf48e0a..b52c18d9f4fd 100644 --- a/processing/src/main/java/io/druid/segment/column/ComplexColumn.java +++ b/processing/src/main/java/io/druid/segment/column/ComplexColumn.java @@ -28,4 +28,7 @@ public interface ComplexColumn extends Closeable public Class getClazz(); public String getTypeName(); public Object getRowValue(int rowNum); + + @Override + void close(); } diff --git a/processing/src/main/java/io/druid/segment/column/ComplexColumnImpl.java b/processing/src/main/java/io/druid/segment/column/ComplexColumnImpl.java deleted file mode 100644 index 1008559975fc..000000000000 --- a/processing/src/main/java/io/druid/segment/column/ComplexColumnImpl.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Licensed to Metamarkets Group Inc. (Metamarkets) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. Metamarkets licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package io.druid.segment.column; - -import io.druid.segment.data.Indexed; - -/** - */ -public class ComplexColumnImpl extends AbstractColumn -{ - private static final ColumnCapabilitiesImpl CAPABILITIES = new ColumnCapabilitiesImpl() - .setType(ValueType.COMPLEX); - - private final Indexed column; - private final String typeName; - - public ComplexColumnImpl(String typeName, Indexed column) - { - this.column = column; - this.typeName = typeName; - } - - @Override - public ColumnCapabilities getCapabilities() - { - return CAPABILITIES; - } - - @Override - public int getLength() - { - return column.size(); - } - - @Override - public ComplexColumn getComplexColumn() - { - return new IndexedComplexColumn(typeName, column); - } -} diff --git a/processing/src/main/java/io/druid/segment/column/GenericColumn.java b/processing/src/main/java/io/druid/segment/column/GenericColumn.java index 7bb75f15a519..27ab6d7e3650 100644 --- a/processing/src/main/java/io/druid/segment/column/GenericColumn.java +++ b/processing/src/main/java/io/druid/segment/column/GenericColumn.java @@ -39,4 +39,7 @@ public interface GenericColumn extends Closeable public IndexedFloats getFloatMultiValueRow(int rowNum); public long getLongSingleValueRow(int rowNum); public IndexedLongs getLongMultiValueRow(int rowNum); + + @Override + void close(); } diff --git a/processing/src/main/java/io/druid/segment/column/IndexedComplexColumn.java b/processing/src/main/java/io/druid/segment/column/IndexedComplexColumn.java index 2f4593ddda20..f45aacc0579b 100644 --- a/processing/src/main/java/io/druid/segment/column/IndexedComplexColumn.java +++ b/processing/src/main/java/io/druid/segment/column/IndexedComplexColumn.java @@ -56,7 +56,7 @@ public Object getRowValue(int rowNum) } @Override - public void close() throws IOException + public void close() { } } diff --git a/processing/src/main/java/io/druid/segment/column/IndexedFloatsGenericColumn.java b/processing/src/main/java/io/druid/segment/column/IndexedFloatsGenericColumn.java index 5335945c3e49..160e4181e56c 100644 --- a/processing/src/main/java/io/druid/segment/column/IndexedFloatsGenericColumn.java +++ b/processing/src/main/java/io/druid/segment/column/IndexedFloatsGenericColumn.java @@ -23,8 +23,6 @@ import io.druid.segment.data.IndexedFloats; import io.druid.segment.data.IndexedLongs; -import java.io.IOException; - /** */ public class IndexedFloatsGenericColumn implements GenericColumn @@ -92,7 +90,7 @@ public IndexedLongs getLongMultiValueRow(int rowNum) } @Override - public void close() throws IOException + public void close() { column.close(); } diff --git a/processing/src/main/java/io/druid/segment/column/IndexedLongsGenericColumn.java b/processing/src/main/java/io/druid/segment/column/IndexedLongsGenericColumn.java index f0a25a96b19a..bc5691764959 100644 --- a/processing/src/main/java/io/druid/segment/column/IndexedLongsGenericColumn.java +++ b/processing/src/main/java/io/druid/segment/column/IndexedLongsGenericColumn.java @@ -23,8 +23,6 @@ import io.druid.segment.data.IndexedFloats; import io.druid.segment.data.IndexedLongs; -import java.io.IOException; - /** */ public class IndexedLongsGenericColumn implements GenericColumn @@ -92,7 +90,7 @@ public IndexedLongs getLongMultiValueRow(int rowNum) } @Override - public void close() throws IOException + public void close() { column.close(); } diff --git a/processing/src/main/java/io/druid/segment/column/SimpleColumn.java b/processing/src/main/java/io/druid/segment/column/SimpleColumn.java index f270021d6915..188b49750e53 100644 --- a/processing/src/main/java/io/druid/segment/column/SimpleColumn.java +++ b/processing/src/main/java/io/druid/segment/column/SimpleColumn.java @@ -20,7 +20,6 @@ package io.druid.segment.column; import com.google.common.base.Supplier; -import com.metamx.common.guava.CloseQuietly; /** */ @@ -62,14 +61,9 @@ public ColumnCapabilities getCapabilities() @Override public int getLength() { - GenericColumn column = null; - try { - column = genericColumn.get(); + try (final GenericColumn column = genericColumn.get()) { return column.length(); } - finally { - CloseQuietly.close(column); - } } @Override diff --git a/processing/src/main/java/io/druid/segment/data/BlockLayoutIndexedFloatSupplier.java b/processing/src/main/java/io/druid/segment/data/BlockLayoutIndexedFloatSupplier.java index 2e54eecd8ff6..edd7403e444f 100644 --- a/processing/src/main/java/io/druid/segment/data/BlockLayoutIndexedFloatSupplier.java +++ b/processing/src/main/java/io/druid/segment/data/BlockLayoutIndexedFloatSupplier.java @@ -20,12 +20,10 @@ package io.druid.segment.data; import com.google.common.base.Supplier; -import com.google.common.io.Closeables; import com.google.common.primitives.Floats; import com.metamx.common.guava.CloseQuietly; import io.druid.collections.ResourceHolder; -import java.io.IOException; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer; @@ -140,9 +138,11 @@ public String toString() } @Override - public void close() throws IOException + public void close() { - Closeables.close(holder, false); + if (holder != null) { + holder.close(); + } } } } diff --git a/processing/src/main/java/io/druid/segment/data/BlockLayoutIndexedLongSupplier.java b/processing/src/main/java/io/druid/segment/data/BlockLayoutIndexedLongSupplier.java index fd7ceaae9e4c..d79de2cfeb2f 100644 --- a/processing/src/main/java/io/druid/segment/data/BlockLayoutIndexedLongSupplier.java +++ b/processing/src/main/java/io/druid/segment/data/BlockLayoutIndexedLongSupplier.java @@ -20,11 +20,9 @@ package io.druid.segment.data; import com.google.common.base.Supplier; -import com.google.common.io.Closeables; import com.metamx.common.guava.CloseQuietly; import io.druid.collections.ResourceHolder; -import java.io.IOException; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.LongBuffer; @@ -175,9 +173,11 @@ public String toString() } @Override - public void close() throws IOException + public void close() { - Closeables.close(holder, false); + if (holder != null) { + holder.close(); + } } } } diff --git a/processing/src/main/java/io/druid/segment/data/EntireLayoutIndexedFloatSupplier.java b/processing/src/main/java/io/druid/segment/data/EntireLayoutIndexedFloatSupplier.java index 57df3e95c32f..14b806fee2a8 100644 --- a/processing/src/main/java/io/druid/segment/data/EntireLayoutIndexedFloatSupplier.java +++ b/processing/src/main/java/io/druid/segment/data/EntireLayoutIndexedFloatSupplier.java @@ -21,7 +21,6 @@ import com.google.common.base.Supplier; -import java.io.IOException; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer; @@ -82,7 +81,7 @@ public String toString() } @Override - public void close() throws IOException + public void close() { } } diff --git a/processing/src/main/java/io/druid/segment/data/EntireLayoutIndexedLongSupplier.java b/processing/src/main/java/io/druid/segment/data/EntireLayoutIndexedLongSupplier.java index fe7c09fe6599..6ddd8da10251 100644 --- a/processing/src/main/java/io/druid/segment/data/EntireLayoutIndexedLongSupplier.java +++ b/processing/src/main/java/io/druid/segment/data/EntireLayoutIndexedLongSupplier.java @@ -21,8 +21,6 @@ import com.google.common.base.Supplier; -import java.io.IOException; - public class EntireLayoutIndexedLongSupplier implements Supplier { @@ -80,7 +78,7 @@ public String toString() } @Override - public void close() throws IOException + public void close() { } } diff --git a/processing/src/main/java/io/druid/segment/data/IndexedFloats.java b/processing/src/main/java/io/druid/segment/data/IndexedFloats.java index 47a7642ae775..8a6f3f516518 100644 --- a/processing/src/main/java/io/druid/segment/data/IndexedFloats.java +++ b/processing/src/main/java/io/druid/segment/data/IndexedFloats.java @@ -29,4 +29,7 @@ public interface IndexedFloats extends Closeable public int size(); public float get(int index); public void fill(int index, float[] toFill); + + @Override + void close(); } diff --git a/processing/src/main/java/io/druid/segment/data/IndexedLongs.java b/processing/src/main/java/io/druid/segment/data/IndexedLongs.java index 8615178513e2..c9b1bc678150 100644 --- a/processing/src/main/java/io/druid/segment/data/IndexedLongs.java +++ b/processing/src/main/java/io/druid/segment/data/IndexedLongs.java @@ -29,4 +29,7 @@ public interface IndexedLongs extends Closeable public int size(); public long get(int index); public void fill(int index, long[] toFill); + + @Override + void close(); }