Search in sources :

Example 26 with KvPairs

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

the class RequestTest method process2.

@Test
public void process2() {
    try {
        String key = "01a089f3ab704c1aaecdbe13777538e0";
        HttpServletRequest request = getRequest("get", key, null, null, null, null);
        Context context = new Context();
        AnyKey anyKey = Request.process(context, request, new KvPairs(key), null, null);
        assertEquals(1, anyKey.size());
        KeyInfo keyInfo = anyKey.getKeyInfo();
        assertFalse(keyInfo.getIsNew());
        // System.out.println(keyInfo.toString());
        assertEquals("KeyInfo(false, user_table, 30, id = ?, [12466])", keyInfo.toString());
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getCause().getMessage());
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Context(doitincloud.rdbcache.supports.Context) MockServletContext(org.springframework.mock.web.MockServletContext) AnyKey(doitincloud.rdbcache.supports.AnyKey) KeyInfo(doitincloud.rdbcache.models.KeyInfo) KvPairs(doitincloud.rdbcache.supports.KvPairs) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 27 with KvPairs

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

the class QueryTest method deleteTest.

@Test
public void deleteTest() {
    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\":{\"id\":{\"=\":[\"3\"]}}}";
        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();
        KvPair pair = new KvPair(key);
        pairs.setPair(pair);
        KeyInfo keyInfo2 = new KeyInfo();
        keyInfo2.setExpire("100");
        keyInfo2.setTable("user_table");
        keyInfo2.setClause("id = ?");
        keyInfo2.setParams(Arrays.asList("3"));
        anyKey.setKeyInfo(keyInfo2);
        query = new Query(context, jdbcTemplate, pairs, anyKey);
        assertTrue(query.ifDeleteOk());
        assertTrue(query.executeDelete());
        pairs.clear();
        query = new Query(context, jdbcTemplate, pairs, anyKey);
        assertTrue(query.ifSelectOk());
        assertFalse(query.executeSelect());
        assertEquals(0, 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) KvPair(doitincloud.rdbcache.models.KvPair) KvPairs(doitincloud.rdbcache.supports.KvPairs) Test(org.junit.Test)

Example 28 with KvPairs

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

the class DbaseRepoImpl method save.

@Override
public boolean save(final Context context, final KvPairs pairs, final AnyKey anyKey) {
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("save: " + pairs.printKey() + anyKey.print());
    }
    if (pairs.size() == 1) {
        if (saveOne(context, pairs.getPair(), anyKey.getKeyInfo())) {
            LOGGER.debug("save - saveOne Ok: " + pairs.getPair().printKey());
            AppCtx.getKeyInfoRepo().save(context, pairs, anyKey);
            return true;
        } else {
            LOGGER.debug("save - saveOne failed: " + pairs.getPair().printKey());
            return false;
        }
    } else {
        boolean result = true;
        for (int i = 0; i < pairs.size(); i++) {
            KvPair pair = pairs.get(i);
            KeyInfo keyInfo = anyKey.getAny(i);
            if (!saveOne(context, pair, keyInfo)) {
                LOGGER.debug("save - saveOne failed: " + pair.getId());
                result = false;
            } else {
                AppCtx.getKeyInfoRepo().save(context, new KvPairs(pair), new AnyKey(keyInfo));
                LOGGER.debug("save - saveOne Ok: " + pair.getId());
            }
        }
        return result;
    }
}
Also used : AnyKey(doitincloud.rdbcache.supports.AnyKey) KvPairs(doitincloud.rdbcache.supports.KvPairs)

Example 29 with KvPairs

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

the class DbaseRepoImpl method update.

@Override
public boolean update(final Context context, final KvPair pair, final KeyInfo keyInfo) {
    KvPairs pairs = new KvPairs(pair);
    AnyKey anyKey = new AnyKey(keyInfo);
    return update(context, pairs, anyKey);
}
Also used : AnyKey(doitincloud.rdbcache.supports.AnyKey) KvPairs(doitincloud.rdbcache.supports.KvPairs)

Example 30 with KvPairs

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

the class DbaseRepoImpl method saveOne.

private boolean saveOne(final Context context, final KvPair pair, final KeyInfo keyInfo) {
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("saveOne: " + pair.getId() + " " + keyInfo.toString());
    }
    String key = pair.getId();
    String table = keyInfo.getTable();
    Map<String, Object> map = pair.getData();
    if (pair.isNewUuid() && keyInfo.getQuery() == null && keyInfo.getParams() == null) {
        return insert(context, new KvPairs(pair), new AnyKey(keyInfo));
    }
    // get it from database
    KvPairs dbPairs = new KvPairs(pair.getIdType());
    if (!find(context, dbPairs, new AnyKey(keyInfo))) {
        return insert(context, new KvPairs(pair), new AnyKey(keyInfo));
    }
    KvPair dbPair = dbPairs.getPair();
    String dbValue = dbPair.getValue();
    String value = pair.getValue();
    if (value != null && value.equals(dbValue)) {
        LOGGER.trace("identical as string");
        return true;
    }
    Map<String, Object> dbMap = dbPair.getData();
    if (dbMap != null && dbMap.size() > 0) {
        if (table != null) {
            String autoIncKey = AppCtx.getDbaseOps().getTableAutoIncColumn(context, table);
            if (autoIncKey != null && !map.containsKey(autoIncKey)) {
                map.put(autoIncKey, dbMap.get(autoIncKey));
                if (enableDataCache) {
                    AppCtx.getCacheOps().updateData(pair);
                }
                if (enableRedisCache) {
                    if (AppCtx.getRedisRepo().ifExist(context, new KvPairs(pair), new AnyKey(keyInfo))) {
                        AppCtx.getRedisRepo().update(context, new KvPairs(pair), new AnyKey(keyInfo));
                    }
                }
            }
            Map<String, Object> indexes = AppCtx.getDbaseOps().getTableIndexes(context, table);
            List<String> primaryIndex = (List<String>) indexes.get("PRIMARY");
            Map<String, Object> todoMap = new LinkedHashMap<String, Object>();
            if (!DbUtils.mapChangesAfterUpdate(map, dbMap, todoMap, primaryIndex)) {
                if (enableDbFallback) {
                    String msg = "switch to default table, unknown field found in input: " + Utils.toJson(todoMap);
                    LOGGER.error(msg);
                    context.logTraceMessage(msg);
                    setUseDefaultTable(keyInfo);
                    todoMap = map;
                } else {
                    // System.out.println("map: " + Utils.toPrettyJson(map));
                    // System.out.println("dbMap: " + Utils.toPrettyJson(dbMap));
                    String msg = "unknown field found in input: " + Utils.toJson(todoMap);
                    LOGGER.error(msg);
                    context.logTraceMessage(msg);
                    if (context.isSync()) {
                        throw new ServerErrorException(context, msg);
                    }
                    return false;
                }
            }
            // identical map
            if (todoMap.size() == 0) {
                LOGGER.trace("identical as map");
                return true;
            }
            pair.setData(todoMap);
        } else if (DbUtils.isMapEquals(map, dbMap)) {
            LOGGER.trace("identical as map match");
            return true;
        }
    }
    if (table != null) {
        Parser.prepareStandardClauseParams(context, dbPair, keyInfo);
    }
    return update(context, new KvPairs(pair), new AnyKey(keyInfo));
}
Also used : AnyKey(doitincloud.rdbcache.supports.AnyKey) KvPairs(doitincloud.rdbcache.supports.KvPairs) ServerErrorException(doitincloud.commons.exceptions.ServerErrorException)

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