Search in sources :

Example 26 with AnyKey

use of doitincloud.rdbcache.supports.AnyKey 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 27 with AnyKey

use of doitincloud.rdbcache.supports.AnyKey in project rdbcache by rdbcache.

the class RdbcacheApis method select_get.

/**
 * select_get get 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 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/select", "/rdbcache/v1/select/{opt1}", "/rdbcache/v1/select/{opt1}/{opt2}", "/rdbcache/v1/select/{opt1}/{opt2}/{opt3}" }, method = RequestMethod.GET)
public ResponseEntity<?> select_get(HttpServletRequest request, @PathVariable Optional<String> opt1, @PathVariable Optional<String> opt2, @PathVariable Optional<String> opt3) {
    if (request.getParameterMap().size() == 0) {
        throw new BadRequestException("query string is needed, try add ?limit=256 to url");
    }
    Context context = new Context(true, true);
    KvPairs pairs = new KvPairs();
    AnyKey anyKey = Request.process(context, request, pairs, opt1, opt2, opt3);
    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 : Context(doitincloud.rdbcache.supports.Context) AnyKey(doitincloud.rdbcache.supports.AnyKey) KeyInfo(doitincloud.rdbcache.models.KeyInfo) BadRequestException(doitincloud.commons.exceptions.BadRequestException) KvPairs(doitincloud.rdbcache.supports.KvPairs) QueryInfo(doitincloud.rdbcache.queries.QueryInfo)

Example 28 with AnyKey

use of doitincloud.rdbcache.supports.AnyKey in project rdbcache by rdbcache.

the class RdbcacheApis method save_post.

/**
 * save_post post multiple items
 *
 * To save one or more entries based on input list.
 * It returns immediately, and asynchronously inserts into redis and 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
 * @param list List, a list of map, than contains key and other fields
 * @return ResponseEntity
 */
@RequestMapping(value = { "/rdbcache/v1/save", "/rdbcache/v1/save/{opt1}", "/rdbcache/v1/save/{opt1}/{opt2}", "/rdbcache/v1/save/{opt1}/{opt2}/{opt3}" }, method = RequestMethod.POST)
public ResponseEntity<?> save_post(HttpServletRequest request, @PathVariable Optional<String> opt1, @PathVariable Optional<String> opt2, @PathVariable Optional<String> opt3, @RequestBody List<Map<String, Object>> list) {
    if (request.getParameterMap().size() != 0) {
        throw new BadRequestException("query string is not supported");
    }
    if (list == null || list.size() == 0) {
        throw new BadRequestException("missing request body");
    }
    Context context = new Context(false, true);
    KvPairs pairs = new KvPairs(list);
    AnyKey anyKey = Request.process(context, request, pairs, opt1, opt2);
    LOGGER.trace(anyKey.print() + " pairs(" + pairs.size() + "): " + pairs.printKey());
    AppCtx.getAsyncOps().doSaveAllToRedisAndSaveAllTodDbase(context, pairs, anyKey);
    return Response.send(context, pairs);
}
Also used : Context(doitincloud.rdbcache.supports.Context) AnyKey(doitincloud.rdbcache.supports.AnyKey) BadRequestException(doitincloud.commons.exceptions.BadRequestException) KvPairs(doitincloud.rdbcache.supports.KvPairs)

Example 29 with AnyKey

use of doitincloud.rdbcache.supports.AnyKey in project rdbcache by rdbcache.

the class RdbcacheApis method set_get.

/**
 * set_get get single item
 *
 * To set a value to a key based on the key and/or query string.
 * It returns immediately, and asynchronously saves to redis and database
 *
 * @param request HttpServletRequest
 * @param key String, hash key
 * @param value String, value
 * @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/set/{key}/{value}", "/rdbcache/v1/set/{key}/{value}/{opt1}", "/rdbcache/v1/set/{key}/{value}/{opt1}/{opt2}", "/rdbcache/v1/set/{key}/{value}/{opt1}/{opt2}/{opt3}" }, method = RequestMethod.GET)
public ResponseEntity<?> set_get(HttpServletRequest request, @PathVariable("key") String key, @PathVariable("value") String value, @PathVariable Optional<String> opt1, @PathVariable Optional<String> opt2, @PathVariable Optional<String> opt3) {
    Context context = new Context();
    KvPairs pairs = new KvPairs(key, value);
    AnyKey anyKey = Request.process(context, request, pairs, opt1, opt2, opt3);
    LOGGER.trace(anyKey.print() + " pairs(" + pairs.size() + "): " + pairs.printKey());
    AppCtx.getAsyncOps().doSaveToRedisAndDbase(context, pairs, anyKey);
    return Response.send(context, pairs);
}
Also used : Context(doitincloud.rdbcache.supports.Context) AnyKey(doitincloud.rdbcache.supports.AnyKey) KvPairs(doitincloud.rdbcache.supports.KvPairs)

Example 30 with AnyKey

use of doitincloud.rdbcache.supports.AnyKey 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 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
 * @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}", "/rdbcache/v1/select/{opt1}/{opt2}/{opt3}" }, method = RequestMethod.POST)
public ResponseEntity<?> select_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 needed");
    }
    Context context = new Context(true, true);
    KvPairs pairs = new KvPairs(keys);
    AnyKey anyKey = Request.process(context, request, pairs, opt1, opt2, opt3);
    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 : Context(doitincloud.rdbcache.supports.Context) AnyKey(doitincloud.rdbcache.supports.AnyKey) KeyInfo(doitincloud.rdbcache.models.KeyInfo) BadRequestException(doitincloud.commons.exceptions.BadRequestException) KvPairs(doitincloud.rdbcache.supports.KvPairs) QueryInfo(doitincloud.rdbcache.queries.QueryInfo)

Aggregations

AnyKey (doitincloud.rdbcache.supports.AnyKey)36 KvPairs (doitincloud.rdbcache.supports.KvPairs)32 Context (doitincloud.rdbcache.supports.Context)23 KeyInfo (doitincloud.rdbcache.models.KeyInfo)21 KvPair (doitincloud.rdbcache.models.KvPair)14 BadRequestException (doitincloud.commons.exceptions.BadRequestException)12 Test (org.junit.Test)9 NotFoundException (doitincloud.commons.exceptions.NotFoundException)4 ServerErrorException (doitincloud.commons.exceptions.ServerErrorException)4 StopWatch (doitincloud.rdbcache.models.StopWatch)2 QueryInfo (doitincloud.rdbcache.queries.QueryInfo)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 MockServletContext (org.springframework.mock.web.MockServletContext)2 Query (doitincloud.rdbcache.queries.Query)1 CacheOps (doitincloud.rdbcache.services.CacheOps)1 ExpireDbOps (doitincloud.rdbcache.supports.ExpireDbOps)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 SQLException (java.sql.SQLException)1