Skip to content

Commit

Permalink
Added Collection.verify() feature to verify the integrity of the coll…
Browse files Browse the repository at this point in the history
…ection and auto-run during init.

Auto-delete of existing database backup before creating archive in DatabaseBackup.archive.
  • Loading branch information
darkfrog26 committed Aug 26, 2024
1 parent 28b860e commit dfaa45b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ val developerURL: String = "https://matthicks.com"

name := projectName
ThisBuild / organization := org
ThisBuild / version := "0.12.2-SNAPSHOT"
ThisBuild / version := "0.12.2-SNAPSHOT1"
ThisBuild / scalaVersion := scala213
ThisBuild / crossScalaVersions := allScalaVersions
ThisBuild / scalacOptions ++= Seq("-unchecked", "-deprecation")
Expand Down
1 change: 1 addition & 0 deletions core/src/main/scala/lightdb/backup/DatabaseBackup.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ object DatabaseBackup {
def archive(db: LightDB,
archive: File = new File("backup.zip")): Int = {
archive.getParentFile.mkdirs()
if (archive.exists()) archive.delete()
val out = new ZipOutputStream(new FileOutputStream(archive))
try {
process(db) {
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/scala/lightdb/collection/Collection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,13 @@ case class Collection[Doc <: Document[Doc], Model <: DocumentModel[Doc]](name: S
}
case _ => // Can't do validation
}

// Verify the data is in-sync
verify()
}

def verify(): Boolean = store.verify()

def reIndex(): Boolean = store.reIndex()

object transaction {
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/scala/lightdb/store/Store.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ abstract class Store[Doc <: Document[Doc], Model <: DocumentModel[Doc]] {

def truncate()(implicit transaction: Transaction[Doc]): Int

def verify(): Boolean = false

def reIndex(): Boolean = false

def dispose(): Unit
Expand Down
19 changes: 18 additions & 1 deletion core/src/main/scala/lightdb/store/split/SplitStore.scala
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,29 @@ case class SplitStore[Doc <: Document[Doc], Model <: DocumentModel[Doc]](storage
searching.truncate()
}

override def verify(): Boolean = collection.transaction { implicit transaction =>
val storageCount = storage.count
val searchCount = searching.count
if (storageCount != searchCount) {
scribe.warn(s"${collection.name} out of sync! Storage Count: $storageCount, Search Count: $searchCount. Re-Indexing...")
reIndexInternal()
scribe.info(s"${collection.name} re-indexed successfully!")
true
} else {
false
}
}

override def reIndex(): Boolean = collection.transaction { implicit transaction =>
reIndexInternal()
true
}

private def reIndexInternal()(implicit transaction: Transaction[Doc]): Unit = {
searching.truncate()
storage.iterator.foreach { doc =>
searching.insert(doc)
}
true
}

override def dispose(): Unit = {
Expand Down

0 comments on commit dfaa45b

Please sign in to comment.