Class NumberRangePrefixTree
java.lang.Object
org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree
org.apache.lucene.spatial.prefix.tree.NumberRangePrefixTree
- Direct Known Subclasses:
DateRangePrefixTree
A SpatialPrefixTree for single-dimensional numbers and number ranges of fixed precision values
(not floating point). Despite its name, the indexed values (and queries) need not actually be
ranges, they can be unit instance/values.
Why might you use this instead of Lucene's built-in integer/long support? Here are some reasons with features based on code in this class, or are possible based on this class but require a subclass to fully realize it.
- Index ranges, not just unit instances. This is especially useful when the requirement calls for a multi-valued range.
- Instead of a fixed "precisionStep", this prefixTree can have a customizable number of child values for any prefix (up to 32768). This allows exact alignment of the prefix-tree with typical/expected values, which results in better performance. For example in a Date implementation, every month can get its own dedicated prefix, every day, etc., even though months vary in duration.
- Arbitrary precision, like
BigDecimal. - Standard Lucene integer/long indexing always indexes the full precision of those data types but this one is customizable.
Shapes created by the methods on this class, not from any SpatialContext.- See Also:
- WARNING: This API is experimental and might change in incompatible ways in the next release.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected classMost of the PrefixTree implementation is in this one class, which is both the Cell, the CellIterator, and the Shape to reduce object allocation.static interfaceBase interface forShapes this prefix tree supports.classA range Shape; based on a pair ofNumberRangePrefixTree.UnitNRShape.static interfaceA unit value Shape implemented as a stack of numbers, one for each level in the prefix tree. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final int[]protected final int[]protected final intprotected final int[]Fields inherited from class org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree
ctx, maxLevels -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected static intCompares a to b, returning less than 0, 0, or greater than 0, if a is less than, equal to, or greater than b, respectively, up to their common prefix (i.e.doublegetDistanceForLevel(int level) Given a cell having the specified level, returns the distance from opposite corners.intgetLevelForDistance(double dist) Returns the level of the largest grid in which its longest side is less than or equal to the provided distance (in degrees).intReturns the number of sub-cells beneath the given UnitNRShape.Returns the level 0 cell which encompasses all spatial data.protected NumberRangePrefixTree.NRCell[]newCellStack(int levels) parseShape(String str) Detects a range pattern and parses it, otherwise it's parsed as one shape viaparseUnitShape(String).protected abstract NumberRangePrefixTree.UnitNRShapeparseUnitShape(String str) Parse a String to a UnitNRShape.This creates a new Cell (or re-usingscratchif provided), initialized to the state as read by the bytes.abstract ObjectConverts a UnitNRShape shape to the corresponding type supported by this class, such as a Calendar/BigDecimal.toRangeShape(NumberRangePrefixTree.UnitNRShape startUnit, NumberRangePrefixTree.UnitNRShape endUnit) Returns a shape that represents the continuous range betweenstartandend.protected NumberRangePrefixTree.UnitNRShapetoShape(int[] valStack, int len) toString()protected abstract StringA string representation of the UnitNRShape that is parse-able byparseUnitShape(String).protected static Stringabstract NumberRangePrefixTree.UnitNRShapetoUnitShape(Object value) Converts the value to a unit shape.Methods inherited from class org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree
getMaxLevels, getSpatialContext, getTreeCellIterator
-
Field Details
-
maxSubCellsByLevel
protected final int[] maxSubCellsByLevel -
termLenByLevel
protected final int[] termLenByLevel -
levelByTermLen
protected final int[] levelByTermLen -
maxTermLen
protected final int maxTermLen
-
-
Constructor Details
-
NumberRangePrefixTree
protected NumberRangePrefixTree(int[] maxSubCellsByLevel)
-
-
Method Details
-
toUnitShape
Converts the value to a unit shape. Doesn't parse strings; seeparseShape(String)for that. This is the reverse oftoObject(org.apache.lucene.spatial.prefix.tree.NumberRangePrefixTree.UnitNRShape). -
toRangeShape
public NumberRangePrefixTree.NRShape toRangeShape(NumberRangePrefixTree.UnitNRShape startUnit, NumberRangePrefixTree.UnitNRShape endUnit) Returns a shape that represents the continuous range betweenstartandend. It will be normalized, and so sometimes aNumberRangePrefixTree.UnitNRShapewill be returned, other times aNumberRangePrefixTree.SpanUnitsNRShapewill be.- Throws:
IllegalArgumentException- if the arguments are in the wrong order, or if either contains the other (yet they aren't equal).
-
toObject
Converts a UnitNRShape shape to the corresponding type supported by this class, such as a Calendar/BigDecimal. This is the reverse oftoUnitShape(Object). -
toString
A string representation of the UnitNRShape that is parse-able byparseUnitShape(String). -
toStringUnitRaw
-
parseShape
Detects a range pattern and parses it, otherwise it's parsed as one shape viaparseUnitShape(String). The range pattern looks like this BNF:'[' + parseShapeLV + ' TO ' + parseShapeLV + ']'
It's the same thing as the toString() of the range shape, notwithstanding range optimization.- Parameters:
str- not null or empty- Returns:
- not null
- Throws:
ParseException- If there is a problem
-
parseUnitShape
protected abstract NumberRangePrefixTree.UnitNRShape parseUnitShape(String str) throws ParseException Parse a String to a UnitNRShape. "*" should be the full-range (level 0 shape).- Throws:
ParseException
-
comparePrefix
protected static int comparePrefix(NumberRangePrefixTree.UnitNRShape a, NumberRangePrefixTree.UnitNRShape b) Compares a to b, returning less than 0, 0, or greater than 0, if a is less than, equal to, or greater than b, respectively, up to their common prefix (i.e. only min(a.levels,b.levels) are compared).- NOTE: This API is for internal purposes only and might change in incompatible ways in the next release.
-
toString
- Overrides:
toStringin classSpatialPrefixTree
-
getLevelForDistance
public int getLevelForDistance(double dist) Description copied from class:SpatialPrefixTreeReturns the level of the largest grid in which its longest side is less than or equal to the provided distance (in degrees). Consequentlydistacts as an error epsilon declaring the amount of detail needed in the grid, such that you can get a grid with just the right amount of precision.- Specified by:
getLevelForDistancein classSpatialPrefixTree- Parameters:
dist->= 0- Returns:
- level [1 to maxLevels]
-
getDistanceForLevel
public double getDistanceForLevel(int level) Description copied from class:SpatialPrefixTreeGiven a cell having the specified level, returns the distance from opposite corners. Since this might vary depending on where the cell is, this method may over-estimate.- Specified by:
getDistanceForLevelin classSpatialPrefixTree- Parameters:
level- [1 to maxLevels]- Returns:
> 0
-
toShape
-
getWorldCell
Description copied from class:SpatialPrefixTreeReturns the level 0 cell which encompasses all spatial data. Equivalent toSpatialPrefixTree.readCell(BytesRef,Cell)with no bytes.- Specified by:
getWorldCellin classSpatialPrefixTree
-
newCellStack
-
readCell
Description copied from class:SpatialPrefixTreeThis creates a new Cell (or re-usingscratchif provided), initialized to the state as read by the bytes. Warning: An implementation may refer to the same byte array (no copy). IfCell.setLeaf()is subsequently called, it would then modify these bytes.- Specified by:
readCellin classSpatialPrefixTree
-
getNumSubCells
Returns the number of sub-cells beneath the given UnitNRShape.
-