taoensso / carmine

Redis client + message queue for Clojure
https://www.taoensso.com/carmine
Eclipse Public License 1.0
1.15k stars 131 forks source link

Code example for Scan function #139

Closed bcambel closed 8 years ago

bcambel commented 8 years ago

It would be so great to have an example of the scan function ? Any examples you can point me out ? Thanks in advance

ptaoussanis commented 8 years ago

Hi Bahadir,

There's an in-depth example at http://redis.io/commands/scan - is that helpful?

bcambel commented 8 years ago

Thanks for the link, that was what I was reading to figure out as well and come up with the following code.

(loop [ idx 0 curs 0 dataset []] 
  (let [[curs data] (wcar* (car/scan curs))] 
    (info curs) 
    (if-not (< idx 10)
      (into dataset data)
      (recur (inc idx) curs (into dataset data)))))

which helped to fetch the keys continuously.

But my initial problem with the call was while I was trying to search via a pattern;

(wcar* 
  (car/scan 0 "*booking*" ))

returns an Exception ; ExceptionInfo ERR syntax error clojure.core/ex-info

normally the following Redis command scan 0 MATCH *booking* returns matching results.

I am using Redis 2.8.19 and Carmine [com.taoensso/carmine "2.11.1"]

Thanks Peter for your time!

ptaoussanis commented 8 years ago

I haven't used the scan API myself, but skimming the docs + that error it looks like your syntax might be off here?:

(wcar* (car/scan 0 "*booking*" )) ; => ExceptionInfo ERR syntax error clojure.core/ex-info

Docs say the syntax is SCAN cursor [MATCH pattern] [COUNT count] i.e. The valid arities appear to be: 1, 3 or 5 but you've got arity 2.

Again, have never used this - but I'd start by confirming that you've got the right call.

bcambel commented 8 years ago

Count is optional and not reliable. The default is 10; if you pass COUNT 1, it might return 2 elements.

The problem might be that Redis expects the MATCH and COUNT words as an argument as well where carmine only passes the values ?

ptaoussanis commented 8 years ago

In (wcar* {} (car/scan 0 "*booking*" )) you're calling scan with 2 arguments; that's not a valid arity. You need to call it with 1 argument (wcar* {} (car/scan 0)), 3 (with :match+pattern or :count+count), or 5 (both :match+pattern AND :count+count).

(wcar* {} (car/scan 0 "*booking*" )) will throw, because it's an invalid Redis call (arity 2). (wcar* {} (car/scan 0)) will return a result (arity 1). (wcar* {} (car/scan 0 :match "booking*")) will return a result (arity 3). (wcar* {} (car/scan 0 :count 3)) will return a result (arity 3). (wcar* {} (car/scan 0 :match "booking*" :count 3)) will return a result (arity 5).

Does that make sense?

bcambel commented 8 years ago

Well that makes sense. Thanks Peter!

On Thu, Aug 6, 2015 at 4:14 PM, Peter Taoussanis notifications@github.com wrote:

In (wcar* {} (car/scan 0 "booking" )) you're calling scan with 2 arguments; that's not a valid arity. You need to call it with 1 argument (wcar* {} (car/scan 0)), 3 (with :match+pattern or :count+count), or 5 (both :match+pattern AND :count+count).

(wcar* {} (car/scan 0 "booking" )) will throw, because it's an invalid Redis call (arity 2). (wcar* {} (car/scan 0)) will return a result (arity 1). (wcar* {} (car/scan 0 :match "booking")) will return a result (arity 3). (wcar {} (car/scan 0 :count 3)) will return a result (arity 3). (wcar* {} (car/scan 0 :match "booking*" :count 3)) will return a result (arity 5).

Does that make sense?

— Reply to this email directly or view it on GitHub https://github.com/ptaoussanis/carmine/issues/139#issuecomment-128383015 .

Regards , Bahadir Cambel

ptaoussanis commented 8 years ago

Great, no problem! Feel free to ping if you run into any other issues, cheers! :-)