From c4d456c5f98048926c513e05ae72cbf029bbb8ca Mon Sep 17 00:00:00 2001 From: Ralph Gasser Date: Fri, 24 May 2024 10:00:29 +0200 Subject: [PATCH] Adds cache for the count() operation. --- .../cottontail/dbms/entity/DefaultEntity.kt | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/cottontaildb-dbms/src/main/kotlin/org/vitrivr/cottontail/dbms/entity/DefaultEntity.kt b/cottontaildb-dbms/src/main/kotlin/org/vitrivr/cottontail/dbms/entity/DefaultEntity.kt index 5d6c9f668..367e0d9d0 100644 --- a/cottontaildb-dbms/src/main/kotlin/org/vitrivr/cottontail/dbms/entity/DefaultEntity.kt +++ b/cottontaildb-dbms/src/main/kotlin/org/vitrivr/cottontail/dbms/entity/DefaultEntity.kt @@ -96,6 +96,9 @@ class DefaultEntity(override val name: Name.EntityName, override val parent: Def /** Map of [Name.IndexName] to [IndexTx]. */ private val indexes = Object2ObjectLinkedOpenHashMap() + /** Cache for [count] (since this seems to take quite some time on large bitmaps. */ + private var countCache: Long? = null + init { /* Cache this Tx for future use. */ context.txn.cacheTx(this) @@ -177,7 +180,10 @@ class DefaultEntity(override val name: Name.EntityName, override val parent: Def * @return The number of entries in this [DefaultEntity]. */ override fun count(): Long = this.txLatch.withLock { - this.bitmap.count(this.context.txn.xodusTx) + if (this.countCache == null) { + this.countCache = this.bitmap.count(this.context.txn.xodusTx) + } + return this.countCache!! } /** @@ -383,6 +389,9 @@ class DefaultEntity(override val name: Name.EntityName, override val parent: Def /* Signal event to transaction context. */ this.context.txn.signalEvent(event) + /* Invalidate count cache. */ + this.countCache = null + /* Return generated record. */ return StandaloneTuple(tupleId, inserts.keys.toTypedArray(), inserts.values.toTypedArray()) } @@ -445,6 +454,9 @@ class DefaultEntity(override val name: Name.EntityName, override val parent: Def index.newTx(this.context).delete(event) } + /* Invalidate count cache. */ + this.countCache = null + /* Signal event to transaction context. */ this.context.txn.signalEvent(event) }