Search in sources :

Example 36 with KvPair

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

the class RdbcacheApis method getset_post.

/**
 * getset_post post single item
 *
 * To get current value of a key and update it to a new value based on key and/or query string.
 * It finds the current value and 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/getset/{key}", "/rdbcache/v1/getset/{key}/{opt1}", "/rdbcache/v1/getset/{key}/{opt1}/{opt2}", "/rdbcache/v1/getset/{key}/{opt1}/{opt2}/{opt3}" }, method = RequestMethod.POST)
public ResponseEntity<?> getset_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(true);
    KvPairs pairs = new KvPairs(key, value);
    AnyKey anyKey = Request.process(context, request, pairs, opt1, opt2);
    LOGGER.trace(anyKey.print() + " pairs(" + pairs.size() + "): " + pairs.printKey());
    KvPairs pairsClone = pairs.clone();
    KvPair pair = pairs.getPair();
    if (key.equals("*")) {
        if (!AppCtx.getDbaseRepo().find(context, pairs, anyKey)) {
            pair.clearData();
        }
        AppCtx.getAsyncOps().doSaveToRedisAndDbase(context, pairsClone, anyKey);
    } else if (AppCtx.getRedisRepo().findAndSave(context, pairs, anyKey)) {
        AppCtx.getAsyncOps().doSaveToDbase(context, pairsClone, anyKey);
    } else {
        if (!AppCtx.getDbaseRepo().find(context, pairs, anyKey)) {
            pair.clearData();
        }
        AppCtx.getAsyncOps().doSaveToDbase(context, pairsClone, anyKey);
    }
    return Response.send(context, pairs);
}
Also used : Context(doitincloud.rdbcache.supports.Context) AnyKey(doitincloud.rdbcache.supports.AnyKey) KvPair(doitincloud.rdbcache.models.KvPair) BadRequestException(doitincloud.commons.exceptions.BadRequestException) KvPairs(doitincloud.rdbcache.supports.KvPairs)

Example 37 with KvPair

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

the class RdbcacheApisTest method delall_get1.

@Test
public void delall_get1() {
    try {
        Map<String, Object> data1 = null, data2 = null;
        {
            RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/rdbcache/v1/select/user_table?email=mike@example.com").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(1, data1.size());
        }
        String key = (String) data1.keySet().toArray()[0];
        {
            RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/rdbcache/v1/delall/" + key + "/user_table").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");
            assertNull(data1);
        }
        // allow time to synchronize data
        try {
            Thread.sleep(250);
        } catch (InterruptedException ie) {
            Thread.currentThread().interrupt();
        }
        assertFalse(AppCtx.getCacheOps().containsKey(key));
        Map<String, Object> redis = MockRedis.getData();
        // System.out.println(Utils.toJsonMap(redis));
        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);
        {
            RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/rdbcache/v1/get/*/user_table?id=1").accept(MediaType.APPLICATION_JSON);
            ResultActions actions = mockMvc.perform(requestBuilder);
            MvcResult result = actions.andReturn();
            MockHttpServletResponse response = result.getResponse();
            assertEquals(404, response.getStatus());
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail("caught an exception");
    }
}
Also used : KvIdType(doitincloud.rdbcache.models.KvIdType) RequestBuilder(org.springframework.test.web.servlet.RequestBuilder) KvPair(doitincloud.rdbcache.models.KvPair) ResultActions(org.springframework.test.web.servlet.ResultActions) MvcResult(org.springframework.test.web.servlet.MvcResult) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)

Example 38 with KvPair

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

the class RdbcacheApisTest method delkey_get1.

@Test
public void delkey_get1() {
    try {
        Map<String, Object> data1 = null, data2 = null;
        {
            RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/rdbcache/v1/select/user_table?limit=1").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(1, data1.size());
        }
        String key = (String) data1.keySet().toArray()[0];
        {
            RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/rdbcache/v1/delkey/" + key + "/user_table").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");
            assertNull(data1);
        }
        // allow time to synchronize data
        try {
            Thread.sleep(250);
        } catch (InterruptedException ie) {
            Thread.currentThread().interrupt();
        }
        assertFalse(AppCtx.getCacheOps().containsKey(key));
        Map<String, Object> redis = MockRedis.getData();
        // System.out.println(Utils.toJsonMap(redis));
        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 : KvIdType(doitincloud.rdbcache.models.KvIdType) RequestBuilder(org.springframework.test.web.servlet.RequestBuilder) KvPair(doitincloud.rdbcache.models.KvPair) ResultActions(org.springframework.test.web.servlet.ResultActions) MvcResult(org.springframework.test.web.servlet.MvcResult) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)

Example 39 with KvPair

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

the class RdbcacheApisTest method delall_post1.

@Test
public void delall_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/delall/user_table/sync").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"));
        }
        Map<String, Object> redis = MockRedis.getData();
        // System.out.println(Utils.toJsonMap(redis));
        int i = 1;
        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");
            // System.out.println(Utils.toJsonMap(rdchkeys));
            assertFalse(rdchkeys.containsKey(key));
            KvPair pair = AppCtx.getKvPairRepo().findById(new KvIdType(key, "keyInfo"));
            assertNull(pair);
            {
                RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/rdbcache/v1/get/*/user_table?id=" + i++).accept(MediaType.APPLICATION_JSON);
                ResultActions actions = mockMvc.perform(requestBuilder);
                MvcResult result = actions.andReturn();
                MockHttpServletResponse response = result.getResponse();
                assertEquals(404, response.getStatus());
            }
        }
    } 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 40 with KvPair

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

the class AsyncOps method deleteKvPairKeyInfo.

public void deleteKvPairKeyInfo(Context context, KvPair pair, KeyInfo keyInfo) {
    String kvType = "keyInfo";
    String type = pair.getType();
    if (!type.equals("data")) {
        kvType += ":" + type;
    }
    KvPair toDeletePair = new KvPair(pair.getId(), kvType);
    StopWatch stopWatch = context.startStopWatch("dbase", "KvPairRepo.delete");
    AppCtx.getKvPairRepo().delete(toDeletePair);
    if (stopWatch != null)
        stopWatch.stopNow();
}
Also used : KvPair(doitincloud.rdbcache.models.KvPair) 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