Search in sources :

Example 1 with KvPairs

use of com.rdbcache.helpers.KvPairs in project rdbcache by rdbcache.

the class QueryTest method updateTest.

@Test
public void updateTest() {
    try {
        Context context = new Context();
        KvPairs pairs = new KvPairs();
        AnyKey anyKey = new AnyKey();
        KeyInfo keyInfo = new KeyInfo();
        keyInfo.setExpire("100");
        keyInfo.setTable("user_table");
        String json1 = "{\"table\":\"user_table\",\"conditions\":{\"email\":{\"=\":[\"david@example.com\"]}}}";
        QueryInfo queryInfo1 = Utils.toPojo(Utils.toMap(json1), QueryInfo.class);
        keyInfo.setQuery(queryInfo1);
        anyKey.setKeyInfo(keyInfo);
        Query query = new Query(context, jdbcTemplate, pairs, anyKey);
        assertTrue(query.ifSelectOk());
        assertTrue(query.executeSelect());
        // System.out.println(Utils.toJsonMap(pairs));
        assertEquals(1, pairs.size());
        String key = pairs.getPair().getId();
        Integer id = (Integer) pairs.getPair().getData().get("id");
        assertNotNull(id);
        // System.out.println("id = "+id);
        String json2 = "{\"name\" : \"David Copper\"}";
        Map<String, Object> map1 = Utils.toMap(json2);
        KvPair pair = new KvPair(key, map1);
        pairs.setPair(pair);
        KeyInfo keyInfo2 = new KeyInfo();
        keyInfo2.setExpire("100");
        keyInfo2.setTable("user_table");
        keyInfo2.setClause("id = ?");
        keyInfo2.setParams(Arrays.asList(id));
        anyKey.setKeyInfo(keyInfo2);
        query = new Query(context, jdbcTemplate, pairs, anyKey);
        assertTrue(query.ifUpdateOk());
        assertTrue(query.executeUpdate());
        pairs.clear();
        query = new Query(context, jdbcTemplate, pairs, anyKey);
        assertTrue(query.ifSelectOk());
        assertTrue(query.executeSelect());
        map1 = pairs.getPair().getData();
        assertEquals("David Copper", map1.get("name"));
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getCause().getMessage());
    }
}
Also used : Context(com.rdbcache.helpers.Context) AnyKey(com.rdbcache.helpers.AnyKey) KeyInfo(com.rdbcache.models.KeyInfo) KvPair(com.rdbcache.models.KvPair) KvPairs(com.rdbcache.helpers.KvPairs) Test(org.junit.Test)

Example 2 with KvPairs

use of com.rdbcache.helpers.KvPairs in project rdbcache by rdbcache.

the class QueryTest method selectTest.

@Test
public void selectTest() {
    try {
        Context context = new Context();
        KvPairs pairs = new KvPairs();
        AnyKey anyKey = new AnyKey();
        KeyInfo keyInfo = new KeyInfo();
        keyInfo.setExpire("100");
        keyInfo.setTable("user_table");
        String json = "{\"table\":\"user_table\",\"conditions\":{\"id\":{\">\":[\"1\"]}},\"limit\":2}";
        QueryInfo queryInfo = Utils.toPojo(Utils.toMap(json), QueryInfo.class);
        keyInfo.setQuery(queryInfo);
        anyKey.setKeyInfo(keyInfo);
        Query query = new Query(context, jdbcTemplate, pairs, anyKey);
        assertTrue(query.ifSelectOk());
        assertTrue(query.executeSelect());
        // System.out.println(Utils.toJsonMap(pairs));
        assertEquals(2, pairs.size());
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getCause().getMessage());
    }
}
Also used : Context(com.rdbcache.helpers.Context) AnyKey(com.rdbcache.helpers.AnyKey) KeyInfo(com.rdbcache.models.KeyInfo) KvPairs(com.rdbcache.helpers.KvPairs) Test(org.junit.Test)

Example 3 with KvPairs

use of com.rdbcache.helpers.KvPairs in project rdbcache by rdbcache.

the class TaskQueue method onReceiveTask.

public void onReceiveTask(String task) {
    LOGGER.debug("Received Task: " + task);
    String[] parts = task.split("::");
    if (parts.length < 3) {
        LOGGER.error("invalid task format");
        return;
    }
    String action = parts[0];
    String key = parts[1];
    String traceId = parts[2];
    Context context = new Context(traceId);
    if (enableMonitor)
        context.enableMonitor(task, "queue", action);
    KvPair pair = new KvPair(key);
    KvPairs pairs = new KvPairs(pair);
    AnyKey anyKey = new AnyKey();
    if (!AppCtx.getKeyInfoRepo().find(context, pairs, anyKey)) {
        String msg = "keyInfo not found";
        LOGGER.error(msg);
        context.logTraceMessage(msg);
        return;
    }
    KeyInfo keyInfo = anyKey.getKeyInfo();
    // ...
    String msg = "unknown task action:" + action;
    LOGGER.error(msg);
    context.logTraceMessage(msg);
    context.closeMonitor();
}
Also used : Context(com.rdbcache.helpers.Context) AnyKey(com.rdbcache.helpers.AnyKey) KvPair(com.rdbcache.models.KvPair) KeyInfo(com.rdbcache.models.KeyInfo) KvPairs(com.rdbcache.helpers.KvPairs)

Example 4 with KvPairs

use of com.rdbcache.helpers.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.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 5 with KvPairs

use of com.rdbcache.helpers.KvPairs in project rdbcache by rdbcache.

the class QueryTest method insertTest.

@Test
public void insertTest() {
    try {
        Context context = new Context();
        KvPairs pairs = new KvPairs();
        String json = "{\n" + "    \"email\" : \"sam@example.com\",\n" + "    \"name\" : \"Sam W.\",\n" + "    \"dob\" : \"1975-08-12\"\n" + "  }";
        Map<String, Object> map1 = Utils.toMap(json);
        KvPair pair = new KvPair("*", map1);
        pairs.setPair(pair);
        AnyKey anyKey = new AnyKey();
        KeyInfo keyInfo = new KeyInfo();
        keyInfo.setExpire("100");
        keyInfo.setTable("user_table");
        anyKey.setKeyInfo(keyInfo);
        Query query = new Query(context, jdbcTemplate, pairs, anyKey);
        assertTrue(query.ifInsertOk());
        assertTrue(query.executeInsert(false, false));
        // System.out.println(Utils.toJsonMap(pairs));
        Map<String, Object> map2 = pair.getData();
        assertEquals("4", map2.get("id"));
        map2.remove("id");
        assertEquals(map1, map2);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getCause().getMessage());
    }
}
Also used : Context(com.rdbcache.helpers.Context) AnyKey(com.rdbcache.helpers.AnyKey) KvPair(com.rdbcache.models.KvPair) KeyInfo(com.rdbcache.models.KeyInfo) KvPairs(com.rdbcache.helpers.KvPairs) Test(org.junit.Test)

Aggregations

AnyKey (com.rdbcache.helpers.AnyKey)7 KvPairs (com.rdbcache.helpers.KvPairs)7 KeyInfo (com.rdbcache.models.KeyInfo)7 KvPair (com.rdbcache.models.KvPair)6 Context (com.rdbcache.helpers.Context)5 Test (org.junit.Test)4 ServerErrorException (com.rdbcache.exceptions.ServerErrorException)1 StopWatch (com.rdbcache.models.StopWatch)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 SQLException (java.sql.SQLException)1 PreparedStatementCreator (org.springframework.jdbc.core.PreparedStatementCreator)1 GeneratedKeyHolder (org.springframework.jdbc.support.GeneratedKeyHolder)1 KeyHolder (org.springframework.jdbc.support.KeyHolder)1