Search in sources :

Example 11 with KvPairs

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

the class DbaseRepoImpl method kvUpdate.

private boolean kvUpdate(final Context context, final KvPairs pairs, final AnyKey anyKey) {
    KvPairs pairsClone = pairs.clone();
    kvFind(context, pairsClone, anyKey);
    for (int i = 0; i < pairsClone.size(); i++) {
        Map<String, Object> data = pairsClone.get(i).getData();
        Map<String, Object> update = pairs.get(i).getData();
        for (Map.Entry<String, Object> entry : update.entrySet()) {
            data.put(entry.getKey(), entry.getValue());
        }
    }
    return kvSave(context, pairsClone, anyKey);
}
Also used : KvPairs(doitincloud.rdbcache.supports.KvPairs)

Example 12 with KvPairs

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

the class KeyInfoRepoImpl method save.

@Override
public boolean save(final Context context, final KvPairs pairs, final AnyKey anyKey) {
    Assert.isTrue(anyKey.size() == pairs.size(), anyKey.size() + " != " + pairs.size() + ", only supports that pairs and anyKey have the same size");
    if (pairs.size() == 0 || anyKey.size() == 0) {
        LOGGER.debug("save " + pairs.printKey() + "anyKey(" + anyKey.size() + ") - nothing to save");
        return false;
    }
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("save " + pairs.printKey() + anyKey.print());
    }
    String type = pairs.get(0).getType();
    String kvType = "keyInfo";
    if (!type.equals("data")) {
        kvType += ":" + type;
    }
    KvPairs keyInfoPairs = new KvPairs();
    Map<String, KeyInfo> keyInfoMap = new LinkedHashMap<>();
    for (int i = 0; i < pairs.size() && i < anyKey.size(); i++) {
        KvPair pair = pairs.get(i);
        if (!pair.getType().equals(type)) {
            throw new ServerErrorException("must have the same type when saving multiple keyInfos");
        }
        String key = pair.getId();
        KeyInfo keyInfo = anyKey.getAny(i);
        if (keyInfo == null) {
            if (LOGGER.isTraceEnabled())
                LOGGER.trace("save KeyInfo is null for " + key);
            continue;
        }
        if (!keyInfo.getIsNew()) {
            if (LOGGER.isTraceEnabled())
                LOGGER.trace("save KeyInfo is not new, skipped for " + key);
            continue;
        }
        keyInfo.cleanup();
        AppCtx.getCacheOps().putKeyInfo(pair.getIdType(), keyInfo);
        if (enableRedisCache) {
            keyInfoMap.put(key, keyInfo);
        }
        keyInfoPairs.add(new KvPair(key, kvType, Utils.toMap(keyInfo)));
    }
    if (keyInfoMap.size() > 0) {
        StopWatch stopWatch = context.startStopWatch("redis", "keyInfoOps.putAll");
        keyInfoOps.putAll(hkeyPrefix + "::" + type, keyInfoMap);
        if (stopWatch != null)
            stopWatch.stopNow();
    }
    if (keyInfoPairs.size() > 0) {
        StopWatch stopWatch = context.startStopWatch("dbase", "kvPairRepo.saveAll");
        AppCtx.getKvPairRepo().saveAll(keyInfoPairs);
        if (stopWatch != null)
            stopWatch.stopNow();
    }
    LOGGER.debug("save Ok: " + pairs.printKey());
    return true;
}
Also used : KvPairs(doitincloud.rdbcache.supports.KvPairs) ServerErrorException(doitincloud.commons.exceptions.ServerErrorException)

Example 13 with KvPairs

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

the class RdbcacheApis method trace_post.

/**
 * trace_post post multiple items
 *
 * get error messages by trace id list
 *
 * @param request HttpServletRequest
 * @param traceIds List trace id list
 * @return ResponseEntity
 */
@RequestMapping(value = { "/rdbcache/v1/trace" }, method = RequestMethod.POST)
public ResponseEntity<?> trace_post(HttpServletRequest request, @RequestBody List<String> traceIds) {
    if (request.getParameterMap().size() != 0) {
        throw new BadRequestException("query string is not supported");
    }
    if (traceIds == null || traceIds.size() == 0) {
        throw new BadRequestException("missing trace ids");
    }
    if (traceIds.contains("*")) {
        throw new BadRequestException("no * allowed as trace id");
    }
    if (request.getParameterMap().size() != 0) {
        throw new BadRequestException("no query string is needed");
    }
    Context context = new Context(true, true);
    KvPairs pairs = new KvPairs();
    Request.process(context, request, pairs);
    LOGGER.trace("pairs(" + pairs.size() + "): " + pairs.printKey());
    for (String referenced_id : traceIds) {
        KvPair pair = AppCtx.getKvPairRepo().findById(new KvIdType(referenced_id, "trace"));
        if (pair != null) {
            pairs.add(pair);
        } else {
            pairs.add(new KvPair(referenced_id));
        }
    }
    return Response.send(context, pairs);
}
Also used : Context(doitincloud.rdbcache.supports.Context) KvIdType(doitincloud.rdbcache.models.KvIdType) KvPair(doitincloud.rdbcache.models.KvPair) BadRequestException(doitincloud.commons.exceptions.BadRequestException) KvPairs(doitincloud.rdbcache.supports.KvPairs)

Example 14 with KvPairs

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

the class RdbcacheApis method put_post.

/**
 * put_post post/put single item
 *
 * To update a key with partial data based on the key and/or query string.
 * It returns immediately, and asynchronously updates to redis and database
 *
 * @param request HttpServletRequest
 * @param key String, hash key
 * @param opt1 String, can be expire or table or "sync" or "async"
 * @param opt2 String, can be expire or table or "sync" or "async", but not otp1
 * @param opt3 String, can be expire or table or "sync" or "async", but not otp1 and opt2
 * @return ResponseEntity
 */
@RequestMapping(value = { "/rdbcache/v1/put/{key}", "/rdbcache/v1/put/{key}/{opt1}", "/rdbcache/v1/put/{key}/{opt1}/{opt2}", "/rdbcache/v1/put/{key}/{opt1}/{opt2}/{opt3}" }, method = { RequestMethod.POST, RequestMethod.PUT })
public ResponseEntity<?> put_post(HttpServletRequest request, @PathVariable("key") String key, @PathVariable Optional<String> opt1, @PathVariable Optional<String> opt2, @PathVariable Optional<String> opt3, @RequestBody String value) {
    if (value == null || value.length() == 0) {
        throw new BadRequestException("missing request body");
    }
    Context context = new Context();
    KvPairs pairs = new KvPairs(key, value);
    AnyKey anyKey = Request.process(context, request, pairs, opt1, opt2, opt3);
    LOGGER.trace(anyKey.print() + " pairs(" + pairs.size() + "): " + pairs.printKey());
    KeyInfo keyInfo = anyKey.getKeyInfo();
    if (key.equals("*") && keyInfo.getQuery() == null) {
        AppCtx.getAsyncOps().doSaveToRedisAndDbase(context, pairs, anyKey);
    } else {
        AppCtx.getAsyncOps().doPutOperation(context, pairs, anyKey);
    }
    return Response.send(context, pairs);
}
Also used : Context(doitincloud.rdbcache.supports.Context) AnyKey(doitincloud.rdbcache.supports.AnyKey) KeyInfo(doitincloud.rdbcache.models.KeyInfo) BadRequestException(doitincloud.commons.exceptions.BadRequestException) KvPairs(doitincloud.rdbcache.supports.KvPairs)

Example 15 with KvPairs

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

the class RdbcacheApis method set_post.

/**
 * set_post post single item
 *
 * To set a value to a key based on the key and/or query string.
 * It returns immediately, and asynchronously saves to redis and database
 *
 * @param request HttpServletRequest
 * @param key String, hash key
 * @param opt1 String, can be expire or table or "sync" or "async"
 * @param opt2 String, can be expire or table or "sync" or "async", but not otp1
 * @param opt3 String, can be expire or table or "sync" or "async", but not otp1 and opt2
 * @return ResponseEntity
 */
@RequestMapping(value = { "/rdbcache/v1/set/{key}", "/rdbcache/v1/set/{key}/{opt1}", "/rdbcache/v1/set/{key}/{opt1}/{opt2}", "/rdbcache/v1/set/{key}/{opt1}/{opt2}/{opt3}" }, method = RequestMethod.POST)
public ResponseEntity<?> set_post(HttpServletRequest request, @PathVariable("key") String key, @PathVariable Optional<String> opt1, @PathVariable Optional<String> opt2, @PathVariable Optional<String> opt3, @RequestBody String value) {
    if (value == null || value.length() == 0) {
        throw new BadRequestException("missing request body");
    }
    Context context = new Context();
    KvPairs pairs = new KvPairs(key, value);
    AnyKey anyKey = Request.process(context, request, pairs, opt1, opt2, opt3);
    LOGGER.trace(anyKey.print() + " pairs(" + pairs.size() + "): " + pairs.printKey());
    AppCtx.getAsyncOps().doSaveToRedisAndDbase(context, pairs, anyKey);
    return Response.send(context, pairs);
}
Also used : Context(doitincloud.rdbcache.supports.Context) AnyKey(doitincloud.rdbcache.supports.AnyKey) BadRequestException(doitincloud.commons.exceptions.BadRequestException) 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