Search in sources :

Example 16 with ServerErrorException

use of doitincloud.commons.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.getCacheOps().listAllTables();
        } else if (action.equals("key")) {
            data = AppCtx.getCacheOps().listAllKeyInfo(null);
        } else if (action.equals("data")) {
            data = AppCtx.getCacheOps().listAllData(null);
        }
    } catch (Exception e) {
        String msg = e.getCause().getMessage();
        throw new ServerErrorException(msg);
    }
    return Response.send(context, data);
}
Also used : Context(doitincloud.rdbcache.supports.Context) ServerErrorException(doitincloud.commons.exceptions.ServerErrorException) ServerErrorException(doitincloud.commons.exceptions.ServerErrorException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 17 with ServerErrorException

use of doitincloud.commons.exceptions.ServerErrorException in project rdbcache by rdbcache.

the class Request method process.

public static AnyKey process(Context context, HttpServletRequest request, KvPairs pairs, Optional<String>... opts) {
    LOGGER.info("URI: " + request.getRequestURI());
    if (PropCfg.getEnableMonitor())
        context.enableMonitor(request);
    // {expire, table}
    String[] options = { null, null };
    for (int i = 0; i < opts.length; i++) {
        Optional<String> opt = opts[i];
        if (opt != null && opt.isPresent()) {
            assignOption(context, opt.get(), options);
        }
    }
    AnyKey anyKey = new AnyKey();
    if (pairs == null) {
        return anyKey;
    }
    KeyInfo keyInfo = anyKey.getAny();
    String table = options[1];
    if (pairs.size() > 0) {
        if (table != null) {
            // populate table info into all pairs
            for (KvPair pair : pairs) {
                pair.setType(table);
            }
        }
        // find key info for the first pair
        // 
        KvPair pair = pairs.get(0);
        if (!pair.isNewUuid()) {
            AppCtx.getKeyInfoRepo().find(context, pair, keyInfo);
        }
    }
    processOptions(context, request, keyInfo, options);
    if (pairs.size() == 0) {
        return anyKey;
    }
    // 
    if (keyInfo.getQuery() != null) {
        return anyKey;
    }
    // 
    if (pairs.size() > 1) {
        AppCtx.getKeyInfoRepo().find(context, pairs, anyKey);
    }
    // 
    for (int i = 0; i < pairs.size() && i < anyKey.size(); i++) {
        keyInfo = anyKey.get(i);
        if (keyInfo.getIsNew()) {
            keyInfo.setIsNew(false);
            KvPair pair = pairs.get(i);
            AppCtx.getCacheOps().putKeyInfo(pair.getIdType(), keyInfo);
            keyInfo.setIsNew(true);
        }
    }
    if (anyKey.size() != 1 && pairs.size() != anyKey.size()) {
        throw new ServerErrorException(context, "case not supported, anyKey size(" + anyKey.size() + ") != 1 && pairs size(" + pairs.size() + ") != anyKey size(" + anyKey.size() + ")");
    }
    return anyKey;
}
Also used : AnyKey(doitincloud.rdbcache.supports.AnyKey) KeyInfo(doitincloud.rdbcache.models.KeyInfo) KvPair(doitincloud.rdbcache.models.KvPair) ServerErrorException(doitincloud.commons.exceptions.ServerErrorException)

Example 18 with ServerErrorException

use of doitincloud.commons.exceptions.ServerErrorException in project rdbcache by rdbcache.

the class DbaseRepoImpl method kvSave.

private boolean kvSave(final Context context, final KvPairs pairs, final AnyKey anyKey) {
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("kvSave: " + pairs.printKey() + anyKey.print());
    }
    if (anyKey.size() == 0) {
        LOGGER.trace("kvSave(0) Ok - nothing to save");
        return true;
    }
    if (anyKey.size() == 1) {
        StopWatch stopWatch = context.startStopWatch("dbase", "kvPairRepo.save");
        try {
            AppCtx.getKvPairRepo().save(pairs.getPair());
            if (stopWatch != null)
                stopWatch.stopNow();
            LOGGER.trace("kvSave(1) Ok");
            return true;
        } catch (Exception e) {
            if (stopWatch != null)
                stopWatch.stopNow();
            String msg = e.getCause().getMessage();
            LOGGER.error(msg);
            context.logTraceMessage(msg);
            e.printStackTrace();
            if (context.isSync()) {
                throw new ServerErrorException(context, msg);
            }
        }
    } else {
        StopWatch stopWatch = context.startStopWatch("dbase", "kvPairRepo.saveAll");
        try {
            AppCtx.getKvPairRepo().saveAll(pairs);
            if (stopWatch != null)
                stopWatch.stopNow();
            if (LOGGER.isTraceEnabled())
                LOGGER.trace("kvSave(" + pairs.size() + ") Ok");
            return true;
        } catch (Exception e) {
            if (stopWatch != null)
                stopWatch.stopNow();
            String msg = e.getCause().getMessage();
            LOGGER.error(msg);
            context.logTraceMessage(msg);
            e.printStackTrace();
            if (context.isSync()) {
                throw new ServerErrorException(context, msg);
            }
        }
    }
    if (LOGGER.isTraceEnabled())
        LOGGER.trace("kvSave(" + pairs.size() + ") failed");
    return false;
}
Also used : ServerErrorException(doitincloud.commons.exceptions.ServerErrorException) ServerErrorException(doitincloud.commons.exceptions.ServerErrorException)

Example 19 with ServerErrorException

use of doitincloud.commons.exceptions.ServerErrorException in project rdbcache by rdbcache.

the class DbaseRepoImpl method saveOne.

private boolean saveOne(final Context context, final KvPair pair, final KeyInfo keyInfo) {
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("saveOne: " + pair.getId() + " " + keyInfo.toString());
    }
    String key = pair.getId();
    String table = keyInfo.getTable();
    Map<String, Object> map = pair.getData();
    if (pair.isNewUuid() && keyInfo.getQuery() == null && keyInfo.getParams() == null) {
        return insert(context, new KvPairs(pair), new AnyKey(keyInfo));
    }
    // get it from database
    KvPairs dbPairs = new KvPairs(pair.getIdType());
    if (!find(context, dbPairs, new AnyKey(keyInfo))) {
        return insert(context, new KvPairs(pair), new AnyKey(keyInfo));
    }
    KvPair dbPair = dbPairs.getPair();
    String dbValue = dbPair.getValue();
    String value = pair.getValue();
    if (value != null && value.equals(dbValue)) {
        LOGGER.trace("identical as string");
        return true;
    }
    Map<String, Object> dbMap = dbPair.getData();
    if (dbMap != null && dbMap.size() > 0) {
        if (table != null) {
            String autoIncKey = AppCtx.getDbaseOps().getTableAutoIncColumn(context, table);
            if (autoIncKey != null && !map.containsKey(autoIncKey)) {
                map.put(autoIncKey, dbMap.get(autoIncKey));
                if (enableDataCache) {
                    AppCtx.getCacheOps().updateData(pair);
                }
                if (enableRedisCache) {
                    if (AppCtx.getRedisRepo().ifExist(context, new KvPairs(pair), new AnyKey(keyInfo))) {
                        AppCtx.getRedisRepo().update(context, new KvPairs(pair), new AnyKey(keyInfo));
                    }
                }
            }
            Map<String, Object> indexes = AppCtx.getDbaseOps().getTableIndexes(context, table);
            List<String> primaryIndex = (List<String>) indexes.get("PRIMARY");
            Map<String, Object> todoMap = new LinkedHashMap<String, Object>();
            if (!DbUtils.mapChangesAfterUpdate(map, dbMap, todoMap, primaryIndex)) {
                if (enableDbFallback) {
                    String msg = "switch to default table, unknown field found in input: " + Utils.toJson(todoMap);
                    LOGGER.error(msg);
                    context.logTraceMessage(msg);
                    setUseDefaultTable(keyInfo);
                    todoMap = map;
                } else {
                    // System.out.println("map: " + Utils.toPrettyJson(map));
                    // System.out.println("dbMap: " + Utils.toPrettyJson(dbMap));
                    String msg = "unknown field found in input: " + Utils.toJson(todoMap);
                    LOGGER.error(msg);
                    context.logTraceMessage(msg);
                    if (context.isSync()) {
                        throw new ServerErrorException(context, msg);
                    }
                    return false;
                }
            }
            // identical map
            if (todoMap.size() == 0) {
                LOGGER.trace("identical as map");
                return true;
            }
            pair.setData(todoMap);
        } else if (DbUtils.isMapEquals(map, dbMap)) {
            LOGGER.trace("identical as map match");
            return true;
        }
    }
    if (table != null) {
        Parser.prepareStandardClauseParams(context, dbPair, keyInfo);
    }
    return update(context, new KvPairs(pair), new AnyKey(keyInfo));
}
Also used : AnyKey(doitincloud.rdbcache.supports.AnyKey) KvPairs(doitincloud.rdbcache.supports.KvPairs) ServerErrorException(doitincloud.commons.exceptions.ServerErrorException)

Example 20 with ServerErrorException

use of doitincloud.commons.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(doitincloud.commons.exceptions.ServerErrorException) URL(java.net.URL) ServerErrorException(doitincloud.commons.exceptions.ServerErrorException)

Aggregations

ServerErrorException (doitincloud.commons.exceptions.ServerErrorException)29 StopWatch (doitincloud.rdbcache.models.StopWatch)12 KeyInfo (doitincloud.rdbcache.models.KeyInfo)9 KvPair (doitincloud.rdbcache.models.KvPair)9 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)5 AnyKey (doitincloud.rdbcache.supports.AnyKey)4 KvPairs (doitincloud.rdbcache.supports.KvPairs)4 SQLException (java.sql.SQLException)4 Query (doitincloud.rdbcache.queries.Query)1 Context (doitincloud.rdbcache.supports.Context)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