Search in sources :

Example 31 with KvPair

use of doitincloud.rdbcache.models.KvPair 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(doitincloud.rdbcache.supports.Context) KvPair(doitincloud.rdbcache.models.KvPair) KeyInfo(doitincloud.rdbcache.models.KeyInfo) Test(org.junit.Test)

Example 32 with KvPair

use of doitincloud.rdbcache.models.KvPair 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(doitincloud.rdbcache.supports.Context) AnyKey(doitincloud.rdbcache.supports.AnyKey) KeyInfo(doitincloud.rdbcache.models.KeyInfo) KvPair(doitincloud.rdbcache.models.KvPair) KvPairs(doitincloud.rdbcache.supports.KvPairs) Test(org.junit.Test)

Example 33 with KvPair

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

the class RdbcacheApis method delkey_post.

/**
 * delkey_post post multiple items
 *
 * To delete one or more keys from redis based on the input keys. No query string.
 * It returns immediately. It will not delete database entry.
 *
 * @param request HttpServletRequest
 * @param keys List, list of keys for returned entries
 * @param opt1 String, can be "sync" or "async" and table
 * @param opt2 String, can be "sync" or "async"  and table, but not the same as opt1
 * @return ResponseEntity
 */
@RequestMapping(value = { "/rdbcache/v1/delkey", "/rdbcache/v1/delkey/{opt1}", "/rdbcache/v1/delkey/{opt1}/{opt2}" }, method = RequestMethod.POST)
public ResponseEntity<?> delkey_post(HttpServletRequest request, @RequestBody List<String> keys, @PathVariable Optional<String> opt1, @PathVariable Optional<String> opt2) {
    if (request.getParameterMap().size() != 0) {
        throw new BadRequestException("query string is not supported");
    }
    if (keys.contains("*")) {
        throw new BadRequestException("no * allowed as key");
    }
    Context context = new Context();
    KvPairs pairs = new KvPairs(keys);
    AnyKey anyKey = Request.process(context, request, pairs, opt1, opt2);
    if (anyKey.size() != keys.size()) {
        context.logTraceMessage("one or more keys not found");
    }
    for (int i = 0; i < anyKey.size(); i++) {
        KvPair pair = pairs.get(i);
        KeyInfo keyInfo = anyKey.get(i);
        if (keyInfo.getIsNew()) {
            context.logTraceMessage("key not found for " + pair.getId());
        }
    }
    LOGGER.trace(anyKey.print() + " pairs(" + pairs.size() + "): " + pairs.printKey());
    AppCtx.getAsyncOps().doDeleteFromRedis(context, pairs, anyKey);
    return Response.send(context, pairs);
}
Also used : Context(doitincloud.rdbcache.supports.Context) AnyKey(doitincloud.rdbcache.supports.AnyKey) KvPair(doitincloud.rdbcache.models.KvPair) KeyInfo(doitincloud.rdbcache.models.KeyInfo) BadRequestException(doitincloud.commons.exceptions.BadRequestException) KvPairs(doitincloud.rdbcache.supports.KvPairs)

Example 34 with KvPair

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

the class RdbcacheApis method delall_post.

/**
 * delall_post post multple items
 *
 * To delete one or more keys from redis and database based on the input keys. No query string.
 * It returns immediately.
 *
 * @param request HttpServletRequest
 * @param keys List, list of keys for returned entries
 * @param opt1 String, can be "sync" and table
 * @param opt2 String, can be "sync" and table, but not the same as opt1
 * @return ResponseEntity
 */
@RequestMapping(value = { "/rdbcache/v1/delall", "/rdbcache/v1/delall/{opt1}", "/rdbcache/v1/delall/{opt1}/{opt2}" }, method = RequestMethod.POST)
public ResponseEntity<?> delall_post(HttpServletRequest request, @RequestBody List<String> keys, @PathVariable Optional<String> opt1, @PathVariable Optional<String> opt2) {
    if (request.getParameterMap().size() != 0) {
        throw new BadRequestException("query string is not supported");
    }
    if (keys.contains("*")) {
        throw new BadRequestException("no * allowed as key");
    }
    Context context = new Context();
    KvPairs pairs = new KvPairs(keys);
    AnyKey anyKey = Request.process(context, request, pairs, opt1, opt2);
    if (anyKey.size() != keys.size()) {
        context.logTraceMessage("one or more keys not found");
    }
    for (int i = 0; i < anyKey.size(); i++) {
        KvPair pair = pairs.get(i);
        KeyInfo keyInfo = anyKey.get(i);
        if (keyInfo.getIsNew()) {
            context.logTraceMessage("key not found for " + pair.getId());
        }
    }
    AppCtx.getAsyncOps().doDeleteFromRedisAndDbase(context, pairs, anyKey);
    return Response.send(context, pairs);
}
Also used : Context(doitincloud.rdbcache.supports.Context) AnyKey(doitincloud.rdbcache.supports.AnyKey) KvPair(doitincloud.rdbcache.models.KvPair) KeyInfo(doitincloud.rdbcache.models.KeyInfo) BadRequestException(doitincloud.commons.exceptions.BadRequestException) KvPairs(doitincloud.rdbcache.supports.KvPairs)

Example 35 with KvPair

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

the class RdbcacheApis method pull_post.

/**
 * pull_post post multiple items
 *
 * To pull one or more entries based on input keys. No * key. No query string.
 * Once data found, it returns immediately. It queries redis first, then database.
 *
 * @param request HttpServletRequest
 * @param opt1 String, can be expire or table or "sync" or "async"
 * @param opt2 String, can be expire or table or "sync" or "async", but not otp1
 * @param opt3 String, can be expire or table or "sync" or "async", but not otp1 and opt2
 * @return ResponseEntity
 */
@RequestMapping(value = { "/rdbcache/v1/pull", "/rdbcache/v1/pull/{opt1}", "/rdbcache/v1/pull/{opt1}/{opt2}", "/rdbcache/v1/pull/{opt1}/{opt2}/{opt3}" }, method = RequestMethod.POST)
public ResponseEntity<?> pull_post(HttpServletRequest request, @PathVariable Optional<String> opt1, @PathVariable Optional<String> opt2, @PathVariable Optional<String> opt3, @RequestBody ArrayList<String> keys) {
    if (request.getParameterMap().size() != 0) {
        throw new BadRequestException("query string is not supported");
    }
    if (keys == null || keys.size() == 0) {
        throw new BadRequestException("missing keys");
    }
    if (keys.contains("*")) {
        throw new BadRequestException("no * allowed as key");
    }
    if (request.getParameterMap().size() > 0) {
        throw new BadRequestException("query string is not supported");
    }
    Context context = new Context(true, true);
    KvPairs pairs = new KvPairs(keys);
    AnyKey anyKey = Request.process(context, request, pairs, opt1, opt2, opt3);
    if (anyKey.size() != pairs.size()) {
        throw new NotFoundException("one or more keys not found");
    }
    for (int i = 0; i < anyKey.size(); i++) {
        KvPair pair = pairs.get(i);
        KeyInfo keyInfo = anyKey.get(i);
        if (keyInfo.getIsNew()) {
            throw new NotFoundException("key not found for " + pair.getId());
        }
    }
    LOGGER.trace(anyKey.print() + " pairs(" + pairs.size() + "): " + pairs.printKey());
    if (!AppCtx.getRedisRepo().find(context, pairs, anyKey)) {
        KvPairs dbPairs = new KvPairs();
        for (int i = 0; i < pairs.size(); i++) {
            KvPair pair = pairs.get(i);
            if (!pair.hasContent()) {
                KeyInfo keyInfo = anyKey.get(i);
                KvPairs pairsNew = new KvPairs(pair);
                AnyKey anyKeyNew = new AnyKey(keyInfo);
                if (AppCtx.getDbaseRepo().find(context, pairsNew, anyKeyNew)) {
                    dbPairs.add(pair);
                }
            }
        }
        if (dbPairs.size() > 0) {
            AppCtx.getAsyncOps().doSaveToRedis(context, dbPairs, anyKey);
        }
    }
    return Response.send(context, pairs);
}
Also used : Context(doitincloud.rdbcache.supports.Context) AnyKey(doitincloud.rdbcache.supports.AnyKey) KvPair(doitincloud.rdbcache.models.KvPair) KeyInfo(doitincloud.rdbcache.models.KeyInfo) BadRequestException(doitincloud.commons.exceptions.BadRequestException) NotFoundException(doitincloud.commons.exceptions.NotFoundException) KvPairs(doitincloud.rdbcache.supports.KvPairs)

Aggregations

KvPair (doitincloud.rdbcache.models.KvPair)51 KeyInfo (doitincloud.rdbcache.models.KeyInfo)29 KvPairs (doitincloud.rdbcache.supports.KvPairs)22 Context (doitincloud.rdbcache.supports.Context)15 Test (org.junit.Test)15 AnyKey (doitincloud.rdbcache.supports.AnyKey)14 StopWatch (doitincloud.rdbcache.models.StopWatch)13 ServerErrorException (doitincloud.commons.exceptions.ServerErrorException)9 BadRequestException (doitincloud.commons.exceptions.BadRequestException)7 KvIdType (doitincloud.rdbcache.models.KvIdType)6 SQLException (java.sql.SQLException)4 WebMvcTest (org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)4 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)4 MvcResult (org.springframework.test.web.servlet.MvcResult)4 RequestBuilder (org.springframework.test.web.servlet.RequestBuilder)4 ResultActions (org.springframework.test.web.servlet.ResultActions)4 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)2 EmptyResultDataAccessException (org.springframework.dao.EmptyResultDataAccessException)2 NotFoundException (doitincloud.commons.exceptions.NotFoundException)1