Specifies a subset of all possible keys values. A KeyFilter
can be used with the {@link Exchange#traverse(Key.Direction,KeyFilter,int)}method to restrict the set of key values within a Persistit Tree
that will actually be traversed.
A KeyFilter
provides two primary methods:
Key
is a member of the subset specified by this filter, andKey
to the nextlarger or smaller key value that lies within the range specified by this filter.Tree
.
KeyFilter
consists of an array of one or more terms. Each term corresponds to one segment in a key value that will be selected or excluded by this KeyFilter
. The Kth term in the list applies to the Kth segment of a key. There are three kinds of term:
The static methods {@link #simpleTerm(Object)}, {@link #rangeTerm(Object,Object)}, {@link #rangeTerm(Object,Object,CoderContext)}, {@link #rangeTerm(Object,Object,boolean,boolean)}, {@link #rangeTerm(Object,Object,boolean,boolean,CoderContext)}, and {@link #orTerm} produce these various kinds of Term
s
For example, consider a key consisting of three segments: a last name, a first name and a person ID number for a person. Such a key value might be constructed with code such as this:
Suppose we now want to enumerate all members of a tree having keys of this form with last names falling between "M" and "Q", and first names equal to either "Alice" or "Bob". The following code constructs a key.clear().append("McDonald").append("Bob").append(12345);
KeyFilter
for this purpose:
The first term specifies a range that includes any last name that sorts alphabetically between "M" and "Q" (inclusively). The second term is an OrTerm that selects the first names "Alice" or "Bob". KeyFilter keyFilter = new KeyFilter(); keyFilter = keyFilter.append(KeyFilter.rangeTerm("M", "Q")); keyFilter = keyFilter.append(KeyFilter.orTerm(new KeyFilter.Term[]{ KeyFilter.simpleTerm("Alice"), KeyFilter.simpleTerm("Bob"))});
A RangeTerm optionally specifies whether the end-points are inclusive. For example, the term
includes the name "Jones" and all names that follow up to, but not including, "Smith". If unspecified, the end-points of the range are included. KeyFilter.rangeTerm("Jones", "Smith", true, false)
KeyFilter
may also specify minimum depth, maximum depth, or both. These values control the number of segments that must be present in key value in order for it to be selected. A KeyFilter
will select a key only if the number of segments in the key lies between the minimum depth and the maximum depth, inclusive. KeyFilter
. For example, the string representation of the filter constructed in above is {"M":"Q",{"Alice","Bob"}}
You can construct a KeyFilter
from its string representation with the {@link KeyParser#parseKeyFilter} method. For example, the followingcode generates an equivalent KeyFilter
: KeyParser parser = new KeyParser("{\"M\":\"Q\",{\"Alice\",\"Bob\"}}; KeyFilter filter = parser.parseKeyFilter();
As a convenience, the constructor {@link #KeyFilter(String)} automaticallycreates and invokes a KeyParser
to create a KeyFilter
from its string representation. Following is an informal grammar for the string representation of a key filter. See string representation in {@link Key} for information on how to specify a keysegment value.
A range may omit either the starting segment value or the ending segment value. When the starting segment value is omitted, the range starts before the first possible key value, and when the ending segment value is omitted, the range ends after the last possible key value. Thus the range specification keyFilter ::= '{' termElement [',' termElement]... '}' termElement ::= [ '>' ] term [ '<' ] term ::= segment | range | qualifiedRange | orTerm segment ::= see segment range ::= segment ':' segment | ':' segment | segment ':' qualifiedRange = ('(' | '[') range (')' | ']') orTerm ::= '{' term [',' term ]...'}'
include every key with a first segment value of "Smith" or above. Similarly, {"Smith":}
includes all keys up to and including "Smith". {:"Smith"}
A qualifiedRange allows you to specify whether the end-points of a range are included or excluded from the selected subset. A square bracket indicates that the end-point is included, while a parenthesis indicates that it is excluded. For example
does not include "Jones" but does include "Smith". An unqualified range specification such as {("Jones":"Smith"]}
includes both end-points. It is equivelent to {"Jones":"Smith"}
{["Jones":"Smith"]}
Within the string representation of a KeyFilter
at most one term element may specify the prefix ">" (greater-than sign), and at most one term element may specify the suffix "<" (less-than sign). These denote the minimum and maximum depths of the KeyFilter
, respectively. The minimum depth is the count of term elements up to and including the term marked with a ">" and the maximum depth is the count of terms up to and including the term marked with a ">". For example, in the KeyFilter
represented by the string
the minimum depth is 2 and the maximum depth is 3. {*,>100:200,*<}
A KeyFilter
is immutable. The methods {@link #append(KeyFilter)}, {@link #append(KeyFilter.Term)}, {@link #append(KeyFilter.Term[])}, create new KeyFilter
s with additional terms supplied by the supplied KeyFilter
, Term
or array of Term
s. The {@link #limit} method creates a new KeyFilter
with modifiedminimum and maximum depth values. Each of these methods returns a new KeyFilter
which results from combining the original KeyFilter
with the supplied information.
A KeyFilter defines a subset of the the set of all Key values: the {@link Exchange#traverse(Key.Direction,KeyFilter,int)} method returns onlyvalues in this subset. The following definitions are used in describing the behavior of the {@link #next(Key,Key.Direction)} method.
traverse
excludes the supplied key. LTEQ and GTEQ are inclusive. A KeyFilter defines the range R as zero or more contiguous subsets of S. The next
method is used to assist the traverse
method by skipping efficiently over keys that are not in contained in R. Given a {@link Key} K and a {@link Key.Direction} D, the method behaves as follows:
true
without modifying the K.true
to indicate that more keys exist in the range. Specifically, for each Direction D, K' is defined as follows: false
to indicate that the range has been exhausted.
|
|
|
|
|
|
|
|
|
|
|
|
|
|