-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adding tests for renaming columns * Adding serialization tests for alter additions * Implementing list pagination * Adding truncation * Adding pagination tests * Adding a readme update * Cleaning up the implementation of timeuuid test records to prevent race conditions * Adding order iterator tests * Adding pagination that can deal explicitly with collections * Adding better docs for select queries
- Loading branch information
1 parent
90993a3
commit 6b3cd9b
Showing
15 changed files
with
410 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
### Select queries | ||
|
||
This explores all the various `SELECT` functionality in CQL as described in the official reference [here](http://cassandra.apache.org/doc/latest/cql/dml.html#select). | ||
Phantom aims to provide a 100% mapping with all the latest CQL features, but if you find anything that you need missing, | ||
please help us by reporting it through GitHub issues. | ||
|
||
#### Pre-requisites and setup. | ||
|
||
To better explain the document to follow, it is easier if we reach common ground by refering to the same Cassandra table. | ||
The below example is a simple Cassandra table with a more complex key that allows us to explore all the features of phantom | ||
and demonstrate the available select API. | ||
|
||
We will create a `AnalyticsEntries` table, where we hold information about a car's state over time for 2 properties we | ||
care about, namely the `velocity` and `tirePressure`. We leverage the `TimeUUID` Cassandra type to store information | ||
about timestamps, order the logs we receive in descending order(most recent record first), and prevent any colissions. | ||
|
||
If we would just use a timestamp type, if we were to receive two logs for the same car at the exact same timestamp, | ||
the entries would override each other in Cassandra, because in effect they would have the same partition key | ||
and the same clustering key, so the whole primary key would be identical. | ||
|
||
```scala | ||
import com.outworkers.phantom.dsl._ | ||
|
||
case class CarMetric( | ||
car: UUID, | ||
id: UUID, | ||
velocity: Double, | ||
tirePressure: Double | ||
) | ||
|
||
abstract class AnalyticsEntries extends CassandraTable[AnalyticsEntries, CarMetric] with RootConnector { | ||
object car extends UUIDColumn(this) with PartitionKey | ||
object id extends TimeUUIDColumn(this) with ClusteringOrder with Descending | ||
object velocity extends DoubleColumn(this) | ||
object tirePressure extends DoubleColumn(this) | ||
} | ||
|
||
``` | ||
|
||
#### Available query methods. | ||
|
||
The following is the list of available query methods on a select, and it can be used to leverage standard `SELECT` functionality | ||
in various ways. | ||
|
||
|
||
| Method name | Return type | Purpose | | ||
| =========== | =========== | ======= | | ||
|
||
|
||
|
||
##### | ||
|
||
|
||
|
||
#### Paginating results by leveraging paging states and automated Cassandra pagination. | ||
|
||
There are situations where you can not retrieve a whole list of results in a single go, and for that reason | ||
Cassandra offers paging states and automated pagination. Phantom makes that functionality available through a set of overloaded | ||
methods called `paginateRecord`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
phantom-dsl/src/main/scala/com/outworkers/phantom/builder/ops/Alteration.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2013 - 2017 Outworkers Ltd. | ||
* | ||
* Licensed 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 com.outworkers.phantom.builder.ops | ||
|
||
import java.nio.ByteBuffer | ||
import java.util.UUID | ||
|
||
import org.joda.time.DateTime | ||
|
||
/** | ||
* Typeclass used to prevent invalid type alterations at compile time. | ||
* Cassandra only allows for specific changes of column types using ALTER Query | ||
* which means we need to somehow enforce the existing mechanism. | ||
* | ||
* We do so by requesting compile time implicit evidence that any type alteration must supply, | ||
* e.g. that Source and Target have a predefined allowed alteration. | ||
* | ||
* The list of allowed ops is found here: [[[https://docs.datastax.com/en/cql/3.1/cql/cql_reference/alter_table_r.html]]. | ||
* | ||
* @tparam Source The source type of the column. | ||
* @tparam Target The target type of the column. | ||
*/ | ||
sealed trait Alteration[Source, Target] | ||
|
||
trait Alterations { | ||
implicit object IntToVarint extends Alteration[Int, BigInt] | ||
|
||
implicit object TimeUUIDToVarint extends Alteration[UUID, UUID] | ||
|
||
implicit object DoubleToBlob extends Alteration[Double, ByteBuffer] | ||
implicit object IntToBlob extends Alteration[Int, ByteBuffer] | ||
implicit object TimestampToBlob extends Alteration[DateTime, ByteBuffer] | ||
implicit object UUIDToBlob extends Alteration[UUID, ByteBuffer] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.