Search in sources :

Example 31 with KeyInfo

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

the class RdbcacheApis method select_get.

/**
 * select_get get 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
 * @param opt2 String, can be expire or table, but not otp1
 * @return ResponseEntity
 */
@RequestMapping(value = { "/rdbcache/v1/select", "/rdbcache/v1/select/{opt1}", "/rdbcache/v1/select/{opt1}/{opt2}" }, method = RequestMethod.GET)
public ResponseEntity<?> select_get(HttpServletRequest request, @PathVariable Optional<String> opt1, @PathVariable Optional<String> opt2) {
    Context context = new Context(true, true);
    KvPairs pairs = new KvPairs();
    AnyKey anyKey = Request.process(context, request, pairs, opt1, opt2);
    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 : KeyInfo(com.rdbcache.models.KeyInfo) QueryInfo(com.rdbcache.queries.QueryInfo)

Example 32 with KeyInfo

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

the class AnyKey method getAny.

public KeyInfo getAny(int index) {
    int size = size();
    if (index > size) {
        throw new ServerErrorException("getAny index out of range");
    }
    if (size == 0 || index == size) {
        KeyInfo keyInfo = null;
        if (size == 0) {
            keyInfo = new KeyInfo();
            keyInfo.setIsNew(true);
        } else {
            keyInfo = get(0).clone();
            keyInfo.setIsNew(true);
            keyInfo.clearParams();
        }
        add(keyInfo);
    }
    return get(index);
}
Also used : KeyInfo(com.rdbcache.models.KeyInfo) ServerErrorException(com.rdbcache.exceptions.ServerErrorException)

Example 33 with KeyInfo

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

the class Query method executeInsert.

public boolean executeInsert(boolean enableLocal, boolean enableRedis) {
    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();
        Map<String, Object> map = pair.getData();
        String autoIncKey = AppCtx.getDbaseOps().getTableAutoIncColumn(context, table);
        boolean cacheUpdate = false;
        if (!map.containsKey(autoIncKey) && keyInfo.getParams() != null && keyInfo.getParams().size() == 1) {
            String stdClause = "(" + autoIncKey + " = ?)";
            if (stdClause.equals(keyInfo.getClause())) {
                map.put(autoIncKey, keyInfo.getParams().get(0));
                cacheUpdate = true;
            }
        }
        Map<String, Object> columns = keyInfo.getColumns();
        AppCtx.getDbaseOps().convertDbMap(columns, map);
        String fields = "", values = "";
        params.clear();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            params.add(entry.getValue());
            if (fields.length() != 0) {
                fields += ", ";
                values += ", ";
            }
            fields += entry.getKey();
            values += "?";
        }
        sql = "insert into " + table + " (" + fields + ") values (" + values + ")";
        LOGGER.trace("sql: " + sql);
        LOGGER.trace("params: " + params.toString());
        int rowCount = 0;
        KeyHolder keyHolder = new GeneratedKeyHolder();
        StopWatch stopWatch = context.startStopWatch("dbase", "jdbcTemplate.update");
        try {
            rowCount = jdbcTemplate.update(new PreparedStatementCreator() {

                @Override
                public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                    PreparedStatement ps;
                    ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
                    int i = 1;
                    for (Object param : params) {
                        ps.setObject(i++, param);
                    }
                    return ps;
                }
            }, keyHolder);
            if (stopWatch != null)
                stopWatch.stopNow();
        } catch (Exception e) {
            if (stopWatch != null)
                stopWatch.stopNow();
            allOk = false;
            String msg = e.getCause().getMessage();
            LOGGER.error(msg);
            context.logTraceMessage(msg);
            if (context.isSync()) {
                throw new ServerErrorException(context, msg);
            }
        }
        if (rowCount > 0) {
            if (autoIncKey != null && keyHolder.getKey() != null) {
                String keyValue = String.valueOf(keyHolder.getKey());
                map.put(autoIncKey, keyValue);
                cacheUpdate = true;
            }
            if (cacheUpdate) {
                if (enableLocal) {
                    AppCtx.getLocalCache().putData(pair, keyInfo);
                }
                if (enableRedis) {
                    AppCtx.getRedisRepo().save(context, new KvPairs(pair), new AnyKey(keyInfo));
                }
            }
            if (!Parser.prepareStandardClauseParams(context, pair, keyInfo)) {
                String msg = "executeInsert failed when prepareStandardClauseParams for " + pair.getId();
                LOGGER.error(msg);
                context.logTraceMessage(msg);
                if (context.isSync()) {
                    throw new ServerErrorException(context, msg);
                }
            }
            LOGGER.trace("inserted " + pair.getId() + " into " + table);
        } else {
            allOk = false;
            LOGGER.warn("failed to insert " + pair.getId() + " into " + table);
        }
    }
    return allOk;
}
Also used : AnyKey(com.rdbcache.helpers.AnyKey) KvPair(com.rdbcache.models.KvPair) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) KvPairs(com.rdbcache.helpers.KvPairs) KeyHolder(org.springframework.jdbc.support.KeyHolder) GeneratedKeyHolder(org.springframework.jdbc.support.GeneratedKeyHolder) ServerErrorException(com.rdbcache.exceptions.ServerErrorException) SQLException(java.sql.SQLException) StopWatch(com.rdbcache.models.StopWatch) GeneratedKeyHolder(org.springframework.jdbc.support.GeneratedKeyHolder) KeyInfo(com.rdbcache.models.KeyInfo) PreparedStatementCreator(org.springframework.jdbc.core.PreparedStatementCreator) ServerErrorException(com.rdbcache.exceptions.ServerErrorException)

Example 34 with KeyInfo

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

the class Query method executeDelete.

public boolean executeDelete() {
    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)) {
            String msg = "executeDelete failed when calling prepareStandardClauseParams for " + pair.getId();
            LOGGER.error(msg);
            context.logTraceMessage(msg);
            if (context.isSync()) {
                throw new ServerErrorException(context, msg);
            }
            continue;
        }
        params = keyInfo.getParams();
        String clause = keyInfo.getClause();
        sql = "delete from " + table + " where " + clause + " limit 1";
        LOGGER.trace("sql: " + sql);
        LOGGER.trace("params: " + params.toString());
        StopWatch stopWatch = context.startStopWatch("dbase", "jdbcTemplate.delete");
        try {
            if (jdbcTemplate.update(sql, params.toArray()) > 0) {
                if (stopWatch != null)
                    stopWatch.stopNow();
                LOGGER.trace("delete " + 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) KeyInfo(com.rdbcache.models.KeyInfo) ServerErrorException(com.rdbcache.exceptions.ServerErrorException) ServerErrorException(com.rdbcache.exceptions.ServerErrorException) SQLException(java.sql.SQLException) StopWatch(com.rdbcache.models.StopWatch)

Example 35 with KeyInfo

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

the class Query method executeSelect.

public boolean executeSelect() {
    KeyInfo keyInfo = anyKey.getKeyInfo();
    String table = keyInfo.getTable();
    LOGGER.trace("sql: " + sql);
    LOGGER.trace("params: " + (params != null ? params.toString() : "null"));
    List<Map<String, Object>> list = null;
    StopWatch stopWatch = context.startStopWatch("dbase", "jdbcTemplate.queryForList");
    try {
        if (params != null) {
            list = jdbcTemplate.queryForList(sql, params.toArray());
        } else {
            list = jdbcTemplate.queryForList(sql);
        }
        if (stopWatch != null)
            stopWatch.stopNow();
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                KvPair pair = pairs.getAny(i);
                keyInfo = anyKey.getAny(i);
                pair.setData(list.get(i));
                // 
                if (!Parser.prepareStandardClauseParams(context, pair, keyInfo)) {
                    String msg = "executeSelect failed when prepareStandardClauseParams for " + pair.getId();
                    LOGGER.error(msg);
                    context.logTraceMessage(msg);
                    if (context.isSync()) {
                        throw new ServerErrorException(context, msg);
                    }
                }
                LOGGER.trace("found " + pair.getId() + " from " + table);
            }
            return true;
        }
    } catch (Exception e) {
        if (stopWatch != null)
            stopWatch.stopNow();
        e.printStackTrace();
        String msg = e.getCause().getMessage();
        LOGGER.error(msg);
        context.logTraceMessage(msg);
        if (context.isSync()) {
            throw new ServerErrorException(context, msg);
        }
    }
    return false;
}
Also used : KeyInfo(com.rdbcache.models.KeyInfo) KvPair(com.rdbcache.models.KvPair) ServerErrorException(com.rdbcache.exceptions.ServerErrorException) ServerErrorException(com.rdbcache.exceptions.ServerErrorException) SQLException(java.sql.SQLException) StopWatch(com.rdbcache.models.StopWatch)

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