Skip to content

Commit

Permalink
Move caching of Table and Ref into new classes
Browse files Browse the repository at this point in the history
Also use singleton for caching
  • Loading branch information
dolfinus committed Jun 29, 2019
1 parent 1be3bef commit 1cad6b6
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 58 deletions.
32 changes: 9 additions & 23 deletions src/org/camunda/latera/bss/connectors/hid/Table.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,16 @@ import static org.camunda.latera.bss.utils.StringUtil.*
import static org.camunda.latera.bss.utils.ListUtil.*
import static org.camunda.latera.bss.utils.MapUtil.*
import static org.camunda.latera.bss.utils.Oracle.*
import org.camunda.latera.bss.internal.TableColumnCache

trait Table {
private static LinkedHashMap TABLE_COLUMNS_CACHE = [:]
private static LinkedHashMap DEFAULT_WHERE = [:]
private static LinkedHashMap DEFAULT_ORDER = [:]
private static List DEFAULT_FIELDS = null

private void putTableColumnsCache(CharSequence tableName, List columnsList) {
if (!TABLE_COLUMNS_CACHE.containsKey(tableName.toString())) {
TABLE_COLUMNS_CACHE[tableName.toString()] = columnsList
}
}

private List getTableColumnsCached(CharSequence tableName) {
if (!TABLE_COLUMNS_CACHE.containsKey(tableName.toString())) {
return TABLE_COLUMNS_CACHE[tableName.toString()]
}
return null
}
private static LinkedHashMap DEFAULT_WHERE = [:]
private static LinkedHashMap DEFAULT_ORDER = [:]
private static List DEFAULT_FIELDS = null

List getTableColumns(CharSequence tableName, CharSequence tableOwner = 'AIS_NET') {
String tableFullName = "${tableOwner}.${tableName}"
List columnsList = getTableColumnsCached(tableFullName)
List columnsList = TableColumnCache.instance.get(tableFullName)
if (columnsList) {
return columnsList
}
Expand All @@ -39,9 +26,8 @@ trait Table {
AND OWNER = '${tableOwner}'
""", false, true)

columnsList = result*.getAt(0) //get only first column values
putTableColumnsCache(tableFullName, columnsList)
return columnsList
columnsList = result*.getAt(0) // get only first column values
return TableColumnCache.instance.putAndGet(tableFullName, columnsList)
}

List getTableData(
Expand Down Expand Up @@ -140,9 +126,9 @@ trait Table {
order = DEFAULT_ORDER
) {
if (isString(fields) && fields != '*') {
return getTableData(tableName, [fields], where)?.getAt(0)?."${fields}"
return getTableData(tableName, [fields], where, order)?.getAt(0)?."${fields}"
}
return getTableData(tableName, fields, where)?.getAt(0)
return getTableData(tableName, fields, where, order)?.getAt(0)
}

def getTableFirst(Map options, CharSequence tableName) {
Expand Down
47 changes: 12 additions & 35 deletions src/org/camunda/latera/bss/connectors/hid/hydra/Ref.groovy
Original file line number Diff line number Diff line change
@@ -1,38 +1,18 @@
package org.camunda.latera.bss.connectors.hid.hydra

import static org.camunda.latera.bss.utils.Numeric.toIntSafe
import org.camunda.latera.bss.internal.RefCache

trait Ref {
private static LinkedHashMap REFS_CACHE = [null: null]
private static String REFS_TABLE = 'SI_V_REF'
private static String DEFAULT_CURRENCY = 'CURR_Ruble'
private static String UNKNOWN_UNIT = 'UNIT_Unknown'
private static String PIECE_UNIT = 'UNIT_Piece'
private static String REFS_TABLE = 'SI_V_REF'
private static String DEFAULT_CURRENCY = 'CURR_Ruble'
private static String UNKNOWN_UNIT = 'UNIT_Unknown'
private static String PIECE_UNIT = 'UNIT_Piece'

String getRefsTable() {
return REFS_TABLE
}

private void putRefCache(CharSequence code, def refId) {
if (!REFS_CACHE.containsKey(code.toString())) {
REFS_CACHE[code.toString()] = toIntSafe(refId)
}
}

private Number getRefIdCached(CharSequence code) {
if (REFS_CACHE.containsKey(code.toString())) {
return REFS_CACHE[code.toString()]
}
return null
}

private String getRefCodeCached(def id) {
if (REFS_CACHE.containsValue(id)) {
return REFS_CACHE.find{it.value == id}?.key
}
return null
}

Map getRef(def refId) {
LinkedHashMap where = [
n_ref_id: refId
Expand Down Expand Up @@ -129,7 +109,7 @@ trait Ref {
List result = hid.getTableData(getRefsTable(), where: where)
if (result) {
result.each { ref ->
putRefCache(ref.vc_code, ref.n_ref_id)
RefCache.instance.put(ref.vc_code, ref.n_ref_id)
}
}
return result
Expand All @@ -148,17 +128,16 @@ trait Ref {
}

Number getRefIdByCode(CharSequence code) {
def id = getRefIdCached(code)
def id = RefCache.instance.get(code)
if (id) {
return id
}

LinkedHashMap where = [
vc_code: code
]
id = toIntSafe(hid.getTableFirst(getRefsTable(), 'n_ref_id', where))
putRefCache(code, id)
return id
id = hid.getTableFirst(getRefsTable(), 'n_ref_id', where)
return RefCache.instance.putAndGet(code, id)
}

Number getRefIdByName(CharSequence name) {
Expand All @@ -169,18 +148,16 @@ trait Ref {
}

String getRefCode(def id) {
id = toIntSafe(id)
String code = getRefCodeCached(id)
String code = RefCache.instance.getKey(id)
if (code) {
return code
}

LinkedHashMap where = [
n_ref_id: id
]
code = hid.getTableFirst(getRefsTable(), 'vc_code', where)
putRefCache(code, id)
return code
code = hid.getTableFirst(getRefsTable(), 'vc_code', where).toString()
return RefCache.instance.putAndGetKey(code, id)
}

String getRefCodeById(def id) {
Expand Down
43 changes: 43 additions & 0 deletions src/org/camunda/latera/bss/internal/RefCache.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.camunda.latera.bss.internal

import java.math.BigInteger
import java.util.concurrent.ConcurrentHashMap
import static org.camunda.latera.bss.utils.Numeric.toIntSafe

class RefCache {
private Map<String, BigInteger> map;
private static final RefCache instance = new RefCache();

static RefCache getInstance() {
return instance;
}

private RefCache() {
map = new ConcurrentHashMap<String, BigInteger>();
}

void put(CharSequence key, def value) {
if (key != null && value != null) {
map[key.toString()] = toIntSafe(value)
}
}

def get(CharSequence key) {
return map[key.toString()]
}

def putAndGet(CharSequence key, def value) {
put(key, value)
return get(key)
}

def getKey(def value) {
BigInteger val = toIntSafe(value)
return map.find{it.value == val}?.key
}

String putAndGetKey(CharSequence key, def value) {
put(key, value)
return key.toString()
}
}
31 changes: 31 additions & 0 deletions src/org/camunda/latera/bss/internal/TableColumnCache.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.camunda.latera.bss.internal

import java.util.concurrent.ConcurrentHashMap

class TableColumnCache {
private Map<String, List> map;
private static final TableColumnCache instance = new TableColumnCache();

static TableColumnCache getInstance() {
return instance;
}

private TableColumnCache() {
map = new ConcurrentHashMap<String, List>();
}

void put(CharSequence key, List value) {
if (key != null && value != null) {
map[key.toString()] = value
}
}

def get(CharSequence key) {
return map[key.toString()]
}

def putAndGet(CharSequence key, def value) {
put(key, value)
return get(key)
}
}

0 comments on commit 1cad6b6

Please sign in to comment.