Skip to content

Commit

Permalink
Working to simplify model references
Browse files Browse the repository at this point in the history
  • Loading branch information
darkfrog26 committed Dec 24, 2023
1 parent 5c18e08 commit a56b659
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 56 deletions.
5 changes: 5 additions & 0 deletions core/src/main/scala/com/outr/arango/query/dsl/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import fabric.rw._
import scala.language.implicitConversions

package object dsl {
implicit class DocumentModelExtras[D <: Document[D], M <: DocumentModel[D]](model: M) {
def ref: DocumentRef[D, M] = DocumentRef[D, M](model, None)
def ref(name: String): DocumentRef[D, M] = DocumentRef(model, Some(name))
}

private val refs = new ThreadLocal[List[Ref]] {
override def initialValue(): List[Ref] = Nil
}
Expand Down
16 changes: 8 additions & 8 deletions driver/src/main/scala/com/outr/arango/Graph.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ import scala.concurrent.duration.{DurationInt, FiniteDuration}
class Graph(private[arango] val db: ArangoDB, val managed: Boolean) {
private val _initialized = new AtomicBoolean(false)

private var _collections: List[DocumentCollection[_ <: Document[_], _ <: DocumentModel[_]]] = Nil
private var _collections: List[DocumentCollection[_ <: Document[_]]] = Nil
private var _views: List[View] = Nil
private var _stores: List[DatabaseStore] = Nil

protected def storeCollectionName: String = "backingStore"

def server: ArangoDBServer = db.server

def collections: List[DocumentCollection[_ <: Document[_], _ <: DocumentModel[_]]] = _collections
def collections: List[DocumentCollection[_ <: Document[_]]] = _collections

def views: List[View] = _views

Expand Down Expand Up @@ -140,9 +140,9 @@ class Graph(private[arango] val db: ArangoDB, val managed: Boolean) {

def truncate(): IO[Unit] = collections.map(_.collection.truncate()).sequence.map(_ => ())

def vertex[D <: Document[D], M <: DocumentModel[D]](model: M,
managed: Boolean = this.managed): DocumentCollection[D, M] = synchronized {
val c = new DocumentCollection[D, M](
def vertex[D <: Document[D]](model: DocumentModel[D],
managed: Boolean = this.managed): DocumentCollection[D] = synchronized {
val c = new DocumentCollection[D](
graph = this,
arangoCollection = db.collection(model.collectionName),
model = model,
Expand All @@ -153,9 +153,9 @@ class Graph(private[arango] val db: ArangoDB, val managed: Boolean) {
c
}

def edge[E <: Edge[E, From, To], M <: EdgeModel[E, From, To], From, To](model: M,
managed: Boolean = this.managed): EdgeCollection[E, M, From, To] = synchronized {
val c = new EdgeCollection[E, M, From, To](this, db.collection(model.collectionName), model, managed)
def edge[E <: Edge[E, From, To], From, To](model: EdgeModel[E, From, To],
managed: Boolean = this.managed): EdgeCollection[E, From, To] = synchronized {
val c = new EdgeCollection[E, From, To](this, db.collection(model.collectionName), model, managed)
_collections = _collections ::: List(c)
c
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import fabric.rw._
trait AuditSupport {
this: Graph =>

val auditLog: DocumentCollection[AuditRecord, AuditRecord.type] = vertex(AuditRecord)
val auditLog: DocumentCollection[AuditRecord] = vertex(AuditRecord)

object audit {
def resource(name: String): Resource = Resource(name)
Expand All @@ -28,7 +28,7 @@ trait AuditSupport {
createdBefore: Option[Long] = None,
sortField: Field[_] = AuditRecord.created,
sortDirection: SortDirection = SortDirection.DESC): QueryBuilder[AuditRecord] = {
auditLog.query.byFilter({ r =>
auditLog.query.byFilter[AuditRecord.type]({ r =>
val filters = List(
action.map(s => r.action === s),
origin.map(s => r.origin === s),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,21 @@ import com.outr.arango.core.{ArangoDBCollection, CollectionSchema, ComputedValue
import com.outr.arango.upsert.UpsertBuilder
import fabric.Json

class DocumentCollection[D <: Document[D], M <: DocumentModel[D]](protected[arango] val graph: Graph,
class DocumentCollection[D <: Document[D]](protected[arango] val graph: Graph,
protected[arango] val arangoCollection: ArangoDBCollection,
val model: M,
val model: DocumentModel[D],
val `type`: CollectionType,
val managed: Boolean) extends WritableCollection[D, M] {
val managed: Boolean) extends WritableCollection[D] {
override def dbName: String = graph.databaseName

override def name: String = arangoCollection.name

override lazy val query: DocumentCollectionQuery[D, M] = new DocumentCollectionQuery[D, M](this)
override lazy val query: DocumentCollectionQuery[D] = new DocumentCollectionQuery[D](this)

override protected def beforeStorage(value: Json): Json = model.allMutations.foldLeft(value)((v, m) => m.store(v))

override protected def afterRetrieval(value: Json): Json = model.allMutations.foldLeft(value)((v, m) => m.retrieve(v))

def ref: DocumentRef[D, M] = DocumentRef[D, M](model, None)
def ref(name: String): DocumentRef[D, M] = DocumentRef(model, Some(name))

lazy val update: UpdateBuilder[D, M] = UpdateBuilder(this)
lazy val upsert: UpsertBuilder[D, M] = UpsertBuilder(this)
lazy val update: UpdateBuilder[D] = UpdateBuilder(this)
lazy val upsert: UpsertBuilder[D] = UpsertBuilder(this)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.outr.arango.query.dsl._
import com.outr.arango.query.{Query, SortDirection}
import com.outr.arango.{Document, DocumentModel, DocumentRef, Field}

class DocumentCollectionQuery[D <: Document[D], M <: DocumentModel[D]](collection: DocumentCollection[D, M]) extends QueryBuilder[D](
class DocumentCollectionQuery[D <: Document[D]](collection: DocumentCollection[D]) extends QueryBuilder[D](
graph = collection.graph,
query = DocumentCollectionQuery.forCollection(collection),
converter = collection.toT
Expand All @@ -26,11 +26,11 @@ class DocumentCollectionQuery[D <: Document[D], M <: DocumentModel[D]](collectio
* @param limit the limit of results
* @return QueryBuilder[D]
*/
def byFilter(filter: DocumentRef[D, M] => Filter,
def byFilter[M <: DocumentModel[D]](filter: DocumentRef[D, M] => Filter,
sort: Option[(Field[_], SortDirection)] = None,
offset: Option[Int] = None,
limit: Option[Int] = None): QueryBuilder[D] = noConsumingRefs {
withRef { d =>
withRef[M] { d =>
FOR(d) IN collection
FILTER(filter(d))
limit.foreach { l =>
Expand All @@ -52,7 +52,7 @@ class DocumentCollectionQuery[D <: Document[D], M <: DocumentModel[D]](collectio
def all(sort: Option[(Field[_], SortDirection)] = None,
offset: Option[Int] = None,
limit: Option[Int] = None): QueryBuilder[D] = noConsumingRefs {
withRef { d =>
withRef[DocumentModel[D]] { d =>
FOR(d) IN collection
limit.foreach { l =>
LIMIT(offset.getOrElse(0), l)
Expand All @@ -68,8 +68,8 @@ class DocumentCollectionQuery[D <: Document[D], M <: DocumentModel[D]](collectio
* @param f the function to create the query
* @return QueryBuilder[D]
*/
def withRef(f: DocumentRef[D, M] => Unit): QueryBuilder[D] = try {
val d: DocumentRef[D, M] = DocumentRef(collection.model, Some("d"))
def withRef[M <: DocumentModel[D]](f: DocumentRef[D, M] => Unit): QueryBuilder[D] = try {
val d: DocumentRef[D, M] = DocumentRef[D, M](collection.model.asInstanceOf[M], Some("d"))
apply(aql {
withReference(d) {
f(d)
Expand All @@ -81,8 +81,8 @@ class DocumentCollectionQuery[D <: Document[D], M <: DocumentModel[D]](collectio
}

object DocumentCollectionQuery {
def forCollection[D <: Document[D], M <: DocumentModel[D]](collection: DocumentCollection[D, M]): Query = try {
val d: DocumentRef[D, DocumentModel[D]] = DocumentRef(collection.model, Some("d"))
def forCollection[D <: Document[D], M <: DocumentModel[D]](collection: DocumentCollection[D]): Query = try {
val d: DocumentRef[D, M] = DocumentRef(collection.model.asInstanceOf[M], Some("d"))
aql {
withReference(d) {
FOR(d) IN collection
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.outr.arango.collection

import com.outr.arango.core.{ArangoDBCollection, CollectionSchema, ComputedValue}
import com.outr.arango.core.ArangoDBCollection
import com.outr.arango.{CollectionType, Edge, EdgeModel, Graph}

class EdgeCollection[E <: Edge[E, From, To], M <: EdgeModel[E, From, To], From, To](graph: Graph,
class EdgeCollection[E <: Edge[E, From, To], From, To](graph: Graph,
arangoDBCollection: ArangoDBCollection,
edgeModel: M,
edgeModel: EdgeModel[E, From, To],
managed: Boolean)
extends DocumentCollection[E, M](graph, arangoDBCollection, edgeModel, CollectionType.Edge, managed)
extends DocumentCollection[E](graph, arangoDBCollection, edgeModel, CollectionType.Edge, managed)
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package com.outr.arango.collection

import com.outr.arango.{Document, DocumentModel}

trait ReadableCollection[D <: Document[D], M <: DocumentModel[D]] extends Collection {
def model: M
trait ReadableCollection[D <: Document[D]] extends Collection {
def model: DocumentModel[D]

def query: DocumentCollectionQuery[D, M]
def query: DocumentCollectionQuery[D]
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.outr.arango.query.dsl._
import com.outr.arango.query.{Query, QueryPart}
import fabric.rw._

case class UpdateBuilder[D <: Document[D], M <: DocumentModel[D]](collection: DocumentCollection[D, M],
case class UpdateBuilder[D <: Document[D], M <: DocumentModel[D]](collection: DocumentCollection[D],
ignoreErrors: Boolean = false,
keepNull: Boolean = true,
mergeObjects: Boolean = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.outr.arango.{Document, DocumentModel}
import fabric.Json
import fabric.rw._

trait WritableCollection[D <: Document[D], M <: DocumentModel[D]] extends ReadableCollection[D, M] with ArangoDBDocuments[D] {
trait WritableCollection[D <: Document[D]] extends ReadableCollection[D] with ArangoDBDocuments[D] {
protected def arangoCollection: ArangoDBCollection
override protected def _collection: arangodb.ArangoCollection = arangoCollection._collection

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ trait PaginationSupport extends Graph { graph =>
/**
* Collection representing the pagination cache
*/
val pagedResults: DocumentCollection[PagedResult, PagedResult.type] = vertex(PagedResult)
val pagedResults: DocumentCollection[PagedResult] = vertex(PagedResult)

/**
* Maximum number of results able to be stored in the cache. If this overflows, subsequent queries will fallback to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import fabric.rw.RW
import java.util.concurrent.ConcurrentLinkedQueue
import java.util.concurrent.atomic.AtomicInteger

case class OperationsQueue[D <: Document[D], M <: DocumentModel[D]](collection: DocumentCollection[D, M],
case class OperationsQueue[D <: Document[D]](collection: DocumentCollection[D],
flushSize: Int,
chunkSize: Int) { oq =>
private var queues = List.empty[ProcessQueue[D]]
Expand All @@ -34,7 +34,7 @@ case class OperationsQueue[D <: Document[D], M <: DocumentModel[D]](collection:
lazy val insert: ProcessQueue[D] = create(list => collection.batch.insert(list).void)
lazy val upsert: ProcessQueue[D] = create(list => collection.batch.upsert(list).void)
lazy val delete: ProcessQueue[D] = create(list => collection.batch.delete(list.map(_._id)).void)
def createUpsertReplace(f: DocumentRef[D, M] => List[Searchable]): ProcessQueue[D] = create { list =>
def createUpsertReplace(f: DocumentRef[D] => List[Searchable]): ProcessQueue[D] = create { list =>
collection.upsert.withListSearch(list)(f).execute()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ object CreateDatabase extends DatabaseUpgrade {
IO.unit
}

private def verifyCollection(collection: DocumentCollection[_, _ <: DocumentModel[_]]): IO[Unit] = if (collection.managed) {
private def verifyCollection(collection: DocumentCollection[_]): IO[Unit] = if (collection.managed) {
for {
exists <- collection.arangoCollection.collection.exists()
created <- if (exists) {
Expand Down
32 changes: 16 additions & 16 deletions driver/src/main/scala/com/outr/arango/upsert/UpsertBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import fabric._
import fabric.io.JsonFormatter
import fabric.rw._

case class UpsertBuilder[D <: Document[D], M <: DocumentModel[D]](collection: DocumentCollection[D, M],
case class UpsertBuilder[D <: Document[D]](collection: DocumentCollection[D],
list: Option[() => (Ref, List[Json], List[QueryPart])] = None,
search: List[QueryPart] = Nil,
insert: Option[Json] = None,
Expand All @@ -24,16 +24,16 @@ case class UpsertBuilder[D <: Document[D], M <: DocumentModel[D]](collection: Do
forceIndexHint: Boolean = false) {
private implicit def rw: RW[D] = collection.model.rw

def withSearch(f: FieldAndValue[_]): UpsertBuilder[D, M] =
def withSearch(f: FieldAndValue[_]): UpsertBuilder[D] =
withSearch(f.field.fieldName, QueryPart.Variable(f.value))
def withSearch(entry: (String, QueryPart)): UpsertBuilder[D, M] = {
def withSearch(entry: (String, QueryPart)): UpsertBuilder[D] = {
val part = QueryPart.Container(List(QueryPart.Static(entry._1), QueryPart.Static(": "), entry._2))
copy(
search = part :: search
)
}
def withListSearch[T <: Document[T], TM <: DocumentModel[T]](collection: DocumentCollection[T, TM], list: List[T])
(f: DocumentRef[T, TM] => List[Searchable]): UpsertBuilder[D, M] = {
def withListSearch[T <: Document[T]](collection: DocumentCollection[T], list: List[T])
(f: DocumentRef[T] => List[Searchable]): UpsertBuilder[D] = {
copy(
list = Some(() => {
val ref = collection.ref("doc")
Expand All @@ -42,18 +42,18 @@ case class UpsertBuilder[D <: Document[D], M <: DocumentModel[D]](collection: Do
})
)
}
def withListSearch(list: List[D])(f: DocumentRef[D, M] => List[Searchable]): UpsertBuilder[D, M] =
withListSearch[D, M](collection, list)(f)
def withInsert(doc: D): UpsertBuilder[D, M] = withInsert(doc.json(collection.model.rw))
def withInsert(json: Json): UpsertBuilder[D, M] = withInsert(JsonFormatter.Compact(json))
def withInsert(insert: String): UpsertBuilder[D, M] = copy(insert = Some(insert))
def withListSearch(list: List[D])(f: DocumentRef[D] => List[Searchable]): UpsertBuilder[D] =
withListSearch[D](collection, list)(f)
def withInsert(doc: D): UpsertBuilder[D] = withInsert(doc.json(collection.model.rw))
def withInsert(json: Json): UpsertBuilder[D] = withInsert(JsonFormatter.Compact(json))
def withInsert(insert: String): UpsertBuilder[D] = copy(insert = Some(insert))

def withUpdate(doc: D): UpsertBuilder[D, M] = withUpdate(doc.json(collection.model.rw))
def withUpdate(json: Json): UpsertBuilder[D, M] = withUpdate(JsonFormatter.Compact(json))
def withUpdate(update: String): UpsertBuilder[D, M] = copy(upsert = Some(Upsert.Update(update)))
def withNoUpdate: UpsertBuilder[D, M] = withUpdate(obj())
def withUpdate(doc: D): UpsertBuilder[D] = withUpdate(doc.json(collection.model.rw))
def withUpdate(json: Json): UpsertBuilder[D] = withUpdate(JsonFormatter.Compact(json))
def withUpdate(update: String): UpsertBuilder[D] = copy(upsert = Some(Upsert.Update(update)))
def withNoUpdate: UpsertBuilder[D] = withUpdate(obj())

def withReplace(doc: D): UpsertBuilder[D, M] = copy(upsert = Some(Upsert.Replace(doc)))
def withReplace(doc: D): UpsertBuilder[D] = copy(upsert = Some(Upsert.Replace(doc)))

def withOptions(ignoreErrors: Boolean = false,
keepNull: Boolean = true,
Expand All @@ -62,7 +62,7 @@ case class UpsertBuilder[D <: Document[D], M <: DocumentModel[D]](collection: Do
ignoreRevs: Boolean = true,
exclusive: Boolean = false,
indexHint: Option[String] = None,
forceIndexHint: Boolean = false): UpsertBuilder[D, M] = copy(
forceIndexHint: Boolean = false): UpsertBuilder[D] = copy(
ignoreErrors = ignoreErrors,
keepNull = keepNull,
mergeObjects = mergeObjects,
Expand Down

0 comments on commit a56b659

Please sign in to comment.