From 7392316d4fa7d80d02ef41ecaaed9e7e70fcdf0b Mon Sep 17 00:00:00 2001 From: Anna Grintsevich Date: Sun, 15 May 2016 16:38:34 +0300 Subject: [PATCH 01/16] Added mongodb base functionality (fixed comments) --- build.sbt | 11 ++ persistence/build.sbt | 20 +++ .../src/main/resources/application.conf | 6 + .../persistence/dao/SongsDAO.scala | 117 ++++++++++++++++++ 4 files changed, 154 insertions(+) create mode 100644 persistence/build.sbt create mode 100644 persistence/src/main/resources/application.conf create mode 100644 persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/SongsDAO.scala diff --git a/build.sbt b/build.sbt index b8ff8f3..7d9704a 100644 --- a/build.sbt +++ b/build.sbt @@ -1,3 +1,9 @@ +import _root_.sbt.Keys._ + +name := "lyrics-engine" + +version := "1.0-SNAPSHOT" + name := "lyrics-engine" lazy val commonSettings = Seq( @@ -23,3 +29,8 @@ lazy val root = (project in file(".")) .aggregate(domain, parser, api) .enablePlugins(UniversalPlugin, JavaAppPackaging) +lazy val domain = project in file("domain") +lazy val parser = project in file("parser") dependsOn domain +lazy val persistence = project in file("persistence") dependsOn domain +lazy val api = project in file("api") + diff --git a/persistence/build.sbt b/persistence/build.sbt new file mode 100644 index 0000000..4ffd3cb --- /dev/null +++ b/persistence/build.sbt @@ -0,0 +1,20 @@ +import sbt.Keys._ + +name := "persistence" + +organization := "scalalab3.lyricsengine" + +version := "0.1" + +scalaVersion := "2.11.8" + +resolvers += "releases" at "https://oss.sonatype.org/content/groups/scala-tools" + + +libraryDependencies ++= + Seq( + "org.scalatest" % "scalatest_2.11" % "2.2.6", + "org.mongodb" %% "casbah" % "3.1.1", + "com.typesafe" % "config" % "1.3.0" + ) + diff --git a/persistence/src/main/resources/application.conf b/persistence/src/main/resources/application.conf new file mode 100644 index 0000000..b2e42bd --- /dev/null +++ b/persistence/src/main/resources/application.conf @@ -0,0 +1,6 @@ +mongo { + db= "lyrics" + songscollection= "songs" + wordsdefinitions= "wordsdefinitions" +} + diff --git a/persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/SongsDAO.scala b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/SongsDAO.scala new file mode 100644 index 0000000..ce2ccfd --- /dev/null +++ b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/SongsDAO.scala @@ -0,0 +1,117 @@ +package scalalab3.lyricsengine.persistence.dao + +import com.mongodb.casbah.Imports._ +import com.typesafe.config._ +import java.io.File +import scalalab3.lyricsengine.domain._ + +object SongsDAO extends App{ + + val conf = ConfigFactory.defaultApplication() + + private val dbName = conf.getString("mongo.db") + private val songsCollectionName = conf.getString("mongo.songscollection") + private val wDefinitionsCollName = conf.getString("mongo.wordsdefinitions") + + private val songs: MongoCollection = MongoConnection()(dbName)(songsCollectionName) + private val wordsDefinitions: MongoCollection = MongoConnection()(dbName)(wDefinitionsCollName) + + def addSongs(songsToAdd: Seq[Song], version: Option[Int] = None) = { + val builder = songs.initializeOrderedBulkOperation //will automatically split the operation into batches + for { + song <- songsToAdd + } builder.insert(songToMongoDBObj(song, version)) + + } + + def addWordsDefinition(wd: WordsDefinition, version: Option[Int] = None) = { + val builder = wordsDefinitions.initializeOrderedBulkOperation + builder.insert(wdToMongoDbObject(wd, version)) + } + + def findSongs(version: Option[Int] = None): Seq[Song] = { + val query = MongoDBObject("version" -> version.get) + val result = songs.find(query) + + val songsSet = for { + song <- result + } yield songFromMongoDBObj(song) + + songsSet.toSeq + } + + def findWordsDefinitions(version: Option[Int] = None): Seq[Map[Int,Int]] ={ + val query = MongoDBObject("version" -> version.get) + val result = songs.find(query) + + val wdSet = for { + wd <- result + } yield wdFromMongoDBObj(wd) + + wdSet.toSeq + } + + private def songFromMongoDBObj(obj: MongoDBObject):Song = { + val _msdTrackId = obj.getAs[String]("msdTrackId").get + val _mxmTrackId = obj.getAs[String]("mxmTrackId").get + + val _words = obj.getAs[Map[String, String]]("words").get.map {case (k,v) => (k.toInt, v.toInt)} + + Song(msdTrackId = _msdTrackId, mxmTrackId = _mxmTrackId, words = _words) + + } + + private def wdFromMongoDBObj(obj: MongoDBObject): Map[Int, Int] = { + + val version = obj.getAs[Int]("version").get + + val words = obj.getAs[Map[String, String]]("wordsDefinitions").get.map {case (k,v) => (k.toInt, v.toInt)} + words + } + + private def songToMongoDBObj(song: Song, version: Option[Int] = None): MongoDBObject = { + val songBuilder = MongoDBObject.newBuilder + songBuilder += ("msdTrackId" -> song.msdTrackId, + "mxmTrackId" -> song.mxmTrackId, + "words" -> getWDefinitions(song), + "version" -> version.getOrElse(getLastVersion) + ) + songBuilder.result() + } + + private def wdToMongoDbObject(wd: WordsDefinition, version: Option[Int] = None): MongoDBObject = { + val wdBuilder = MongoDBObject.newBuilder + wdBuilder += ( + "wordsDefinitions" -> transformWordsDef(wd), + "version" -> version.getOrElse(getLastVersion) + ) + wdBuilder.result() + } + + private def transformWordsDef(w: WordsDefinition): MongoDBObject = { + val collection = MongoDBObject.newBuilder + val transformed = w.map { case (k, v) => (k.toString, v) } + collection ++= transformed + collection.result() + } + + private def getWDefinitions(song: Song):MongoDBObject = { + val words = MongoDBObject.newBuilder + val songWords = song.words.map { case (k, v) => (k.toString, v) } + words ++= songWords + words.result() + } + + private def getLastVersion: Option[Int] = { + val query = MongoDBObject() // All documents + val fields = MongoDBObject("version" -> 1) // Only return `version` + val orderBy = MongoDBObject("version" -> -1) // Order by version descending + + val result = wordsDefinitions.findOne(query, fields, orderBy) + + result.get.getAs[Int]("version") + } + +// wordsDefinitions.find() +// for { wd <- wordsDefinitions} yield wd +} \ No newline at end of file From a6978e9aae6c64fdbec9e9fc10689c8b5f7aa1da Mon Sep 17 00:00:00 2001 From: agrintsevich Date: Mon, 16 May 2016 11:44:27 +0300 Subject: [PATCH 02/16] Update build.sbt --- build.sbt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/build.sbt b/build.sbt index 7d9704a..fd88e6c 100644 --- a/build.sbt +++ b/build.sbt @@ -29,8 +29,8 @@ lazy val root = (project in file(".")) .aggregate(domain, parser, api) .enablePlugins(UniversalPlugin, JavaAppPackaging) -lazy val domain = project in file("domain") -lazy val parser = project in file("parser") dependsOn domain -lazy val persistence = project in file("persistence") dependsOn domain -lazy val api = project in file("api") +lazy val persistence = (project in file("persistence")) + .settings(commonSettings: _*) + .dependsOn(domain) + From e4267b0e71033960044e9eeeb950d217636cdde6 Mon Sep 17 00:00:00 2001 From: agrintsevich Date: Mon, 16 May 2016 11:46:09 +0300 Subject: [PATCH 03/16] Update build.sbt --- build.sbt | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/build.sbt b/build.sbt index fd88e6c..319336d 100644 --- a/build.sbt +++ b/build.sbt @@ -22,15 +22,17 @@ lazy val parser = (project in file("parser")) .dependsOn(domain) lazy val api = (project in file("api")) .settings(commonSettings: _*) + +lazy val persistence = (project in file("persistence")) + .settings(commonSettings: _*) + .dependsOn(domain) lazy val root = (project in file(".")) .settings(commonSettings: _*) .dependsOn(api) - .aggregate(domain, parser, api) + .aggregate(domain, parser, api, persistence) .enablePlugins(UniversalPlugin, JavaAppPackaging) -lazy val persistence = (project in file("persistence")) - .settings(commonSettings: _*) - .dependsOn(domain) + From 73d08d877fdc8c2c47b619caa1c8def9b90b4a1c Mon Sep 17 00:00:00 2001 From: Anna Grintsevich Date: Tue, 17 May 2016 23:39:41 +0300 Subject: [PATCH 04/16] decomposition --- .../src/main/resources/application.conf | 6 +- .../persistence/dao/SongsDAO.scala | 117 ------------------ .../persistence/dao/StorageComponent.scala | 23 ++++ .../dao/StorageComponentImpl.scala | 112 +++++++++++++++++ .../persistence/dao/mongo/MongoConfig.scala | 32 +++++ .../persistence/dao/mongo/MongoContext.scala | 37 ++++++ .../mongo/constants/DefaultConfigKeys.scala | 14 +++ .../mongo/constants/DefaultConfigValues.scala | 12 ++ 8 files changed, 235 insertions(+), 118 deletions(-) delete mode 100644 persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/SongsDAO.scala create mode 100644 persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/StorageComponent.scala create mode 100644 persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/StorageComponentImpl.scala create mode 100644 persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/mongo/MongoConfig.scala create mode 100644 persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/mongo/MongoContext.scala create mode 100644 persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/mongo/constants/DefaultConfigKeys.scala create mode 100644 persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/mongo/constants/DefaultConfigValues.scala diff --git a/persistence/src/main/resources/application.conf b/persistence/src/main/resources/application.conf index b2e42bd..e6cbba5 100644 --- a/persistence/src/main/resources/application.conf +++ b/persistence/src/main/resources/application.conf @@ -1,5 +1,9 @@ mongo { - db= "lyrics" + host= "localhost" + port= "8080" + user="" + password="" + dbname= "lyrics" songscollection= "songs" wordsdefinitions= "wordsdefinitions" } diff --git a/persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/SongsDAO.scala b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/SongsDAO.scala deleted file mode 100644 index ce2ccfd..0000000 --- a/persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/SongsDAO.scala +++ /dev/null @@ -1,117 +0,0 @@ -package scalalab3.lyricsengine.persistence.dao - -import com.mongodb.casbah.Imports._ -import com.typesafe.config._ -import java.io.File -import scalalab3.lyricsengine.domain._ - -object SongsDAO extends App{ - - val conf = ConfigFactory.defaultApplication() - - private val dbName = conf.getString("mongo.db") - private val songsCollectionName = conf.getString("mongo.songscollection") - private val wDefinitionsCollName = conf.getString("mongo.wordsdefinitions") - - private val songs: MongoCollection = MongoConnection()(dbName)(songsCollectionName) - private val wordsDefinitions: MongoCollection = MongoConnection()(dbName)(wDefinitionsCollName) - - def addSongs(songsToAdd: Seq[Song], version: Option[Int] = None) = { - val builder = songs.initializeOrderedBulkOperation //will automatically split the operation into batches - for { - song <- songsToAdd - } builder.insert(songToMongoDBObj(song, version)) - - } - - def addWordsDefinition(wd: WordsDefinition, version: Option[Int] = None) = { - val builder = wordsDefinitions.initializeOrderedBulkOperation - builder.insert(wdToMongoDbObject(wd, version)) - } - - def findSongs(version: Option[Int] = None): Seq[Song] = { - val query = MongoDBObject("version" -> version.get) - val result = songs.find(query) - - val songsSet = for { - song <- result - } yield songFromMongoDBObj(song) - - songsSet.toSeq - } - - def findWordsDefinitions(version: Option[Int] = None): Seq[Map[Int,Int]] ={ - val query = MongoDBObject("version" -> version.get) - val result = songs.find(query) - - val wdSet = for { - wd <- result - } yield wdFromMongoDBObj(wd) - - wdSet.toSeq - } - - private def songFromMongoDBObj(obj: MongoDBObject):Song = { - val _msdTrackId = obj.getAs[String]("msdTrackId").get - val _mxmTrackId = obj.getAs[String]("mxmTrackId").get - - val _words = obj.getAs[Map[String, String]]("words").get.map {case (k,v) => (k.toInt, v.toInt)} - - Song(msdTrackId = _msdTrackId, mxmTrackId = _mxmTrackId, words = _words) - - } - - private def wdFromMongoDBObj(obj: MongoDBObject): Map[Int, Int] = { - - val version = obj.getAs[Int]("version").get - - val words = obj.getAs[Map[String, String]]("wordsDefinitions").get.map {case (k,v) => (k.toInt, v.toInt)} - words - } - - private def songToMongoDBObj(song: Song, version: Option[Int] = None): MongoDBObject = { - val songBuilder = MongoDBObject.newBuilder - songBuilder += ("msdTrackId" -> song.msdTrackId, - "mxmTrackId" -> song.mxmTrackId, - "words" -> getWDefinitions(song), - "version" -> version.getOrElse(getLastVersion) - ) - songBuilder.result() - } - - private def wdToMongoDbObject(wd: WordsDefinition, version: Option[Int] = None): MongoDBObject = { - val wdBuilder = MongoDBObject.newBuilder - wdBuilder += ( - "wordsDefinitions" -> transformWordsDef(wd), - "version" -> version.getOrElse(getLastVersion) - ) - wdBuilder.result() - } - - private def transformWordsDef(w: WordsDefinition): MongoDBObject = { - val collection = MongoDBObject.newBuilder - val transformed = w.map { case (k, v) => (k.toString, v) } - collection ++= transformed - collection.result() - } - - private def getWDefinitions(song: Song):MongoDBObject = { - val words = MongoDBObject.newBuilder - val songWords = song.words.map { case (k, v) => (k.toString, v) } - words ++= songWords - words.result() - } - - private def getLastVersion: Option[Int] = { - val query = MongoDBObject() // All documents - val fields = MongoDBObject("version" -> 1) // Only return `version` - val orderBy = MongoDBObject("version" -> -1) // Order by version descending - - val result = wordsDefinitions.findOne(query, fields, orderBy) - - result.get.getAs[Int]("version") - } - -// wordsDefinitions.find() -// for { wd <- wordsDefinitions} yield wd -} \ No newline at end of file diff --git a/persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/StorageComponent.scala b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/StorageComponent.scala new file mode 100644 index 0000000..4057c88 --- /dev/null +++ b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/StorageComponent.scala @@ -0,0 +1,23 @@ +package scalalab3.lyricsengine.persistence.dao + +import scalalab3.lyricsengine.domain._ + +/** + * Created by annie on 5/17/16. + */ +trait StorageComponent { + + val storage: Storage + + trait Storage { + + def addSongs(songsToAdd: Seq[Song], version: Option[Int] = None) + + def addWordsDefinition(wd: WordsDefinition, version: Option[Int] = None) + + def findSongs(version: Option[Int] = None): Seq[Song] + + def findWordsDefinitions(version: Option[Int] = None): Seq[Map[Int, Int]] + } + +} diff --git a/persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/StorageComponentImpl.scala b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/StorageComponentImpl.scala new file mode 100644 index 0000000..726d9df --- /dev/null +++ b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/StorageComponentImpl.scala @@ -0,0 +1,112 @@ +package scalalab3.lyricsengine.persistence.dao + +import com.mongodb.casbah.Imports._ + +import scalalab3.lyricsengine.domain._ +import scalalab3.lyricsengine.persistence.dao.mongo.MongoContext + +trait StorageComponentImpl extends StorageComponent { + + override val storage: Storage + + + class StorageImpl(implicit context: MongoContext) extends Storage { + + def addSongs(songsToAdd: Seq[Song], version: Option[Int] = None) = { + val builder = context.songsCollection.initializeOrderedBulkOperation //will automatically split the operation into batches + for { + song <- songsToAdd + } builder.insert(songToMongoDBObj(song, version)) + + } + + def addWordsDefinition(wd: WordsDefinition, version: Option[Int] = None) = { + val builder = context.wdCollection.initializeOrderedBulkOperation + builder.insert(wdToMongoDbObject(wd, version)) + } + + def findSongs(version: Option[Int] = None): Seq[Song] = { + val query = MongoDBObject("version" -> version.get) + val result = context.songsCollection.find(query) + + val songsSet = for { + song <- result + } yield songFromMongoDBObj(song) + + songsSet.toSeq + } + + def findWordsDefinitions(version: Option[Int] = None): Seq[Map[Int, Int]] = { + val query = MongoDBObject("version" -> version.get) + val result = context.wdCollection.find(query) + + val wdSet = for { + wd <- result + } yield wdFromMongoDBObj(wd) + + wdSet.toSeq + } + + private def songFromMongoDBObj(obj: MongoDBObject): Song = { + val _msdTrackId = obj.getAs[String]("msdTrackId").get + val _mxmTrackId = obj.getAs[String]("mxmTrackId").get + + val _words = obj.getAs[Map[String, String]]("words").get.map { case (k, v) => (k.toInt, v.toInt) } + + Song(msdTrackId = _msdTrackId, mxmTrackId = _mxmTrackId, words = _words) + + } + + private def wdFromMongoDBObj(obj: MongoDBObject): Map[Int, Int] = { + + val version = obj.getAs[Int]("version").get + + val words = obj.getAs[Map[String, String]]("wordsDefinitions").get.map { case (k, v) => (k.toInt, v.toInt) } + words + } + + private def songToMongoDBObj(song: Song, version: Option[Int] = None): MongoDBObject = { + val songBuilder = MongoDBObject.newBuilder + songBuilder +=("msdTrackId" -> song.msdTrackId, + "mxmTrackId" -> song.mxmTrackId, + "words" -> getWDefinitions(song), + "version" -> version.getOrElse(getLastVersion) + ) + songBuilder.result() + } + + private def wdToMongoDbObject(wd: WordsDefinition, version: Option[Int] = None): MongoDBObject = { + val wdBuilder = MongoDBObject.newBuilder + wdBuilder +=( + "wordsDefinitions" -> transformWordsDef(wd), + "version" -> version.getOrElse(getLastVersion) + ) + wdBuilder.result() + } + + private def transformWordsDef(w: WordsDefinition): MongoDBObject = { + val collection = MongoDBObject.newBuilder + val transformed = w.map { case (k, v) => (k.toString, v) } + collection ++= transformed + collection.result() + } + + private def getWDefinitions(song: Song): MongoDBObject = { + val words = MongoDBObject.newBuilder + val songWords = song.words.map { case (k, v) => (k.toString, v) } + words ++= songWords + words.result() + } + + private def getLastVersion: Option[Int] = { + val query = MongoDBObject() // All documents + val fields = MongoDBObject("version" -> 1) // Only return `version` + val orderBy = MongoDBObject("version" -> -1) // Order by version descending + + val result = context.wdCollection.findOne(query, fields, orderBy) + + result.get.getAs[Int]("version") + } + } + +} \ No newline at end of file diff --git a/persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/mongo/MongoConfig.scala b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/mongo/MongoConfig.scala new file mode 100644 index 0000000..e4d5d49 --- /dev/null +++ b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/mongo/MongoConfig.scala @@ -0,0 +1,32 @@ +package scalalab3.lyricsengine.persistence.dao.mongo + +import com.typesafe.config.ConfigFactory + +import constants.DefaultConfigValues._ +import constants.DefaultConfigKeys._ +import scala.util.Try + +case class MongoConfig(host: String = defaultHost, + port: Int = defaultPort, + user: String = defaultUser, + password: String = defaultPassword, + dbName: String = defaultDbName, + songsCollection: String = defaultSongsCollection, + wdCollection: String = defaultWDCollection) + +object MongoConfig { + private val config = ConfigFactory.load() + + def load(): MongoConfig = + MongoConfig( + getString(host, defaultHost), + getInt(port, defaultPort), + getString(user, defaultUser), + getString(password, defaultPassword), + getString(dbname, defaultDbName), + getString(songsCollection, defaultSongsCollection), + getString(wordsDefinitions, defaultWDCollection) + ) + private def getString(key: String, defaultValue: String) = Try(config.getString(key)).getOrElse(defaultValue) + private def getInt(key: String, defaultValue: Int) = Try(config.getInt(key)).getOrElse(defaultValue) +} diff --git a/persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/mongo/MongoContext.scala b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/mongo/MongoContext.scala new file mode 100644 index 0000000..1fdf3e2 --- /dev/null +++ b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/mongo/MongoContext.scala @@ -0,0 +1,37 @@ +package scalalab3.lyricsengine.persistence.dao.mongo + +import com.mongodb.BasicDBObjectBuilder +import com.mongodb.casbah.Imports._ +import com.mongodb.casbah.MongoClient + +class MongoContext(val config: MongoConfig) { + + val mongoClient = MongoClient() + val mongoDB = mongoClient(config.dbName) + + implicit val options: DBObject = BasicDBObjectBuilder.start().add("capped", true).add("size", 2000000000l).get() + + def songsCollection = + if (mongoDB.collectionExists(config.songsCollection)) { + mongoDB(config.songsCollection) + } else { + mongoDB.createCollection(config.songsCollection, options) + mongoDB(config.songsCollection) + } + + def wdCollection = { + if (mongoDB.collectionExists(config.wdCollection)) { + mongoDB(config.wdCollection) + } else { + mongoDB.createCollection(config.wdCollection, options) + mongoDB(config.wdCollection) + } + } + + def drop = { + mongoDB.dropDatabase() + } + + + +} diff --git a/persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/mongo/constants/DefaultConfigKeys.scala b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/mongo/constants/DefaultConfigKeys.scala new file mode 100644 index 0000000..294cc56 --- /dev/null +++ b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/mongo/constants/DefaultConfigKeys.scala @@ -0,0 +1,14 @@ +package scalalab3.lyricsengine.persistence.dao.mongo.constants + + +object DefaultConfigKeys { + val mongo = "mongo" + val host = s"$mongo.host" + val port = s"$mongo.port" + val user = s"$mongo.user" + val dbname = s"$mongo.dbname" + val password = s"$mongo.password" + val songsCollection = s"$mongo.songscollection" + val wordsDefinitions = s"$mongo.wordsdefinitions" + +} diff --git a/persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/mongo/constants/DefaultConfigValues.scala b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/mongo/constants/DefaultConfigValues.scala new file mode 100644 index 0000000..73106d0 --- /dev/null +++ b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/mongo/constants/DefaultConfigValues.scala @@ -0,0 +1,12 @@ +package scalalab3.lyricsengine.persistence.dao.mongo.constants + + +object DefaultConfigValues { + val defaultHost = "localhost" + val defaultPort = 27017 + val defaultUser = "admin" + val defaultPassword = "" + val defaultDbName = "lyrics" + val defaultSongsCollection = "songs" + val defaultWDCollection = "songs" +} From ca67f9faf6398ee4acc14bfd7a49752a326e2674 Mon Sep 17 00:00:00 2001 From: Anna Grintsevich Date: Tue, 17 May 2016 23:41:25 +0300 Subject: [PATCH 05/16] decomposition --- .../lyricsengine/persistence/{dao => }/StorageComponent.scala | 2 +- .../persistence/{dao => }/StorageComponentImpl.scala | 4 ++-- .../persistence/{dao => }/mongo/MongoConfig.scala | 2 +- .../persistence/{dao => }/mongo/MongoContext.scala | 2 +- .../{dao => }/mongo/constants/DefaultConfigKeys.scala | 3 +-- .../{dao => }/mongo/constants/DefaultConfigValues.scala | 3 +-- 6 files changed, 7 insertions(+), 9 deletions(-) rename persistence/src/main/scala/scalalab3/lyricsengine/persistence/{dao => }/StorageComponent.scala (90%) rename persistence/src/main/scala/scalalab3/lyricsengine/persistence/{dao => }/StorageComponentImpl.scala (96%) rename persistence/src/main/scala/scalalab3/lyricsengine/persistence/{dao => }/mongo/MongoConfig.scala (95%) rename persistence/src/main/scala/scalalab3/lyricsengine/persistence/{dao => }/mongo/MongoContext.scala (94%) rename persistence/src/main/scala/scalalab3/lyricsengine/persistence/{dao => }/mongo/constants/DefaultConfigKeys.scala (82%) rename persistence/src/main/scala/scalalab3/lyricsengine/persistence/{dao => }/mongo/constants/DefaultConfigValues.scala (79%) diff --git a/persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/StorageComponent.scala b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponent.scala similarity index 90% rename from persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/StorageComponent.scala rename to persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponent.scala index 4057c88..4cffc26 100644 --- a/persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/StorageComponent.scala +++ b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponent.scala @@ -1,4 +1,4 @@ -package scalalab3.lyricsengine.persistence.dao +package scalalab3.lyricsengine.persistence import scalalab3.lyricsengine.domain._ diff --git a/persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/StorageComponentImpl.scala b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl.scala similarity index 96% rename from persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/StorageComponentImpl.scala rename to persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl.scala index 726d9df..415bd9f 100644 --- a/persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/StorageComponentImpl.scala +++ b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl.scala @@ -1,9 +1,9 @@ -package scalalab3.lyricsengine.persistence.dao +package scalalab3.lyricsengine.persistence import com.mongodb.casbah.Imports._ import scalalab3.lyricsengine.domain._ -import scalalab3.lyricsengine.persistence.dao.mongo.MongoContext +import scalalab3.lyricsengine.persistence.mongo.MongoContext trait StorageComponentImpl extends StorageComponent { diff --git a/persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/mongo/MongoConfig.scala b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/mongo/MongoConfig.scala similarity index 95% rename from persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/mongo/MongoConfig.scala rename to persistence/src/main/scala/scalalab3/lyricsengine/persistence/mongo/MongoConfig.scala index e4d5d49..54000fe 100644 --- a/persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/mongo/MongoConfig.scala +++ b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/mongo/MongoConfig.scala @@ -1,4 +1,4 @@ -package scalalab3.lyricsengine.persistence.dao.mongo +package scalalab3.lyricsengine.persistence.mongo import com.typesafe.config.ConfigFactory diff --git a/persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/mongo/MongoContext.scala b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/mongo/MongoContext.scala similarity index 94% rename from persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/mongo/MongoContext.scala rename to persistence/src/main/scala/scalalab3/lyricsengine/persistence/mongo/MongoContext.scala index 1fdf3e2..938d723 100644 --- a/persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/mongo/MongoContext.scala +++ b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/mongo/MongoContext.scala @@ -1,4 +1,4 @@ -package scalalab3.lyricsengine.persistence.dao.mongo +package scalalab3.lyricsengine.persistence.mongo import com.mongodb.BasicDBObjectBuilder import com.mongodb.casbah.Imports._ diff --git a/persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/mongo/constants/DefaultConfigKeys.scala b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/mongo/constants/DefaultConfigKeys.scala similarity index 82% rename from persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/mongo/constants/DefaultConfigKeys.scala rename to persistence/src/main/scala/scalalab3/lyricsengine/persistence/mongo/constants/DefaultConfigKeys.scala index 294cc56..166ea53 100644 --- a/persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/mongo/constants/DefaultConfigKeys.scala +++ b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/mongo/constants/DefaultConfigKeys.scala @@ -1,5 +1,4 @@ -package scalalab3.lyricsengine.persistence.dao.mongo.constants - +package scalalab3.lyricsengine.persistence.mongo.constants object DefaultConfigKeys { val mongo = "mongo" diff --git a/persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/mongo/constants/DefaultConfigValues.scala b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/mongo/constants/DefaultConfigValues.scala similarity index 79% rename from persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/mongo/constants/DefaultConfigValues.scala rename to persistence/src/main/scala/scalalab3/lyricsengine/persistence/mongo/constants/DefaultConfigValues.scala index 73106d0..ce27c88 100644 --- a/persistence/src/main/scala/scalalab3/lyricsengine/persistence/dao/mongo/constants/DefaultConfigValues.scala +++ b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/mongo/constants/DefaultConfigValues.scala @@ -1,5 +1,4 @@ -package scalalab3.lyricsengine.persistence.dao.mongo.constants - +package scalalab3.lyricsengine.persistence.mongo.constants object DefaultConfigValues { val defaultHost = "localhost" From 5e3e070b918747c7d5ba08d74f6bf3c9147a986f Mon Sep 17 00:00:00 2001 From: NellyHargeliya Date: Fri, 20 May 2016 11:50:10 +0300 Subject: [PATCH 06/16] add Test mondodb --- .../test/scala/StorageComponent$Test.scala | 6 ++ .../scala/StorageComponentImpl$Test.scala | 65 +++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 persistence/src/test/scala/StorageComponent$Test.scala create mode 100644 persistence/src/test/scala/StorageComponentImpl$Test.scala diff --git a/persistence/src/test/scala/StorageComponent$Test.scala b/persistence/src/test/scala/StorageComponent$Test.scala new file mode 100644 index 0000000..04a4908 --- /dev/null +++ b/persistence/src/test/scala/StorageComponent$Test.scala @@ -0,0 +1,6 @@ +/** + * Created by Nelly on 19.05.2016. + */ +class StorageComponent$Test { + +} diff --git a/persistence/src/test/scala/StorageComponentImpl$Test.scala b/persistence/src/test/scala/StorageComponentImpl$Test.scala new file mode 100644 index 0000000..e54b0b0 --- /dev/null +++ b/persistence/src/test/scala/StorageComponentImpl$Test.scala @@ -0,0 +1,65 @@ + + +import jdk.nashorn.internal.ir.debug.ObjectSizeCalculator.MemoryLayoutSpecification +import org.scalatest.BeforeAndAfterAll + +import scalalab3.lyricsengine.domain.{DataSet, Song} +import scalalab3.lyricsengine.persistence.StorageComponentImpl +import java.time.OffsetDateTime +import java.util.UUID +import org.scalatest +import org.specs2.mutable.Specification + +import scalalab3.lyricsengine.persistence.mongo.{MongoContext,MongoConfig} + +import scala.util.Try + +class StorageComponentImpl$Test extends Specification with BeforeAndAfterAll { + sequential + + val tryMongoContext = Try(new MongoContext(MongoConfig.load())) + + + // val tryMongo = Try(MongoConnection("localhost", 12345)) + + "Mongo Test" >> { + if (tryMongoContext.isSuccess) { + implicit val s=tryMongoContext.get + val now = OffsetDateTime.now() + val words = Map(1 -> "i", 2 -> "the", 3 -> "you", 4 -> "to", 5 -> "and") + val firstSong = Song("TRZZZYV128F92E996D", "6849828", Map(1 -> 10, 2 -> 6, 3 -> 20, 5 -> 2, 7 -> 30)) + val secondSong = Song("TRZZZYX128F92D32C6", "681124", Map(1 -> 4, 2 -> 18, 4 -> 3, 5 -> 6, 6 -> 9)) + val seqSong=Seq(firstSong, secondSong) + DataSet(words,seqSong ) + val mongoStorage = new StorageComponentImpl { + override val storage: Storage = new StorageImpl + }.storage + + + "add Song " in { + mongoStorage.addSongs(seqSong) + } + + "add WordsDefinitions " in { + mongoStorage.addWordsDefinition(words) + } + + "find Songs" in{ + mongoStorage.findSongs() + } + + "find WordsDefinitions" in{ + mongoStorage.findWordsDefinitions() + } + + + + + }else "Skipped Test" >> skipped ("MongoDB is not available in ") + } + def uuid() = Some(UUID.randomUUID()) + def drop() = for (m <- tryMongoContext) m.dbDrop() + + override def beforeAll(): Unit = drop() + override def afterAll(): Unit = drop() +} From cbf450e147de07c93672e1e3d2a5a8e8a37bf0de Mon Sep 17 00:00:00 2001 From: angr0316 Date: Fri, 20 May 2016 12:02:18 +0300 Subject: [PATCH 07/16] version, same as in global build.sbt --- persistence/build.sbt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/persistence/build.sbt b/persistence/build.sbt index 4ffd3cb..c1ced47 100644 --- a/persistence/build.sbt +++ b/persistence/build.sbt @@ -4,7 +4,7 @@ name := "persistence" organization := "scalalab3.lyricsengine" -version := "0.1" +version := "0.1.0-SNAPSHOT" scalaVersion := "2.11.8" From 7c9144a8215037481a92e31bf4a134d44f2d28d1 Mon Sep 17 00:00:00 2001 From: angr0316 Date: Fri, 20 May 2016 12:33:20 +0300 Subject: [PATCH 08/16] fixes with dependencies --- persistence/build.sbt | 6 ++- .../scala/StorageComponentImpl$Test.scala | 38 +++++++------------ 2 files changed, 19 insertions(+), 25 deletions(-) diff --git a/persistence/build.sbt b/persistence/build.sbt index c1ced47..fa50396 100644 --- a/persistence/build.sbt +++ b/persistence/build.sbt @@ -10,10 +10,14 @@ scalaVersion := "2.11.8" resolvers += "releases" at "https://oss.sonatype.org/content/groups/scala-tools" +val specsV = "3.7.2" +val scalaTestV = "2.2.6" libraryDependencies ++= Seq( - "org.scalatest" % "scalatest_2.11" % "2.2.6", + "org.scalatest" %% "scalatest" % scalaTestV, + "org.specs2" %% "specs2-core" % specsV, + "org.specs2" %% "specs2-matcher-extra" % specsV, "org.mongodb" %% "casbah" % "3.1.1", "com.typesafe" % "config" % "1.3.0" ) diff --git a/persistence/src/test/scala/StorageComponentImpl$Test.scala b/persistence/src/test/scala/StorageComponentImpl$Test.scala index e54b0b0..64cd796 100644 --- a/persistence/src/test/scala/StorageComponentImpl$Test.scala +++ b/persistence/src/test/scala/StorageComponentImpl$Test.scala @@ -1,36 +1,27 @@ - - -import jdk.nashorn.internal.ir.debug.ObjectSizeCalculator.MemoryLayoutSpecification -import org.scalatest.BeforeAndAfterAll - +import org.specs2.specification._ import scalalab3.lyricsengine.domain.{DataSet, Song} import scalalab3.lyricsengine.persistence.StorageComponentImpl -import java.time.OffsetDateTime import java.util.UUID -import org.scalatest import org.specs2.mutable.Specification -import scalalab3.lyricsengine.persistence.mongo.{MongoContext,MongoConfig} +import scalalab3.lyricsengine.persistence.mongo.{MongoContext, MongoConfig} import scala.util.Try -class StorageComponentImpl$Test extends Specification with BeforeAndAfterAll { +class StorageComponentImpl$Test extends Specification with BeforeAfterAll { sequential val tryMongoContext = Try(new MongoContext(MongoConfig.load())) - - // val tryMongo = Try(MongoConnection("localhost", 12345)) - "Mongo Test" >> { if (tryMongoContext.isSuccess) { - implicit val s=tryMongoContext.get - val now = OffsetDateTime.now() + implicit val s = tryMongoContext.get + val words = Map(1 -> "i", 2 -> "the", 3 -> "you", 4 -> "to", 5 -> "and") val firstSong = Song("TRZZZYV128F92E996D", "6849828", Map(1 -> 10, 2 -> 6, 3 -> 20, 5 -> 2, 7 -> 30)) val secondSong = Song("TRZZZYX128F92D32C6", "681124", Map(1 -> 4, 2 -> 18, 4 -> 3, 5 -> 6, 6 -> 9)) - val seqSong=Seq(firstSong, secondSong) - DataSet(words,seqSong ) + val seqSong = Seq(firstSong, secondSong) + DataSet(words, seqSong) val mongoStorage = new StorageComponentImpl { override val storage: Storage = new StorageImpl }.storage @@ -44,21 +35,20 @@ class StorageComponentImpl$Test extends Specification with BeforeAndAfterAll { mongoStorage.addWordsDefinition(words) } - "find Songs" in{ - mongoStorage.findSongs() + "find Songs" in { + mongoStorage.findSongs() } - "find WordsDefinitions" in{ + "find WordsDefinitions" in { mongoStorage.findWordsDefinitions() } - - - - }else "Skipped Test" >> skipped ("MongoDB is not available in ") + } else "Skipped Test" >> skipped("MongoDB is not available in ") } + def uuid() = Some(UUID.randomUUID()) - def drop() = for (m <- tryMongoContext) m.dbDrop() + + def drop() = for (m <- tryMongoContext) m.drop override def beforeAll(): Unit = drop() override def afterAll(): Unit = drop() From 39abcb4ca8160a49a4cb6e72ec5063bab531966c Mon Sep 17 00:00:00 2001 From: angr0316 Date: Fri, 20 May 2016 12:35:27 +0300 Subject: [PATCH 09/16] moving in proper directories --- .../persistence}/StorageComponent$Test.scala | 2 ++ .../persistence}/StorageComponentImpl$Test.scala | 11 ++++++----- 2 files changed, 8 insertions(+), 5 deletions(-) rename persistence/src/test/scala/{ => scalalab3/lyricsengine/persistence}/StorageComponent$Test.scala (63%) rename persistence/src/test/scala/{ => scalalab3/lyricsengine/persistence}/StorageComponentImpl$Test.scala (91%) diff --git a/persistence/src/test/scala/StorageComponent$Test.scala b/persistence/src/test/scala/scalalab3/lyricsengine/persistence/StorageComponent$Test.scala similarity index 63% rename from persistence/src/test/scala/StorageComponent$Test.scala rename to persistence/src/test/scala/scalalab3/lyricsengine/persistence/StorageComponent$Test.scala index 04a4908..45b104c 100644 --- a/persistence/src/test/scala/StorageComponent$Test.scala +++ b/persistence/src/test/scala/scalalab3/lyricsengine/persistence/StorageComponent$Test.scala @@ -1,3 +1,5 @@ +package scalalab3.lyricsengine.persistence + /** * Created by Nelly on 19.05.2016. */ diff --git a/persistence/src/test/scala/StorageComponentImpl$Test.scala b/persistence/src/test/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl$Test.scala similarity index 91% rename from persistence/src/test/scala/StorageComponentImpl$Test.scala rename to persistence/src/test/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl$Test.scala index 64cd796..b8b364e 100644 --- a/persistence/src/test/scala/StorageComponentImpl$Test.scala +++ b/persistence/src/test/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl$Test.scala @@ -1,12 +1,13 @@ -import org.specs2.specification._ -import scalalab3.lyricsengine.domain.{DataSet, Song} -import scalalab3.lyricsengine.persistence.StorageComponentImpl +package scalalab3.lyricsengine.persistence + import java.util.UUID -import org.specs2.mutable.Specification -import scalalab3.lyricsengine.persistence.mongo.{MongoContext, MongoConfig} +import org.specs2.mutable.Specification +import org.specs2.specification._ import scala.util.Try +import scalalab3.lyricsengine.domain.{DataSet, Song} +import scalalab3.lyricsengine.persistence.mongo.{MongoConfig, MongoContext} class StorageComponentImpl$Test extends Specification with BeforeAfterAll { sequential From 28e29d9d5362a26d4dac002e363a94f9ca823991 Mon Sep 17 00:00:00 2001 From: angr0316 Date: Tue, 24 May 2016 11:22:55 +0300 Subject: [PATCH 10/16] tests coverage --- .../persistence/StorageComponent.scala | 6 ++- .../persistence/StorageComponentImpl.scala | 43 +++++++++++++------ .../StorageComponentImpl$Test.scala | 42 ++++++++++++++---- 3 files changed, 68 insertions(+), 23 deletions(-) diff --git a/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponent.scala b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponent.scala index 4cffc26..5fa9abf 100644 --- a/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponent.scala +++ b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponent.scala @@ -17,7 +17,11 @@ trait StorageComponent { def findSongs(version: Option[Int] = None): Seq[Song] - def findWordsDefinitions(version: Option[Int] = None): Seq[Map[Int, Int]] + def findWordsDefinitions(version: Option[Int] = None): Seq[Map[Int, String]] + + def countSongs(): Int + + def countWD(): Int } } diff --git a/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl.scala b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl.scala index 415bd9f..acff22a 100644 --- a/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl.scala +++ b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl.scala @@ -16,17 +16,23 @@ trait StorageComponentImpl extends StorageComponent { val builder = context.songsCollection.initializeOrderedBulkOperation //will automatically split the operation into batches for { song <- songsToAdd - } builder.insert(songToMongoDBObj(song, version)) - + } builder.insert(songToMongoDBObj(song, version)) + val result = builder.execute() } def addWordsDefinition(wd: WordsDefinition, version: Option[Int] = None) = { val builder = context.wdCollection.initializeOrderedBulkOperation builder.insert(wdToMongoDbObject(wd, version)) + + val result = builder.execute() } def findSongs(version: Option[Int] = None): Seq[Song] = { - val query = MongoDBObject("version" -> version.get) + + val query = version match { + case Some(v) => MongoDBObject("version" -> v) + case None => MongoDBObject("version" -> null) + } val result = context.songsCollection.find(query) val songsSet = for { @@ -36,8 +42,11 @@ trait StorageComponentImpl extends StorageComponent { songsSet.toSeq } - def findWordsDefinitions(version: Option[Int] = None): Seq[Map[Int, Int]] = { - val query = MongoDBObject("version" -> version.get) + def findWordsDefinitions(version: Option[Int] = None): Seq[Map[Int, String]] = { + val query = version match { + case Some(v) => MongoDBObject("version" -> version.get) + case None => MongoDBObject("version" -> null) + } val result = context.wdCollection.find(query) val wdSet = for { @@ -47,25 +56,29 @@ trait StorageComponentImpl extends StorageComponent { wdSet.toSeq } + def countSongs(): Int = context.songsCollection.find().count() + + def countWD(): Int = context.wdCollection.find().count() + private def songFromMongoDBObj(obj: MongoDBObject): Song = { val _msdTrackId = obj.getAs[String]("msdTrackId").get val _mxmTrackId = obj.getAs[String]("mxmTrackId").get - val _words = obj.getAs[Map[String, String]]("words").get.map { case (k, v) => (k.toInt, v.toInt) } + val _words = obj.getAs[Map[Int, Int]]("words").get Song(msdTrackId = _msdTrackId, mxmTrackId = _mxmTrackId, words = _words) } - private def wdFromMongoDBObj(obj: MongoDBObject): Map[Int, Int] = { + private def wdFromMongoDBObj(obj: MongoDBObject): Map[Int, String] = { - val version = obj.getAs[Int]("version").get + val version = obj.getAs[Int]("version").getOrElse(null) - val words = obj.getAs[Map[String, String]]("wordsDefinitions").get.map { case (k, v) => (k.toInt, v.toInt) } + val words = obj.getAs[Map[String, String]]("wordsDefinitions").get.map { case (k, v) => (k.toInt, v.toString) } words } - private def songToMongoDBObj(song: Song, version: Option[Int] = None): MongoDBObject = { + private def songToMongoDBObj(song: Song, version: Option[Int]): MongoDBObject = { val songBuilder = MongoDBObject.newBuilder songBuilder +=("msdTrackId" -> song.msdTrackId, "mxmTrackId" -> song.mxmTrackId, @@ -75,11 +88,11 @@ trait StorageComponentImpl extends StorageComponent { songBuilder.result() } - private def wdToMongoDbObject(wd: WordsDefinition, version: Option[Int] = None): MongoDBObject = { + private def wdToMongoDbObject(wd: WordsDefinition, version: Option[Int]): MongoDBObject = { val wdBuilder = MongoDBObject.newBuilder wdBuilder +=( "wordsDefinitions" -> transformWordsDef(wd), - "version" -> version.getOrElse(getLastVersion) + "version" -> version.getOrElse(getLastVersion.getOrElse(null)) ) wdBuilder.result() } @@ -105,8 +118,12 @@ trait StorageComponentImpl extends StorageComponent { val result = context.wdCollection.findOne(query, fields, orderBy) - result.get.getAs[Int]("version") + result match { + case Some(res) => res.getAs[Number]("version").map(_.intValue()) + case None => None + } } + } } \ No newline at end of file diff --git a/persistence/src/test/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl$Test.scala b/persistence/src/test/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl$Test.scala index b8b364e..968dd04 100644 --- a/persistence/src/test/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl$Test.scala +++ b/persistence/src/test/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl$Test.scala @@ -18,33 +18,57 @@ class StorageComponentImpl$Test extends Specification with BeforeAfterAll { if (tryMongoContext.isSuccess) { implicit val s = tryMongoContext.get - val words = Map(1 -> "i", 2 -> "the", 3 -> "you", 4 -> "to", 5 -> "and") + val wd = Map(1 -> "i", 2 -> "the", 3 -> "you", 4 -> "to", 5 -> "and") val firstSong = Song("TRZZZYV128F92E996D", "6849828", Map(1 -> 10, 2 -> 6, 3 -> 20, 5 -> 2, 7 -> 30)) val secondSong = Song("TRZZZYX128F92D32C6", "681124", Map(1 -> 4, 2 -> 18, 4 -> 3, 5 -> 6, 6 -> 9)) val seqSong = Seq(firstSong, secondSong) - DataSet(words, seqSong) + DataSet(wd, seqSong) val mongoStorage = new StorageComponentImpl { override val storage: Storage = new StorageImpl }.storage "add Song " in { + mongoStorage.countSongs() must_== 0 mongoStorage.addSongs(seqSong) + mongoStorage.countSongs() must_== 2 } - "add WordsDefinitions " in { - mongoStorage.addWordsDefinition(words) + "add Song with version " in { + mongoStorage.countSongs() must_== 2 + mongoStorage.addSongs(seqSong, Some(1)) + mongoStorage.countSongs() must_== 4 } - "find Songs" in { - mongoStorage.findSongs() + "add Words Definitions " in { + mongoStorage.countWD() must_== 0 + mongoStorage.addWordsDefinition(wd) + mongoStorage.countWD() must_== 1 } - "find WordsDefinitions" in { - mongoStorage.findWordsDefinitions() + "add Words Definitions with version" in { + mongoStorage.countWD() must_== 1 + mongoStorage.addWordsDefinition(wd, Some(1)) + mongoStorage.countWD() must_== 2 } - } else "Skipped Test" >> skipped("MongoDB is not available in ") + "find Songs without version" in { + mongoStorage.findSongs().toSeq must have size 2 + } + + "find Songs with version" in { + mongoStorage.findSongs(Some(1)).toSeq must have size 2 + } + + "find WordsDefinitions without version" in { + mongoStorage.findWordsDefinitions().toSeq must have size 1 + } + + "find WordsDefinitions with version" in { + mongoStorage.findWordsDefinitions(Some(1)).toSeq must have size 1 + } + + } else "Skipped Test" >> skipped("Mongo context is not available in ") } def uuid() = Some(UUID.randomUUID()) From 171e820c0bf47aa693ec5f41e70f488f43f773b0 Mon Sep 17 00:00:00 2001 From: NellyHargeliya Date: Wed, 25 May 2016 11:02:34 +0300 Subject: [PATCH 11/16] add Last Version test --- .../persistence/StorageComponent.scala | 2 ++ .../persistence/StorageComponentImpl.scala | 2 +- .../persistence/StorageComponent$Test.scala | 8 ------- .../StorageComponentImpl$Test.scala | 23 +++++++++++-------- 4 files changed, 17 insertions(+), 18 deletions(-) delete mode 100644 persistence/src/test/scala/scalalab3/lyricsengine/persistence/StorageComponent$Test.scala diff --git a/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponent.scala b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponent.scala index 5fa9abf..8520a42 100644 --- a/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponent.scala +++ b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponent.scala @@ -22,6 +22,8 @@ trait StorageComponent { def countSongs(): Int def countWD(): Int + + def getLastVersion():Option[Int] } } diff --git a/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl.scala b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl.scala index acff22a..f2fda5f 100644 --- a/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl.scala +++ b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl.scala @@ -111,7 +111,7 @@ trait StorageComponentImpl extends StorageComponent { words.result() } - private def getLastVersion: Option[Int] = { + def getLastVersion: Option[Int] = { val query = MongoDBObject() // All documents val fields = MongoDBObject("version" -> 1) // Only return `version` val orderBy = MongoDBObject("version" -> -1) // Order by version descending diff --git a/persistence/src/test/scala/scalalab3/lyricsengine/persistence/StorageComponent$Test.scala b/persistence/src/test/scala/scalalab3/lyricsengine/persistence/StorageComponent$Test.scala deleted file mode 100644 index 45b104c..0000000 --- a/persistence/src/test/scala/scalalab3/lyricsengine/persistence/StorageComponent$Test.scala +++ /dev/null @@ -1,8 +0,0 @@ -package scalalab3.lyricsengine.persistence - -/** - * Created by Nelly on 19.05.2016. - */ -class StorageComponent$Test { - -} diff --git a/persistence/src/test/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl$Test.scala b/persistence/src/test/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl$Test.scala index 968dd04..9981228 100644 --- a/persistence/src/test/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl$Test.scala +++ b/persistence/src/test/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl$Test.scala @@ -28,15 +28,15 @@ class StorageComponentImpl$Test extends Specification with BeforeAfterAll { }.storage - "add Song " in { + "add Song with version " in { mongoStorage.countSongs() must_== 0 - mongoStorage.addSongs(seqSong) + mongoStorage.addSongs(seqSong, Some(1)) mongoStorage.countSongs() must_== 2 - } - "add Song with version " in { + } + "add Song " in { mongoStorage.countSongs() must_== 2 - mongoStorage.addSongs(seqSong, Some(1)) + mongoStorage.addSongs(seqSong) mongoStorage.countSongs() must_== 4 } @@ -53,21 +53,26 @@ class StorageComponentImpl$Test extends Specification with BeforeAfterAll { } "find Songs without version" in { - mongoStorage.findSongs().toSeq must have size 2 + mongoStorage.findSongs() must have size 2 } "find Songs with version" in { - mongoStorage.findSongs(Some(1)).toSeq must have size 2 + mongoStorage.findSongs(Some(1)) must have size 2 } "find WordsDefinitions without version" in { - mongoStorage.findWordsDefinitions().toSeq must have size 1 + mongoStorage.findWordsDefinitions() must have size 1 } "find WordsDefinitions with version" in { - mongoStorage.findWordsDefinitions(Some(1)).toSeq must have size 1 + mongoStorage.findWordsDefinitions(Some(1)) must have size 1 } + "get Last Version" in { + mongoStorage.getLastVersion() must_==Some(1) + } + + } else "Skipped Test" >> skipped("Mongo context is not available in ") } From 94d76f9f374dc046ccfcfe619fc8b21a4bf4695a Mon Sep 17 00:00:00 2001 From: NellyHargeliya Date: Wed, 25 May 2016 12:19:03 +0300 Subject: [PATCH 12/16] add DataSet iMongoDB --- .../persistence/StorageComponent.scala | 2 ++ .../persistence/StorageComponentImpl.scala | 7 +++++++ .../persistence/StorageComponentImpl$Test.scala | 15 ++++++++++++--- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponent.scala b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponent.scala index 8520a42..ae98e6b 100644 --- a/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponent.scala +++ b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponent.scala @@ -24,6 +24,8 @@ trait StorageComponent { def countWD(): Int def getLastVersion():Option[Int] + + def addDataSet(dataSet: DataSet, version: Option[Int] = None) } } diff --git a/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl.scala b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl.scala index f2fda5f..10b6302 100644 --- a/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl.scala +++ b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl.scala @@ -12,6 +12,13 @@ trait StorageComponentImpl extends StorageComponent { class StorageImpl(implicit context: MongoContext) extends Storage { + def addDataSet(dataSet: DataSet, version: Option[Int] = None)={ + addWordsDefinition(dataSet.definition) + addSongs(dataSet.songs) + + } + // case class DataSet(definition: WordsDefinition, songs: Seq[Song]) + def addSongs(songsToAdd: Seq[Song], version: Option[Int] = None) = { val builder = context.songsCollection.initializeOrderedBulkOperation //will automatically split the operation into batches for { diff --git a/persistence/src/test/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl$Test.scala b/persistence/src/test/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl$Test.scala index 9981228..653f331 100644 --- a/persistence/src/test/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl$Test.scala +++ b/persistence/src/test/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl$Test.scala @@ -17,12 +17,11 @@ class StorageComponentImpl$Test extends Specification with BeforeAfterAll { "Mongo Test" >> { if (tryMongoContext.isSuccess) { implicit val s = tryMongoContext.get - val wd = Map(1 -> "i", 2 -> "the", 3 -> "you", 4 -> "to", 5 -> "and") val firstSong = Song("TRZZZYV128F92E996D", "6849828", Map(1 -> 10, 2 -> 6, 3 -> 20, 5 -> 2, 7 -> 30)) val secondSong = Song("TRZZZYX128F92D32C6", "681124", Map(1 -> 4, 2 -> 18, 4 -> 3, 5 -> 6, 6 -> 9)) - val seqSong = Seq(firstSong, secondSong) - DataSet(wd, seqSong) + val seqSong = Seq(firstSong, secondSong) + val MdataSet=DataSet(wd, seqSong) val mongoStorage = new StorageComponentImpl { override val storage: Storage = new StorageImpl }.storage @@ -72,7 +71,17 @@ class StorageComponentImpl$Test extends Specification with BeforeAfterAll { mongoStorage.getLastVersion() must_==Some(1) } + "add DataSet " in { + mongoStorage.countWD() must_== 2 + mongoStorage.addDataSet(MdataSet) + mongoStorage.countWD() must_== 3 + } + "add DataSet with version " in { + mongoStorage.countWD() must_== 3 + mongoStorage.addDataSet(MdataSet, Some(3)) + mongoStorage.countWD() must_== 4 + } } else "Skipped Test" >> skipped("Mongo context is not available in ") } From 62ea813fa3bd6742f2d28b85307be5a9b1e04dfc Mon Sep 17 00:00:00 2001 From: NellyHargeliya Date: Wed, 25 May 2016 12:33:11 +0300 Subject: [PATCH 13/16] add version --- .../lyricsengine/persistence/StorageComponentImpl.scala | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl.scala b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl.scala index 10b6302..a7f498f 100644 --- a/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl.scala +++ b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl.scala @@ -9,16 +9,13 @@ trait StorageComponentImpl extends StorageComponent { override val storage: Storage - class StorageImpl(implicit context: MongoContext) extends Storage { def addDataSet(dataSet: DataSet, version: Option[Int] = None)={ - addWordsDefinition(dataSet.definition) - addSongs(dataSet.songs) + addWordsDefinition(dataSet.definition,version) + addSongs(dataSet.songs,version) } - // case class DataSet(definition: WordsDefinition, songs: Seq[Song]) - def addSongs(songsToAdd: Seq[Song], version: Option[Int] = None) = { val builder = context.songsCollection.initializeOrderedBulkOperation //will automatically split the operation into batches for { @@ -30,12 +27,10 @@ trait StorageComponentImpl extends StorageComponent { def addWordsDefinition(wd: WordsDefinition, version: Option[Int] = None) = { val builder = context.wdCollection.initializeOrderedBulkOperation builder.insert(wdToMongoDbObject(wd, version)) - val result = builder.execute() } def findSongs(version: Option[Int] = None): Seq[Song] = { - val query = version match { case Some(v) => MongoDBObject("version" -> v) case None => MongoDBObject("version" -> null) From 91090593bf423d43f160aeb688f685d8e84d5dd3 Mon Sep 17 00:00:00 2001 From: NellyHargeliya Date: Thu, 26 May 2016 21:02:24 +0300 Subject: [PATCH 14/16] add test get DataSet --- .../persistence/StorageComponent.scala | 15 +++---- .../persistence/StorageComponentImpl.scala | 19 ++++---- .../StorageComponentImpl$Test.scala | 45 ++++++++++++------- 3 files changed, 44 insertions(+), 35 deletions(-) diff --git a/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponent.scala b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponent.scala index ae98e6b..3e39e8f 100644 --- a/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponent.scala +++ b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponent.scala @@ -3,18 +3,14 @@ package scalalab3.lyricsengine.persistence import scalalab3.lyricsengine.domain._ /** - * Created by annie on 5/17/16. - */ + * Created by annie on 5/17/16. + */ trait StorageComponent { val storage: Storage trait Storage { - def addSongs(songsToAdd: Seq[Song], version: Option[Int] = None) - - def addWordsDefinition(wd: WordsDefinition, version: Option[Int] = None) - def findSongs(version: Option[Int] = None): Seq[Song] def findWordsDefinitions(version: Option[Int] = None): Seq[Map[Int, String]] @@ -23,9 +19,12 @@ trait StorageComponent { def countWD(): Int - def getLastVersion():Option[Int] - def addDataSet(dataSet: DataSet, version: Option[Int] = None) + + def getDataSet(version: Option[Int] = None) + + def getLastVersion: Option[Int] + } } diff --git a/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl.scala b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl.scala index a7f498f..af0645b 100644 --- a/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl.scala +++ b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl.scala @@ -12,11 +12,16 @@ trait StorageComponentImpl extends StorageComponent { class StorageImpl(implicit context: MongoContext) extends Storage { def addDataSet(dataSet: DataSet, version: Option[Int] = None)={ - addWordsDefinition(dataSet.definition,version) + addWordsDefinition(dataSet.definition,version) addSongs(dataSet.songs,version) + } + def getDataSet(version: Option[Int] = None)={ + findWordsDefinitions(version) + findSongs(version) } - def addSongs(songsToAdd: Seq[Song], version: Option[Int] = None) = { + + private def addSongs(songsToAdd: Seq[Song], version: Option[Int] = None) = { val builder = context.songsCollection.initializeOrderedBulkOperation //will automatically split the operation into batches for { song <- songsToAdd @@ -24,7 +29,7 @@ trait StorageComponentImpl extends StorageComponent { val result = builder.execute() } - def addWordsDefinition(wd: WordsDefinition, version: Option[Int] = None) = { + private def addWordsDefinition(wd: WordsDefinition, version: Option[Int] = None) = { val builder = context.wdCollection.initializeOrderedBulkOperation builder.insert(wdToMongoDbObject(wd, version)) val result = builder.execute() @@ -65,17 +70,13 @@ trait StorageComponentImpl extends StorageComponent { private def songFromMongoDBObj(obj: MongoDBObject): Song = { val _msdTrackId = obj.getAs[String]("msdTrackId").get val _mxmTrackId = obj.getAs[String]("mxmTrackId").get - val _words = obj.getAs[Map[Int, Int]]("words").get Song(msdTrackId = _msdTrackId, mxmTrackId = _mxmTrackId, words = _words) - } private def wdFromMongoDBObj(obj: MongoDBObject): Map[Int, String] = { - val version = obj.getAs[Int]("version").getOrElse(null) - val words = obj.getAs[Map[String, String]]("wordsDefinitions").get.map { case (k, v) => (k.toInt, v.toString) } words } @@ -113,7 +114,7 @@ trait StorageComponentImpl extends StorageComponent { words.result() } - def getLastVersion: Option[Int] = { + def getLastVersion: Option[Int] = { val query = MongoDBObject() // All documents val fields = MongoDBObject("version" -> 1) // Only return `version` val orderBy = MongoDBObject("version" -> -1) // Order by version descending @@ -125,7 +126,5 @@ trait StorageComponentImpl extends StorageComponent { case None => None } } - } - } \ No newline at end of file diff --git a/persistence/src/test/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl$Test.scala b/persistence/src/test/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl$Test.scala index 653f331..c554309 100644 --- a/persistence/src/test/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl$Test.scala +++ b/persistence/src/test/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl$Test.scala @@ -20,19 +20,41 @@ class StorageComponentImpl$Test extends Specification with BeforeAfterAll { val wd = Map(1 -> "i", 2 -> "the", 3 -> "you", 4 -> "to", 5 -> "and") val firstSong = Song("TRZZZYV128F92E996D", "6849828", Map(1 -> 10, 2 -> 6, 3 -> 20, 5 -> 2, 7 -> 30)) val secondSong = Song("TRZZZYX128F92D32C6", "681124", Map(1 -> 4, 2 -> 18, 4 -> 3, 5 -> 6, 6 -> 9)) - val seqSong = Seq(firstSong, secondSong) + val seqSong = Seq(firstSong, secondSong) val MdataSet=DataSet(wd, seqSong) val mongoStorage = new StorageComponentImpl { override val storage: Storage = new StorageImpl }.storage + "add DataSet " in { + mongoStorage.countWD() must_== 0 + mongoStorage.addDataSet(MdataSet) + mongoStorage.countWD() must_== 1 + } + + "add DataSet with version " in { + mongoStorage.countWD() must_== 1 + mongoStorage.addDataSet(MdataSet, Some(1)) + mongoStorage.countWD() must_== 2 + } - "add Song with version " in { + "get DataSet " in { + mongoStorage.getDataSet() + mongoStorage.findSongs() must have size 2 + } + + "get DataSet with version" in { + mongoStorage.getDataSet(Some(1)) + mongoStorage.findSongs() must have size 2 + } + + + /* "add Song with version " in { mongoStorage.countSongs() must_== 0 mongoStorage.addSongs(seqSong, Some(1)) mongoStorage.countSongs() must_== 2 - } + "add Song " in { mongoStorage.countSongs() must_== 2 mongoStorage.addSongs(seqSong) @@ -67,21 +89,10 @@ class StorageComponentImpl$Test extends Specification with BeforeAfterAll { mongoStorage.findWordsDefinitions(Some(1)) must have size 1 } - "get Last Version" in { - mongoStorage.getLastVersion() must_==Some(1) - } - - "add DataSet " in { - mongoStorage.countWD() must_== 2 - mongoStorage.addDataSet(MdataSet) - mongoStorage.countWD() must_== 3 - } - - "add DataSet with version " in { - mongoStorage.countWD() must_== 3 - mongoStorage.addDataSet(MdataSet, Some(3)) - mongoStorage.countWD() must_== 4 + "get LastVersion" in { + mongoStorage.getLastVersion == Some(1) } +*/ } else "Skipped Test" >> skipped("Mongo context is not available in ") } From 27f25a5fa6abad94ca30d0649ea5d3109eaf5b29 Mon Sep 17 00:00:00 2001 From: NellyHargeliya Date: Thu, 26 May 2016 21:47:27 +0300 Subject: [PATCH 15/16] add test LastVersion --- .../StorageComponentImpl$Test.scala | 47 +------------------ 1 file changed, 2 insertions(+), 45 deletions(-) diff --git a/persistence/src/test/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl$Test.scala b/persistence/src/test/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl$Test.scala index c554309..23b92a0 100644 --- a/persistence/src/test/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl$Test.scala +++ b/persistence/src/test/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl$Test.scala @@ -1,10 +1,8 @@ package scalalab3.lyricsengine.persistence import java.util.UUID - import org.specs2.mutable.Specification import org.specs2.specification._ - import scala.util.Try import scalalab3.lyricsengine.domain.{DataSet, Song} import scalalab3.lyricsengine.persistence.mongo.{MongoConfig, MongoContext} @@ -14,7 +12,7 @@ class StorageComponentImpl$Test extends Specification with BeforeAfterAll { val tryMongoContext = Try(new MongoContext(MongoConfig.load())) - "Mongo Test" >> { + "MongoDB Test" >> { if (tryMongoContext.isSuccess) { implicit val s = tryMongoContext.get val wd = Map(1 -> "i", 2 -> "the", 3 -> "you", 4 -> "to", 5 -> "and") @@ -48,51 +46,10 @@ class StorageComponentImpl$Test extends Specification with BeforeAfterAll { mongoStorage.findSongs() must have size 2 } - - /* "add Song with version " in { - mongoStorage.countSongs() must_== 0 - mongoStorage.addSongs(seqSong, Some(1)) - mongoStorage.countSongs() must_== 2 - } - - "add Song " in { - mongoStorage.countSongs() must_== 2 - mongoStorage.addSongs(seqSong) - mongoStorage.countSongs() must_== 4 - } - - "add Words Definitions " in { - mongoStorage.countWD() must_== 0 - mongoStorage.addWordsDefinition(wd) - mongoStorage.countWD() must_== 1 - } - - "add Words Definitions with version" in { - mongoStorage.countWD() must_== 1 - mongoStorage.addWordsDefinition(wd, Some(1)) - mongoStorage.countWD() must_== 2 - } - - "find Songs without version" in { - mongoStorage.findSongs() must have size 2 - } - - "find Songs with version" in { - mongoStorage.findSongs(Some(1)) must have size 2 - } - - "find WordsDefinitions without version" in { - mongoStorage.findWordsDefinitions() must have size 1 - } - - "find WordsDefinitions with version" in { - mongoStorage.findWordsDefinitions(Some(1)) must have size 1 - } - "get LastVersion" in { mongoStorage.getLastVersion == Some(1) } -*/ + } else "Skipped Test" >> skipped("Mongo context is not available in ") } From bfb6dbc141a2a20b4fea137fa330984fec1324a6 Mon Sep 17 00:00:00 2001 From: angr0316 Date: Fri, 27 May 2016 16:15:45 +0300 Subject: [PATCH 16/16] fixed comments --- .../persistence/StorageComponent.scala | 14 +-- .../persistence/StorageComponentImpl.scala | 85 ++++++++----------- .../StorageComponentImpl$Test.scala | 24 ++---- 3 files changed, 46 insertions(+), 77 deletions(-) diff --git a/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponent.scala b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponent.scala index 3e39e8f..2f8ac9a 100644 --- a/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponent.scala +++ b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponent.scala @@ -10,21 +10,11 @@ trait StorageComponent { val storage: Storage trait Storage { - - def findSongs(version: Option[Int] = None): Seq[Song] - - def findWordsDefinitions(version: Option[Int] = None): Seq[Map[Int, String]] - - def countSongs(): Int - - def countWD(): Int - def addDataSet(dataSet: DataSet, version: Option[Int] = None) - def getDataSet(version: Option[Int] = None) - - def getLastVersion: Option[Int] + def getDataSet(version: Option[Int] = None): DataSet + def getLastVersion: Int } } diff --git a/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl.scala b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl.scala index af0645b..65e0687 100644 --- a/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl.scala +++ b/persistence/src/main/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl.scala @@ -11,74 +11,71 @@ trait StorageComponentImpl extends StorageComponent { class StorageImpl(implicit context: MongoContext) extends Storage { - def addDataSet(dataSet: DataSet, version: Option[Int] = None)={ - addWordsDefinition(dataSet.definition,version) - addSongs(dataSet.songs,version) + def addDataSet(dataSet: DataSet, version: Option[Int] = None) = { + addWordsDefinition(dataSet.definition, version) + addSongs(dataSet.songs, version) } - def getDataSet(version: Option[Int] = None)={ - findWordsDefinitions(version) - findSongs(version) + def getDataSet(version: Option[Int] = None): DataSet = { + val wd = findWordsDefinitions(version) + val songs = findSongs(version) + DataSet(wd, songs) } - private def addSongs(songsToAdd: Seq[Song], version: Option[Int] = None) = { + def getLastVersion: Int = { + val query = MongoDBObject() // All documents + val fields = MongoDBObject("version" -> 1) // Only return `version` + val orderBy = MongoDBObject("version" -> -1) // Order by version descending + val result = context.wdCollection.findOne(query, fields, orderBy) + val defaultVersion = -1 + result match { + case Some(res) => res.getAs[Int]("version").getOrElse(defaultVersion) + case None => defaultVersion + } + } + + private def addSongs(songsToAdd: Seq[Song], version: Option[Int] = None) = { val builder = context.songsCollection.initializeOrderedBulkOperation //will automatically split the operation into batches for { song <- songsToAdd - } builder.insert(songToMongoDBObj(song, version)) + } builder.insert(songToMongoDBObj(song, version)) val result = builder.execute() } - private def addWordsDefinition(wd: WordsDefinition, version: Option[Int] = None) = { + private def addWordsDefinition(wd: WordsDefinition, version: Option[Int] = None) = { val builder = context.wdCollection.initializeOrderedBulkOperation builder.insert(wdToMongoDbObject(wd, version)) val result = builder.execute() } - def findSongs(version: Option[Int] = None): Seq[Song] = { - val query = version match { - case Some(v) => MongoDBObject("version" -> v) - case None => MongoDBObject("version" -> null) - } + private def findSongs(version: Option[Int] = None): Seq[Song] = { + val query = MongoDBObject("version" -> version.getOrElse(getLastVersion)) val result = context.songsCollection.find(query) - val songsSet = for { song <- result } yield songFromMongoDBObj(song) - songsSet.toSeq } - def findWordsDefinitions(version: Option[Int] = None): Seq[Map[Int, String]] = { - val query = version match { - case Some(v) => MongoDBObject("version" -> version.get) - case None => MongoDBObject("version" -> null) - } + private def findWordsDefinitions(version: Option[Int] = None): WordsDefinition = { + val query = MongoDBObject("version" -> version.getOrElse(getLastVersion)) val result = context.wdCollection.find(query) - - val wdSet = for { - wd <- result - } yield wdFromMongoDBObj(wd) - - wdSet.toSeq + result.map(wdFromMongoDBObj(_)).next() } - def countSongs(): Int = context.songsCollection.find().count() + private def countSongs(): Int = context.songsCollection.find().count() - def countWD(): Int = context.wdCollection.find().count() + private def countWD(): Int = context.wdCollection.find().count() private def songFromMongoDBObj(obj: MongoDBObject): Song = { - val _msdTrackId = obj.getAs[String]("msdTrackId").get - val _mxmTrackId = obj.getAs[String]("mxmTrackId").get - val _words = obj.getAs[Map[Int, Int]]("words").get - + val _msdTrackId = obj.as[String]("msdTrackId") + val _mxmTrackId = obj.as[String]("mxmTrackId") + val _words = obj.as[Map[Int, Int]]("words") Song(msdTrackId = _msdTrackId, mxmTrackId = _mxmTrackId, words = _words) } private def wdFromMongoDBObj(obj: MongoDBObject): Map[Int, String] = { - val version = obj.getAs[Int]("version").getOrElse(null) - val words = obj.getAs[Map[String, String]]("wordsDefinitions").get.map { case (k, v) => (k.toInt, v.toString) } - words + obj.getAs[Map[String, String]]("wordsDefinitions").get.map { case (k, v) => (k.toInt, v.toString) } } private def songToMongoDBObj(song: Song, version: Option[Int]): MongoDBObject = { @@ -95,7 +92,7 @@ trait StorageComponentImpl extends StorageComponent { val wdBuilder = MongoDBObject.newBuilder wdBuilder +=( "wordsDefinitions" -> transformWordsDef(wd), - "version" -> version.getOrElse(getLastVersion.getOrElse(null)) + "version" -> version.getOrElse(getLastVersion) ) wdBuilder.result() } @@ -113,18 +110,6 @@ trait StorageComponentImpl extends StorageComponent { words ++= songWords words.result() } - - def getLastVersion: Option[Int] = { - val query = MongoDBObject() // All documents - val fields = MongoDBObject("version" -> 1) // Only return `version` - val orderBy = MongoDBObject("version" -> -1) // Order by version descending - - val result = context.wdCollection.findOne(query, fields, orderBy) - - result match { - case Some(res) => res.getAs[Number]("version").map(_.intValue()) - case None => None - } - } } + } \ No newline at end of file diff --git a/persistence/src/test/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl$Test.scala b/persistence/src/test/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl$Test.scala index 23b92a0..82bd63f 100644 --- a/persistence/src/test/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl$Test.scala +++ b/persistence/src/test/scala/scalalab3/lyricsengine/persistence/StorageComponentImpl$Test.scala @@ -25,29 +25,23 @@ class StorageComponentImpl$Test extends Specification with BeforeAfterAll { }.storage "add DataSet " in { - mongoStorage.countWD() must_== 0 mongoStorage.addDataSet(MdataSet) - mongoStorage.countWD() must_== 1 + mongoStorage.getDataSet().songs.size must_== seqSong.size + mongoStorage.getDataSet().definition.size must_== wd.size } - "add DataSet with version " in { - mongoStorage.countWD() must_== 1 - mongoStorage.addDataSet(MdataSet, Some(1)) - mongoStorage.countWD() must_== 2 - } - - "get DataSet " in { - mongoStorage.getDataSet() - mongoStorage.findSongs() must have size 2 + "get default LastVersion" in { + mongoStorage.getLastVersion must_== -1 } - "get DataSet with version" in { - mongoStorage.getDataSet(Some(1)) - mongoStorage.findSongs() must have size 2 + "add DataSet with version " in { + mongoStorage.addDataSet(MdataSet, Some(1)) + mongoStorage.getDataSet().songs.size must_== seqSong.size + mongoStorage.getDataSet().definition.size must_== wd.size } "get LastVersion" in { - mongoStorage.getLastVersion == Some(1) + mongoStorage.getLastVersion must_== 1 } } else "Skipped Test" >> skipped("Mongo context is not available in ")