use of com.rdbcache.models.KeyInfo in project rdbcache by rdbcache.
the class Query method ifSelectOk.
public boolean ifSelectOk() {
Assert.isTrue(anyKey.size() == 1, "anyKey.size() = " + anyKey.size() + ", select only supports anyKey size == 1");
KeyInfo keyInfo = anyKey.getKeyInfo();
String table = keyInfo.getTable();
if (table == null) {
return false;
}
String queryKey = keyInfo.getQueryKey();
if (queryKey == null) {
return false;
}
String clause = keyInfo.getClause();
params = keyInfo.getParams();
if (clause == null || clause.length() == 0 || params == null || params.size() == 0) {
boolean ready = false;
KvPair pair = pairs.getPair();
if (Parser.prepareQueryClauseParams(context, pair, keyInfo)) {
ready = true;
} else if (keyInfo.getQueryLimit() != null) {
ready = true;
} else if (pairs.size() > 0) {
ready = true;
} else {
if (!keyInfo.getIsNew() && !keyInfo.hasParams() && keyInfo.ifJustCreated()) {
waitForParamsUpdate(pair.getId(), keyInfo);
}
if (Parser.prepareStandardClauseParams(context, pair, keyInfo)) {
ready = true;
}
}
if (!ready) {
return false;
}
clause = keyInfo.getClause();
params = keyInfo.getParams();
}
int limit = getLimit();
sql = "select * from " + table;
if (clause != null && clause.length() > 0) {
sql += " where " + clause;
}
if (limit != 0) {
sql += " limit " + limit;
}
QueryInfo queryInfo = keyInfo.getQuery();
if (queryInfo != null) {
keyInfo.setQuery(null);
Utils.getExcutorService().submit(() -> {
Thread.yield();
AppCtx.getDbaseOps().saveQuery(context, queryInfo);
});
}
return true;
}
use of com.rdbcache.models.KeyInfo in project rdbcache by rdbcache.
the class Query method waitForParamsUpdate.
// it could be the data is just inserted, not synchronized yet
// max wait for 30 seconds
private void waitForParamsUpdate(String key, KeyInfo keyInfo) {
if (keyInfo.hasParams()) {
return;
}
for (int j = 0; j < 300; j++) {
KeyInfo cachedKeyInfo = AppCtx.getLocalCache().getKeyInfo(key);
if (cachedKeyInfo.hasParams()) {
keyInfo.setClause(cachedKeyInfo.getClause());
keyInfo.setParams(cachedKeyInfo.getParams());
return;
}
// allow time to synchronize data
try {
LOGGER.trace("waitForParamsUpdate 100 ms ...");
Thread.sleep(100);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
}
}
LOGGER.warn("failed on waitForParamsUpdate");
}
use of com.rdbcache.models.KeyInfo in project rdbcache by rdbcache.
the class Query method ifDeleteOk.
public boolean ifDeleteOk() {
Assert.isTrue(anyKey.size() == pairs.size(), anyKey.size() + " != " + pairs.size() + ", delete only supports anyKey size == pairs size");
KeyInfo keyInfo = anyKey.getKeyInfo();
String table = keyInfo.getTable();
if (table == null) {
return false;
}
String queryKey = keyInfo.getQueryKey();
if (queryKey == null) {
return false;
}
return true;
}
use of com.rdbcache.models.KeyInfo in project rdbcache by rdbcache.
the class ParserTest method prepareQueryClauseParams.
@Test
public void prepareQueryClauseParams() {
Context context = new Context();
KvPair pair = new KvPair("hash_key");
KeyInfo keyInfo = new KeyInfo();
keyInfo.setExpire("100");
keyInfo.setTable("user_table");
String json = "{\"table\":\"user_table\",\"conditions\":{\"id\":{\"=\":[\"1\",\"2\",\"3\"]}},\"limit\":2}";
Map<String, Object> map = Utils.toMap(json);
QueryInfo queryInfo = Utils.toPojo(map, QueryInfo.class);
keyInfo.setQuery(queryInfo);
Parser.prepareQueryClauseParams(context, pair, keyInfo);
assertEquals("(id = ? OR id = ? OR id = ?)", keyInfo.getClause());
assertEquals("[1, 2, 3]", keyInfo.getParams().toString());
}
use of com.rdbcache.models.KeyInfo in project rdbcache by rdbcache.
the class QueryTest method insertTest.
@Test
public void insertTest() {
try {
Context context = new Context();
KvPairs pairs = new KvPairs();
String json = "{\n" + " \"email\" : \"sam@example.com\",\n" + " \"name\" : \"Sam W.\",\n" + " \"dob\" : \"1975-08-12\"\n" + " }";
Map<String, Object> map1 = Utils.toMap(json);
KvPair pair = new KvPair("*", map1);
pairs.setPair(pair);
AnyKey anyKey = new AnyKey();
KeyInfo keyInfo = new KeyInfo();
keyInfo.setExpire("100");
keyInfo.setTable("user_table");
anyKey.setKeyInfo(keyInfo);
Query query = new Query(context, jdbcTemplate, pairs, anyKey);
assertTrue(query.ifInsertOk());
assertTrue(query.executeInsert(false, false));
// System.out.println(Utils.toJsonMap(pairs));
Map<String, Object> map2 = pair.getData();
assertEquals("4", map2.get("id"));
map2.remove("id");
assertEquals(map1, map2);
} catch (Exception e) {
e.printStackTrace();
fail(e.getCause().getMessage());
}
}
Aggregations