Search in sources :

Example 36 with KeyInfo

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;
}
Also used : KeyInfo(com.rdbcache.models.KeyInfo) KvPair(com.rdbcache.models.KvPair)

Example 37 with KeyInfo

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");
}
Also used : KeyInfo(com.rdbcache.models.KeyInfo)

Example 38 with KeyInfo

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;
}
Also used : KeyInfo(com.rdbcache.models.KeyInfo)

Example 39 with KeyInfo

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());
}
Also used : Context(com.rdbcache.helpers.Context) KvPair(com.rdbcache.models.KvPair) KeyInfo(com.rdbcache.models.KeyInfo) Test(org.junit.Test)

Example 40 with KeyInfo

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());
    }
}
Also used : Context(com.rdbcache.helpers.Context) AnyKey(com.rdbcache.helpers.AnyKey) KvPair(com.rdbcache.models.KvPair) KeyInfo(com.rdbcache.models.KeyInfo) KvPairs(com.rdbcache.helpers.KvPairs) Test(org.junit.Test)

Aggregations

KeyInfo (com.rdbcache.models.KeyInfo)44 KvPair (com.rdbcache.models.KvPair)23 Test (org.junit.Test)13 ServerErrorException (com.rdbcache.exceptions.ServerErrorException)9 StopWatch (com.rdbcache.models.StopWatch)9 AnyKey (com.rdbcache.helpers.AnyKey)7 Context (com.rdbcache.helpers.Context)7 KvPairs (com.rdbcache.helpers.KvPairs)7 BadRequestException (com.rdbcache.exceptions.BadRequestException)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 SQLException (java.sql.SQLException)4 QueryInfo (com.rdbcache.queries.QueryInfo)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 MockServletContext (org.springframework.mock.web.MockServletContext)2 KeyInfoRedisTemplate (com.rdbcache.configs.KeyInfoRedisTemplate)1 NotFoundException (com.rdbcache.exceptions.NotFoundException)1 DbaseOps (com.rdbcache.services.DbaseOps)1 LocalCache (com.rdbcache.services.LocalCache)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1