Search in sources :

Example 41 with KeyInfo

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

the class QueryTest method deleteTest.

@Test
public void deleteTest() {
    try {
        Context context = new Context();
        KvPairs pairs = new KvPairs();
        AnyKey anyKey = new AnyKey();
        KeyInfo keyInfo = new KeyInfo();
        keyInfo.setExpire("100");
        keyInfo.setTable("user_table");
        String json1 = "{\"table\":\"user_table\",\"conditions\":{\"id\":{\"=\":[\"3\"]}}}";
        QueryInfo queryInfo1 = Utils.toPojo(Utils.toMap(json1), QueryInfo.class);
        keyInfo.setQuery(queryInfo1);
        anyKey.setKeyInfo(keyInfo);
        Query query = new Query(context, jdbcTemplate, pairs, anyKey);
        assertTrue(query.ifSelectOk());
        assertTrue(query.executeSelect());
        // System.out.println(Utils.toJsonMap(pairs));
        assertEquals(1, pairs.size());
        String key = pairs.getPair().getId();
        KvPair pair = new KvPair(key);
        pairs.setPair(pair);
        KeyInfo keyInfo2 = new KeyInfo();
        keyInfo2.setExpire("100");
        keyInfo2.setTable("user_table");
        keyInfo2.setClause("id = ?");
        keyInfo2.setParams(Arrays.asList("3"));
        anyKey.setKeyInfo(keyInfo2);
        query = new Query(context, jdbcTemplate, pairs, anyKey);
        assertTrue(query.ifDeleteOk());
        assertTrue(query.executeDelete());
        pairs.clear();
        query = new Query(context, jdbcTemplate, pairs, anyKey);
        assertTrue(query.ifSelectOk());
        assertFalse(query.executeSelect());
        assertEquals(0, pairs.size());
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getCause().getMessage());
    }
}
Also used : Context(com.rdbcache.helpers.Context) AnyKey(com.rdbcache.helpers.AnyKey) KeyInfo(com.rdbcache.models.KeyInfo) KvPair(com.rdbcache.models.KvPair) KvPairs(com.rdbcache.helpers.KvPairs) Test(org.junit.Test)

Example 42 with KeyInfo

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

the class SimpleKeyInfoRepo method save.

@Override
public boolean save(Context context, KvPairs pairs, AnyKey anyKey) {
    Assert.isTrue(anyKey.size() == pairs.size(), anyKey.size() + " != " + pairs.size() + ", only supports that pairs and anyKey have the same size");
    for (int i = 0; i < pairs.size(); i++) {
        KvPair pair = pairs.get(i);
        KeyInfo keyInfo = anyKey.getAny(i);
        String key = pair.getId();
        Map<String, Object> map = Utils.toMap(keyInfo);
        data.put(key, map);
        LOGGER.trace("save: " + key);
    }
    return true;
}
Also used : KvPair(com.rdbcache.models.KvPair) KeyInfo(com.rdbcache.models.KeyInfo)

Example 43 with KeyInfo

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

Example 44 with KeyInfo

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

the class Request method process.

public static AnyKey process(Context context, HttpServletRequest request, KvPairs pairs, Optional<String> opt1, Optional<String> opt2) {
    LOGGER.info("URI: " + request.getRequestURI());
    if (PropCfg.getEnableMonitor())
        context.enableMonitor(request);
    AnyKey anyKey = new AnyKey();
    if (pairs == null) {
        return anyKey;
    }
    // find the keyinfo for the first key
    if (pairs.size() > 0) {
        AppCtx.getKeyInfoRepo().find(context, new KvPairs(pairs.getPair()), anyKey);
    }
    KeyInfo keyInfo = anyKey.getAny();
    Map<String, String[]> params = request.getParameterMap();
    if ((params != null && params.size() > 0) || opt1 != null || opt2 != null) {
        processOptions(context, keyInfo, params, opt1, opt2);
    }
    if (pairs.size() == 0 || context.getAction().startsWith("select_")) {
        return anyKey;
    }
    // find keyinfo for the second key and after
    if (pairs.size() > 1) {
        AppCtx.getKeyInfoRepo().find(context, pairs, anyKey);
    }
    for (int i = 0; i < pairs.size() && i < anyKey.size(); i++) {
        keyInfo = anyKey.get(i);
        if (keyInfo.getIsNew()) {
            keyInfo.setIsNew(false);
            String key = pairs.get(i).getId();
            AppCtx.getLocalCache().putKeyInfo(key, keyInfo);
            keyInfo.setIsNew(true);
        }
    }
    if (anyKey.size() != 1 && pairs.size() != anyKey.size()) {
        throw new ServerErrorException(context, "case not supported, anyKey size(" + anyKey.size() + ") != 1 && pairs size(" + pairs.size() + ") != anyKey size(" + anyKey.size() + ")");
    }
    return anyKey;
}
Also used : KeyInfo(com.rdbcache.models.KeyInfo) ServerErrorException(com.rdbcache.exceptions.ServerErrorException)

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