package com.lithium.dbi.rdbi.recipes.queue;

import com.lithium.dbi.rdbi.BindArg;
import com.lithium.dbi.rdbi.BindKey;
import com.lithium.dbi.rdbi.Query;

public interface RedisCircularBufferDAO {

    @Query(
            "local size = redis.call('RPUSH', $key$, $valueToAdd$)\n" +
                    "if size > tonumber($maxSize$) then\n" +
                    "   redis.call('LPOP', $key$)\n" +
                    "   size = size - 1\n" +
                    "end\n" +
                    "return size\n"
    )
    int add(
            @BindKey("key") String key,
            @BindArg("valueToAdd") String valueToAdd,
            @BindArg("maxSize") Integer maxSize);

}

@@ -332,24 +332,164 @@
 ) then%5Cn%22 +%0A
+                    %22   local start = size - tonumber($maxSize$)%5Cn%22 +%0A                    %22   local stop = start + tonumber($maxSize$)%5Cn%22 +%0A
             
@@ -517,19 +517,20 @@
 l('L
-POP
+TRIM
 ', $key$
 )%5Cn%22
@@ -525,16 +525,29 @@
 ', $key$
+, start, stop
 )%5Cn%22 +%0A 

trim instead of pop for circular add