ringles commented on a change in pull request #7403:
URL: https://github.com/apache/geode/pull/7403#discussion_r832338602



##########
File path: 
geode-for-redis/src/main/java/org/apache/geode/redis/internal/data/RedisList.java
##########
@@ -127,50 +127,139 @@ private int getArrayIndex(int listIndex) {
   }
 
   /**
-   * @param elementsToAdd elements to add to this set; NOTE this list may by 
modified by this call
-   * @param region the region this instance is stored in
-   * @param key the name of the set to add to
-   * @param onlyIfExists if true then the elements should only be added if the 
key already exists
-   *        and holds a list, otherwise no operation is performed.
-   * @return the length of the list after the operation
-   */
-  public long lpush(List<byte[]> elementsToAdd, Region<RedisKey, RedisData> 
region, RedisKey key,
-      final boolean onlyIfExists) {
-    elementsPush(elementsToAdd);
-    storeChanges(region, key, new AddByteArrays(elementsToAdd));
+   * @return the number of elements in the list
+   **/
+  public int llen() {
     return elementList.size();
   }
 
   /**
    * @param region the region this instance is stored in
-   * @param key the name of the set to add to
+   * @param key the name of the list to add to
    * @return the element actually popped
    */
   public byte[] lpop(Region<RedisKey, RedisData> region, RedisKey key) {
-    byte[] popped = elementRemove(0);
-    RemoveElementsByIndex removed = new RemoveElementsByIndex();
-    removed.add(0);
+    byte newVersion;
+    byte[] popped;
+    RemoveElementsByIndex removed;
+    synchronized (this) {
+      newVersion = incrementAndGetVersion();
+      popped = removeFirstElement();
+      removed = new RemoveElementsByIndex(newVersion);
+      removed.add(0);
+    }
     storeChanges(region, key, removed);
     return popped;
   }
 
+  public synchronized byte[] removeFirstElement() {
+    return elementList.removeFirst();
+  }
+
+  public synchronized byte[] removeLastElement() {
+    return elementList.removeLast();
+  }
+
   /**
-   * @return the number of elements in the list
+   * @param elementsToAdd elements to add to this list; NOTE this list may be 
modified by this call
+   * @param region the region this instance is stored in
+   * @param key the name of the list to add to
+   * @param onlyIfExists if true then the elements should only be added if the 
key already exists
+   *        and holds a list, otherwise no operation is performed.
+   * @return the length of the list after the operation
    */
-  public int llen() {
+  public long lpush(List<byte[]> elementsToAdd, Region<RedisKey, RedisData> 
region, RedisKey key,
+      final boolean onlyIfExists) {
+    byte newVersion;
+    AddByteArrays addByteArrays;
+    synchronized (this) {
+      newVersion = incrementAndGetVersion();
+      elementsPush(elementsToAdd);
+      addByteArrays = new AddByteArrays(newVersion, elementsToAdd);
+    }
+    storeChanges(region, key, addByteArrays);
     return elementList.size();
   }
 
+  /**
+   * @param elementsToAdd elements to add to this list; NOTE this list may be 
modified by this call
+   * @param region the region this instance is stored in
+   * @param key the name of the list to add to
+   * @return the number of elements actually added
+   */
+  public long lpush(List<byte[]> elementsToAdd, Region<RedisKey, RedisData> 
region, RedisKey key) {
+    byte newVersion;
+    AddByteArrays addByteArrays;
+    synchronized (this) {
+      newVersion = incrementAndGetVersion();
+      elementsPush(elementsToAdd);
+      addByteArrays = new AddByteArrays(newVersion, elementsToAdd);
+    }
+    storeChanges(region, key, addByteArrays);
+    return elementList.size();
+  }
+
+  public byte[] ltrim(long start, long end, Region<RedisKey, RedisData> region,
+      RedisKey key) {
+    byte newVersion;
+    int length = elementList.size();
+    int boundedStart = getBoundedStartIndex(start, length);
+    int boundedEnd = getBoundedEndIndex(end, length);
+    List<Integer> removed = new ArrayList<>();
+    RemoveElementsByIndex removeElementsByIndex;
+
+    synchronized (this) {
+      if (boundedStart > boundedEnd || boundedStart == length) {
+        // Remove everything
+        for (int i = length - 1; i >= 0; i--) {
+          removed.add(i);
+        }
+      } else {
+        // Remove any elements after boundedEnd
+        for (int i = length - 1; i > boundedEnd; i--) {
+          removed.add(i);
+        }
+
+        // Remove any elements before boundedStart
+        for (int i = boundedStart - 1; i >= 0; i--) {
+          removed.add(i);
+        }
+      }
+
+      if (removed.size() > 0) {
+        elementsRemove(removed);
+      }
+      newVersion = incrementAndGetVersion();
+      removeElementsByIndex = new RemoveElementsByIndex(newVersion, removed);
+    }
+    storeChanges(region, key, removeElementsByIndex);
+    return null;

Review comment:
       listLockedExecuted wants a return value, it doesn't take void(). I 
suppose we could add one like that...




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@geode.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to