use of com.rdbcache.models.KeyInfo 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
* @param opt2 String, can be expire or table, but not otp1
* @return ResponseEntity
*/
@RequestMapping(value = { "/rdbcache/v1/select", "/rdbcache/v1/select/{opt1}", "/rdbcache/v1/select/{opt1}/{opt2}" }, method = RequestMethod.GET)
public ResponseEntity<?> select_get(HttpServletRequest request, @PathVariable Optional<String> opt1, @PathVariable Optional<String> opt2) {
Context context = new Context(true, true);
KvPairs pairs = new KvPairs();
AnyKey anyKey = Request.process(context, request, pairs, opt1, opt2);
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 com.rdbcache.models.KeyInfo in project rdbcache by rdbcache.
the class AnyKey method getAny.
public KeyInfo getAny(int index) {
int size = size();
if (index > size) {
throw new ServerErrorException("getAny index out of range");
}
if (size == 0 || index == size) {
KeyInfo keyInfo = null;
if (size == 0) {
keyInfo = new KeyInfo();
keyInfo.setIsNew(true);
} else {
keyInfo = get(0).clone();
keyInfo.setIsNew(true);
keyInfo.clearParams();
}
add(keyInfo);
}
return get(index);
}
use of com.rdbcache.models.KeyInfo in project rdbcache by rdbcache.
the class Query method executeInsert.
public boolean executeInsert(boolean enableLocal, boolean enableRedis) {
params = new ArrayList<>();
boolean allOk = true;
for (int i = 0; i < pairs.size(); i++) {
KvPair pair = pairs.get(i);
KeyInfo keyInfo = anyKey.getAny(i);
String table = keyInfo.getTable();
Map<String, Object> map = pair.getData();
String autoIncKey = AppCtx.getDbaseOps().getTableAutoIncColumn(context, table);
boolean cacheUpdate = false;
if (!map.containsKey(autoIncKey) && keyInfo.getParams() != null && keyInfo.getParams().size() == 1) {
String stdClause = "(" + autoIncKey + " = ?)";
if (stdClause.equals(keyInfo.getClause())) {
map.put(autoIncKey, keyInfo.getParams().get(0));
cacheUpdate = true;
}
}
Map<String, Object> columns = keyInfo.getColumns();
AppCtx.getDbaseOps().convertDbMap(columns, map);
String fields = "", values = "";
params.clear();
for (Map.Entry<String, Object> entry : map.entrySet()) {
params.add(entry.getValue());
if (fields.length() != 0) {
fields += ", ";
values += ", ";
}
fields += entry.getKey();
values += "?";
}
sql = "insert into " + table + " (" + fields + ") values (" + values + ")";
LOGGER.trace("sql: " + sql);
LOGGER.trace("params: " + params.toString());
int rowCount = 0;
KeyHolder keyHolder = new GeneratedKeyHolder();
StopWatch stopWatch = context.startStopWatch("dbase", "jdbcTemplate.update");
try {
rowCount = jdbcTemplate.update(new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
PreparedStatement ps;
ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
int i = 1;
for (Object param : params) {
ps.setObject(i++, param);
}
return ps;
}
}, keyHolder);
if (stopWatch != null)
stopWatch.stopNow();
} catch (Exception e) {
if (stopWatch != null)
stopWatch.stopNow();
allOk = false;
String msg = e.getCause().getMessage();
LOGGER.error(msg);
context.logTraceMessage(msg);
if (context.isSync()) {
throw new ServerErrorException(context, msg);
}
}
if (rowCount > 0) {
if (autoIncKey != null && keyHolder.getKey() != null) {
String keyValue = String.valueOf(keyHolder.getKey());
map.put(autoIncKey, keyValue);
cacheUpdate = true;
}
if (cacheUpdate) {
if (enableLocal) {
AppCtx.getLocalCache().putData(pair, keyInfo);
}
if (enableRedis) {
AppCtx.getRedisRepo().save(context, new KvPairs(pair), new AnyKey(keyInfo));
}
}
if (!Parser.prepareStandardClauseParams(context, pair, keyInfo)) {
String msg = "executeInsert failed when prepareStandardClauseParams for " + pair.getId();
LOGGER.error(msg);
context.logTraceMessage(msg);
if (context.isSync()) {
throw new ServerErrorException(context, msg);
}
}
LOGGER.trace("inserted " + pair.getId() + " into " + table);
} else {
allOk = false;
LOGGER.warn("failed to insert " + pair.getId() + " into " + table);
}
}
return allOk;
}
use of com.rdbcache.models.KeyInfo in project rdbcache by rdbcache.
the class Query method executeDelete.
public boolean executeDelete() {
boolean allOk = true;
for (int i = 0; i < pairs.size(); i++) {
KvPair pair = pairs.get(i);
KeyInfo keyInfo = anyKey.getAny(i);
String table = keyInfo.getTable();
if (!keyInfo.getIsNew() && !keyInfo.hasParams() && keyInfo.ifJustCreated()) {
waitForParamsUpdate(pair.getId(), keyInfo);
}
if (!Parser.prepareStandardClauseParams(context, pair, keyInfo)) {
String msg = "executeDelete failed when calling prepareStandardClauseParams for " + pair.getId();
LOGGER.error(msg);
context.logTraceMessage(msg);
if (context.isSync()) {
throw new ServerErrorException(context, msg);
}
continue;
}
params = keyInfo.getParams();
String clause = keyInfo.getClause();
sql = "delete from " + table + " where " + clause + " limit 1";
LOGGER.trace("sql: " + sql);
LOGGER.trace("params: " + params.toString());
StopWatch stopWatch = context.startStopWatch("dbase", "jdbcTemplate.delete");
try {
if (jdbcTemplate.update(sql, params.toArray()) > 0) {
if (stopWatch != null)
stopWatch.stopNow();
LOGGER.trace("delete " + pair.getId() + " from " + table);
continue;
} else {
if (stopWatch != null)
stopWatch.stopNow();
allOk = false;
}
} catch (Exception e) {
if (stopWatch != null)
stopWatch.stopNow();
allOk = false;
String msg = e.getCause().getMessage();
LOGGER.error(msg);
context.logTraceMessage(msg);
e.printStackTrace();
if (context.isSync()) {
throw new ServerErrorException(context, msg);
}
}
keyInfo.setQueryKey(null);
}
return allOk;
}
use of com.rdbcache.models.KeyInfo in project rdbcache by rdbcache.
the class Query method executeSelect.
public boolean executeSelect() {
KeyInfo keyInfo = anyKey.getKeyInfo();
String table = keyInfo.getTable();
LOGGER.trace("sql: " + sql);
LOGGER.trace("params: " + (params != null ? params.toString() : "null"));
List<Map<String, Object>> list = null;
StopWatch stopWatch = context.startStopWatch("dbase", "jdbcTemplate.queryForList");
try {
if (params != null) {
list = jdbcTemplate.queryForList(sql, params.toArray());
} else {
list = jdbcTemplate.queryForList(sql);
}
if (stopWatch != null)
stopWatch.stopNow();
if (list != null && list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
KvPair pair = pairs.getAny(i);
keyInfo = anyKey.getAny(i);
pair.setData(list.get(i));
//
if (!Parser.prepareStandardClauseParams(context, pair, keyInfo)) {
String msg = "executeSelect failed when prepareStandardClauseParams for " + pair.getId();
LOGGER.error(msg);
context.logTraceMessage(msg);
if (context.isSync()) {
throw new ServerErrorException(context, msg);
}
}
LOGGER.trace("found " + pair.getId() + " from " + table);
}
return true;
}
} catch (Exception e) {
if (stopWatch != null)
stopWatch.stopNow();
e.printStackTrace();
String msg = e.getCause().getMessage();
LOGGER.error(msg);
context.logTraceMessage(msg);
if (context.isSync()) {
throw new ServerErrorException(context, msg);
}
}
return false;
}
Aggregations