Search in sources :

Example 16 with KvPairs

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

the class RdbcacheApis method push_post.

/**
 * push_post post multiple items
 *
 * To update one or more entries based on input key and value map. No * key. No query string.
 * It returns immediately, and asynchronously updates 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 map Map, a map of key and value pairs
 * @return ResponseEntity
 */
@RequestMapping(value = { "/rdbcache/v1/push", "/rdbcache/v1/push/{opt1}", "/rdbcache/v1/push/{opt1}/{opt2}", "/rdbcache/v1/push/{opt1}/{opt2}/{opt3}" }, method = RequestMethod.POST)
public ResponseEntity<?> push_post(HttpServletRequest request, @PathVariable Optional<String> opt1, @PathVariable Optional<String> opt2, @PathVariable Optional<String> opt3, @RequestBody Map<String, Object> map) {
    if (request.getParameterMap().size() != 0) {
        throw new BadRequestException("query string is not supported");
    }
    if (map == null || map.size() == 0) {
        throw new BadRequestException("missing request body");
    }
    if (map.containsKey("*")) {
        throw new BadRequestException("no * allowed as key");
    }
    if (request.getParameterMap().size() > 0) {
        throw new BadRequestException("query string is not supported");
    }
    Context context = new Context(false, true);
    KvPairs pairs = new KvPairs(map);
    AnyKey anyKey = Request.process(context, request, pairs, opt1, opt2, opt3);
    if (anyKey.size() != map.size()) {
        throw new BadRequestException("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 BadRequestException("key not found for " + pair.getId());
        }
    }
    LOGGER.trace(anyKey.print() + " pairs(" + pairs.size() + "): " + pairs.printKey());
    AppCtx.getAsyncOps().doPushOperations(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 17 with KvPairs

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

the class RdbcacheApis method delall_get.

/**
 * delall_get get single item
 *
 * To delete a key from redis and database based on the input key. No query string.
 * It returns immediately.
 *
 * @param request HttpServletRequest
 * @param key String, hash key
 * @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/delall/{key}", "/rdbcache/v1/delall/{key}/{opt1}", "/rdbcache/v1/delall/{key}/{opt1}/{opt2}" }, method = RequestMethod.GET)
public ResponseEntity<?> delall_get(HttpServletRequest request, @PathVariable("key") String key, @PathVariable Optional<String> opt1, @PathVariable Optional<String> opt2) {
    if (request.getParameterMap().size() != 0) {
        throw new BadRequestException("query string is not supported");
    }
    if (key.equals("*")) {
        throw new BadRequestException("no * allowed as key");
    }
    Context context = new Context();
    KvPairs pairs = new KvPairs(key);
    AnyKey anyKey = Request.process(context, request, pairs, opt1, opt2);
    if (anyKey.getKeyInfo().getIsNew()) {
        throw new NotFoundException("key not found for " + key);
    }
    LOGGER.trace(anyKey.print() + " pairs(" + pairs.size() + "): " + pairs.printKey());
    AppCtx.getAsyncOps().doDeleteFromRedisAndDbase(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) NotFoundException(doitincloud.commons.exceptions.NotFoundException) KvPairs(doitincloud.rdbcache.supports.KvPairs)

Example 18 with KvPairs

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

the class RdbcacheApis method getset_get.

/**
 * getset_get get single item
 *
 * To get current value of a key and update it to a new value based on the key and/or query string.
 * It finds the current value and returns immediately, and asynchronously updates 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/getset/{key}/{value}", "/rdbcache/v1/getset/{key}/{value}/{opt1}", "/rdbcache/v1/getset/{key}/{value}/{opt1}/{opt2}", "/rdbcache/v1/getset/{key}/{value}/{opt1}/{opt2}/{opt3}" }, method = RequestMethod.GET)
public ResponseEntity<?> getset_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(true);
    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());
    KvPairs pairsClone = pairs.clone();
    KvPair pair = pairs.getPair();
    if (key.equals("*")) {
        if (!AppCtx.getDbaseRepo().find(context, pairs, anyKey)) {
            pair.clearData();
        }
        AppCtx.getAsyncOps().doSaveToRedis(context, pairsClone, anyKey);
    } else if (AppCtx.getRedisRepo().findAndSave(context, pairs, anyKey)) {
        AppCtx.getAsyncOps().doSaveToDbase(context, pairsClone, anyKey);
    } else {
        if (!AppCtx.getDbaseRepo().find(context, pairs, anyKey)) {
            pair.clearData();
        }
        AppCtx.getAsyncOps().doSaveToDbase(context, pairsClone, anyKey);
    }
    return Response.send(context, pairs);
}
Also used : Context(doitincloud.rdbcache.supports.Context) AnyKey(doitincloud.rdbcache.supports.AnyKey) KvPair(doitincloud.rdbcache.models.KvPair) KvPairs(doitincloud.rdbcache.supports.KvPairs)

Example 19 with KvPairs

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

the class RdbcacheApis method delkey_get.

/**
 * delkey_get get/delete single item
 *
 * To delete a key from redis based on the input key. No query string.
 * It returns immediately. It will not delete database entry.
 *
 * @param request HttpServletRequest
 * @param key String, hash key
 * @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/{key}", "/rdbcache/v1/delkey/{key}/{opt1}", "/rdbcache/v1/delkey/{key}/{opt1}/{opt2}" }, method = { RequestMethod.GET, RequestMethod.DELETE })
public ResponseEntity<?> delkey_get(HttpServletRequest request, @PathVariable("key") String key, @PathVariable Optional<String> opt1, @PathVariable Optional<String> opt2) {
    if (request.getParameterMap().size() != 0) {
        throw new BadRequestException("query string is not supported");
    }
    if (key.equals("*")) {
        throw new BadRequestException("no * allowed as key");
    }
    Context context = new Context();
    KvPairs pairs = new KvPairs(key);
    AnyKey anyKey = Request.process(context, request, pairs, opt1, opt2);
    if (anyKey.getKeyInfo().getIsNew()) {
        throw new NotFoundException("key not found for " + key);
    }
    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) BadRequestException(doitincloud.commons.exceptions.BadRequestException) NotFoundException(doitincloud.commons.exceptions.NotFoundException) KvPairs(doitincloud.rdbcache.supports.KvPairs)

Example 20 with KvPairs

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

the class RdbcacheApis method trace_get.

/**
 * trace_get get single item
 *
 * get error messages by trace id
 *
 * @param request HttpServletRequest
 * @param traceId the trace id return by API call
 * @return ResponseEntity
 */
@RequestMapping(value = { "/rdbcache/v1/trace/{traceId}" }, method = RequestMethod.GET)
public ResponseEntity<?> trace_get(HttpServletRequest request, @PathVariable("traceId") String traceId) {
    if (request.getParameterMap().size() != 0) {
        throw new BadRequestException("query string is not supported");
    }
    if (traceId.equals("*")) {
        throw new BadRequestException("no * allowed as trace id");
    }
    if (request.getParameterMap().size() != 0) {
        throw new BadRequestException("no query string is allowed");
    }
    Context context = new Context(true);
    KvPairs pairs = new KvPairs();
    Request.process(context, request, pairs);
    LOGGER.trace("pairs(" + pairs.size() + "): " + pairs.printKey());
    KvPair pair = AppCtx.getKvPairRepo().findById(new KvIdType(traceId, "trace"));
    if (pair != null) {
        pairs.add(pair);
    }
    return Response.send(context, pairs);
}
Also used : Context(doitincloud.rdbcache.supports.Context) KvIdType(doitincloud.rdbcache.models.KvIdType) KvPair(doitincloud.rdbcache.models.KvPair) BadRequestException(doitincloud.commons.exceptions.BadRequestException) KvPairs(doitincloud.rdbcache.supports.KvPairs)

Aggregations

KvPairs (doitincloud.rdbcache.supports.KvPairs)44 AnyKey (doitincloud.rdbcache.supports.AnyKey)32 Context (doitincloud.rdbcache.supports.Context)25 KvPair (doitincloud.rdbcache.models.KvPair)22 KeyInfo (doitincloud.rdbcache.models.KeyInfo)17 BadRequestException (doitincloud.commons.exceptions.BadRequestException)14 Test (org.junit.Test)13 NotFoundException (doitincloud.commons.exceptions.NotFoundException)4 ServerErrorException (doitincloud.commons.exceptions.ServerErrorException)4 StopWatch (doitincloud.rdbcache.models.StopWatch)3 KvIdType (doitincloud.rdbcache.models.KvIdType)2 QueryInfo (doitincloud.rdbcache.queries.QueryInfo)2 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)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 ExpireDbOps (doitincloud.rdbcache.supports.ExpireDbOps)1 Connection (java.sql.Connection)1