Search in sources :

Example 1 with ServerErrorException

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

the class RTQueryApis method queryCache.

/**
 * query cache
 *
 * query local cache
 *
 * @param request HttpServletRequest
 * @param action config, table, key and data
 * @return ResponseEntity
 */
@RequestMapping(value = { "/rtquery/v1/cache/{action}" }, method = RequestMethod.GET)
public ResponseEntity<?> queryCache(HttpServletRequest request, @PathVariable String action) {
    Context context = new Context();
    Request.process(context, request);
    Map<String, Object> data = null;
    try {
        if (action.equals("config")) {
            data = new LinkedHashMap<>();
            data.put("keyMinCacheTTL", PropCfg.getKeyMinCacheTTL());
            data.put("dataMaxCacheTLL", PropCfg.getDataMaxCacheTLL());
            data.put("tableInfoCacheTTL", PropCfg.getTableInfoCacheTTL());
        } else if (action.equals("table")) {
            data = AppCtx.getLocalCache().listAllTables();
        } else if (action.equals("key")) {
            data = AppCtx.getLocalCache().listAllKeyInfos();
        } else if (action.equals("data")) {
            data = AppCtx.getLocalCache().listAllData();
        }
    } catch (Exception e) {
        String msg = e.getCause().getMessage();
        throw new ServerErrorException(msg);
    }
    return Response.send(context, data);
}
Also used : ServerErrorException(com.rdbcache.exceptions.ServerErrorException) ServerErrorException(com.rdbcache.exceptions.ServerErrorException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with ServerErrorException

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

the class RedisProperties method setUrl.

public void setUrl(String url) {
    try {
        if (!url.substring(0, 5).equalsIgnoreCase("redis")) {
            throw new ServerErrorException("redis protocol is expected");
        }
        String httpUrl = "http" + url.substring(5);
        URL urlObj = new URL(httpUrl);
        host = urlObj.getHost();
        port = urlObj.getPort();
    } catch (Exception e) {
        e.printStackTrace();
        throw new ServerErrorException(e.getCause().getMessage());
    }
}
Also used : ServerErrorException(com.rdbcache.exceptions.ServerErrorException) URL(java.net.URL) ServerErrorException(com.rdbcache.exceptions.ServerErrorException)

Example 3 with ServerErrorException

use of com.rdbcache.exceptions.ServerErrorException 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)

Example 4 with ServerErrorException

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

the class RedisRepoImpl method find.

@Override
public boolean find(Context context, KvPairs pairs, AnyKey anyKey) {
    LOGGER.trace("find pairs(" + pairs.size() + "): " + pairs.printKey() + "anyKey(" + anyKey.size() + "): " + anyKey.printTable());
    boolean foundAll = true;
    for (int i = 0; i < pairs.size(); i++) {
        KvPair pair = pairs.get(i);
        String key = pair.getId();
        KeyInfo keyInfo = anyKey.getAny(i);
        String hashKey = hdataPrefix + "::" + key;
        Map<String, Object> map = null;
        if (enableDataCache) {
            map = (Map<String, Object>) AppCtx.getLocalCache().getData(key);
            if (map != null && map.size() > 0) {
                LOGGER.debug("find - found from cache " + key);
            }
        }
        if (map == null) {
            StopWatch stopWatch = context.startStopWatch("redis", "hashOps.entries");
            try {
                map = hashOps.entries(hashKey);
                if (stopWatch != null)
                    stopWatch.stopNow();
                if (map != null && map.size() > 0) {
                    if (enableDataCache) {
                        AppCtx.getLocalCache().putData(pair, keyInfo);
                    }
                    LOGGER.debug("find - found from redis " + key);
                }
            } catch (Exception e) {
                if (stopWatch != null)
                    stopWatch.stopNow();
                foundAll = false;
                String msg = e.getCause().getMessage();
                LOGGER.error(msg);
                context.logTraceMessage(msg);
                e.printStackTrace();
                if (context.isSync()) {
                    throw new ServerErrorException(context, msg);
                }
                continue;
            }
        }
        if (map == null || map.size() == 0) {
            foundAll = false;
            LOGGER.debug("find - not found " + key);
            continue;
        }
        pair.setData(map);
    }
    LOGGER.trace("find returns " + foundAll);
    return foundAll;
}
Also used : KvPair(com.rdbcache.models.KvPair) KeyInfo(com.rdbcache.models.KeyInfo) ServerErrorException(com.rdbcache.exceptions.ServerErrorException) ServerErrorException(com.rdbcache.exceptions.ServerErrorException) StopWatch(com.rdbcache.models.StopWatch)

Example 5 with ServerErrorException

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

the class RedisRepoImpl method update.

@Override
public boolean update(Context context, KvPairs pairs, AnyKey anyKey) {
    LOGGER.trace("update pairs(" + pairs.size() + "): " + pairs.printKey() + "anyKey(" + anyKey.size() + "): " + anyKey.printTable());
    boolean foundAll = true;
    for (int i = 0; i < pairs.size(); i++) {
        KvPair pair = pairs.get(i);
        if (enableDataCache) {
            AppCtx.getLocalCache().updateData(pair);
        }
        String key = pair.getId();
        String hashKey = hdataPrefix + "::" + key;
        Map<String, Object> map = pair.getData();
        StopWatch stopWatch = context.startStopWatch("redis", "hashOps.putAll");
        try {
            hashOps.putAll(hashKey, map);
            if (stopWatch != null)
                stopWatch.stopNow();
            LOGGER.debug("update redis for " + key);
        } catch (Exception e) {
            if (stopWatch != null)
                stopWatch.stopNow();
            foundAll = false;
            String msg = e.getCause().getMessage();
            LOGGER.error(msg);
            e.printStackTrace();
            throw new ServerErrorException(context, msg);
        }
    }
    LOGGER.debug("update returns " + foundAll);
    return foundAll;
}
Also used : KvPair(com.rdbcache.models.KvPair) ServerErrorException(com.rdbcache.exceptions.ServerErrorException) ServerErrorException(com.rdbcache.exceptions.ServerErrorException) StopWatch(com.rdbcache.models.StopWatch)

Aggregations

ServerErrorException (com.rdbcache.exceptions.ServerErrorException)18 KeyInfo (com.rdbcache.models.KeyInfo)9 KvPair (com.rdbcache.models.KvPair)8 StopWatch (com.rdbcache.models.StopWatch)8 SQLException (java.sql.SQLException)4 AnyKey (com.rdbcache.helpers.AnyKey)1 KvPairs (com.rdbcache.helpers.KvPairs)1 URL (java.net.URL)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 EventListener (org.springframework.context.event.EventListener)1 PreparedStatementCreator (org.springframework.jdbc.core.PreparedStatementCreator)1 GeneratedKeyHolder (org.springframework.jdbc.support.GeneratedKeyHolder)1 KeyHolder (org.springframework.jdbc.support.KeyHolder)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1