Skip to content

Commit

Permalink
#1213 Added extra functionality to row builder
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjstevo committed Mar 13, 2024
1 parent 0d23ed2 commit 80baace
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,24 @@ class BasketProvider(val table: DataTable)(implicit lifecycle: LifecycleContaine
private val runner = new RunOnceLifeCycleRunner("BasketProvider", runOnce)
private val basketLoader = new BasketLoader()

private val idColumn = table.columnForName(Id)
private val nameColumn = table.columnForName(Name)

lifecycle(this).dependsOn(runner)
def runOnce(): Unit = {
val data = basketLoader.loadBasketIds()

//reuse of the builder...
val builder = table.rowBuilder

data.foreach(id => {
table.processUpdate(id, RowWithData(id, Map(
Id -> id,
Name -> id
)), clock.now())
table.processUpdate(id,
builder.setKey(id)
.setString(idColumn, id)
.setString(nameColumn, id)
//as row clears out the data from the builder
.asRow,
clock.now())
})
}
override val lifecycleId: String = "org.finos.vuu.core.module.basket.provider.BasketProvider"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import scala.collection.mutable
class InMemMapRowBuilder extends RowBuilder {

private val mutableMap = new mutable.HashMap[String, Any]()
private var key: String = ""
private var key: String = null
override def setLong(column: Column, v: Long): RowBuilder = {
mutableMap.put(column.name, v)
this
Expand Down Expand Up @@ -36,10 +36,13 @@ class InMemMapRowBuilder extends RowBuilder {
this
}
override def asRow: RowData = {
if(key == null){
throw new RuntimeException("Key has not been set, this is likely a coding error.")
}
val immMap = mutableMap.toMap
val rowData = RowWithData(key, immMap)
mutableMap.clear()
key = ""
key = null
rowData
}
}
19 changes: 19 additions & 0 deletions vuu/src/main/scala/org/finos/vuu/core/row/RowBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,24 @@ trait RowBuilder {
def setInt(column: Column, v: Int): RowBuilder
def setString(column: Column, v: String): RowBuilder
def setBoolean(column: Column, v: Boolean): RowBuilder
/**
* this metyhod effectively resets the builder, emptying its existing contents to begin again.
* @return row with data set
*/
def asRow: RowData
}

object NoRowBuilder extends RowBuilder{
override def setKey(key: String): RowBuilder = ???
override def setLong(column: Column, v: Long): RowBuilder = ???
override def setDouble(column: Column, v: Double): RowBuilder = ???
override def setInt(column: Column, v: Int): RowBuilder = ???
override def setString(column: Column, v: String): RowBuilder = ???
override def setBoolean(column: Column, v: Boolean): RowBuilder = ???
/**
* this metyhod effectively resets the builder, emptying its existing contents to begin again.
*
* @return row with data set
*/
override def asRow: RowData = ???
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.finos.vuu.api.TableDef
import org.finos.vuu.provider.JoinTableProvider
import io.vertx.core.impl.ConcurrentHashSet
import org.finos.toolbox.jmx.MetricsProvider
import org.finos.vuu.core.row.RowBuilder

class AutoSubscribeTable(tableDef: TableDef, joinProvider: JoinTableProvider)(implicit override val metrics: MetricsProvider) extends InMemDataTable(tableDef, joinProvider) with StrictLogging {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ trait DataTable extends KeyedObservable[RowKeyUpdate] with RowSource {
def updateCounter: Long

def newRow(key: String): RowBuilder

def rowBuilder: RowBuilder
def incrementUpdateCounter(): Unit

def indexForColumn(column: Column): Option[IndexedField[_]]
Expand Down Expand Up @@ -237,6 +237,7 @@ class InMemDataTable(val tableDef: TableDef, val joinProvider: JoinTableProvider
override def newRow(key: String): RowBuilder = {
new InMemMapRowBuilder().setKey(key)
}
override def rowBuilder: RowBuilder = new InMemMapRowBuilder

private def buildIndexForColumn(c: Column): IndexedField[_] = {
c.dataType match {
Expand Down
4 changes: 3 additions & 1 deletion vuu/src/main/scala/org/finos/vuu/core/table/JoinTable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.finos.vuu.provider.JoinTableProvider
import org.finos.vuu.viewport.{RowProcessor, ViewPortColumns}
import org.finos.toolbox.collection.array.{ImmutableArray, ImmutableArrays}
import org.finos.toolbox.jmx.MetricsProvider
import org.finos.vuu.core.row.RowBuilder
import org.finos.vuu.core.row.{NoRowBuilder, RowBuilder}
import org.finos.vuu.feature.inmem.InMemTablePrimaryKeys

import java.util
Expand Down Expand Up @@ -660,4 +660,6 @@ class JoinTable(val tableDef: JoinTableDef, val sourceTables: Map[String, DataTa

override def getColumnValueProvider: ColumnValueProvider = InMemColumnValueProvider(this)
override def newRow(key: String): RowBuilder = ???

override def rowBuilder: RowBuilder = NoRowBuilder
}
120 changes: 120 additions & 0 deletions vuu/src/test/scala/org/finos/vuu/core/row/RowBuilderTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package org.finos.vuu.core.row

import org.finos.toolbox.jmx.MetricsProviderImpl
import org.finos.toolbox.lifecycle.LifecycleContainer
import org.finos.toolbox.time.{Clock, TestFriendlyClock}
import org.finos.vuu.api.TableDef
import org.finos.vuu.core.table.{Columns, InMemDataTable}
import org.finos.vuu.provider.VuuJoinTableProvider
import org.scalatest.GivenWhenThen
import org.scalatest.featurespec.AnyFeatureSpec
import org.scalatest.matchers.should.Matchers

class RowBuilderTest extends AnyFeatureSpec with Matchers with GivenWhenThen{

def getTableDef = {
TableDef(
name = "instruments",
keyField = "ric",
columns = Columns.fromNames("ric:String", "description:String", "currency: String", "exchange:String", "lotSize:Double"),
joinFields = "ric"
)
}

Feature("Test the row Builder for use in providers"){

Scenario("Test New Each Time Row Builder"){

implicit val clock: Clock = new TestFriendlyClock(1000000)
implicit val lifecycle: LifecycleContainer = new LifecycleContainer
implicit val metrics: MetricsProviderImpl = new MetricsProviderImpl

val tableDef = getTableDef
val joinTableProvider: VuuJoinTableProvider = new VuuJoinTableProvider()

val (ricColumn, descColumn, currColumn, exchangeColumn, lotSizeColumn) =
(
tableDef.columnForName("ric"), tableDef.columnForName("description"),
tableDef.columnForName("currency"),tableDef.columnForName("exchange"), tableDef.columnForName("lotSize")
)

val table = new InMemDataTable(tableDef, joinTableProvider)

val row = table.rowBuilder
.setKey("FOO.L")
.setString(ricColumn, "FOO.L")
.setString(descColumn, "Foo Inc")
.setString(currColumn, "GBP")
.setString(exchangeColumn, "LSE")
.setDouble(lotSizeColumn, 1000.123)
.asRow

row.key() should equal("FOO.L")
row.get(ricColumn) should equal("FOO.L")
row.get(descColumn) should equal("Foo Inc")
row.get(currColumn) should equal("GBP")
row.get(exchangeColumn) should equal("LSE")
row.get(lotSizeColumn) should equal(1000.123)

}

Scenario("Test Reuse of Row Builder"){

implicit val clock: Clock = new TestFriendlyClock(1000000)
implicit val lifecycle: LifecycleContainer = new LifecycleContainer
implicit val metrics: MetricsProviderImpl = new MetricsProviderImpl

val tableDef = getTableDef
val joinTableProvider: VuuJoinTableProvider = new VuuJoinTableProvider()

val (ricColumn, descColumn, currColumn, exchangeColumn, lotSizeColumn) =
(
tableDef.columnForName("ric"), tableDef.columnForName("description"),
tableDef.columnForName("currency"),tableDef.columnForName("exchange"), tableDef.columnForName("lotSize")
)

val table = new InMemDataTable(tableDef, joinTableProvider)

val builder = table.rowBuilder

val row = builder.setKey("FOO.L")
.setString(ricColumn, "FOO.L")
.setString(descColumn, "Foo Inc")
.setString(currColumn, "GBP")
.setString(exchangeColumn, "LSE")
.setDouble(lotSizeColumn, 1000.123)
.asRow

row.key() should equal("FOO.L")
row.get(ricColumn) should equal("FOO.L")
row.get(descColumn) should equal("Foo Inc")
row.get(currColumn) should equal("GBP")
row.get(exchangeColumn) should equal("LSE")
row.get(lotSizeColumn) should equal(1000.123)

intercept[RuntimeException]{
builder.asRow
}

val row2 = builder.setKey("BAR.L")
.setString(ricColumn, "BAR.L")
.setString(descColumn, "Bar Inc")
.setString(currColumn, "USD")
.setString(exchangeColumn, "NYSE")
.setDouble(lotSizeColumn, 1010.123)
.asRow

row2.key() should equal("BAR.L")
row2.get(ricColumn) should equal("BAR.L")
row2.get(descColumn) should equal("Bar Inc")
row2.get(currColumn) should equal("USD")
row2.get(exchangeColumn) should equal("NYSE")
row2.get(lotSizeColumn) should equal(1010.123)

}


}


}

0 comments on commit 80baace

Please sign in to comment.