use of doitincloud.rdbcache.queries.QueryInfo in project rdbcache by rdbcache.
the class DbaseRepoImpl method kvFind.
private boolean kvFind(final Context context, final KvPairs pairs, final AnyKey anyKey) {
if (LOGGER.isTraceEnabled()) {
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");
KvPair dbPair = AppCtx.getKvPairRepo().findById(new KvIdType(key, "data"));
if (stopWatch != null)
stopWatch.stopNow();
if (dbPair == null) {
if (LOGGER.isTraceEnabled())
LOGGER.trace("kvFind: not found from default table for " + key);
return false;
}
setUseDefaultTable(keyInfo);
pair.setData(dbPair.getData());
if (LOGGER.isTraceEnabled())
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()) {
if (LOGGER.isTraceEnabled())
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++);
}
if (LOGGER.isTraceEnabled())
LOGGER.trace("kvFind(" + anyKey.size() + ") Ok - found from default table");
return true;
}
}
use of doitincloud.rdbcache.queries.QueryInfo 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);
}
use of doitincloud.rdbcache.queries.QueryInfo in project rdbcache by rdbcache.
the class RdbcacheApis method select_get.
/**
* select_get get multiple items
*
* To select one or more entries based on query string.
* It queries database and return immediately, and asynchronously saves the data to redis
*
* @param request HttpServletRequest
* @param opt1 String, can be expire or table or "sync" or "async"
* @param opt2 String, can be expire or table or "sync" or "async", but not otp1
* @param opt3 String, can be expire or table or "sync" or "async", but not otp1 and opt2
* @return ResponseEntity
*/
@RequestMapping(value = { "/rdbcache/v1/select", "/rdbcache/v1/select/{opt1}", "/rdbcache/v1/select/{opt1}/{opt2}", "/rdbcache/v1/select/{opt1}/{opt2}/{opt3}" }, method = RequestMethod.GET)
public ResponseEntity<?> select_get(HttpServletRequest request, @PathVariable Optional<String> opt1, @PathVariable Optional<String> opt2, @PathVariable Optional<String> opt3) {
if (request.getParameterMap().size() == 0) {
throw new BadRequestException("query string is needed, try add ?limit=256 to url");
}
Context context = new Context(true, true);
KvPairs pairs = new KvPairs();
AnyKey anyKey = Request.process(context, request, pairs, opt1, opt2, opt3);
LOGGER.trace(anyKey.print() + " pairs(" + pairs.size() + "): " + pairs.printKey());
KeyInfo keyInfo = anyKey.getKeyInfo();
if (keyInfo.getQuery() == null && pairs.size() == 0) {
QueryInfo query = new QueryInfo(keyInfo.getTable());
query.setLimit(1024);
keyInfo.setQuery(query);
String msg = "no query string found, max rows limit is forced to 1024";
LOGGER.info(msg);
context.logTraceMessage(msg);
}
if (!AppCtx.getDbaseRepo().find(context, pairs, anyKey)) {
LOGGER.debug("no record(s) found from database");
} else {
AppCtx.getAsyncOps().doSaveToRedis(context, pairs, anyKey);
}
return Response.send(context, pairs);
}
use of doitincloud.rdbcache.queries.QueryInfo in project rdbcache by rdbcache.
the class RdbcacheApis method select_post.
/**
* select_post post multiple items
*
* To select one or more entries based on query string.
* It queries database and return immediately, and asynchronously saves the data to redis
*
* @param request HttpServletRequest
* @param opt1 String, can be expire or table or "sync" or "async"
* @param opt2 String, can be expire or table or "sync" or "async", but not otp1
* @param opt3 String, can be expire or table or "sync" or "async", but not otp1 and opt2
* @param keys List, list of keys for returned entries
* @return ResponseEntity
*/
@RequestMapping(value = { "/rdbcache/v1/select", "/rdbcache/v1/select/{opt1}", "/rdbcache/v1/select/{opt1}/{opt2}", "/rdbcache/v1/select/{opt1}/{opt2}/{opt3}" }, method = RequestMethod.POST)
public ResponseEntity<?> select_post(HttpServletRequest request, @PathVariable Optional<String> opt1, @PathVariable Optional<String> opt2, @PathVariable Optional<String> opt3, @RequestBody ArrayList<String> keys) {
if (request.getParameterMap().size() == 0) {
throw new BadRequestException("query string is needed");
}
Context context = new Context(true, true);
KvPairs pairs = new KvPairs(keys);
AnyKey anyKey = Request.process(context, request, pairs, opt1, opt2, opt3);
LOGGER.trace(anyKey.print() + " pairs(" + pairs.size() + "): " + pairs.printKey());
KeyInfo keyInfo = anyKey.getKeyInfo();
if (keyInfo.getQuery() == null && pairs.size() == 0) {
QueryInfo query = new QueryInfo(keyInfo.getTable());
query.setLimit(1024);
keyInfo.setQuery(query);
String msg = "no query string found, max rows limit is forced to 1024";
LOGGER.info(msg);
context.logTraceMessage(msg);
}
if (!AppCtx.getDbaseRepo().find(context, pairs, anyKey)) {
LOGGER.debug("no record(s) found from database");
} else {
AppCtx.getAsyncOps().doSaveToRedis(context, pairs, anyKey);
}
return Response.send(context, pairs);
}
use of doitincloud.rdbcache.queries.QueryInfo in project rdbcache by rdbcache.
the class Request method processOptions.
private static void processOptions(Context context, HttpServletRequest request, KeyInfo keyInfo, String[] options) {
Map<String, String[]> params = request.getParameterMap();
if (keyInfo.getIsNew()) {
if (options[1] != null) {
keyInfo.setTable(options[1]);
}
if (options[0] != null) {
keyInfo.setExpire(options[0]);
}
if (params != null && params.size() > 0) {
QueryInfo queryInfo = new QueryInfo(keyInfo.getTable(), params);
keyInfo.setQuery(queryInfo);
}
} else {
if (options[0] != null && !options[0].equals(keyInfo.getExpire())) {
keyInfo.setExpire(options[0]);
keyInfo.setIsNew(true);
}
if (options[1] != null && !options[1].equals(keyInfo.getTable())) {
throw new BadRequestException(context, "can not change table name for an existing key");
}
if (params != null && params.size() > 0) {
QueryInfo queryInfo = new QueryInfo(keyInfo.getTable(), params);
if (keyInfo.getQueryKey() == null || !keyInfo.getQueryKey().equals(queryInfo.getKey())) {
throw new BadRequestException(context, "can not modify condition for an existing key");
}
}
}
}
Aggregations