use of doitincloud.rdbcache.supports.KvPairs 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;
}
use of doitincloud.rdbcache.supports.KvPairs in project rdbcache by rdbcache.
the class KvPairsTest method mapConstructor.
@Test
public void mapConstructor() {
Map<String, Object> map = new LinkedHashMap<>();
map.put("k0", Utils.toMap("{\"kk0\": 0}"));
map.put("k1", Utils.toMap("{\"kk1\": 1}"));
map.put("k2", Utils.toMap("{\"kk2\": 2}"));
KvPairs pairs = new KvPairs(map);
assertEquals(3, pairs.size());
for (int i = 0; i < 3; i++) {
KvPair pair = pairs.get(i);
assertEquals("k" + i, pair.getId());
map = pair.getData();
assertEquals(i, map.get("kk" + i));
}
}
use of doitincloud.rdbcache.supports.KvPairs in project rdbcache by rdbcache.
the class KvPairsTest method setPair.
@Test
public void setPair() {
KvPairs pairs;
KvPair pair;
pairs = new KvPairs();
pair = new KvPair();
pairs.setPair(pair);
assertEquals(1, pairs.size());
assertTrue(pair == pairs.get(0));
pairs = new KvPairs(new KvPair());
pair = new KvPair("key", "data", "value");
pairs.setPair(pair);
assertEquals(1, pairs.size());
assertTrue(pair == pairs.get(0));
}
use of doitincloud.rdbcache.supports.KvPairs in project rdbcache by rdbcache.
the class KvPairsTest method getAny.
@Test
public void getAny() {
KvPairs pairs;
KvPair pair;
pairs = new KvPairs();
pair = pairs.getAny();
assertNotNull(pair);
pair = new KvPair("key", "data", "value");
pairs = new KvPairs(pair);
KvPair pair2 = pairs.getAny();
assertNotNull(pair2);
assertTrue(pair == pair2);
assertTrue(pair == pairs.get(0));
for (int i = 0; i < 10; i++) {
pair = pairs.getAny(i);
assertNotNull(pair);
assertEquals(i + 1, pairs.size());
}
}
use of doitincloud.rdbcache.supports.KvPairs in project rdbcache by rdbcache.
the class SimpleExpireOps method setExpireKey.
public void setExpireKey(Context context, KvPairs pairs, AnyKey anyKey) {
for (int i = 0; i < pairs.size(); i++) {
KvPair pair = pairs.get(i);
String key = pair.getId();
KeyInfo keyInfo = anyKey.getAny(i);
LOGGER.debug("setExpireKey: " + key + " expire: " + keyInfo.getExpire());
String expire = keyInfo.getExpire();
String expKey = eventPrefix + "::" + key;
boolean hasKey = AppCtx.getRedisRepo().ifExist(context, new KvPairs(expKey), new AnyKey(keyInfo));
Long expValue = Long.valueOf(expire);
boolean done = false;
// remove existing expire key
if (hasKey) {
if (expValue <= 0L || expire.startsWith("+")) {
AppCtx.getRedisRepo().delete(context, new KvPairs(expKey), new AnyKey(keyInfo));
} else {
// for unsigned expire, event existed, no update
done = true;
}
}
// zero means no expiration
if (!done && expValue == 0L) {
done = true;
}
if (!done) {
if (expValue < 0) {
expValue = -expValue;
}
LOGGER.debug("setup expire: " + key + " expire: " + keyInfo.getExpire());
AppCtx.getStringRedisTemplate().opsForValue().set(expKey, expire, expValue);
} else {
keyInfo.restoreExpire();
}
if (keyInfo.getIsNew()) {
LOGGER.debug("save keyInfo: " + key + " expire: " + keyInfo.getExpire());
AppCtx.getKeyInfoRepo().save(context, new KvPairs(pair), new AnyKey(keyInfo));
}
}
}
Aggregations