Skip to content

Commit

Permalink
Added temporary object caching
Browse files Browse the repository at this point in the history
  • Loading branch information
Fonoster Team committed May 26, 2019
1 parent 5750f8c commit 110cca7
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 38 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ dependencies {
compile 'redis.clients:jedis:2.9.0'
compile 'org.mongodb:bson:3.7.1'
compile 'junit:junit:4.+'
compile 'com.github.ben-manes.caffeine:caffeine:2.7.0'
compile 'org.graalvm.sdk:graal-sdk:19.0.0'
compile 'org.graalvm.truffle:truffle-api:19.0.0'
compile 'org.graalvm.js:js:19.0.0'
Expand Down
6 changes: 6 additions & 0 deletions config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ spec:
port: 5060
- protocol: tcp
port: 5060
dataSource:
provider: 'redis_data_provider'
parameters:
host: '192.168.1.127'
port: 6379
secret: osopolar
8 changes: 4 additions & 4 deletions etc/tests/sipp/register.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash

SIPP_PORT=5080
ROUTR_HOST=192.168.1.12
ROUTR_HOST=192.168.1.127
SCENARIO_FILE=uac_register.xml
REGISTER_FILE=register.csv

Expand All @@ -14,6 +14,6 @@ docker run -it -p $SIPP_PORT:$SIPP_PORT/udp \
-p $SIPP_PORT \
-sf $SCENARIO_FILE \
-inf $REGISTER_FILE \
-r 20000 \
-m 100000 \
-l 500
-r 5000 \
-m 200000 \
-l 100000
44 changes: 32 additions & 12 deletions mod/data_api/agents_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ const DSUtil = require('@routr/data_api/utils')
const { Status } = require('@routr/core/status')
const { UNFULFILLED_DEPENDENCY_RESPONSE } = require('@routr/core/status')
const isEmpty = require('@routr/utils/obj_util')
const Caffeine = Java.type('com.github.benmanes.caffeine.cache.Caffeine')
const TimeUnit = Java.type('java.util.concurrent.TimeUnit')

class AgentsAPI {

constructor(dataSource) {
this.ds = dataSource
this.cache = Caffeine.newBuilder()
.expireAfterWrite(5, TimeUnit.MINUTES)
.maximumSize(5000)
.build();
}

save(agent, operation) {
Expand All @@ -36,23 +42,29 @@ class AgentsAPI {
}

getAgentByDomain(domainUri, username) {
const response = this.getAgents()
let agent
response.result.forEach(obj => {
if (obj.spec.credentials.username == username) {
obj.spec.domains.forEach(d => {
if (domainUri == d) {
agent = obj
}
})
}
})
const key = domainUri.trim() + '.' + username.trim()
let agent = this.cache.getIfPresent(key)

if(agent == null) {
const response = this.getAgents()

response.result.forEach(obj => {
if (obj.spec.credentials.username == username) {
obj.spec.domains.forEach(d => {
if (domainUri == d) {
agent = obj
this.cache.put(key, agent)
}
})
}
})
}

return isEmpty(agent)? CoreUtils.buildResponse(Status.NOT_FOUND): CoreUtils.buildResponse(Status.OK, agent)
}

getAgentByRef(ref) {
return DSUtil.deepSearch(this.getAgents(), "metadata.ref", ref)
return this.ds.withCollection('agents').get(ref)
}

/**
Expand All @@ -67,6 +79,14 @@ class AgentsAPI {
}

deleteAgent(ref) {
const agent = this.getAgent(ref)
agent.spec.domains.forEach(domain => {
const key = domain.trim() + '.' + agent.spec.credentials.username.trim()
if (this.cache.getIfPresent(key)) {
this.cache.invalidate(key)
}
})

return this.ds.withCollection('agents').remove(ref)
}

Expand Down
20 changes: 18 additions & 2 deletions mod/data_api/dids_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@ const CoreUtils = require('@routr/core/utils')
const DSUtil = require('@routr/data_api/utils')
const { Status } = require('@routr/core/status')
const { UNFULFILLED_DEPENDENCY_RESPONSE } = require('@routr/core/status')
const Caffeine = Java.type('com.github.benmanes.caffeine.cache.Caffeine')
const TimeUnit = Java.type('java.util.concurrent.TimeUnit')

class DIDsAPI {

constructor(dataSource) {
this.ds = dataSource
this.cache = Caffeine.newBuilder()
.expireAfterWrite(5, TimeUnit.MINUTES)
.maximumSize(5000)
.build();
}

createFromJSON(jsonObj) {
Expand Down Expand Up @@ -40,22 +46,32 @@ class DIDsAPI {
}

getDID(ref) {
return DSUtil.deepSearch(this.getDIDs(), "metadata.ref", ref)
return this.ds.withCollection('gateways').get(ref)
}

/**
* note: telUrl maybe a string in form of 'tel:${number}' or
* a TelURL Object.
*/
getDIDByTelUrl(telUrl) {
return DSUtil.deepSearch(this.getDIDs(), "spec.location.telUrl", telUrl)
let did = this.cache.getIfPresent(telUrl)

if (did == null) {
did = DSUtil.deepSearch(this.getDIDs(), "spec.location.telUrl", telUrl)
this.cache.put(telUrl, did)
}

return did
}

didExist(telUrl) {
return DSUtil.objExist(this.getDIDByTelUrl(telUrl))
}

deleteDID(ref) {
if (this.cache.getIfPresent(ref)) {
this.cache.invalidate(ref)
}
return this.ds.withCollection('dids').remove(ref)
}

Expand Down
21 changes: 19 additions & 2 deletions mod/data_api/domains_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@
const CoreUtils = require('@routr/core/utils')
const DSUtil = require('@routr/data_api/utils')
const { Status } = require('@routr/core/status')
const Caffeine = Java.type('com.github.benmanes.caffeine.cache.Caffeine')
const TimeUnit = Java.type('java.util.concurrent.TimeUnit')

const foundDependentObjects = { status: Status.CONFLICT, message: Status.message[4092].value }

class DomainsAPI {

constructor(dataSource) {
this.ds = dataSource
this.cache = Caffeine.newBuilder()
.expireAfterWrite(5, TimeUnit.MINUTES)
.maximumSize(5000)
.build();
}

createFromJSON(jsonObj) {
Expand All @@ -28,18 +34,29 @@ class DomainsAPI {
}

getDomain(ref) {
return DSUtil.deepSearch(this.getDomains(), "metadata.ref", ref)
returnthis.ds.withCollection('domains').get(ref)
}

getDomainByUri(domainUri) {
return DSUtil.deepSearch(this.getDomains(), "spec.context.domainUri", domainUri)
let domain = this.cache.getIfPresent(domainUri)

if (domain == null) {
domain = DSUtil.deepSearch(this.getDomains(), "spec.context.domainUri", domainUri)
this.cache.put(domainUri, domain)
}

return domain
}

domainExist(domainUri) {
return DSUtil.objExist(this.getDomainByUri(domainUri))
}

deleteDomain(ref) {
if (this.cache.getIfPresent(ref)) {
this.cache.invalidate(ref)
}

let response = this.getDomain(ref)

if (response.status != Status.OK) {
Expand Down
21 changes: 19 additions & 2 deletions mod/data_api/gateways_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@ const CoreUtils = require('@routr/core/utils')
const DSUtil = require('@routr/data_api/utils')
const { Status } = require('@routr/core/status')
const { FOUND_DEPENDENT_OBJECTS_RESPONSE } = require ('@routr/core/status')
const Caffeine = Java.type('com.github.benmanes.caffeine.cache.Caffeine')
const TimeUnit = Java.type('java.util.concurrent.TimeUnit')

class GatewaysAPI {

constructor(dataSource) {
this.ds = dataSource
this.cache = Caffeine.newBuilder()
.expireAfterWrite(5, TimeUnit.MINUTES)
.maximumSize(100)
.build();
}

createFromJSON(jsonObj) {
Expand All @@ -26,18 +32,29 @@ class GatewaysAPI {
}

getGateway(ref) {
return DSUtil.deepSearch(this.getGateways(), "metadata.ref", ref)
return this.ds.withCollection('gateways').get(ref)
}

getGatewayByHost(host) {
return DSUtil.deepSearch(this.getGateways(), "spec.host", host)
let gw = this.cache.getIfPresent(host)

if (gw == null) {
gw = DSUtil.deepSearch(this.getGateways(), "spec.host", host)
this.cache.put(host, gw)
}

return gw
}

gatewayExist(host) {
return DSUtil.objExist(this.getGatewayByHost(host))
}

deleteGateway(ref) {
if (this.cache.getIfPresent(ref)) {
this.cache.invalidate(ref)
}

let response = this.getGateway(ref)

if (response.status != Status.OK) {
Expand Down
21 changes: 19 additions & 2 deletions mod/data_api/peers_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@
const CoreUtils = require('@routr/core/utils')
const DSUtil = require('@routr/data_api/utils')
const { Status } = require('@routr/core/status')
const Caffeine = Java.type('com.github.benmanes.caffeine.cache.Caffeine')
const TimeUnit = Java.type('java.util.concurrent.TimeUnit')

class PeersAPI {

constructor(dataSource) {
this.ds = dataSource
this.cache = Caffeine.newBuilder()
.expireAfterWrite(5, TimeUnit.MINUTES)
.maximumSize(100)
.build();
}

updateFromJSON(jsonObj) {
Expand All @@ -25,18 +31,29 @@ class PeersAPI {
}

getPeer(ref) {
return DSUtil.deepSearch(this.getPeers(), "metadata.ref", ref)
return this.ds.withCollection('peers').get(ref)
}

peerExist(username) {
return DSUtil.objExist(this.getPeerByUsername(username))
}

getPeerByUsername(username) {
return DSUtil.deepSearch(this.getPeers(), "spec.credentials.username", username)
let peer = this.cache.getIfPresent(username)

if (peer == null) {
peer = DSUtil.deepSearch(this.getPeers(), "spec.credentials.username", username)
this.cache.put(username, peer)
}

return peer
}

deletePeer(ref) {
if (this.cache.getIfPresent(ref)) {
this.cache.invalidate(ref)
}

return this.ds.withCollection('peers').remove(ref)
}

Expand Down
Loading

0 comments on commit 110cca7

Please sign in to comment.