Search in sources :

Example 1 with BadRequestException

use of com.rdbcache.exceptions.BadRequestException 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
 * @param opt2 String, can be expire or table, but not otp1
 * @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}" }, method = RequestMethod.POST)
public ResponseEntity<?> push_post(HttpServletRequest request, @PathVariable Optional<String> opt1, @PathVariable Optional<String> opt2, @RequestBody Map<String, Object> map) {
    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);
    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 : KvPair(com.rdbcache.models.KvPair) KeyInfo(com.rdbcache.models.KeyInfo) BadRequestException(com.rdbcache.exceptions.BadRequestException)

Example 2 with BadRequestException

use of com.rdbcache.exceptions.BadRequestException in project rdbcache by rdbcache.

the class RdbcacheApis method put_post.

/**
 * put_post post/put single item
 *
 * To update a key with partial data based on the key and/or query string.
 * It returns immediately, and asynchronously updates to redis and database
 *
 * @param request HttpServletRequest
 * @param key String, hash key
 * @param opt1 String, can be expire or table
 * @param opt2 String, can be expire or table, but not otp1
 * @return ResponseEntity
 */
@RequestMapping(value = { "/rdbcache/v1/put/{key}", "/rdbcache/v1/put/{key}/{opt1}", "/rdbcache/v1/put/{key}/{opt1}/{opt2}" }, method = { RequestMethod.POST, RequestMethod.PUT })
public ResponseEntity<?> put_post(HttpServletRequest request, @PathVariable("key") String key, @PathVariable Optional<String> opt1, @PathVariable Optional<String> opt2, @RequestBody String value) {
    if (value == null || value.length() == 0) {
        throw new BadRequestException("missing request body");
    }
    Context context = new Context();
    KvPairs pairs = new KvPairs(key, value);
    AnyKey anyKey = Request.process(context, request, pairs, opt1, opt2);
    LOGGER.trace(anyKey.print() + " pairs(" + pairs.size() + "): " + pairs.printKey());
    KeyInfo keyInfo = anyKey.getKeyInfo();
    if (key.equals("*") && keyInfo.getQuery() == null) {
        AppCtx.getAsyncOps().doSaveToRedisAndDbase(context, pairs, anyKey);
    } else {
        AppCtx.getAsyncOps().doPutOperation(context, pairs, anyKey);
    }
    return Response.send(context, pairs);
}
Also used : KeyInfo(com.rdbcache.models.KeyInfo) BadRequestException(com.rdbcache.exceptions.BadRequestException)

Example 3 with BadRequestException

use of com.rdbcache.exceptions.BadRequestException 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
 * @param opt2 String, can be expire or table, but not otp1
 * @return ResponseEntity
 */
@RequestMapping(value = { "/rdbcache/v1/pull", "/rdbcache/v1/pull/{opt1}", "/rdbcache/v1/pull/{opt1}/{opt2}" }, method = RequestMethod.POST)
public ResponseEntity<?> pull_post(HttpServletRequest request, @PathVariable Optional<String> opt1, @PathVariable Optional<String> opt2, @RequestBody ArrayList<String> keys) {
    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);
    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 : KvPair(com.rdbcache.models.KvPair) KeyInfo(com.rdbcache.models.KeyInfo) BadRequestException(com.rdbcache.exceptions.BadRequestException) NotFoundException(com.rdbcache.exceptions.NotFoundException)

Example 4 with BadRequestException

use of com.rdbcache.exceptions.BadRequestException 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 (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 pairKey = new KvPair(traceId, "trace");
    Optional<KvPair> opt = AppCtx.getKvPairRepo().findById(new KvIdType(traceId, "trace"));
    if (opt.isPresent()) {
        pairs.add(opt.get());
    }
    return Response.send(context, pairs);
}
Also used : KvIdType(com.rdbcache.models.KvIdType) KvPair(com.rdbcache.models.KvPair) BadRequestException(com.rdbcache.exceptions.BadRequestException)

Example 5 with BadRequestException

use of com.rdbcache.exceptions.BadRequestException in project rdbcache by rdbcache.

the class Request method processOptions.

private static QueryInfo processOptions(Context context, KeyInfo keyInfo, Map<String, String[]> params, Optional<String> opt1, Optional<String> opt2) {
    // {expire, table}
    String[] opts = { null, null };
    if (opt1 != null && opt1.isPresent()) {
        assignOption(context, opt1.get(), opts);
    }
    if (opt2 != null && opt2.isPresent()) {
        assignOption(context, opt2.get(), opts);
    }
    QueryInfo queryInfo = null;
    if (keyInfo.getIsNew()) {
        if (opts[1] != null) {
            keyInfo.setTable(opts[1]);
        }
        if (opts[0] != null) {
            keyInfo.setExpire(opts[0]);
        }
        if (params != null && params.size() > 0) {
            queryInfo = new QueryInfo(keyInfo.getTable(), params);
            keyInfo.setQuery(queryInfo);
        }
    } else {
        if (opts[0] != null && !opts[0].equals(keyInfo.getExpire())) {
            keyInfo.setExpire(opts[0]);
            keyInfo.setIsNew(true);
        }
        if (opts[1] != null && !opts[1].equals(keyInfo.getTable())) {
            throw new BadRequestException(context, "can not change table name for an existing key");
        }
        if (params != null && params.size() > 0) {
            queryInfo = new QueryInfo(keyInfo.getTable(), params);
            if (keyInfo.getQueryKey() == null || !keyInfo.getQueryKey().equals(queryInfo.getKey())) {
                throw new BadRequestException(context, "can not modify condition for an existing key");
            }
        }
    }
    return queryInfo;
}
Also used : BadRequestException(com.rdbcache.exceptions.BadRequestException) QueryInfo(com.rdbcache.queries.QueryInfo)

Aggregations

BadRequestException (com.rdbcache.exceptions.BadRequestException)8 KvPair (com.rdbcache.models.KvPair)6 KeyInfo (com.rdbcache.models.KeyInfo)5 KvIdType (com.rdbcache.models.KvIdType)2 NotFoundException (com.rdbcache.exceptions.NotFoundException)1 QueryInfo (com.rdbcache.queries.QueryInfo)1