Skip to content

Commit

Permalink
finos#989 refactoring the ignite query index and row count calculatio…
Browse files Browse the repository at this point in the history
…n logic and bug fix
  • Loading branch information
naleeha authored and rumakt committed Feb 15, 2024
1 parent 56e8810 commit 387040d
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,31 @@ import org.finos.vuu.core.table.RowWithData
import org.finos.vuu.example.ignite.IgniteOrderStore
import org.finos.vuu.example.ignite.module.IgniteOrderDataModule
import org.finos.vuu.example.ignite.provider.IgniteOrderDataProvider.columnNameByExternalField
import org.finos.vuu.example.ignite.query.IndexCalculator
import org.finos.vuu.example.ignite.schema.IgniteChildOrderEntity
import org.finos.vuu.feature.ignite.schema.SchemaMapper
import org.finos.vuu.plugin.virtualized.table.{VirtualizedRange, VirtualizedSessionTable, VirtualizedViewPortKeys}
import org.finos.vuu.provider.VirtualizedProvider
import org.finos.vuu.viewport.ViewPort
import org.finos.vuu.viewport.{ViewPort, ViewPortRange}

import java.util.concurrent.atomic.AtomicInteger

class IgniteOrderDataProvider(final val igniteStore: IgniteOrderStore)
(implicit clock: Clock) extends VirtualizedProvider with StrictLogging {
private val extraRowsCount = 5000 //fetch extra rows to reduce need to re-fetch when view port change by small amount

private val schemaMapper = SchemaMapper(IgniteChildOrderEntity.getSchema, IgniteOrderDataModule.columns, columnNameByExternalField)
private val dataQuery = IgniteOrderDataQuery(igniteStore, schemaMapper)
private val indexCalculator = IndexCalculator(extraRowsCount = 5000)

override def runOnce(viewPort: ViewPort): Unit = {

val internalTable = viewPort.table.asTable.asInstanceOf[VirtualizedSessionTable]

val range = viewPort.getRange
val totalSize = igniteStore.childOrderCount().toInt

internalTable.setSize(totalSize)

val startIndex = Math.max(range.from - extraRowsCount, 0)
val endIndex = range.to + extraRowsCount
val rowCount = if (endIndex > startIndex) endIndex - startIndex else 1
val (startIndex, endIndex, rowCount) = indexCalculator.calc(viewPort.getRange, totalSize)

internalTable.setSize(totalSize)//todo should this be long?
internalTable.setRange(VirtualizedRange(startIndex, endIndex))

logger.info(s"Loading data between $startIndex and $endIndex")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.finos.vuu.example.ignite.query

import org.finos.vuu.viewport.ViewPortRange

object IndexCalculator {
def apply(extraRowsCount : Int) : IndexCalculator =
new IndexCalculator(extraRowsCount)
}

class IndexCalculator(extraRowCount:Int) {
private val extraRowsCount = extraRowCount //fetch extra rows to reduce need to re-fetch when view port change by small amount
private type StartIndex = Int
private type EndIndex = Int
private type RowCount = Int
def calc(viewPortRange: ViewPortRange, totalSize: Int): (StartIndex, EndIndex, RowCount) = {

val startIndex = Math.max(viewPortRange.from - extraRowsCount, 0)
val endIndex = Math.min(viewPortRange.to + extraRowsCount, totalSize)
val rowCount =
if (startIndex == 0 && endIndex==0) 0
else if (endIndex == startIndex) 1
else endIndex - startIndex
(startIndex, endIndex, rowCount)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.finos.vuu.example.ignite.query

import org.finos.vuu.viewport.ViewPortRange
import org.scalatest.funsuite.AnyFunSuiteLike
import org.scalatest.matchers.should.Matchers

class CalcIndexTest extends AnyFunSuiteLike with Matchers {

test("Get index given view port range is at start") {
val (starIndex, endIndex, rowCount) =
IndexCalculator(extraRowsCount = 10)
.calc(ViewPortRange(from = 0, to = 100), totalSize = 1000)

starIndex shouldEqual 0
endIndex shouldEqual 110
rowCount shouldEqual 110
}

test("Get index given view port range is at middle") {
val (starIndex, endIndex, rowCount) =
IndexCalculator(extraRowsCount = 10)
.calc(ViewPortRange(from = 100, to = 200), totalSize = 1000)

starIndex shouldEqual 90
endIndex shouldEqual 210
rowCount shouldEqual 120
}

test("Get index given view port range is at end") {
val (starIndex, endIndex, rowCount) =
IndexCalculator(extraRowsCount = 10)
.calc(ViewPortRange(from = 900, to = 1000), totalSize = 1000)

starIndex shouldEqual 890
endIndex shouldEqual 1000
rowCount shouldEqual 110
}

test("Get single row given view port range of one") {
val (starIndex, endIndex, rowCount) =
IndexCalculator(extraRowsCount = 0)
.calc(ViewPortRange(from = 5, to = 5), totalSize = 1000)

starIndex shouldEqual 5
endIndex shouldEqual 5
rowCount shouldEqual 1
}

test("Get zero rows given view port range or zero") {
val (starIndex, endIndex, rowCount) =
IndexCalculator(extraRowsCount = 0)
.calc(ViewPortRange(from = 0, to = 0), totalSize = 1000)

starIndex shouldEqual 0
endIndex shouldEqual 0
rowCount shouldEqual 0
}
}

0 comments on commit 387040d

Please sign in to comment.