From 01f83faa26c78aa8aaff1200cabcb746628a2bdd Mon Sep 17 00:00:00 2001 From: brido4125 Date: Wed, 28 Jun 2023 12:54:06 +0900 Subject: [PATCH] FIX: Element sequence when using asyncLopInsert api. --- .../collection/CollectionPipedInsert.java | 6 ++- .../bulkoperation/PipeInsertTest.java | 51 +++++++++++++++++++ .../PipedBulkInsertListWithAttrTest.java | 6 +-- 3 files changed, 59 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/spy/memcached/collection/CollectionPipedInsert.java b/src/main/java/net/spy/memcached/collection/CollectionPipedInsert.java index d83590240..91db6ddcf 100644 --- a/src/main/java/net/spy/memcached/collection/CollectionPipedInsert.java +++ b/src/main/java/net/spy/memcached/collection/CollectionPipedInsert.java @@ -90,7 +90,11 @@ public ByteBuffer getAsciiCommand() { CollectionCreate.makeCreateClause(attribute, cd.getFlags()) : ""; for (int i = this.nextOpIndex; i < eSize; i++) { byte[] each = encodedList.get(i); - setArguments(bb, COMMAND, key, index, each.length, + int offset = i; + if (index < 0) { + offset = 0; + } + setArguments(bb, COMMAND, key, index + offset, each.length, createOption, (i < eSize - 1) ? PIPE : ""); bb.put(each); bb.put(CRLF); diff --git a/src/test/manual/net/spy/memcached/bulkoperation/PipeInsertTest.java b/src/test/manual/net/spy/memcached/bulkoperation/PipeInsertTest.java index cb7a345ec..445c6a9c8 100644 --- a/src/test/manual/net/spy/memcached/bulkoperation/PipeInsertTest.java +++ b/src/test/manual/net/spy/memcached/bulkoperation/PipeInsertTest.java @@ -147,6 +147,57 @@ public void testLopPipeInsert() { } } + public void testLopPipeInsertIndex() { + int elementCount = 3; + List middleElements = new ArrayList(elementCount); + List headerElements = new ArrayList(elementCount); + List footerElements = new ArrayList(elementCount); + + for (int i = 0; i < elementCount; i++) { + middleElements.add("middleValue" + i); + headerElements.add("headerValue" + i); + footerElements.add("footerValue" + i); + } + + try { + CollectionAttributes attr = new CollectionAttributes(); + + mc.asyncLopInsert(KEY, 0, "FirstValue", attr).get(5000L, TimeUnit.MILLISECONDS); + mc.asyncLopInsert(KEY, -1, "LastValue", attr).get(5000L, TimeUnit.MILLISECONDS); + + CollectionFuture> future1 = + mc.asyncLopPipedInsertBulk(KEY, 1, middleElements, attr); + CollectionFuture> future2 = + mc.asyncLopPipedInsertBulk(KEY, 0, headerElements, attr); + CollectionFuture> future3 = + mc.asyncLopPipedInsertBulk(KEY, -1, footerElements, attr); + + + Map map1 + = future1.get(5000L, TimeUnit.MILLISECONDS); + Map map2 + = future2.get(5000L, TimeUnit.MILLISECONDS); + Map map3 + = future3.get(5000L, TimeUnit.MILLISECONDS); + + + Assert.assertEquals(map1.size() + map2.size() + map3.size(), 0); + + List list = mc.asyncLopGet(KEY, 0, 9999, false, false).get(); + + System.out.println("list = " + list); + + Assert.assertEquals((elementCount * 3) + 2, list.size()); + + Assert.assertEquals(headerElements, list.subList(0, elementCount)); + Assert.assertEquals(middleElements, list.subList(elementCount + 1, elementCount * 2 + 1)); + Assert.assertEquals(footerElements, list.subList(elementCount * 3 - 1, elementCount * 3 + 2)); + } catch (Exception e) { + e.printStackTrace(); + Assert.fail(e.getMessage()); + } + } + public void testMopPipeInsert() { int elementCount = 5000; diff --git a/src/test/manual/net/spy/memcached/emptycollection/PipedBulkInsertListWithAttrTest.java b/src/test/manual/net/spy/memcached/emptycollection/PipedBulkInsertListWithAttrTest.java index 9313fe4c4..469e1a9d5 100644 --- a/src/test/manual/net/spy/memcached/emptycollection/PipedBulkInsertListWithAttrTest.java +++ b/src/test/manual/net/spy/memcached/emptycollection/PipedBulkInsertListWithAttrTest.java @@ -74,7 +74,7 @@ public void testInsertWithAttribute() { // check values List list2 = mc.asyncLopGet(KEY, 0, 10, false, false).get(); for (int i = 0; i < list2.size(); i++) { - Assert.assertEquals(10 - i, list2.get(i)); + Assert.assertEquals(i + 1, list2.get(i)); } // check expire time @@ -114,7 +114,7 @@ public void testInsertWithDefaultAttribute() { // check values List list2 = mc.asyncLopGet(KEY, 0, 10, false, false).get(); for (int i = 0; i < list2.size(); i++) { - Assert.assertEquals(10 - i, list2.get(i)); + Assert.assertEquals(i + 1, list2.get(i)); } } catch (Exception e) { e.printStackTrace(); @@ -147,7 +147,7 @@ public void testInsertWithoutAttributeCreate() { // check values List list2 = mc.asyncLopGet(KEY, 0, 10, false, false).get(); for (int i = 0; i < list2.size(); i++) { - Assert.assertEquals(10 - i, list2.get(i)); + Assert.assertEquals(i + 1, list2.get(i)); } } catch (Exception e) { e.printStackTrace();