forked from finos/vuu
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
finos#1167 refactor and improve the flow of IgniteOrderDataProvider
- Loading branch information
1 parent
d60be88
commit f0ca7ba
Showing
9 changed files
with
199 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...he-ignite/src/main/scala/org/finos/vuu/example/ignite/provider/IgniteOrderDataQuery.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package org.finos.vuu.example.ignite.provider | ||
|
||
import org.finos.vuu.core.module.simul.model.ChildOrder | ||
import org.finos.vuu.core.sort.ModelType.SortSpecInternal | ||
import org.finos.vuu.example.ignite.IgniteOrderStore | ||
import org.finos.vuu.example.ignite.provider.IgniteOrderDataQuery.RowKeyAndData | ||
import org.finos.vuu.feature.ignite.FilterAndSortSpecToSql | ||
import org.finos.vuu.feature.ignite.schema.SchemaMapper | ||
import org.finos.vuu.net.FilterSpec | ||
|
||
class IgniteOrderDataQuery private (private val igniteOrderStore: IgniteOrderStore, | ||
private val schemaMapper: SchemaMapper) { | ||
|
||
private val filterAndSortSpecToSql = FilterAndSortSpecToSql(schemaMapper) | ||
|
||
def fetch(filterSpec: FilterSpec, sortSpec: SortSpecInternal, startIndex: Long, rowCount: Int): LazyList[ChildOrder] = { | ||
igniteOrderStore.findChildOrder( | ||
filterAndSortSpecToSql.filterToSql(filterSpec), | ||
filterAndSortSpecToSql.sortToSql(sortSpec), | ||
startIndex = startIndex, | ||
rowCount = rowCount | ||
) | ||
} | ||
|
||
def toInternalRow(keyField: String): Product => RowKeyAndData = | ||
dto => { | ||
val values = dto.productIterator.toList | ||
val rowData = schemaMapper.toTableRowData(values) | ||
val key = values(schemaMapper.externalSchemaField(keyField).get.index) | ||
(key.toString, rowData) | ||
} | ||
} | ||
|
||
object IgniteOrderDataQuery { | ||
def apply(igniteOrderStore: IgniteOrderStore, schemaMapper: SchemaMapper): IgniteOrderDataQuery = | ||
new IgniteOrderDataQuery(igniteOrderStore, schemaMapper) | ||
|
||
private type RowKey = String | ||
type RowKeyAndData = (RowKey, Map[String, Any]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...test/scala/org/finos/vuu/example/ignite/provider/IgniteOrderDataQueryFunctionalTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package org.finos.vuu.example.ignite.provider | ||
|
||
import org.apache.ignite.IgniteCache | ||
import org.finos.vuu.core.module.simul.model.{ChildOrder, ParentOrder} | ||
import org.finos.vuu.core.sort.SortDirection | ||
import org.finos.vuu.example.ignite.module.IgniteOrderDataModule | ||
import org.finos.vuu.example.ignite.{IgniteOrderStore, TestUtils} | ||
import org.finos.vuu.net.FilterSpec | ||
import org.scalatest.funsuite.AnyFunSuiteLike | ||
import org.scalatest.matchers.should.Matchers | ||
|
||
class IgniteOrderDataQueryFunctionalTest extends AnyFunSuiteLike with Matchers { | ||
|
||
private val ignite = TestUtils.setupIgnite() | ||
private val parentOrderCache: IgniteCache[Int, ParentOrder] = ignite.getOrCreateCache("parentOrderCache") | ||
private val childOrderCache: IgniteCache[Int, ChildOrder] = ignite.getOrCreateCache("childOrderCache") | ||
private val orderStore = new IgniteOrderStore(parentOrderCache, childOrderCache) | ||
private val dataQuery = IgniteOrderDataQuery(orderStore, IgniteOrderDataModule.schemaMapper) | ||
|
||
test("Can parse and apply filtering and sorting when fetching") { | ||
val testOrder1 = TestUtils.createChildOrder(1, ric = "ABC.HK", price = 5.55) | ||
val testOrder2 = TestUtils.createChildOrder(2, ric = "ABC.LDN", price = 6.0) | ||
val testOrder3 = TestUtils.createChildOrder(3, ric = "ABC.NY", price = 4.5) | ||
givenChildOrdersExist(testOrder1, testOrder2, testOrder3) | ||
|
||
val filterSpec = FilterSpec("orderId > 1 and ric starts \"ABC\"") | ||
val sortSpec = Map("price" -> SortDirection.Ascending) | ||
|
||
val res = dataQuery.fetch(filterSpec, sortSpec, startIndex = 0, rowCount = 3) | ||
|
||
res.toList shouldEqual List(testOrder3, testOrder2) | ||
} | ||
|
||
private def givenChildOrdersExist(childOrders: ChildOrder*): Unit = { | ||
val parentOrder = TestUtils.createParentOrder(1) | ||
childOrders.foreach(o => orderStore.storeChildOrder(parentOrder, o)) | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...gnite/src/test/scala/org/finos/vuu/example/ignite/provider/IgniteOrderDataQueryTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package org.finos.vuu.example.ignite.provider | ||
|
||
import org.finos.vuu.core.sort.SortDirection | ||
import org.finos.vuu.core.table.{Column, SimpleColumn} | ||
import org.finos.vuu.example.ignite.IgniteOrderStore | ||
import org.finos.vuu.example.ignite.schema.IgniteEntitySchemaBuilder | ||
import org.finos.vuu.feature.ignite.schema.SchemaMapper | ||
import org.finos.vuu.net.FilterSpec | ||
import org.scalamock.scalatest.MockFactory | ||
import org.scalatest.featurespec.AnyFeatureSpec | ||
import org.scalatest.matchers.should.Matchers | ||
|
||
class IgniteOrderDataQueryTest extends AnyFeatureSpec with Matchers with MockFactory { | ||
|
||
private val igniteStore: IgniteOrderStore = mock[IgniteOrderStore] | ||
private val schemaMapper = SchemaMapper(entitySchema, externalFieldByColumns) | ||
private val igniteDataQuery = IgniteOrderDataQuery(igniteStore, schemaMapper) | ||
|
||
Feature("toInternalRow") { | ||
val testEntity = TestDto(1, "TestDto", 100) | ||
|
||
Scenario("can convert a dto to internal row representation") { | ||
val internalKey = "id" | ||
|
||
val res = igniteDataQuery.toInternalRow(internalKey)(testEntity) | ||
|
||
res._1 shouldEqual "1" | ||
res._2 shouldEqual Map("id" -> testEntity.key, "name" -> testEntity.name, "value" -> testEntity.value) | ||
} | ||
} | ||
|
||
Feature("fetch") { | ||
|
||
Scenario("can parse and apply filter and sort spec") { | ||
val filterSpec = FilterSpec("id = 2 and name != \"ABC\"") | ||
val sortSpec = Map("value" -> SortDirection.Ascending) | ||
|
||
(igniteStore.findChildOrder _).expects("(key = 2 AND name != 'ABC')", "value ASC", *, *).once() | ||
|
||
igniteDataQuery.fetch(filterSpec, sortSpec, 0, 0) | ||
} | ||
} | ||
|
||
private def externalFieldByColumns: Map[String, Column] = Map( | ||
"key" -> SimpleColumn("id", 0, classOf[Int]), | ||
"name" -> SimpleColumn("name", 1, classOf[String]), | ||
"value" -> SimpleColumn("value", 2, classOf[String]), | ||
) | ||
|
||
private def entitySchema = IgniteEntitySchemaBuilder().withCaseClass[TestDto].build() | ||
} | ||
|
||
private case class TestDto(key: Int, name: String, value: Int) |
36 changes: 36 additions & 0 deletions
36
...in/ignite-plugin/src/main/scala/org/finos/vuu/feature/ignite/FilterAndSortSpecToSql.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package org.finos.vuu.feature.ignite | ||
|
||
import org.finos.vuu.core.filter.FilterSpecParser | ||
import org.finos.vuu.core.sort.ModelType.SortSpecInternal | ||
import org.finos.vuu.feature.ignite.filter.{IgniteSqlFilterClause, IgniteSqlFilterTreeVisitor} | ||
import org.finos.vuu.feature.ignite.schema.SchemaMapper | ||
import org.finos.vuu.feature.ignite.sort.IgniteSqlSortBuilder | ||
import org.finos.vuu.net.FilterSpec | ||
|
||
trait FilterAndSortSpecToSql { | ||
def filterToSql(filterSpec: FilterSpec): String | ||
def sortToSql(sortSpec: SortSpecInternal): String | ||
} | ||
|
||
object FilterAndSortSpecToSql { | ||
def apply(schemaMapper: SchemaMapper): FilterAndSortSpecToSql = { | ||
new FilterAndSortSpecToSqlImpl(schemaMapper) | ||
} | ||
} | ||
|
||
private class FilterAndSortSpecToSqlImpl(private val schemaMapper: SchemaMapper) extends FilterAndSortSpecToSql { | ||
// @Todo convert IgniteSqlFilterTreeVisitor & IgniteSqlSortBuilder to objects? | ||
private val filterTreeVisitor = new IgniteSqlFilterTreeVisitor | ||
private val igniteSqlSortBuilder = new IgniteSqlSortBuilder | ||
|
||
override def filterToSql(filterSpec: FilterSpec): String = { | ||
if (filterSpec.filter == null || filterSpec.filter.isEmpty) { | ||
"" | ||
} else { | ||
val clause = FilterSpecParser.parse[IgniteSqlFilterClause](filterSpec.filter, filterTreeVisitor) | ||
clause.toSql(schemaMapper) | ||
} | ||
} | ||
|
||
override def sortToSql(sortSpec: SortSpecInternal): String = igniteSqlSortBuilder.toSql(sortSpec, schemaMapper) | ||
} |