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



##########
File path: 
geode-for-redis/src/main/java/org/apache/geode/redis/internal/data/RedisList.java
##########
@@ -252,6 +253,56 @@ public void lset(Region<RedisKey, RedisData> region, 
RedisKey key, int index, by
     storeChanges(region, key, new ReplaceByteArrayAtOffset(index, value));
   }
 
+  /**
+   * @param start the index of the first element to retain
+   * @param end the index of the last element to retain
+   * @param region the region this instance is stored in
+   * @param key the name of the list to pop from
+   */
+  public Void ltrim(int start, int end, Region<RedisKey, RedisData> region,
+      RedisKey key) {
+    int length = elementList.size();
+    int boundedStart = getBoundedStartIndex(start, length);
+    int boundedEnd = getBoundedEndIndex(end, length);
+
+    if (boundedStart > boundedEnd || boundedStart == length) {
+      // Remove everything
+      region.remove(key);
+      return null;
+    }
+
+    if (boundedStart == 0 && boundedEnd == length) {
+      // No-op, return without modifying the list
+      return null;
+    }
+
+    RetainElementsByIndexRange retainElementsByRange;
+    synchronized (this) {
+      elementsRetainByIndexRange(boundedStart, boundedEnd);
+
+      retainElementsByRange =
+          new RetainElementsByIndexRange(incrementAndGetVersion(), 
boundedStart, boundedEnd);
+    }
+    storeChanges(region, key, retainElementsByRange);
+    return null;
+  }
+
+  private int getBoundedStartIndex(long index, int size) {
+    if (index >= 0L) {
+      return (int) Math.min(index, size);
+    } else {
+      return (int) Math.max(index + size, 0);
+    }
+  }
+
+  private int getBoundedEndIndex(long index, int size) {

Review comment:
       Tweaked.




-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to