-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Compact Methods for Database or DAO
- Loading branch information
QuadStingray
committed
Mar 27, 2023
1 parent
cc13a06
commit 0926b1d
Showing
10 changed files
with
121 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/main/scala/dev/mongocamp/driver/mongodb/database/CompactResult.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package dev.mongocamp.driver.mongodb.database | ||
import dev.mongocamp.driver.mongodb._ | ||
import org.mongodb.scala.bson.Document | ||
|
||
case class CompactResult(bytesFreed: Long) | ||
|
||
object CompactResult { | ||
def apply(document: Document): Option[CompactResult] = { | ||
if (document.getLongValue("ok") == 1) { | ||
Some( | ||
CompactResult( | ||
document.getLongValue("bytesFreed") | ||
) | ||
) | ||
} | ||
else { | ||
None | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/test/scala/dev/mongocamp/driver/mongodb/CompactSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package dev.mongocamp.driver.mongodb | ||
|
||
import better.files.{File, Resource} | ||
import dev.mongocamp.driver.mongodb.database.CompactResult | ||
import dev.mongocamp.driver.mongodb.test.TestDatabase | ||
import dev.mongocamp.driver.mongodb.test.TestDatabase.BookDAO | ||
import org.specs2.mutable.Specification | ||
import org.specs2.specification.BeforeAll | ||
|
||
import java.text.SimpleDateFormat | ||
import java.util.Date | ||
|
||
class CompactSpec extends Specification with BeforeAll { | ||
val DateFormat = new SimpleDateFormat("yyyy-MM-dd") | ||
val From: Date = DateFormat.parse("2000-01-01") | ||
|
||
override def beforeAll(): Unit = { | ||
BookDAO.drop().result() | ||
BookDAO.importJsonFile(File(Resource.getUrl("json/books.json"))).result() | ||
val stats = BookDAO.collectionStatus.result() | ||
stats.count mustEqual 431 | ||
} | ||
|
||
"CompactSpec" should { | ||
"compact single collection" in { | ||
val count: Option[CompactResult] = BookDAO.compact.result() | ||
count must beSome() | ||
count.get.bytesFreed must beGreaterThanOrEqualTo(0L) | ||
} | ||
"compact complete database" in { | ||
val count: CompactResult = TestDatabase.provider.compact() | ||
count.bytesFreed must beGreaterThanOrEqualTo(0L) | ||
} | ||
} | ||
|
||
} |
12 changes: 5 additions & 7 deletions
12
src/test/scala/dev/mongocamp/driver/mongodb/dao/BookDAOSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 2 additions & 3 deletions
5
src/test/scala/dev/mongocamp/driver/mongodb/dao/PersonDAOSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,5 @@ | ||
import scala.io.Source | ||
import scala.tools.nsc.io.File | ||
import dev.quadstingray.sbt.json.JsonFile | ||
|
||
ThisBuild / version := { | ||
val packageJsonFile = File("package.json") | ||
val source = Source.fromFile(packageJsonFile.toURI) | ||
val versionPattern = "\"version\":(.*?)\"(.*?)\"".r | ||
val versionPartString = versionPattern.findFirstIn(source.mkString).get | ||
val replacedVersion = versionPartString.replace("\"version\":", "").replace("\"", "").replace("\",", "").trim.trim.trim | ||
replacedVersion.toLowerCase.replace(".snapshot", "-SNAPSHOT") | ||
} | ||
val json = JsonFile(file("package.json")) | ||
|
||
ThisBuild / version := json.stringValue("version") |