Search in sources :

Example 1 with KvPairs

use of doitincloud.rdbcache.supports.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("*", "data", 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(doitincloud.rdbcache.supports.Context) AnyKey(doitincloud.rdbcache.supports.AnyKey) KvPair(doitincloud.rdbcache.models.KvPair) KeyInfo(doitincloud.rdbcache.models.KeyInfo) KvPairs(doitincloud.rdbcache.supports.KvPairs) Test(org.junit.Test)

Example 2 with KvPairs

use of doitincloud.rdbcache.supports.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(doitincloud.rdbcache.supports.Context) AnyKey(doitincloud.rdbcache.supports.AnyKey) KeyInfo(doitincloud.rdbcache.models.KeyInfo) KvPairs(doitincloud.rdbcache.supports.KvPairs) Test(org.junit.Test)

Example 3 with KvPairs

use of doitincloud.rdbcache.supports.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, "data", 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(doitincloud.rdbcache.supports.Context) AnyKey(doitincloud.rdbcache.supports.AnyKey) KeyInfo(doitincloud.rdbcache.models.KeyInfo) KvPair(doitincloud.rdbcache.models.KvPair) KvPairs(doitincloud.rdbcache.supports.KvPairs) Test(org.junit.Test)

Example 4 with KvPairs

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

the class AsyncOps method deleteKvPairsKeyInfo.

public void deleteKvPairsKeyInfo(Context context, KvPairs pairs, AnyKey anyKey) {
    KvPairs toDeletePairs = new KvPairs();
    for (KvPair pair : pairs) {
        String kvType = "keyInfo";
        String type = pair.getType();
        if (!type.equals("data")) {
            kvType += ":" + type;
        }
        toDeletePairs.add(new KvPair(pair.getId(), kvType));
    }
    if (toDeletePairs.size() > 0) {
        StopWatch stopWatch = context.startStopWatch("dbase", "KvPairRepo.deleteAll");
        AppCtx.getKvPairRepo().deleteAll(toDeletePairs);
        if (stopWatch != null)
            stopWatch.stopNow();
    }
}
Also used : KvPair(doitincloud.rdbcache.models.KvPair) KvPairs(doitincloud.rdbcache.supports.KvPairs) StopWatch(doitincloud.rdbcache.models.StopWatch)

Example 5 with KvPairs

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

the class QueueOps 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 hashKey = parts[1];
    int index = hashKey.indexOf(":");
    if (index < 0) {
        LOGGER.error("invalid event format, failed to figure out type and key");
        return;
    }
    String type = hashKey.substring(0, index);
    String key = hashKey.substring(index + 1);
    String traceId = parts[2];
    Context context = new Context(traceId);
    if (enableMonitor)
        context.enableMonitor(task, "queue", action);
    KvPair pair = new KvPair(key, type);
    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(doitincloud.rdbcache.supports.Context) 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