Search in sources :

Example 26 with KeyInfo

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

the class UtilsTest method toPojoTest.

@Test
public void toPojoTest() {
    KeyInfo keyInfo1 = new KeyInfo();
    keyInfo1.setExpire("100");
    keyInfo1.setTable("table");
    Map<String, Object> map = Utils.toMap(keyInfo1);
    KeyInfo keyInfo2 = Utils.toPojo(map, KeyInfo.class);
    assertEquals(keyInfo1, keyInfo2);
}
Also used : KeyInfo(com.rdbcache.models.KeyInfo) Test(org.junit.Test)

Example 27 with KeyInfo

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

the class UtilsTest method toMapTestWithPojo.

@Test
public void toMapTestWithPojo() {
    KeyInfo keyInfo = new KeyInfo();
    keyInfo.setExpire("100");
    keyInfo.setTable("table");
    keyInfo.cleanup();
    Map<String, Object> map = Utils.toMap(keyInfo);
    // System.out.println(Utils.toJsonMap(map));
    assertNotNull(map.get("created_at"));
    map.remove("created_at");
    String json = "{\"expire\":\"100\",\"table\":\"table\",\"clause\":\"\",\"query_key\":\"\",\"is_new\":false}";
    Map<String, Object> newMap = Utils.toMap(json);
    assertEquals(map, newMap);
}
Also used : KeyInfo(com.rdbcache.models.KeyInfo) Test(org.junit.Test)

Example 28 with KeyInfo

use of com.rdbcache.models.KeyInfo 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 only
 * @return ResponseEntity
 */
@RequestMapping(value = { "/rdbcache/v1/delkey", "/rdbcache/v1/delkey/{opt1}" }, method = RequestMethod.POST)
public ResponseEntity<?> delkey_post(HttpServletRequest request, @RequestBody List<String> keys, @PathVariable Optional<String> opt1) {
    if (keys.contains("*")) {
        throw new BadRequestException("no * allowed as key");
    }
    if (opt1.isPresent() && !"sync".equals(opt1.get())) {
        throw new BadRequestException("only option allowed is sync");
    }
    Context context = new Context();
    KvPairs pairs = new KvPairs(keys);
    AnyKey anyKey = Request.process(context, request, pairs);
    if (anyKey.size() != keys.size()) {
        context.logTraceMessage("one or more keys not found");
    }
    if (opt1.isPresent() && !"sync".equals(opt1.get())) {
        throw new BadRequestException("only option allowed is sync");
    }
    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 : KvPair(com.rdbcache.models.KvPair) KeyInfo(com.rdbcache.models.KeyInfo) BadRequestException(com.rdbcache.exceptions.BadRequestException)

Example 29 with KeyInfo

use of com.rdbcache.models.KeyInfo 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 only
 * @return ResponseEntity
 */
@RequestMapping(value = { "/rdbcache/v1/delall", "/rdbcache/v1/delall/{opt1}" }, method = RequestMethod.POST)
public ResponseEntity<?> delall_post(HttpServletRequest request, @RequestBody List<String> keys, @PathVariable Optional<String> opt1) {
    if (keys.contains("*")) {
        throw new BadRequestException("no * allowed as key");
    }
    if (opt1.isPresent() && !"sync".equals(opt1.get())) {
        throw new BadRequestException("only option allowed is sync");
    }
    Context context = new Context();
    KvPairs pairs = new KvPairs(keys);
    AnyKey anyKey = Request.process(context, request, pairs);
    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 : KvPair(com.rdbcache.models.KvPair) KeyInfo(com.rdbcache.models.KeyInfo) BadRequestException(com.rdbcache.exceptions.BadRequestException)

Example 30 with KeyInfo

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

the class RdbcacheApis method select_post.

/**
 * select_post post multiple items
 *
 * To select one or more entries based on query string.
 * It queries database and return immediately, and asynchronously saves the data to redis
 *
 * @param request HttpServletRequest
 * @param opt1 String, can be expire or table
 * @param opt2 String, can be expire or table, but not otp1
 * @param keys List, list of keys for returned entries
 * @return ResponseEntity
 */
@RequestMapping(value = { "/rdbcache/v1/select", "/rdbcache/v1/select/{opt1}", "/rdbcache/v1/select/{opt1}/{opt2}" }, method = RequestMethod.POST)
public ResponseEntity<?> select_post(HttpServletRequest request, @PathVariable Optional<String> opt1, @PathVariable Optional<String> opt2, @RequestBody ArrayList<String> keys) {
    Context context = new Context(true, true);
    KvPairs pairs = new KvPairs(keys);
    AnyKey anyKey = Request.process(context, request, pairs, opt1, opt2);
    LOGGER.trace(anyKey.print() + " pairs(" + pairs.size() + "): " + pairs.printKey());
    KeyInfo keyInfo = anyKey.getKeyInfo();
    if (keyInfo.getQuery() == null && pairs.size() == 0) {
        QueryInfo query = new QueryInfo(keyInfo.getTable());
        query.setLimit(1024);
        keyInfo.setQuery(query);
        String msg = "no query string found, max rows limit is forced to 1024";
        LOGGER.info(msg);
        context.logTraceMessage(msg);
    }
    if (!AppCtx.getDbaseRepo().find(context, pairs, anyKey)) {
        LOGGER.debug("no record(s) found from database");
    } else {
        AppCtx.getAsyncOps().doSaveToRedis(context, pairs, anyKey);
    }
    return Response.send(context, pairs);
}
Also used : KeyInfo(com.rdbcache.models.KeyInfo) QueryInfo(com.rdbcache.queries.QueryInfo)

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