Skip to content

Commit

Permalink
Complete separation of IndexedLinks from collection
Browse files Browse the repository at this point in the history
  • Loading branch information
darkfrog26 committed Jun 7, 2024
1 parent 928a985 commit f7c16d2
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 29 deletions.
2 changes: 1 addition & 1 deletion all/src/test/scala/spec/SimpleHaloAndDuckDBSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ class SimpleHaloAndDuckDBSpec extends AsyncWordSpec with AsyncIOSpec with Matche

val name: I[String] = index.one("name", _.name)
val age: I[Int] = index.one("age", _.age)
val ageLinks: IndexedLinks[Int, Person] = indexedLinks[Int]("age", _.age, _.toString)
val ageLinks: IndexedLinks[Int, Person] = IndexedLinks[Int, Person]("age", _.age, _.toString, this)
}

object InitialSetupUpgrade extends DatabaseUpgrade {
Expand Down
2 changes: 1 addition & 1 deletion all/src/test/scala/spec/SimpleHaloAndSQLiteSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ class SimpleHaloAndSQLiteSpec extends AsyncWordSpec with AsyncIOSpec with Matche

val name: I[String] = index.one("name", _.name)
val age: I[Int] = index.one("age", _.age)
val ageLinks: IndexedLinks[Int, Person] = indexedLinks[Int]("age", _.age, _.toString)
val ageLinks: IndexedLinks[Int, Person] = IndexedLinks[Int, Person]("age", _.age, _.toString, this)
}

object InitialSetupUpgrade extends DatabaseUpgrade {
Expand Down
15 changes: 12 additions & 3 deletions core/src/main/scala/lightdb/IndexedLinks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,21 @@ package lightdb
import cats.effect.IO
import lightdb.model.{AbstractCollection, DocumentAction}

/**
* Creates a key/value stored object with a list of links. This can be incredibly efficient for small lists, but much
* slower for larger sets of data and a standard index would be preferable.
*
* @param name the name of the index
* @param createV creates the value from the document
* @param createKey creates a unique identifier from the value
* @param collection the collection to associate this with
* @param maxLinks determines how to handle maximum number of links
*/
case class IndexedLinks[V, D <: Document[D]](name: String,
createV: D => V,
createKey: V => String,
loadStore: () => Store,
collection: AbstractCollection[D],
maxLinks: MaxLinks) {
maxLinks: MaxLinks = MaxLinks.OverflowWarn()) {
collection.postSet.add((_: DocumentAction, doc: D, _: AbstractCollection[D]) => {
add(doc).map(_ => Some(doc))
})
Expand All @@ -17,7 +26,7 @@ case class IndexedLinks[V, D <: Document[D]](name: String,
})
collection.truncateActions += clear()

private lazy val store: Store = loadStore()
private lazy val store: Store = collection.db.createStoreInternal(s"${collection.collectionName}.indexedLinks.$name")

protected[lightdb] def add(doc: D): IO[Unit] = {
val v = createV(doc)
Expand Down
11 changes: 11 additions & 0 deletions core/src/main/scala/lightdb/ValueStore.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package lightdb

import fabric.rw.RW
import lightdb.model.AbstractCollection

case class ValueStore[V, D <: Document[D]](key: String,
createV: D => V,
loadStore: () => Store,
collection: AbstractCollection[D],
distinct: Boolean = true)
(implicit rw: RW[V])
25 changes: 2 additions & 23 deletions core/src/main/scala/lightdb/model/AbstractCollection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ trait AbstractCollection[D <: Document[D]] extends DocumentActionSupport[D] {

def atomic: Boolean

protected[lightdb] def db: LightDB
def db: LightDB

protected lazy val store: Store = db.createStoreInternal(collectionName)

Expand Down Expand Up @@ -134,27 +134,6 @@ trait AbstractCollection[D <: Document[D]] extends DocumentActionSupport[D] {
def dispose(): IO[Unit] = store.dispose().flatMap { _ =>
disposeActions.invoke()
}

/**
* Creates a key/value stored object with a list of links. This can be incredibly efficient for small lists, but much
* slower for larger sets of data and a standard index would be preferable.
*
* @param name the name of the index
* @param createV creates the value from the document
* @param createKey creates a unique identifier from the value
* @param maxLinks determines how to handle maximum number of links
*/
def indexedLinks[V](name: String,
createV: D => V,
createKey: V => String,
maxLinks: MaxLinks = MaxLinks.OverflowWarn()): IndexedLinks[V, D] = IndexedLinks[V, D](
name = name,
createV = createV,
createKey = createKey,
loadStore = () => db.createStoreInternal(s"$collectionName.indexed.$name"),
collection = this,
maxLinks = maxLinks
)
}

object AbstractCollection {
Expand All @@ -171,7 +150,7 @@ object AbstractCollection {
override def collectionName: String = name
override def defaultCommitMode: CommitMode = cm
override def atomic: Boolean = at
override protected[lightdb] def db: LightDB = lightDB
override def db: LightDB = lightDB

override implicit val rw: RW[D] = docRW
override def model: DocumentModel[D] = documentModel
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/lightdb/model/Collection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import fabric.rw.RW
import lightdb.{CommitMode, Document, LightDB}

abstract class Collection[D <: Document[D]](val collectionName: String,
protected[lightdb] val db: LightDB,
val db: LightDB,
commitMode: CommitMode = CommitMode.Manual,
val atomic: Boolean = true) extends AbstractCollection[D] with DocumentModel[D] {
override def model: DocumentModel[D] = this
Expand Down

0 comments on commit f7c16d2

Please sign in to comment.