Giter VIP home page Giter VIP logo

freestyle-cassandra's People

Contributors

47degfreestyle avatar adrianrafo avatar anamariamv avatar fedefernandez avatar gmadorell avatar raulraja avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

freestyle-cassandra's Issues

Story - Auto Insert/Update for case classes

Based on #14 we should add support for inserting and updating case classes.

Planned features:

  • Spike #48
  • case classes annotation for defining the table
  • insert/select operation on case classes that returns FS
  • index support
  • CREATE TABLE statement generator (and potentially an FS operation)

Research - Cluster information

Operations for fetching cluster information and health status

As part of this ticket we should create some coding issues

Add bind and setBytesUnsafe methods to LowLevelAPI

We need to add the following methods to LowLevelAPI to increase its safety:

Datastax driver code

public interface PreparedStatement {
  ...
    public BoundStatement bind();
  ...
}

public class BoundStatement  {
  ...
    public BoundStatement setBytesUnsafe(int i, ByteBuffer v) {
        return wrapper.setBytesUnsafe(i, v);
    }

    public BoundStatement setBytesUnsafe(String name, ByteBuffer v) {
        return wrapper.setBytesUnsafe(name, v);
    }
  ...
}

Freestyle-cassandra

trait LowLevelAPI {
  ...
  def bind: FS[BoundStatement]

  def setBytesUnsafe(index: Int, value: ByteBuffer): FS[BoundStatement]

  def setBytesUnsafe(name: String, value: ByteBuffer): FS[BoundStatement]
  ...
}

Abstract ByteBufferCodec over M[_]

As part of this ticket, we should also:

  trait MappedField {
    def name: String
    def mapField[M[_]](implicit M: MonadError[M, ByteBuffer]): ByteBuffer
  }
  • Modify the interpolator acordingly

This ticket depends on #71

Adds the option in the interpolator for executing the query without preparing the statement

Currently, in #80 we're making always a prepareStatement

We should add the option for executing the query without preparing the statement.

A possible solution could be to use SessionAPI.executeStatement and override the SimpleStatement class with:

import scala.collection.JavaConverters._
class ByteBufferSimpleStatement(query: String, values: List[ByteBuffer]) extends SimpleStatement(query, values.toArray) {
  override def getValues(protocolVersion: ProtocolVersion, codecRegistry: CodecRegistry): Array[ByteBuffer] = values.toArray
}

Story - Object mapper

  • Spike #46
  • FieldMapper (from case class to List[(String, ByteBuffer)]) - #38
  • Insert statement from a case class - #124
  • Populate a prepared statement with a case class instance - #125
  • Method for inserting case class instances - #126
  • Select statements from case classes - #127
  • Fetch case classes instances - #128
  • Unify all methods under the same class/trait

Start embed Cassandra from SBT

In order to test the interpolator, we need a Cassandra instance running before the test compilation. In this way, one approach could be to start and stop the Cassandra instance from SBT.

Also, finish the test MetadataInterpolatorTest in macros-tests project.

Delete operation

See #57

In this ticket, we should take a decision about how the delete operation will be written with the Datastax driver and/or LowLevelAPI.

Insert operations

See #57

In this ticket, we should take a decision about how the insert operation will be written with the Datastax driver and/or LowLevelAPI.

CRUD operations with the CQL Interpolator

This ticket depends on #57

We need to define how all these operations will be written with a CQL Interpolator and what features will be supported.

For example:
Select

cql"SELECT name FROM keyspace1.users WHERE id = $myId"

Aspects to consider:

  • The interpolator should accept variables, but how are the values decoded. Currently, we have a ByteBufferCodec definition, but the instances should be available in the scope
  • What is the value type returned by the interpolator? FreeS[F, ResultSet] for the selects?

Update operation

See #57

In this ticket, we should take a decision about how the delete operation will be written with the Datastax driver and/or LowLevelAPI.

Story - Schema validator

As part of this story we should:

  • Spike #47
  • Define the keyspace and table models - Issue #24
  • Add a mechanism (through implicits?) for retrieving schema information, providing different strategies (local schema sql, remote connection to the db, ...) - Issue #27
  • Add a schema parser for keyspaces - PR #15
  • Add a scheme parser for tables - Not needed (#49)
  • Implement a local schema sql provider - Issue #49
  • Implement a remote scheme provider - Issue #51

Basic CQL Interpolator

Implement a basic CQL interpolator based on propensive/contextual and with support for statements without arguments

Select operations

We want to give support for CRUD operations. All operations will be delegated to the LowLevelAPI. The LowLevelAPI allows the execution of all async operations of the Session object from the Datastax driver.

In this ticket, we should take a decision about how the select operation will be written with the Datastax driver and/or LowLevelAPI.

For example:

Select

# CQL
SELECT name FROM keyspace1.users WHERE id = <myId>
// Datastax code
val prepSt = session.prepare("SELECT name FROM keyspace1.users WHERE id = ?")
val boundStatement = prepSt.bind()
boundStatement.setBytesUnsafe("id", byteBufferCodec.serialize(myId))
val resultSet = session.execute(boundStatement)
def prepareStatement(implicit c: ByteBufferCodec[UUID]): FreeS[F, BoundStatement] =
  lowLevelAPI.prepare("SELECT name FROM keyspace1.users WHERE id = ?") map { st =>
    val boundStatement = st.bind()
    boundStatement.setBytesUnsafe("id", c.serialize(newUser.id))
    boundStatement
}

for {
  prepSt    <- prepareStatement
  resultSet <- lowLevelAPI.execute(prepSt)
} resultSet

See the freestyle-cassandra-example to learn about how to use LowLevelAPI

We should take care of what's the recommended way to make each operation in Cassandra

Spike - Object Mapper

Research and discussion about the Object Mapper feature.

Basically, we need to describe what features we want and how they should like (in code)

Update the freestyle-cassandra-example with an interpolator example

Using the version 0.0.1 we should add an example using the interpolator in the frees-io/freestyle-cassandra-example repo.

As part of this ticket, we should take the decision of creating a new project inside the repo or just a new main App.

I've tested the interpolator with this code, that can be used to create the example:

package freestyle.cassandra.sample

import java.util.UUID

import cats.instances.future._
import com.datastax.driver.core.{ProtocolVersion, ResultSet, Session, TypeCodec}
import freestyle._
import freestyle.FreeS
import freestyle.async.implicits._
import freestyle.asyncMonix.implicits._
import freestyle.cassandra.api.{ClusterAPI, SessionAPI}
import freestyle.cassandra.codecs._
import freestyle.cassandra.query.interpolator._
import freestyle.cassandra.query.interpolator.RuntimeCQLInterpolator._
import freestyle.cassandra.sample.Implicits._
import freestyle.implicits._
import monix.cats._
import monix.eval.{Task => MonixTask}
import monix.execution.Scheduler

import scala.collection.JavaConverters._
import scala.concurrent.Await
import scala.concurrent.duration.Duration
import scala.util.control.NonFatal

object Test extends App {

  implicit val executionContext: Scheduler = Scheduler.Implicits.global

  def connect[F[_]](implicit clusterAPI: ClusterAPI[F]): FreeS[F, Session] =
    clusterAPI.connectKeyspace("demodb")

  def closeSession[F[_]](implicit sessionAPI: SessionAPI[F]): FreeS[F, Unit] = sessionAPI.close

  def close[F[_]](implicit clusterAPI: ClusterAPI[F]): FreeS[F, Unit] = clusterAPI.close

  implicit val session: Session =
    Await.result(connect[ClusterAPI.Op].interpret[MonixTask].runAsync, Duration.Inf)

  val uuid = java.util.UUID.fromString("43aab350-f393-45be-a10e-d2244e45818c")

  implicit val uuidCodec: ByteBufferCodec[UUID] =
    freestyle.cassandra.codecs.byteBufferCodec(TypeCodec.uuid(), ProtocolVersion.V5)

  val task: MonixTask[ResultSet] = cql"SELECT id, name FROM User WHERE id = $uuid".asResultSet[MonixTask]
  val resultSet: ResultSet       = Await.result(task.runAsync, Duration.Inf)
  try {
    resultSet.iterator().asScala.foreach { row =>
      println(s"User ${row.get[java.util.UUID](0, TypeCodec.uuid()).toString}")
    }
  } catch {
    case NonFatal(t) =>
      t.printStackTrace()
  }

  Await.result(closeSession[SessionAPI.Op].interpret[MonixTask].runAsync, Duration.Inf)

  Await.result(close[ClusterAPI.Op].interpret[MonixTask].runAsync, Duration.Inf)

}

High-Level API algebra

Create an algebra with methods for inserting, updating and reading columns and tables and for modifying counts

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.