Skip to content

Commit cc2a5a1

Browse files
committed
added index handling
1 parent 68b014f commit cc2a5a1

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

src/main/scala/com/sfxcode/nosql/mongo/operation/Base.scala

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package com.sfxcode.nosql.mongo.operation
22

33
import com.sfxcode.nosql.mongo._
4-
import com.sfxcode.nosql.mongo.bson.BsonConverter._
54

5+
import org.mongodb.scala.model.Sorts._
6+
7+
import com.sfxcode.nosql.mongo.bson.BsonConverter._
68
import com.typesafe.scalalogging.LazyLogging
79
import org.bson.BsonValue
810
import org.mongodb.scala.bson.conversions.Bson
9-
import org.mongodb.scala.{ Completed, DistinctObservable, Document, MongoCollection, Observable, Observer }
11+
import org.mongodb.scala.model.IndexOptions
12+
import org.mongodb.scala.{ Completed, DistinctObservable, Document, MongoCollection, Observable, Observer, SingleObservable }
1013

1114
import scala.reflect.ClassTag
1215

@@ -30,6 +33,27 @@ abstract class Base[A]()(implicit ct: ClassTag[A]) extends LazyLogging {
3033

3134
def dropResult(): Completed = drop().headResult()
3235

36+
def createIndexForField(field: String, sortAscending: Boolean = true): SingleObservable[String] = {
37+
if (sortAscending)
38+
createIndex(ascending(field))
39+
else
40+
createIndex(descending(field))
41+
}
42+
43+
def createIndexForFieldResult(field: String, sortAscending: Boolean = true): String = createIndexForField(field, sortAscending)
44+
45+
def createIndex(key: Bson, options: IndexOptions = IndexOptions()): SingleObservable[String] = coll.createIndex(key, options)
46+
47+
def createsIndexResult(key: Bson, options: IndexOptions = IndexOptions()): String = createIndex(key, options)
48+
49+
def dropIndexForName(name: String): SingleObservable[Completed] = coll.dropIndex(name)
50+
51+
def dropIndexForNameResult(name: String): Completed = dropIndexForName(name)
52+
53+
def dropIndex(keys: Bson): SingleObservable[Completed] = coll.dropIndex(keys)
54+
55+
def dropIndexResult(keys: Bson): Completed = dropIndex(keys)
56+
3357
}
3458

3559
class SimpleCompletedObserver[T] extends Observer[T] with LazyLogging {

src/test/scala/com/sfxcode/nosql/mongo/model/Person.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ case class Person(
2727
friends: List[Friend],
2828
greeting: String,
2929
favoriteFruit: String,
30-
_id: ObjectId = new ObjectId()
31-
)
30+
_id: ObjectId = new ObjectId())
3231

3332
case class Friend(id: Long, name: String)
3433

src/test/scala/com/sfxcode/nosql/mongo/operation/BaseSpec.scala

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.sfxcode.nosql.mongo.operation
22

3-
import com.sfxcode.nosql.mongo.tour.Database._
43
import com.sfxcode.nosql.mongo._
54
import com.sfxcode.nosql.mongo.model.{ Author, Book }
5+
import com.sfxcode.nosql.mongo.tour.Database._
66
import org.specs2.mutable.Specification
77
import org.specs2.specification.BeforeAll
88

@@ -39,6 +39,18 @@ class BaseSpec extends Specification with BeforeAll {
3939
genderList must have size 1
4040
}
4141

42+
"must create / drop indexes for key" in {
43+
44+
val createIndexResult = PersonDAO.createIndexForFieldResult("name")
45+
46+
createIndexResult must be equalTo "name_1"
47+
48+
PersonDAO.createIndexForFieldResult("name") must be equalTo "name_1"
49+
50+
PersonDAO.dropIndexForNameResult("name_1") must not beNull
51+
52+
}
53+
4254
}
4355

4456
override def beforeAll: Unit = BookDAO.dropResult()

0 commit comments

Comments
 (0)