Skip to content

Commit

Permalink
V3 casl 570 db table test (#361)
Browse files Browse the repository at this point in the history
* refactor bug

* collection indices in db test

* bug not creating default db indices and remove index id txn uid that would be overwritten

* indices test

* add tests and bug fixes

* enum for db storing policy of history, shadow features, and meta

* docs

* delegate autoPurge and disableHistory booleans to new enums
  • Loading branch information
gunplar authored Oct 16, 2024
1 parent 144a67c commit 67c2e0f
Show file tree
Hide file tree
Showing 7 changed files with 300 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

package naksha.model.objects

import naksha.base.Int64
import naksha.base.NotNullProperty
import naksha.base.NullableProperty
import naksha.base.StringList
import naksha.base.*
import naksha.model.Flags
import kotlin.js.JsExport
import kotlin.js.JsName
Expand All @@ -18,22 +15,26 @@ import kotlin.jvm.JvmStatic
@JsExport
open class NakshaCollection() : NakshaFeature() {

// TODO: Add documentation!
/**
* Create a Naksha collection with settings.
*/
@JsName("of")
constructor(
id: String,
partitions: Int = 1,
storageClass: String? = null,
autoPurge: Boolean = false,
disableHistory: Boolean = false,
geoIndex: String? = null
geoIndex: String? = null,
storeDeleted: StoreMode = StoreMode.ON,
storeHistory: StoreMode = StoreMode.ON,
storeMeta: StoreMode = StoreMode.ON,
) : this() {
this.id = id
this.storageClass = storageClass
this.partitions = partitions
this.autoPurge = autoPurge
this.disableHistory = disableHistory
this.geoIndex = geoIndex ?: DEFAULT_GEO_INDEX
this.storeDeleted = storeDeleted
this.storeHistory = storeHistory
this.storeMeta = storeMeta
}

override fun defaultFeatureType(): String = FEATURE_TYPE
Expand Down Expand Up @@ -113,15 +114,22 @@ open class NakshaCollection() : NakshaFeature() {
var encodeDict by STRING_NULL

/**
* _true_ - disables history of features' modifications.
* If [StoreMode.OFF] there will be no history table in the database for features in this collection,
* which boosts performance in certain operations.
*/
var storeHistory by STORE_HISTORY

/**
* If [StoreMode.OFF] there will be no table in the database for deleted features from this collection,
* which boosts performance in certain operations.
*/
var disableHistory by DISABLE_HISTORY
var storeDeleted by STORE_DELETED

/**
* If autoPurge is enabled, deleted features are automatically purged and no shadow state is kept available.
* Note that if [disableHistory] is false, the deleted features will still be around in the history. This mainly effects lib-view.
* If [StoreMode.OFF] there will be no meta table in the database for statistics of features in this collection,
* which boosts performance in certain operations.
*/
var autoPurge by AUTO_PURGE
var storeMeta by STORE_META

/**
* The indices list contains the list of indices to add to the collection.
Expand Down Expand Up @@ -193,12 +201,21 @@ open class NakshaCollection() : NakshaFeature() {
private val DEFAULT_TYPE = NotNullProperty<NakshaCollection, String>(String::class) { _, _ -> "Feature" }
private val DEFAULT_FLAGS = NullableProperty<NakshaCollection, Flags>(Flags::class)
private val STRING_NULL = NullableProperty<NakshaCollection, String>(String::class)
private val DISABLE_HISTORY = NotNullProperty<NakshaCollection, Boolean>(Boolean::class) { _, _ -> false }
private val AUTO_PURGE = NotNullProperty<NakshaCollection, Boolean>(Boolean::class) { _, _ -> false }
private val INDICES = NotNullProperty<NakshaCollection, StringList>(StringList::class)
private val MAX_AGE = NotNullProperty<NakshaCollection, Int64>(Int64::class) { _, _ -> Int64(-1) }
private val QUAD_PARTITION_SIZE = NotNullProperty<NakshaCollection, Int>(Int::class) { _, _ -> 10_485_760 }
private val ESTIMATED_FEATURE_COUNT = NotNullProperty<NakshaCollection, Int64>(Int64::class) { _, _ -> BEFORE_ESTIMATION }
private val ESTIMATED_DELETED_FEATURES = NotNullProperty<NakshaCollection, Int64>(Int64::class) { _, _ -> BEFORE_ESTIMATION }
private val STORE_HISTORY = NotNullEnum<NakshaCollection, StoreMode>(StoreMode::class) { self, _ ->
// For downward compatibility with Naksha version 2
val old = self.getRaw("disableHistory")
if (old == true) StoreMode.SUSPEND else StoreMode.ON
}
private val STORE_DELETED = NotNullEnum<NakshaCollection, StoreMode>(StoreMode::class) { self, _ ->
// For downward compatibility with Naksha version 2
val old = self.getRaw("autoPurge")
if (old == true) StoreMode.SUSPEND else StoreMode.ON
}
private val STORE_META = NotNullEnum<NakshaCollection, StoreMode>(StoreMode::class) { _, _ -> StoreMode.ON }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
@file:Suppress("OPT_IN_USAGE")
package naksha.model.objects

import naksha.base.JsEnum
import kotlin.js.JsExport
import kotlin.jvm.JvmField
import kotlin.reflect.KClass

/**
* How the data should be stored for certain components of [NakshaCollection].
* [ON] data should be stored.
* [SUSPEND] newer data should not be collected, older data still available.
* [OFF] all data is wiped and no new data collected.
*/
@JsExport
class StoreMode: JsEnum() {
companion object StoreMode_C {
@JvmField
val ON = defIgnoreCase(StoreMode::class, "on")

@JvmField
val SUSPEND = defIgnoreCase(StoreMode::class, "suspend")

@JvmField
val OFF = defIgnoreCase(StoreMode::class, "off")
}

@Suppress("NON_EXPORTABLE_TYPE")
override fun namespace(): KClass<out JsEnum> = StoreMode::class

override fun initClass() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package naksha.model

import naksha.base.Int64
import naksha.model.objects.NakshaCollection
import naksha.model.objects.StoreMode
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class NakshaCollectionProxyTest {

Expand All @@ -14,16 +14,16 @@ class NakshaCollectionProxyTest {
val collection = NakshaCollection(
id = "ID",
partitions = 3,
autoPurge = true,
disableHistory = true
storeDeleted = StoreMode.SUSPEND,
storeHistory = StoreMode.OFF
)
collection.maxAge = Int64(42)

// expect
assertEquals("ID", collection.id)
assertEquals(3, collection.partitions)
assertEquals(42, collection.maxAge.toInt())
assertTrue(collection.autoPurge)
assertTrue(collection.disableHistory)
assertEquals(StoreMode.SUSPEND, collection.storeDeleted)
assertEquals(StoreMode.OFF, collection.storeHistory)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,6 @@ ${if (addFillFactor) "WITH (fillfactor="+if (table.isVolatile) "65)" else "100)"
@JvmField
@JsStatic
var DEFAULT_INDICES = listOf(
id_txn_uid,
gist_geo,
geo_grid_id_txn_uid,
tags_id_txn_uid,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import naksha.model.*
import naksha.model.Naksha.NakshaCompanion.VIRT_COLLECTIONS_QUOTED
import naksha.model.objects.NakshaCollection
import naksha.model.objects.NakshaFeature
import naksha.model.objects.StoreMode
import naksha.psql.*
import naksha.psql.executors.WriteExt
import naksha.psql.executors.write.WriteFeatureUtils.allColumnValues
Expand Down Expand Up @@ -41,7 +42,11 @@ class CreateCollection(
collection.create(
connection = session.usePgConnection(),
partitions = feature.partitions,
storageClass = PgStorageClass.of(feature.storageClass)
storageClass = PgStorageClass.of(feature.storageClass),
indices = PgIndex.DEFAULT_INDICES,
storeHistory = feature.storeHistory != StoreMode.OFF,
storedDeleted = feature.storeDeleted != StoreMode.OFF,
storeMeta = feature.storeMeta != StoreMode.OFF
)
return tuple
}
Expand Down
Loading

0 comments on commit 67c2e0f

Please sign in to comment.