use of com.rdbcache.queries.Condition in project rdbcache by rdbcache.
the class DbaseRepoImpl method kvFind.
private boolean kvFind(Context context, KvPairs pairs, AnyKey anyKey) {
LOGGER.trace("kvFind: " + pairs.printKey() + anyKey.print());
KeyInfo keyInfo = anyKey.getKeyInfo();
if (pairs.size() == 1) {
KvPair pair = pairs.getPair();
String key = pair.getId();
StopWatch stopWatch = context.startStopWatch("dbase", "kvPairRepo.findOne");
Optional<KvPair> opt = AppCtx.getKvPairRepo().findById(new KvIdType(key, "data"));
if (stopWatch != null)
stopWatch.stopNow();
if (!opt.isPresent()) {
LOGGER.trace("kvFind: not found from default table for " + key);
return false;
}
KvPair dbPair = opt.get();
setUseDefaultTable(keyInfo);
pair.setData(dbPair.getData());
LOGGER.trace("kvFind(1) Ok - found from default table for " + key);
return true;
} else {
QueryInfo queryInfo = keyInfo.getQuery();
if (queryInfo == null) {
// no queryKey means no query
return false;
}
Map<String, Condition> conditions = queryInfo.getConditions();
if (// must have condtion
conditions == null || // only 1 condition allowed
conditions.size() != 1 || !conditions.containsKey("key")) {
// only condition with key allowed
return false;
}
Condition condition = conditions.get("key");
if (// must have condition
condition == null || // only 1 condition
condition.size() != 1 || !condition.containsKey("=")) {
// only = allowed
return false;
}
List<String> keys = condition.get("=");
if (keys == null || keys.size() == 0) {
return false;
}
List<KvIdType> idTypes = new ArrayList<KvIdType>();
for (String key : keys) {
KvIdType idType = new KvIdType(key, "data");
idTypes.add(idType);
}
StopWatch stopWatch = context.startStopWatch("dbase", "kvPairRepo.findAll");
Iterable<KvPair> dbPairs = AppCtx.getKvPairRepo().findAllById(idTypes);
if (stopWatch != null)
stopWatch.stopNow();
if (dbPairs == null && !dbPairs.iterator().hasNext()) {
LOGGER.trace("kvFind(" + anyKey.size() + ") failed to find from default table");
return false;
}
if (queryInfo != null) {
keyInfo.setQuery(null);
Utils.getExcutorService().submit(() -> {
Thread.yield();
AppCtx.getDbaseOps().saveQuery(context, queryInfo);
});
}
anyKey.clear();
int i = 0;
for (KvPair dbPair : dbPairs) {
pairs.add(dbPair);
anyKey.getAny(i++);
}
LOGGER.trace("kvFind(" + anyKey.size() + ") Ok - found from default table");
return true;
}
}
use of com.rdbcache.queries.Condition in project rdbcache by rdbcache.
the class KeyInfoTest method objectToMap4.
@Test
public void objectToMap4() {
Map<String, Object> map1 = Utils.toMap("{\"expire\":\"30\",\"table\":\"user_table\",\"clause\":" + "\"id = ?\",\"params\":[12466],\"query\":{\"table\":\"user_table\",\"conditions\":{\"id\":" + "{\"=\":[\"12466\"]}}},\"query_key\":\"28f0a2d90b3c9d340e853b838d27845c\",\"is_new\":" + "false,\"expire_old\":\"180\",\"created_at\":1522367412704}");
KeyInfo keyInfo1 = Utils.toPojo(map1, KeyInfo.class);
QueryInfo queryInfo = new QueryInfo("user_table");
String[] params = { "12466" };
Condition condition = new Condition("=", params);
Map<String, Condition> conditions = new LinkedHashMap<>();
conditions.put("id", condition);
queryInfo.setConditions(conditions);
keyInfo1.setQuery(queryInfo);
Map<String, Object> map2 = Utils.toMap(keyInfo1);
KeyInfo keyInfo2 = Utils.toPojo(map2, KeyInfo.class);
// System.out.println(Utils.toJsonMap(map2));
assertEquals(keyInfo1, keyInfo2);
assertEquals(map1, map2);
}