Skip to content

Commit

Permalink
Cleaning up api
Browse files Browse the repository at this point in the history
  • Loading branch information
kclay committed Nov 20, 2015
1 parent 4d61d66 commit 5e0f914
Show file tree
Hide file tree
Showing 14 changed files with 136 additions and 109 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ SBT Users
```scala
val main = Project(....).settings(resolvers ++= Seq("RethinkScala Repository" at "http://kclay.github.io/releases"))

val rethinkscala = "com.rethinkscala" %% "core" % "0.4.7",
val rethinkscala = "com.rethinkscala" %% "core" % "0.4.8-SNAPSHOT"
val rethinkscala = "com.rethinkscala" %% "core" % "0.5.0",
val rethinkscala = "com.rethinkscala" %% "core" % "0.5.1-SNAPSHOT"
```
To get started
```scala
Expand Down Expand Up @@ -112,14 +112,18 @@ sbt compile

Version
-
###0.5.0 - 11/08/15
- Update to Scala 2.11.7, SBT 0.13.8, and target JVM 1.8
###0.5.0 - 11/19/15
- Update to Scala 2.11.7, SBT 0.13.8, and target JVM 1.8 (thanks @crockpotveggies)
- Added scalaz-stream support
- Refactored connections to have `Backends`, this will allow switching connection/stream implementations
- Updated to support rethink API 2.0


###0.4.6 - 05/12/15
- Polymorphism support #23
- No need to extend `Document` anymore #19
- Breaking Change - table.getAll(indes:String,attr:Any*) is now table.getAll(attr:Any*).withIndex(index:String)
- Breaking Change - Updated table.indexCreate defination
- Breaking Change - Updated table.indexCreate definition
- Support for Continuing a stream
- Added functional blocking delegate which uses `Try`
- Updated Netty.io to 4.0.27.Final
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/scala/com/rethinkscala/Delegate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.rethinkscala.backend.netty.async.AsyncDelegate
import com.rethinkscala.backend.netty.blocking.BlockingDelegate
import com.rethinkscala.net._

import com.rethinkscala.backend.{Connection => BackendConnection}
/**
* Created by IntelliJ IDEA.
* User: Keyston
Expand All @@ -20,7 +21,7 @@ case class ResultExtractor[T](cursorFactory: CursorFactory, manifest: Manifest[T
}

object Delegate {
def apply[T](produce: Produce[T], connection: Connection): backend.Delegate[T] = connection match {
def apply[T](produce: Produce[T], connection: BackendConnection): backend.Delegate[T] = connection match {
case c: BlockingConnection => apply(produce, c)
case c: AsyncConnection => apply(produce, c)
}
Expand Down
105 changes: 58 additions & 47 deletions core/src/main/scala/com/rethinkscala/Implicits.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.rethinkscala

import com.rethinkscala.ast._
import com.rethinkscala.backend.netty
import com.rethinkscala.changefeeds.ast.Changes
import com.rethinkscala.magnets.ReceptacleImplicits
import com.rethinkscala.net._

import scala.collection.Iterable
import scala.concurrent.Future
import com.rethinkscala.backend.{Connection => BackendConnection}


/** Created with IntelliJ IDEA.
Expand Down Expand Up @@ -210,15 +212,7 @@ trait DefaultValue[T] {
}


private[rethinkscala] trait ImplicitConversions {

object RethinkApi extends RethinkApi

implicit def anyToPimpled(v: Any): PimpedAny = new PimpedAny(v)


implicit def collectionToAst[T](coll: Iterable[T]): MakeArray[T] = Expr[T](coll)

trait LiteralConversions {
implicit def intToDatNum(i: Int): NumberDatum = NumberDatum(i)

implicit def longToDatNum(l: Long): NumberDatum = NumberDatum(l)
Expand All @@ -235,21 +229,18 @@ private[rethinkscala] trait ImplicitConversions {
implicit def toOptLiteral[T <% Literal](v: T): Option[T] = Some(v)

implicit def toOptFromDatum[T <% Datum](v: T): Option[T] = Some(v)
}

implicit def toOptFromWrappedValue[T <: WrappedValue[_]](v: T): Option[T] = Some(v)
trait FunctionConversions {

implicit def toPredicate1Opt(f: (Var) => Typed): Option[Predicate1] = Some(new ScalaPredicate1(f))

implicit def toPredicate2Opt(f: (Var, Var) => Typed): Option[Predicate2] = Some(new ScalaPredicate2(f))


implicit def toPredicate1(f: Var => Typed): Predicate1 = new ScalaPredicate1(f)

implicit def toPredicate2(f: (Var, Var) => Typed): ScalaPredicate2 = new ScalaPredicate2(f)


implicit def map2Typed(m: Map[String, Any]): Typed = Expr(m)

implicit def untypedPredicateToTyped(f: Var => Map[String, Any]): Predicate1 = (v: Var) => Expr(f(v))


Expand All @@ -258,6 +249,28 @@ private[rethinkscala] trait ImplicitConversions {

implicit def toBooleanPredicate2(f: (Var, Var) => Binary): BooleanPredicate2 = new ScalaBooleanPredicate2(f)

}

final class SliceSupport(val start: Int) extends AnyVal {
def ~>(end: Int): SliceRange = SliceRange(start, end)
}

private[rethinkscala] trait ImplicitConversions extends LiteralConversions with FunctionConversions {


implicit def anyToPimpled(v: Any): PimpedAny = new PimpedAny(v)


implicit def collectionToAst[T](coll: Iterable[T]): MakeArray[T] = Expr[T](coll)


implicit def toOptFromWrappedValue[T <: WrappedValue[_]](v: T): Option[T] = Some(v)


implicit def map2Typed(m: Map[String, Any]): Typed = Expr(m)


implicit def string2DB(name: String): DB = DB(name)

implicit def bool2Option(value: Boolean): Option[Boolean] = Some(value)

Expand All @@ -267,14 +280,12 @@ private[rethinkscala] trait ImplicitConversions {

implicit def int2Option(value: Int): Option[Int] = Some(value)

implicit def string2DB(name: String): DB = DB(name)

case class String2Ast(name: String) {
def row = RethinkApi.row(name)
def row: ProduceAny = RethinkApi.row(name)

def asc = Asc(name.wrap)
def asc: Asc = Asc(name.wrap)

def desc = Desc(name.wrap)
def desc: Desc = Desc(name.wrap)
}

private def p2t(p: Product): MakeArray[Any] = Expr(p.productIterator.toSeq)
Expand All @@ -289,9 +300,9 @@ private[rethinkscala] trait ImplicitConversions {

implicit def string2Ordering(name: String): Asc = name.asc

implicit def intWithTildyArrow(start: Int) = new {
def ~>(end: Int) = SliceRange(start, end)
}

implicit def intWithTildyArrow(start: Int): SliceSupport = new SliceSupport(start)


implicit def func2Order(f: Var => Typed): Order = new FuncWrap(new ScalaPredicate1(f)) with Order

Expand All @@ -306,34 +317,34 @@ trait FromAst[T] {
trait Helpers {


object backends {
object Backends {


import com.rethinkscala.backend.netty

val Blocking = netty.blocking.BlockingBackend
val Async = netty.async.AsyncBackend
}

type Var = com.rethinkscala.ast.Var
val Expr = com.rethinkscala.ast.Expr
val Blocking = backends.Blocking.profile
val Async = backends.Async.profile
type BlockingConnection = backends.Blocking.ConnectionDef
type AsyncConnection = backends.Async.ConnectionDef
val AsyncConnection = backends.Async.Connection
val BlockingConnection = backends.Blocking.Connection
type BlockResult[T] = backends.Blocking.Result[T]
type AsyncResult[T] = backends.Async.Result[T]
val Blocking = Backends.Blocking.profile
val Async = Backends.Async.profile
type BlockingConnection = Backends.Blocking.ConnectionDef
type AsyncConnection = Backends.Async.ConnectionDef
val AsyncConnection = Backends.Async.Connection
val BlockingConnection = Backends.Blocking.Connection
type BlockResult[T] = Backends.Blocking.Result[T]
type AsyncResult[T] = Backends.Async.Result[T]

def async[T](p: Produce[T])(implicit c: Connection, extractor: ResultExtractor[T]): AsyncResult[T] = async(_.apply(p))
def async[T](p: Produce[T])(implicit c: BackendConnection, extractor: ResultExtractor[T]): AsyncResult[T] = async(_.apply(p))

def async[T](f: AsyncConnection => Future[T])(implicit c: Connection): AsyncResult[T] = f(AsyncConnection(c))
def async[T](f: AsyncConnection => Future[T])(implicit c: BackendConnection): AsyncResult[T] = f(AsyncConnection(c))

def block[T](f: BlockingConnection => Unit)(implicit c: Connection): Unit = f(BlockingConnection(c))
def block[T](f: BlockingConnection => Unit)(implicit c: BackendConnection): Unit = f(BlockingConnection(c))

def block[T](f: BlockingConnection => BlockResult[T])(implicit c: Connection): BlockResult[T] = f(BlockingConnection(c))
def block[T](f: BlockingConnection => BlockResult[T])(implicit c: BackendConnection): BlockResult[T] = f(BlockingConnection(c))

def block[T: Manifest](p: Produce[T])(implicit c: Connection): BlockResult[T] = {
def block[T: Manifest](p: Produce[T])(implicit c: BackendConnection): BlockResult[T] = {

block { bc: BlockingConnection =>
implicit val extractor = bc.resultExtractorFactory.create[T]
Expand All @@ -345,30 +356,30 @@ trait Helpers {

class PimpedAny(val v: Any) extends AnyVal {
@inline
def wrap = FuncWrap(v)
def wrap: FuncWrap = FuncWrap(v)

@inline
def optWrap = Some(FuncWrap(v))
def optWrap: Option[FuncWrap] = Some(FuncWrap(v))
}


final class ChangeFeedSupport[T](val target: Typed) extends AnyVal {
def changes(squash: Option[Boolean] = None, includeStates: Option[Boolean] = None) = new Changes[T](target, squash, includeStates)
def changes(squash: Option[Boolean] = None, includeStates: Option[Boolean] = None): Changes[T] = new Changes[T](target, squash, includeStates)

def changes() = new Changes[T](target)
def changes(): Changes[T] = new Changes[T](target)
}


object Implicits {


trait Common extends CanMapImplicits
with ToAstImplicts
with ReceptacleImplicits with ImplicitConversions
with net.Versions
with Helpers
with ToFloatImplicits
with GeometryImplicits {
with ToAstImplicts
with ReceptacleImplicits with ImplicitConversions
with net.Versions
with Helpers
with ToFloatImplicits
with GeometryImplicits {

object r extends RethinkApi

Expand Down
5 changes: 3 additions & 2 deletions core/src/main/scala/com/rethinkscala/Message.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import ql2.{Ql2 => ql2}
import ql2.Term.TermType
import ql2.Datum.DatumType
import com.rethinkscala.reflect.Reflector
import com.rethinkscala.net.Connection

import com.rethinkscala.backend.{Connection => BackendConnection}

trait AssocPair {

Expand Down Expand Up @@ -65,7 +66,7 @@ trait Term extends WithAst {
.addAllOptargs(optargs.map(_.pair.asInstanceOf[ql2.Term.AssocPair])).build()
*/
def ast(implicit connection: Connection) = connection toAst underlyingTerm
def ast(implicit connection: BackendConnection) = connection toAst underlyingTerm

lazy val args: Seq[Term] = if (extractArgs) buildArgs(Reflector.fields(this).map(_.get(this)): _*) else Seq.empty[Term]

Expand Down
14 changes: 8 additions & 6 deletions core/src/main/scala/com/rethinkscala/R.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@ trait TableApi extends DBApi {

def table(name: String): Table[Document] = table(name, None)

@deprecated("use table(String,ReadMode.Kind)")
@deprecated("use table(String,ReadMode.Kind)","0.5.0")
def table(name: String, useOutDated: Boolean): Table[Document] = table(name)

@deprecated("use table(String,ReadMode.Kind)")
@deprecated("use table(String,ReadMode.Kind)","0.5.0")
def table(name: String, useOutDated: Option[Boolean]): Table[Document] = table(name)

def table(name: String, readMode: ReadMode.Kind = ReadMode.Single): Table[Document] = Table[Document](name, readMode)

def tableAs[T <: AnyRef](name: String): Table[T] = tableAs[T](name, ReadMode.Single)

@deprecated("use tableAs(String,ReadMode.Kind)")
@deprecated("use tableAs(String,ReadMode.Kind)","0.5.0")
def tableAs[T <: AnyRef](name: String, useOutDated: Boolean): Table[T] = tableAs[T](name)

@deprecated("use tableAs(String,ReadMode.Kind)")
@deprecated("use tableAs(String,ReadMode.Kind)","0.5.0")
def tableAs[T <: AnyRef](name: String, useOutDated: Option[Boolean]): Table[T] = tableAs[T](name)

def tableAs[T <: AnyRef](name: String, readMode: ReadMode.Kind): Table[T] = Table[T](name, readMode)
Expand Down Expand Up @@ -88,6 +88,8 @@ trait ExprApi {
def expr[T](value: Iterable[T]) = Expr(value)
}

object RethinkApi extends RethinkApi

trait RethinkApi extends TableApi with TimeNames with GeometryApi with ExprApi {
def getInstance = this

Expand All @@ -103,10 +105,10 @@ trait RethinkApi extends TableApi with TimeNames with GeometryApi with ExprApi {

def branch(predicate: Binary, passed: Typed, failed: Typed): Branch = Branch(predicate, passed, failed)

@deprecated("Call .sum on collection")
@deprecated("Call .sum on collection","0.5.0")
def sum(attr: String): BySum = BySum(attr)

@deprecated("Call .avg on collection")
@deprecated("Call .avg on collection","0.5.0")
def avg(attr: String): ByAvg = ByAvg(attr)

def random: Random.RandomType[Double] = Random[Double](Seq.empty)
Expand Down
10 changes: 5 additions & 5 deletions core/src/main/scala/com/rethinkscala/Schema.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.fasterxml.jackson.annotation.PropertyAccessor
import com.fasterxml.jackson.databind.{DeserializationFeature, ObjectMapper}
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.rethinkscala.ast._
import com.rethinkscala.net.{ReqlClientError, ReqlRuntimeError, ReqlError, Connection}
import com.rethinkscala.backend.{Connection => BackendConnection}
import com.rethinkscala.reflect.Reflector

import scala.collection.mutable.ArrayBuffer
Expand Down Expand Up @@ -39,7 +39,7 @@ class Schema extends Helpers {
protected implicit def thisSchema = this


def defaultConnection: Option[Connection] = None
def defaultConnection: Option[BackendConnection] = None


protected def defineMapper = {
Expand All @@ -65,10 +65,10 @@ class Schema extends Helpers {
def liftAs[T <: Document, R](f: PartialFunction[Table[T], R])(implicit mf: Manifest[T]): Option[R] = get[T] map (f(_))


implicit def doc2Active[A <: Document](a: A)(implicit extractor: ResultExtractor[A], c: Connection): ActiveRecord[A] =
implicit def doc2Active[A <: Document](a: A)(implicit extractor: ResultExtractor[A], c: BackendConnection): ActiveRecord[A] =
new ActiveRecord(a)

class ActiveRecord[T <: Document](o: T)(implicit c: Connection, extractor: ResultExtractor[T]) {
class ActiveRecord[T <: Document](o: T)(implicit c: BackendConnection, extractor: ResultExtractor[T]) {
private def performAction[R](action: (Table[T]) => Produce[R])(implicit mf: Manifest[R]): Either[Exception, R] = {

val m = extractor.manifest
Expand Down Expand Up @@ -114,7 +114,7 @@ class Schema extends Helpers {
table(tableNameFromClass(manifestT.runtimeClass))(manifestT)


@deprecated("use table(String,ReadMode.Kind,Option[String])")
@deprecated("use table(String,ReadMode.Kind,Option[String])","0.4.8")
protected def table[T <: Document](name: String, useOutDated: Option[Boolean], db: Option[String]
)(implicit manifestT: Manifest[T]): Table[T] = {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package com.rethinkscala.backend.netty
import java.util.concurrent.atomic.{AtomicBoolean, AtomicLong}

import com.rethinkscala.backend.Connection
import com.rethinkscala.net.{Version, VersionHandler}
import com.rethinkscala.net.{Version, ProtocolHandler}
import com.rethinkscala.utils.ConnectionWithId
import com.rethinkscala.{ResultExtractor, Term}
import com.typesafe.scalalogging.slf4j.LazyLogging
Expand Down Expand Up @@ -32,7 +32,7 @@ case class SingleConnection(version: Version) extends Connection {
override def write[T](term: Term, opts: Map[String, Any])(implicit mf: Manifest[T]) = ???
} */

class ConnectionAttachment[T](versionHandler: VersionHandler[T], restore: Throwable => Unit) {
class ConnectionAttachment[T](versionHandler: ProtocolHandler[T], restore: Throwable => Unit) {
def handle(tokenId: Long, response: T) = versionHandler.handle(tokenId, response)

def failure(e: Throwable) = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import com.rethinkscala._
import com.rethinkscala.ast.Produce
import com.rethinkscala.backend.Delegate
import com.rethinkscala.changefeeds.net.ChangeCursor
import com.rethinkscala.net.Connection

import com.rethinkscala.backend.{Connection => BackendConnection}

import scalaz.\/._
import scalaz._
Expand All @@ -19,7 +20,7 @@ import scalaz.stream.Process
* Time: 9:09 AM
*/
case class ChangeFeedDelegate[T](producer: Produce[ChangeCursor[CursorChange[T]]],
connection: Connection, connectionId: Option[Long] = None)
connection: BackendConnection, connectionId: Option[Long] = None)
extends Delegate[T] {
lazy val underlying = BlockingDelegate(producer, BlockingConnection(connection), connectionId)

Expand Down
Loading

0 comments on commit 5e0f914

Please sign in to comment.