Search in sources :

Example 36 with KvPairs

use of doitincloud.rdbcache.supports.KvPairs 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)

Example 37 with KvPairs

use of doitincloud.rdbcache.supports.KvPairs 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)

Example 38 with KvPairs

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

the class RdbcacheApis method getset_post.

/**
 * getset_post post single item
 *
 * To get current value of a key and update it to a new value based on 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 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}", "/rdbcache/v1/getset/{key}/{opt1}", "/rdbcache/v1/getset/{key}/{opt1}/{opt2}", "/rdbcache/v1/getset/{key}/{opt1}/{opt2}/{opt3}" }, method = RequestMethod.POST)
public ResponseEntity<?> getset_post(HttpServletRequest request, @PathVariable("key") String key, @PathVariable Optional<String> opt1, @PathVariable Optional<String> opt2, @PathVariable Optional<String> opt3, @RequestBody String value) {
    if (value == null || value.length() == 0) {
        throw new BadRequestException("missing request body");
    }
    Context context = new Context(true);
    KvPairs pairs = new KvPairs(key, value);
    AnyKey anyKey = Request.process(context, request, pairs, opt1, opt2);
    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().doSaveToRedisAndDbase(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) BadRequestException(doitincloud.commons.exceptions.BadRequestException) KvPairs(doitincloud.rdbcache.supports.KvPairs)

Example 39 with KvPairs

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

the class RdbcacheApis method get_get.

/**
 * get_get get single item
 *
 * To get data based on key and/or query string.
 * Once data found, it returns immediately. It queries redis first, then database.
 *
 * @param request HttpServletRequest
 * @param key String, hash key
 * @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/get/{key}", "/rdbcache/v1/get/{key}/{opt1}", "/rdbcache/v1/get/{key}/{opt1}/{opt2}", "/rdbcache/v1/get/{key}/{opt1}/{opt2}/{opt3}" }, method = RequestMethod.GET)
public ResponseEntity<?> get_get(HttpServletRequest request, @PathVariable("key") String key, @PathVariable Optional<String> opt1, @PathVariable Optional<String> opt2, @PathVariable Optional<String> opt3) {
    Context context = new Context(true);
    KvPairs pairs = new KvPairs(key);
    AnyKey anyKey = Request.process(context, request, pairs, opt1, opt2, opt3);
    LOGGER.trace(anyKey.print() + " pairs(" + pairs.size() + "): " + pairs.printKey());
    if (key.equals("*")) {
        if (AppCtx.getDbaseRepo().find(context, pairs, anyKey)) {
            AppCtx.getAsyncOps().doSaveToRedis(context, pairs, anyKey);
        } else {
            throw new NotFoundException(context, "data not found");
        }
    } else {
        if (!AppCtx.getRedisRepo().find(context, pairs, anyKey)) {
            if (AppCtx.getDbaseRepo().find(context, pairs, anyKey)) {
                AppCtx.getAsyncOps().doSaveToRedis(context, pairs, anyKey);
            } else {
                throw new NotFoundException(context, "data not found");
            }
        }
    }
    return Response.send(context, pairs);
}
Also used : Context(doitincloud.rdbcache.supports.Context) AnyKey(doitincloud.rdbcache.supports.AnyKey) NotFoundException(doitincloud.commons.exceptions.NotFoundException) KvPairs(doitincloud.rdbcache.supports.KvPairs)

Example 40 with KvPairs

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

the class RequestTest method process1.

@Test
public void process1() {
    try {
        String key = "*";
        Optional<String> tableOpt = Optional.of("tb1");
        Optional<String> expireOpt = Optional.of("30");
        String queryString = "id=1";
        HttpServletRequest request = getRequest("get", key, null, tableOpt, expireOpt, queryString);
        Context context = new Context();
        AnyKey anyKey = Request.process(context, request, new KvPairs(key), tableOpt, expireOpt);
        assertEquals(1, anyKey.size());
        KeyInfo keyInfo = anyKey.getKeyInfo();
        assertTrue(keyInfo.getIsNew());
        assertEquals("KeyInfo(true, tb1, 30, {id: {=: [1]}})", keyInfo.toString());
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getCause().getMessage());
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Context(doitincloud.rdbcache.supports.Context) MockServletContext(org.springframework.mock.web.MockServletContext) AnyKey(doitincloud.rdbcache.supports.AnyKey) KeyInfo(doitincloud.rdbcache.models.KeyInfo) KvPairs(doitincloud.rdbcache.supports.KvPairs) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

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