Top Description Fields Constructors Methods
org.h2.tools

public Class MultiDimension

extends Object
implements Comparator<long[]>
Class Inheritance
All Implemented Interfaces
java.util.Comparator
Imports
java.sql.PreparedStatement, .ResultSet, .SQLException, java.util.ArrayList, .Comparator, org.h2.util.StringUtils

A tool to help an application execute multi-dimensional range queries. The algorithm used is database independent, the only requirement is that the engine supports a range index (for example b-tree).

Field Summary

Modifier and TypeField and Description
private static final MultiDimension

Constructor Summary

AccessConstructor and Description
protected
MultiDimension()

Protected constructor

Method Summary

Modifier and TypeMethod and Description
private void
addMortonRanges(ArrayList<long[]> list, int[] min, int[] max, int len, int level)

private void
combineEntries(ArrayList<long[]>
list of pairs(low, high)
list
,
int
product of the gap lengths
total
)

Combine entries if the size of the list is too large.

public int
compare(long[]
the first object to be compared.
a
,
long[]
the second object to be compared.
b
)

Implements java.util.Comparator.compare.

Compares its two arguments for order.

public int

Returns:

the value
deinterleave
(int
the number of dimensions
dimensions
,
long
the scalar value
scalar
,
int
the dimension of the returned value (starting from 0)
dim
)

Gets one of the original multi-dimensional values from a scalar value.

private static int
findMiddle(int a, int b)

public String

Returns:

the query
generatePreparedQuery
(String
the table name
table
,
String
the column name of the computed scalar column
scalarColumn
,
String[]
the list of columns
columns
)

Generates an optimized multi-dimensional range query.

private static int
getBitsPerValue(int dimensions)

public static MultiDimension

Returns:

the singleton
getInstance
()

Get the singleton.

public int

Returns:

the maximum value
getMaxValue
(int
the number of dimensions
dimensions
)

Get the maximum value for the given dimension count.

private long[][]

Returns:

the list of ranges (low, high pairs)
getMortonRanges
(int[]
the minimum value
min
,
int[]
the maximum value
max
)

Gets a list of ranges to be searched for a multi-dimensional range query where min <= value <= max.

public ResultSet

Returns:

the result set
getResult
(PreparedStatement
the prepared statement
prep
,
int[]
the lower values
min
,
int[]
the upper values
max
)

Executes a prepared query that was generated using generatePreparedQuery.

private static int
getSize(int[] min, int[] max, int len)

public long

Returns:

the scalar value
interleave
(int...
the multi-dimensional value
values
)

Convert the multi-dimensional value into a one-dimensional (scalar) value.

public long

Returns:

the scalar value
interleave
(int
the value of the first dimension, normalized
x
,
int
the value of the second dimension, normalized
y
)

Convert the two-dimensional value into a one-dimensional (scalar) value.

public int

Returns:

the normalized value in the range 0..getMaxValue(dimensions)
normalize
(int
the number of dimensions
dimensions
,
double
the value (must be in the range min..max)
value
,
double
the minimum value
min
,
double
the maximum value (must be larger than min)
max
)

Normalize a value so that it is between the minimum and maximum for the given number of dimensions.

private static int
roundUp(int x, int blockSizePowerOf2)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

INSTANCEback to summary
private static final MultiDimension INSTANCE

Constructor Detail

MultiDimensionback to summary
protected MultiDimension()

Protected constructor

Method Detail

addMortonRangesback to summary
private void addMortonRanges(ArrayList<long[]> list, int[] min, int[] max, int len, int level)
combineEntriesback to summary
private void combineEntries(ArrayList<long[]> list, int total)

Combine entries if the size of the list is too large.

Parameters
list:ArrayList<long[]>

list of pairs(low, high)

total:int

product of the gap lengths

compareback to summary
public int compare(long[] a, long[] b)

Implements java.util.Comparator.compare.

Doc from java.util.Comparator.compare.

Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

The implementor must ensure that signum(compare(x, y)) == -signum(compare(y, x)) for all x and y. (This implies that compare(x, y) must throw an exception if and only if compare(y, x) throws an exception.)

The implementor must also ensure that the relation is transitive: ((compare(x, y)>0) && (compare(y, z)>0)) implies compare(x, z)>0.

Finally, the implementor must ensure that compare(x, y)==0 implies that signum(compare(x, z))==signum(compare(y, z)) for all z.

Parameters
a:long[]

the first object to be compared.

b:long[]

the second object to be compared.

Returns:int

a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

Annotations
@Override
deinterleaveback to summary
public int deinterleave(int dimensions, long scalar, int dim)

Gets one of the original multi-dimensional values from a scalar value.

Parameters
dimensions:int

the number of dimensions

scalar:long

the scalar value

dim:int

the dimension of the returned value (starting from 0)

Returns:int

the value

findMiddleback to summary
private static int findMiddle(int a, int b)
generatePreparedQueryback to summary
public String generatePreparedQuery(String table, String scalarColumn, String[] columns)

Generates an optimized multi-dimensional range query. The query contains parameters. It can only be used with the H2 database.

Parameters
table:String

the table name

scalarColumn:String

the column name of the computed scalar column

columns:String[]

the list of columns

Returns:String

the query

getBitsPerValueback to summary
private static int getBitsPerValue(int dimensions)
getInstanceback to summary
public static MultiDimension getInstance()

Get the singleton.

Returns:MultiDimension

the singleton

getMaxValueback to summary
public int getMaxValue(int dimensions)

Get the maximum value for the given dimension count. For two dimensions, each value must contain at most 32 bit, for 3: 21 bit, 4: 16 bit, 5: 12 bit, 6: 10 bit, 7: 9 bit, 8: 8 bit.

Parameters
dimensions:int

the number of dimensions

Returns:int

the maximum value

getMortonRangesback to summary
private long[][] getMortonRanges(int[] min, int[] max)

Gets a list of ranges to be searched for a multi-dimensional range query where min <= value <= max. In most cases, the ranges will be larger than required in order to combine smaller ranges into one. Usually, about double as many points will be included in the resulting range.

Parameters
min:int[]

the minimum value

max:int[]

the maximum value

Returns:long[][]

the list of ranges (low, high pairs)

getResultback to summary
public ResultSet getResult(PreparedStatement prep, int[] min, int[] max) throws SQLException

Executes a prepared query that was generated using generatePreparedQuery.

Parameters
prep:PreparedStatement

the prepared statement

min:int[]

the lower values

max:int[]

the upper values

Returns:ResultSet

the result set

Exceptions
SQLException:
on failure
getSizeback to summary
private static int getSize(int[] min, int[] max, int len)
interleaveback to summary
public long interleave(int... values)

Convert the multi-dimensional value into a one-dimensional (scalar) value. This is done by interleaving the bits of the values. Each values must be between 0 (including) and the maximum value for the given number of dimensions (getMaxValue, excluding). To normalize values to this range, use the normalize function.

Parameters
values:int[]

the multi-dimensional value

Returns:long

the scalar value

interleaveback to summary
public long interleave(int x, int y)

Convert the two-dimensional value into a one-dimensional (scalar) value. This is done by interleaving the bits of the values. Each values must be between 0 (including) and the maximum value for the given number of dimensions (getMaxValue, excluding). To normalize values to this range, use the normalize function.

Parameters
x:int

the value of the first dimension, normalized

y:int

the value of the second dimension, normalized

Returns:long

the scalar value

normalizeback to summary
public int normalize(int dimensions, double value, double min, double max)

Normalize a value so that it is between the minimum and maximum for the given number of dimensions.

Parameters
dimensions:int

the number of dimensions

value:double

the value (must be in the range min..max)

min:double

the minimum value

max:double

the maximum value (must be larger than min)

Returns:int

the normalized value in the range 0..getMaxValue(dimensions)

roundUpback to summary
private static int roundUp(int x, int blockSizePowerOf2)