Skip to content
This repository was archived by the owner on Jul 16, 2024. It is now read-only.

Commit bb642f2

Browse files
committed
Updated the driver to use the Async Observables
Removes the dependency on the reactive streams driver and provides a solid core foundation for the driver. SCALA-185
1 parent 2c6f2e4 commit bb642f2

File tree

59 files changed

+1921
-1779
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+1921
-1779
lines changed

README.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ This is an experimental reactive driver for Scala and MongoDB.
77

88
Here be dragons
99

10-
Currently, under heavy development, depends on the 3.0.x MongoDB
11-
Java Driver (also under heavy development) and as such may change
12-
at *any* time.
10+
Currently, under heavy development, depends on the 3.1.x MongoDB
11+
Java Driver and as such may change at *any* time.
1312

1413
___, ____--'
1514
_,-.'_,-' (

core/rootdoc.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
This is the documentation for the MongoDB Scala driver core.
2+
3+
4+
+3-2
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,16 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.mongodb.scala.reactivestreams.client
17+
package com.mongodb.scala
1818

1919
import scala.language.implicitConversions
20+
2021
import scala.reflect.ClassTag
2122

2223
/**
2324
* Custom helpers for the client
2425
*/
25-
private[client] object Helpers {
26+
private[scala] object Helpers {
2627

2728
/**
2829
* Helper to get the class from a classTag
+33-31
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,30 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.mongodb.scala.reactivestreams.client
17+
package com.mongodb.scala
1818

1919
import java.util.Date
2020

21-
import com.mongodb.scala.reactivestreams.client.Helpers.DefaultsTo
22-
import com.mongodb.scala.reactivestreams.client.collection.immutable.{ Document => ImmutableDocument }
23-
import com.mongodb.scala.reactivestreams.client.collection.mutable.{ Document => MutableDocument }
24-
import com.mongodb.scala.reactivestreams.client.collection.immutable.Document
25-
import org.bson._
26-
import org.bson.types.ObjectId
21+
import scala.language.implicitConversions
2722

2823
import scala.collection.JavaConverters._
29-
import scala.language.implicitConversions
3024
import scala.util.matching.Regex
31-
import scala.util.{ Failure, Success, Try }
25+
import scala.util.{Failure, Success, Try}
26+
27+
import org.bson.types.ObjectId
28+
import org.bson.{Document => JDocument, _}
29+
import com.mongodb.scala.Helpers.DefaultsTo
30+
import com.mongodb.scala.collection.{immutable, mutable}
3231

3332
object Implicits extends FromAnyImplicits with IterableImplicits with DateTimeImplicits {
3433

35-
implicit def immutableDocumentToBsonDocument(value: ImmutableDocument): BsonDocument = value.underlying
34+
implicit def immutableDocumentToBsonDocument(value: immutable.Document): BsonDocument = value.underlying
3635

37-
implicit def bsonDocumentToImmutableDocument(value: BsonDocument): ImmutableDocument = ImmutableDocument(value)
36+
implicit def bsonDocumentToImmutableDocument(value: BsonDocument): immutable.Document = immutable.Document(value)
3837

39-
implicit def mutableDocumentToBsonDocument(value: MutableDocument): BsonDocument = value.underlying
38+
implicit def mutableDocumentToBsonDocument(value: mutable.Document): BsonDocument = value.underlying
4039

41-
implicit def bsonDocumentToMutableDocument(value: BsonDocument): MutableDocument = MutableDocument(value)
40+
implicit def bsonDocumentToMutableDocument(value: BsonDocument): mutable.Document = mutable.Document(value)
4241

4342
implicit def binaryArrayToBsonBinary(value: Array[Byte]): BsonBinary = new BsonBinary(value)
4443

@@ -107,7 +106,11 @@ trait IterableImplicits {
107106

108107
implicit def bsonArrayToIterableSymbol(value: BsonArray): Iterable[Symbol] = value.asScala map (v => Symbol(v.asSymbol().getSymbol))
109108

110-
implicit def bsonArrayToIterableDocument(value: BsonArray): Iterable[Document] = value.asScala map (v => Document(v.asDocument()))
109+
implicit def bsonArrayToIterableImmutableDocument(value: BsonArray): Iterable[immutable.Document] = value.asScala map (v =>
110+
immutable.Document(v.asDocument()))
111+
112+
implicit def bsonArrayToIterableMutableDocument(value: BsonArray): Iterable[mutable.Document] = value.asScala map (v =>
113+
mutable.Document(v.asDocument()))
111114

112115
implicit def bsonArrayToIterableIterable(value: BsonArray): Iterable[Iterable[BsonValue]] = value.asScala map (v => v.asArray().getValues.asScala)
113116

@@ -132,31 +135,30 @@ trait FromAnyImplicits {
132135
// scalastyle:off cyclomatic.complexity
133136
implicit def anyToBsonValue(v: Any): BsonValue = {
134137
v match {
135-
case x @ (v: BsonValue) => v
136-
case x @ (v: Array[Byte]) => new BsonBinary(v)
137-
case x @ (v: Boolean) => new BsonBoolean(v)
138-
case x @ (v: Date) => new BsonDateTime(v.getTime)
139-
case x @ (v: Double) => new BsonDouble(v)
140-
case x @ (v: Int) => new BsonInt32(v)
141-
case x @ (v: Long) => new BsonInt64(v)
142-
case x @ (v: ObjectId) => new BsonObjectId(v)
143-
case x @ (v: String) => new BsonString(v)
144-
case x @ (v: Symbol) => new BsonSymbol(v.name)
145-
case x @ (v: Regex) => new BsonRegularExpression(v.regex)
146-
case None => new BsonNull
147-
case x @ (v: ImmutableDocument) => v.underlying
148-
case x @ (v: MutableDocument) => v.underlying
149-
case x @ (v: Iterable[_]) => {
138+
case x@(v: BsonValue) => v
139+
case x@(v: Array[Byte]) => new BsonBinary(v)
140+
case x@(v: Boolean) => new BsonBoolean(v)
141+
case x@(v: Date) => new BsonDateTime(v.getTime)
142+
case x@(v: Double) => new BsonDouble(v)
143+
case x@(v: Int) => new BsonInt32(v)
144+
case x@(v: Long) => new BsonInt64(v)
145+
case x@(v: ObjectId) => new BsonObjectId(v)
146+
case x@(v: String) => new BsonString(v)
147+
case x@(v: Symbol) => new BsonSymbol(v.name)
148+
case x@(v: Regex) => new BsonRegularExpression(v.regex)
149+
case None => new BsonNull
150+
case x@(v: immutable.Document) => v.underlying
151+
case x@(v: mutable.Document) => v.underlying
152+
case x@(v: Iterable[_]) => {
150153
Try(iterableAnyToBsonArray(v)) match {
151154
case Success(bsonValue) => bsonValue
152-
case Failure(ex) => throw new BsonInvalidOperationException(s"Invalid type cannot be converted to a BsonValue: $v")
155+
case Failure(ex) => throw new BsonInvalidOperationException(s"Invalid type cannot be converted to a BsonValue: $v")
153156
}
154157
}
155158
case _ =>
156159
throw new BsonInvalidOperationException(s"Invalid type cannot be converted to a BsonValue: $v")
157160
}
158161
}
159-
160162
// scalastyle:on cyclomatic.complexity
161163

162164
private def iterableAnyToBsonArray[B](value: Iterable[B])(implicit e: B DefaultsTo BsonValue, ev: B => BsonValue): BsonArray =
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,20 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.mongodb.scala.reactivestreams.client.codecs
17+
package com.mongodb.scala.codecs
1818

19-
import com.mongodb.scala.reactivestreams.client.collection.immutable.{ Document => ImmutableDocument }
20-
import com.mongodb.scala.reactivestreams.client.collection.mutable.{ Document => MutableDocument }
2119
import org.bson.codecs.Codec
22-
import org.bson.codecs.configuration.{ CodecProvider, CodecRegistry }
20+
import org.bson.codecs.configuration.{CodecProvider, CodecRegistry}
21+
import com.mongodb.scala.collection.{immutable, mutable}
2322

2423
/**
2524
* A {@code CodecProvider} for the Document class and all the default Codec implementations on which it depends.
2625
*
2726
*/
2827
case class DocumentCodecProvider() extends CodecProvider {
2928

30-
val IMMUTABLE: Class[ImmutableDocument] = classOf[ImmutableDocument]
31-
val MUTABLE: Class[MutableDocument] = classOf[MutableDocument]
29+
val IMMUTABLE: Class[immutable.Document] = classOf[immutable.Document]
30+
val MUTABLE: Class[mutable.Document] = classOf[mutable.Document]
3231

3332
// scalastyle:off null
3433
@SuppressWarnings(Array("unchecked"))
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.mongodb.scala.reactivestreams.client.codecs
17+
package com.mongodb.scala.codecs
1818

19-
import com.mongodb.scala.reactivestreams.client.collection.immutable.Document
2019
import org.bson.codecs.configuration.CodecRegistry
21-
import org.bson.codecs.{ BsonDocumentCodec, CollectibleCodec, DecoderContext, EncoderContext }
22-
import org.bson.{ BsonReader, BsonValue, BsonWriter }
20+
import org.bson.codecs.{BsonDocumentCodec, CollectibleCodec, DecoderContext, EncoderContext}
21+
import org.bson.{BsonReader, BsonValue, BsonWriter}
22+
import com.mongodb.scala.collection.immutable.Document
2323

2424
/**
2525
* Companion helper for immutable Document instances.
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.mongodb.scala.reactivestreams.client.codecs
17+
package com.mongodb.scala.codecs
1818

19-
import com.mongodb.scala.reactivestreams.client.collection.mutable.Document
2019
import org.bson.codecs.configuration.CodecRegistry
21-
import org.bson.codecs.{ BsonDocumentCodec, CollectibleCodec, DecoderContext, EncoderContext }
22-
import org.bson.{ BsonReader, BsonValue, BsonWriter }
20+
import org.bson.codecs.{BsonDocumentCodec, CollectibleCodec, DecoderContext, EncoderContext}
21+
import org.bson.{BsonReader, BsonValue, BsonWriter}
22+
import com.mongodb.scala.collection.mutable.Document
2323

2424
/**
2525
* Companion helper for mutable Document instances.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2015 MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.mongodb.scala
18+
19+
import org.bson.codecs.configuration.CodecRegistries._
20+
import org.bson.codecs.configuration.CodecRegistry
21+
import org.bson.codecs.{DocumentCodecProvider => ADocumentCodecProvider}
22+
import com.mongodb.async.client.MongoClients
23+
24+
package object codecs {
25+
val DEFAULT_CODEC_REGISTRY: CodecRegistry = fromRegistries(MongoClients.getDefaultCodecRegistry, fromProviders(DocumentCodecProvider()))
26+
}
+13-13
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.mongodb.scala.reactivestreams.client.collection
17+
package com.mongodb.scala.collection
18+
19+
import scala.collection.JavaConverters._
20+
import scala.collection.{GenTraversableOnce, Traversable}
21+
import scala.reflect.ClassTag
22+
import scala.util.{Failure, Success, Try}
1823

19-
import com.mongodb.scala.reactivestreams.client.Helpers.DefaultsTo
2024
import org.bson._
2125
import org.bson.codecs.configuration.CodecRegistry
2226
import org.bson.conversions.Bson
23-
24-
import scala.collection.JavaConverters._
25-
import scala.collection.{ GenTraversableOnce, Traversable }
26-
import scala.reflect.ClassTag
27-
import scala.util.{ Failure, Success, Try }
27+
import com.mongodb.scala.Helpers.DefaultsTo
2828

2929
/**
3030
* Base Document trait.
@@ -41,24 +41,24 @@ trait BaseDocument[T] extends Traversable[(String, BsonValue)] with Bson {
4141
*
4242
* Restricted access to the underlying BsonDocument
4343
*/
44-
protected[client] val underlying: BsonDocument
44+
protected[scala] val underlying: BsonDocument
4545

4646
/**
4747
* Create a concrete document instance
4848
*
4949
* @param underlying the underlying BsonDocument
5050
* @return a concrete document instance
5151
*/
52-
protected[client] def apply(underlying: BsonDocument): T
52+
protected[scala] def apply(underlying: BsonDocument): T
5353

5454
/**
5555
* Retrieves the value which is associated with the given key or throws a `NoSuchElementException`.
5656
*
5757
* @param key the key
5858
* @return the value associated with the given key, or throws `NoSuchElementException`.
5959
*/
60-
def apply[T <: BsonValue](key: String)(implicit e: T DefaultsTo BsonValue, ct: ClassTag[T]): T = {
61-
get[T](key) match {
60+
def apply[TResult <: BsonValue](key: String)(implicit e: TResult DefaultsTo BsonValue, ct: ClassTag[TResult]): TResult = {
61+
get[TResult](key) match {
6262
case Some(value) => value
6363
case None => throw new NoSuchElementException("key not found: " + key)
6464
}
@@ -193,10 +193,10 @@ trait BaseDocument[T] extends Traversable[(String, BsonValue)] with Bson {
193193
* @return an option value containing the value associated with `key` in this document,
194194
* or `None` if none exists.
195195
*/
196-
def get[T <: BsonValue](key: String)(implicit e: T DefaultsTo BsonValue, ct: ClassTag[T]): Option[T] = {
196+
def get[TResult <: BsonValue](key: String)(implicit e: TResult DefaultsTo BsonValue, ct: ClassTag[TResult]): Option[TResult] = {
197197
underlying.containsKey(key) match {
198198
case true => Try(ct.runtimeClass.cast(underlying.get(key))) match {
199-
case Success(v) => Some(v.asInstanceOf[T])
199+
case Success(v) => Some(v.asInstanceOf[TResult])
200200
case Failure(ex) => None
201201
}
202202
case false => None
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,16 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.mongodb.scala.reactivestreams.client.collection.immutable
18-
19-
import com.mongodb.scala.reactivestreams.client.collection
20-
import org.bson.codecs.configuration.CodecRegistry
21-
import org.bson.conversions.Bson
22-
import org.bson.{ BsonDocument, BsonValue }
17+
package com.mongodb.scala.collection.immutable
2318

2419
import scala.collection.JavaConverters._
2520
import scala.collection.generic.CanBuildFrom
26-
import scala.collection.mutable.{ Builder, ListBuffer }
27-
import scala.collection.{ Traversable, TraversableLike }
21+
import scala.collection.mutable.{Builder, ListBuffer}
22+
import scala.collection.{Traversable, TraversableLike}
23+
24+
import org.bson.{BsonDocument, BsonValue}
25+
import com.mongodb.scala.collection.BaseDocument
26+
2827

2928
/**
3029
* The immutable [[Document]] companion object for easy creation.
@@ -86,22 +85,22 @@ object Document {
8685
* An immutable Document implementation.
8786
*
8887
* A strictly typed `Map[String, BsonValue]` like structure that traverses the elements in insertion order. Unlike native scala maps there
89-
* is no variance in the value type and it always has to be a `BsonValue`. The [[com.mongodb.scala.reactivestreams.client.Implicits]]
88+
* is no variance in the value type and it always has to be a `BsonValue`. The [[com.mongodb.scala.Implicits]]
9089
* helper provides simple interactions with Documents taking native data types and converting them to `BsonValues`.
9190
*
9291
* @note All user operations on the document are immutable. The *only* time the document can mutate state is when an `_id` is added by
93-
* the underlying [[com.mongodb.scala.reactivestreams.client.codecs.ImmutableDocumentCodec]] codec on insertion to the database.
92+
* the underlying [[com.mongodb.scala.codecs.ImmutableDocumentCodec]] codec on insertion to the database.
9493
* @param underlying the underlying BsonDocument which stores the data.
9594
*/
96-
case class Document(protected[client] val underlying: BsonDocument)
97-
extends collection.BaseDocument[Document] with TraversableLike[(String, BsonValue), Document] {
95+
case class Document(protected[scala] val underlying: BsonDocument)
96+
extends BaseDocument[Document] with TraversableLike[(String, BsonValue), Document] {
9897

9998
/**
10099
* Creates a new immutable document
101100
* @param underlying the underlying BsonDocument
102101
* @return a new document
103102
*/
104-
protected[client] def apply(underlying: BsonDocument) = new Document(underlying)
103+
protected[scala] def apply(underlying: BsonDocument) = new Document(underlying)
105104

106105
/**
107106
* Applies a function `f` to all elements of this document.
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,16 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.mongodb.scala.reactivestreams.client.collection.mutable
18-
19-
import com.mongodb.scala.reactivestreams.client.collection
20-
import org.bson.{ BsonDocument, BsonValue }
17+
package com.mongodb.scala.collection.mutable
2118

2219
import scala.collection.JavaConverters._
2320
import scala.collection.generic.CanBuildFrom
24-
import scala.collection.mutable.{ Builder, ListBuffer }
25-
import scala.collection.{ Traversable, TraversableLike, TraversableOnce }
21+
import scala.collection.mutable.{Builder, ListBuffer}
22+
import scala.collection.{Traversable, TraversableLike, TraversableOnce}
23+
24+
import org.bson.{BsonDocument, BsonValue}
25+
import com.mongodb.scala.collection.BaseDocument
26+
2627

2728
/**
2829
* Mutable [[Document]] companion object for easy creation.
@@ -84,20 +85,20 @@ object Document {
8485
* An mutable Document implementation.
8586
*
8687
* A strictly typed `Map[String, BsonValue]` like structure that traverses the elements in insertion order. Unlike native scala maps there
87-
* is no variance in the value type and it always has to be a `BsonValue`. The [[com.mongodb.scala.reactivestreams.client.Implicits]]
88+
* is no variance in the value type and it always has to be a `BsonValue`. The [[com.mongodb.scala.Implicits]]
8889
* helper provides simple interactions with Documents taking native data types and converting them to `BsonValues`.
8990
*
9091
* @param underlying the underlying BsonDocument which stores the data.
9192
*/
92-
case class Document(protected[client] val underlying: BsonDocument)
93-
extends collection.BaseDocument[Document] with TraversableLike[(String, BsonValue), Document] {
93+
case class Document(protected[scala] val underlying: BsonDocument)
94+
extends BaseDocument[Document] with TraversableLike[(String, BsonValue), Document] {
9495

9596
/**
9697
* Creates a new immutable document
9798
* @param underlying the underlying BsonDocument
9899
* @return a new document
99100
*/
100-
protected[client] def apply(underlying: BsonDocument) = new Document(underlying)
101+
protected[scala] def apply(underlying: BsonDocument) = new Document(underlying)
101102

102103
/**
103104
* Applies a function `f` to all elements of this document.

0 commit comments

Comments
 (0)