Search in sources :

Example 21 with KvPairs

use of doitincloud.rdbcache.supports.KvPairs 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 22 with KvPairs

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

the class KvPairsTest method mapConstructor.

@Test
public void mapConstructor() {
    Map<String, Object> map = new LinkedHashMap<>();
    map.put("k0", Utils.toMap("{\"kk0\": 0}"));
    map.put("k1", Utils.toMap("{\"kk1\": 1}"));
    map.put("k2", Utils.toMap("{\"kk2\": 2}"));
    KvPairs pairs = new KvPairs(map);
    assertEquals(3, pairs.size());
    for (int i = 0; i < 3; i++) {
        KvPair pair = pairs.get(i);
        assertEquals("k" + i, pair.getId());
        map = pair.getData();
        assertEquals(i, map.get("kk" + i));
    }
}
Also used : KvPair(doitincloud.rdbcache.models.KvPair) KvPairs(doitincloud.rdbcache.supports.KvPairs) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 23 with KvPairs

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

the class KvPairsTest method setPair.

@Test
public void setPair() {
    KvPairs pairs;
    KvPair pair;
    pairs = new KvPairs();
    pair = new KvPair();
    pairs.setPair(pair);
    assertEquals(1, pairs.size());
    assertTrue(pair == pairs.get(0));
    pairs = new KvPairs(new KvPair());
    pair = new KvPair("key", "data", "value");
    pairs.setPair(pair);
    assertEquals(1, pairs.size());
    assertTrue(pair == pairs.get(0));
}
Also used : KvPair(doitincloud.rdbcache.models.KvPair) KvPairs(doitincloud.rdbcache.supports.KvPairs) Test(org.junit.Test)

Example 24 with KvPairs

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

the class KvPairsTest method getAny.

@Test
public void getAny() {
    KvPairs pairs;
    KvPair pair;
    pairs = new KvPairs();
    pair = pairs.getAny();
    assertNotNull(pair);
    pair = new KvPair("key", "data", "value");
    pairs = new KvPairs(pair);
    KvPair pair2 = pairs.getAny();
    assertNotNull(pair2);
    assertTrue(pair == pair2);
    assertTrue(pair == pairs.get(0));
    for (int i = 0; i < 10; i++) {
        pair = pairs.getAny(i);
        assertNotNull(pair);
        assertEquals(i + 1, pairs.size());
    }
}
Also used : KvPair(doitincloud.rdbcache.models.KvPair) KvPairs(doitincloud.rdbcache.supports.KvPairs) Test(org.junit.Test)

Example 25 with KvPairs

use of doitincloud.rdbcache.supports.KvPairs 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)

Aggregations

KvPairs (doitincloud.rdbcache.supports.KvPairs)44 AnyKey (doitincloud.rdbcache.supports.AnyKey)32 Context (doitincloud.rdbcache.supports.Context)25 KvPair (doitincloud.rdbcache.models.KvPair)22 KeyInfo (doitincloud.rdbcache.models.KeyInfo)17 BadRequestException (doitincloud.commons.exceptions.BadRequestException)14 Test (org.junit.Test)13 NotFoundException (doitincloud.commons.exceptions.NotFoundException)4 ServerErrorException (doitincloud.commons.exceptions.ServerErrorException)4 StopWatch (doitincloud.rdbcache.models.StopWatch)3 KvIdType (doitincloud.rdbcache.models.KvIdType)2 QueryInfo (doitincloud.rdbcache.queries.QueryInfo)2 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)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 ExpireDbOps (doitincloud.rdbcache.supports.ExpireDbOps)1 Connection (java.sql.Connection)1