Search in sources :

Example 16 with AnyKey

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

the class RdbcacheApis method delkey_get.

/**
 * delkey_get get/delete single item
 *
 * To delete a key from redis based on the input key. No query string.
 * It returns immediately. It will not delete database entry.
 *
 * @param request HttpServletRequest
 * @param key String, hash key
 * @param opt1 String, can be "sync" or "async" and table
 * @param opt2 String, can be "sync" or "async"  and table, but not the same as opt1
 * @return ResponseEntity
 */
@RequestMapping(value = { "/rdbcache/v1/delkey/{key}", "/rdbcache/v1/delkey/{key}/{opt1}", "/rdbcache/v1/delkey/{key}/{opt1}/{opt2}" }, method = { RequestMethod.GET, RequestMethod.DELETE })
public ResponseEntity<?> delkey_get(HttpServletRequest request, @PathVariable("key") String key, @PathVariable Optional<String> opt1, @PathVariable Optional<String> opt2) {
    if (request.getParameterMap().size() != 0) {
        throw new BadRequestException("query string is not supported");
    }
    if (key.equals("*")) {
        throw new BadRequestException("no * allowed as key");
    }
    Context context = new Context();
    KvPairs pairs = new KvPairs(key);
    AnyKey anyKey = Request.process(context, request, pairs, opt1, opt2);
    if (anyKey.getKeyInfo().getIsNew()) {
        throw new NotFoundException("key not found for " + key);
    }
    LOGGER.trace(anyKey.print() + " pairs(" + pairs.size() + "): " + pairs.printKey());
    AppCtx.getAsyncOps().doDeleteFromRedis(context, pairs, anyKey);
    return Response.send(context, pairs);
}
Also used : Context(doitincloud.rdbcache.supports.Context) AnyKey(doitincloud.rdbcache.supports.AnyKey) BadRequestException(doitincloud.commons.exceptions.BadRequestException) NotFoundException(doitincloud.commons.exceptions.NotFoundException) KvPairs(doitincloud.rdbcache.supports.KvPairs)

Example 17 with AnyKey

use of doitincloud.rdbcache.supports.AnyKey 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.getCacheOps().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(doitincloud.rdbcache.supports.AnyKey) KvPair(doitincloud.rdbcache.models.KvPair) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) KvPairs(doitincloud.rdbcache.supports.KvPairs) KeyHolder(org.springframework.jdbc.support.KeyHolder) GeneratedKeyHolder(org.springframework.jdbc.support.GeneratedKeyHolder) SQLException(java.sql.SQLException) ServerErrorException(doitincloud.commons.exceptions.ServerErrorException) StopWatch(doitincloud.rdbcache.models.StopWatch) GeneratedKeyHolder(org.springframework.jdbc.support.GeneratedKeyHolder) KeyInfo(doitincloud.rdbcache.models.KeyInfo) PreparedStatementCreator(org.springframework.jdbc.core.PreparedStatementCreator) ServerErrorException(doitincloud.commons.exceptions.ServerErrorException)

Example 18 with AnyKey

use of doitincloud.rdbcache.supports.AnyKey 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 19 with AnyKey

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

the class SimpleExpireOps method setExpireKey.

public void setExpireKey(Context context, KvPairs pairs, AnyKey anyKey) {
    for (int i = 0; i < pairs.size(); i++) {
        KvPair pair = pairs.get(i);
        String key = pair.getId();
        KeyInfo keyInfo = anyKey.getAny(i);
        LOGGER.debug("setExpireKey: " + key + " expire: " + keyInfo.getExpire());
        String expire = keyInfo.getExpire();
        String expKey = eventPrefix + "::" + key;
        boolean hasKey = AppCtx.getRedisRepo().ifExist(context, new KvPairs(expKey), new AnyKey(keyInfo));
        Long expValue = Long.valueOf(expire);
        boolean done = false;
        // remove existing expire key
        if (hasKey) {
            if (expValue <= 0L || expire.startsWith("+")) {
                AppCtx.getRedisRepo().delete(context, new KvPairs(expKey), new AnyKey(keyInfo));
            } else {
                // for unsigned expire, event existed, no update
                done = true;
            }
        }
        // zero means no expiration
        if (!done && expValue == 0L) {
            done = true;
        }
        if (!done) {
            if (expValue < 0) {
                expValue = -expValue;
            }
            LOGGER.debug("setup expire: " + key + " expire: " + keyInfo.getExpire());
            AppCtx.getStringRedisTemplate().opsForValue().set(expKey, expire, expValue);
        } else {
            keyInfo.restoreExpire();
        }
        if (keyInfo.getIsNew()) {
            LOGGER.debug("save keyInfo: " + key + " expire: " + keyInfo.getExpire());
            AppCtx.getKeyInfoRepo().save(context, new KvPairs(pair), new AnyKey(keyInfo));
        }
    }
}
Also used : AnyKey(doitincloud.rdbcache.supports.AnyKey) KvPair(doitincloud.rdbcache.models.KvPair) KeyInfo(doitincloud.rdbcache.models.KeyInfo) KvPairs(doitincloud.rdbcache.supports.KvPairs)

Example 20 with AnyKey

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

the class RequestTest method process2.

@Test
public void process2() {
    try {
        String key = "01a089f3ab704c1aaecdbe13777538e0";
        HttpServletRequest request = getRequest("get", key, null, null, null, null);
        Context context = new Context();
        AnyKey anyKey = Request.process(context, request, new KvPairs(key), null, null);
        assertEquals(1, anyKey.size());
        KeyInfo keyInfo = anyKey.getKeyInfo();
        assertFalse(keyInfo.getIsNew());
        // System.out.println(keyInfo.toString());
        assertEquals("KeyInfo(false, user_table, 30, id = ?, [12466])", 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

AnyKey (doitincloud.rdbcache.supports.AnyKey)36 KvPairs (doitincloud.rdbcache.supports.KvPairs)32 Context (doitincloud.rdbcache.supports.Context)23 KeyInfo (doitincloud.rdbcache.models.KeyInfo)21 KvPair (doitincloud.rdbcache.models.KvPair)14 BadRequestException (doitincloud.commons.exceptions.BadRequestException)12 Test (org.junit.Test)9 NotFoundException (doitincloud.commons.exceptions.NotFoundException)4 ServerErrorException (doitincloud.commons.exceptions.ServerErrorException)4 StopWatch (doitincloud.rdbcache.models.StopWatch)2 QueryInfo (doitincloud.rdbcache.queries.QueryInfo)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 CacheOps (doitincloud.rdbcache.services.CacheOps)1 ExpireDbOps (doitincloud.rdbcache.supports.ExpireDbOps)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 SQLException (java.sql.SQLException)1