Skip to content

Commit

Permalink
Adds cache for the count() operation.
Browse files Browse the repository at this point in the history
  • Loading branch information
ppanopticon committed May 24, 2024
1 parent e08a061 commit c4d456c
Showing 1 changed file with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Name.IndexName, Index>()

/** 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)
Expand Down Expand Up @@ -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!!
}

/**
Expand Down Expand Up @@ -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())
}
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit c4d456c

Please sign in to comment.