Search in sources :

Example 1 with KeyInfo

use of com.rdbcache.models.KeyInfo 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 KeyInfo

use of com.rdbcache.models.KeyInfo 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 KeyInfo

use of com.rdbcache.models.KeyInfo 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 KeyInfo

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

the class RedisConfig method keyInfoRedisTemplate.

@Bean
public KeyInfoRedisTemplate keyInfoRedisTemplate() {
    KeyInfoRedisTemplate template = new KeyInfoRedisTemplate();
    template.setConnectionFactory(redisConnectionFactory());
    template.setKeySerializer(new StringRedisSerializer());
    template.setHashKeySerializer(new StringRedisSerializer());
    template.setHashValueSerializer(new Jackson2JsonRedisSerializer<KeyInfo>(KeyInfo.class));
    return template;
}
Also used : StringRedisSerializer(org.springframework.data.redis.serializer.StringRedisSerializer) KeyInfo(com.rdbcache.models.KeyInfo) Bean(org.springframework.context.annotation.Bean)

Example 5 with KeyInfo

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

the class Query method executeUpdate.

public boolean executeUpdate() {
    params = new ArrayList<>();
    boolean allOk = true;
    for (int i = 0; i < pairs.size(); i++) {
        KvPair pair = pairs.get(i);
        KeyInfo keyInfo = anyKey.getAny(i);
        String table = keyInfo.getTable();
        if (!keyInfo.getIsNew() && !keyInfo.hasParams() && keyInfo.ifJustCreated()) {
            waitForParamsUpdate(pair.getId(), keyInfo);
        }
        // 
        if (!Parser.prepareStandardClauseParams(context, pair, keyInfo)) {
            allOk = false;
            String msg = "executeUpdate failed when calling prepareStandardClauseParams for " + pair.getId();
            LOGGER.error(msg);
            context.logTraceMessage(msg);
            if (context.isSync()) {
                throw new ServerErrorException(context, msg);
            }
            continue;
        }
        Map<String, Object> map = pair.getData();
        params.clear();
        String updates = "";
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            params.add(entry.getValue());
            if (updates.length() != 0)
                updates += ", ";
            updates += entry.getKey() + " = ?";
        }
        params.addAll(keyInfo.getParams());
        String clause = keyInfo.getClause();
        sql = "update " + table + " set " + updates + " where " + clause + " limit 1";
        LOGGER.trace("sql: " + sql);
        LOGGER.trace("params: " + params.toString());
        StopWatch stopWatch = context.startStopWatch("dbase", "jdbcTemplate.update");
        try {
            if (jdbcTemplate.update(sql, params.toArray()) > 0) {
                if (stopWatch != null)
                    stopWatch.stopNow();
                LOGGER.trace("update " + pair.getId() + " from " + table);
                continue;
            } else {
                if (stopWatch != null)
                    stopWatch.stopNow();
                allOk = false;
            }
        } catch (Exception e) {
            if (stopWatch != null)
                stopWatch.stopNow();
            allOk = false;
            String msg = e.getCause().getMessage();
            LOGGER.error(msg);
            context.logTraceMessage(msg);
            e.printStackTrace();
            if (context.isSync()) {
                throw new ServerErrorException(context, msg);
            }
        }
        keyInfo.setQueryKey(null);
    }
    return allOk;
}
Also used : KvPair(com.rdbcache.models.KvPair) ServerErrorException(com.rdbcache.exceptions.ServerErrorException) SQLException(java.sql.SQLException) StopWatch(com.rdbcache.models.StopWatch) KeyInfo(com.rdbcache.models.KeyInfo) ServerErrorException(com.rdbcache.exceptions.ServerErrorException)

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