generated from giis-uniovi/samples-giis-template
-
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.
- Loading branch information
1 parent
88e64d2
commit b46e3f5
Showing
13 changed files
with
178 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
target | ||
.tdrules-cache | ||
pom.xml.versionsBackup | ||
TESTS-TestSuites.xml | ||
dependency-reduced-pom.xml | ||
|
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
71 changes: 71 additions & 0 deletions
71
qacover-core/src/main/java/giis/qacover/core/services/TdRulesApi.java
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,71 @@ | ||
package giis.qacover.core.services; | ||
|
||
import giis.tdrules.openapi.model.DbSchema; | ||
import giis.tdrules.openapi.model.SqlParametersBody; | ||
import giis.tdrules.openapi.model.SqlRules; | ||
import giis.tdrules.openapi.model.SqlRulesBody; | ||
import giis.tdrules.openapi.model.SqlTableListBody; | ||
|
||
/** | ||
* Temporal implementation of the client api, to be moved to tdrules-client | ||
*/ | ||
public class TdRulesApi extends giis.tdrules.client.TdRulesApi { | ||
|
||
private boolean useCache; | ||
private String cacheLocation; | ||
|
||
public TdRulesApi(String uri) { | ||
super(uri); | ||
this.cacheLocation = Configuration.getInstance().getCacheRulesLocation(); | ||
this.useCache = !"".equals(coalesce(cacheLocation)); | ||
} | ||
|
||
/** | ||
* Gets the fpc rules for a query executed under the specified schema | ||
*/ | ||
@Override | ||
public SqlRules getRules(DbSchema schema, String query, String options) { | ||
SqlRulesBody request=new SqlRulesBody().schema(schema).sql(query).options(coalesce(options)); | ||
TdRulesCache cache = getCache("rulesPost", request); | ||
|
||
if (useCache() && cache.hit()) | ||
return (SqlRules)cache.getPayload(SqlRules.class); | ||
SqlRules result = super.rulesPost(request); | ||
if (useCache()) | ||
cache.putPayload(result); | ||
return result; | ||
} | ||
|
||
public SqlTableListBody getTables(String sql) { | ||
TdRulesCache cache = getCache("sqlTablesPost", sql); | ||
if (useCache() && cache.hit()) | ||
return (SqlTableListBody)cache.getPayload(SqlTableListBody.class); | ||
SqlTableListBody result = super.sqlTablesPost(sql); | ||
if (useCache()) | ||
cache.putPayload(result); | ||
return result; | ||
} | ||
|
||
public SqlParametersBody getParameters(String sql) { | ||
TdRulesCache cache = getCache("sqlParametersPost", sql); | ||
if (useCache() && cache.hit()) | ||
return (SqlParametersBody)cache.getPayload(SqlParametersBody.class); | ||
SqlParametersBody result = super.sqlParametersPost(sql); | ||
if (useCache()) | ||
cache.putPayload(result); | ||
return result; | ||
} | ||
|
||
// Cache management | ||
|
||
private boolean useCache() { | ||
return this.useCache; | ||
} | ||
private TdRulesCache getCache(String endpoint, Object request) { | ||
return useCache() ? new TdRulesCache(this.cacheLocation, endpoint, request) : null; | ||
} | ||
private String coalesce(String value) { | ||
return value == null ? "" : value; | ||
} | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
qacover-core/src/main/java/giis/qacover/core/services/TdRulesCache.java
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,59 @@ | ||
package giis.qacover.core.services; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import giis.portable.util.FileUtil; | ||
import giis.portable.util.JavaCs; | ||
import giis.tdrules.model.io.ModelJsonSerializer; | ||
|
||
/** | ||
* Temporal implementation of a local storage cache for payloads sent to | ||
* TdRules, to be moved to tdrules-client | ||
*/ | ||
public class TdRulesCache { | ||
private static final Logger log = LoggerFactory.getLogger(TdRulesCache.class); | ||
|
||
private ModelJsonSerializer serializer; | ||
String endpoint; | ||
String payload; | ||
String hash; | ||
String cacheFile; | ||
String hit; | ||
|
||
public TdRulesCache(String cacheFolder, String endpoint, Object request) { | ||
serializer = new ModelJsonSerializer(); | ||
this.endpoint = endpoint; | ||
this.payload = serializer.serialize(request, true); | ||
this.hash = JavaCs.getHash(payload); | ||
this.cacheFile = getCacheFile(cacheFolder, endpoint, hash); | ||
this.hit = FileUtil.fileRead(cacheFile, false); | ||
log.debug("Cache {} {} hit: {}", endpoint, hash, this.hit != null); | ||
} | ||
|
||
/** | ||
* Gets the payload of a given request from cache, if does not exists returns | ||
* null | ||
*/ | ||
public boolean hit() { | ||
return this.hit != null; | ||
} | ||
|
||
@SuppressWarnings("rawtypes") | ||
public Object getPayload(Class clazz) { | ||
return serializer.deserialize(hit, clazz); | ||
} | ||
|
||
/** | ||
* Saves the response payload of a given request to cache | ||
*/ | ||
public void putPayload(Object result) { | ||
FileUtil.fileWrite(cacheFile, serializer.serialize(result, true)); | ||
log.debug("Cache {} {} update: {}", endpoint, hash, result); | ||
} | ||
|
||
private String getCacheFile(String cacheFolder, String endpoint, String hash) { | ||
return FileUtil.getPath(cacheFolder, endpoint, hash + ".json"); | ||
} | ||
|
||
} |
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
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