Package com.google.cloud.spanner
Class KeyRange
- java.lang.Object
-
- com.google.cloud.spanner.KeyRange
-
- All Implemented Interfaces:
Serializable
public final class KeyRange extends Object implements Serializable
Represents a range of rows in a table or index.A range has a start key and an end key. These keys can be open or closed, indicating if the range includes rows with that key.
For example, consider the following table definition:
CREATE TABLE UserEvents ( UserName STRING(MAX), EventDate STRING(10), ) PRIMARY KEY(UserName, EventDate);
The following keys name rows in this table:Key.of("Bob", "2014-09-23")
Key.of("Alfred", "2015-06-12")
UserEvents
table'sPRIMARY KEY
clause names two columns, eachUserEvents
key has two elements; the first is theUserName
, and the second is theEventDate
.Key ranges with multiple components are interpreted lexicographically by component using the table or index key's declared sort order. For example, the following range returns all events for user "Bob" that occurred in the year 2015:
KeyRange.closedClosed( Key.of("Bob", "2015-01-01"), Key.of("Bob", "2015-12-31"))
Start and end keys can omit trailing key components. This affects the inclusion and exclusion of rows that exactly match the provided key components: if the key is closed, then rows that exactly match the provided components are included; if the key is open, then rows that exactly match are not included.For example, the following range includes all events for "Bob" that occurred during and after the year 2000:
KeyRange.closedClosed( Key.of("Bob", "2000-01-01"), Key.of("Bob"))
The next example retrieves all events for "Bob":KeyRange.prefix(Key.of("Bob"))
To retrieve events before the year 2000:KeyRange.closedOpen( Key.of("Bob"), Key.of("Bob", "2000-01-01"))
The following range includes all rows in the table:KeyRange.all()
This range returns all users whoseUserName
begins with any character from A to C:KeyRange.closedOpen(Key.of("A"), Key.of("D"))
This range returns all users whoseUserName
begins with B:KeyRange.closedOpen(Key.of("B"), Key.of("C"))
Key ranges honor column sort order. For example, suppose a table is defined as follows:CREATE TABLE DescendingSortedTable { Key INT64, ... ) PRIMARY KEY(Key DESC);
The following range retrieves all rows with key values between 1 and 100 inclusive:KeyRange.closedClosed(Key.of(100), Key.of(1))
Note that 100 is passed as the start, and 1 is passed as the end, becauseKey
is a descending column in the schema.KeyRange
instances are immutable.- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
KeyRange.Builder
Builder forKeyRange
instances.static class
KeyRange.Endpoint
Defines whether a range includes or excludes its endpoint keys.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static KeyRange
closedClosed(Key start, Key end)
Returns a key range fromstart
inclusive toend
inclusive.static KeyRange
closedOpen(Key start, Key end)
Returns a key range fromstart
inclusive toend
exclusive.boolean
equals(Object o)
KeyRange.Endpoint
geEndType()
Indicates whether the end key is inclusive (CLOSED
) or exclusive (OPEN
).Key
getEnd()
Returns the end key of the range.Key
getStart()
Returns the start key of the range.KeyRange.Endpoint
getStartType()
Indicates whether the start key is inclusive (CLOSED
) or exclusive (OPEN
).int
hashCode()
static KeyRange.Builder
newBuilder()
Returns a new builder for constructing a range.static KeyRange
openClosed(Key start, Key end)
Returns a key range fromstart
exclusive toend
inclusive.static KeyRange
openOpen(Key start, Key end)
Returns a key range fromstart
exclusive toend
exclusive.static KeyRange
prefix(Key prefix)
Returns a key range that covers all keys where the firstprefix.size()
components matchprefix
exactly.KeyRange.Builder
toBuilder()
Returns a builder initialized with the value of this range.String
toString()
-
-
-
Method Detail
-
closedOpen
public static KeyRange closedOpen(Key start, Key end)
Returns a key range fromstart
inclusive toend
exclusive.
-
closedClosed
public static KeyRange closedClosed(Key start, Key end)
Returns a key range fromstart
inclusive toend
inclusive.
-
openOpen
public static KeyRange openOpen(Key start, Key end)
Returns a key range fromstart
exclusive toend
exclusive.
-
openClosed
public static KeyRange openClosed(Key start, Key end)
Returns a key range fromstart
exclusive toend
inclusive.
-
prefix
public static KeyRange prefix(Key prefix)
Returns a key range that covers all keys where the firstprefix.size()
components matchprefix
exactly.
-
newBuilder
public static KeyRange.Builder newBuilder()
Returns a new builder for constructing a range.
-
getStart
public Key getStart()
Returns the start key of the range.
-
getStartType
public KeyRange.Endpoint getStartType()
Indicates whether the start key is inclusive (CLOSED
) or exclusive (OPEN
).
-
getEnd
public Key getEnd()
Returns the end key of the range.
-
geEndType
public KeyRange.Endpoint geEndType()
Indicates whether the end key is inclusive (CLOSED
) or exclusive (OPEN
).
-
toBuilder
public KeyRange.Builder toBuilder()
Returns a builder initialized with the value of this range.
-
-