Search in sources :

Example 41 with KeyInfo

use of doitincloud.rdbcache.models.KeyInfo in project rdbcache by rdbcache.

the class RequestTest method process1.

@Test
public void process1() {
    try {
        String key = "*";
        Optional<String> tableOpt = Optional.of("tb1");
        Optional<String> expireOpt = Optional.of("30");
        String queryString = "id=1";
        HttpServletRequest request = getRequest("get", key, null, tableOpt, expireOpt, queryString);
        Context context = new Context();
        AnyKey anyKey = Request.process(context, request, new KvPairs(key), tableOpt, expireOpt);
        assertEquals(1, anyKey.size());
        KeyInfo keyInfo = anyKey.getKeyInfo();
        assertTrue(keyInfo.getIsNew());
        assertEquals("KeyInfo(true, tb1, 30, {id: {=: [1]}})", keyInfo.toString());
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getCause().getMessage());
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Context(doitincloud.rdbcache.supports.Context) MockServletContext(org.springframework.mock.web.MockServletContext) AnyKey(doitincloud.rdbcache.supports.AnyKey) KeyInfo(doitincloud.rdbcache.models.KeyInfo) KvPairs(doitincloud.rdbcache.supports.KvPairs) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 42 with KeyInfo

use of doitincloud.rdbcache.models.KeyInfo in project rdbcache by rdbcache.

the class Query method ifInsertOk.

public boolean ifInsertOk() {
    Assert.isTrue(anyKey.size() == 1, "anyKey.size() = " + anyKey.size() + ", insert 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;
    }
    return true;
}
Also used : KeyInfo(doitincloud.rdbcache.models.KeyInfo)

Example 43 with KeyInfo

use of doitincloud.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(doitincloud.rdbcache.models.KeyInfo)

Example 44 with KeyInfo

use of doitincloud.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);
                if (pair.getType().equals("data")) {
                    pair.setType(table);
                }
                pair.setData(list.get(i));
                keyInfo = anyKey.getAny(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;
}
Also used : KeyInfo(doitincloud.rdbcache.models.KeyInfo) KvPair(doitincloud.rdbcache.models.KvPair) ServerErrorException(doitincloud.commons.exceptions.ServerErrorException) SQLException(java.sql.SQLException) ServerErrorException(doitincloud.commons.exceptions.ServerErrorException) StopWatch(doitincloud.rdbcache.models.StopWatch)

Example 45 with KeyInfo

use of doitincloud.rdbcache.models.KeyInfo in project rdbcache by rdbcache.

the class Query method getLimit.

private int getLimit() {
    KeyInfo keyInfo = anyKey.getKeyInfo();
    Integer queryLimit = keyInfo.getQueryLimit();
    int size = pairs.size();
    int limit = 0;
    if (queryLimit != null || size > 0) {
        if (size == 0) {
            limit = queryLimit;
        } else if (queryLimit != null && size > queryLimit) {
            limit = queryLimit;
        } else {
            limit = size;
        }
    }
    if (limit == 0 && !context.isBatch()) {
        limit = 1;
    }
    return limit;
}
Also used : KeyInfo(doitincloud.rdbcache.models.KeyInfo)

Aggregations

KeyInfo (doitincloud.rdbcache.models.KeyInfo)50 KvPair (doitincloud.rdbcache.models.KvPair)29 AnyKey (doitincloud.rdbcache.supports.AnyKey)21 Context (doitincloud.rdbcache.supports.Context)17 KvPairs (doitincloud.rdbcache.supports.KvPairs)17 Test (org.junit.Test)13 ServerErrorException (doitincloud.commons.exceptions.ServerErrorException)9 StopWatch (doitincloud.rdbcache.models.StopWatch)8 BadRequestException (doitincloud.commons.exceptions.BadRequestException)7 SQLException (java.sql.SQLException)4 QueryInfo (doitincloud.rdbcache.queries.QueryInfo)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 MockServletContext (org.springframework.mock.web.MockServletContext)2 NotFoundException (doitincloud.commons.exceptions.NotFoundException)1 CacheOps (doitincloud.rdbcache.services.CacheOps)1 DbaseOps (doitincloud.rdbcache.services.DbaseOps)1 ExpireDbOps (doitincloud.rdbcache.supports.ExpireDbOps)1