Search in sources :

Example 1 with KvPair

use of doitincloud.rdbcache.models.KvPair in project rdbcache by rdbcache.

the class ParserTest method prepareStandardClauseParams.

@Test
public void prepareStandardClauseParams() {
    InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("test-table.json");
    assertNotNull(inputStream);
    String text = null;
    try (final Reader reader = new InputStreamReader(inputStream)) {
        text = CharStreams.toString(reader);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getCause().getMessage());
    }
    assertNotNull(text);
    Map<String, Object> testTable = Utils.toMap(text);
    assertNotNull(testTable);
    DbaseOps dbaseOps = new DbaseOps();
    AppCtx.setDbaseOps(dbaseOps);
    Context context = new Context();
    String json = "{\n" + "    \"id\" : 12467,\n" + "    \"email\" : \"kevin@example.com\",\n" + "    \"name\" : \"Kevin B.\",\n" + "    \"dob\" : \"1980-07-21\"\n" + "  }";
    KvPair pair = new KvPair("*", "data", Utils.toMap(json));
    try {
        KeyInfo keyInfo = new KeyInfo();
        keyInfo.setExpire("100");
        keyInfo.setTable("user_table");
        keyInfo.setCreatedAt(1522367710621L);
        String json2 = "{\"table\":\"user_table\",\"conditions\":{\"id\":{\"=\":[\"1\",\"2\",\"3\"]}},\"limit\":2}";
        QueryInfo queryInfo = Utils.toPojo(Utils.toMap(json2), QueryInfo.class);
        keyInfo.setQuery(queryInfo);
        keyInfo.setColumns((Map<String, Object>) testTable.get("table_columns::user_table"));
        keyInfo.setPrimaryIndexes(Arrays.asList("id"));
        Parser.prepareStandardClauseParams(context, pair, keyInfo);
        // System.out.println(Utils.toJsonMap(keyInfo));
        assertEquals("{\"expire\":\"100\",\"table\":\"user_table\",\"clause\":\"id = ?\"," + "\"params\":[12467],\"query\":{\"table\":\"user_table\",\"conditions\":{\"id\":{\"=\":" + "[\"1\",\"2\",\"3\"]}},\"limit\":2},\"query_key\":\"87677684c30a46c6e5afec88d0131410\"," + "\"is_new\":false,\"expire_old\":\"180\",\"created_at\":1522367710621}", Utils.toJsonMap(keyInfo));
        keyInfo.cleanup();
        // System.out.println(Utils.toJsonMap(keyInfo));
        assertEquals("{\"expire\":\"100\",\"table\":\"user_table\",\"clause\":\"id = ?\",\"params\":[12467]" + ",\"query_key\":\"87677684c30a46c6e5afec88d0131410\",\"is_new\":false,\"created_at\":1522367710621}", Utils.toJsonMap(keyInfo));
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getCause().getMessage());
    }
}
Also used : Context(doitincloud.rdbcache.supports.Context) DbaseOps(doitincloud.rdbcache.services.DbaseOps) InputStreamReader(java.io.InputStreamReader) KvPair(doitincloud.rdbcache.models.KvPair) KeyInfo(doitincloud.rdbcache.models.KeyInfo) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) Test(org.junit.Test)

Example 2 with KvPair

use of doitincloud.rdbcache.models.KvPair 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 3 with KvPair

use of doitincloud.rdbcache.models.KvPair 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 KvPair

use of doitincloud.rdbcache.models.KvPair in project rdbcache by rdbcache.

the class RdbcacheApisTest method delkey_post1.

@Test
public void delkey_post1() {
    try {
        Map<String, Object> data1 = null, data2 = null;
        {
            RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/rdbcache/v1/select/user_table?limit=3").accept(MediaType.APPLICATION_JSON);
            ResultActions actions = mockMvc.perform(requestBuilder);
            MvcResult result = actions.andReturn();
            MockHttpServletResponse response = result.getResponse();
            assertEquals(200, response.getStatus());
            String body = response.getContentAsString();
            // System.out.println(body);
            Map<String, Object> map = Utils.toMap(body);
            data1 = (Map<String, Object>) map.get("data");
            assertNotNull(data1);
            assertEquals(3, data1.size());
        }
        Set<String> keys = data1.keySet();
        {
            RequestBuilder requestBuilder = MockMvcRequestBuilders.post("/rdbcache/v1/delkey/user_table").contentType(MediaType.APPLICATION_JSON).content(Utils.toJsonMap(keys)).accept(MediaType.APPLICATION_JSON);
            ResultActions actions = mockMvc.perform(requestBuilder);
            MvcResult result = actions.andReturn();
            MockHttpServletResponse response = result.getResponse();
            assertEquals(200, response.getStatus());
            String body = response.getContentAsString();
            // System.out.println(body);
            Map<String, Object> map = Utils.toMap(body);
            assertNotNull(map.get("data"));
        }
        // allow time to synchronize data
        try {
            Thread.sleep(500);
        } catch (InterruptedException ie) {
            Thread.currentThread().interrupt();
        }
        Map<String, Object> redis = MockRedis.getData();
        for (String key : keys) {
            assertFalse(AppCtx.getCacheOps().containsKey(key));
            assertFalse(redis.containsKey(PropCfg.getHdataPrefix() + "::" + key));
            Map<String, Object> rdchkeys = (Map<String, Object>) redis.get("rdchkeys::user_table");
            assertFalse(rdchkeys.containsKey(key));
            KvPair pair = AppCtx.getKvPairRepo().findById(new KvIdType(key, "keyInfo"));
            assertNull(pair);
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail("caught an exception");
    }
}
Also used : RequestBuilder(org.springframework.test.web.servlet.RequestBuilder) KvPair(doitincloud.rdbcache.models.KvPair) MvcResult(org.springframework.test.web.servlet.MvcResult) KvIdType(doitincloud.rdbcache.models.KvIdType) ResultActions(org.springframework.test.web.servlet.ResultActions) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)

Example 5 with KvPair

use of doitincloud.rdbcache.models.KvPair 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)

Aggregations

KvPair (doitincloud.rdbcache.models.KvPair)51 KeyInfo (doitincloud.rdbcache.models.KeyInfo)29 KvPairs (doitincloud.rdbcache.supports.KvPairs)22 Context (doitincloud.rdbcache.supports.Context)15 Test (org.junit.Test)15 AnyKey (doitincloud.rdbcache.supports.AnyKey)14 StopWatch (doitincloud.rdbcache.models.StopWatch)13 ServerErrorException (doitincloud.commons.exceptions.ServerErrorException)9 BadRequestException (doitincloud.commons.exceptions.BadRequestException)7 KvIdType (doitincloud.rdbcache.models.KvIdType)6 SQLException (java.sql.SQLException)4 WebMvcTest (org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)4 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)4 MvcResult (org.springframework.test.web.servlet.MvcResult)4 RequestBuilder (org.springframework.test.web.servlet.RequestBuilder)4 ResultActions (org.springframework.test.web.servlet.ResultActions)4 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)2 EmptyResultDataAccessException (org.springframework.dao.EmptyResultDataAccessException)2 NotFoundException (doitincloud.commons.exceptions.NotFoundException)1