Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 152 additions & 0 deletions api/src/main/java/io/druid/data/ValueType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*
* 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.data;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.google.common.collect.Ordering;
import com.google.common.primitives.Doubles;
import com.google.common.primitives.Floats;
import com.google.common.primitives.Longs;

import java.util.Comparator;

/**
*/
public enum ValueType
{
FLOAT {
@Override
public Class classOfObject()
{
return Float.TYPE;
}

@Override
public Comparator<Float> comparator()
{
return new Comparator<Float>()
{
@Override
public int compare(Float o1, Float o2)
{
return Floats.compare(o1, o2);
}
};
}
},
LONG {
@Override
public Class classOfObject()
{
return Long.TYPE;
}

@Override
public Comparator<Long> comparator()
{
return new Comparator<Long>()
{
@Override
public int compare(Long o1, Long o2)
{
return Longs.compare(o1, o2);
}
};
}
},
DOUBLE {
@Override
public Class classOfObject()
{
return Double.TYPE;
}

@Override
public Comparator<Double> comparator()
{
return new Comparator<Double>()
{
@Override
public int compare(Double o1, Double o2)
{
return Doubles.compare(o1, o2);
}
};
}
},
STRING {
@Override
public Class classOfObject()
{
return String.class;
}

public Comparator<String> comparator()
{
return Ordering.natural().nullsFirst();
}
},
COMPLEX {
@Override
public Class classOfObject()
{
return Object.class;
}

@Override
public Comparator comparator()
{
throw new UnsupportedOperationException();
}
};

public abstract Class classOfObject();

public abstract Comparator comparator();

@JsonValue
@Override
public String toString()
{
return this.name().toUpperCase();
}

@JsonCreator
public static ValueType fromString(String name)
{
return valueOf(name.toUpperCase());
}

public static ValueType of(String name)
{
try {
return name == null ? COMPLEX : fromString(name);
}
catch (IllegalArgumentException e) {
return COMPLEX;
}
}

public static boolean isNumeric(ValueType type)
{
return type == DOUBLE || type == FLOAT || type == LONG;
}
}
25 changes: 23 additions & 2 deletions api/src/main/java/io/druid/data/input/MapBasedRow.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.metamx.common.IAE;
import com.metamx.common.logger.Logger;
import com.metamx.common.parsers.ParseException;
import org.joda.time.DateTime;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -134,6 +132,29 @@ public float getFloatMetric(String metric)
}
}

@Override
public double getDoubleMetric(String metric)
{
Object metricValue = event.get(metric);

if (metricValue == null) {
return 0.0d;
}

if (metricValue instanceof Number) {
return ((Number) metricValue).doubleValue();
} else if (metricValue instanceof String) {
try {
return Double.valueOf(((String) metricValue).replace(",", ""));
}
catch (Exception e) {
throw new ParseException(e, "Unable to parse metrics[%s], value[%s]", metric, metricValue);
}
} else {
throw new ParseException("Unknown type[%s]", metricValue.getClass());
}
}

@Override
public long getLongMetric(String metric)
{
Expand Down
3 changes: 2 additions & 1 deletion api/src/main/java/io/druid/data/input/Row.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.metamx.common.parsers.ParseException;
import org.joda.time.DateTime;

import java.util.List;
Expand Down Expand Up @@ -82,6 +81,8 @@ public interface Row extends Comparable<Row>
*/
public float getFloatMetric(String metric);

public double getDoubleMetric(String metric);

/**
* Returns the long value of the given metric column.
* <p/>
Expand Down
31 changes: 1 addition & 30 deletions api/src/main/java/io/druid/data/input/impl/DimensionSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,12 @@

package io.druid.data.input.impl;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.databind.jsontype.NamedType;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.google.common.base.Preconditions;

import java.util.List;
import io.druid.data.ValueType;

/**
*/
Expand All @@ -47,30 +42,6 @@ public abstract class DimensionSchema
public static final String FLOAT_TYPE_NAME = "float";
public static final String SPATIAL_TYPE_NAME = "spatial";


// main druid and druid-api should really use the same ValueType enum.
// merge them when druid-api is merged back into the main repo
public enum ValueType
{
FLOAT,
LONG,
STRING,
COMPLEX;

@JsonValue
@Override
public String toString()
{
return this.name().toUpperCase();
}

@JsonCreator
public static ValueType fromString(String name)
{
return valueOf(name.toUpperCase());
}
}

private final String name;

protected DimensionSchema(String name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.druid.data.ValueType;

public class FloatDimensionSchema extends DimensionSchema
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.druid.data.ValueType;

public class LongDimensionSchema extends DimensionSchema
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.druid.data.ValueType;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.druid.data.ValueType;

public class StringDimensionSchema extends DimensionSchema
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* 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.benchmark;

// Run DoubleCompressionBenchmarkFileGenerator to generate the required files before running this benchmark

import com.google.common.base.Supplier;
import com.google.common.io.Files;
import io.druid.segment.data.CompressedDoublesIndexedSupplier;
import io.druid.segment.data.IndexedDoubles;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole;

import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@State(Scope.Benchmark)
@Fork(value = 1)
@Warmup(iterations = 10)
@Measurement(iterations = 25)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public class DoubleCompressionBenchmark
{
@Param("doubleCompress/")
private static String dirPath;

@Param({"enumerate", "zipfLow", "zipfHigh", "sequential", "uniform"})
private static String file;

@Param({"lz4", "none"})
private static String strategy;

private Random rand;
private Supplier<IndexedDoubles> supplier;

@Setup
public void setup() throws Exception
{
File dir = new File(dirPath);
File compFile = new File(dir, file + "-" + strategy);
rand = new Random();
ByteBuffer buffer = Files.map(compFile);
supplier = CompressedDoublesIndexedSupplier.fromByteBuffer(buffer, ByteOrder.nativeOrder());
}

@Benchmark
public void readContinuous(Blackhole bh) throws IOException
{
IndexedDoubles indexedDoubles = supplier.get();
int count = indexedDoubles.size();
double sum = 0;
for (int i = 0; i < count; i++) {
sum += indexedDoubles.get(i);
}
bh.consume(sum);
indexedDoubles.close();
}

@Benchmark
public void readSkipping(Blackhole bh) throws IOException
{
IndexedDoubles indexedDoubles = supplier.get();
int count = indexedDoubles.size();
double sum = 0;
for (int i = 0; i < count; i += rand.nextInt(2000)) {
sum += indexedDoubles.get(i);
}
bh.consume(sum);
indexedDoubles.close();
}

}
Loading