use of doitincloud.commons.exceptions.ServerErrorException in project rdbcache by rdbcache.
the class CacheOps method update.
public Map<String, Object> update(String key, Map<String, Object> update) {
Cached cached = cache.get(key);
if (cached == null) {
return null;
}
if (cached.isTimeout()) {
if (!cached.isRefreshable()) {
cache.remove(key);
return null;
} else {
try {
Map<String, Object> map = cached.refreshable.call();
if (map == null) {
cache.remove(key);
return null;
}
for (Map.Entry<String, Object> entry : update.entrySet()) {
map.put(entry.getKey(), entry.getValue());
}
cached.setMap(map);
cached.renew();
return map;
} catch (Exception e) {
String msg = e.getCause().getMessage();
LOGGER.error(msg);
throw new ServerErrorException(msg);
}
}
} else {
Map<String, Object> map = cached.getMap();
if (map != null) {
for (Map.Entry<String, Object> entry : update.entrySet()) {
map.put(entry.getKey(), entry.getValue());
}
}
return map;
}
}
use of doitincloud.commons.exceptions.ServerErrorException in project rdbcache by rdbcache.
the class DbaseRepoImpl method find.
@Override
public boolean find(final Context context, final KvPairs pairs, final AnyKey anyKey) {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("find: " + pairs.printKey() + anyKey.print());
}
String table = anyKey.getKeyInfo().getTable();
if (table == null) {
if (!kvFind(context, pairs, anyKey)) {
LOGGER.debug("find - kvFind failed: " + pairs.printKey());
return false;
} else {
LOGGER.debug("find - kvFind Ok: " + pairs.printKey());
}
AppCtx.getKeyInfoRepo().save(context, pairs, anyKey);
return true;
} else {
JdbcTemplate jdbcTemplate = AppCtx.getDbaseOps().getJdbcTemplate(context, table);
boolean allOk = true;
if (anyKey.size() == 1) {
Query query = new Query(context, jdbcTemplate, pairs, anyKey);
if (!query.ifSelectOk() || !query.executeSelect()) {
if (enableDbFallback) {
if (!kvFind(context, pairs, anyKey)) {
String msg = "find - not found fallbacked to default table: " + pairs.printKey();
LOGGER.trace(msg);
allOk = false;
} else {
String msg = "find - found fallbacked to default table Ok: " + pairs.printKey();
context.logTraceMessage(msg);
LOGGER.warn(msg);
}
} else {
LOGGER.debug("find - not found: from " + table + " " + pairs.printKey());
allOk = false;
}
} else {
LOGGER.debug("find - found Ok: from " + table + " " + pairs.printKey());
AppCtx.getKeyInfoRepo().save(context, pairs, anyKey);
}
} else {
for (int i = 0; i < anyKey.size(); i++) {
KeyInfo keyInfo = anyKey.get(i);
if (keyInfo.getQuery() != null) {
throw new ServerErrorException("query for muliple keyInfos is not supported");
}
table = keyInfo.getTable();
AnyKey newAnyKey = new AnyKey(keyInfo);
KvPair pair = pairs.getAny(i);
KvPairs newPairs = new KvPairs(pair);
Query query = new Query(context, jdbcTemplate, newPairs, newAnyKey);
if (!query.ifSelectOk() || !query.executeSelect()) {
if (enableDbFallback) {
if (!kvFind(context, newPairs, newAnyKey)) {
String msg = "find - not found fallbacked to default table: " + newPairs.printKey();
LOGGER.trace(msg);
allOk = false;
} else {
String msg = "find - found fallbacked to default table Ok: " + newPairs.printKey();
context.logTraceMessage(msg);
LOGGER.warn(msg);
}
} else {
LOGGER.debug("find - not found: from " + table + " " + newPairs.printKey());
allOk = false;
}
} else {
LOGGER.debug("find - found Ok: from " + table + " " + pairs.printKey());
AppCtx.getKeyInfoRepo().save(context, newPairs, newAnyKey);
}
}
}
return allOk;
}
}
use of doitincloud.commons.exceptions.ServerErrorException in project rdbcache by rdbcache.
the class DbaseRepoImpl method kvDelete.
private boolean kvDelete(final Context context, final KvPairs pairs, final AnyKey anyKey) {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("kvDelete: " + pairs.printKey() + anyKey.print());
}
if (anyKey.size() == 0) {
LOGGER.trace("kvDelete(0) Ok - nothing to delete");
return true;
}
if (anyKey.size() == 1) {
StopWatch stopWatch = context.startStopWatch("dbase", "kvPairRepo.delete");
try {
AppCtx.getKvPairRepo().delete(pairs.getPair());
if (stopWatch != null)
stopWatch.stopNow();
LOGGER.trace("kvDelete(1) Ok");
return true;
} catch (Exception e) {
if (stopWatch != null)
stopWatch.stopNow();
String msg = e.getCause().getMessage();
LOGGER.error(msg);
context.logTraceMessage(msg);
e.printStackTrace();
if (context.isSync()) {
throw new ServerErrorException(context, msg);
}
}
} else {
StopWatch stopWatch = context.startStopWatch("dbase", "kvPairRepo.deleteAll");
try {
AppCtx.getKvPairRepo().deleteAll(pairs);
if (stopWatch != null)
stopWatch.stopNow();
if (LOGGER.isTraceEnabled())
LOGGER.trace("kvDelete(" + pairs.size() + ") Ok");
return true;
} catch (Exception e) {
if (stopWatch != null)
stopWatch.stopNow();
String msg = e.getCause().getMessage();
LOGGER.error(msg);
context.logTraceMessage(msg);
e.printStackTrace();
if (context.isSync()) {
throw new ServerErrorException(context, msg);
}
}
}
if (LOGGER.isTraceEnabled())
LOGGER.trace("kvDelete(" + pairs.size() + ") failed");
return false;
}
use of doitincloud.commons.exceptions.ServerErrorException in project rdbcache by rdbcache.
the class KeyInfoRepoImpl method save.
@Override
public boolean save(final Context context, final KvPairs pairs, final AnyKey anyKey) {
Assert.isTrue(anyKey.size() == pairs.size(), anyKey.size() + " != " + pairs.size() + ", only supports that pairs and anyKey have the same size");
if (pairs.size() == 0 || anyKey.size() == 0) {
LOGGER.debug("save " + pairs.printKey() + "anyKey(" + anyKey.size() + ") - nothing to save");
return false;
}
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("save " + pairs.printKey() + anyKey.print());
}
String type = pairs.get(0).getType();
String kvType = "keyInfo";
if (!type.equals("data")) {
kvType += ":" + type;
}
KvPairs keyInfoPairs = new KvPairs();
Map<String, KeyInfo> keyInfoMap = new LinkedHashMap<>();
for (int i = 0; i < pairs.size() && i < anyKey.size(); i++) {
KvPair pair = pairs.get(i);
if (!pair.getType().equals(type)) {
throw new ServerErrorException("must have the same type when saving multiple keyInfos");
}
String key = pair.getId();
KeyInfo keyInfo = anyKey.getAny(i);
if (keyInfo == null) {
if (LOGGER.isTraceEnabled())
LOGGER.trace("save KeyInfo is null for " + key);
continue;
}
if (!keyInfo.getIsNew()) {
if (LOGGER.isTraceEnabled())
LOGGER.trace("save KeyInfo is not new, skipped for " + key);
continue;
}
keyInfo.cleanup();
AppCtx.getCacheOps().putKeyInfo(pair.getIdType(), keyInfo);
if (enableRedisCache) {
keyInfoMap.put(key, keyInfo);
}
keyInfoPairs.add(new KvPair(key, kvType, Utils.toMap(keyInfo)));
}
if (keyInfoMap.size() > 0) {
StopWatch stopWatch = context.startStopWatch("redis", "keyInfoOps.putAll");
keyInfoOps.putAll(hkeyPrefix + "::" + type, keyInfoMap);
if (stopWatch != null)
stopWatch.stopNow();
}
if (keyInfoPairs.size() > 0) {
StopWatch stopWatch = context.startStopWatch("dbase", "kvPairRepo.saveAll");
AppCtx.getKvPairRepo().saveAll(keyInfoPairs);
if (stopWatch != null)
stopWatch.stopNow();
}
LOGGER.debug("save Ok: " + pairs.printKey());
return true;
}
use of doitincloud.commons.exceptions.ServerErrorException 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.getCacheOps().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;
}
Aggregations