Search in sources :

Example 11 with KeyInfo

use of com.rdbcache.models.KeyInfo in project rdbcache by rdbcache.

the class AnyKeyTest method getKey.

@Test
public void getKey() {
    AnyKey anyKey;
    KeyInfo keyInfo;
    anyKey = new AnyKey();
    keyInfo = anyKey.getKeyInfo();
    assertNull(keyInfo);
    keyInfo = new KeyInfo();
    keyInfo.setExpire("100");
    keyInfo.setTable("table");
    anyKey = new AnyKey(keyInfo);
    KeyInfo keyInfo2 = anyKey.getKeyInfo();
    assertNotNull(keyInfo2);
    assertTrue(keyInfo == keyInfo2);
}
Also used : KeyInfo(com.rdbcache.models.KeyInfo) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 12 with KeyInfo

use of com.rdbcache.models.KeyInfo 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(com.rdbcache.helpers.Context) DbaseOps(com.rdbcache.services.DbaseOps) InputStreamReader(java.io.InputStreamReader) KvPair(com.rdbcache.models.KvPair) KeyInfo(com.rdbcache.models.KeyInfo) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) Test(org.junit.Test)

Example 13 with KeyInfo

use of com.rdbcache.models.KeyInfo 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 14 with KeyInfo

use of com.rdbcache.models.KeyInfo 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 15 with KeyInfo

use of com.rdbcache.models.KeyInfo in project rdbcache by rdbcache.

the class SimpleKeyInfoRepo method find.

@Override
public boolean find(Context context, KvPairs pairs, AnyKey anyKey) {
    LOGGER.trace("find pairs(" + pairs.size() + ") anyKey(" + anyKey.size() + ")");
    boolean foundAll = true;
    for (int i = 0; i < pairs.size(); i++) {
        KvPair pair = pairs.get(i);
        KeyInfo keyInfo = anyKey.getAny(i);
        String key = pair.getId();
        Map<String, Object> map = (Map<String, Object>) data.get(key);
        if (map == null) {
            foundAll = false;
            LOGGER.trace("find: Not Found " + key);
            continue;
        } else {
            keyInfo = Utils.toPojo(map, KeyInfo.class);
            anyKey.set(i, keyInfo);
            LOGGER.trace("find: Found " + key);
        }
    }
    return foundAll;
}
Also used : KvPair(com.rdbcache.models.KvPair) KeyInfo(com.rdbcache.models.KeyInfo) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

KeyInfo (com.rdbcache.models.KeyInfo)44 KvPair (com.rdbcache.models.KvPair)23 Test (org.junit.Test)13 ServerErrorException (com.rdbcache.exceptions.ServerErrorException)9 StopWatch (com.rdbcache.models.StopWatch)9 AnyKey (com.rdbcache.helpers.AnyKey)7 Context (com.rdbcache.helpers.Context)7 KvPairs (com.rdbcache.helpers.KvPairs)7 BadRequestException (com.rdbcache.exceptions.BadRequestException)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 SQLException (java.sql.SQLException)4 QueryInfo (com.rdbcache.queries.QueryInfo)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 MockServletContext (org.springframework.mock.web.MockServletContext)2 KeyInfoRedisTemplate (com.rdbcache.configs.KeyInfoRedisTemplate)1 NotFoundException (com.rdbcache.exceptions.NotFoundException)1 DbaseOps (com.rdbcache.services.DbaseOps)1 LocalCache (com.rdbcache.services.LocalCache)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1