Search in sources :

Example 21 with AnyKey

use of doitincloud.rdbcache.supports.AnyKey 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 22 with AnyKey

use of doitincloud.rdbcache.supports.AnyKey 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 23 with AnyKey

use of doitincloud.rdbcache.supports.AnyKey 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 24 with AnyKey

use of doitincloud.rdbcache.supports.AnyKey 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)

Example 25 with AnyKey

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

the class RdbcacheApis method delkey_post.

/**
 * delkey_post post multiple items
 *
 * To delete one or more keys from redis based on the input keys. No query string.
 * It returns immediately. It will not delete database entry.
 *
 * @param request HttpServletRequest
 * @param keys List, list of keys for returned entries
 * @param opt1 String, can be "sync" or "async" and table
 * @param opt2 String, can be "sync" or "async"  and table, but not the same as opt1
 * @return ResponseEntity
 */
@RequestMapping(value = { "/rdbcache/v1/delkey", "/rdbcache/v1/delkey/{opt1}", "/rdbcache/v1/delkey/{opt1}/{opt2}" }, method = RequestMethod.POST)
public ResponseEntity<?> delkey_post(HttpServletRequest request, @RequestBody List<String> keys, @PathVariable Optional<String> opt1, @PathVariable Optional<String> opt2) {
    if (request.getParameterMap().size() != 0) {
        throw new BadRequestException("query string is not supported");
    }
    if (keys.contains("*")) {
        throw new BadRequestException("no * allowed as key");
    }
    Context context = new Context();
    KvPairs pairs = new KvPairs(keys);
    AnyKey anyKey = Request.process(context, request, pairs, opt1, opt2);
    if (anyKey.size() != keys.size()) {
        context.logTraceMessage("one or more keys not found");
    }
    for (int i = 0; i < anyKey.size(); i++) {
        KvPair pair = pairs.get(i);
        KeyInfo keyInfo = anyKey.get(i);
        if (keyInfo.getIsNew()) {
            context.logTraceMessage("key not found for " + pair.getId());
        }
    }
    LOGGER.trace(anyKey.print() + " pairs(" + pairs.size() + "): " + pairs.printKey());
    AppCtx.getAsyncOps().doDeleteFromRedis(context, pairs, anyKey);
    return Response.send(context, pairs);
}
Also used : Context(doitincloud.rdbcache.supports.Context) AnyKey(doitincloud.rdbcache.supports.AnyKey) KvPair(doitincloud.rdbcache.models.KvPair) KeyInfo(doitincloud.rdbcache.models.KeyInfo) BadRequestException(doitincloud.commons.exceptions.BadRequestException) KvPairs(doitincloud.rdbcache.supports.KvPairs)

Aggregations

AnyKey (doitincloud.rdbcache.supports.AnyKey)36 KvPairs (doitincloud.rdbcache.supports.KvPairs)32 Context (doitincloud.rdbcache.supports.Context)23 KeyInfo (doitincloud.rdbcache.models.KeyInfo)21 KvPair (doitincloud.rdbcache.models.KvPair)14 BadRequestException (doitincloud.commons.exceptions.BadRequestException)12 Test (org.junit.Test)9 NotFoundException (doitincloud.commons.exceptions.NotFoundException)4 ServerErrorException (doitincloud.commons.exceptions.ServerErrorException)4 StopWatch (doitincloud.rdbcache.models.StopWatch)2 QueryInfo (doitincloud.rdbcache.queries.QueryInfo)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 CacheOps (doitincloud.rdbcache.services.CacheOps)1 ExpireDbOps (doitincloud.rdbcache.supports.ExpireDbOps)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 SQLException (java.sql.SQLException)1