diff --git a/.gitignore b/.gitignore index e917613..982e6b6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,11 @@ .classpath +*.iml +*.ipr +*.iws .project .settings/ .gradle/ target/ build/ +bin/ +tags diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..00f29bf --- /dev/null +++ b/Makefile @@ -0,0 +1,42 @@ +define REDIS1_CONF +daemonize yes +port 6379 +requirepass foobared +pidfile /tmp/redis1.pid +endef + +define REDIS2_CONF +daemonize yes +port 6380 +requirepass foobared +pidfile /tmp/redis2.pid +endef + + +define REDIS_SENTINEL1 +port 26379 +daemonize yes +sentinel monitor mymaster 127.0.0.1 6379 2 +sentinel auth-pass mymaster foobared +sentinel down-after-milliseconds mymaster 5000 +sentinel failover-timeout mymaster 900000 +sentinel can-failover mymaster yes +sentinel parallel-syncs mymaster 1 +pidfile /tmp/sentinel1.pid +endef + +export REDIS1_CONF +export REDIS2_CONF +export REDIS_SENTINEL1 +test: + echo "$$REDIS1_CONF" | redis-server - + echo "$$REDIS2_CONF" | redis-server - + echo "$$REDIS_SENTINEL1" | redis-sentinel - + + mvn clean compile test + + kill `cat /tmp/redis1.pid` + kill `cat /tmp/redis2.pid` + kill `cat /tmp/sentinel1.pid` + +.PHONY: test diff --git a/README.md b/README.md index 558111b..ae89c9f 100644 --- a/README.md +++ b/README.md @@ -48,20 +48,23 @@ You can download the latest build at: Or use it as a maven dependency: - - redis.clients - jedis - 2.0.0 - jar - compile - - +```xml + + redis.clients + jedis + 2.0.0 + jar + compile + +``` To use it just: - Jedis jedis = new Jedis("localhost"); - jedis.set("foo", "bar"); - String value = jedis.get("foo"); +```java +Jedis jedis = new Jedis("localhost"); +jedis.set("foo", "bar"); +String value = jedis.get("foo"); +``` For more usage examples check the tests. @@ -77,7 +80,7 @@ To run the tests: - Use the latest redis master branch. -- Run 2 instances of redis [using conf files in conf folder](https://github.com/xetorthio/jedis/wiki). For the tests we use 2 redis servers, one on default port (6379) and the other one on (6380). Both have authentication enabled with default password (foobared). This way we can test both sharding and auth command. +- Run ```make test```. This will run 2 instances of redis. We use 2 redis servers, one on default port (6379) and the other one on (6380). Both have authentication enabled with default password (foobared). This way we can test both sharding and auth command. Thanks for helping! diff --git a/build.gradle b/build.gradle index a956f7e..e9076f3 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,6 @@ apply plugin: 'java' apply plugin: 'maven' +apply plugin: 'eclipse' group = 'com.googlecode.jedis' archiveBaseName = 'jedis' @@ -25,3 +26,8 @@ uploadArchives { } } */ + +task createWrapper(type: Wrapper) { + gradleVersion = '1.0-milestone-6' +} + diff --git a/conf/redis.conf b/conf/redis.conf deleted file mode 100644 index eceedb6..0000000 --- a/conf/redis.conf +++ /dev/null @@ -1,332 +0,0 @@ -# Redis configuration file example - -# Note on units: when memory size is needed, it is possible to specifiy -# it in the usual form of 1k 5GB 4M and so forth: -# -# 1k => 1000 bytes -# 1kb => 1024 bytes -# 1m => 1000000 bytes -# 1mb => 1024*1024 bytes -# 1g => 1000000000 bytes -# 1gb => 1024*1024*1024 bytes -# -# units are case insensitive so 1GB 1Gb 1gB are all the same. - -# By default Redis does not run as a daemon. Use 'yes' if you need it. -# Note that Redis will write a pid file in /var/run/redis.pid when daemonized. -daemonize no - -# When running daemonized, Redis writes a pid file in /var/run/redis.pid by -# default. You can specify a custom pid file location here. -pidfile /var/run/redis.pid - -# Accept connections on the specified port, default is 6379 -port 6379 - -# If you want you can bind a single interface, if the bind option is not -# specified all the interfaces will listen for incoming connections. -# -# bind 127.0.0.1 - -# Close the connection after a client is idle for N seconds (0 to disable) -timeout 300 - -# Set server verbosity to 'debug' -# it can be one of: -# debug (a lot of information, useful for development/testing) -# verbose (many rarely useful info, but not a mess like the debug level) -# notice (moderately verbose, what you want in production probably) -# warning (only very important / critical messages are logged) -loglevel verbose - -# Specify the log file name. Also 'stdout' can be used to force -# Redis to log on the standard output. Note that if you use standard -# output for logging but daemonize, logs will be sent to /dev/null -logfile stdout - -# Set the number of databases. The default database is DB 0, you can select -# a different one on a per-connection basis using SELECT where -# dbid is a number between 0 and 'databases'-1 -databases 16 - -################################ SNAPSHOTTING ################################# -# -# Save the DB on disk: -# -# save -# -# Will save the DB if both the given number of seconds and the given -# number of write operations against the DB occurred. -# -# In the example below the behaviour will be to save: -# after 900 sec (15 min) if at least 1 key changed -# after 300 sec (5 min) if at least 10 keys changed -# after 60 sec if at least 10000 keys changed -# -# Note: you can disable saving at all commenting all the "save" lines. - -save 900 1 -save 300 10 -save 60 10000 - -# Compress string objects using LZF when dump .rdb databases? -# For default that's set to 'yes' as it's almost always a win. -# If you want to save some CPU in the saving child set it to 'no' but -# the dataset will likely be bigger if you have compressible values or keys. -rdbcompression yes - -# The filename where to dump the DB -dbfilename dump.rdb - -# The working directory. -# -# The DB will be written inside this directory, with the filename specified -# above using the 'dbfilename' configuration directive. -# -# Also the Append Only File will be created inside this directory. -# -# Note that you must specify a directory here, not a file name. -dir ./ - -################################# REPLICATION ################################# - -# Master-Slave replication. Use slaveof to make a Redis instance a copy of -# another Redis server. Note that the configuration is local to the slave -# so for example it is possible to configure the slave to save the DB with a -# different interval, or to listen to another port, and so on. -# -# slaveof - -# If the master is password protected (using the "requirepass" configuration -# directive below) it is possible to tell the slave to authenticate before -# starting the replication synchronization process, otherwise the master will -# refuse the slave request. -# -# masterauth - -################################## SECURITY ################################### - -# Require clients to issue AUTH before processing any other -# commands. This might be useful in environments in which you do not trust -# others with access to the host running redis-server. -# -# This should stay commented out for backward compatibility and because most -# people do not need auth (e.g. they run their own servers). -# -# Warning: since Redis is pretty fast an outside user can try up to -# 150k passwords per second against a good box. This means that you should -# use a very strong password otherwise it will be very easy to break. -# -requirepass foobared - -################################### LIMITS #################################### - -# Set the max number of connected clients at the same time. By default there -# is no limit, and it's up to the number of file descriptors the Redis process -# is able to open. The special value '0' means no limits. -# Once the limit is reached Redis will close all the new connections sending -# an error 'max number of clients reached'. -# -# maxclients 128 - -# Don't use more memory than the specified amount of bytes. -# When the memory limit is reached Redis will try to remove keys with an -# EXPIRE set. It will try to start freeing keys that are going to expire -# in little time and preserve keys with a longer time to live. -# Redis will also try to remove objects from free lists if possible. -# -# If all this fails, Redis will start to reply with errors to commands -# that will use more memory, like SET, LPUSH, and so on, and will continue -# to reply to most read-only commands like GET. -# -# WARNING: maxmemory can be a good idea mainly if you want to use Redis as a -# 'state' server or cache, not as a real DB. When Redis is used as a real -# database the memory usage will grow over the weeks, it will be obvious if -# it is going to use too much memory in the long run, and you'll have the time -# to upgrade. With maxmemory after the limit is reached you'll start to get -# errors for write operations, and this may even lead to DB inconsistency. -# -# maxmemory - -############################## APPEND ONLY MODE ############################### - -# By default Redis asynchronously dumps the dataset on disk. If you can live -# with the idea that the latest records will be lost if something like a crash -# happens this is the preferred way to run Redis. If instead you care a lot -# about your data and don't want to that a single record can get lost you should -# enable the append only mode: when this mode is enabled Redis will append -# every write operation received in the file appendonly.aof. This file will -# be read on startup in order to rebuild the full dataset in memory. -# -# Note that you can have both the async dumps and the append only file if you -# like (you have to comment the "save" statements above to disable the dumps). -# Still if append only mode is enabled Redis will load the data from the -# log file at startup ignoring the dump.rdb file. -# -# IMPORTANT: Check the BGREWRITEAOF to check how to rewrite the append -# log file in background when it gets too big. - -appendonly no - -# The name of the append only file (default: "appendonly.aof") -# appendfilename appendonly.aof - -# The fsync() call tells the Operating System to actually write data on disk -# instead to wait for more data in the output buffer. Some OS will really flush -# data on disk, some other OS will just try to do it ASAP. -# -# Redis supports three different modes: -# -# no: don't fsync, just let the OS flush the data when it wants. Faster. -# always: fsync after every write to the append only log . Slow, Safest. -# everysec: fsync only if one second passed since the last fsync. Compromise. -# -# The default is "everysec" that's usually the right compromise between -# speed and data safety. It's up to you to understand if you can relax this to -# "no" that will will let the operating system flush the output buffer when -# it wants, for better performances (but if you can live with the idea of -# some data loss consider the default persistence mode that's snapshotting), -# or on the contrary, use "always" that's very slow but a bit safer than -# everysec. -# -# If unsure, use "everysec". - -# appendfsync always -appendfsync everysec -# appendfsync no - -# When the AOF fsync policy is set to always or everysec, and a background -# saving process (a background save or AOF log background rewriting) is -# performing a lot of I/O against the disk, in some Linux configurations -# Redis may block too long on the fsync() call. Note that there is no fix for -# this currently, as even performing fsync in a different thread will block -# our synchronous write(2) call. -# -# In order to mitigate this problem it's possible to use the following option -# that will prevent fsync() from being called in the main process while a -# BGSAVE or BGREWRITEAOF is in progress. -# -# This means that while another child is saving the durability of Redis is -# the same as "appendfsync none", that in pratical terms means that it is -# possible to lost up to 30 seconds of log in the worst scenario (with the -# default Linux settings). -# -# If you have latency problems turn this to "yes". Otherwise leave it as -# "no" that is the safest pick from the point of view of durability. -no-appendfsync-on-rewrite no - -################################ VIRTUAL MEMORY ############################### - -# Virtual Memory allows Redis to work with datasets bigger than the actual -# amount of RAM needed to hold the whole dataset in memory. -# In order to do so very used keys are taken in memory while the other keys -# are swapped into a swap file, similarly to what operating systems do -# with memory pages. -# -# To enable VM just set 'vm-enabled' to yes, and set the following three -# VM parameters accordingly to your needs. - -vm-enabled no -# vm-enabled yes - -# This is the path of the Redis swap file. As you can guess, swap files -# can't be shared by different Redis instances, so make sure to use a swap -# file for every redis process you are running. Redis will complain if the -# swap file is already in use. -# -# The best kind of storage for the Redis swap file (that's accessed at random) -# is a Solid State Disk (SSD). -# -# *** WARNING *** if you are using a shared hosting the default of putting -# the swap file under /tmp is not secure. Create a dir with access granted -# only to Redis user and configure Redis to create the swap file there. -vm-swap-file /tmp/redis.swap - -# vm-max-memory configures the VM to use at max the specified amount of -# RAM. Everything that deos not fit will be swapped on disk *if* possible, that -# is, if there is still enough contiguous space in the swap file. -# -# With vm-max-memory 0 the system will swap everything it can. Not a good -# default, just specify the max amount of RAM you can in bytes, but it's -# better to leave some margin. For instance specify an amount of RAM -# that's more or less between 60 and 80% of your free RAM. -vm-max-memory 0 - -# Redis swap files is split into pages. An object can be saved using multiple -# contiguous pages, but pages can't be shared between different objects. -# So if your page is too big, small objects swapped out on disk will waste -# a lot of space. If you page is too small, there is less space in the swap -# file (assuming you configured the same number of total swap file pages). -# -# If you use a lot of small objects, use a page size of 64 or 32 bytes. -# If you use a lot of big objects, use a bigger page size. -# If unsure, use the default :) -vm-page-size 32 - -# Number of total memory pages in the swap file. -# Given that the page table (a bitmap of free/used pages) is taken in memory, -# every 8 pages on disk will consume 1 byte of RAM. -# -# The total swap size is vm-page-size * vm-pages -# -# With the default of 32-bytes memory pages and 134217728 pages Redis will -# use a 4 GB swap file, that will use 16 MB of RAM for the page table. -# -# It's better to use the smallest acceptable value for your application, -# but the default is large in order to work in most conditions. -vm-pages 134217728 - -# Max number of VM I/O threads running at the same time. -# This threads are used to read/write data from/to swap file, since they -# also encode and decode objects from disk to memory or the reverse, a bigger -# number of threads can help with big objects even if they can't help with -# I/O itself as the physical device may not be able to couple with many -# reads/writes operations at the same time. -# -# The special value of 0 turn off threaded I/O and enables the blocking -# Virtual Memory implementation. -vm-max-threads 4 - -############################### ADVANCED CONFIG ############################### - -# Glue small output buffers together in order to send small replies in a -# single TCP packet. Uses a bit more CPU but most of the times it is a win -# in terms of number of queries per second. Use 'yes' if unsure. -glueoutputbuf yes - -# Hashes are encoded in a special way (much more memory efficient) when they -# have at max a given numer of elements, and the biggest element does not -# exceed a given threshold. You can configure this limits with the following -# configuration directives. -hash-max-zipmap-entries 64 -hash-max-zipmap-value 512 - -# Active rehashing uses 1 millisecond every 100 milliseconds of CPU time in -# order to help rehashing the main Redis hash table (the one mapping top-level -# keys to values). The hash table implementation redis uses (see dict.c) -# performs a lazy rehashing: the more operation you run into an hash table -# that is rhashing, the more rehashing "steps" are performed, so if the -# server is idle the rehashing is never complete and some more memory is used -# by the hash table. -# -# The default is to use this millisecond 10 times every second in order to -# active rehashing the main dictionaries, freeing memory when possible. -# -# If unsure: -# use "activerehashing no" if you have hard latency requirements and it is -# not a good thing in your environment that Redis can reply form time to time -# to queries with 2 milliseconds delay. -# -# use "activerehashing yes" if you don't have such hard requirements but -# want to free memory asap when possible. -activerehashing yes - -################################## INCLUDES ################################### - -# Include one or more other config files here. This is useful if you -# have a standard template that goes to all redis server but also need -# to customize a few per-server settings. Include files can include -# other files, so use this wisely. -# -# include /path/to/local.conf -# include /path/to/other.conf diff --git a/conf/redis2.conf b/conf/redis2.conf deleted file mode 100644 index c59e989..0000000 --- a/conf/redis2.conf +++ /dev/null @@ -1,332 +0,0 @@ -# Redis configuration file example - -# Note on units: when memory size is needed, it is possible to specifiy -# it in the usual form of 1k 5GB 4M and so forth: -# -# 1k => 1000 bytes -# 1kb => 1024 bytes -# 1m => 1000000 bytes -# 1mb => 1024*1024 bytes -# 1g => 1000000000 bytes -# 1gb => 1024*1024*1024 bytes -# -# units are case insensitive so 1GB 1Gb 1gB are all the same. - -# By default Redis does not run as a daemon. Use 'yes' if you need it. -# Note that Redis will write a pid file in /var/run/redis.pid when daemonized. -daemonize no - -# When running daemonized, Redis writes a pid file in /var/run/redis.pid by -# default. You can specify a custom pid file location here. -pidfile /var/run/redis.pid - -# Accept connections on the specified port, default is 6379 -port 6380 - -# If you want you can bind a single interface, if the bind option is not -# specified all the interfaces will listen for incoming connections. -# -# bind 127.0.0.1 - -# Close the connection after a client is idle for N seconds (0 to disable) -timeout 300 - -# Set server verbosity to 'debug' -# it can be one of: -# debug (a lot of information, useful for development/testing) -# verbose (many rarely useful info, but not a mess like the debug level) -# notice (moderately verbose, what you want in production probably) -# warning (only very important / critical messages are logged) -loglevel verbose - -# Specify the log file name. Also 'stdout' can be used to force -# Redis to log on the standard output. Note that if you use standard -# output for logging but daemonize, logs will be sent to /dev/null -logfile stdout - -# Set the number of databases. The default database is DB 0, you can select -# a different one on a per-connection basis using SELECT where -# dbid is a number between 0 and 'databases'-1 -databases 16 - -################################ SNAPSHOTTING ################################# -# -# Save the DB on disk: -# -# save -# -# Will save the DB if both the given number of seconds and the given -# number of write operations against the DB occurred. -# -# In the example below the behaviour will be to save: -# after 900 sec (15 min) if at least 1 key changed -# after 300 sec (5 min) if at least 10 keys changed -# after 60 sec if at least 10000 keys changed -# -# Note: you can disable saving at all commenting all the "save" lines. - -save 900 1 -save 300 10 -save 60 10000 - -# Compress string objects using LZF when dump .rdb databases? -# For default that's set to 'yes' as it's almost always a win. -# If you want to save some CPU in the saving child set it to 'no' but -# the dataset will likely be bigger if you have compressible values or keys. -rdbcompression yes - -# The filename where to dump the DB -dbfilename dump.rdb - -# The working directory. -# -# The DB will be written inside this directory, with the filename specified -# above using the 'dbfilename' configuration directive. -# -# Also the Append Only File will be created inside this directory. -# -# Note that you must specify a directory here, not a file name. -dir ./ - -################################# REPLICATION ################################# - -# Master-Slave replication. Use slaveof to make a Redis instance a copy of -# another Redis server. Note that the configuration is local to the slave -# so for example it is possible to configure the slave to save the DB with a -# different interval, or to listen to another port, and so on. -# -# slaveof - -# If the master is password protected (using the "requirepass" configuration -# directive below) it is possible to tell the slave to authenticate before -# starting the replication synchronization process, otherwise the master will -# refuse the slave request. -# -# masterauth - -################################## SECURITY ################################### - -# Require clients to issue AUTH before processing any other -# commands. This might be useful in environments in which you do not trust -# others with access to the host running redis-server. -# -# This should stay commented out for backward compatibility and because most -# people do not need auth (e.g. they run their own servers). -# -# Warning: since Redis is pretty fast an outside user can try up to -# 150k passwords per second against a good box. This means that you should -# use a very strong password otherwise it will be very easy to break. -# -requirepass foobared - -################################### LIMITS #################################### - -# Set the max number of connected clients at the same time. By default there -# is no limit, and it's up to the number of file descriptors the Redis process -# is able to open. The special value '0' means no limits. -# Once the limit is reached Redis will close all the new connections sending -# an error 'max number of clients reached'. -# -# maxclients 128 - -# Don't use more memory than the specified amount of bytes. -# When the memory limit is reached Redis will try to remove keys with an -# EXPIRE set. It will try to start freeing keys that are going to expire -# in little time and preserve keys with a longer time to live. -# Redis will also try to remove objects from free lists if possible. -# -# If all this fails, Redis will start to reply with errors to commands -# that will use more memory, like SET, LPUSH, and so on, and will continue -# to reply to most read-only commands like GET. -# -# WARNING: maxmemory can be a good idea mainly if you want to use Redis as a -# 'state' server or cache, not as a real DB. When Redis is used as a real -# database the memory usage will grow over the weeks, it will be obvious if -# it is going to use too much memory in the long run, and you'll have the time -# to upgrade. With maxmemory after the limit is reached you'll start to get -# errors for write operations, and this may even lead to DB inconsistency. -# -# maxmemory - -############################## APPEND ONLY MODE ############################### - -# By default Redis asynchronously dumps the dataset on disk. If you can live -# with the idea that the latest records will be lost if something like a crash -# happens this is the preferred way to run Redis. If instead you care a lot -# about your data and don't want to that a single record can get lost you should -# enable the append only mode: when this mode is enabled Redis will append -# every write operation received in the file appendonly.aof. This file will -# be read on startup in order to rebuild the full dataset in memory. -# -# Note that you can have both the async dumps and the append only file if you -# like (you have to comment the "save" statements above to disable the dumps). -# Still if append only mode is enabled Redis will load the data from the -# log file at startup ignoring the dump.rdb file. -# -# IMPORTANT: Check the BGREWRITEAOF to check how to rewrite the append -# log file in background when it gets too big. - -appendonly no - -# The name of the append only file (default: "appendonly.aof") -# appendfilename appendonly.aof - -# The fsync() call tells the Operating System to actually write data on disk -# instead to wait for more data in the output buffer. Some OS will really flush -# data on disk, some other OS will just try to do it ASAP. -# -# Redis supports three different modes: -# -# no: don't fsync, just let the OS flush the data when it wants. Faster. -# always: fsync after every write to the append only log . Slow, Safest. -# everysec: fsync only if one second passed since the last fsync. Compromise. -# -# The default is "everysec" that's usually the right compromise between -# speed and data safety. It's up to you to understand if you can relax this to -# "no" that will will let the operating system flush the output buffer when -# it wants, for better performances (but if you can live with the idea of -# some data loss consider the default persistence mode that's snapshotting), -# or on the contrary, use "always" that's very slow but a bit safer than -# everysec. -# -# If unsure, use "everysec". - -# appendfsync always -appendfsync everysec -# appendfsync no - -# When the AOF fsync policy is set to always or everysec, and a background -# saving process (a background save or AOF log background rewriting) is -# performing a lot of I/O against the disk, in some Linux configurations -# Redis may block too long on the fsync() call. Note that there is no fix for -# this currently, as even performing fsync in a different thread will block -# our synchronous write(2) call. -# -# In order to mitigate this problem it's possible to use the following option -# that will prevent fsync() from being called in the main process while a -# BGSAVE or BGREWRITEAOF is in progress. -# -# This means that while another child is saving the durability of Redis is -# the same as "appendfsync none", that in pratical terms means that it is -# possible to lost up to 30 seconds of log in the worst scenario (with the -# default Linux settings). -# -# If you have latency problems turn this to "yes". Otherwise leave it as -# "no" that is the safest pick from the point of view of durability. -no-appendfsync-on-rewrite no - -################################ VIRTUAL MEMORY ############################### - -# Virtual Memory allows Redis to work with datasets bigger than the actual -# amount of RAM needed to hold the whole dataset in memory. -# In order to do so very used keys are taken in memory while the other keys -# are swapped into a swap file, similarly to what operating systems do -# with memory pages. -# -# To enable VM just set 'vm-enabled' to yes, and set the following three -# VM parameters accordingly to your needs. - -vm-enabled no -# vm-enabled yes - -# This is the path of the Redis swap file. As you can guess, swap files -# can't be shared by different Redis instances, so make sure to use a swap -# file for every redis process you are running. Redis will complain if the -# swap file is already in use. -# -# The best kind of storage for the Redis swap file (that's accessed at random) -# is a Solid State Disk (SSD). -# -# *** WARNING *** if you are using a shared hosting the default of putting -# the swap file under /tmp is not secure. Create a dir with access granted -# only to Redis user and configure Redis to create the swap file there. -vm-swap-file /tmp/redis.swap - -# vm-max-memory configures the VM to use at max the specified amount of -# RAM. Everything that deos not fit will be swapped on disk *if* possible, that -# is, if there is still enough contiguous space in the swap file. -# -# With vm-max-memory 0 the system will swap everything it can. Not a good -# default, just specify the max amount of RAM you can in bytes, but it's -# better to leave some margin. For instance specify an amount of RAM -# that's more or less between 60 and 80% of your free RAM. -vm-max-memory 0 - -# Redis swap files is split into pages. An object can be saved using multiple -# contiguous pages, but pages can't be shared between different objects. -# So if your page is too big, small objects swapped out on disk will waste -# a lot of space. If you page is too small, there is less space in the swap -# file (assuming you configured the same number of total swap file pages). -# -# If you use a lot of small objects, use a page size of 64 or 32 bytes. -# If you use a lot of big objects, use a bigger page size. -# If unsure, use the default :) -vm-page-size 32 - -# Number of total memory pages in the swap file. -# Given that the page table (a bitmap of free/used pages) is taken in memory, -# every 8 pages on disk will consume 1 byte of RAM. -# -# The total swap size is vm-page-size * vm-pages -# -# With the default of 32-bytes memory pages and 134217728 pages Redis will -# use a 4 GB swap file, that will use 16 MB of RAM for the page table. -# -# It's better to use the smallest acceptable value for your application, -# but the default is large in order to work in most conditions. -vm-pages 134217728 - -# Max number of VM I/O threads running at the same time. -# This threads are used to read/write data from/to swap file, since they -# also encode and decode objects from disk to memory or the reverse, a bigger -# number of threads can help with big objects even if they can't help with -# I/O itself as the physical device may not be able to couple with many -# reads/writes operations at the same time. -# -# The special value of 0 turn off threaded I/O and enables the blocking -# Virtual Memory implementation. -vm-max-threads 4 - -############################### ADVANCED CONFIG ############################### - -# Glue small output buffers together in order to send small replies in a -# single TCP packet. Uses a bit more CPU but most of the times it is a win -# in terms of number of queries per second. Use 'yes' if unsure. -glueoutputbuf yes - -# Hashes are encoded in a special way (much more memory efficient) when they -# have at max a given numer of elements, and the biggest element does not -# exceed a given threshold. You can configure this limits with the following -# configuration directives. -hash-max-zipmap-entries 64 -hash-max-zipmap-value 512 - -# Active rehashing uses 1 millisecond every 100 milliseconds of CPU time in -# order to help rehashing the main Redis hash table (the one mapping top-level -# keys to values). The hash table implementation redis uses (see dict.c) -# performs a lazy rehashing: the more operation you run into an hash table -# that is rhashing, the more rehashing "steps" are performed, so if the -# server is idle the rehashing is never complete and some more memory is used -# by the hash table. -# -# The default is to use this millisecond 10 times every second in order to -# active rehashing the main dictionaries, freeing memory when possible. -# -# If unsure: -# use "activerehashing no" if you have hard latency requirements and it is -# not a good thing in your environment that Redis can reply form time to time -# to queries with 2 milliseconds delay. -# -# use "activerehashing yes" if you don't have such hard requirements but -# want to free memory asap when possible. -activerehashing yes - -################################## INCLUDES ################################### - -# Include one or more other config files here. This is useful if you -# have a standard template that goes to all redis server but also need -# to customize a few per-server settings. Include files can include -# other files, so use this wisely. -# -# include /path/to/local.conf -# include /path/to/other.conf diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index b7853ef..45bfb5c 100644 Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index f96e2df..b3e777e 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,9 +1,6 @@ -#Mon Jan 18 09:35:53 CET 2010 +#Fri Dec 23 12:54:50 CST 2011 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME -distributionVersion=0.9-preview-3 zipStorePath=wrapper/dists -urlRoot=http\://dist.codehaus.org/gradle/ -distributionName=gradle -distributionClassifier=bin +distributionUrl=http\://repo.gradle.org/gradle/distributions/gradle-1.0-milestone-6-bin.zip diff --git a/gradlew b/gradlew index 820f5cf..d8809f1 100755 --- a/gradlew +++ b/gradlew @@ -7,21 +7,25 @@ ############################################################################## # Uncomment those lines to set JVM options. GRADLE_OPTS and JAVA_OPTS can be used together. -GRADLE_OPTS="$GRADLE_OPTS -Xmx1024m" -# JAVA_OPTS="$JAVA_OPTS -Xmx512" +# GRADLE_OPTS="$GRADLE_OPTS -Xmx512m" +# JAVA_OPTS="$JAVA_OPTS -Xmx512m" GRADLE_APP_NAME=Gradle +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + warn ( ) { - echo "${PROGNAME}: $*" + echo "$*" } die ( ) { - warn "$*" + echo + echo "$*" + echo exit 1 } - # OS specific support (must be 'true' or 'false'). cygwin=false msys=false @@ -79,12 +83,31 @@ if [ -z "$JAVACMD" ] ; then fi fi if [ ! -x "$JAVACMD" ] ; then - die "JAVA_HOME is not defined correctly, can not execute: $JAVACMD" + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." fi if [ -z "$JAVA_HOME" ] ; then warn "JAVA_HOME environment variable is not set" fi +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query businessSystem maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + # For Darwin, add GRADLE_APP_NAME to the JAVA_OPTS as -Xdock:name if $darwin; then JAVA_OPTS="$JAVA_OPTS -Xdock:name=$GRADLE_APP_NAME" @@ -135,8 +158,11 @@ if $cygwin ; then esac fi -"$JAVACMD" $JAVA_OPTS $GRADLE_OPTS \ +GRADLE_APP_BASE_NAME=`basename "$0"` + +exec "$JAVACMD" $JAVA_OPTS $GRADLE_OPTS \ -classpath "$CLASSPATH" \ + -Dorg.gradle.appname="$GRADLE_APP_BASE_NAME" \ -Dorg.gradle.wrapper.properties="$WRAPPER_PROPERTIES" \ $STARTER_MAIN_CLASS \ "$@" diff --git a/gradlew.bat b/gradlew.bat index 114aaa4..4855abb 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -5,83 +5,39 @@ @rem ## @rem ########################################################################## -@rem -@rem $Revision: 10602 $ $Date: 2008-01-25 02:49:54 +0100 (ven., 25 janv. 2008) $ -@rem - @rem Set local scope for the variables with windows NT shell if "%OS%"=="Windows_NT" setlocal @rem Uncomment those lines to set JVM options. GRADLE_OPTS and JAVA_OPTS can be used together. -@rem set GRADLE_OPTS=%GRADLE_OPTS% -Xmx512 -@rem set JAVA_OPTS=%JAVA_OPTS% -Xmx512 +@rem set GRADLE_OPTS=%GRADLE_OPTS% -Xmx512m +@rem set JAVA_OPTS=%JAVA_OPTS% -Xmx512m set DIRNAME=%~dp0 if "%DIRNAME%" == "" set DIRNAME=.\ -@rem Determine the command interpreter to execute the "CD" later -set COMMAND_COM="cmd.exe" -if exist "%SystemRoot%\system32\cmd.exe" set COMMAND_COM="%SystemRoot%\system32\cmd.exe" -if exist "%SystemRoot%\command.com" set COMMAND_COM="%SystemRoot%\command.com" +@rem Find java.exe +set JAVA_EXE=java.exe +if not defined JAVA_HOME goto init -@rem Use explicit find.exe to prevent cygwin and others find.exe from being used -set FIND_EXE="find.exe" -if exist "%SystemRoot%\system32\find.exe" set FIND_EXE="%SystemRoot%\system32\find.exe" -if exist "%SystemRoot%\command\find.exe" set FIND_EXE="%SystemRoot%\command\find.exe" +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe -:check_JAVA_HOME -@rem Make sure we have a valid JAVA_HOME -if not "%JAVA_HOME%" == "" goto have_JAVA_HOME +if exist "%JAVA_EXE%" goto init echo. -echo ERROR: Environment variable JAVA_HOME has not been set. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% echo. echo Please set the JAVA_HOME variable in your environment to match the echo location of your Java installation. echo. goto end -:have_JAVA_HOME -@rem Validate JAVA_HOME -%COMMAND_COM% /C DIR "%JAVA_HOME%" 2>&1 | %FIND_EXE% /I /C "%JAVA_HOME%" >nul -if not errorlevel 1 goto init - -echo. -echo ERROR: JAVA_HOME might be set to an invalid directory: %JAVA_HOME% -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation if there are problems. -echo. - :init -@rem get name of script to launch with full path @rem Get command-line arguments, handling Windowz variants -SET _marker=%JAVA_HOME: =% -@rem IF NOT "%_marker%" == "%JAVA_HOME%" ECHO JAVA_HOME "%JAVA_HOME%" contains spaces. Please change to a location without spaces if this causes problems. if not "%OS%" == "Windows_NT" goto win9xME_args if "%eval[2+2]" == "4" goto 4NT_args -IF "%_marker%" == "%JAVA_HOME%" goto :win9xME_args - -set _FIXPATH= -call :fixpath "%JAVA_HOME%" -set JAVA_HOME=%_FIXPATH:~1% - -goto win9xME_args - -:fixpath -if not %1.==. ( -for /f "tokens=1* delims=;" %%a in (%1) do ( -call :shortfilename "%%a" & call :fixpath "%%b" -) -) -goto :EOF -:shortfilename -for %%i in (%1) do set _FIXPATH=%_FIXPATH%;%%~fsi -goto :EOF - - :win9xME_args @rem Slurp the command line arguments. set CMD_LINE_ARGS= @@ -103,10 +59,10 @@ set CMD_LINE_ARGS=%$ set STARTER_MAIN_CLASS=org.gradle.wrapper.GradleWrapperMain set CLASSPATH=%DIRNAME%\gradle\wrapper\gradle-wrapper.jar set WRAPPER_PROPERTIES=%DIRNAME%\gradle\wrapper\gradle-wrapper.properties -set JAVA_EXE=%JAVA_HOME%\bin\java.exe set GRADLE_OPTS=%JAVA_OPTS% %GRADLE_OPTS% -Dorg.gradle.wrapper.properties="%WRAPPER_PROPERTIES%" +@rem Execute Gradle "%JAVA_EXE%" %GRADLE_OPTS% -classpath "%CLASSPATH%" %STARTER_MAIN_CLASS% %CMD_LINE_ARGS% :end diff --git a/pom.xml b/pom.xml index ab999ec..54a2657 100644 --- a/pom.xml +++ b/pom.xml @@ -3,17 +3,17 @@ org.sonatype.oss oss-parent - 5 + 7 4.0.0 jar redis.clients jedis - 2.0.1-SNAPSHOT + 2.2.0-SNAPSHOT Jedis Jedis is a blazingly small and sane Redis java client. - http://code.google.com/p/jedis/ + https://github.com/xetorthio/jedis @@ -27,7 +27,7 @@ - Jedis License + MIT http://github.com/xetorthio/jedis/raw/master/LICENSE.txt repo @@ -38,11 +38,11 @@ http://github.com/xetorthio/jedis/issues - - http://github.com/xetorthio/jedis - scm:git:git@github.com:xetorthio/jedis.git - scm:git:git@github.com:xetorthio/jedis.git - + + scm:git:git@github.com:xetorthio/jedis.git + scm:git:git@github.com:xetorthio/jedis.git + scm:git:git@github.com:xetorthio/jedis.git + localhost:6379,localhost:6380 @@ -102,6 +102,11 @@ true + + org.apache.maven.plugins + maven-release-plugin + 2.0-beta-9 + diff --git a/src/main/java/redis/clients/jedis/AdvancedBinaryJedisCommands.java b/src/main/java/redis/clients/jedis/AdvancedBinaryJedisCommands.java new file mode 100644 index 0000000..51b4879 --- /dev/null +++ b/src/main/java/redis/clients/jedis/AdvancedBinaryJedisCommands.java @@ -0,0 +1,25 @@ +package redis.clients.jedis; + + +import java.util.List; + +public interface AdvancedBinaryJedisCommands { + + List configGet(byte[] pattern); + + byte[] configSet(byte[] parameter, byte[] value); + + String slowlogReset(); + + Long slowlogLen(); + + List slowlogGetBinary(); + + List slowlogGetBinary(long entries); + + Long objectRefcount(byte[] key); + + byte[] objectEncoding(byte[] key); + + Long objectIdletime(byte[] key); +} diff --git a/src/main/java/redis/clients/jedis/AdvancedJedisCommands.java b/src/main/java/redis/clients/jedis/AdvancedJedisCommands.java new file mode 100644 index 0000000..5ed50eb --- /dev/null +++ b/src/main/java/redis/clients/jedis/AdvancedJedisCommands.java @@ -0,0 +1,26 @@ +package redis.clients.jedis; + +import redis.clients.util.Slowlog; + +import java.util.List; + + +public interface AdvancedJedisCommands { + List configGet(String pattern); + + String configSet(String parameter, String value); + + String slowlogReset(); + + Long slowlogLen(); + + List slowlogGet(); + + List slowlogGet(long entries); + + Long objectRefcount(String string); + + String objectEncoding(String string); + + Long objectIdletime(String string); +} diff --git a/src/main/java/redis/clients/jedis/BasicCommands.java b/src/main/java/redis/clients/jedis/BasicCommands.java new file mode 100644 index 0000000..482d43b --- /dev/null +++ b/src/main/java/redis/clients/jedis/BasicCommands.java @@ -0,0 +1,42 @@ +package redis.clients.jedis; + +public interface BasicCommands { + + String ping(); + + String quit(); + + String flushDB(); + + Long dbSize(); + + String select(int index); + + String flushAll(); + + String auth(String password); + + String save(); + + String bgsave(); + + String bgrewriteaof(); + + Long lastsave(); + + String shutdown(); + + String info(); + + String info(String section); + + String slaveof(String host, int port); + + String slaveofNoOne(); + + Long getDB(); + + String debug(DebugParams params); + + String configResetStat(); +} diff --git a/src/main/java/redis/clients/jedis/BasicRedisPipeline.java b/src/main/java/redis/clients/jedis/BasicRedisPipeline.java new file mode 100644 index 0000000..97658d8 --- /dev/null +++ b/src/main/java/redis/clients/jedis/BasicRedisPipeline.java @@ -0,0 +1,38 @@ +package redis.clients.jedis; + + +import java.util.List; + +/** + * Pipelined responses for all of the low level, non key related commands + */ +public interface BasicRedisPipeline { + + Response bgrewriteaof(); + + Response bgsave(); + + Response configGet(String pattern); + + Response configSet(String parameter, String value); + + Response configResetStat(); + + Response save(); + + Response lastsave(); + + Response flushDB(); + + Response flushAll(); + + Response info(); + + Response dbSize(); + + Response shutdown(); + + Response ping(); + + Response select(int index); +} diff --git a/src/main/java/redis/clients/jedis/BinaryClient.java b/src/main/java/redis/clients/jedis/BinaryClient.java index c982151..4d21f7a 100644 --- a/src/main/java/redis/clients/jedis/BinaryClient.java +++ b/src/main/java/redis/clients/jedis/BinaryClient.java @@ -2,15 +2,21 @@ package redis.clients.jedis; import static redis.clients.jedis.Protocol.toByteArray; import static redis.clients.jedis.Protocol.Command.*; +import static redis.clients.jedis.Protocol.Keyword.ENCODING; +import static redis.clients.jedis.Protocol.Keyword.IDLETIME; +import static redis.clients.jedis.Protocol.Keyword.LEN; import static redis.clients.jedis.Protocol.Keyword.LIMIT; import static redis.clients.jedis.Protocol.Keyword.NO; import static redis.clients.jedis.Protocol.Keyword.ONE; +import static redis.clients.jedis.Protocol.Keyword.REFCOUNT; +import static redis.clients.jedis.Protocol.Keyword.RESET; import static redis.clients.jedis.Protocol.Keyword.STORE; import static redis.clients.jedis.Protocol.Keyword.WITHSCORES; import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import redis.clients.jedis.Protocol.Command; import redis.clients.jedis.Protocol.Keyword; @@ -18,12 +24,12 @@ import redis.clients.util.SafeEncoder; public class BinaryClient extends Connection { public enum LIST_POSITION { - BEFORE, AFTER; - public final byte[] raw; + BEFORE, AFTER; + public final byte[] raw; - private LIST_POSITION() { - raw = SafeEncoder.encode(name()); - } + private LIST_POSITION() { + raw = SafeEncoder.encode(name()); + } } private boolean isInMulti; @@ -33,462 +39,506 @@ public class BinaryClient extends Connection { private long db; public boolean isInMulti() { - return isInMulti; + return isInMulti; } public BinaryClient(final String host) { - super(host); + super(host); } public BinaryClient(final String host, final int port) { - super(host, port); + super(host, port); + } + + private byte[][] joinParameters(byte[] first, byte[][] rest) { + byte[][] result = new byte[rest.length + 1][]; + result[0] = first; + for (int i = 0; i < rest.length; i++) { + result[i + 1] = rest[i]; + } + return result; } public void setPassword(final String password) { - this.password = password; + this.password = password; } @Override public void connect() { - if (!isConnected()) { - super.connect(); - if (password != null) { - auth(password); - getStatusCodeReply(); - } - if (db > 0) { - select(Long.valueOf(db).intValue()); - getStatusCodeReply(); - } - } + if (!isConnected()) { + super.connect(); + if (password != null) { + auth(password); + getStatusCodeReply(); + } + if (db > 0) { + select(Long.valueOf(db).intValue()); + getStatusCodeReply(); + } + } } public void ping() { - sendCommand(PING); + sendCommand(Command.PING); } public void set(final byte[] key, final byte[] value) { - sendCommand(Command.SET, key, value); + sendCommand(Command.SET, key, value); } public void get(final byte[] key) { - sendCommand(Command.GET, key); + sendCommand(Command.GET, key); } public void quit() { - db = 0; - sendCommand(QUIT); + db = 0; + sendCommand(QUIT); } public void exists(final byte[] key) { - sendCommand(EXISTS, key); + sendCommand(EXISTS, key); } public void del(final byte[]... keys) { - sendCommand(DEL, keys); + sendCommand(DEL, keys); } public void type(final byte[] key) { - sendCommand(TYPE, key); + sendCommand(TYPE, key); } public void flushDB() { - sendCommand(FLUSHDB); + sendCommand(FLUSHDB); } public void keys(final byte[] pattern) { - sendCommand(KEYS, pattern); + sendCommand(KEYS, pattern); } public void randomKey() { - sendCommand(RANDOMKEY); + sendCommand(RANDOMKEY); } public void rename(final byte[] oldkey, final byte[] newkey) { - sendCommand(RENAME, oldkey, newkey); + sendCommand(RENAME, oldkey, newkey); } public void renamenx(final byte[] oldkey, final byte[] newkey) { - sendCommand(RENAMENX, oldkey, newkey); + sendCommand(RENAMENX, oldkey, newkey); } public void dbSize() { - sendCommand(DBSIZE); + sendCommand(DBSIZE); } public void expire(final byte[] key, final int seconds) { - sendCommand(EXPIRE, key, toByteArray(seconds)); + sendCommand(EXPIRE, key, toByteArray(seconds)); } public void expireAt(final byte[] key, final long unixTime) { - sendCommand(EXPIREAT, key, toByteArray(unixTime)); + sendCommand(EXPIREAT, key, toByteArray(unixTime)); } public void ttl(final byte[] key) { - sendCommand(TTL, key); + sendCommand(TTL, key); } public void select(final int index) { - db = index; - sendCommand(SELECT, toByteArray(index)); + db = index; + sendCommand(SELECT, toByteArray(index)); } public void move(final byte[] key, final int dbIndex) { - sendCommand(MOVE, key, toByteArray(dbIndex)); + sendCommand(MOVE, key, toByteArray(dbIndex)); } public void flushAll() { - sendCommand(FLUSHALL); + sendCommand(FLUSHALL); } public void getSet(final byte[] key, final byte[] value) { - sendCommand(GETSET, key, value); + sendCommand(GETSET, key, value); } public void mget(final byte[]... keys) { - sendCommand(MGET, keys); + sendCommand(MGET, keys); } public void setnx(final byte[] key, final byte[] value) { - sendCommand(SETNX, key, value); + sendCommand(SETNX, key, value); } public void setex(final byte[] key, final int seconds, final byte[] value) { - sendCommand(SETEX, key, toByteArray(seconds), value); + sendCommand(SETEX, key, toByteArray(seconds), value); } public void mset(final byte[]... keysvalues) { - sendCommand(MSET, keysvalues); + sendCommand(MSET, keysvalues); } public void msetnx(final byte[]... keysvalues) { - sendCommand(MSETNX, keysvalues); + sendCommand(MSETNX, keysvalues); } public void decrBy(final byte[] key, final long integer) { - sendCommand(DECRBY, key, toByteArray(integer)); + sendCommand(DECRBY, key, toByteArray(integer)); } public void decr(final byte[] key) { - sendCommand(DECR, key); + sendCommand(DECR, key); } public void incrBy(final byte[] key, final long integer) { - sendCommand(INCRBY, key, toByteArray(integer)); + sendCommand(INCRBY, key, toByteArray(integer)); } public void incr(final byte[] key) { - sendCommand(INCR, key); + sendCommand(INCR, key); } public void append(final byte[] key, final byte[] value) { - sendCommand(APPEND, key, value); + sendCommand(APPEND, key, value); } public void substr(final byte[] key, final int start, final int end) { - sendCommand(SUBSTR, key, toByteArray(start), toByteArray(end)); + sendCommand(SUBSTR, key, toByteArray(start), toByteArray(end)); } public void hset(final byte[] key, final byte[] field, final byte[] value) { - sendCommand(HSET, key, field, value); + sendCommand(HSET, key, field, value); } public void hget(final byte[] key, final byte[] field) { - sendCommand(HGET, key, field); + sendCommand(HGET, key, field); } public void hsetnx(final byte[] key, final byte[] field, final byte[] value) { - sendCommand(HSETNX, key, field, value); + sendCommand(HSETNX, key, field, value); } public void hmset(final byte[] key, final Map hash) { - final List params = new ArrayList(); - params.add(key); + final List params = new ArrayList(); + params.add(key); - for (final byte[] field : hash.keySet()) { - params.add(field); - params.add(hash.get(field)); - } - sendCommand(HMSET, params.toArray(new byte[params.size()][])); + for (final Entry entry : hash.entrySet()) { + params.add(entry.getKey()); + params.add(entry.getValue()); + } + sendCommand(HMSET, params.toArray(new byte[params.size()][])); } public void hmget(final byte[] key, final byte[]... fields) { - final byte[][] params = new byte[fields.length + 1][]; - params[0] = key; - System.arraycopy(fields, 0, params, 1, fields.length); - sendCommand(HMGET, params); + final byte[][] params = new byte[fields.length + 1][]; + params[0] = key; + System.arraycopy(fields, 0, params, 1, fields.length); + sendCommand(HMGET, params); } public void hincrBy(final byte[] key, final byte[] field, final long value) { - sendCommand(HINCRBY, key, field, toByteArray(value)); + sendCommand(HINCRBY, key, field, toByteArray(value)); } public void hexists(final byte[] key, final byte[] field) { - sendCommand(HEXISTS, key, field); + sendCommand(HEXISTS, key, field); } - public void hdel(final byte[] key, final byte[] field) { - sendCommand(HDEL, key, field); + public void hdel(final byte[] key, final byte[]... fields) { + sendCommand(HDEL, joinParameters(key, fields)); } public void hlen(final byte[] key) { - sendCommand(HLEN, key); + sendCommand(HLEN, key); } public void hkeys(final byte[] key) { - sendCommand(HKEYS, key); + sendCommand(HKEYS, key); } public void hvals(final byte[] key) { - sendCommand(HVALS, key); + sendCommand(HVALS, key); } public void hgetAll(final byte[] key) { - sendCommand(HGETALL, key); + sendCommand(HGETALL, key); } - public void rpush(final byte[] key, final byte[] string) { - sendCommand(RPUSH, key, string); + public void rpush(final byte[] key, final byte[]... strings) { + sendCommand(RPUSH, joinParameters(key, strings)); } - public void lpush(final byte[] key, final byte[] string) { - sendCommand(LPUSH, key, string); + public void lpush(final byte[] key, final byte[]... strings) { + sendCommand(LPUSH, joinParameters(key, strings)); } public void llen(final byte[] key) { - sendCommand(LLEN, key); + sendCommand(LLEN, key); } public void lrange(final byte[] key, final long start, final long end) { - sendCommand(LRANGE, key, toByteArray(start), toByteArray(end)); + sendCommand(LRANGE, key, toByteArray(start), toByteArray(end)); } public void ltrim(final byte[] key, final long start, final long end) { - sendCommand(LTRIM, key, toByteArray(start), toByteArray(end)); + sendCommand(LTRIM, key, toByteArray(start), toByteArray(end)); } public void lindex(final byte[] key, final long index) { - sendCommand(LINDEX, key, toByteArray(index)); + sendCommand(LINDEX, key, toByteArray(index)); } public void lset(final byte[] key, final long index, final byte[] value) { - sendCommand(LSET, key, toByteArray(index), value); + sendCommand(LSET, key, toByteArray(index), value); } public void lrem(final byte[] key, long count, final byte[] value) { - sendCommand(LREM, key, toByteArray(count), value); + sendCommand(LREM, key, toByteArray(count), value); } public void lpop(final byte[] key) { - sendCommand(LPOP, key); + sendCommand(LPOP, key); } public void rpop(final byte[] key) { - sendCommand(RPOP, key); + sendCommand(RPOP, key); } public void rpoplpush(final byte[] srckey, final byte[] dstkey) { - sendCommand(RPOPLPUSH, srckey, dstkey); + sendCommand(RPOPLPUSH, srckey, dstkey); } - public void sadd(final byte[] key, final byte[] member) { - sendCommand(SADD, key, member); + public void sadd(final byte[] key, final byte[]... members) { + sendCommand(SADD, joinParameters(key, members)); } public void smembers(final byte[] key) { - sendCommand(SMEMBERS, key); + sendCommand(SMEMBERS, key); } - public void srem(final byte[] key, final byte[] member) { - sendCommand(SREM, key, member); + public void srem(final byte[] key, final byte[]... members) { + sendCommand(SREM, joinParameters(key, members)); } public void spop(final byte[] key) { - sendCommand(SPOP, key); + sendCommand(SPOP, key); } public void smove(final byte[] srckey, final byte[] dstkey, - final byte[] member) { - sendCommand(SMOVE, srckey, dstkey, member); + final byte[] member) { + sendCommand(SMOVE, srckey, dstkey, member); } public void scard(final byte[] key) { - sendCommand(SCARD, key); + sendCommand(SCARD, key); } public void sismember(final byte[] key, final byte[] member) { - sendCommand(SISMEMBER, key, member); + sendCommand(SISMEMBER, key, member); } public void sinter(final byte[]... keys) { - sendCommand(SINTER, keys); + sendCommand(SINTER, keys); } public void sinterstore(final byte[] dstkey, final byte[]... keys) { - final byte[][] params = new byte[keys.length + 1][]; - params[0] = dstkey; - System.arraycopy(keys, 0, params, 1, keys.length); - sendCommand(SINTERSTORE, params); + final byte[][] params = new byte[keys.length + 1][]; + params[0] = dstkey; + System.arraycopy(keys, 0, params, 1, keys.length); + sendCommand(SINTERSTORE, params); } public void sunion(final byte[]... keys) { - sendCommand(SUNION, keys); + sendCommand(SUNION, keys); } public void sunionstore(final byte[] dstkey, final byte[]... keys) { - byte[][] params = new byte[keys.length + 1][]; - params[0] = dstkey; - System.arraycopy(keys, 0, params, 1, keys.length); - sendCommand(SUNIONSTORE, params); + byte[][] params = new byte[keys.length + 1][]; + params[0] = dstkey; + System.arraycopy(keys, 0, params, 1, keys.length); + sendCommand(SUNIONSTORE, params); } public void sdiff(final byte[]... keys) { - sendCommand(SDIFF, keys); + sendCommand(SDIFF, keys); } public void sdiffstore(final byte[] dstkey, final byte[]... keys) { - byte[][] params = new byte[keys.length + 1][]; - params[0] = dstkey; - System.arraycopy(keys, 0, params, 1, keys.length); - sendCommand(SDIFFSTORE, params); + byte[][] params = new byte[keys.length + 1][]; + params[0] = dstkey; + System.arraycopy(keys, 0, params, 1, keys.length); + sendCommand(SDIFFSTORE, params); } public void srandmember(final byte[] key) { - sendCommand(SRANDMEMBER, key); + sendCommand(SRANDMEMBER, key); } public void zadd(final byte[] key, final double score, final byte[] member) { - sendCommand(ZADD, key, toByteArray(score), member); + sendCommand(ZADD, key, toByteArray(score), member); } - public void zrange(final byte[] key, final int start, final int end) { - sendCommand(ZRANGE, key, toByteArray(start), toByteArray(end)); + public void zaddBinary(final byte[] key, Map scoreMembers) { + ArrayList args = new ArrayList( + scoreMembers.size() * 2 + 1); + + args.add(key); + + for (Map.Entry entry : scoreMembers.entrySet()) { + args.add(toByteArray(entry.getKey())); + args.add(entry.getValue()); + } + + byte[][] argsArray = new byte[args.size()][]; + args.toArray(argsArray); + + sendCommand(ZADD, argsArray); } - public void zrem(final byte[] key, final byte[] member) { - sendCommand(ZREM, key, member); + public void zrange(final byte[] key, final long start, final long end) { + sendCommand(ZRANGE, key, toByteArray(start), toByteArray(end)); + } + + public void zrem(final byte[] key, final byte[]... members) { + sendCommand(ZREM, joinParameters(key, members)); } public void zincrby(final byte[] key, final double score, - final byte[] member) { - sendCommand(ZINCRBY, key, toByteArray(score), member); + final byte[] member) { + sendCommand(ZINCRBY, key, toByteArray(score), member); } public void zrank(final byte[] key, final byte[] member) { - sendCommand(ZRANK, key, member); + sendCommand(ZRANK, key, member); } public void zrevrank(final byte[] key, final byte[] member) { - sendCommand(ZREVRANK, key, member); + sendCommand(ZREVRANK, key, member); } - public void zrevrange(final byte[] key, final int start, final int end) { - sendCommand(ZREVRANGE, key, toByteArray(start), toByteArray(end)); + public void zrevrange(final byte[] key, final long start, final long end) { + sendCommand(ZREVRANGE, key, toByteArray(start), toByteArray(end)); } - public void zrangeWithScores(final byte[] key, final int start, - final int end) { - sendCommand(ZRANGE, key, toByteArray(start), toByteArray(end), - WITHSCORES.raw); + public void zrangeWithScores(final byte[] key, final long start, + final long end) { + sendCommand(ZRANGE, key, toByteArray(start), toByteArray(end), + WITHSCORES.raw); } - public void zrevrangeWithScores(final byte[] key, final int start, - final int end) { - sendCommand(ZREVRANGE, key, toByteArray(start), toByteArray(end), - WITHSCORES.raw); + public void zrevrangeWithScores(final byte[] key, final long start, + final long end) { + sendCommand(ZREVRANGE, key, toByteArray(start), toByteArray(end), + WITHSCORES.raw); } public void zcard(final byte[] key) { - sendCommand(ZCARD, key); + sendCommand(ZCARD, key); } public void zscore(final byte[] key, final byte[] member) { - sendCommand(ZSCORE, key, member); + sendCommand(ZSCORE, key, member); } public void multi() { - sendCommand(MULTI); - isInMulti = true; + sendCommand(MULTI); + isInMulti = true; } public void discard() { - sendCommand(DISCARD); - isInMulti = false; + sendCommand(DISCARD); + isInMulti = false; } public void exec() { - sendCommand(EXEC); - isInMulti = false; + sendCommand(EXEC); + isInMulti = false; } public void watch(final byte[]... keys) { - sendCommand(WATCH, keys); + sendCommand(WATCH, keys); } public void unwatch() { - sendCommand(UNWATCH); + sendCommand(UNWATCH); } public void sort(final byte[] key) { - sendCommand(SORT, key); + sendCommand(SORT, key); } public void sort(final byte[] key, final SortingParams sortingParameters) { - final List args = new ArrayList(); - args.add(key); - args.addAll(sortingParameters.getParams()); - sendCommand(SORT, args.toArray(new byte[args.size()][])); + final List args = new ArrayList(); + args.add(key); + args.addAll(sortingParameters.getParams()); + sendCommand(SORT, args.toArray(new byte[args.size()][])); } public void blpop(final byte[][] args) { - sendCommand(BLPOP, args); + sendCommand(BLPOP, args); + } + + public void blpop(final int timeout, final byte[]... keys) { + final List args = new ArrayList(); + for (final byte[] arg : keys) { + args.add(arg); + } + args.add(Protocol.toByteArray(timeout)); + blpop(args.toArray(new byte[args.size()][])); } public void sort(final byte[] key, final SortingParams sortingParameters, - final byte[] dstkey) { - final List args = new ArrayList(); - args.add(key); - args.addAll(sortingParameters.getParams()); - args.add(STORE.raw); - args.add(dstkey); - sendCommand(SORT, args.toArray(new byte[args.size()][])); + final byte[] dstkey) { + final List args = new ArrayList(); + args.add(key); + args.addAll(sortingParameters.getParams()); + args.add(STORE.raw); + args.add(dstkey); + sendCommand(SORT, args.toArray(new byte[args.size()][])); } public void sort(final byte[] key, final byte[] dstkey) { - sendCommand(SORT, key, STORE.raw, dstkey); + sendCommand(SORT, key, STORE.raw, dstkey); } public void brpop(final byte[][] args) { - sendCommand(BRPOP, args); + sendCommand(BRPOP, args); + } + + public void brpop(final int timeout, final byte[]... keys) { + final List args = new ArrayList(); + for (final byte[] arg : keys) { + args.add(arg); + } + args.add(Protocol.toByteArray(timeout)); + brpop(args.toArray(new byte[args.size()][])); } public void auth(final String password) { - setPassword(password); - sendCommand(AUTH, password); + setPassword(password); + sendCommand(AUTH, password); } public void subscribe(final byte[]... channels) { - sendCommand(SUBSCRIBE, channels); + sendCommand(SUBSCRIBE, channels); } public void publish(final byte[] channel, final byte[] message) { - sendCommand(PUBLISH, channel, message); + sendCommand(PUBLISH, channel, message); } public void unsubscribe() { - sendCommand(UNSUBSCRIBE); + sendCommand(UNSUBSCRIBE); } public void unsubscribe(final byte[]... channels) { - sendCommand(UNSUBSCRIBE, channels); + sendCommand(UNSUBSCRIBE, channels); } public void psubscribe(final byte[]... patterns) { - sendCommand(PSUBSCRIBE, patterns); + sendCommand(PSUBSCRIBE, patterns); } public void punsubscribe() { - sendCommand(PUNSUBSCRIBE); + sendCommand(PUNSUBSCRIBE); } public void punsubscribe(final byte[]... patterns) { @@ -521,8 +571,8 @@ public class BinaryClient extends Connection { } public void zrangeByScore(final byte[] key, final byte[] min, - final byte[] max) { - sendCommand(ZRANGEBYSCORE, key, min, max); + final byte[] max) { + sendCommand(ZRANGEBYSCORE, key, min, max); } public void zrangeByScore(final byte[] key, final String min, @@ -540,8 +590,8 @@ public class BinaryClient extends Connection { } public void zrevrangeByScore(final byte[] key, final byte[] max, - final byte[] min) { - sendCommand(ZREVRANGEBYSCORE, key, max, min); + final byte[] min) { + sendCommand(ZREVRANGEBYSCORE, key, max, min); } public void zrevrangeByScore(final byte[] key, final String max, @@ -558,14 +608,7 @@ public class BinaryClient extends Connection { sendCommand(ZRANGEBYSCORE, key, byteArrayMin, byteArrayMax, LIMIT.raw, toByteArray(offset), toByteArray(count)); } - - public void zrangeByScore(final byte[] key, final byte min[], - final byte max[], final int offset, int count) { - - sendCommand(ZRANGEBYSCORE, key, min, max, - LIMIT.raw, toByteArray(offset), toByteArray(count)); - } - + public void zrangeByScore(final byte[] key, final String min, final String max, final int offset, int count) { @@ -581,14 +624,7 @@ public class BinaryClient extends Connection { sendCommand(ZREVRANGEBYSCORE, key, byteArrayMax, byteArrayMin, LIMIT.raw, toByteArray(offset), toByteArray(count)); - } - - public void zrevrangeByScore(final byte[] key, final byte max[], - final byte min[], final int offset, int count) { - - sendCommand(ZREVRANGEBYSCORE, key, max, min, - LIMIT.raw, toByteArray(offset), toByteArray(count)); - } + } public void zrevrangeByScore(final byte[] key, final String max, final String min, final int offset, int count) { @@ -607,13 +643,6 @@ public class BinaryClient extends Connection { WITHSCORES.raw); } - public void zrangeByScoreWithScores(final byte[] key, final byte min[], - final byte max[]) { - - sendCommand(ZRANGEBYSCORE, key, min, max, - WITHSCORES.raw); - } - public void zrangeByScoreWithScores(final byte[] key, final String min, final String max) { @@ -631,12 +660,6 @@ public class BinaryClient extends Connection { WITHSCORES.raw); } - public void zrevrangeByScoreWithScores(final byte[] key, final byte max[], - final byte min[]) { - sendCommand(ZREVRANGEBYSCORE, key, max, min, - WITHSCORES.raw); - } - public void zrevrangeByScoreWithScores(final byte[] key, final String max, final String min) { sendCommand(ZREVRANGEBYSCORE, key, max.getBytes(), min.getBytes(), @@ -654,13 +677,6 @@ public class BinaryClient extends Connection { WITHSCORES.raw); } - public void zrangeByScoreWithScores(final byte[] key, final byte min[], - final byte max[], final int offset, final int count) { - sendCommand(ZRANGEBYSCORE, key, min, max, - LIMIT.raw, toByteArray(offset), toByteArray(count), - WITHSCORES.raw); - } - public void zrangeByScoreWithScores(final byte[] key, final String min, final String max, final int offset, final int count) { sendCommand(ZRANGEBYSCORE, key, min.getBytes(), max.getBytes(), @@ -679,14 +695,6 @@ public class BinaryClient extends Connection { WITHSCORES.raw); } - public void zrevrangeByScoreWithScores(final byte[] key, final byte max[], - final byte min[], final int offset, final int count) { - - sendCommand(ZREVRANGEBYSCORE, key, max, min, - LIMIT.raw, toByteArray(offset), toByteArray(count), - WITHSCORES.raw); - } - public void zrevrangeByScoreWithScores(final byte[] key, final String max, final String min, final int offset, final int count) { @@ -694,175 +702,401 @@ public class BinaryClient extends Connection { LIMIT.raw, toByteArray(offset), toByteArray(count), WITHSCORES.raw); } - - public void zremrangeByRank(final byte[] key, final int start, final int end) { - sendCommand(ZREMRANGEBYRANK, key, toByteArray(start), toByteArray(end)); + + public void zrangeByScore(final byte[] key, final byte[] min, + final byte[] max, final int offset, int count) { + sendCommand(ZRANGEBYSCORE, key, min, max, LIMIT.raw, + toByteArray(offset), toByteArray(count)); } - public void zremrangeByScore(final byte[] key, final double start, - final double end) { - sendCommand(ZREMRANGEBYSCORE, key, toByteArray(start), toByteArray(end)); + public void zrevrangeByScore(final byte[] key, final byte[] max, + final byte[] min, final int offset, int count) { + sendCommand(ZREVRANGEBYSCORE, key, max, min, LIMIT.raw, + toByteArray(offset), toByteArray(count)); } - public void zremrangeByScore(final byte[] key, final byte start[], - final byte end[]) { - sendCommand(ZREMRANGEBYSCORE, key, start, end); + public void zrangeByScoreWithScores(final byte[] key, final byte[] min, + final byte[] max) { + sendCommand(ZRANGEBYSCORE, key, min, max, WITHSCORES.raw); } + public void zrevrangeByScoreWithScores(final byte[] key, final byte[] max, + final byte[] min) { + sendCommand(ZREVRANGEBYSCORE, key, max, min, WITHSCORES.raw); + } + + public void zrangeByScoreWithScores(final byte[] key, final byte[] min, + final byte[] max, final int offset, final int count) { + sendCommand(ZRANGEBYSCORE, key, min, max, LIMIT.raw, + toByteArray(offset), toByteArray(count), WITHSCORES.raw); + } + + public void zrevrangeByScoreWithScores(final byte[] key, final byte[] max, + final byte[] min, final int offset, final int count) { + sendCommand(ZREVRANGEBYSCORE, key, max, min, LIMIT.raw, + toByteArray(offset), toByteArray(count), WITHSCORES.raw); + } + + public void zremrangeByRank(final byte[] key, final long start, + final long end) { + sendCommand(ZREMRANGEBYRANK, key, toByteArray(start), toByteArray(end)); + } + + public void zremrangeByScore(final byte[] key, final byte[] start, + final byte[] end) { + sendCommand(ZREMRANGEBYSCORE, key, start, end); + } + public void zremrangeByScore(final byte[] key, final String start, final String end) { sendCommand(ZREMRANGEBYSCORE, key, start.getBytes(), end.getBytes()); } public void zunionstore(final byte[] dstkey, final byte[]... sets) { - final byte[][] params = new byte[sets.length + 2][]; - params[0] = dstkey; - params[1] = toByteArray(sets.length); - System.arraycopy(sets, 0, params, 2, sets.length); - sendCommand(ZUNIONSTORE, params); + final byte[][] params = new byte[sets.length + 2][]; + params[0] = dstkey; + params[1] = toByteArray(sets.length); + System.arraycopy(sets, 0, params, 2, sets.length); + sendCommand(ZUNIONSTORE, params); } public void zunionstore(final byte[] dstkey, final ZParams params, - final byte[]... sets) { - final List args = new ArrayList(); - args.add(dstkey); - args.add(Protocol.toByteArray(sets.length)); - for (final byte[] set : sets) { - args.add(set); - } - args.addAll(params.getParams()); - sendCommand(ZUNIONSTORE, args.toArray(new byte[args.size()][])); + final byte[]... sets) { + final List args = new ArrayList(); + args.add(dstkey); + args.add(Protocol.toByteArray(sets.length)); + for (final byte[] set : sets) { + args.add(set); + } + args.addAll(params.getParams()); + sendCommand(ZUNIONSTORE, args.toArray(new byte[args.size()][])); } public void zinterstore(final byte[] dstkey, final byte[]... sets) { - final byte[][] params = new byte[sets.length + 2][]; - params[0] = dstkey; - params[1] = Protocol.toByteArray(sets.length); - System.arraycopy(sets, 0, params, 2, sets.length); - sendCommand(ZINTERSTORE, params); + final byte[][] params = new byte[sets.length + 2][]; + params[0] = dstkey; + params[1] = Protocol.toByteArray(sets.length); + System.arraycopy(sets, 0, params, 2, sets.length); + sendCommand(ZINTERSTORE, params); } public void zinterstore(final byte[] dstkey, final ZParams params, - final byte[]... sets) { - final List args = new ArrayList(); - args.add(dstkey); - args.add(Protocol.toByteArray(sets.length)); - for (final byte[] set : sets) { - args.add(set); - } - args.addAll(params.getParams()); - sendCommand(ZINTERSTORE, args.toArray(new byte[args.size()][])); + final byte[]... sets) { + final List args = new ArrayList(); + args.add(dstkey); + args.add(Protocol.toByteArray(sets.length)); + for (final byte[] set : sets) { + args.add(set); + } + args.addAll(params.getParams()); + sendCommand(ZINTERSTORE, args.toArray(new byte[args.size()][])); } public void save() { - sendCommand(SAVE); + sendCommand(SAVE); } public void bgsave() { - sendCommand(BGSAVE); + sendCommand(BGSAVE); } public void bgrewriteaof() { - sendCommand(BGREWRITEAOF); + sendCommand(BGREWRITEAOF); } public void lastsave() { - sendCommand(LASTSAVE); + sendCommand(LASTSAVE); } public void shutdown() { - sendCommand(SHUTDOWN); + sendCommand(SHUTDOWN); } public void info() { - sendCommand(INFO); + sendCommand(INFO); + } + + public void info(final String section) { + sendCommand(INFO, section); } public void monitor() { - sendCommand(MONITOR); + sendCommand(MONITOR); } public void slaveof(final String host, final int port) { - sendCommand(SLAVEOF, host, String.valueOf(port)); + sendCommand(SLAVEOF, host, String.valueOf(port)); } public void slaveofNoOne() { - sendCommand(SLAVEOF, NO.raw, ONE.raw); + sendCommand(SLAVEOF, NO.raw, ONE.raw); } - public void configGet(final String pattern) { - sendCommand(CONFIG, Keyword.GET.name(), pattern); + public void configGet(final byte[] pattern) { + sendCommand(CONFIG, Keyword.GET.raw, pattern); } - public void configSet(final String parameter, final String value) { - sendCommand(CONFIG, Keyword.SET.name(), parameter, value); + public void configSet(final byte[] parameter, final byte[] value) { + sendCommand(CONFIG, Keyword.SET.raw, parameter, value); } public void strlen(final byte[] key) { - sendCommand(STRLEN, key); + sendCommand(STRLEN, key); } public void sync() { - sendCommand(SYNC); + sendCommand(SYNC); } - public void lpushx(final byte[] key, final byte[] string) { - sendCommand(LPUSHX, key, string); + public void lpushx(final byte[] key, final byte[]... string) { + sendCommand(LPUSHX, joinParameters(key, string)); } public void persist(final byte[] key) { - sendCommand(PERSIST, key); + sendCommand(PERSIST, key); } - public void rpushx(final byte[] key, final byte[] string) { - sendCommand(RPUSHX, key, string); + public void rpushx(final byte[] key, final byte[]... string) { + sendCommand(RPUSHX, joinParameters(key, string)); } public void echo(final byte[] string) { - sendCommand(ECHO, string); + sendCommand(ECHO, string); } public void linsert(final byte[] key, final LIST_POSITION where, - final byte[] pivot, final byte[] value) { - sendCommand(LINSERT, key, where.raw, pivot, value); + final byte[] pivot, final byte[] value) { + sendCommand(LINSERT, key, where.raw, pivot, value); } public void debug(final DebugParams params) { - sendCommand(DEBUG, params.getCommand()); + sendCommand(DEBUG, params.getCommand()); } public void brpoplpush(final byte[] source, final byte[] destination, - final int timeout) { - sendCommand(BRPOPLPUSH, source, destination, toByteArray(timeout)); + final int timeout) { + sendCommand(BRPOPLPUSH, source, destination, toByteArray(timeout)); } public void configResetStat() { - sendCommand(CONFIG, Keyword.RESETSTAT.name()); + sendCommand(CONFIG, Keyword.RESETSTAT.name()); } public void setbit(byte[] key, long offset, byte[] value) { - sendCommand(SETBIT, key, toByteArray(offset), value); + sendCommand(SETBIT, key, toByteArray(offset), value); + } + + public void setbit(byte[] key, long offset, boolean value) { + sendCommand(SETBIT, key, toByteArray(offset), toByteArray(value)); } public void getbit(byte[] key, long offset) { - sendCommand(GETBIT, key, toByteArray(offset)); + sendCommand(GETBIT, key, toByteArray(offset)); } public void setrange(byte[] key, long offset, byte[] value) { - sendCommand(SETRANGE, key, toByteArray(offset), value); + sendCommand(SETRANGE, key, toByteArray(offset), value); } public void getrange(byte[] key, long startOffset, long endOffset) { - sendCommand(GETRANGE, key, toByteArray(startOffset), - toByteArray(endOffset)); + sendCommand(GETRANGE, key, toByteArray(startOffset), + toByteArray(endOffset)); } public Long getDB() { - return db; + return db; } public void disconnect() { - db = 0; - super.disconnect(); + db = 0; + super.disconnect(); + } + + private void sendEvalCommand(Command command, byte[] script, + byte[] keyCount, byte[][] params) { + + final byte[][] allArgs = new byte[params.length + 2][]; + + allArgs[0] = script; + allArgs[1] = keyCount; + + for (int i = 0; i < params.length; i++) + allArgs[i + 2] = params[i]; + + sendCommand(command, allArgs); + } + + public void eval(byte[] script, byte[] keyCount, byte[][] params) { + sendEvalCommand(EVAL, script, keyCount, params); + } + + public void eval(byte[] script, int keyCount, byte[]... params) { + eval(script, toByteArray(keyCount), params); + } + + public void evalsha(byte[] sha1, byte[] keyCount, byte[]... params) { + sendEvalCommand(EVALSHA, sha1, keyCount, params); + } + + public void evalsha(byte[] sha1, int keyCount, byte[]... params) { + sendEvalCommand(EVALSHA, sha1, toByteArray(keyCount), params); + } + + public void scriptFlush() { + sendCommand(SCRIPT, Keyword.FLUSH.raw); + } + + public void scriptExists(byte[]... sha1) { + byte[][] args = new byte[sha1.length + 1][]; + args[0] = Keyword.EXISTS.raw; + for (int i = 0; i < sha1.length; i++) + args[i + 1] = sha1[i]; + + sendCommand(SCRIPT, args); + } + + public void scriptLoad(byte[] script) { + sendCommand(SCRIPT, Keyword.LOAD.raw, script); + } + + public void scriptKill() { + sendCommand(SCRIPT, Keyword.KILL.raw); + } + + public void slowlogGet() { + sendCommand(SLOWLOG, Keyword.GET.raw); + } + + public void slowlogGet(long entries) { + sendCommand(SLOWLOG, Keyword.GET.raw, toByteArray(entries)); + } + + public void slowlogReset() { + sendCommand(SLOWLOG, RESET.raw); + } + + public void slowlogLen() { + sendCommand(SLOWLOG, LEN.raw); + } + + public void objectRefcount(byte[] key) { + sendCommand(OBJECT, REFCOUNT.raw, key); + } + + public void objectIdletime(byte[] key) { + sendCommand(OBJECT, IDLETIME.raw, key); + } + + public void objectEncoding(byte[] key) { + sendCommand(OBJECT, ENCODING.raw, key); + } + + public void bitcount(byte[] key) { + sendCommand(BITCOUNT, key); + } + + public void bitcount(byte[] key, long start, long end) { + sendCommand(BITCOUNT, key, toByteArray(start), toByteArray(end)); + } + + public void bitop(BitOP op, byte[] destKey, byte[]... srcKeys) { + Keyword kw = Keyword.AND; + int len = srcKeys.length; + switch (op) { + case AND: + kw = Keyword.AND; + break; + case OR: + kw = Keyword.OR; + break; + case XOR: + kw = Keyword.XOR; + break; + case NOT: + kw = Keyword.NOT; + len = Math.min(1, len); + break; + } + + byte[][] bargs = new byte[len + 2][]; + bargs[0] = kw.raw; + bargs[1] = destKey; + for (int i = 0; i < len; ++i) { + bargs[i + 2] = srcKeys[i]; + } + + sendCommand(BITOP, bargs); + } + + public void sentinel(final byte[]... args) { + sendCommand(SENTINEL, args); + } + + public void dump(final byte[] key) { + sendCommand(DUMP, key); + } + + public void restore(final byte[] key, final int ttl, final byte[] serializedValue) { + sendCommand(RESTORE, key, toByteArray(ttl), serializedValue); + } + + public void pexpire(final byte[] key, final int milliseconds) { + sendCommand(PEXPIRE, key, toByteArray(milliseconds)); + } + + public void pexpireAt(final byte[] key, final long millisecondsTimestamp) { + sendCommand(PEXPIREAT, key, toByteArray(millisecondsTimestamp)); + } + + public void pttl(final byte[] key) { + sendCommand(PTTL, key); + } + + public void incrByFloat(final byte[] key, final double increment) { + sendCommand(INCRBYFLOAT, key, toByteArray(increment)); + } + + public void psetex(final byte[] key, final int milliseconds, final byte[] value) { + sendCommand(PSETEX, key, toByteArray(milliseconds), value); + } + + public void set(final byte[] key, final byte[] value, final byte[] nxxx) { + sendCommand(Command.SET, key, value, nxxx); + } + + public void set(final byte[] key, final byte[] value, final byte[] nxxx, final byte[] expx, final int time) { + sendCommand(Command.SET, key, value, nxxx, expx, toByteArray(time)); + } + + public void srandmember(final byte[] key, final int count) { + sendCommand(SRANDMEMBER, key, toByteArray(count)); + } + + public void clientKill(final byte[] client) { + sendCommand(CLIENT, Keyword.KILL.raw, client); + } + + public void clientGetname() { + sendCommand(CLIENT, Keyword.GETNAME.raw); + } + + public void clientList() { + sendCommand(CLIENT, Keyword.LIST.raw); + } + + public void clientSetname(final byte[] name) { + sendCommand(CLIENT, Keyword.SETNAME.raw, name); + } + + public void time() { + sendCommand(TIME); + } + + public void migrate(final byte[] host, final int port, final byte[] key, final int destinationDb, final int timeout) { + sendCommand(MIGRATE, host, toByteArray(port), key, toByteArray(destinationDb), toByteArray(timeout)); + } + + public void hincrByFloat(final byte[] key, final byte[] field, double increment) { + sendCommand(HINCRBYFLOAT, key, field, toByteArray(increment)); } } diff --git a/src/main/java/redis/clients/jedis/BinaryJedis.java b/src/main/java/redis/clients/jedis/BinaryJedis.java index 6b6c626..89b42ae 100644 --- a/src/main/java/redis/clients/jedis/BinaryJedis.java +++ b/src/main/java/redis/clients/jedis/BinaryJedis.java @@ -1,5 +1,8 @@ package redis.clients.jedis; +import static redis.clients.jedis.Protocol.toByteArray; + +import java.net.URI; import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; @@ -14,32 +17,49 @@ import redis.clients.jedis.exceptions.JedisException; import redis.clients.util.JedisByteHashMap; import redis.clients.util.SafeEncoder; -public class BinaryJedis implements BinaryJedisCommands { +public class BinaryJedis implements BasicCommands, BinaryJedisCommands, MultiKeyBinaryCommands, AdvancedBinaryJedisCommands, BinaryScriptingCommands { protected Client client = null; public BinaryJedis(final String host) { - client = new Client(host); + URI uri = URI.create(host); + if (uri.getScheme() != null && uri.getScheme().equals("redis")) { + client = new Client(uri.getHost(), uri.getPort()); + client.auth(uri.getUserInfo().split(":", 2)[1]); + client.getStatusCodeReply(); + client.select(Integer.parseInt(uri.getPath().split("/", 2)[1])); + client.getStatusCodeReply(); + } else { + client = new Client(host); + } } public BinaryJedis(final String host, final int port) { - client = new Client(host, port); + client = new Client(host, port); } public BinaryJedis(final String host, final int port, final int timeout) { - client = new Client(host, port); - client.setTimeout(timeout); + client = new Client(host, port); + client.setTimeout(timeout); } public BinaryJedis(final JedisShardInfo shardInfo) { - client = new Client(shardInfo.getHost(), shardInfo.getPort()); - client.setTimeout(shardInfo.getTimeout()); - client.setPassword(shardInfo.getPassword()); + client = new Client(shardInfo.getHost(), shardInfo.getPort()); + client.setTimeout(shardInfo.getTimeout()); + client.setPassword(shardInfo.getPassword()); + } + + public BinaryJedis(URI uri) { + client = new Client(uri.getHost(), uri.getPort()); + client.auth(uri.getUserInfo().split(":", 2)[1]); + client.getStatusCodeReply(); + client.select(Integer.parseInt(uri.getPath().split("/", 2)[1])); + client.getStatusCodeReply(); } public String ping() { - checkIsInMulti(); - client.ping(); - return client.getStatusCodeReply(); + checkIsInMulti(); + client.ping(); + return client.getStatusCodeReply(); } /** @@ -53,9 +73,9 @@ public class BinaryJedis implements BinaryJedisCommands { * @return Status code reply */ public String set(final byte[] key, final byte[] value) { - checkIsInMulti(); - client.set(key, value); - return client.getStatusCodeReply(); + checkIsInMulti(); + client.set(key, value); + return client.getStatusCodeReply(); } /** @@ -69,23 +89,23 @@ public class BinaryJedis implements BinaryJedisCommands { * @return Bulk reply */ public byte[] get(final byte[] key) { - checkIsInMulti(); - client.get(key); - return client.getBinaryBulkReply(); + checkIsInMulti(); + client.get(key); + return client.getBinaryBulkReply(); } /** * Ask the server to silently close the connection. */ public String quit() { - checkIsInMulti(); - client.quit(); - return client.getStatusCodeReply(); + checkIsInMulti(); + client.quit(); + return client.getStatusCodeReply(); } /** - * Test if the specified key exists. The command returns "0" if the key - * exists, otherwise "1" is returned. Note that even keys set with an empty + * Test if the specified key exists. The command returns "1" if the key + * exists, otherwise "0" is returned. Note that even keys set with an empty * string as value will return "1". * * Time complexity: O(1) @@ -94,9 +114,9 @@ public class BinaryJedis implements BinaryJedisCommands { * @return Integer reply, "1" if the key exists, otherwise "0" */ public Boolean exists(final byte[] key) { - checkIsInMulti(); - client.exists(key); - return client.getIntegerReply() == 1; + checkIsInMulti(); + client.exists(key); + return client.getIntegerReply() == 1; } /** @@ -110,9 +130,15 @@ public class BinaryJedis implements BinaryJedisCommands { * more keys were removed 0 if none of the specified key existed */ public Long del(final byte[]... keys) { - checkIsInMulti(); - client.del(keys); - return client.getIntegerReply(); + checkIsInMulti(); + client.del(keys); + return client.getIntegerReply(); + } + + public Long del(final byte[] key) { + checkIsInMulti(); + client.del(key); + return client.getIntegerReply(); } /** @@ -130,9 +156,9 @@ public class BinaryJedis implements BinaryJedisCommands { * contains a Hash value */ public String type(final byte[] key) { - checkIsInMulti(); - client.type(key); - return client.getStatusCodeReply(); + checkIsInMulti(); + client.type(key); + return client.getStatusCodeReply(); } /** @@ -142,9 +168,9 @@ public class BinaryJedis implements BinaryJedisCommands { * @return Status code reply */ public String flushDB() { - checkIsInMulti(); - client.flushDB(); - return client.getStatusCodeReply(); + checkIsInMulti(); + client.flushDB(); + return client.getStatusCodeReply(); } /** @@ -179,11 +205,11 @@ public class BinaryJedis implements BinaryJedisCommands { * @return Multi bulk reply */ public Set keys(final byte[] pattern) { - checkIsInMulti(); - client.keys(pattern); - final HashSet keySet = new HashSet(client - .getBinaryMultiBulkReply()); - return keySet; + checkIsInMulti(); + client.keys(pattern); + final HashSet keySet = new HashSet( + client.getBinaryMultiBulkReply()); + return keySet; } /** @@ -195,9 +221,9 @@ public class BinaryJedis implements BinaryJedisCommands { * empty string is the database is empty */ public byte[] randomBinaryKey() { - checkIsInMulti(); - client.randomKey(); - return client.getBinaryBulkReply(); + checkIsInMulti(); + client.randomKey(); + return client.getBinaryBulkReply(); } /** @@ -212,9 +238,9 @@ public class BinaryJedis implements BinaryJedisCommands { * @return Status code repy */ public String rename(final byte[] oldkey, final byte[] newkey) { - checkIsInMulti(); - client.rename(oldkey, newkey); - return client.getStatusCodeReply(); + checkIsInMulti(); + client.rename(oldkey, newkey); + return client.getStatusCodeReply(); } /** @@ -229,9 +255,9 @@ public class BinaryJedis implements BinaryJedisCommands { * target key already exist */ public Long renamenx(final byte[] oldkey, final byte[] newkey) { - checkIsInMulti(); - client.renamenx(oldkey, newkey); - return client.getIntegerReply(); + checkIsInMulti(); + client.renamenx(oldkey, newkey); + return client.getIntegerReply(); } /** @@ -240,9 +266,9 @@ public class BinaryJedis implements BinaryJedisCommands { * @return Integer reply */ public Long dbSize() { - checkIsInMulti(); - client.dbSize(); - return client.getIntegerReply(); + checkIsInMulti(); + client.dbSize(); + return client.getIntegerReply(); } /** @@ -258,7 +284,7 @@ public class BinaryJedis implements BinaryJedisCommands { *

* Since Redis 2.1.3 you can update the value of the timeout of a key * already having an expire set. It is also possible to undo the expire at - * all turning the key into a normal key using the {@link #persist(String) + * all turning the key into a normal key using the {@link #persist(byte[]) * PERSIST} command. *

* Time complexity: O(1) @@ -274,13 +300,13 @@ public class BinaryJedis implements BinaryJedisCommands { * exist. */ public Long expire(final byte[] key, final int seconds) { - checkIsInMulti(); - client.expire(key, seconds); - return client.getIntegerReply(); + checkIsInMulti(); + client.expire(key, seconds); + return client.getIntegerReply(); } /** - * EXPIREAT works exctly like {@link #expire(String, int) EXPIRE} but + * EXPIREAT works exctly like {@link #expire(byte[], int) EXPIRE} but * instead to get the number of seconds representing the Time To Live of the * key as a second argument (that is a relative way of specifing the TTL), * it takes an absolute one in the form of a UNIX timestamp (Number of @@ -294,7 +320,7 @@ public class BinaryJedis implements BinaryJedisCommands { *

* Since Redis 2.1.3 you can update the value of the timeout of a key * already having an expire set. It is also possible to undo the expire at - * all turning the key into a normal key using the {@link #persist(String) + * all turning the key into a normal key using the {@link #persist(byte[]) * PERSIST} command. *

* Time complexity: O(1) @@ -310,14 +336,14 @@ public class BinaryJedis implements BinaryJedisCommands { * exist. */ public Long expireAt(final byte[] key, final long unixTime) { - checkIsInMulti(); - client.expireAt(key, unixTime); - return client.getIntegerReply(); + checkIsInMulti(); + client.expireAt(key, unixTime); + return client.getIntegerReply(); } /** * The TTL command returns the remaining time to live in seconds of a key - * that has an {@link #expire(String, int) EXPIRE} set. This introspection + * that has an {@link #expire(byte[], int) EXPIRE} set. This introspection * capability allows a Redis client to check how many seconds a given key * will continue to be part of the dataset. * @@ -327,9 +353,9 @@ public class BinaryJedis implements BinaryJedisCommands { * have an associated expire, -1 is returned. */ public Long ttl(final byte[] key) { - checkIsInMulti(); - client.ttl(key); - return client.getIntegerReply(); + checkIsInMulti(); + client.ttl(key); + return client.getIntegerReply(); } /** @@ -340,9 +366,9 @@ public class BinaryJedis implements BinaryJedisCommands { * @return Status code reply */ public String select(final int index) { - checkIsInMulti(); - client.select(index); - return client.getStatusCodeReply(); + checkIsInMulti(); + client.select(index); + return client.getStatusCodeReply(); } /** @@ -359,9 +385,9 @@ public class BinaryJedis implements BinaryJedisCommands { * found in the current DB. */ public Long move(final byte[] key, final int dbIndex) { - checkIsInMulti(); - client.move(key, dbIndex); - return client.getIntegerReply(); + checkIsInMulti(); + client.move(key, dbIndex); + return client.getIntegerReply(); } /** @@ -371,9 +397,9 @@ public class BinaryJedis implements BinaryJedisCommands { * @return Status code reply */ public String flushAll() { - checkIsInMulti(); - client.flushAll(); - return client.getStatusCodeReply(); + checkIsInMulti(); + client.flushAll(); + return client.getStatusCodeReply(); } /** @@ -388,9 +414,9 @@ public class BinaryJedis implements BinaryJedisCommands { * @return Bulk reply */ public byte[] getSet(final byte[] key, final byte[] value) { - checkIsInMulti(); - client.getSet(key, value); - return client.getBinaryBulkReply(); + checkIsInMulti(); + client.getSet(key, value); + return client.getBinaryBulkReply(); } /** @@ -404,13 +430,13 @@ public class BinaryJedis implements BinaryJedisCommands { * @return Multi bulk reply */ public List mget(final byte[]... keys) { - checkIsInMulti(); - client.mget(keys); - return client.getBinaryMultiBulkReply(); + checkIsInMulti(); + client.mget(keys); + return client.getBinaryMultiBulkReply(); } /** - * SETNX works exactly like {@link #set(String, String) SET} with the only + * SETNX works exactly like {@link #set(byte[], byte[]) SET} with the only * difference that if the key already exists no operation is performed. * SETNX actually means "SET if Not eXists". *

@@ -422,14 +448,14 @@ public class BinaryJedis implements BinaryJedisCommands { * was not set */ public Long setnx(final byte[] key, final byte[] value) { - checkIsInMulti(); - client.setnx(key, value); - return client.getIntegerReply(); + checkIsInMulti(); + client.setnx(key, value); + return client.getIntegerReply(); } /** * The command is exactly equivalent to the following group of commands: - * {@link #set(String, String) SET} + {@link #expire(String, int) EXPIRE}. + * {@link #set(byte[], byte[]) SET} + {@link #expire(byte[], int) EXPIRE}. * The operation is atomic. *

* Time complexity: O(1) @@ -440,9 +466,9 @@ public class BinaryJedis implements BinaryJedisCommands { * @return Status code reply */ public String setex(final byte[] key, final int seconds, final byte[] value) { - checkIsInMulti(); - client.setex(key, seconds, value); - return client.getStatusCodeReply(); + checkIsInMulti(); + client.setex(key, seconds, value); + return client.getStatusCodeReply(); } /** @@ -466,9 +492,9 @@ public class BinaryJedis implements BinaryJedisCommands { * @return Status code reply Basically +OK as MSET can't fail */ public String mset(final byte[]... keysvalues) { - checkIsInMulti(); - client.mset(keysvalues); - return client.getStatusCodeReply(); + checkIsInMulti(); + client.mset(keysvalues); + return client.getStatusCodeReply(); } /** @@ -493,9 +519,9 @@ public class BinaryJedis implements BinaryJedisCommands { * no key was set (at least one key already existed) */ public Long msetnx(final byte[]... keysvalues) { - checkIsInMulti(); - client.msetnx(keysvalues); - return client.getIntegerReply(); + checkIsInMulti(); + client.msetnx(keysvalues); + return client.getIntegerReply(); } /** @@ -511,9 +537,9 @@ public class BinaryJedis implements BinaryJedisCommands { *

* Time complexity: O(1) * - * @see #incr(String) - * @see #decr(String) - * @see #incrBy(String, int) + * @see #incr(byte[]) + * @see #decr(byte[]) + * @see #incrBy(byte[], long) * * @param key * @param integer @@ -521,9 +547,9 @@ public class BinaryJedis implements BinaryJedisCommands { * after the increment. */ public Long decrBy(final byte[] key, final long integer) { - checkIsInMulti(); - client.decrBy(key, integer); - return client.getIntegerReply(); + checkIsInMulti(); + client.decrBy(key, integer); + return client.getIntegerReply(); } /** @@ -540,22 +566,22 @@ public class BinaryJedis implements BinaryJedisCommands { *

* Time complexity: O(1) * - * @see #incr(String) - * @see #incrBy(String, int) - * @see #decrBy(String, int) + * @see #incr(byte[]) + * @see #incrBy(byte[], long) + * @see #decrBy(byte[], long) * * @param key * @return Integer reply, this commands will reply with the new value of key * after the increment. */ public Long decr(final byte[] key) { - checkIsInMulti(); - client.decr(key); - return client.getIntegerReply(); + checkIsInMulti(); + client.decr(key); + return client.getIntegerReply(); } /** - * INCRBY work just like {@link #incr(String) INCR} but instead to increment + * INCRBY work just like {@link #incr(byte[]) INCR} but instead to increment * by 1 the increment is integer. *

* INCR commands are limited to 64 bit signed integers. @@ -567,9 +593,9 @@ public class BinaryJedis implements BinaryJedisCommands { *

* Time complexity: O(1) * - * @see #incr(String) - * @see #decr(String) - * @see #decrBy(String, int) + * @see #incr(byte[]) + * @see #decr(byte[]) + * @see #decrBy(byte[], long) * * @param key * @param integer @@ -577,9 +603,9 @@ public class BinaryJedis implements BinaryJedisCommands { * after the increment. */ public Long incrBy(final byte[] key, final long integer) { - checkIsInMulti(); - client.incrBy(key, integer); - return client.getIntegerReply(); + checkIsInMulti(); + client.incrBy(key, integer); + return client.getIntegerReply(); } /** @@ -596,18 +622,18 @@ public class BinaryJedis implements BinaryJedisCommands { *

* Time complexity: O(1) * - * @see #incrBy(String, int) - * @see #decr(String) - * @see #decrBy(String, int) + * @see #incrBy(byte[], long) + * @see #decr(byte[]) + * @see #decrBy(byte[], long) * * @param key * @return Integer reply, this commands will reply with the new value of key * after the increment. */ public Long incr(final byte[] key) { - checkIsInMulti(); - client.incr(key); - return client.getIntegerReply(); + checkIsInMulti(); + client.incr(key); + return client.getIntegerReply(); } /** @@ -627,9 +653,9 @@ public class BinaryJedis implements BinaryJedisCommands { * the append operation. */ public Long append(final byte[] key, final byte[] value) { - checkIsInMulti(); - client.append(key, value); - return client.getIntegerReply(); + checkIsInMulti(); + client.append(key, value); + return client.getIntegerReply(); } /** @@ -651,9 +677,9 @@ public class BinaryJedis implements BinaryJedisCommands { * @return Bulk reply */ public byte[] substr(final byte[] key, final int start, final int end) { - checkIsInMulti(); - client.substr(key, start, end); - return client.getBinaryBulkReply(); + checkIsInMulti(); + client.substr(key, start, end); + return client.getBinaryBulkReply(); } /** @@ -672,9 +698,9 @@ public class BinaryJedis implements BinaryJedisCommands { * 1 is returned. */ public Long hset(final byte[] key, final byte[] field, final byte[] value) { - checkIsInMulti(); - client.hset(key, field, value); - return client.getIntegerReply(); + checkIsInMulti(); + client.hset(key, field, value); + return client.getIntegerReply(); } /** @@ -691,9 +717,9 @@ public class BinaryJedis implements BinaryJedisCommands { * @return Bulk reply */ public byte[] hget(final byte[] key, final byte[] field) { - checkIsInMulti(); - client.hget(key, field); - return client.getBinaryBulkReply(); + checkIsInMulti(); + client.hget(key, field); + return client.getBinaryBulkReply(); } /** @@ -708,9 +734,9 @@ public class BinaryJedis implements BinaryJedisCommands { * field is created 1 is returned. */ public Long hsetnx(final byte[] key, final byte[] field, final byte[] value) { - checkIsInMulti(); - client.hsetnx(key, field, value); - return client.getIntegerReply(); + checkIsInMulti(); + client.hsetnx(key, field, value); + return client.getIntegerReply(); } /** @@ -726,9 +752,9 @@ public class BinaryJedis implements BinaryJedisCommands { * @return Always OK because HMSET can't fail */ public String hmset(final byte[] key, final Map hash) { - checkIsInMulti(); - client.hmset(key, hash); - return client.getStatusCodeReply(); + checkIsInMulti(); + client.hmset(key, hash); + return client.getStatusCodeReply(); } /** @@ -745,9 +771,9 @@ public class BinaryJedis implements BinaryJedisCommands { * with the specified fields, in the same order of the request. */ public List hmget(final byte[] key, final byte[]... fields) { - checkIsInMulti(); - client.hmget(key, fields); - return client.getBinaryMultiBulkReply(); + checkIsInMulti(); + client.hmget(key, fields); + return client.getBinaryMultiBulkReply(); } /** @@ -769,9 +795,9 @@ public class BinaryJedis implements BinaryJedisCommands { * operation. */ public Long hincrBy(final byte[] key, final byte[] field, final long value) { - checkIsInMulti(); - client.hincrBy(key, field, value); - return client.getIntegerReply(); + checkIsInMulti(); + client.hincrBy(key, field, value); + return client.getIntegerReply(); } /** @@ -785,9 +811,9 @@ public class BinaryJedis implements BinaryJedisCommands { * Return 0 if the key is not found or the field is not present. */ public Boolean hexists(final byte[] key, final byte[] field) { - checkIsInMulti(); - client.hexists(key, field); - return client.getIntegerReply() == 1; + checkIsInMulti(); + client.hexists(key, field); + return client.getIntegerReply() == 1; } /** @@ -796,14 +822,14 @@ public class BinaryJedis implements BinaryJedisCommands { * Time complexity: O(1) * * @param key - * @param field + * @param fields * @return If the field was present in the hash it is deleted and 1 is * returned, otherwise 0 is returned and no operation is performed. */ - public Long hdel(final byte[] key, final byte[] field) { - checkIsInMulti(); - client.hdel(key, field); - return client.getIntegerReply(); + public Long hdel(final byte[] key, final byte[]... fields) { + checkIsInMulti(); + client.hdel(key, fields); + return client.getIntegerReply(); } /** @@ -817,9 +843,9 @@ public class BinaryJedis implements BinaryJedisCommands { * an empty hash. */ public Long hlen(final byte[] key) { - checkIsInMulti(); - client.hlen(key); - return client.getIntegerReply(); + checkIsInMulti(); + client.hlen(key); + return client.getIntegerReply(); } /** @@ -831,10 +857,10 @@ public class BinaryJedis implements BinaryJedisCommands { * @return All the fields names contained into a hash. */ public Set hkeys(final byte[] key) { - checkIsInMulti(); - client.hkeys(key); - final List lresult = client.getBinaryMultiBulkReply(); - return new HashSet(lresult); + checkIsInMulti(); + client.hkeys(key); + final List lresult = client.getBinaryMultiBulkReply(); + return new HashSet(lresult); } /** @@ -846,10 +872,10 @@ public class BinaryJedis implements BinaryJedisCommands { * @return All the fields values contained into a hash. */ public List hvals(final byte[] key) { - checkIsInMulti(); - client.hvals(key); - final List lresult = client.getBinaryMultiBulkReply(); - return lresult; + checkIsInMulti(); + client.hvals(key); + final List lresult = client.getBinaryMultiBulkReply(); + return lresult; } /** @@ -861,16 +887,16 @@ public class BinaryJedis implements BinaryJedisCommands { * @return All the fields and values contained into a hash. */ public Map hgetAll(final byte[] key) { - checkIsInMulti(); - client.hgetAll(key); - final List flatHash = client.getBinaryMultiBulkReply(); - final Map hash = new JedisByteHashMap(); - final Iterator iterator = flatHash.iterator(); - while (iterator.hasNext()) { - hash.put(iterator.next(), iterator.next()); - } + checkIsInMulti(); + client.hgetAll(key); + final List flatHash = client.getBinaryMultiBulkReply(); + final Map hash = new JedisByteHashMap(); + final Iterator iterator = flatHash.iterator(); + while (iterator.hasNext()) { + hash.put(iterator.next(), iterator.next()); + } - return hash; + return hash; } /** @@ -881,17 +907,17 @@ public class BinaryJedis implements BinaryJedisCommands { *

* Time complexity: O(1) * - * @see BinaryJedis#lpush(String, String) + * @see BinaryJedis#rpush(byte[], byte[]...) * * @param key - * @param string + * @param strings * @return Integer reply, specifically, the number of elements inside the * list after the push operation. */ - public Long rpush(final byte[] key, final byte[] string) { - checkIsInMulti(); - client.rpush(key, string); - return client.getIntegerReply(); + public Long rpush(final byte[] key, final byte[]... strings) { + checkIsInMulti(); + client.rpush(key, strings); + return client.getIntegerReply(); } /** @@ -902,17 +928,17 @@ public class BinaryJedis implements BinaryJedisCommands { *

* Time complexity: O(1) * - * @see BinaryJedis#rpush(String, String) + * @see BinaryJedis#rpush(byte[], byte[]...) * * @param key - * @param string + * @param strings * @return Integer reply, specifically, the number of elements inside the * list after the push operation. */ - public Long lpush(final byte[] key, final byte[] string) { - checkIsInMulti(); - client.lpush(key, string); - return client.getIntegerReply(); + public Long lpush(final byte[] key, final byte[]... strings) { + checkIsInMulti(); + client.lpush(key, strings); + return client.getIntegerReply(); } /** @@ -926,9 +952,9 @@ public class BinaryJedis implements BinaryJedisCommands { * @return The length of the list. */ public Long llen(final byte[] key) { - checkIsInMulti(); - client.llen(key); - return client.getIntegerReply(); + checkIsInMulti(); + client.llen(key); + return client.getIntegerReply(); } /** @@ -969,10 +995,10 @@ public class BinaryJedis implements BinaryJedisCommands { * @return Multi bulk reply, specifically a list of elements in the * specified range. */ - public List lrange(final byte[] key, final int start, final int end) { - checkIsInMulti(); - client.lrange(key, start, end); - return client.getBinaryMultiBulkReply(); + public List lrange(final byte[] key, final long start, final long end) { + checkIsInMulti(); + client.lrange(key, start, end); + return client.getBinaryMultiBulkReply(); } /** @@ -1009,10 +1035,10 @@ public class BinaryJedis implements BinaryJedisCommands { * @param end * @return Status code reply */ - public String ltrim(final byte[] key, final int start, final int end) { - checkIsInMulti(); - client.ltrim(key, start, end); - return client.getStatusCodeReply(); + public String ltrim(final byte[] key, final long start, final long end) { + checkIsInMulti(); + client.ltrim(key, start, end); + return client.getStatusCodeReply(); } /** @@ -1033,10 +1059,10 @@ public class BinaryJedis implements BinaryJedisCommands { * @param index * @return Bulk reply, specifically the requested element */ - public byte[] lindex(final byte[] key, final int index) { - checkIsInMulti(); - client.lindex(key, index); - return client.getBinaryBulkReply(); + public byte[] lindex(final byte[] key, final long index) { + checkIsInMulti(); + client.lindex(key, index); + return client.getBinaryBulkReply(); } /** @@ -1053,17 +1079,17 @@ public class BinaryJedis implements BinaryJedisCommands { * O(N) (with N being the length of the list), setting the first or last * elements of the list is O(1). * - * @see #lindex(String, int) + * @see #lindex(byte[], int) * * @param key * @param index * @param value * @return Status code reply */ - public String lset(final byte[] key, final int index, final byte[] value) { - checkIsInMulti(); - client.lset(key, index, value); - return client.getStatusCodeReply(); + public String lset(final byte[] key, final long index, final byte[] value) { + checkIsInMulti(); + client.lset(key, index, value); + return client.getStatusCodeReply(); } /** @@ -1071,7 +1097,7 @@ public class BinaryJedis implements BinaryJedisCommands { * count is zero all the elements are removed. If count is negative elements * are removed from tail to head, instead to go from head to tail that is * the normal behaviour. So for example LREM with count -2 and hello as - * value to remove against the list (a,b,c,hello,x,hello,hello) will lave + * value to remove against the list (a,b,c,hello,x,hello,hello) will have * the list (a,b,c,hello,x). The number of removed elements is returned as * an integer, see below for more information about the returned value. Note * that non existing keys are considered like empty lists by LREM, so LREM @@ -1085,10 +1111,10 @@ public class BinaryJedis implements BinaryJedisCommands { * @return Integer Reply, specifically: The number of removed elements if * the operation succeeded */ - public Long lrem(final byte[] key, final int count, final byte[] value) { - checkIsInMulti(); - client.lrem(key, count, value); - return client.getIntegerReply(); + public Long lrem(final byte[] key, final long count, final byte[] value) { + checkIsInMulti(); + client.lrem(key, count, value); + return client.getIntegerReply(); } /** @@ -1099,15 +1125,15 @@ public class BinaryJedis implements BinaryJedisCommands { * If the key does not exist or the list is already empty the special value * 'nil' is returned. * - * @see #rpop(String) + * @see #rpop(byte[]) * * @param key * @return Bulk reply */ public byte[] lpop(final byte[] key) { - checkIsInMulti(); - client.lpop(key); - return client.getBinaryBulkReply(); + checkIsInMulti(); + client.lpop(key); + return client.getBinaryBulkReply(); } /** @@ -1118,15 +1144,15 @@ public class BinaryJedis implements BinaryJedisCommands { * If the key does not exist or the list is already empty the special value * 'nil' is returned. * - * @see #lpop(String) + * @see #lpop(byte[]) * * @param key * @return Bulk reply */ public byte[] rpop(final byte[] key) { - checkIsInMulti(); - client.rpop(key); - return client.getBinaryBulkReply(); + checkIsInMulti(); + client.rpop(key); + return client.getBinaryBulkReply(); } /** @@ -1148,9 +1174,9 @@ public class BinaryJedis implements BinaryJedisCommands { * @return Bulk reply */ public byte[] rpoplpush(final byte[] srckey, final byte[] dstkey) { - checkIsInMulti(); - client.rpoplpush(srckey, dstkey); - return client.getBinaryBulkReply(); + checkIsInMulti(); + client.rpoplpush(srckey, dstkey); + return client.getBinaryBulkReply(); } /** @@ -1162,14 +1188,14 @@ public class BinaryJedis implements BinaryJedisCommands { * Time complexity O(1) * * @param key - * @param member + * @param members * @return Integer reply, specifically: 1 if the new element was added 0 if * the element was already a member of the set */ - public Long sadd(final byte[] key, final byte[] member) { - checkIsInMulti(); - client.sadd(key, member); - return client.getIntegerReply(); + public Long sadd(final byte[] key, final byte[]... members) { + checkIsInMulti(); + client.sadd(key, members); + return client.getIntegerReply(); } /** @@ -1182,10 +1208,10 @@ public class BinaryJedis implements BinaryJedisCommands { * @return Multi bulk reply */ public Set smembers(final byte[] key) { - checkIsInMulti(); - client.smembers(key); - final List members = client.getBinaryMultiBulkReply(); - return new HashSet(members); + checkIsInMulti(); + client.smembers(key); + final List members = client.getBinaryMultiBulkReply(); + return new HashSet(members); } /** @@ -1200,17 +1226,17 @@ public class BinaryJedis implements BinaryJedisCommands { * @return Integer reply, specifically: 1 if the new element was removed 0 * if the new element was not a member of the set */ - public Long srem(final byte[] key, final byte[] member) { - checkIsInMulti(); - client.srem(key, member); - return client.getIntegerReply(); + public Long srem(final byte[] key, final byte[]... member) { + checkIsInMulti(); + client.srem(key, member); + return client.getIntegerReply(); } /** * Remove a random element from a Set returning it as return value. If the * Set is empty or the key does not exist, a nil object is returned. *

- * The {@link #srandmember(String)} command does a similar work but the + * The {@link #srandmember(byte[])} command does a similar work but the * returned element is not removed from the Set. *

* Time complexity O(1) @@ -1219,13 +1245,13 @@ public class BinaryJedis implements BinaryJedisCommands { * @return Bulk reply */ public byte[] spop(final byte[] key) { - checkIsInMulti(); - client.spop(key); - return client.getBinaryBulkReply(); + checkIsInMulti(); + client.spop(key); + return client.getBinaryBulkReply(); } /** - * Move the specifided member from the set at srckey to the set at dstkey. + * Move the specified member from the set at srckey to the set at dstkey. * This operation is atomic, in every given moment the element will appear * to be in the source or destination set for accessing clients. *

@@ -1248,10 +1274,10 @@ public class BinaryJedis implements BinaryJedisCommands { * performed */ public Long smove(final byte[] srckey, final byte[] dstkey, - final byte[] member) { - checkIsInMulti(); - client.smove(srckey, dstkey, member); - return client.getIntegerReply(); + final byte[] member) { + checkIsInMulti(); + client.smove(srckey, dstkey, member); + return client.getIntegerReply(); } /** @@ -1263,9 +1289,9 @@ public class BinaryJedis implements BinaryJedisCommands { * of the set as an integer. */ public Long scard(final byte[] key) { - checkIsInMulti(); - client.scard(key); - return client.getIntegerReply(); + checkIsInMulti(); + client.scard(key); + return client.getIntegerReply(); } /** @@ -1281,18 +1307,18 @@ public class BinaryJedis implements BinaryJedisCommands { * does not exist */ public Boolean sismember(final byte[] key, final byte[] member) { - checkIsInMulti(); - client.sismember(key, member); - return client.getIntegerReply() == 1; + checkIsInMulti(); + client.sismember(key, member); + return client.getIntegerReply() == 1; } /** * Return the members of a set resulting from the intersection of all the * sets hold at the specified keys. Like in - * {@link #lrange(String, int, int) LRANGE} the result is sent to the client + * {@link #lrange(byte[], int, int) LRANGE} the result is sent to the client * as a multi-bulk reply (see the protocol specification for more * information). If just a single key is specified, then this command - * produces the same result as {@link #smembers(String) SMEMBERS}. Actually + * produces the same result as {@link #smembers(byte[]) SMEMBERS}. Actually * SMEMBERS is just syntax sugar for SINTER. *

* Non existing keys are considered like empty sets, so if one of the keys @@ -1306,10 +1332,10 @@ public class BinaryJedis implements BinaryJedisCommands { * @return Multi bulk reply, specifically the list of common elements. */ public Set sinter(final byte[]... keys) { - checkIsInMulti(); - client.sinter(keys); - final List members = client.getBinaryMultiBulkReply(); - return new HashSet(members); + checkIsInMulti(); + client.sinter(keys); + final List members = client.getBinaryMultiBulkReply(); + return new HashSet(members); } /** @@ -1324,17 +1350,17 @@ public class BinaryJedis implements BinaryJedisCommands { * @return Status code reply */ public Long sinterstore(final byte[] dstkey, final byte[]... keys) { - checkIsInMulti(); - client.sinterstore(dstkey, keys); - return client.getIntegerReply(); + checkIsInMulti(); + client.sinterstore(dstkey, keys); + return client.getIntegerReply(); } /** * Return the members of a set resulting from the union of all the sets hold - * at the specified keys. Like in {@link #lrange(String, int, int) LRANGE} + * at the specified keys. Like in {@link #lrange(byte[], int, int) LRANGE} * the result is sent to the client as a multi-bulk reply (see the protocol * specification for more information). If just a single key is specified, - * then this command produces the same result as {@link #smembers(String) + * then this command produces the same result as {@link #smembers(byte[]) * SMEMBERS}. *

* Non existing keys are considered like empty sets. @@ -1346,10 +1372,10 @@ public class BinaryJedis implements BinaryJedisCommands { * @return Multi bulk reply, specifically the list of common elements. */ public Set sunion(final byte[]... keys) { - checkIsInMulti(); - client.sunion(keys); - final List members = client.getBinaryMultiBulkReply(); - return new HashSet(members); + checkIsInMulti(); + client.sunion(keys); + final List members = client.getBinaryMultiBulkReply(); + return new HashSet(members); } /** @@ -1365,9 +1391,9 @@ public class BinaryJedis implements BinaryJedisCommands { * @return Status code reply */ public Long sunionstore(final byte[] dstkey, final byte[]... keys) { - checkIsInMulti(); - client.sunionstore(dstkey, keys); - return client.getIntegerReply(); + checkIsInMulti(); + client.sunionstore(dstkey, keys); + return client.getIntegerReply(); } /** @@ -1394,10 +1420,10 @@ public class BinaryJedis implements BinaryJedisCommands { * the first set provided and all the successive sets. */ public Set sdiff(final byte[]... keys) { - checkIsInMulti(); - client.sdiff(keys); - final List members = client.getBinaryMultiBulkReply(); - return new HashSet(members); + checkIsInMulti(); + client.sdiff(keys); + final List members = client.getBinaryMultiBulkReply(); + return new HashSet(members); } /** @@ -1409,9 +1435,9 @@ public class BinaryJedis implements BinaryJedisCommands { * @return Status code reply */ public Long sdiffstore(final byte[] dstkey, final byte[]... keys) { - checkIsInMulti(); - client.sdiffstore(dstkey, keys); - return client.getIntegerReply(); + checkIsInMulti(); + client.sdiffstore(dstkey, keys); + return client.getIntegerReply(); } /** @@ -1427,9 +1453,15 @@ public class BinaryJedis implements BinaryJedisCommands { * @return Bulk reply */ public byte[] srandmember(final byte[] key) { - checkIsInMulti(); - client.srandmember(key); - return client.getBinaryBulkReply(); + checkIsInMulti(); + client.srandmember(key); + return client.getBinaryBulkReply(); + } + + public List srandmember(final byte[] key, final int count) { + checkIsInMulti(); + client.srandmember(key, count); + return client.getBinaryMultiBulkReply(); } /** @@ -1454,16 +1486,22 @@ public class BinaryJedis implements BinaryJedisCommands { * was updated */ public Long zadd(final byte[] key, final double score, final byte[] member) { - checkIsInMulti(); - client.zadd(key, score, member); - return client.getIntegerReply(); + checkIsInMulti(); + client.zadd(key, score, member); + return client.getIntegerReply(); } - public Set zrange(final byte[] key, final int start, final int end) { - checkIsInMulti(); - client.zrange(key, start, end); - final List members = client.getBinaryMultiBulkReply(); - return new LinkedHashSet(members); + public Long zadd(final byte[] key, final Map scoreMembers) { + checkIsInMulti(); + client.zaddBinary(key, scoreMembers); + return client.getIntegerReply(); + } + + public Set zrange(final byte[] key, final long start, final long end) { + checkIsInMulti(); + client.zrange(key, start, end); + final List members = client.getBinaryMultiBulkReply(); + return new LinkedHashSet(members); } /** @@ -1477,14 +1515,14 @@ public class BinaryJedis implements BinaryJedisCommands { * * * @param key - * @param member + * @param members * @return Integer reply, specifically: 1 if the new element was removed 0 * if the new element was not a member of the set */ - public Long zrem(final byte[] key, final byte[] member) { - checkIsInMulti(); - client.zrem(key, member); - return client.getIntegerReply(); + public Long zrem(final byte[] key, final byte[]... members) { + checkIsInMulti(); + client.zrem(key, members); + return client.getIntegerReply(); } /** @@ -1512,11 +1550,11 @@ public class BinaryJedis implements BinaryJedisCommands { * @return The new score */ public Double zincrby(final byte[] key, final double score, - final byte[] member) { - checkIsInMulti(); - client.zincrby(key, score, member); - String newscore = client.getBulkReply(); - return Double.valueOf(newscore); + final byte[] member) { + checkIsInMulti(); + client.zincrby(key, score, member); + String newscore = client.getBulkReply(); + return Double.valueOf(newscore); } /** @@ -1531,7 +1569,7 @@ public class BinaryJedis implements BinaryJedisCommands { *

* O(log(N)) * - * @see #zrevrank(String, String) + * @see #zrevrank(byte[], byte[]) * * @param key * @param member @@ -1540,9 +1578,9 @@ public class BinaryJedis implements BinaryJedisCommands { * reply if there is no such element. */ public Long zrank(final byte[] key, final byte[] member) { - checkIsInMulti(); - client.zrank(key, member); - return client.getIntegerReply(); + checkIsInMulti(); + client.zrank(key, member); + return client.getIntegerReply(); } /** @@ -1557,7 +1595,7 @@ public class BinaryJedis implements BinaryJedisCommands { *

* O(log(N)) * - * @see #zrank(String, String) + * @see #zrank(byte[], byte[]) * * @param key * @param member @@ -1566,33 +1604,33 @@ public class BinaryJedis implements BinaryJedisCommands { * reply if there is no such element. */ public Long zrevrank(final byte[] key, final byte[] member) { - checkIsInMulti(); - client.zrevrank(key, member); - return client.getIntegerReply(); + checkIsInMulti(); + client.zrevrank(key, member); + return client.getIntegerReply(); } - public Set zrevrange(final byte[] key, final int start, - final int end) { - checkIsInMulti(); - client.zrevrange(key, start, end); - final List members = client.getBinaryMultiBulkReply(); - return new LinkedHashSet(members); + public Set zrevrange(final byte[] key, final long start, + final long end) { + checkIsInMulti(); + client.zrevrange(key, start, end); + final List members = client.getBinaryMultiBulkReply(); + return new LinkedHashSet(members); } - public Set zrangeWithScores(final byte[] key, final int start, - final int end) { - checkIsInMulti(); - client.zrangeWithScores(key, start, end); - Set set = getBinaryTupledSet(); - return set; + public Set zrangeWithScores(final byte[] key, final long start, + final long end) { + checkIsInMulti(); + client.zrangeWithScores(key, start, end); + Set set = getBinaryTupledSet(); + return set; } - public Set zrevrangeWithScores(final byte[] key, final int start, - final int end) { - checkIsInMulti(); - client.zrevrangeWithScores(key, start, end); - Set set = getBinaryTupledSet(); - return set; + public Set zrevrangeWithScores(final byte[] key, final long start, + final long end) { + checkIsInMulti(); + client.zrevrangeWithScores(key, start, end); + Set set = getBinaryTupledSet(); + return set; } /** @@ -1605,9 +1643,9 @@ public class BinaryJedis implements BinaryJedisCommands { * @return the cardinality (number of elements) of the set as an integer. */ public Long zcard(final byte[] key) { - checkIsInMulti(); - client.zcard(key); - return client.getIntegerReply(); + checkIsInMulti(); + client.zcard(key); + return client.getIntegerReply(); } /** @@ -1622,53 +1660,53 @@ public class BinaryJedis implements BinaryJedisCommands { * @return the score */ public Double zscore(final byte[] key, final byte[] member) { - checkIsInMulti(); - client.zscore(key, member); - final String score = client.getBulkReply(); - return (score != null ? new Double(score) : null); + checkIsInMulti(); + client.zscore(key, member); + final String score = client.getBulkReply(); + return (score != null ? new Double(score) : null); } public Transaction multi() { - client.multi(); - return new Transaction(client); + client.multi(); + return new Transaction(client); } public List multi(final TransactionBlock jedisTransaction) { - List results = null; - jedisTransaction.setClient(client); - try { - client.multi(); - jedisTransaction.execute(); - results = jedisTransaction.exec(); - } catch (Exception ex) { - jedisTransaction.discard(); - } - return results; + List results = null; + jedisTransaction.setClient(client); + try { + client.multi(); + jedisTransaction.execute(); + results = jedisTransaction.exec(); + } catch (Exception ex) { + jedisTransaction.discard(); + } + return results; } protected void checkIsInMulti() { - if (client.isInMulti()) { - throw new JedisDataException( - "Cannot use Jedis when in Multi. Please use JedisTransaction instead."); - } + if (client.isInMulti()) { + throw new JedisDataException( + "Cannot use Jedis when in Multi. Please use JedisTransaction instead."); + } } public void connect() { - client.connect(); + client.connect(); } public void disconnect() { - client.disconnect(); + client.disconnect(); } public String watch(final byte[]... keys) { - client.watch(keys); - return client.getStatusCodeReply(); + client.watch(keys); + return client.getStatusCodeReply(); } public String unwatch() { - client.unwatch(); - return client.getStatusCodeReply(); + client.unwatch(); + return client.getStatusCodeReply(); } /** @@ -1678,9 +1716,9 @@ public class BinaryJedis implements BinaryJedisCommands { * By default sorting is numeric with elements being compared as double * precision floating point numbers. This is the simplest form of SORT. * - * @see #sort(String, String) - * @see #sort(String, SortingParams) - * @see #sort(String, SortingParams, String) + * @see #sort(byte[], byte[]) + * @see #sort(byte[], SortingParams) + * @see #sort(byte[], SortingParams, byte[]) * * * @param key @@ -1689,9 +1727,9 @@ public class BinaryJedis implements BinaryJedisCommands { * smallest to the biggest number. */ public List sort(final byte[] key) { - checkIsInMulti(); - client.sort(key); - return client.getBinaryMultiBulkReply(); + checkIsInMulti(); + client.sort(key); + return client.getBinaryMultiBulkReply(); } /** @@ -1763,18 +1801,18 @@ public class BinaryJedis implements BinaryJedisCommands { * -> [3, x, 2, y, 1, z] * * - * @see #sort(String) - * @see #sort(String, SortingParams, String) + * @see #sort(byte[]) + * @see #sort(byte[], SortingParams, byte[]) * * @param key * @param sortingParameters * @return a list of sorted elements. */ public List sort(final byte[] key, - final SortingParams sortingParameters) { - checkIsInMulti(); - client.sort(key, sortingParameters); - return client.getBinaryMultiBulkReply(); + final SortingParams sortingParameters) { + checkIsInMulti(); + client.sort(key, sortingParameters); + return client.getBinaryMultiBulkReply(); } /** @@ -1850,27 +1888,27 @@ public class BinaryJedis implements BinaryJedisCommands { * programming language used. */ public List blpop(final int timeout, final byte[]... keys) { - checkIsInMulti(); - final List args = new ArrayList(); - for (final byte[] arg : keys) { - args.add(arg); - } - args.add(Protocol.toByteArray(timeout)); + checkIsInMulti(); + final List args = new ArrayList(); + for (final byte[] arg : keys) { + args.add(arg); + } + args.add(Protocol.toByteArray(timeout)); - client.blpop(args.toArray(new byte[args.size()][])); - client.setTimeoutInfinite(); - final List multiBulkReply = client.getBinaryMultiBulkReply(); - client.rollbackTimeout(); - return multiBulkReply; + client.blpop(args.toArray(new byte[args.size()][])); + client.setTimeoutInfinite(); + final List multiBulkReply = client.getBinaryMultiBulkReply(); + client.rollbackTimeout(); + return multiBulkReply; } /** * Sort a Set or a List accordingly to the specified parameters and store * the result at dstkey. * - * @see #sort(String, SortingParams) - * @see #sort(String) - * @see #sort(String, String) + * @see #sort(byte[], SortingParams) + * @see #sort(byte[]) + * @see #sort(byte[], byte[]) * * @param key * @param sortingParameters @@ -1878,10 +1916,10 @@ public class BinaryJedis implements BinaryJedisCommands { * @return The number of elements of the list at dstkey. */ public Long sort(final byte[] key, final SortingParams sortingParameters, - final byte[] dstkey) { - checkIsInMulti(); - client.sort(key, sortingParameters, dstkey); - return client.getIntegerReply(); + final byte[] dstkey) { + checkIsInMulti(); + client.sort(key, sortingParameters, dstkey); + return client.getIntegerReply(); } /** @@ -1892,18 +1930,18 @@ public class BinaryJedis implements BinaryJedisCommands { * elements being compared as double precision floating point numbers. This * is the simplest form of SORT. * - * @see #sort(String) - * @see #sort(String, SortingParams) - * @see #sort(String, SortingParams, String) + * @see #sort(byte[]) + * @see #sort(byte[], SortingParams) + * @see #sort(byte[], SortingParams, byte[]) * * @param key * @param dstkey * @return The number of elements of the list at dstkey. */ public Long sort(final byte[] key, final byte[] dstkey) { - checkIsInMulti(); - client.sort(key, dstkey); - return client.getIntegerReply(); + checkIsInMulti(); + client.sort(key, dstkey); + return client.getIntegerReply(); } /** @@ -1979,18 +2017,58 @@ public class BinaryJedis implements BinaryJedisCommands { * programming language used. */ public List brpop(final int timeout, final byte[]... keys) { - checkIsInMulti(); - final List args = new ArrayList(); - for (final byte[] arg : keys) { - args.add(arg); - } - args.add(Protocol.toByteArray(timeout)); + checkIsInMulti(); + final List args = new ArrayList(); + for (final byte[] arg : keys) { + args.add(arg); + } + args.add(Protocol.toByteArray(timeout)); - client.brpop(args.toArray(new byte[args.size()][])); + client.brpop(args.toArray(new byte[args.size()][])); + client.setTimeoutInfinite(); + final List multiBulkReply = client.getBinaryMultiBulkReply(); + client.rollbackTimeout(); + + return multiBulkReply; + } + + public List blpop(byte[] arg) { + checkIsInMulti(); + byte[][] args = new byte[1][]; + args[0] = arg; + client.blpop(args); client.setTimeoutInfinite(); final List multiBulkReply = client.getBinaryMultiBulkReply(); client.rollbackTimeout(); + return multiBulkReply; + } + public List brpop(byte[] arg) { + checkIsInMulti(); + byte[][] args = new byte[1][]; + args[0] = arg; + client.brpop(args); + client.setTimeoutInfinite(); + final List multiBulkReply = client.getBinaryMultiBulkReply(); + client.rollbackTimeout(); + return multiBulkReply; + } + + public List blpop(byte[]... args) { + checkIsInMulti(); + client.blpop(args); + client.setTimeoutInfinite(); + final List multiBulkReply = client.getBinaryMultiBulkReply(); + client.rollbackTimeout(); + return multiBulkReply; + } + + public List brpop(byte[]... args) { + checkIsInMulti(); + client.brpop(args); + client.setTimeoutInfinite(); + final List multiBulkReply = client.getBinaryMultiBulkReply(); + client.rollbackTimeout(); return multiBulkReply; } @@ -2010,9 +2088,9 @@ public class BinaryJedis implements BinaryJedisCommands { * @return Status code reply */ public String auth(final String password) { - checkIsInMulti(); - client.auth(password); - return client.getStatusCodeReply(); + checkIsInMulti(); + client.auth(password); + return client.getStatusCodeReply(); } /** @@ -2025,42 +2103,27 @@ public class BinaryJedis implements BinaryJedisCommands { * @return The results of the command in the same order you've run them. */ public List pipelined(final PipelineBlock jedisPipeline) { - jedisPipeline.setClient(client); - jedisPipeline.execute(); - return jedisPipeline.syncAndReturnAll(); + jedisPipeline.setClient(client); + jedisPipeline.execute(); + return jedisPipeline.syncAndReturnAll(); } public Pipeline pipelined() { - Pipeline pipeline = new Pipeline(); - pipeline.setClient(client); - return pipeline; - } - - public void subscribe(final JedisPubSub jedisPubSub, - final String... channels) { - client.setTimeoutInfinite(); - jedisPubSub.proceed(client, channels); - client.rollbackTimeout(); - } - - public Long publish(final String channel, final String message) { - client.publish(channel, message); - return client.getIntegerReply(); - } - - public void psubscribe(final JedisPubSub jedisPubSub, - final String... patterns) { - client.setTimeoutInfinite(); - jedisPubSub.proceedWithPatterns(client, patterns); - client.rollbackTimeout(); + Pipeline pipeline = new Pipeline(); + pipeline.setClient(client); + return pipeline; } public Long zcount(final byte[] key, final double min, final double max) { - checkIsInMulti(); - client.zcount(key, min, max); - return client.getIntegerReply(); + return zcount(key, toByteArray(min), toByteArray(max)); } - + + public Long zcount(final byte[] key, final byte[] min, final byte[] max) { + checkIsInMulti(); + client.zcount(key, min, max); + return client.getIntegerReply(); + } + /** * Return the all the elements in the sorted set at key with a score between * min and max (including elements with score equal to min or max). @@ -2070,13 +2133,13 @@ public class BinaryJedis implements BinaryJedisCommands { * does not involve further computation). *

* Using the optional - * {@link #zrangeByScore(String, double, double, int, int) LIMIT} it's + * {@link #zrangeByScore(byte[], double, double, int, int) LIMIT} it's * possible to get only a range of the matching elements in an SQL-alike * way. Note that if offset is large the commands needs to traverse the list * for offset elements and this adds up to the O(M) figure. *

- * The {@link #zcount(String, double, double) ZCOUNT} command is similar to - * {@link #zrangeByScore(String, double, double) ZRANGEBYSCORE} but instead + * The {@link #zcount(byte[], double, double) ZCOUNT} command is similar to + * {@link #zrangeByScore(byte[], double, double) ZRANGEBYSCORE} but instead * of returning the actual elements in the specified interval, it just * returns the number of matching elements. *

@@ -2105,12 +2168,11 @@ public class BinaryJedis implements BinaryJedisCommands { * (for instance you always ask for the first ten elements with LIMIT) you * can consider it O(log(N)) * - * @see #zrangeByScore(String, double, double) - * @see #zrangeByScore(String, double, double, int, int) - * @see #zrangeByScoreWithScores(String, double, double) - * @see #zrangeByScoreWithScores(String, String, String) - * @see #zrangeByScoreWithScores(String, double, double, int, int) - * @see #zcount(String, double, double) + * @see #zrangeByScore(byte[], double, double) + * @see #zrangeByScore(byte[], double, double, int, int) + * @see #zrangeByScoreWithScores(byte[], double, double) + * @see #zrangeByScoreWithScores(byte[], double, double, int, int) + * @see #zcount(byte[], double, double) * * @param key * @param min @@ -2119,17 +2181,15 @@ public class BinaryJedis implements BinaryJedisCommands { * score range. */ public Set zrangeByScore(final byte[] key, final double min, - final double max) { - checkIsInMulti(); - client.zrangeByScore(key, min, max); - return new LinkedHashSet(client.getBinaryMultiBulkReply()); - } + final double max) { + return zrangeByScore(key, toByteArray(min), toByteArray(max)); + } public Set zrangeByScore(final byte[] key, final byte[] min, - final byte[] max) { - checkIsInMulti(); - client.zrangeByScore(key, min, max); - return new LinkedHashSet(client.getBinaryMultiBulkReply()); + final byte[] max) { + checkIsInMulti(); + client.zrangeByScore(key, min, max); + return new LinkedHashSet(client.getBinaryMultiBulkReply()); } /** @@ -2141,13 +2201,13 @@ public class BinaryJedis implements BinaryJedisCommands { * does not involve further computation). *

* Using the optional - * {@link #zrangeByScore(String, double, double, int, int) LIMIT} it's + * {@link #zrangeByScore(byte[], double, double, int, int) LIMIT} it's * possible to get only a range of the matching elements in an SQL-alike * way. Note that if offset is large the commands needs to traverse the list * for offset elements and this adds up to the O(M) figure. *

- * The {@link #zcount(String, double, double) ZCOUNT} command is similar to - * {@link #zrangeByScore(String, double, double) ZRANGEBYSCORE} but instead + * The {@link #zcount(byte[], double, double) ZCOUNT} command is similar to + * {@link #zrangeByScore(byte[], double, double) ZRANGEBYSCORE} but instead * of returning the actual elements in the specified interval, it just * returns the number of matching elements. *

@@ -2176,11 +2236,11 @@ public class BinaryJedis implements BinaryJedisCommands { * (for instance you always ask for the first ten elements with LIMIT) you * can consider it O(log(N)) * - * @see #zrangeByScore(String, double, double) - * @see #zrangeByScore(String, double, double, int, int) - * @see #zrangeByScoreWithScores(String, double, double) - * @see #zrangeByScoreWithScores(String, double, double, int, int) - * @see #zcount(String, double, double) + * @see #zrangeByScore(byte[], double, double) + * @see #zrangeByScore(byte[], double, double, int, int) + * @see #zrangeByScoreWithScores(byte[], double, double) + * @see #zrangeByScoreWithScores(byte[], double, double, int, int) + * @see #zcount(byte[], double, double) * * @param key * @param min @@ -2189,11 +2249,16 @@ public class BinaryJedis implements BinaryJedisCommands { * score range. */ public Set zrangeByScore(final byte[] key, final double min, - final double max, final int offset, final int count) { - checkIsInMulti(); - client.zrangeByScore(key, min, max, offset, count); - return new LinkedHashSet(client.getBinaryMultiBulkReply()); + final double max, final int offset, final int count) { + return zrangeByScore(key, toByteArray(min),toByteArray(max),offset, count); } + + public Set zrangeByScore(final byte[] key, final byte[] min, + final byte[] max, final int offset, final int count) { + checkIsInMulti(); + client.zrangeByScore(key, min, max, offset, count); + return new LinkedHashSet(client.getBinaryMultiBulkReply()); + } /** * Return the all the elements in the sorted set at key with a score between @@ -2204,13 +2269,13 @@ public class BinaryJedis implements BinaryJedisCommands { * does not involve further computation). *

* Using the optional - * {@link #zrangeByScore(String, double, double, int, int) LIMIT} it's + * {@link #zrangeByScore(byte[], double, double, int, int) LIMIT} it's * possible to get only a range of the matching elements in an SQL-alike * way. Note that if offset is large the commands needs to traverse the list * for offset elements and this adds up to the O(M) figure. *

- * The {@link #zcount(String, double, double) ZCOUNT} command is similar to - * {@link #zrangeByScore(String, double, double) ZRANGEBYSCORE} but instead + * The {@link #zcount(byte[], double, double) ZCOUNT} command is similar to + * {@link #zrangeByScore(byte[], double, double) ZRANGEBYSCORE} but instead * of returning the actual elements in the specified interval, it just * returns the number of matching elements. *

@@ -2239,11 +2304,11 @@ public class BinaryJedis implements BinaryJedisCommands { * (for instance you always ask for the first ten elements with LIMIT) you * can consider it O(log(N)) * - * @see #zrangeByScore(String, double, double) - * @see #zrangeByScore(String, double, double, int, int) - * @see #zrangeByScoreWithScores(String, double, double) - * @see #zrangeByScoreWithScores(String, double, double, int, int) - * @see #zcount(String, double, double) + * @see #zrangeByScore(byte[], double, double) + * @see #zrangeByScore(byte[], double, double, int, int) + * @see #zrangeByScoreWithScores(byte[], double, double) + * @see #zrangeByScoreWithScores(byte[], double, double, int, int) + * @see #zcount(byte[], double, double) * * @param key * @param min @@ -2252,12 +2317,17 @@ public class BinaryJedis implements BinaryJedisCommands { * score range. */ public Set zrangeByScoreWithScores(final byte[] key, - final double min, final double max) { - checkIsInMulti(); - client.zrangeByScoreWithScores(key, min, max); - Set set = getBinaryTupledSet(); - return set; + final double min, final double max) { + return zrangeByScoreWithScores(key, toByteArray(min), toByteArray(max)); } + + public Set zrangeByScoreWithScores(final byte[] key, + final byte[] min, final byte[] max) { + checkIsInMulti(); + client.zrangeByScoreWithScores(key, min, max); + Set set = getBinaryTupledSet(); + return set; + } /** * Return the all the elements in the sorted set at key with a score between @@ -2268,13 +2338,13 @@ public class BinaryJedis implements BinaryJedisCommands { * does not involve further computation). *

* Using the optional - * {@link #zrangeByScore(String, double, double, int, int) LIMIT} it's + * {@link #zrangeByScore(byte[], double, double, int, int) LIMIT} it's * possible to get only a range of the matching elements in an SQL-alike * way. Note that if offset is large the commands needs to traverse the list * for offset elements and this adds up to the O(M) figure. *

- * The {@link #zcount(String, double, double) ZCOUNT} command is similar to - * {@link #zrangeByScore(String, double, double) ZRANGEBYSCORE} but instead + * The {@link #zcount(byte[], double, double) ZCOUNT} command is similar to + * {@link #zrangeByScore(byte[], double, double) ZRANGEBYSCORE} but instead * of returning the actual elements in the specified interval, it just * returns the number of matching elements. *

@@ -2303,11 +2373,11 @@ public class BinaryJedis implements BinaryJedisCommands { * (for instance you always ask for the first ten elements with LIMIT) you * can consider it O(log(N)) * - * @see #zrangeByScore(String, double, double) - * @see #zrangeByScore(String, double, double, int, int) - * @see #zrangeByScoreWithScores(String, double, double) - * @see #zrangeByScoreWithScores(String, double, double, int, int) - * @see #zcount(String, double, double) + * @see #zrangeByScore(byte[], double, double) + * @see #zrangeByScore(byte[], double, double, int, int) + * @see #zrangeByScoreWithScores(byte[], double, double) + * @see #zrangeByScoreWithScores(byte[], double, double, int, int) + * @see #zcount(byte[], double, double) * * @param key * @param min @@ -2316,63 +2386,83 @@ public class BinaryJedis implements BinaryJedisCommands { * score range. */ public Set zrangeByScoreWithScores(final byte[] key, - final double min, final double max, final int offset, - final int count) { - checkIsInMulti(); - client.zrangeByScoreWithScores(key, min, max, offset, count); - Set set = getBinaryTupledSet(); - return set; + final double min, final double max, final int offset, + final int count) { + return zrangeByScoreWithScores(key, toByteArray(min), toByteArray(max), offset, count); } + + public Set zrangeByScoreWithScores(final byte[] key, + final byte[] min, final byte[] max, final int offset, + final int count) { + checkIsInMulti(); + client.zrangeByScoreWithScores(key, min, max, offset, count); + Set set = getBinaryTupledSet(); + return set; + } private Set getBinaryTupledSet() { - checkIsInMulti(); - List membersWithScores = client.getBinaryMultiBulkReply(); - Set set = new LinkedHashSet(); - Iterator iterator = membersWithScores.iterator(); - while (iterator.hasNext()) { - set.add(new Tuple(iterator.next(), Double.valueOf(SafeEncoder - .encode(iterator.next())))); - } - return set; + checkIsInMulti(); + List membersWithScores = client.getBinaryMultiBulkReply(); + Set set = new LinkedHashSet(); + Iterator iterator = membersWithScores.iterator(); + while (iterator.hasNext()) { + set.add(new Tuple(iterator.next(), Double.valueOf(SafeEncoder + .encode(iterator.next())))); + } + return set; } public Set zrevrangeByScore(final byte[] key, final double max, - final double min) { - checkIsInMulti(); - client.zrevrangeByScore(key, max, min); - return new LinkedHashSet(client.getBinaryMultiBulkReply()); + final double min) { + return zrevrangeByScore(key, toByteArray(max), toByteArray(min)); } public Set zrevrangeByScore(final byte[] key, final byte[] max, - final byte[] min) { - checkIsInMulti(); - client.zrevrangeByScore(key, max, min); - return new LinkedHashSet(client.getBinaryMultiBulkReply()); + final byte[] min) { + checkIsInMulti(); + client.zrevrangeByScore(key, max, min); + return new LinkedHashSet(client.getBinaryMultiBulkReply()); } public Set zrevrangeByScore(final byte[] key, final double max, - final double min, final int offset, final int count) { - checkIsInMulti(); - client.zrevrangeByScore(key, max, min, offset, count); - return new LinkedHashSet(client.getBinaryMultiBulkReply()); + final double min, final int offset, final int count) { + return zrevrangeByScore(key, toByteArray(max), toByteArray(min), offset, count); + } + + public Set zrevrangeByScore(final byte[] key, final byte[] max, + final byte[] min, final int offset, final int count) { + checkIsInMulti(); + client.zrevrangeByScore(key, max, min, offset, count); + return new LinkedHashSet(client.getBinaryMultiBulkReply()); + } + + public Set zrevrangeByScoreWithScores(final byte[] key, + final double max, final double min) { + return zrevrangeByScoreWithScores(key, toByteArray(max), toByteArray(min)); } public Set zrevrangeByScoreWithScores(final byte[] key, - final double max, final double min) { - checkIsInMulti(); - client.zrevrangeByScoreWithScores(key, max, min); - Set set = getBinaryTupledSet(); - return set; + final double max, final double min, final int offset, + final int count) { + return zrevrangeByScoreWithScores(key, toByteArray(max), toByteArray(min), offset, count); + } + + public Set zrevrangeByScoreWithScores(final byte[] key, + final byte[] max, final byte[] min) { + checkIsInMulti(); + client.zrevrangeByScoreWithScores(key, max, min); + Set set = getBinaryTupledSet(); + return set; } public Set zrevrangeByScoreWithScores(final byte[] key, - final double max, final double min, final int offset, - final int count) { - checkIsInMulti(); - client.zrevrangeByScoreWithScores(key, max, min, offset, count); - Set set = getBinaryTupledSet(); - return set; - } + final byte[] max, final byte[] min, final int offset, + final int count) { + checkIsInMulti(); + client.zrevrangeByScoreWithScores(key, max, min, offset, count); + Set set = getBinaryTupledSet(); + return set; + } /** * Remove all elements in the sorted set at key with rank between start and @@ -2387,10 +2477,10 @@ public class BinaryJedis implements BinaryJedisCommands { * operation * */ - public Long zremrangeByRank(final byte[] key, final int start, final int end) { - checkIsInMulti(); - client.zremrangeByRank(key, start, end); - return client.getIntegerReply(); + public Long zremrangeByRank(final byte[] key, final long start, final long end) { + checkIsInMulti(); + client.zremrangeByRank(key, start, end); + return client.getIntegerReply(); } /** @@ -2408,11 +2498,16 @@ public class BinaryJedis implements BinaryJedisCommands { * @return Integer reply, specifically the number of elements removed. */ public Long zremrangeByScore(final byte[] key, final double start, - final double end) { - checkIsInMulti(); - client.zremrangeByScore(key, start, end); - return client.getIntegerReply(); + final double end) { + return zremrangeByScore(key, toByteArray(start), toByteArray(end)); } + + public Long zremrangeByScore(final byte[] key, final byte[] start, + final byte[] end) { + checkIsInMulti(); + client.zremrangeByScore(key, start, end); + return client.getIntegerReply(); + } /** * Creates a union or intersection of N sorted sets given by keys k1 through @@ -2453,9 +2548,9 @@ public class BinaryJedis implements BinaryJedisCommands { * set at dstkey */ public Long zunionstore(final byte[] dstkey, final byte[]... sets) { - checkIsInMulti(); - client.zunionstore(dstkey, sets); - return client.getIntegerReply(); + checkIsInMulti(); + client.zunionstore(dstkey, sets); + return client.getIntegerReply(); } /** @@ -2498,10 +2593,10 @@ public class BinaryJedis implements BinaryJedisCommands { * set at dstkey */ public Long zunionstore(final byte[] dstkey, final ZParams params, - final byte[]... sets) { - checkIsInMulti(); - client.zunionstore(dstkey, params, sets); - return client.getIntegerReply(); + final byte[]... sets) { + checkIsInMulti(); + client.zunionstore(dstkey, params, sets); + return client.getIntegerReply(); } /** @@ -2543,9 +2638,9 @@ public class BinaryJedis implements BinaryJedisCommands { * set at dstkey */ public Long zinterstore(final byte[] dstkey, final byte[]... sets) { - checkIsInMulti(); - client.zinterstore(dstkey, sets); - return client.getIntegerReply(); + checkIsInMulti(); + client.zinterstore(dstkey, sets); + return client.getIntegerReply(); } /** @@ -2588,10 +2683,10 @@ public class BinaryJedis implements BinaryJedisCommands { * set at dstkey */ public Long zinterstore(final byte[] dstkey, final ZParams params, - final byte[]... sets) { - checkIsInMulti(); - client.zinterstore(dstkey, params, sets); - return client.getIntegerReply(); + final byte[]... sets) { + checkIsInMulti(); + client.zinterstore(dstkey, params, sets); + return client.getIntegerReply(); } /** @@ -2611,8 +2706,8 @@ public class BinaryJedis implements BinaryJedisCommands { * @return Status code reply */ public String save() { - client.save(); - return client.getStatusCodeReply(); + client.save(); + return client.getStatusCodeReply(); } /** @@ -2626,8 +2721,8 @@ public class BinaryJedis implements BinaryJedisCommands { * @return Status code reply */ public String bgsave() { - client.bgsave(); - return client.getStatusCodeReply(); + client.bgsave(); + return client.getStatusCodeReply(); } /** @@ -2649,8 +2744,8 @@ public class BinaryJedis implements BinaryJedisCommands { * @return Status code reply */ public String bgrewriteaof() { - client.bgrewriteaof(); - return client.getStatusCodeReply(); + client.bgrewriteaof(); + return client.getStatusCodeReply(); } /** @@ -2665,8 +2760,8 @@ public class BinaryJedis implements BinaryJedisCommands { * @return Integer reply, specifically an UNIX time stamp. */ public Long lastsave() { - client.lastsave(); - return client.getIntegerReply(); + client.lastsave(); + return client.getIntegerReply(); } /** @@ -2682,14 +2777,14 @@ public class BinaryJedis implements BinaryJedisCommands { * the server quits and the connection is closed. */ public String shutdown() { - client.shutdown(); - String status = null; - try { - status = client.getStatusCodeReply(); - } catch (JedisException ex) { - status = null; - } - return status; + client.shutdown(); + String status = null; + try { + status = client.getStatusCodeReply(); + } catch (JedisException ex) { + status = null; + } + return status; } /** @@ -2733,8 +2828,13 @@ public class BinaryJedis implements BinaryJedisCommands { * @return Bulk reply */ public String info() { - client.info(); - return client.getBulkReply(); + client.info(); + return client.getBulkReply(); + } + + public String info(final String section) { + client.info(section); + return client.getBulkReply(); } /** @@ -2748,8 +2848,8 @@ public class BinaryJedis implements BinaryJedisCommands { * @param jedisMonitor */ public void monitor(final JedisMonitor jedisMonitor) { - client.monitor(); - jedisMonitor.proceed(client); + client.monitor(); + jedisMonitor.proceed(client); } /** @@ -2778,13 +2878,13 @@ public class BinaryJedis implements BinaryJedisCommands { * @return Status code reply */ public String slaveof(final String host, final int port) { - client.slaveof(host, port); - return client.getStatusCodeReply(); + client.slaveof(host, port); + return client.getStatusCodeReply(); } public String slaveofNoOne() { - client.slaveofNoOne(); - return client.getStatusCodeReply(); + client.slaveofNoOne(); + return client.getStatusCodeReply(); } /** @@ -2823,9 +2923,9 @@ public class BinaryJedis implements BinaryJedisCommands { * @param pattern * @return Bulk reply. */ - public List configGet(final String pattern) { - client.configGet(pattern); - return client.getMultiBulkReply(); + public List configGet(final byte[] pattern) { + client.configGet(pattern); + return client.getBinaryMultiBulkReply(); } /** @@ -2834,8 +2934,8 @@ public class BinaryJedis implements BinaryJedisCommands { * @return */ public String configResetStat() { - client.configResetStat(); - return client.getStatusCodeReply(); + client.configResetStat(); + return client.getStatusCodeReply(); } /** @@ -2872,31 +2972,31 @@ public class BinaryJedis implements BinaryJedisCommands { * @param value * @return Status code reply */ - public String configSet(final String parameter, final String value) { - client.configSet(parameter, value); - return client.getStatusCodeReply(); + public byte[] configSet(final byte[] parameter, final byte[] value) { + client.configSet(parameter, value); + return client.getBinaryBulkReply(); } public boolean isConnected() { - return client.isConnected(); + return client.isConnected(); } public Long strlen(final byte[] key) { - client.strlen(key); - return client.getIntegerReply(); + client.strlen(key); + return client.getIntegerReply(); } public void sync() { - client.sync(); + client.sync(); } - public Long lpushx(final byte[] key, final byte[] string) { - client.lpushx(key, string); - return client.getIntegerReply(); + public Long lpushx(final byte[] key, final byte[]... string) { + client.lpushx(key, string); + return client.getIntegerReply(); } /** - * Undo a {@link #expire(String, int) expire} at turning the expire key into + * Undo a {@link #expire(byte[], int) expire} at turning the expire key into * a normal key. *

* Time complexity: O(1) @@ -2906,33 +3006,33 @@ public class BinaryJedis implements BinaryJedisCommands { * key is not persist (only happens when key not set). */ public Long persist(final byte[] key) { - client.persist(key); - return client.getIntegerReply(); + client.persist(key); + return client.getIntegerReply(); } - public Long rpushx(final byte[] key, final byte[] string) { - client.rpushx(key, string); - return client.getIntegerReply(); + public Long rpushx(final byte[] key, final byte[]... string) { + client.rpushx(key, string); + return client.getIntegerReply(); } public byte[] echo(final byte[] string) { - client.echo(string); - return client.getBinaryBulkReply(); + client.echo(string); + return client.getBinaryBulkReply(); } public Long linsert(final byte[] key, final LIST_POSITION where, - final byte[] pivot, final byte[] value) { - client.linsert(key, where, pivot, value); - return client.getIntegerReply(); + final byte[] pivot, final byte[] value) { + client.linsert(key, where, pivot, value); + return client.getIntegerReply(); } public String debug(final DebugParams params) { - client.debug(params); - return client.getStatusCodeReply(); + client.debug(params); + return client.getStatusCodeReply(); } public Client getClient() { - return client; + return client; } /** @@ -2945,11 +3045,11 @@ public class BinaryJedis implements BinaryJedisCommands { * @return the element */ public byte[] brpoplpush(byte[] source, byte[] destination, int timeout) { - client.brpoplpush(source, destination, timeout); - client.setTimeoutInfinite(); - byte[] reply = client.getBinaryBulkReply(); - client.rollbackTimeout(); - return reply; + client.brpoplpush(source, destination, timeout); + client.setTimeoutInfinite(); + byte[] reply = client.getBinaryBulkReply(); + client.rollbackTimeout(); + return reply; } /** @@ -2960,9 +3060,14 @@ public class BinaryJedis implements BinaryJedisCommands { * @param value * @return */ - public Long setbit(byte[] key, long offset, byte[] value) { - client.setbit(key, offset, value); - return client.getIntegerReply(); + public Boolean setbit(byte[] key, long offset, boolean value) { + client.setbit(key, offset, value); + return client.getIntegerReply() == 1; + } + + public Boolean setbit(byte[] key, long offset, byte[] value) { + client.setbit(key, offset, value); + return client.getIntegerReply() == 1; } /** @@ -2972,39 +3077,270 @@ public class BinaryJedis implements BinaryJedisCommands { * @param offset * @return */ - public Long getbit(byte[] key, long offset) { - client.getbit(key, offset); - return client.getIntegerReply(); + public Boolean getbit(byte[] key, long offset) { + client.getbit(key, offset); + return client.getIntegerReply() == 1; } - public long setrange(byte[] key, long offset, byte[] value) { - client.setrange(key, offset, value); - return client.getIntegerReply(); + public Long setrange(byte[] key, long offset, byte[] value) { + client.setrange(key, offset, value); + return client.getIntegerReply(); } - public String getrange(byte[] key, long startOffset, long endOffset) { - client.getrange(key, startOffset, endOffset); - return client.getBulkReply(); + public byte[] getrange(byte[] key, long startOffset, long endOffset) { + client.getrange(key, startOffset, endOffset); + return client.getBinaryBulkReply(); } public Long publish(byte[] channel, byte[] message) { - client.publish(channel, message); - return client.getIntegerReply(); + client.publish(channel, message); + return client.getIntegerReply(); } public void subscribe(BinaryJedisPubSub jedisPubSub, byte[]... channels) { - client.setTimeoutInfinite(); - jedisPubSub.proceed(client, channels); - client.rollbackTimeout(); + client.setTimeoutInfinite(); + jedisPubSub.proceed(client, channels); + client.rollbackTimeout(); } public void psubscribe(BinaryJedisPubSub jedisPubSub, byte[]... patterns) { - client.setTimeoutInfinite(); - jedisPubSub.proceedWithPatterns(client, patterns); - client.rollbackTimeout(); + client.setTimeoutInfinite(); + jedisPubSub.proceedWithPatterns(client, patterns); + client.rollbackTimeout(); } public Long getDB() { - return client.getDB(); + return client.getDB(); } -} \ No newline at end of file + + /** + * Evaluates scripts using the Lua interpreter built into Redis starting + * from version 2.6.0. + *

+ * + * @return Script result + */ + public Object eval(byte[] script, List keys, List args) { + client.setTimeoutInfinite(); + client.eval(script, toByteArray(keys.size()), getParams(keys, args)); + return client.getOne(); + } + + private byte[][] getParams(List keys, List args) { + int keyCount = keys.size(); + byte[][] params = new byte[keyCount + args.size()][]; + + for (int i = 0; i < keyCount; i++) + params[i] = keys.get(i); + + for (int i = 0; i < keys.size(); i++) + params[keyCount + i] = args.get(i); + + return params; + } + + public Object eval(byte[] script, byte[] keyCount, byte[]... params) { + client.setTimeoutInfinite(); + client.eval(script, keyCount, params); + return client.getOne(); + } + + public Object eval(byte[] script, int keyCount, byte[]... params) { + client.setTimeoutInfinite(); + client.eval(script, SafeEncoder.encode(Integer.toString(keyCount)), params); + return client.getOne(); + } + + public Object eval(byte[] script) { + client.setTimeoutInfinite(); + client.eval(script, 0); + return client.getOne(); + } + + public Object evalsha(byte[] sha1) { + client.setTimeoutInfinite(); + client.evalsha(sha1, 0); + return client.getOne(); + } + + public Object evalsha(byte[] sha1, List keys, List args) { + client.setTimeoutInfinite(); + client.evalsha(sha1, keys.size(), keys.toArray(new byte[0][])); + return client.getOne(); + } + + public Object evalsha(byte[] sha1, int keyCount, byte[]... params) { + client.setTimeoutInfinite(); + client.evalsha(sha1, keyCount, params); + return client.getOne(); + } + + public String scriptFlush() { + client.scriptFlush(); + return client.getStatusCodeReply(); + } + + public List scriptExists(byte[]... sha1) { + client.scriptExists(sha1); + return client.getIntegerMultiBulkReply(); + } + + public byte[] scriptLoad(byte[] script) { + client.scriptLoad(script); + return client.getBinaryBulkReply(); + } + + public String scriptKill() { + client.scriptKill(); + return client.getStatusCodeReply(); + } + + public String slowlogReset() { + client.slowlogReset(); + return client.getBulkReply(); + } + + public Long slowlogLen() { + client.slowlogLen(); + return client.getIntegerReply(); + } + + public List slowlogGetBinary() { + client.slowlogGet(); + return client.getBinaryMultiBulkReply(); + } + + public List slowlogGetBinary(long entries) { + client.slowlogGet(entries); + return client.getBinaryMultiBulkReply(); + } + + public Long objectRefcount(byte[] key) { + client.objectRefcount(key); + return client.getIntegerReply(); + } + + public byte[] objectEncoding(byte[] key) { + client.objectEncoding(key); + return client.getBinaryBulkReply(); + } + + public Long objectIdletime(byte[] key) { + client.objectIdletime(key); + return client.getIntegerReply(); + } + + public Long bitcount(final byte[] key) { + client.bitcount(key); + return client.getIntegerReply(); + } + + public Long bitcount(final byte[] key, long start, long end) { + client.bitcount(key, start, end); + return client.getIntegerReply(); + } + + public Long bitop(BitOP op, final byte[] destKey, byte[]... srcKeys) { + client.bitop(op, destKey, srcKeys); + return client.getIntegerReply(); + } + + public byte[] dump(final byte[] key) { + checkIsInMulti(); + client.dump(key); + return client.getBinaryBulkReply(); + } + + public String restore(final byte[] key, final int ttl, final byte[] serializedValue) { + checkIsInMulti(); + client.restore(key, ttl, serializedValue); + return client.getStatusCodeReply(); + } + + public Long pexpire(final byte[] key, final int milliseconds) { + checkIsInMulti(); + client.pexpire(key, milliseconds); + return client.getIntegerReply(); + } + + public Long pexpireAt(final byte[] key, final long millisecondsTimestamp) { + checkIsInMulti(); + client.pexpireAt(key, millisecondsTimestamp); + return client.getIntegerReply(); + } + + public Long pttl(final byte[] key) { + checkIsInMulti(); + client.pttl(key); + return client.getIntegerReply(); + } + + public Double incrByFloat(final byte[] key, final double increment) { + checkIsInMulti(); + client.incrByFloat(key, increment); + String relpy = client.getBulkReply(); + return (relpy != null ? new Double(relpy) : null); + } + + public String psetex(final byte[] key, final int milliseconds, final byte[] value) { + checkIsInMulti(); + client.psetex(key, milliseconds, value); + return client.getStatusCodeReply(); + } + + public String set(final byte[] key, final byte[] value, final byte[] nxxx) { + checkIsInMulti(); + client.set(key, value, nxxx); + return client.getStatusCodeReply(); + } + + public String set(final byte[] key, final byte[] value, final byte[] nxxx, final byte[] expx, final int time) { + checkIsInMulti(); + client.set(key, value, nxxx, expx, time); + return client.getStatusCodeReply(); + } + + public String clientKill(final byte[] client) { + checkIsInMulti(); + this.client.clientKill(client); + return this.client.getStatusCodeReply(); + } + + public String clientGetname() { + checkIsInMulti(); + client.clientGetname(); + return client.getBulkReply(); + } + + public String clientList() { + checkIsInMulti(); + client.clientList(); + return client.getBulkReply(); + } + + public String clientSetname(final byte[] name) { + checkIsInMulti(); + client.clientSetname(name); + return client.getBulkReply(); + } + + public List time() { + checkIsInMulti(); + client.time(); + return client.getMultiBulkReply(); + } + + public String migrate(final byte[] host, final int port, final byte[] key, final int destinationDb, final int timeout) { + checkIsInMulti(); + client.migrate(host, port, key, destinationDb, timeout); + return client.getStatusCodeReply(); + } + + public Double hincrByFloat(final byte[] key, final byte[] field, double increment) { + checkIsInMulti(); + client.hincrByFloat(key, field, increment); + String relpy = client.getBulkReply(); + return (relpy != null ? new Double(relpy) : null); + } + +} diff --git a/src/main/java/redis/clients/jedis/BinaryJedisCommands.java b/src/main/java/redis/clients/jedis/BinaryJedisCommands.java index ef8aebd..35e1879 100644 --- a/src/main/java/redis/clients/jedis/BinaryJedisCommands.java +++ b/src/main/java/redis/clients/jedis/BinaryJedisCommands.java @@ -17,6 +17,8 @@ public interface BinaryJedisCommands { Boolean exists(byte[] key); + Long persist(byte[] key); + String type(byte[] key); Long expire(byte[] key, int seconds); @@ -25,6 +27,16 @@ public interface BinaryJedisCommands { Long ttl(byte[] key); + Boolean setbit(byte[] key, long offset, boolean value); + + Boolean setbit(byte[] key, long offset, byte[] value); + + Boolean getbit(byte[] key, long offset); + + Long setrange(byte[] key, long offset, byte[] value); + + byte[] getrange(byte[] key, long startOffset, long endOffset); + byte[] getSet(byte[] key, byte[] value); Long setnx(byte[] key, byte[] value); @@ -57,7 +69,7 @@ public interface BinaryJedisCommands { Boolean hexists(byte[] key, byte[] field); - Long hdel(byte[] key, byte[] field); + Long hdel(byte[] key, byte[]... field); Long hlen(byte[] key); @@ -67,31 +79,31 @@ public interface BinaryJedisCommands { Map hgetAll(byte[] key); - Long rpush(byte[] key, byte[] string); + Long rpush(byte[] key, byte[]... args); - Long lpush(byte[] key, byte[] string); + Long lpush(byte[] key, byte[]... args); Long llen(byte[] key); - List lrange(byte[] key, int start, int end); + List lrange(byte[] key, long start, long end); - String ltrim(byte[] key, int start, int end); + String ltrim(byte[] key, long start, long end); - byte[] lindex(byte[] key, int index); + byte[] lindex(byte[] key, long index); - String lset(byte[] key, int index, byte[] value); + String lset(byte[] key, long index, byte[] value); - Long lrem(byte[] key, int count, byte[] value); + Long lrem(byte[] key, long count, byte[] value); byte[] lpop(byte[] key); byte[] rpop(byte[] key); - Long sadd(byte[] key, byte[] member); + Long sadd(byte[] key, byte[]... member); Set smembers(byte[] key); - Long srem(byte[] key, byte[] member); + Long srem(byte[] key, byte[]... member); byte[] spop(byte[] key); @@ -101,11 +113,15 @@ public interface BinaryJedisCommands { byte[] srandmember(byte[] key); + Long strlen(byte[] key); + Long zadd(byte[] key, double score, byte[] member); + + Long zadd(byte[] key, Map scoreMembers); - Set zrange(byte[] key, int start, int end); + Set zrange(byte[] key, long start, long end); - Long zrem(byte[] key, byte[] member); + Long zrem(byte[] key, byte[]... member); Double zincrby(byte[] key, double score, byte[] member); @@ -113,11 +129,11 @@ public interface BinaryJedisCommands { Long zrevrank(byte[] key, byte[] member); - Set zrevrange(byte[] key, int start, int end); + Set zrevrange(byte[] key, long start, long end); - Set zrangeWithScores(byte[] key, int start, int end); + Set zrangeWithScores(byte[] key, long start, long end); - Set zrevrangeWithScores(byte[] key, int start, int end); + Set zrevrangeWithScores(byte[] key, long start, long end); Long zcard(byte[] key); @@ -129,29 +145,72 @@ public interface BinaryJedisCommands { Long zcount(byte[] key, double min, double max); + Long zcount(byte[] key, byte[] min, byte[] max); + Set zrangeByScore(byte[] key, double min, double max); - Set zrangeByScore(byte[] key, double min, double max, int offset, - int count); - - Set zrangeByScoreWithScores(byte[] key, double min, double max); - - Set zrangeByScoreWithScores(byte[] key, double min, double max, - int offset, int count); + Set zrangeByScore(byte[] key, byte[] min, byte[] max); Set zrevrangeByScore(byte[] key, double max, double min); - Set zrevrangeByScore(byte[] key, double max, double min, int offset, + Set zrangeByScore(byte[] key, double min, double max, int offset, + int count); + + Set zrevrangeByScore(byte[] key, byte[] max, byte[] min); + + Set zrangeByScore(byte[] key, byte[] min, byte[] max, int offset, int count); + Set zrevrangeByScore(byte[] key, double max, double min, + int offset, int count); + + Set zrangeByScoreWithScores(byte[] key, double min, double max); + Set zrevrangeByScoreWithScores(byte[] key, double max, double min); - Set zrevrangeByScoreWithScores(byte[] key, double max, double min, + Set zrangeByScoreWithScores(byte[] key, double min, double max, + int offset, int count); + + Set zrevrangeByScore(byte[] key, byte[] max, byte[] min, int offset, int count); - Long zremrangeByRank(byte[] key, int start, int end); + Set zrangeByScoreWithScores(byte[] key, byte[] min, byte[] max); + + Set zrevrangeByScoreWithScores(byte[] key, byte[] max, byte[] min); + + Set zrangeByScoreWithScores(byte[] key, byte[] min, byte[] max, + int offset, int count); + + Set zrevrangeByScoreWithScores(byte[] key, double max, double min, + int offset, int count); + + Set zrevrangeByScoreWithScores(byte[] key, byte[] max, byte[] min, + int offset, int count); + + Long zremrangeByRank(byte[] key, long start, long end); Long zremrangeByScore(byte[] key, double start, double end); + + Long zremrangeByScore(byte[] key, byte[] start, byte[] end); - Long linsert(byte[] key, LIST_POSITION where, byte[] pivot, byte[] value); + Long linsert(byte[] key, Client.LIST_POSITION where, byte[] pivot, + byte[] value); + + Long lpushx(byte[] key, byte[]... arg); + + Long rpushx(byte[] key, byte[]... arg); + + List blpop(byte[] arg); + + List brpop(byte[] arg); + + Long del(byte[] key); + + byte[] echo(byte[] arg); + + Long move(byte[] key, int dbIndex); + + Long bitcount(final byte[] key); + + Long bitcount(final byte[] key, long start, long end); } diff --git a/src/main/java/redis/clients/jedis/BinaryRedisPipeline.java b/src/main/java/redis/clients/jedis/BinaryRedisPipeline.java new file mode 100644 index 0000000..d48800c --- /dev/null +++ b/src/main/java/redis/clients/jedis/BinaryRedisPipeline.java @@ -0,0 +1,219 @@ +package redis.clients.jedis; + +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * @author guy + */ +public interface BinaryRedisPipeline { + Response append(byte[] key, byte[] value); + + Response> blpop(byte[] arg); + + Response> brpop(byte[] arg); + + Response decr(byte[] key); + + Response decrBy(byte[] key, long integer); + + Response del(byte[] keys); + + Response echo(byte[] string); + + Response exists(byte[] key); + + Response expire(byte[] key, int seconds); + + Response expireAt(byte[] key, long unixTime); + + Response get(byte[] key); + + Response getbit(byte[] key, long offset); + + Response getSet(byte[] key, byte[] value); + + Response getrange(byte[] key, long startOffset, long endOffset); + + Response hdel(byte[] key, byte[]... field); + + Response hexists(byte[] key, byte[] field); + + Response hget(byte[] key, byte[] field); + + Response> hgetAll(byte[] key); + + Response hincrBy(byte[] key, byte[] field, long value); + + Response> hkeys(byte[] key); + + Response hlen(byte[] key); + + Response> hmget(byte[] key, byte[]... fields); + + Response hmset(byte[] key, Map hash); + + Response hset(byte[] key, byte[] field, byte[] value); + + Response hsetnx(byte[] key, byte[] field, byte[] value); + + Response> hvals(byte[] key); + + Response incr(byte[] key); + + Response incrBy(byte[] key, long integer); + + Response lindex(byte[] key, long index); + + Response linsert(byte[] key, BinaryClient.LIST_POSITION where, + byte[] pivot, byte[] value); + + Response llen(byte[] key); + + Response lpop(byte[] key); + + Response lpush(byte[] key, byte[]... string); + + Response lpushx(byte[] key, byte[]... bytes); + + Response> lrange(byte[] key, long start, long end); + + Response lrem(byte[] key, long count, byte[] value); + + Response lset(byte[] key, long index, byte[] value); + + Response ltrim(byte[] key, long start, long end); + + Response move(byte[] key, int dbIndex); + + Response persist(byte[] key); + + Response rpop(byte[] key); + + Response rpush(byte[] key, byte[]... string); + + Response rpushx(byte[] key, byte[]... string); + + Response sadd(byte[] key, byte[]... member); + + Response scard(byte[] key); + + Response set(byte[] key, byte[] value); + + Response setbit(byte[] key, long offset, byte[] value); + + Response setrange(byte[] key, long offset, byte[] value); + + Response setex(byte[] key, int seconds, byte[] value); + + Response setnx(byte[] key, byte[] value); + + Response setrange(String key, long offset, String value); + + Response> smembers(byte[] key); + + Response sismember(byte[] key, byte[] member); + + Response> sort(byte[] key); + + Response> sort(byte[] key, + SortingParams sortingParameters); + + Response spop(byte[] key); + + Response srandmember(byte[] key); + + Response srem(byte[] key, byte[]... member); + + Response strlen(byte[] key); + + Response substr(byte[] key, int start, int end); + + Response ttl(byte[] key); + + Response type(byte[] key); + + Response zadd(byte[] key, double score, byte[] member); + + Response zcard(byte[] key); + + Response zcount(byte[] key, double min, double max); + + Response zincrby(byte[] key, double score, byte[] member); + + Response> zrange(byte[] key, long start, long end); + + Response> zrangeByScore(byte[] key, double min, + double max); + + Response> zrangeByScore(byte[] key, byte[] min, + byte[] max); + + Response> zrangeByScore(byte[] key, double min, + double max, int offset, int count); + + Response> zrangeByScore(byte[] key, byte[] min, + byte[] max, int offset, int count); + + Response> zrangeByScoreWithScores(byte[] key, double min, + double max); + + Response> zrangeByScoreWithScores(byte[] key, byte[] min, + byte[] max); + + Response> zrangeByScoreWithScores(byte[] key, double min, + double max, int offset, int count); + + Response> zrangeByScoreWithScores(byte[] key, byte[] min, + byte[] max, int offset, int count); + + Response> zrevrangeByScore(byte[] key, double max, + double min); + + Response> zrevrangeByScore(byte[] key, byte[] max, + byte[] min); + + Response> zrevrangeByScore(byte[] key, double max, + double min, int offset, int count); + + Response> zrevrangeByScore(byte[] key, byte[] max, + byte[] min, int offset, int count); + + Response> zrevrangeByScoreWithScores(byte[] key, + double max, double min); + + Response> zrevrangeByScoreWithScores(byte[] key, + byte[] max, byte[] min); + + Response> zrevrangeByScoreWithScores(byte[] key, + double max, double min, int offset, int count); + + Response> zrevrangeByScoreWithScores(byte[] key, + byte[] max, byte[] min, int offset, int count); + + Response> zrangeWithScores(byte[] key, long start, long end); + + Response zrank(byte[] key, byte[] member); + + Response zrem(byte[] key, byte[]... member); + + Response zremrangeByRank(byte[] key, long start, long end); + + Response zremrangeByScore(byte[] key, double start, double end); + + Response zremrangeByScore(byte[] key, byte[] start, byte[] end); + + Response> zrevrange(byte[] key, long start, long end); + + Response> zrevrangeWithScores(byte[] key, long start, + long end); + + Response zrevrank(byte[] key, byte[] member); + + Response zscore(byte[] key, byte[] member); + + Response bitcount(byte[] key); + + Response bitcount(byte[] key, long start, long end); +} diff --git a/src/main/java/redis/clients/jedis/BinaryScriptingCommands.java b/src/main/java/redis/clients/jedis/BinaryScriptingCommands.java new file mode 100644 index 0000000..092ce7a --- /dev/null +++ b/src/main/java/redis/clients/jedis/BinaryScriptingCommands.java @@ -0,0 +1,30 @@ +package redis.clients.jedis; + + +import java.util.List; + +public interface BinaryScriptingCommands { + + Object eval(byte[] script, byte[] keyCount, byte[]... params); + + Object eval(byte[] script, int keyCount, byte[]... params); + + Object eval(byte[] script, List keys, List args); + + Object eval(byte[] script); + + Object evalsha(byte[] script); + + Object evalsha(byte[] sha1, List keys, List args); + + Object evalsha(byte[] sha1, int keyCount, byte[]... params); + + // TODO: should be Boolean, add singular version + List scriptExists(byte[]... sha1); + + byte[] scriptLoad(byte[] script); + + String scriptFlush(); + + String scriptKill(); +} diff --git a/src/main/java/redis/clients/jedis/BinaryShardedJedis.java b/src/main/java/redis/clients/jedis/BinaryShardedJedis.java index 113511b..2723aa1 100644 --- a/src/main/java/redis/clients/jedis/BinaryShardedJedis.java +++ b/src/main/java/redis/clients/jedis/BinaryShardedJedis.java @@ -1,5 +1,9 @@ package redis.clients.jedis; +import redis.clients.jedis.BinaryClient.LIST_POSITION; +import redis.clients.util.Hashing; +import redis.clients.util.Sharded; + import java.io.IOException; import java.util.Collection; import java.util.List; @@ -7,397 +11,556 @@ import java.util.Map; import java.util.Set; import java.util.regex.Pattern; -import redis.clients.jedis.BinaryClient.LIST_POSITION; -import redis.clients.util.Hashing; -import redis.clients.util.Sharded; - public class BinaryShardedJedis extends Sharded - implements BinaryJedisCommands { + implements BinaryJedisCommands { public BinaryShardedJedis(List shards) { - super(shards); + super(shards); } public BinaryShardedJedis(List shards, Hashing algo) { - super(shards, algo); + super(shards, algo); } public BinaryShardedJedis(List shards, Pattern keyTagPattern) { - super(shards, keyTagPattern); + super(shards, keyTagPattern); } public BinaryShardedJedis(List shards, Hashing algo, - Pattern keyTagPattern) { - super(shards, algo, keyTagPattern); + Pattern keyTagPattern) { + super(shards, algo, keyTagPattern); } - public void disconnect() throws IOException { - for (Jedis jedis : getAllShards()) { - jedis.disconnect(); - } + public void disconnect() { + for (Jedis jedis : getAllShards()) { + jedis.quit(); + jedis.disconnect(); + } } protected Jedis create(JedisShardInfo shard) { - return new Jedis(shard); + return new Jedis(shard); } public String set(byte[] key, byte[] value) { - Jedis j = getShard(key); - return j.set(key, value); + Jedis j = getShard(key); + return j.set(key, value); } public byte[] get(byte[] key) { - Jedis j = getShard(key); - return j.get(key); + Jedis j = getShard(key); + return j.get(key); } public Boolean exists(byte[] key) { - Jedis j = getShard(key); - return j.exists(key); + Jedis j = getShard(key); + return j.exists(key); } public String type(byte[] key) { - Jedis j = getShard(key); - return j.type(key); + Jedis j = getShard(key); + return j.type(key); } public Long expire(byte[] key, int seconds) { - Jedis j = getShard(key); - return j.expire(key, seconds); + Jedis j = getShard(key); + return j.expire(key, seconds); } public Long expireAt(byte[] key, long unixTime) { - Jedis j = getShard(key); - return j.expireAt(key, unixTime); + Jedis j = getShard(key); + return j.expireAt(key, unixTime); } public Long ttl(byte[] key) { - Jedis j = getShard(key); - return j.ttl(key); + Jedis j = getShard(key); + return j.ttl(key); } public byte[] getSet(byte[] key, byte[] value) { - Jedis j = getShard(key); - return j.getSet(key, value); + Jedis j = getShard(key); + return j.getSet(key, value); } public Long setnx(byte[] key, byte[] value) { - Jedis j = getShard(key); - return j.setnx(key, value); + Jedis j = getShard(key); + return j.setnx(key, value); } public String setex(byte[] key, int seconds, byte[] value) { - Jedis j = getShard(key); - return j.setex(key, seconds, value); + Jedis j = getShard(key); + return j.setex(key, seconds, value); } public Long decrBy(byte[] key, long integer) { - Jedis j = getShard(key); - return j.decrBy(key, integer); + Jedis j = getShard(key); + return j.decrBy(key, integer); } public Long decr(byte[] key) { - Jedis j = getShard(key); - return j.decr(key); + Jedis j = getShard(key); + return j.decr(key); } - public Long incrBy(byte[] key, long integer) { + public Long del(byte[] key) { Jedis j = getShard(key); - return j.incrBy(key, integer); + return j.del(key); + } + + public Long incrBy(byte[] key, long integer) { + Jedis j = getShard(key); + return j.incrBy(key, integer); } public Long incr(byte[] key) { - Jedis j = getShard(key); - return j.incr(key); + Jedis j = getShard(key); + return j.incr(key); } public Long append(byte[] key, byte[] value) { - Jedis j = getShard(key); - return j.append(key, value); + Jedis j = getShard(key); + return j.append(key, value); } public byte[] substr(byte[] key, int start, int end) { - Jedis j = getShard(key); - return j.substr(key, start, end); + Jedis j = getShard(key); + return j.substr(key, start, end); } public Long hset(byte[] key, byte[] field, byte[] value) { - Jedis j = getShard(key); - return j.hset(key, field, value); + Jedis j = getShard(key); + return j.hset(key, field, value); } public byte[] hget(byte[] key, byte[] field) { - Jedis j = getShard(key); - return j.hget(key, field); + Jedis j = getShard(key); + return j.hget(key, field); } public Long hsetnx(byte[] key, byte[] field, byte[] value) { - Jedis j = getShard(key); - return j.hsetnx(key, field, value); + Jedis j = getShard(key); + return j.hsetnx(key, field, value); } public String hmset(byte[] key, Map hash) { - Jedis j = getShard(key); - return j.hmset(key, hash); + Jedis j = getShard(key); + return j.hmset(key, hash); } public List hmget(byte[] key, byte[]... fields) { - Jedis j = getShard(key); - return j.hmget(key, fields); + Jedis j = getShard(key); + return j.hmget(key, fields); } public Long hincrBy(byte[] key, byte[] field, long value) { - Jedis j = getShard(key); - return j.hincrBy(key, field, value); + Jedis j = getShard(key); + return j.hincrBy(key, field, value); } public Boolean hexists(byte[] key, byte[] field) { - Jedis j = getShard(key); - return j.hexists(key, field); + Jedis j = getShard(key); + return j.hexists(key, field); } - public Long hdel(byte[] key, byte[] field) { - Jedis j = getShard(key); - return j.hdel(key, field); + public Long hdel(byte[] key, byte[]... fields) { + Jedis j = getShard(key); + return j.hdel(key, fields); } public Long hlen(byte[] key) { - Jedis j = getShard(key); - return j.hlen(key); + Jedis j = getShard(key); + return j.hlen(key); } public Set hkeys(byte[] key) { - Jedis j = getShard(key); - return j.hkeys(key); + Jedis j = getShard(key); + return j.hkeys(key); } public Collection hvals(byte[] key) { - Jedis j = getShard(key); - return j.hvals(key); + Jedis j = getShard(key); + return j.hvals(key); } public Map hgetAll(byte[] key) { - Jedis j = getShard(key); - return j.hgetAll(key); + Jedis j = getShard(key); + return j.hgetAll(key); } - public Long rpush(byte[] key, byte[] string) { - Jedis j = getShard(key); - return j.rpush(key, string); + public Long rpush(byte[] key, byte[]... strings) { + Jedis j = getShard(key); + return j.rpush(key, strings); } - public Long lpush(byte[] key, byte[] string) { + public Long lpush(byte[] key, byte[]... strings) { + Jedis j = getShard(key); + return j.lpush(key, strings); + } + + public Long strlen(final byte[] key) { + Jedis j = getShard(key); + return j.strlen(key); + } + + public Long lpushx(byte[] key, byte[]... string) { Jedis j = getShard(key); - return j.lpush(key, string); + return j.lpushx(key, string); + } + + public Long persist(final byte[] key) { + Jedis j = getShard(key); + return j.persist(key); + } + + public Long rpushx(byte[] key, byte[]... string) { + Jedis j = getShard(key); + return j.rpushx(key, string); } public Long llen(byte[] key) { - Jedis j = getShard(key); - return j.llen(key); + Jedis j = getShard(key); + return j.llen(key); } - public List lrange(byte[] key, int start, int end) { - Jedis j = getShard(key); - return j.lrange(key, start, end); + public List lrange(byte[] key, long start, long end) { + Jedis j = getShard(key); + return j.lrange(key, start, end); } - public String ltrim(byte[] key, int start, int end) { - Jedis j = getShard(key); - return j.ltrim(key, start, end); + public String ltrim(byte[] key, long start, long end) { + Jedis j = getShard(key); + return j.ltrim(key, start, end); } - public byte[] lindex(byte[] key, int index) { - Jedis j = getShard(key); - return j.lindex(key, index); + public byte[] lindex(byte[] key, long index) { + Jedis j = getShard(key); + return j.lindex(key, index); } - public String lset(byte[] key, int index, byte[] value) { - Jedis j = getShard(key); - return j.lset(key, index, value); + public String lset(byte[] key, long index, byte[] value) { + Jedis j = getShard(key); + return j.lset(key, index, value); } - public Long lrem(byte[] key, int count, byte[] value) { - Jedis j = getShard(key); - return j.lrem(key, count, value); + public Long lrem(byte[] key, long count, byte[] value) { + Jedis j = getShard(key); + return j.lrem(key, count, value); } public byte[] lpop(byte[] key) { - Jedis j = getShard(key); - return j.lpop(key); + Jedis j = getShard(key); + return j.lpop(key); } public byte[] rpop(byte[] key) { - Jedis j = getShard(key); - return j.rpop(key); + Jedis j = getShard(key); + return j.rpop(key); } - public Long sadd(byte[] key, byte[] member) { - Jedis j = getShard(key); - return j.sadd(key, member); + public Long sadd(byte[] key, byte[]... members) { + Jedis j = getShard(key); + return j.sadd(key, members); } public Set smembers(byte[] key) { - Jedis j = getShard(key); - return j.smembers(key); + Jedis j = getShard(key); + return j.smembers(key); } - public Long srem(byte[] key, byte[] member) { - Jedis j = getShard(key); - return j.srem(key, member); + public Long srem(byte[] key, byte[]... members) { + Jedis j = getShard(key); + return j.srem(key, members); } public byte[] spop(byte[] key) { - Jedis j = getShard(key); - return j.spop(key); + Jedis j = getShard(key); + return j.spop(key); } public Long scard(byte[] key) { - Jedis j = getShard(key); - return j.scard(key); + Jedis j = getShard(key); + return j.scard(key); } public Boolean sismember(byte[] key, byte[] member) { - Jedis j = getShard(key); - return j.sismember(key, member); + Jedis j = getShard(key); + return j.sismember(key, member); } public byte[] srandmember(byte[] key) { - Jedis j = getShard(key); - return j.srandmember(key); + Jedis j = getShard(key); + return j.srandmember(key); } public Long zadd(byte[] key, double score, byte[] member) { - Jedis j = getShard(key); - return j.zadd(key, score, member); + Jedis j = getShard(key); + return j.zadd(key, score, member); } - public Set zrange(byte[] key, int start, int end) { - Jedis j = getShard(key); - return j.zrange(key, start, end); + public Long zadd(byte[] key, Map scoreMembers) { + Jedis j = getShard(key); + return j.zadd(key, scoreMembers); } - public Long zrem(byte[] key, byte[] member) { - Jedis j = getShard(key); - return j.zrem(key, member); + public Set zrange(byte[] key, long start, long end) { + Jedis j = getShard(key); + return j.zrange(key, start, end); + } + + public Long zrem(byte[] key, byte[]... members) { + Jedis j = getShard(key); + return j.zrem(key, members); } public Double zincrby(byte[] key, double score, byte[] member) { - Jedis j = getShard(key); - return j.zincrby(key, score, member); + Jedis j = getShard(key); + return j.zincrby(key, score, member); } public Long zrank(byte[] key, byte[] member) { - Jedis j = getShard(key); - return j.zrank(key, member); + Jedis j = getShard(key); + return j.zrank(key, member); } public Long zrevrank(byte[] key, byte[] member) { - Jedis j = getShard(key); - return j.zrevrank(key, member); + Jedis j = getShard(key); + return j.zrevrank(key, member); } - public Set zrevrange(byte[] key, int start, int end) { - Jedis j = getShard(key); - return j.zrevrange(key, start, end); + public Set zrevrange(byte[] key, long start, long end) { + Jedis j = getShard(key); + return j.zrevrange(key, start, end); } - public Set zrangeWithScores(byte[] key, int start, int end) { - Jedis j = getShard(key); - return j.zrangeWithScores(key, start, end); + public Set zrangeWithScores(byte[] key, long start, long end) { + Jedis j = getShard(key); + return j.zrangeWithScores(key, start, end); } - public Set zrevrangeWithScores(byte[] key, int start, int end) { - Jedis j = getShard(key); - return j.zrevrangeWithScores(key, start, end); + public Set zrevrangeWithScores(byte[] key, long start, long end) { + Jedis j = getShard(key); + return j.zrevrangeWithScores(key, start, end); } public Long zcard(byte[] key) { - Jedis j = getShard(key); - return j.zcard(key); + Jedis j = getShard(key); + return j.zcard(key); } public Double zscore(byte[] key, byte[] member) { - Jedis j = getShard(key); - return j.zscore(key, member); + Jedis j = getShard(key); + return j.zscore(key, member); } public List sort(byte[] key) { - Jedis j = getShard(key); - return j.sort(key); + Jedis j = getShard(key); + return j.sort(key); } public List sort(byte[] key, SortingParams sortingParameters) { - Jedis j = getShard(key); - return j.sort(key, sortingParameters); + Jedis j = getShard(key); + return j.sort(key, sortingParameters); } public Long zcount(byte[] key, double min, double max) { - Jedis j = getShard(key); - return j.zcount(key, min, max); + Jedis j = getShard(key); + return j.zcount(key, min, max); + } + + public Long zcount(byte[] key, byte[] min, byte[] max) { + Jedis j = getShard(key); + return j.zcount(key, min, max); } public Set zrangeByScore(byte[] key, double min, double max) { + Jedis j = getShard(key); + return j.zrangeByScore(key, min, max); + } + + public Set zrangeByScore(byte[] key, double min, double max, + int offset, int count) { + Jedis j = getShard(key); + return j.zrangeByScore(key, min, max, offset, count); + } + + public Set zrangeByScoreWithScores(byte[] key, double min, double max) { + Jedis j = getShard(key); + return j.zrangeByScoreWithScores(key, min, max); + } + + public Set zrangeByScoreWithScores(byte[] key, double min, + double max, int offset, int count) { + Jedis j = getShard(key); + return j.zrangeByScoreWithScores(key, min, max, offset, count); + } + + public Set zrangeByScore(byte[] key, byte[] min, byte[] max) { Jedis j = getShard(key); return j.zrangeByScore(key, min, max); } - public Set zrangeByScore(byte[] key, double min, double max, - int offset, int count) { + public Set zrangeByScoreWithScores(byte[] key, byte[] min, byte[] max) { + Jedis j = getShard(key); + return j.zrangeByScoreWithScores(key, min, max); + } + + public Set zrangeByScoreWithScores(byte[] key, byte[] min, + byte[] max, int offset, int count) { + Jedis j = getShard(key); + return j.zrangeByScoreWithScores(key, min, max, offset, count); + } + + public Set zrangeByScore(byte[] key, byte[] min, byte[] max, int offset, int count) { Jedis j = getShard(key); return j.zrangeByScore(key, min, max, offset, count); } - public Set zrangeByScoreWithScores(byte[] key, double min, double max) { - Jedis j = getShard(key); - return j.zrangeByScoreWithScores(key, min, max); - } - - public Set zrangeByScoreWithScores(byte[] key, double min, - double max, int offset, int count) { - Jedis j = getShard(key); - return j.zrangeByScoreWithScores(key, min, max, offset, count); - } - public Set zrevrangeByScore(byte[] key, double max, double min) { - Jedis j = getShard(key); - return j.zrevrangeByScore(key, max, min); + Jedis j = getShard(key); + return j.zrevrangeByScore(key, max, min); } public Set zrevrangeByScore(byte[] key, double max, double min, - int offset, int count) { - Jedis j = getShard(key); - return j.zrevrangeByScore(key, max, min, offset, count); - } - - public Set zrevrangeByScoreWithScores(byte[] key, double max, double min) { - Jedis j = getShard(key); - return j.zrevrangeByScoreWithScores(key, max, min); + int offset, int count) { + Jedis j = getShard(key); + return j.zrevrangeByScore(key, max, min, offset, count); } public Set zrevrangeByScoreWithScores(byte[] key, double max, - double min, int offset, int count) { - Jedis j = getShard(key); - return j.zrevrangeByScoreWithScores(key, max, min, offset, count); + double min) { + Jedis j = getShard(key); + return j.zrevrangeByScoreWithScores(key, max, min); } - public Long zremrangeByRank(byte[] key, int start, int end) { - Jedis j = getShard(key); - return j.zremrangeByRank(key, start, end); + public Set zrevrangeByScoreWithScores(byte[] key, double max, + double min, int offset, int count) { + Jedis j = getShard(key); + return j.zrevrangeByScoreWithScores(key, max, min, offset, count); + } + + public Set zrevrangeByScore(byte[] key, byte[] max, byte[] min) { + Jedis j = getShard(key); + return j.zrevrangeByScore(key, max, min); + } + + public Set zrevrangeByScore(byte[] key, byte[] max, byte[] min, + int offset, int count) { + Jedis j = getShard(key); + return j.zrevrangeByScore(key, max, min, offset, count); + } + + public Set zrevrangeByScoreWithScores(byte[] key, byte[] max, + byte[] min) { + Jedis j = getShard(key); + return j.zrevrangeByScoreWithScores(key, max, min); + } + + public Set zrevrangeByScoreWithScores(byte[] key, byte[] max, + byte[] min, int offset, int count) { + Jedis j = getShard(key); + return j.zrevrangeByScoreWithScores(key, max, min, offset, count); + } + + public Long zremrangeByRank(byte[] key, long start, long end) { + Jedis j = getShard(key); + return j.zremrangeByRank(key, start, end); } public Long zremrangeByScore(byte[] key, double start, double end) { - Jedis j = getShard(key); - return j.zremrangeByScore(key, start, end); + Jedis j = getShard(key); + return j.zremrangeByScore(key, start, end); + } + + public Long zremrangeByScore(byte[] key, byte[] start, byte[] end) { + Jedis j = getShard(key); + return j.zremrangeByScore(key, start, end); } public Long linsert(byte[] key, LIST_POSITION where, byte[] pivot, - byte[] value) { - Jedis j = getShard(key); - return j.linsert(key, where, pivot, value); + byte[] value) { + Jedis j = getShard(key); + return j.linsert(key, where, pivot, value); } + @Deprecated public List pipelined(ShardedJedisPipeline shardedJedisPipeline) { - shardedJedisPipeline.setShardedJedis(this); - shardedJedisPipeline.execute(); - return shardedJedisPipeline.getResults(); + shardedJedisPipeline.setShardedJedis(this); + shardedJedisPipeline.execute(); + return shardedJedisPipeline.getResults(); + } + + public ShardedJedisPipeline pipelined() { + ShardedJedisPipeline pipeline = new ShardedJedisPipeline(); + pipeline.setShardedJedis(this); + return pipeline; + } + + public Long objectRefcount(byte[] key) { + Jedis j = getShard(key); + return j.objectRefcount(key); + } + + public byte[] objectEncoding(byte[] key) { + Jedis j = getShard(key); + return j.objectEncoding(key); + } + + public Long objectIdletime(byte[] key) { + Jedis j = getShard(key); + return j.objectIdletime(key); + } + + public Boolean setbit(byte[] key, long offset, boolean value) { + Jedis j = getShard(key); + return j.setbit(key, offset, value); + } + + public Boolean setbit(byte[] key, long offset, byte[] value) { + Jedis j = getShard(key); + return j.setbit(key, offset, value); + } + + public Boolean getbit(byte[] key, long offset) { + Jedis j = getShard(key); + return j.getbit(key, offset); + } + + public Long setrange(byte[] key, long offset, byte[] value) { + Jedis j = getShard(key); + return j.setrange(key, offset, value); + } + + public byte[] getrange(byte[] key, long startOffset, long endOffset) { + Jedis j = getShard(key); + return j.getrange(key, startOffset, endOffset); + } + + public Long move(byte[] key, int dbIndex) { + Jedis j = getShard(key); + return j.move(key, dbIndex); + } + + public byte[] echo(byte[] arg) { + Jedis j = getShard(arg); + return j.echo(arg); + } + + public List brpop(byte[] arg) { + Jedis j = getShard(arg); + return j.brpop(arg); + } + + public List blpop(byte[] arg) { + Jedis j = getShard(arg); + return j.blpop(arg); + } + + public Long bitcount(byte[] key) { + Jedis j = getShard(key); + return j.bitcount(key); + } + + public Long bitcount(byte[] key, long start, long end) { + Jedis j = getShard(key); + return j.bitcount(key, start, end); } } \ No newline at end of file diff --git a/src/main/java/redis/clients/jedis/BinaryTransaction.java b/src/main/java/redis/clients/jedis/BinaryTransaction.java deleted file mode 100644 index 7f50a68..0000000 --- a/src/main/java/redis/clients/jedis/BinaryTransaction.java +++ /dev/null @@ -1,546 +0,0 @@ -package redis.clients.jedis; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import redis.clients.jedis.BinaryClient.LIST_POSITION; - -public class BinaryTransaction extends Queable { - protected Client client = null; - protected boolean inTransaction = true; - - public BinaryTransaction() { - } - - public BinaryTransaction(final Client client) { - this.client = client; - } - - public List exec() { - client.exec(); - client.getAll(1); // Discard all but the last reply - - List unformatted = client.getObjectMultiBulkReply(); - if (unformatted == null) { - return null; - } - List formatted = new ArrayList(); - for (Object o : unformatted) { - formatted.add(generateResponse(o).get()); - } - return formatted; - } - - public String discard() { - client.discard(); - client.getAll(1); // Discard all but the last reply - inTransaction = false; - clean(); - return client.getStatusCodeReply(); - } - - public Response append(byte[] key, byte[] value) { - client.append(key, value); - return getResponse(BuilderFactory.LONG); - } - - public Response> blpop(byte[]... args) { - client.blpop(args); - return getResponse(BuilderFactory.STRING_LIST); - } - - public Response> brpop(byte[]... args) { - client.brpop(args); - return getResponse(BuilderFactory.STRING_LIST); - } - - public Response decr(byte[] key) { - client.decr(key); - return getResponse(BuilderFactory.LONG); - } - - public Response decrBy(byte[] key, long integer) { - client.decrBy(key, integer); - return getResponse(BuilderFactory.LONG); - } - - public Response del(byte[]... keys) { - client.del(keys); - return getResponse(BuilderFactory.LONG); - } - - public Response echo(byte[] string) { - client.echo(string); - return getResponse(BuilderFactory.STRING); - } - - public Response exists(byte[] key) { - client.exists(key); - return getResponse(BuilderFactory.BOOLEAN); - } - - public Response expire(byte[] key, int seconds) { - client.expire(key, seconds); - return getResponse(BuilderFactory.LONG); - } - - public Response expireAt(byte[] key, long unixTime) { - client.expireAt(key, unixTime); - return getResponse(BuilderFactory.LONG); - } - - public Response get(byte[] key) { - client.get(key); - return getResponse(BuilderFactory.BYTE_ARRAY); - } - - public Response getSet(byte[] key, byte[] value) { - client.getSet(key, value); - return getResponse(BuilderFactory.STRING); - } - - public Response hdel(byte[] key, byte[] field) { - client.hdel(key, field); - return getResponse(BuilderFactory.LONG); - } - - public Response hexists(byte[] key, byte[] field) { - client.hexists(key, field); - return getResponse(BuilderFactory.BOOLEAN); - } - - public Response hget(byte[] key, byte[] field) { - client.hget(key, field); - return getResponse(BuilderFactory.BYTE_ARRAY); - } - - public Response> hgetAll(byte[] key) { - client.hgetAll(key); - return getResponse(BuilderFactory.STRING_MAP); - } - - public Response hincrBy(byte[] key, byte[] field, long value) { - client.hincrBy(key, field, value); - return getResponse(BuilderFactory.LONG); - } - - public Response> hkeys(byte[] key) { - client.hkeys(key); - return getResponse(BuilderFactory.BYTE_ARRAY_ZSET); - } - - public Response hlen(byte[] key) { - client.hlen(key); - return getResponse(BuilderFactory.LONG); - } - - public Response> hmget(byte[] key, byte[]... fields) { - client.hmget(key, fields); - return getResponse(BuilderFactory.BYTE_ARRAY_LIST); - } - - public Response hmset(byte[] key, Map hash) { - client.hmset(key, hash); - return getResponse(BuilderFactory.BYTE_ARRAY); - } - - public Response hset(byte[] key, byte[] field, byte[] value) { - client.hset(key, field, value); - return getResponse(BuilderFactory.LONG); - } - - public Response hsetnx(byte[] key, byte[] field, byte[] value) { - client.hsetnx(key, field, value); - return getResponse(BuilderFactory.LONG); - } - - public Response> hvals(byte[] key) { - client.hvals(key); - return getResponse(BuilderFactory.BYTE_ARRAY_LIST); - } - - public Response incr(byte[] key) { - client.incr(key); - return getResponse(BuilderFactory.LONG); - } - - public Response incrBy(byte[] key, long integer) { - client.incrBy(key, integer); - return getResponse(BuilderFactory.LONG); - } - - public Response> keys(byte[] pattern) { - client.keys(pattern); - return getResponse(BuilderFactory.BYTE_ARRAY_ZSET); - } - - public Response lindex(byte[] key, long index) { - client.lindex(key, index); - return getResponse(BuilderFactory.BYTE_ARRAY); - } - - public Response linsert(byte[] key, LIST_POSITION where, - byte[] pivot, byte[] value) { - client.linsert(key, where, pivot, value); - return getResponse(BuilderFactory.LONG); - } - - public Response llen(byte[] key) { - client.llen(key); - return getResponse(BuilderFactory.LONG); - } - - public Response lpop(byte[] key) { - client.lpop(key); - return getResponse(BuilderFactory.BYTE_ARRAY); - } - - public Response lpush(byte[] key, byte[] string) { - client.lpush(key, string); - return getResponse(BuilderFactory.LONG); - } - - public Response lpushx(byte[] key, byte[] bytes) { - client.lpushx(key, bytes); - return getResponse(BuilderFactory.LONG); - } - - public Response> lrange(byte[] key, long start, long end) { - client.lrange(key, start, end); - return getResponse(BuilderFactory.BYTE_ARRAY_LIST); - } - - public Response lrem(byte[] key, long count, byte[] value) { - client.lrem(key, count, value); - return getResponse(BuilderFactory.LONG); - } - - public Response lset(byte[] key, long index, byte[] value) { - client.lset(key, index, value); - return getResponse(BuilderFactory.STRING); - } - - public Response ltrim(byte[] key, long start, long end) { - client.ltrim(key, start, end); - return getResponse(BuilderFactory.STRING); - } - - public Response> mget(byte[]... keys) { - client.mget(keys); - return getResponse(BuilderFactory.BYTE_ARRAY_LIST); - } - - public Response move(byte[] key, int dbIndex) { - client.move(key, dbIndex); - return getResponse(BuilderFactory.LONG); - } - - public Response mset(byte[]... keysvalues) { - client.mset(keysvalues); - return getResponse(BuilderFactory.STRING); - } - - public Response msetnx(byte[]... keysvalues) { - client.msetnx(keysvalues); - return getResponse(BuilderFactory.LONG); - } - - public Response persist(byte[] key) { - client.persist(key); - return getResponse(BuilderFactory.LONG); - } - - public Response rename(byte[] oldkey, byte[] newkey) { - client.rename(oldkey, newkey); - return getResponse(BuilderFactory.STRING); - } - - public Response renamenx(byte[] oldkey, byte[] newkey) { - client.renamenx(oldkey, newkey); - return getResponse(BuilderFactory.LONG); - } - - public Response rpop(byte[] key) { - client.rpop(key); - return getResponse(BuilderFactory.BYTE_ARRAY); - } - - public Response rpoplpush(byte[] srckey, byte[] dstkey) { - client.rpoplpush(srckey, dstkey); - return getResponse(BuilderFactory.BYTE_ARRAY); - } - - public Response rpush(byte[] key, byte[] string) { - client.rpush(key, string); - return getResponse(BuilderFactory.LONG); - } - - public Response rpushx(byte[] key, byte[] string) { - client.rpushx(key, string); - return getResponse(BuilderFactory.LONG); - } - - public Response sadd(byte[] key, byte[] member) { - client.sadd(key, member); - return getResponse(BuilderFactory.LONG); - } - - public Response scard(byte[] key) { - client.scard(key); - return getResponse(BuilderFactory.LONG); - } - - public Response> sdiff(byte[]... keys) { - client.sdiff(keys); - return getResponse(BuilderFactory.BYTE_ARRAY_ZSET); - } - - public Response sdiffstore(byte[] dstkey, byte[]... keys) { - client.sdiffstore(dstkey, keys); - return getResponse(BuilderFactory.LONG); - } - - public Response set(byte[] key, byte[] value) { - client.set(key, value); - return getResponse(BuilderFactory.BYTE_ARRAY); - } - - public Response setbit(String key, long offset, boolean value) { - client.setbit(key, offset, value); - return getResponse(BuilderFactory.BOOLEAN); - } - - public Response setex(byte[] key, int seconds, byte[] value) { - client.setex(key, seconds, value); - return getResponse(BuilderFactory.STRING); - } - - public Response setnx(byte[] key, byte[] value) { - client.setnx(key, value); - return getResponse(BuilderFactory.LONG); - } - - public Response> sinter(byte[]... keys) { - client.sinter(keys); - return getResponse(BuilderFactory.BYTE_ARRAY_ZSET); - } - - public Response sinterstore(byte[] dstkey, byte[]... keys) { - client.sinterstore(dstkey, keys); - return getResponse(BuilderFactory.LONG); - } - - public Response sismember(byte[] key, byte[] member) { - client.sismember(key, member); - return getResponse(BuilderFactory.BOOLEAN); - } - - public Response> smembers(byte[] key) { - client.smembers(key); - return getResponse(BuilderFactory.BYTE_ARRAY_ZSET); - } - - public Response smove(byte[] srckey, byte[] dstkey, byte[] member) { - client.smove(srckey, dstkey, member); - return getResponse(BuilderFactory.LONG); - } - - public Response> sort(byte[] key) { - client.sort(key); - return getResponse(BuilderFactory.BYTE_ARRAY_LIST); - } - - public Response> sort(byte[] key, - SortingParams sortingParameters) { - client.sort(key, sortingParameters); - return getResponse(BuilderFactory.BYTE_ARRAY_LIST); - } - - public Response> sort(byte[] key, - SortingParams sortingParameters, byte[] dstkey) { - client.sort(key, sortingParameters, dstkey); - return getResponse(BuilderFactory.BYTE_ARRAY_LIST); - } - - public Response> sort(byte[] key, byte[] dstkey) { - client.sort(key, dstkey); - return getResponse(BuilderFactory.BYTE_ARRAY_LIST); - } - - public Response spop(byte[] key) { - client.spop(key); - return getResponse(BuilderFactory.BYTE_ARRAY); - } - - public Response srandmember(byte[] key) { - client.srandmember(key); - return getResponse(BuilderFactory.BYTE_ARRAY); - } - - public Response srem(byte[] key, byte[] member) { - client.srem(key, member); - return getResponse(BuilderFactory.LONG); - } - - public Response strlen(byte[] key) { - client.strlen(key); - return getResponse(BuilderFactory.LONG); - } - - public Response substr(byte[] key, int start, int end) { // what's - // that? - client.substr(key, start, end); - return getResponse(BuilderFactory.STRING); - } - - public Response> sunion(byte[]... keys) { - client.sunion(keys); - return getResponse(BuilderFactory.BYTE_ARRAY_ZSET); - } - - public Response sunionstore(byte[] dstkey, byte[]... keys) { - client.sunionstore(dstkey, keys); - return getResponse(BuilderFactory.LONG); - } - - public Response ttl(byte[] key) { - client.ttl(key); - return getResponse(BuilderFactory.LONG); - } - - public Response type(byte[] key) { - client.type(key); - return getResponse(BuilderFactory.STRING); - } - - public Response zadd(byte[] key, double score, byte[] member) { - client.zadd(key, score, member); - return getResponse(BuilderFactory.LONG); - } - - public Response zcard(byte[] key) { - client.zcard(key); - return getResponse(BuilderFactory.LONG); - } - - public Response zcount(byte[] key, double min, double max) { - client.zcount(key, min, max); - return getResponse(BuilderFactory.LONG); - } - - public Response zincrby(byte[] key, double score, byte[] member) { - client.zincrby(key, score, member); - return getResponse(BuilderFactory.DOUBLE); - } - - public Response zinterstore(byte[] dstkey, byte[]... sets) { - client.zinterstore(dstkey, sets); - return getResponse(BuilderFactory.LONG); - } - - public Response zinterstore(byte[] dstkey, ZParams params, - byte[]... sets) { - client.zinterstore(dstkey, params, sets); - return getResponse(BuilderFactory.LONG); - } - - public Response> zrange(byte[] key, int start, int end) { - client.zrange(key, start, end); - return getResponse(BuilderFactory.BYTE_ARRAY_ZSET); - } - - public Response> zrangeByScore(byte[] key, double min, - double max) { - client.zrangeByScore(key, min, max); - return getResponse(BuilderFactory.BYTE_ARRAY_ZSET); - } - - public Response> zrangeByScore(byte[] key, byte[] min, - byte[] max) { - client.zrangeByScore(key, min, max); - return getResponse(BuilderFactory.BYTE_ARRAY_ZSET); - } - - public Response> zrangeByScore(byte[] key, double min, - double max, int offset, int count) { - client.zrangeByScore(key, min, max, offset, count); - return getResponse(BuilderFactory.BYTE_ARRAY_ZSET); - } - - public Response> zrangeByScoreWithScores(byte[] key, double min, - double max) { - client.zrangeByScoreWithScores(key, min, max); - return getResponse(BuilderFactory.TUPLE_ZSET_BINARY); - } - - public Response> zrangeByScoreWithScores(byte[] key, double min, - double max, int offset, int count) { - client.zrangeByScoreWithScores(key, min, max, offset, count); - return getResponse(BuilderFactory.TUPLE_ZSET_BINARY); - } - - public Response> zrangeWithScores(byte[] key, int start, int end) { - client.zrangeWithScores(key, start, end); - return getResponse(BuilderFactory.TUPLE_ZSET_BINARY); - } - - public Response zrank(byte[] key, byte[] member) { - client.zrank(key, member); - return getResponse(BuilderFactory.LONG); - } - - public Response zrem(byte[] key, byte[] member) { - client.zrem(key, member); - return getResponse(BuilderFactory.LONG); - } - - public Response zremrangeByRank(byte[] key, int start, int end) { - client.zremrangeByRank(key, start, end); - return getResponse(BuilderFactory.LONG); - } - - public Response zremrangeByScore(byte[] key, double start, double end) { - client.zremrangeByScore(key, start, end); - return getResponse(BuilderFactory.LONG); - } - - public Response> zrevrange(byte[] key, int start, int end) { - client.zrevrange(key, start, end); - return getResponse(BuilderFactory.BYTE_ARRAY_ZSET); - } - - public Response> zrevrangeWithScores(byte[] key, int start, - int end) { - client.zrevrangeWithScores(key, start, end); - return getResponse(BuilderFactory.TUPLE_ZSET_BINARY); - } - - public Response zrevrank(byte[] key, byte[] member) { - client.zrevrank(key, member); - return getResponse(BuilderFactory.LONG); - } - - public Response zscore(byte[] key, byte[] member) { - client.zscore(key, member); - return getResponse(BuilderFactory.DOUBLE); - } - - public Response zunionstore(byte[] dstkey, byte[]... sets) { - client.zunionstore(dstkey, sets); - return getResponse(BuilderFactory.LONG); - } - - public Response zunionstore(byte[] dstkey, ZParams params, - byte[]... sets) { - client.zunionstore(dstkey, params, sets); - return getResponse(BuilderFactory.LONG); - } - - public Response brpoplpush(byte[] source, byte[] destination, - int timeout) { - client.brpoplpush(source, destination, timeout); - return getResponse(BuilderFactory.BYTE_ARRAY); - } -} diff --git a/src/main/java/redis/clients/jedis/BitOP.java b/src/main/java/redis/clients/jedis/BitOP.java new file mode 100644 index 0000000..5e3ee89 --- /dev/null +++ b/src/main/java/redis/clients/jedis/BitOP.java @@ -0,0 +1,8 @@ +package redis.clients.jedis; + +public enum BitOP { + AND, + OR, + XOR, + NOT; +} diff --git a/src/main/java/redis/clients/jedis/BuilderFactory.java b/src/main/java/redis/clients/jedis/BuilderFactory.java old mode 100644 new mode 100755 index ad94282..bebd2d6 --- a/src/main/java/redis/clients/jedis/BuilderFactory.java +++ b/src/main/java/redis/clients/jedis/BuilderFactory.java @@ -7,7 +7,8 @@ import java.util.*; public class BuilderFactory { public static final Builder DOUBLE = new Builder() { public Double build(Object data) { - return Double.valueOf(STRING.build(data)); + String asString = STRING.build(data); + return asString == null ? null : Double.valueOf(asString); } public String toString() { @@ -143,6 +144,13 @@ public class BuilderFactory { } List l = (List) data; final Set result = new LinkedHashSet(l); + for (final byte[] barray : l) { + if (barray == null) { + result.add(null); + } else { + result.add(barray); + } + } return result; } @@ -150,6 +158,24 @@ public class BuilderFactory { return "ZSet"; } }; + public static final Builder> BYTE_ARRAY_MAP = new Builder>() { + @SuppressWarnings("unchecked") + public Map build(Object data) { + final List flatHash = (List) data; + final Map hash = new HashMap(); + final Iterator iterator = flatHash.iterator(); + while (iterator.hasNext()) { + hash.put(iterator.next(), iterator.next()); + } + + return hash; + } + + public String toString() { + return "Map"; + } + + }; public static final Builder> STRING_ZSET = new Builder>() { @SuppressWarnings("unchecked") diff --git a/src/main/java/redis/clients/jedis/Client.java b/src/main/java/redis/clients/jedis/Client.java index fb35c10..ce20fbc 100644 --- a/src/main/java/redis/clients/jedis/Client.java +++ b/src/main/java/redis/clients/jedis/Client.java @@ -1,590 +1,811 @@ package redis.clients.jedis; -import redis.clients.util.SafeEncoder; - -import java.util.HashMap; -import java.util.Map; -import java.util.Map.Entry; - import static redis.clients.jedis.Protocol.toByteArray; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +import redis.clients.util.SafeEncoder; + public class Client extends BinaryClient implements Commands { public Client(final String host) { - super(host); + super(host); } public Client(final String host, final int port) { - super(host, port); + super(host, port); } public void set(final String key, final String value) { - set(SafeEncoder.encode(key), SafeEncoder.encode(value)); + set(SafeEncoder.encode(key), SafeEncoder.encode(value)); } public void get(final String key) { - get(SafeEncoder.encode(key)); + get(SafeEncoder.encode(key)); } public void exists(final String key) { - exists(SafeEncoder.encode(key)); + exists(SafeEncoder.encode(key)); } public void del(final String... keys) { - final byte[][] bkeys = new byte[keys.length][]; - for (int i = 0; i < keys.length; i++) { - bkeys[i] = SafeEncoder.encode(keys[i]); - } - del(bkeys); + final byte[][] bkeys = new byte[keys.length][]; + for (int i = 0; i < keys.length; i++) { + bkeys[i] = SafeEncoder.encode(keys[i]); + } + del(bkeys); } public void type(final String key) { - type(SafeEncoder.encode(key)); + type(SafeEncoder.encode(key)); } public void keys(final String pattern) { - keys(SafeEncoder.encode(pattern)); + keys(SafeEncoder.encode(pattern)); } public void rename(final String oldkey, final String newkey) { - rename(SafeEncoder.encode(oldkey), SafeEncoder.encode(newkey)); + rename(SafeEncoder.encode(oldkey), SafeEncoder.encode(newkey)); } public void renamenx(final String oldkey, final String newkey) { - renamenx(SafeEncoder.encode(oldkey), SafeEncoder.encode(newkey)); + renamenx(SafeEncoder.encode(oldkey), SafeEncoder.encode(newkey)); } public void expire(final String key, final int seconds) { - expire(SafeEncoder.encode(key), seconds); + expire(SafeEncoder.encode(key), seconds); } public void expireAt(final String key, final long unixTime) { - expireAt(SafeEncoder.encode(key), unixTime); + expireAt(SafeEncoder.encode(key), unixTime); } public void ttl(final String key) { - ttl(SafeEncoder.encode(key)); + ttl(SafeEncoder.encode(key)); } public void move(final String key, final int dbIndex) { - move(SafeEncoder.encode(key), dbIndex); + move(SafeEncoder.encode(key), dbIndex); } public void getSet(final String key, final String value) { - getSet(SafeEncoder.encode(key), SafeEncoder.encode(value)); + getSet(SafeEncoder.encode(key), SafeEncoder.encode(value)); } public void mget(final String... keys) { - final byte[][] bkeys = new byte[keys.length][]; - for (int i = 0; i < bkeys.length; i++) { - bkeys[i] = SafeEncoder.encode(keys[i]); - } - mget(bkeys); + final byte[][] bkeys = new byte[keys.length][]; + for (int i = 0; i < bkeys.length; i++) { + bkeys[i] = SafeEncoder.encode(keys[i]); + } + mget(bkeys); } public void setnx(final String key, final String value) { - setnx(SafeEncoder.encode(key), SafeEncoder.encode(value)); + setnx(SafeEncoder.encode(key), SafeEncoder.encode(value)); } public void setex(final String key, final int seconds, final String value) { - setex(SafeEncoder.encode(key), seconds, SafeEncoder.encode(value)); + setex(SafeEncoder.encode(key), seconds, SafeEncoder.encode(value)); } public void mset(final String... keysvalues) { - final byte[][] bkeysvalues = new byte[keysvalues.length][]; - for (int i = 0; i < keysvalues.length; i++) { - bkeysvalues[i] = SafeEncoder.encode(keysvalues[i]); - } - mset(bkeysvalues); + final byte[][] bkeysvalues = new byte[keysvalues.length][]; + for (int i = 0; i < keysvalues.length; i++) { + bkeysvalues[i] = SafeEncoder.encode(keysvalues[i]); + } + mset(bkeysvalues); } public void msetnx(final String... keysvalues) { - final byte[][] bkeysvalues = new byte[keysvalues.length][]; - for (int i = 0; i < keysvalues.length; i++) { - bkeysvalues[i] = SafeEncoder.encode(keysvalues[i]); - } - msetnx(bkeysvalues); + final byte[][] bkeysvalues = new byte[keysvalues.length][]; + for (int i = 0; i < keysvalues.length; i++) { + bkeysvalues[i] = SafeEncoder.encode(keysvalues[i]); + } + msetnx(bkeysvalues); } public void decrBy(final String key, final long integer) { - decrBy(SafeEncoder.encode(key), integer); + decrBy(SafeEncoder.encode(key), integer); } public void decr(final String key) { - decr(SafeEncoder.encode(key)); + decr(SafeEncoder.encode(key)); } public void incrBy(final String key, final long integer) { - incrBy(SafeEncoder.encode(key), integer); + incrBy(SafeEncoder.encode(key), integer); } public void incr(final String key) { - incr(SafeEncoder.encode(key)); + incr(SafeEncoder.encode(key)); } public void append(final String key, final String value) { - append(SafeEncoder.encode(key), SafeEncoder.encode(value)); + append(SafeEncoder.encode(key), SafeEncoder.encode(value)); } public void substr(final String key, final int start, final int end) { - substr(SafeEncoder.encode(key), start, end); + substr(SafeEncoder.encode(key), start, end); } public void hset(final String key, final String field, final String value) { - hset(SafeEncoder.encode(key), SafeEncoder.encode(field), SafeEncoder - .encode(value)); + hset(SafeEncoder.encode(key), SafeEncoder.encode(field), + SafeEncoder.encode(value)); } public void hget(final String key, final String field) { - hget(SafeEncoder.encode(key), SafeEncoder.encode(field)); + hget(SafeEncoder.encode(key), SafeEncoder.encode(field)); } public void hsetnx(final String key, final String field, final String value) { - hsetnx(SafeEncoder.encode(key), SafeEncoder.encode(field), SafeEncoder - .encode(value)); + hsetnx(SafeEncoder.encode(key), SafeEncoder.encode(field), + SafeEncoder.encode(value)); } public void hmset(final String key, final Map hash) { - final Map bhash = new HashMap(hash - .size()); - for (final Entry entry : hash.entrySet()) { - bhash.put(SafeEncoder.encode(entry.getKey()), SafeEncoder - .encode(entry.getValue())); - } - hmset(SafeEncoder.encode(key), bhash); + final Map bhash = new HashMap( + hash.size()); + for (final Entry entry : hash.entrySet()) { + bhash.put(SafeEncoder.encode(entry.getKey()), + SafeEncoder.encode(entry.getValue())); + } + hmset(SafeEncoder.encode(key), bhash); } public void hmget(final String key, final String... fields) { - final byte[][] bfields = new byte[fields.length][]; - for (int i = 0; i < bfields.length; i++) { - bfields[i] = SafeEncoder.encode(fields[i]); - } - hmget(SafeEncoder.encode(key), bfields); + final byte[][] bfields = new byte[fields.length][]; + for (int i = 0; i < bfields.length; i++) { + bfields[i] = SafeEncoder.encode(fields[i]); + } + hmget(SafeEncoder.encode(key), bfields); } public void hincrBy(final String key, final String field, final long value) { - hincrBy(SafeEncoder.encode(key), SafeEncoder.encode(field), value); + hincrBy(SafeEncoder.encode(key), SafeEncoder.encode(field), value); } public void hexists(final String key, final String field) { - hexists(SafeEncoder.encode(key), SafeEncoder.encode(field)); + hexists(SafeEncoder.encode(key), SafeEncoder.encode(field)); } - public void hdel(final String key, final String field) { - hdel(SafeEncoder.encode(key), SafeEncoder.encode(field)); + public void hdel(final String key, final String... fields) { + hdel(SafeEncoder.encode(key), SafeEncoder.encodeMany(fields)); } public void hlen(final String key) { - hlen(SafeEncoder.encode(key)); + hlen(SafeEncoder.encode(key)); } public void hkeys(final String key) { - hkeys(SafeEncoder.encode(key)); + hkeys(SafeEncoder.encode(key)); } public void hvals(final String key) { - hvals(SafeEncoder.encode(key)); + hvals(SafeEncoder.encode(key)); } public void hgetAll(final String key) { - hgetAll(SafeEncoder.encode(key)); + hgetAll(SafeEncoder.encode(key)); } - public void rpush(final String key, final String string) { - rpush(SafeEncoder.encode(key), SafeEncoder.encode(string)); + public void rpush(final String key, final String... string) { + rpush(SafeEncoder.encode(key), SafeEncoder.encodeMany(string)); } - public void lpush(final String key, final String string) { - lpush(SafeEncoder.encode(key), SafeEncoder.encode(string)); + public void lpush(final String key, final String... string) { + lpush(SafeEncoder.encode(key), SafeEncoder.encodeMany(string)); } public void llen(final String key) { - llen(SafeEncoder.encode(key)); + llen(SafeEncoder.encode(key)); } public void lrange(final String key, final long start, final long end) { - lrange(SafeEncoder.encode(key), start, end); + lrange(SafeEncoder.encode(key), start, end); } public void ltrim(final String key, final long start, final long end) { - ltrim(SafeEncoder.encode(key), start, end); + ltrim(SafeEncoder.encode(key), start, end); } public void lindex(final String key, final long index) { - lindex(SafeEncoder.encode(key), index); + lindex(SafeEncoder.encode(key), index); } public void lset(final String key, final long index, final String value) { - lset(SafeEncoder.encode(key), index, SafeEncoder.encode(value)); + lset(SafeEncoder.encode(key), index, SafeEncoder.encode(value)); } public void lrem(final String key, long count, final String value) { - lrem(SafeEncoder.encode(key), count, SafeEncoder.encode(value)); + lrem(SafeEncoder.encode(key), count, SafeEncoder.encode(value)); } public void lpop(final String key) { - lpop(SafeEncoder.encode(key)); + lpop(SafeEncoder.encode(key)); } public void rpop(final String key) { - rpop(SafeEncoder.encode(key)); + rpop(SafeEncoder.encode(key)); } public void rpoplpush(final String srckey, final String dstkey) { - rpoplpush(SafeEncoder.encode(srckey), SafeEncoder.encode(dstkey)); + rpoplpush(SafeEncoder.encode(srckey), SafeEncoder.encode(dstkey)); } - public void sadd(final String key, final String member) { - sadd(SafeEncoder.encode(key), SafeEncoder.encode(member)); + public void sadd(final String key, final String... members) { + sadd(SafeEncoder.encode(key), SafeEncoder.encodeMany(members)); } public void smembers(final String key) { - smembers(SafeEncoder.encode(key)); + smembers(SafeEncoder.encode(key)); } - public void srem(final String key, final String member) { - srem(SafeEncoder.encode(key), SafeEncoder.encode(member)); + public void srem(final String key, final String... members) { + srem(SafeEncoder.encode(key), SafeEncoder.encodeMany(members)); } public void spop(final String key) { - spop(SafeEncoder.encode(key)); + spop(SafeEncoder.encode(key)); } public void smove(final String srckey, final String dstkey, - final String member) { - smove(SafeEncoder.encode(srckey), SafeEncoder.encode(dstkey), - SafeEncoder.encode(member)); + final String member) { + smove(SafeEncoder.encode(srckey), SafeEncoder.encode(dstkey), + SafeEncoder.encode(member)); } public void scard(final String key) { - scard(SafeEncoder.encode(key)); + scard(SafeEncoder.encode(key)); } public void sismember(final String key, final String member) { - sismember(SafeEncoder.encode(key), SafeEncoder.encode(member)); + sismember(SafeEncoder.encode(key), SafeEncoder.encode(member)); } public void sinter(final String... keys) { - final byte[][] bkeys = new byte[keys.length][]; - for (int i = 0; i < bkeys.length; i++) { - bkeys[i] = SafeEncoder.encode(keys[i]); - } - sinter(bkeys); + final byte[][] bkeys = new byte[keys.length][]; + for (int i = 0; i < bkeys.length; i++) { + bkeys[i] = SafeEncoder.encode(keys[i]); + } + sinter(bkeys); } public void sinterstore(final String dstkey, final String... keys) { - final byte[][] bkeys = new byte[keys.length][]; - for (int i = 0; i < bkeys.length; i++) { - bkeys[i] = SafeEncoder.encode(keys[i]); - } - sinterstore(SafeEncoder.encode(dstkey), bkeys); + final byte[][] bkeys = new byte[keys.length][]; + for (int i = 0; i < bkeys.length; i++) { + bkeys[i] = SafeEncoder.encode(keys[i]); + } + sinterstore(SafeEncoder.encode(dstkey), bkeys); } public void sunion(final String... keys) { - final byte[][] bkeys = new byte[keys.length][]; - for (int i = 0; i < bkeys.length; i++) { - bkeys[i] = SafeEncoder.encode(keys[i]); - } - sunion(bkeys); + final byte[][] bkeys = new byte[keys.length][]; + for (int i = 0; i < bkeys.length; i++) { + bkeys[i] = SafeEncoder.encode(keys[i]); + } + sunion(bkeys); } public void sunionstore(final String dstkey, final String... keys) { - final byte[][] bkeys = new byte[keys.length][]; - for (int i = 0; i < bkeys.length; i++) { - bkeys[i] = SafeEncoder.encode(keys[i]); - } - sunionstore(SafeEncoder.encode(dstkey), bkeys); + final byte[][] bkeys = new byte[keys.length][]; + for (int i = 0; i < bkeys.length; i++) { + bkeys[i] = SafeEncoder.encode(keys[i]); + } + sunionstore(SafeEncoder.encode(dstkey), bkeys); } public void sdiff(final String... keys) { - final byte[][] bkeys = new byte[keys.length][]; - for (int i = 0; i < bkeys.length; i++) { - bkeys[i] = SafeEncoder.encode(keys[i]); - } - sdiff(bkeys); + final byte[][] bkeys = new byte[keys.length][]; + for (int i = 0; i < bkeys.length; i++) { + bkeys[i] = SafeEncoder.encode(keys[i]); + } + sdiff(bkeys); } public void sdiffstore(final String dstkey, final String... keys) { - final byte[][] bkeys = new byte[keys.length][]; - for (int i = 0; i < bkeys.length; i++) { - bkeys[i] = SafeEncoder.encode(keys[i]); - } - sdiffstore(SafeEncoder.encode(dstkey), bkeys); + final byte[][] bkeys = new byte[keys.length][]; + for (int i = 0; i < bkeys.length; i++) { + bkeys[i] = SafeEncoder.encode(keys[i]); + } + sdiffstore(SafeEncoder.encode(dstkey), bkeys); } public void srandmember(final String key) { - srandmember(SafeEncoder.encode(key)); + srandmember(SafeEncoder.encode(key)); } public void zadd(final String key, final double score, final String member) { - zadd(SafeEncoder.encode(key), score, SafeEncoder.encode(member)); + zadd(SafeEncoder.encode(key), score, SafeEncoder.encode(member)); } - public void zrange(final String key, final int start, final int end) { - zrange(SafeEncoder.encode(key), start, end); + public void zrange(final String key, final long start, final long end) { + zrange(SafeEncoder.encode(key), start, end); } - public void zrem(final String key, final String member) { - zrem(SafeEncoder.encode(key), SafeEncoder.encode(member)); + public void zrem(final String key, final String... members) { + zrem(SafeEncoder.encode(key), SafeEncoder.encodeMany(members)); } public void zincrby(final String key, final double score, - final String member) { - zincrby(SafeEncoder.encode(key), score, SafeEncoder.encode(member)); + final String member) { + zincrby(SafeEncoder.encode(key), score, SafeEncoder.encode(member)); } public void zrank(final String key, final String member) { - zrank(SafeEncoder.encode(key), SafeEncoder.encode(member)); + zrank(SafeEncoder.encode(key), SafeEncoder.encode(member)); } public void zrevrank(final String key, final String member) { - zrevrank(SafeEncoder.encode(key), SafeEncoder.encode(member)); + zrevrank(SafeEncoder.encode(key), SafeEncoder.encode(member)); } - public void zrevrange(final String key, final int start, final int end) { - zrevrange(SafeEncoder.encode(key), start, end); + public void zrevrange(final String key, final long start, final long end) { + zrevrange(SafeEncoder.encode(key), start, end); } - public void zrangeWithScores(final String key, final int start, - final int end) { - zrangeWithScores(SafeEncoder.encode(key), start, end); + public void zrangeWithScores(final String key, final long start, + final long end) { + zrangeWithScores(SafeEncoder.encode(key), start, end); } - public void zrevrangeWithScores(final String key, final int start, - final int end) { - zrevrangeWithScores(SafeEncoder.encode(key), start, end); + public void zrevrangeWithScores(final String key, final long start, + final long end) { + zrevrangeWithScores(SafeEncoder.encode(key), start, end); } public void zcard(final String key) { - zcard(SafeEncoder.encode(key)); + zcard(SafeEncoder.encode(key)); } public void zscore(final String key, final String member) { - zscore(SafeEncoder.encode(key), SafeEncoder.encode(member)); + zscore(SafeEncoder.encode(key), SafeEncoder.encode(member)); } public void watch(final String... keys) { - final byte[][] bargs = new byte[keys.length][]; - for (int i = 0; i < bargs.length; i++) { - bargs[i] = SafeEncoder.encode(keys[i]); - } - watch(bargs); + final byte[][] bargs = new byte[keys.length][]; + for (int i = 0; i < bargs.length; i++) { + bargs[i] = SafeEncoder.encode(keys[i]); + } + watch(bargs); } public void sort(final String key) { - sort(SafeEncoder.encode(key)); + sort(SafeEncoder.encode(key)); } public void sort(final String key, final SortingParams sortingParameters) { - sort(SafeEncoder.encode(key), sortingParameters); + sort(SafeEncoder.encode(key), sortingParameters); } public void blpop(final String[] args) { - final byte[][] bargs = new byte[args.length][]; - for (int i = 0; i < bargs.length; i++) { - bargs[i] = SafeEncoder.encode(args[i]); - } - blpop(bargs); + final byte[][] bargs = new byte[args.length][]; + for (int i = 0; i < bargs.length; i++) { + bargs[i] = SafeEncoder.encode(args[i]); + } + blpop(bargs); + } + + public void blpop(final int timeout, final String... keys) { + List args = new ArrayList(); + for (String arg : keys) { + args.add(arg); + } + args.add(String.valueOf(timeout)); + blpop(args.toArray(new String[args.size()])); } public void sort(final String key, final SortingParams sortingParameters, - final String dstkey) { - sort(SafeEncoder.encode(key), sortingParameters, SafeEncoder - .encode(dstkey)); + final String dstkey) { + sort(SafeEncoder.encode(key), sortingParameters, + SafeEncoder.encode(dstkey)); } public void sort(final String key, final String dstkey) { - sort(SafeEncoder.encode(key), SafeEncoder.encode(dstkey)); + sort(SafeEncoder.encode(key), SafeEncoder.encode(dstkey)); } public void brpop(final String[] args) { - final byte[][] bargs = new byte[args.length][]; - for (int i = 0; i < bargs.length; i++) { - bargs[i] = SafeEncoder.encode(args[i]); - } - brpop(bargs); + final byte[][] bargs = new byte[args.length][]; + for (int i = 0; i < bargs.length; i++) { + bargs[i] = SafeEncoder.encode(args[i]); + } + brpop(bargs); + } + + public void brpop(final int timeout, final String... keys) { + List args = new ArrayList(); + for (String arg : keys) { + args.add(arg); + } + args.add(String.valueOf(timeout)); + brpop(args.toArray(new String[args.size()])); } public void zcount(final String key, final double min, final double max) { - zcount(SafeEncoder.encode(key), min, max); + zcount(SafeEncoder.encode(key), toByteArray(min), toByteArray(max)); + } + + public void zcount(final String key, final String min, final String max) { + zcount(SafeEncoder.encode(key), SafeEncoder.encode(min), + SafeEncoder.encode(max)); } public void zrangeByScore(final String key, final double min, - final double max) { - zrangeByScore(SafeEncoder.encode(key), min, max); + final double max) { + zrangeByScore(SafeEncoder.encode(key), toByteArray(min), + toByteArray(max)); } public void zrangeByScore(final String key, final String min, - final String max) { - zrangeByScore(SafeEncoder.encode(key), SafeEncoder.encode(min), - SafeEncoder.encode(max)); + final String max) { + zrangeByScore(SafeEncoder.encode(key), SafeEncoder.encode(min), + SafeEncoder.encode(max)); } public void zrangeByScore(final String key, final double min, - final double max, final int offset, int count) { - zrangeByScore(SafeEncoder.encode(key), min, max, offset, count); + final double max, final int offset, int count) { + zrangeByScore(SafeEncoder.encode(key), toByteArray(min), + toByteArray(max), offset, count); } public void zrangeByScoreWithScores(final String key, final double min, - final double max) { - zrangeByScoreWithScores(SafeEncoder.encode(key), min, max); + final double max) { + zrangeByScoreWithScores(SafeEncoder.encode(key), toByteArray(min), + toByteArray(max)); } public void zrangeByScoreWithScores(final String key, final double min, - final double max, final int offset, final int count) { - zrangeByScoreWithScores(SafeEncoder.encode(key), min, max, offset, - count); + final double max, final int offset, final int count) { + zrangeByScoreWithScores(SafeEncoder.encode(key), toByteArray(min), + toByteArray(max), offset, count); } public void zrevrangeByScore(final String key, final double max, - final double min) { - zrevrangeByScore(SafeEncoder.encode(key), max, min); + final double min) { + zrevrangeByScore(SafeEncoder.encode(key), toByteArray(max), + toByteArray(min)); + } + + public void zrangeByScore(final String key, final String min, + final String max, final int offset, int count) { + zrangeByScore(SafeEncoder.encode(key), SafeEncoder.encode(min), + SafeEncoder.encode(max), offset, count); + } + + public void zrangeByScoreWithScores(final String key, final String min, + final String max) { + zrangeByScoreWithScores(SafeEncoder.encode(key), + SafeEncoder.encode(min), SafeEncoder.encode(max)); + } + + public void zrangeByScoreWithScores(final String key, final String min, + final String max, final int offset, final int count) { + zrangeByScoreWithScores(SafeEncoder.encode(key), + SafeEncoder.encode(min), SafeEncoder.encode(max), offset, count); } public void zrevrangeByScore(final String key, final String max, - final String min) { - zrevrangeByScore(SafeEncoder.encode(key), SafeEncoder.encode(max), - SafeEncoder.encode(min)); + final String min) { + zrevrangeByScore(SafeEncoder.encode(key), SafeEncoder.encode(max), + SafeEncoder.encode(min)); } public void zrevrangeByScore(final String key, final double max, - final double min, final int offset, int count) { - zrevrangeByScore(SafeEncoder.encode(key), max, min, offset, count); + final double min, final int offset, int count) { + zrevrangeByScore(SafeEncoder.encode(key), toByteArray(max), + toByteArray(min), offset, count); + } + + public void zrevrangeByScore(final String key, final String max, + final String min, final int offset, int count) { + zrevrangeByScore(SafeEncoder.encode(key), SafeEncoder.encode(max), + SafeEncoder.encode(min), offset, count); } public void zrevrangeByScoreWithScores(final String key, final double max, - final double min) { - zrevrangeByScoreWithScores(SafeEncoder.encode(key), max, min); + final double min) { + zrevrangeByScoreWithScores(SafeEncoder.encode(key), toByteArray(max), + toByteArray(min)); + } + + public void zrevrangeByScoreWithScores(final String key, final String max, + final String min) { + zrevrangeByScoreWithScores(SafeEncoder.encode(key), + SafeEncoder.encode(max), SafeEncoder.encode(min)); } public void zrevrangeByScoreWithScores(final String key, final double max, - final double min, final int offset, final int count) { - zrevrangeByScoreWithScores(SafeEncoder.encode(key), max, min, offset, - count); + final double min, final int offset, final int count) { + zrevrangeByScoreWithScores(SafeEncoder.encode(key), toByteArray(max), + toByteArray(min), offset, count); } - public void zremrangeByRank(final String key, final int start, final int end) { - zremrangeByRank(SafeEncoder.encode(key), start, end); + public void zrevrangeByScoreWithScores(final String key, final String max, + final String min, final int offset, final int count) { + zrevrangeByScoreWithScores(SafeEncoder.encode(key), + SafeEncoder.encode(max), SafeEncoder.encode(min), offset, count); + } + + public void zremrangeByRank(final String key, final long start, + final long end) { + zremrangeByRank(SafeEncoder.encode(key), start, end); } public void zremrangeByScore(final String key, final double start, - final double end) { - zremrangeByScore(SafeEncoder.encode(key), start, end); + final double end) { + zremrangeByScore(SafeEncoder.encode(key), toByteArray(start), + toByteArray(end)); + } + + public void zremrangeByScore(final String key, final String start, + final String end) { + zremrangeByScore(SafeEncoder.encode(key), SafeEncoder.encode(start), + SafeEncoder.encode(end)); } public void zunionstore(final String dstkey, final String... sets) { - final byte[][] bsets = new byte[sets.length][]; - for (int i = 0; i < bsets.length; i++) { - bsets[i] = SafeEncoder.encode(sets[i]); - } - zunionstore(SafeEncoder.encode(dstkey), bsets); + final byte[][] bsets = new byte[sets.length][]; + for (int i = 0; i < bsets.length; i++) { + bsets[i] = SafeEncoder.encode(sets[i]); + } + zunionstore(SafeEncoder.encode(dstkey), bsets); } public void zunionstore(final String dstkey, final ZParams params, - final String... sets) { - final byte[][] bsets = new byte[sets.length][]; - for (int i = 0; i < bsets.length; i++) { - bsets[i] = SafeEncoder.encode(sets[i]); - } - zunionstore(SafeEncoder.encode(dstkey), params, bsets); + final String... sets) { + final byte[][] bsets = new byte[sets.length][]; + for (int i = 0; i < bsets.length; i++) { + bsets[i] = SafeEncoder.encode(sets[i]); + } + zunionstore(SafeEncoder.encode(dstkey), params, bsets); } public void zinterstore(final String dstkey, final String... sets) { - final byte[][] bsets = new byte[sets.length][]; - for (int i = 0; i < bsets.length; i++) { - bsets[i] = SafeEncoder.encode(sets[i]); - } - zinterstore(SafeEncoder.encode(dstkey), bsets); + final byte[][] bsets = new byte[sets.length][]; + for (int i = 0; i < bsets.length; i++) { + bsets[i] = SafeEncoder.encode(sets[i]); + } + zinterstore(SafeEncoder.encode(dstkey), bsets); } public void zinterstore(final String dstkey, final ZParams params, - final String... sets) { - final byte[][] bsets = new byte[sets.length][]; - for (int i = 0; i < bsets.length; i++) { - bsets[i] = SafeEncoder.encode(sets[i]); - } - zinterstore(SafeEncoder.encode(dstkey), params, bsets); + final String... sets) { + final byte[][] bsets = new byte[sets.length][]; + for (int i = 0; i < bsets.length; i++) { + bsets[i] = SafeEncoder.encode(sets[i]); + } + zinterstore(SafeEncoder.encode(dstkey), params, bsets); } public void strlen(final String key) { - strlen(SafeEncoder.encode(key)); + strlen(SafeEncoder.encode(key)); } - public void lpushx(final String key, final String string) { - lpushx(SafeEncoder.encode(key), SafeEncoder.encode(string)); + public void lpushx(final String key, final String... string) { + lpushx(SafeEncoder.encode(key), getByteParams(string)); } public void persist(final String key) { - persist(SafeEncoder.encode(key)); + persist(SafeEncoder.encode(key)); } - public void rpushx(final String key, final String string) { - rpushx(SafeEncoder.encode(key), SafeEncoder.encode(string)); + public void rpushx(final String key, final String... string) { + rpushx(SafeEncoder.encode(key), getByteParams(string)); } public void echo(final String string) { - echo(SafeEncoder.encode(string)); + echo(SafeEncoder.encode(string)); } public void linsert(final String key, final LIST_POSITION where, - final String pivot, final String value) { - linsert(SafeEncoder.encode(key), where, SafeEncoder.encode(pivot), - SafeEncoder.encode(value)); + final String pivot, final String value) { + linsert(SafeEncoder.encode(key), where, SafeEncoder.encode(pivot), + SafeEncoder.encode(value)); } public void brpoplpush(String source, String destination, int timeout) { - brpoplpush(SafeEncoder.encode(source), SafeEncoder.encode(destination), - timeout); + brpoplpush(SafeEncoder.encode(source), SafeEncoder.encode(destination), + timeout); } public void setbit(final String key, final long offset, final boolean value) { - setbit(SafeEncoder.encode(key), offset, toByteArray(value ? 1 : 0)); + setbit(SafeEncoder.encode(key), offset, value); + } + + public void setbit(final String key, final long offset, final String value) { + setbit(SafeEncoder.encode(key), offset, SafeEncoder.encode(value)); } public void getbit(String key, long offset) { - getbit(SafeEncoder.encode(key), offset); + getbit(SafeEncoder.encode(key), offset); } public void setrange(String key, long offset, String value) { - setrange(SafeEncoder.encode(key), offset, SafeEncoder.encode(value)); + setrange(SafeEncoder.encode(key), offset, SafeEncoder.encode(value)); } public void getrange(String key, long startOffset, long endOffset) { - getrange(SafeEncoder.encode(key), startOffset, endOffset); + getrange(SafeEncoder.encode(key), startOffset, endOffset); } public void publish(final String channel, final String message) { - publish(SafeEncoder.encode(channel), SafeEncoder.encode(message)); + publish(SafeEncoder.encode(channel), SafeEncoder.encode(message)); } public void unsubscribe(final String... channels) { - final byte[][] cs = new byte[channels.length][]; - for (int i = 0; i < cs.length; i++) { - cs[i] = SafeEncoder.encode(channels[i]); - } - unsubscribe(cs); + final byte[][] cs = new byte[channels.length][]; + for (int i = 0; i < cs.length; i++) { + cs[i] = SafeEncoder.encode(channels[i]); + } + unsubscribe(cs); } public void psubscribe(final String... patterns) { - final byte[][] ps = new byte[patterns.length][]; - for (int i = 0; i < ps.length; i++) { - ps[i] = SafeEncoder.encode(patterns[i]); - } - psubscribe(ps); + final byte[][] ps = new byte[patterns.length][]; + for (int i = 0; i < ps.length; i++) { + ps[i] = SafeEncoder.encode(patterns[i]); + } + psubscribe(ps); } public void punsubscribe(final String... patterns) { - final byte[][] ps = new byte[patterns.length][]; - for (int i = 0; i < ps.length; i++) { - ps[i] = SafeEncoder.encode(patterns[i]); - } - punsubscribe(ps); + final byte[][] ps = new byte[patterns.length][]; + for (int i = 0; i < ps.length; i++) { + ps[i] = SafeEncoder.encode(patterns[i]); + } + punsubscribe(ps); } public void subscribe(final String... channels) { - final byte[][] cs = new byte[channels.length][]; - for (int i = 0; i < cs.length; i++) { - cs[i] = SafeEncoder.encode(channels[i]); - } - subscribe(cs); + final byte[][] cs = new byte[channels.length][]; + for (int i = 0; i < cs.length; i++) { + cs[i] = SafeEncoder.encode(channels[i]); + } + subscribe(cs); + } + + public void configSet(String parameter, String value) { + configSet(SafeEncoder.encode(parameter), SafeEncoder.encode(value)); + } + + public void configGet(String pattern) { + configGet(SafeEncoder.encode(pattern)); + } + + private byte[][] getByteParams(String... params) { + byte[][] p = new byte[params.length][]; + for (int i = 0; i < params.length; i++) + p[i] = SafeEncoder.encode(params[i]); + + return p; + } + + public void eval(String script, int keyCount, String... params) { + eval(SafeEncoder.encode(script), toByteArray(keyCount), + getByteParams(params)); + } + + public void evalsha(String sha1, int keyCount, String... params) { + evalsha(SafeEncoder.encode(sha1), toByteArray(keyCount), + getByteParams(params)); + } + + public void scriptExists(String... sha1) { + final byte[][] bsha1 = new byte[sha1.length][]; + for (int i = 0; i < bsha1.length; i++) { + bsha1[i] = SafeEncoder.encode(sha1[i]); + } + scriptExists(bsha1); + } + + public void scriptLoad(String script) { + scriptLoad(SafeEncoder.encode(script)); + } + + public void zadd(String key, Map scoreMembers) { + HashMap binaryScoreMembers = new HashMap(); + + for (Map.Entry entry : scoreMembers.entrySet()) { + binaryScoreMembers.put(entry.getKey(), + SafeEncoder.encode(entry.getValue())); + } + + zaddBinary(SafeEncoder.encode(key), binaryScoreMembers); + } + + public void objectRefcount(String key) { + objectRefcount(SafeEncoder.encode(key)); + } + + public void objectIdletime(String key) { + objectIdletime(SafeEncoder.encode(key)); + } + + public void objectEncoding(String key) { + objectEncoding(SafeEncoder.encode(key)); + } + + public void bitcount(final String key) { + bitcount(SafeEncoder.encode(key)); + } + + public void bitcount(final String key, long start, long end) { + bitcount(SafeEncoder.encode(key), start, end); + } + + public void bitop(BitOP op, final String destKey, String... srcKeys) { + bitop(op, SafeEncoder.encode(destKey), getByteParams(srcKeys)); + } + + public void sentinel(final String... args) { + final byte[][] arg = new byte[args.length][]; + for (int i = 0; i < arg.length; i++) { + arg[i] = SafeEncoder.encode(args[i]); + } + sentinel(arg); + } + + public void sentinel(final String cmd, String arg1, int arg2) { + sentinel(SafeEncoder.encode(cmd), SafeEncoder.encode(arg1), + toByteArray(arg2)); + } + + public void dump(final String key) { + dump(SafeEncoder.encode(key)); + } + + public void restore(final String key, final int ttl, final byte[] serializedValue) { + restore(SafeEncoder.encode(key), ttl, serializedValue); + } + + public void pexpire(final String key, final int milliseconds) { + pexpire(SafeEncoder.encode(key), milliseconds); + } + + public void pexpireAt(final String key, final long millisecondsTimestamp) { + pexpireAt(SafeEncoder.encode(key), millisecondsTimestamp); + } + + public void pttl(final String key) { + pttl(SafeEncoder.encode(key)); + } + + public void incrByFloat(final String key, final double increment) { + incrByFloat(SafeEncoder.encode(key), increment); + } + + public void psetex(final String key, final int milliseconds, final String value) { + psetex(SafeEncoder.encode(key), milliseconds, SafeEncoder.encode(value)); + } + + public void set(final String key, final String value, final String nxxx) { + set(SafeEncoder.encode(key), SafeEncoder.encode(value), SafeEncoder.encode(nxxx)); + } + + public void set(final String key, final String value, final String nxxx, final String expx, final int time) { + set(SafeEncoder.encode(key), SafeEncoder.encode(value), SafeEncoder.encode(nxxx), SafeEncoder.encode(expx), time); + } + + public void srandmember(final String key, final int count) { + srandmember(SafeEncoder.encode(key), count); + } + + public void clientKill(final String client) { + clientKill(SafeEncoder.encode(client)); + } + + public void clientSetname(final String name) { + clientSetname(SafeEncoder.encode(name)); + } + + public void migrate(final String host, final int port, final String key, final int destinationDb, final int timeout) { + migrate(SafeEncoder.encode(host), port, SafeEncoder.encode(key), destinationDb, timeout); + } + + public void hincrByFloat(final String key, final String field, double increment) { + hincrByFloat(SafeEncoder.encode(key), SafeEncoder.encode(field), increment); } } diff --git a/src/main/java/redis/clients/jedis/Commands.java b/src/main/java/redis/clients/jedis/Commands.java index 235ee88..7d0e60d 100644 --- a/src/main/java/redis/clients/jedis/Commands.java +++ b/src/main/java/redis/clients/jedis/Commands.java @@ -30,6 +30,8 @@ public interface Commands { public void setbit(String key, long offset, boolean value); + public void setbit(String key, long offset, String value); + public void getbit(String key, long offset); public void setrange(String key, long offset, String value); @@ -76,7 +78,7 @@ public interface Commands { public void hexists(final String key, final String field); - public void hdel(final String key, final String field); + public void hdel(final String key, final String... fields); public void hlen(final String key); @@ -86,9 +88,9 @@ public interface Commands { public void hgetAll(final String key); - public void rpush(final String key, final String string); + public void rpush(final String key, final String... strings); - public void lpush(final String key, final String string); + public void lpush(final String key, final String... strings); public void llen(final String key); @@ -108,16 +110,16 @@ public interface Commands { public void rpoplpush(final String srckey, final String dstkey); - public void sadd(final String key, final String member); + public void sadd(final String key, final String... members); public void smembers(final String key); - public void srem(final String key, final String member); + public void srem(final String key, final String... member); public void spop(final String key); public void smove(final String srckey, final String dstkey, - final String member); + final String member); public void scard(final String key); @@ -139,24 +141,26 @@ public interface Commands { public void zadd(final String key, final double score, final String member); - public void zrange(final String key, final int start, final int end); + public void zadd(final String key, final Map scoreMembers); - public void zrem(final String key, final String member); + public void zrange(final String key, final long start, final long end); + + public void zrem(final String key, final String... members); public void zincrby(final String key, final double score, - final String member); + final String member); public void zrank(final String key, final String member); public void zrevrank(final String key, final String member); - public void zrevrange(final String key, final int start, final int end); + public void zrevrange(final String key, final long start, final long end); - public void zrangeWithScores(final String key, final int start, - final int end); + public void zrangeWithScores(final String key, final long start, + final long end); - public void zrevrangeWithScores(final String key, final int start, - final int end); + public void zrevrangeWithScores(final String key, final long start, + final long end); public void zcard(final String key); @@ -171,74 +175,92 @@ public interface Commands { public void blpop(final String[] args); public void sort(final String key, final SortingParams sortingParameters, - final String dstkey); + final String dstkey); public void sort(final String key, final String dstkey); public void brpop(final String[] args); public void brpoplpush(final String source, final String destination, - final int timeout); + final int timeout); public void zcount(final String key, final double min, final double max); + public void zcount(final String key, final String min, final String max); + public void zrangeByScore(final String key, final double min, - final double max); + final double max); public void zrangeByScore(final String key, final String min, - final String max); + final String max); public void zrangeByScore(final String key, final double min, - final double max, final int offset, int count); + final double max, final int offset, int count); public void zrangeByScoreWithScores(final String key, final double min, - final double max); + final double max); public void zrangeByScoreWithScores(final String key, final double min, - final double max, final int offset, final int count); + final double max, final int offset, final int count); + + public void zrangeByScoreWithScores(final String key, final String min, + final String max); + + public void zrangeByScoreWithScores(final String key, final String min, + final String max, final int offset, final int count); public void zrevrangeByScore(final String key, final double max, - final double min); + final double min); public void zrevrangeByScore(final String key, final String max, - final String min); + final String min); public void zrevrangeByScore(final String key, final double max, - final double min, final int offset, int count); + final double min, final int offset, int count); public void zrevrangeByScoreWithScores(final String key, final double max, - final double min); + final double min); public void zrevrangeByScoreWithScores(final String key, final double max, - final double min, final int offset, final int count); + final double min, final int offset, final int count); - public void zremrangeByRank(final String key, final int start, final int end); + public void zrevrangeByScoreWithScores(final String key, final String max, + final String min); + + public void zrevrangeByScoreWithScores(final String key, final String max, + final String min, final int offset, final int count); + + public void zremrangeByRank(final String key, final long start, + final long end); public void zremrangeByScore(final String key, final double start, - final double end); + final double end); + + public void zremrangeByScore(final String key, final String start, + final String end); public void zunionstore(final String dstkey, final String... sets); public void zunionstore(final String dstkey, final ZParams params, - final String... sets); + final String... sets); public void zinterstore(final String dstkey, final String... sets); public void zinterstore(final String dstkey, final ZParams params, - final String... sets); + final String... sets); public void strlen(final String key); - public void lpushx(final String key, final String string); + public void lpushx(final String key, final String... string); public void persist(final String key); - public void rpushx(final String key, final String string); + public void rpushx(final String key, final String... string); public void echo(final String string); public void linsert(final String key, final LIST_POSITION where, - final String pivot, final String value); + final String pivot, final String value); public void bgrewriteaof(); @@ -259,4 +281,16 @@ public interface Commands { public void exec(); public void discard(); -} \ No newline at end of file + + public void objectRefcount(String key); + + public void objectIdletime(String key); + + public void objectEncoding(String key); + + public void bitcount(final String key); + + public void bitcount(final String key, long start, long end); + + public void bitop(BitOP op, final String destKey, String... srcKeys); +} diff --git a/src/main/java/redis/clients/jedis/Connection.java b/src/main/java/redis/clients/jedis/Connection.java index be5e664..1c42b94 100644 --- a/src/main/java/redis/clients/jedis/Connection.java +++ b/src/main/java/redis/clients/jedis/Connection.java @@ -9,6 +9,7 @@ import java.util.List; import redis.clients.jedis.Protocol.Command; import redis.clients.jedis.exceptions.JedisConnectionException; +import redis.clients.jedis.exceptions.JedisDataException; import redis.clients.jedis.exceptions.JedisException; import redis.clients.util.RedisInputStream; import redis.clients.util.RedisOutputStream; @@ -18,7 +19,6 @@ public class Connection { private String host; private int port = Protocol.DEFAULT_PORT; private Socket socket; - private Protocol protocol = new Protocol(); private RedisOutputStream outputStream; private RedisInputStream inputStream; private int pipelinedCommands = 0; @@ -38,6 +38,10 @@ public class Connection { public void setTimeoutInfinite() { try { + if(!isConnected()) { + connect(); + } + socket.setKeepAlive(true); socket.setSoTimeout(0); } catch (SocketException ex) { throw new JedisException(ex); @@ -47,6 +51,7 @@ public class Connection { public void rollbackTimeout() { try { socket.setSoTimeout(timeout); + socket.setKeepAlive(false); } catch (SocketException ex) { throw new JedisException(ex); } @@ -75,14 +80,14 @@ public class Connection { protected Connection sendCommand(final Command cmd, final byte[]... args) { connect(); - protocol.sendCommand(outputStream, cmd, args); + Protocol.sendCommand(outputStream, cmd, args); pipelinedCommands++; return this; } - + protected Connection sendCommand(final Command cmd) { connect(); - protocol.sendCommand(outputStream, cmd, new byte[0][]); + Protocol.sendCommand(outputStream, cmd, new byte[0][]); pipelinedCommands++; return this; } @@ -110,12 +115,20 @@ public class Connection { } public Connection() { + } public void connect() { if (!isConnected()) { try { socket = new Socket(); + //->@wjw_add + socket.setReuseAddress(true); + socket.setKeepAlive(true); //Will monitor the TCP connection is valid + socket.setTcpNoDelay(true); //Socket buffer Whetherclosed, to ensure timely delivery of data + socket.setSoLinger(true,0); //Control calls close () method, the underlying socket is closed immediately + //<-@wjw_add + socket.connect(new InetSocketAddress(host, port), timeout); socket.setSoTimeout(timeout); outputStream = new RedisOutputStream(socket.getOutputStream()); @@ -149,7 +162,7 @@ public class Connection { protected String getStatusCodeReply() { flush(); pipelinedCommands--; - final byte[] resp = (byte[]) protocol.read(inputStream); + final byte[] resp = (byte[]) Protocol.read(inputStream); if (null == resp) { return null; } else { @@ -169,13 +182,13 @@ public class Connection { public byte[] getBinaryBulkReply() { flush(); pipelinedCommands--; - return (byte[]) protocol.read(inputStream); + return (byte[]) Protocol.read(inputStream); } public Long getIntegerReply() { flush(); pipelinedCommands--; - return (Long) protocol.read(inputStream); + return (Long) Protocol.read(inputStream); } public List getMultiBulkReply() { @@ -186,14 +199,21 @@ public class Connection { public List getBinaryMultiBulkReply() { flush(); pipelinedCommands--; - return (List) protocol.read(inputStream); + return (List) Protocol.read(inputStream); } @SuppressWarnings("unchecked") public List getObjectMultiBulkReply() { flush(); pipelinedCommands--; - return (List) protocol.read(inputStream); + return (List) Protocol.read(inputStream); + } + + @SuppressWarnings("unchecked") + public List getIntegerMultiBulkReply() { + flush(); + pipelinedCommands--; + return (List) Protocol.read(inputStream); } public List getAll() { @@ -204,7 +224,11 @@ public class Connection { List all = new ArrayList(); flush(); while (pipelinedCommands > except) { - all.add(protocol.read(inputStream)); + try{ + all.add(Protocol.read(inputStream)); + }catch(JedisDataException e){ + all.add(e); + } pipelinedCommands--; } return all; @@ -213,6 +237,6 @@ public class Connection { public Object getOne() { flush(); pipelinedCommands--; - return protocol.read(inputStream); + return Protocol.read(inputStream); } -} \ No newline at end of file +} diff --git a/src/main/java/redis/clients/jedis/Jedis.java b/src/main/java/redis/clients/jedis/Jedis.java index f0e7796..526459f 100644 --- a/src/main/java/redis/clients/jedis/Jedis.java +++ b/src/main/java/redis/clients/jedis/Jedis.java @@ -1,6 +1,8 @@ package redis.clients.jedis; +import java.net.URI; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashSet; @@ -9,28 +11,28 @@ import java.util.Map; import java.util.Set; import redis.clients.jedis.BinaryClient.LIST_POSITION; +import redis.clients.util.SafeEncoder; +import redis.clients.util.Slowlog; -public class Jedis extends BinaryJedis implements JedisCommands { +public class Jedis extends BinaryJedis implements JedisCommands, MultiKeyCommands, AdvancedJedisCommands, ScriptingCommands { public Jedis(final String host) { - super(host); + super(host); } public Jedis(final String host, final int port) { - super(host, port); + super(host, port); } public Jedis(final String host, final int port, final int timeout) { - super(host, port, timeout); + super(host, port, timeout); } public Jedis(JedisShardInfo shardInfo) { - super(shardInfo); + super(shardInfo); } - public String ping() { - checkIsInMulti(); - client.ping(); - return client.getStatusCodeReply(); + public Jedis(URI uri) { + super(uri); } /** @@ -44,9 +46,9 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Status code reply */ public String set(final String key, String value) { - checkIsInMulti(); - client.set(key, value); - return client.getStatusCodeReply(); + checkIsInMulti(); + client.set(key, value); + return client.getStatusCodeReply(); } /** @@ -60,25 +62,15 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Bulk reply */ public String get(final String key) { - checkIsInMulti(); - client.sendCommand(Protocol.Command.GET, key); - return client.getBulkReply(); - } - - /** - * Ask the server to silently close the connection. - */ - - public String quit() { - checkIsInMulti(); - client.quit(); - return client.getStatusCodeReply(); + checkIsInMulti(); + client.sendCommand(Protocol.Command.GET, key); + return client.getBulkReply(); } /** * Test if the specified key exists. The command returns "1" if the key - * exists, otherwise "1" is returned. Note that even keys set with an empty - * string as value will return "0". + * exists, otherwise "0" is returned. Note that even keys set with an empty + * string as value will return "1". * * Time complexity: O(1) * @@ -86,9 +78,9 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Boolean reply, true if the key exists, otherwise false */ public Boolean exists(final String key) { - checkIsInMulti(); - client.exists(key); - return client.getIntegerReply() == 1; + checkIsInMulti(); + client.exists(key); + return client.getIntegerReply() == 1; } /** @@ -102,9 +94,14 @@ public class Jedis extends BinaryJedis implements JedisCommands { * more keys were removed 0 if none of the specified key existed */ public Long del(final String... keys) { - checkIsInMulti(); - client.del(keys); - return client.getIntegerReply(); + checkIsInMulti(); + client.del(keys); + return client.getIntegerReply(); + } + + public Long del(String key) { + client.del(key); + return client.getIntegerReply(); } /** @@ -122,22 +119,9 @@ public class Jedis extends BinaryJedis implements JedisCommands { * contains a Hash value */ public String type(final String key) { - checkIsInMulti(); - client.type(key); - return client.getStatusCodeReply(); - } - - /** - * Delete all the keys of the currently selected DB. This command never - * fails. - * - * @return Status code reply - */ - - public String flushDB() { - checkIsInMulti(); - client.flushDB(); - return client.getStatusCodeReply(); + checkIsInMulti(); + client.type(key); + return client.getStatusCodeReply(); } /** @@ -172,10 +156,10 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Multi bulk reply */ public Set keys(final String pattern) { - checkIsInMulti(); - client.keys(pattern); - return BuilderFactory.STRING_SET - .build(client.getBinaryMultiBulkReply()); + checkIsInMulti(); + client.keys(pattern); + return BuilderFactory.STRING_SET + .build(client.getBinaryMultiBulkReply()); } /** @@ -187,9 +171,9 @@ public class Jedis extends BinaryJedis implements JedisCommands { * empty string is the database is empty */ public String randomKey() { - checkIsInMulti(); - client.randomKey(); - return client.getBulkReply(); + checkIsInMulti(); + client.randomKey(); + return client.getBulkReply(); } /** @@ -204,9 +188,9 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Status code repy */ public String rename(final String oldkey, final String newkey) { - checkIsInMulti(); - client.rename(oldkey, newkey); - return client.getStatusCodeReply(); + checkIsInMulti(); + client.rename(oldkey, newkey); + return client.getStatusCodeReply(); } /** @@ -221,21 +205,9 @@ public class Jedis extends BinaryJedis implements JedisCommands { * target key already exist */ public Long renamenx(final String oldkey, final String newkey) { - checkIsInMulti(); - client.renamenx(oldkey, newkey); - return client.getIntegerReply(); - } - - /** - * Return the number of keys in the currently selected database. - * - * @return Integer reply - */ - - public Long dbSize() { - checkIsInMulti(); - client.dbSize(); - return client.getIntegerReply(); + checkIsInMulti(); + client.renamenx(oldkey, newkey); + return client.getIntegerReply(); } /** @@ -267,9 +239,9 @@ public class Jedis extends BinaryJedis implements JedisCommands { * exist. */ public Long expire(final String key, final int seconds) { - checkIsInMulti(); - client.expire(key, seconds); - return client.getIntegerReply(); + checkIsInMulti(); + client.expire(key, seconds); + return client.getIntegerReply(); } /** @@ -303,9 +275,9 @@ public class Jedis extends BinaryJedis implements JedisCommands { * exist. */ public Long expireAt(final String key, final long unixTime) { - checkIsInMulti(); - client.expireAt(key, unixTime); - return client.getIntegerReply(); + checkIsInMulti(); + client.expireAt(key, unixTime); + return client.getIntegerReply(); } /** @@ -320,23 +292,9 @@ public class Jedis extends BinaryJedis implements JedisCommands { * have an associated expire, -1 is returned. */ public Long ttl(final String key) { - checkIsInMulti(); - client.ttl(key); - return client.getIntegerReply(); - } - - /** - * Select the DB with having the specified zero-based numeric index. For - * default every new client connection is automatically selected to DB 0. - * - * @param index - * @return Status code reply - */ - - public String select(final int index) { - checkIsInMulti(); - client.select(index); - return client.getStatusCodeReply(); + checkIsInMulti(); + client.ttl(key); + return client.getIntegerReply(); } /** @@ -353,22 +311,9 @@ public class Jedis extends BinaryJedis implements JedisCommands { * found in the current DB. */ public Long move(final String key, final int dbIndex) { - checkIsInMulti(); - client.move(key, dbIndex); - return client.getIntegerReply(); - } - - /** - * Delete all the keys of all the existing databases, not just the currently - * selected one. This command never fails. - * - * @return Status code reply - */ - - public String flushAll() { - checkIsInMulti(); - client.flushAll(); - return client.getStatusCodeReply(); + checkIsInMulti(); + client.move(key, dbIndex); + return client.getIntegerReply(); } /** @@ -383,9 +328,9 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Bulk reply */ public String getSet(final String key, final String value) { - checkIsInMulti(); - client.getSet(key, value); - return client.getBulkReply(); + checkIsInMulti(); + client.getSet(key, value); + return client.getBulkReply(); } /** @@ -399,9 +344,9 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Multi bulk reply */ public List mget(final String... keys) { - checkIsInMulti(); - client.mget(keys); - return client.getMultiBulkReply(); + checkIsInMulti(); + client.mget(keys); + return client.getMultiBulkReply(); } /** @@ -417,9 +362,9 @@ public class Jedis extends BinaryJedis implements JedisCommands { * was not set */ public Long setnx(final String key, final String value) { - checkIsInMulti(); - client.setnx(key, value); - return client.getIntegerReply(); + checkIsInMulti(); + client.setnx(key, value); + return client.getIntegerReply(); } /** @@ -435,9 +380,9 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Status code reply */ public String setex(final String key, final int seconds, final String value) { - checkIsInMulti(); - client.setex(key, seconds, value); - return client.getStatusCodeReply(); + checkIsInMulti(); + client.setex(key, seconds, value); + return client.getStatusCodeReply(); } /** @@ -461,9 +406,9 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Status code reply Basically +OK as MSET can't fail */ public String mset(final String... keysvalues) { - checkIsInMulti(); - client.mset(keysvalues); - return client.getStatusCodeReply(); + checkIsInMulti(); + client.mset(keysvalues); + return client.getStatusCodeReply(); } /** @@ -488,9 +433,9 @@ public class Jedis extends BinaryJedis implements JedisCommands { * no key was set (at least one key already existed) */ public Long msetnx(final String... keysvalues) { - checkIsInMulti(); - client.msetnx(keysvalues); - return client.getIntegerReply(); + checkIsInMulti(); + client.msetnx(keysvalues); + return client.getIntegerReply(); } /** @@ -508,7 +453,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * * @see #incr(String) * @see #decr(String) - * @see #incrBy(String, int) + * @see #incrBy(String, long) * * @param key * @param integer @@ -516,9 +461,9 @@ public class Jedis extends BinaryJedis implements JedisCommands { * after the increment. */ public Long decrBy(final String key, final long integer) { - checkIsInMulti(); - client.decrBy(key, integer); - return client.getIntegerReply(); + checkIsInMulti(); + client.decrBy(key, integer); + return client.getIntegerReply(); } /** @@ -536,17 +481,17 @@ public class Jedis extends BinaryJedis implements JedisCommands { * Time complexity: O(1) * * @see #incr(String) - * @see #incrBy(String, int) - * @see #decrBy(String, int) + * @see #incrBy(String, long) + * @see #decrBy(String, long) * * @param key * @return Integer reply, this commands will reply with the new value of key * after the increment. */ public Long decr(final String key) { - checkIsInMulti(); - client.decr(key); - return client.getIntegerReply(); + checkIsInMulti(); + client.decr(key); + return client.getIntegerReply(); } /** @@ -564,7 +509,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * * @see #incr(String) * @see #decr(String) - * @see #decrBy(String, int) + * @see #decrBy(String, long) * * @param key * @param integer @@ -572,9 +517,9 @@ public class Jedis extends BinaryJedis implements JedisCommands { * after the increment. */ public Long incrBy(final String key, final long integer) { - checkIsInMulti(); - client.incrBy(key, integer); - return client.getIntegerReply(); + checkIsInMulti(); + client.incrBy(key, integer); + return client.getIntegerReply(); } /** @@ -591,18 +536,18 @@ public class Jedis extends BinaryJedis implements JedisCommands { *

* Time complexity: O(1) * - * @see #incrBy(String, int) + * @see #incrBy(String, long) * @see #decr(String) - * @see #decrBy(String, int) + * @see #decrBy(String, long) * * @param key * @return Integer reply, this commands will reply with the new value of key * after the increment. */ public Long incr(final String key) { - checkIsInMulti(); - client.incr(key); - return client.getIntegerReply(); + checkIsInMulti(); + client.incr(key); + return client.getIntegerReply(); } /** @@ -622,9 +567,9 @@ public class Jedis extends BinaryJedis implements JedisCommands { * the append operation. */ public Long append(final String key, final String value) { - checkIsInMulti(); - client.append(key, value); - return client.getIntegerReply(); + checkIsInMulti(); + client.append(key, value); + return client.getIntegerReply(); } /** @@ -646,9 +591,9 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Bulk reply */ public String substr(final String key, final int start, final int end) { - checkIsInMulti(); - client.substr(key, start, end); - return client.getBulkReply(); + checkIsInMulti(); + client.substr(key, start, end); + return client.getBulkReply(); } /** @@ -667,9 +612,9 @@ public class Jedis extends BinaryJedis implements JedisCommands { * 1 is returned. */ public Long hset(final String key, final String field, final String value) { - checkIsInMulti(); - client.hset(key, field, value); - return client.getIntegerReply(); + checkIsInMulti(); + client.hset(key, field, value); + return client.getIntegerReply(); } /** @@ -686,9 +631,9 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Bulk reply */ public String hget(final String key, final String field) { - checkIsInMulti(); - client.hget(key, field); - return client.getBulkReply(); + checkIsInMulti(); + client.hget(key, field); + return client.getBulkReply(); } /** @@ -703,9 +648,9 @@ public class Jedis extends BinaryJedis implements JedisCommands { * field is created 1 is returned. */ public Long hsetnx(final String key, final String field, final String value) { - checkIsInMulti(); - client.hsetnx(key, field, value); - return client.getIntegerReply(); + checkIsInMulti(); + client.hsetnx(key, field, value); + return client.getIntegerReply(); } /** @@ -721,9 +666,9 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Return OK or Exception if hash is empty */ public String hmset(final String key, final Map hash) { - checkIsInMulti(); - client.hmset(key, hash); - return client.getStatusCodeReply(); + checkIsInMulti(); + client.hmset(key, hash); + return client.getStatusCodeReply(); } /** @@ -740,9 +685,9 @@ public class Jedis extends BinaryJedis implements JedisCommands { * with the specified fields, in the same order of the request. */ public List hmget(final String key, final String... fields) { - checkIsInMulti(); - client.hmget(key, fields); - return client.getMultiBulkReply(); + checkIsInMulti(); + client.hmget(key, fields); + return client.getMultiBulkReply(); } /** @@ -764,9 +709,9 @@ public class Jedis extends BinaryJedis implements JedisCommands { * operation. */ public Long hincrBy(final String key, final String field, final long value) { - checkIsInMulti(); - client.hincrBy(key, field, value); - return client.getIntegerReply(); + checkIsInMulti(); + client.hincrBy(key, field, value); + return client.getIntegerReply(); } /** @@ -780,9 +725,9 @@ public class Jedis extends BinaryJedis implements JedisCommands { * Return 0 if the key is not found or the field is not present. */ public Boolean hexists(final String key, final String field) { - checkIsInMulti(); - client.hexists(key, field); - return client.getIntegerReply() == 1; + checkIsInMulti(); + client.hexists(key, field); + return client.getIntegerReply() == 1; } /** @@ -791,14 +736,14 @@ public class Jedis extends BinaryJedis implements JedisCommands { * Time complexity: O(1) * * @param key - * @param field + * @param fields * @return If the field was present in the hash it is deleted and 1 is * returned, otherwise 0 is returned and no operation is performed. */ - public Long hdel(final String key, final String field) { - checkIsInMulti(); - client.hdel(key, field); - return client.getIntegerReply(); + public Long hdel(final String key, final String... fields) { + checkIsInMulti(); + client.hdel(key, fields); + return client.getIntegerReply(); } /** @@ -812,9 +757,9 @@ public class Jedis extends BinaryJedis implements JedisCommands { * an empty hash. */ public Long hlen(final String key) { - checkIsInMulti(); - client.hlen(key); - return client.getIntegerReply(); + checkIsInMulti(); + client.hlen(key); + return client.getIntegerReply(); } /** @@ -826,10 +771,10 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return All the fields names contained into a hash. */ public Set hkeys(final String key) { - checkIsInMulti(); - client.hkeys(key); - return BuilderFactory.STRING_SET - .build(client.getBinaryMultiBulkReply()); + checkIsInMulti(); + client.hkeys(key); + return BuilderFactory.STRING_SET + .build(client.getBinaryMultiBulkReply()); } /** @@ -841,10 +786,10 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return All the fields values contained into a hash. */ public List hvals(final String key) { - checkIsInMulti(); - client.hvals(key); - final List lresult = client.getMultiBulkReply(); - return lresult; + checkIsInMulti(); + client.hvals(key); + final List lresult = client.getMultiBulkReply(); + return lresult; } /** @@ -856,10 +801,10 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return All the fields and values contained into a hash. */ public Map hgetAll(final String key) { - checkIsInMulti(); - client.hgetAll(key); - return BuilderFactory.STRING_MAP - .build(client.getBinaryMultiBulkReply()); + checkIsInMulti(); + client.hgetAll(key); + return BuilderFactory.STRING_MAP + .build(client.getBinaryMultiBulkReply()); } /** @@ -870,17 +815,15 @@ public class Jedis extends BinaryJedis implements JedisCommands { *

* Time complexity: O(1) * - * @see Jedis#lpush(String, String) - * * @param key - * @param string + * @param strings * @return Integer reply, specifically, the number of elements inside the * list after the push operation. */ - public Long rpush(final String key, final String string) { - checkIsInMulti(); - client.rpush(key, string); - return client.getIntegerReply(); + public Long rpush(final String key, final String... strings) { + checkIsInMulti(); + client.rpush(key, strings); + return client.getIntegerReply(); } /** @@ -891,17 +834,15 @@ public class Jedis extends BinaryJedis implements JedisCommands { *

* Time complexity: O(1) * - * @see Jedis#rpush(String, String) - * * @param key - * @param string + * @param strings * @return Integer reply, specifically, the number of elements inside the * list after the push operation. */ - public Long lpush(final String key, final String string) { - checkIsInMulti(); - client.lpush(key, string); - return client.getIntegerReply(); + public Long lpush(final String key, final String... strings) { + checkIsInMulti(); + client.lpush(key, strings); + return client.getIntegerReply(); } /** @@ -915,9 +856,9 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return The length of the list. */ public Long llen(final String key) { - checkIsInMulti(); - client.llen(key); - return client.getIntegerReply(); + checkIsInMulti(); + client.llen(key); + return client.getIntegerReply(); } /** @@ -958,10 +899,11 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Multi bulk reply, specifically a list of elements in the * specified range. */ - public List lrange(final String key, final long start, final long end) { - checkIsInMulti(); - client.lrange(key, start, end); - return client.getMultiBulkReply(); + public List lrange(final String key, final long start, + final long end) { + checkIsInMulti(); + client.lrange(key, start, end); + return client.getMultiBulkReply(); } /** @@ -999,9 +941,9 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Status code reply */ public String ltrim(final String key, final long start, final long end) { - checkIsInMulti(); - client.ltrim(key, start, end); - return client.getStatusCodeReply(); + checkIsInMulti(); + client.ltrim(key, start, end); + return client.getStatusCodeReply(); } /** @@ -1023,9 +965,9 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Bulk reply, specifically the requested element */ public String lindex(final String key, final long index) { - checkIsInMulti(); - client.lindex(key, index); - return client.getBulkReply(); + checkIsInMulti(); + client.lindex(key, index); + return client.getBulkReply(); } /** @@ -1042,7 +984,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * O(N) (with N being the length of the list), setting the first or last * elements of the list is O(1). * - * @see #lindex(String, int) + * @see #lindex(String, long) * * @param key * @param index @@ -1050,9 +992,9 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Status code reply */ public String lset(final String key, final long index, final String value) { - checkIsInMulti(); - client.lset(key, index, value); - return client.getStatusCodeReply(); + checkIsInMulti(); + client.lset(key, index, value); + return client.getStatusCodeReply(); } /** @@ -1075,9 +1017,9 @@ public class Jedis extends BinaryJedis implements JedisCommands { * the operation succeeded */ public Long lrem(final String key, final long count, final String value) { - checkIsInMulti(); - client.lrem(key, count, value); - return client.getIntegerReply(); + checkIsInMulti(); + client.lrem(key, count, value); + return client.getIntegerReply(); } /** @@ -1094,9 +1036,9 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Bulk reply */ public String lpop(final String key) { - checkIsInMulti(); - client.lpop(key); - return client.getBulkReply(); + checkIsInMulti(); + client.lpop(key); + return client.getBulkReply(); } /** @@ -1113,9 +1055,9 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Bulk reply */ public String rpop(final String key) { - checkIsInMulti(); - client.rpop(key); - return client.getBulkReply(); + checkIsInMulti(); + client.rpop(key); + return client.getBulkReply(); } /** @@ -1137,9 +1079,9 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Bulk reply */ public String rpoplpush(final String srckey, final String dstkey) { - checkIsInMulti(); - client.rpoplpush(srckey, dstkey); - return client.getBulkReply(); + checkIsInMulti(); + client.rpoplpush(srckey, dstkey); + return client.getBulkReply(); } /** @@ -1151,14 +1093,14 @@ public class Jedis extends BinaryJedis implements JedisCommands { * Time complexity O(1) * * @param key - * @param member + * @param members * @return Integer reply, specifically: 1 if the new element was added 0 if * the element was already a member of the set */ - public Long sadd(final String key, final String member) { - checkIsInMulti(); - client.sadd(key, member); - return client.getIntegerReply(); + public Long sadd(final String key, final String... members) { + checkIsInMulti(); + client.sadd(key, members); + return client.getIntegerReply(); } /** @@ -1171,10 +1113,10 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Multi bulk reply */ public Set smembers(final String key) { - checkIsInMulti(); - client.smembers(key); - final List members = client.getMultiBulkReply(); - return new HashSet(members); + checkIsInMulti(); + client.smembers(key); + final List members = client.getMultiBulkReply(); + return new HashSet(members); } /** @@ -1185,14 +1127,14 @@ public class Jedis extends BinaryJedis implements JedisCommands { * Time complexity O(1) * * @param key - * @param member + * @param members * @return Integer reply, specifically: 1 if the new element was removed 0 * if the new element was not a member of the set */ - public Long srem(final String key, final String member) { - checkIsInMulti(); - client.srem(key, member); - return client.getIntegerReply(); + public Long srem(final String key, final String... members) { + checkIsInMulti(); + client.srem(key, members); + return client.getIntegerReply(); } /** @@ -1208,9 +1150,9 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Bulk reply */ public String spop(final String key) { - checkIsInMulti(); - client.spop(key); - return client.getBulkReply(); + checkIsInMulti(); + client.spop(key); + return client.getBulkReply(); } /** @@ -1237,10 +1179,10 @@ public class Jedis extends BinaryJedis implements JedisCommands { * performed */ public Long smove(final String srckey, final String dstkey, - final String member) { - checkIsInMulti(); - client.smove(srckey, dstkey, member); - return client.getIntegerReply(); + final String member) { + checkIsInMulti(); + client.smove(srckey, dstkey, member); + return client.getIntegerReply(); } /** @@ -1252,9 +1194,9 @@ public class Jedis extends BinaryJedis implements JedisCommands { * of the set as an integer. */ public Long scard(final String key) { - checkIsInMulti(); - client.scard(key); - return client.getIntegerReply(); + checkIsInMulti(); + client.scard(key); + return client.getIntegerReply(); } /** @@ -1270,16 +1212,16 @@ public class Jedis extends BinaryJedis implements JedisCommands { * does not exist */ public Boolean sismember(final String key, final String member) { - checkIsInMulti(); - client.sismember(key, member); - return client.getIntegerReply() == 1; + checkIsInMulti(); + client.sismember(key, member); + return client.getIntegerReply() == 1; } /** * Return the members of a set resulting from the intersection of all the * sets hold at the specified keys. Like in - * {@link #lrange(String, int, int) LRANGE} the result is sent to the client - * as a multi-bulk reply (see the protocol specification for more + * {@link #lrange(String, long, long) LRANGE} the result is sent to the + * client as a multi-bulk reply (see the protocol specification for more * information). If just a single key is specified, then this command * produces the same result as {@link #smembers(String) SMEMBERS}. Actually * SMEMBERS is just syntax sugar for SINTER. @@ -1295,10 +1237,10 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Multi bulk reply, specifically the list of common elements. */ public Set sinter(final String... keys) { - checkIsInMulti(); - client.sinter(keys); - final List members = client.getMultiBulkReply(); - return new HashSet(members); + checkIsInMulti(); + client.sinter(keys); + final List members = client.getMultiBulkReply(); + return new HashSet(members); } /** @@ -1313,14 +1255,14 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Status code reply */ public Long sinterstore(final String dstkey, final String... keys) { - checkIsInMulti(); - client.sinterstore(dstkey, keys); - return client.getIntegerReply(); + checkIsInMulti(); + client.sinterstore(dstkey, keys); + return client.getIntegerReply(); } /** * Return the members of a set resulting from the union of all the sets hold - * at the specified keys. Like in {@link #lrange(String, int, int) LRANGE} + * at the specified keys. Like in {@link #lrange(String, long, long) LRANGE} * the result is sent to the client as a multi-bulk reply (see the protocol * specification for more information). If just a single key is specified, * then this command produces the same result as {@link #smembers(String) @@ -1335,10 +1277,10 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Multi bulk reply, specifically the list of common elements. */ public Set sunion(final String... keys) { - checkIsInMulti(); - client.sunion(keys); - final List members = client.getMultiBulkReply(); - return new HashSet(members); + checkIsInMulti(); + client.sunion(keys); + final List members = client.getMultiBulkReply(); + return new HashSet(members); } /** @@ -1354,9 +1296,9 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Status code reply */ public Long sunionstore(final String dstkey, final String... keys) { - checkIsInMulti(); - client.sunionstore(dstkey, keys); - return client.getIntegerReply(); + checkIsInMulti(); + client.sunionstore(dstkey, keys); + return client.getIntegerReply(); } /** @@ -1383,10 +1325,10 @@ public class Jedis extends BinaryJedis implements JedisCommands { * the first set provided and all the successive sets. */ public Set sdiff(final String... keys) { - checkIsInMulti(); - client.sdiff(keys); - return BuilderFactory.STRING_SET - .build(client.getBinaryMultiBulkReply()); + checkIsInMulti(); + client.sdiff(keys); + return BuilderFactory.STRING_SET + .build(client.getBinaryMultiBulkReply()); } /** @@ -1398,9 +1340,9 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Status code reply */ public Long sdiffstore(final String dstkey, final String... keys) { - checkIsInMulti(); - client.sdiffstore(dstkey, keys); - return client.getIntegerReply(); + checkIsInMulti(); + client.sdiffstore(dstkey, keys); + return client.getIntegerReply(); } /** @@ -1416,9 +1358,15 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Bulk reply */ public String srandmember(final String key) { - checkIsInMulti(); - client.srandmember(key); - return client.getBulkReply(); + checkIsInMulti(); + client.srandmember(key); + return client.getBulkReply(); + } + + public List srandmember(final String key, final int count) { + checkIsInMulti(); + client.srandmember(key, count); + return client.getMultiBulkReply(); } /** @@ -1443,16 +1391,22 @@ public class Jedis extends BinaryJedis implements JedisCommands { * was updated */ public Long zadd(final String key, final double score, final String member) { - checkIsInMulti(); - client.zadd(key, score, member); - return client.getIntegerReply(); + checkIsInMulti(); + client.zadd(key, score, member); + return client.getIntegerReply(); } - public Set zrange(final String key, final int start, final int end) { - checkIsInMulti(); - client.zrange(key, start, end); - final List members = client.getMultiBulkReply(); - return new LinkedHashSet(members); + public Long zadd(final String key, final Map scoreMembers) { + checkIsInMulti(); + client.zadd(key, scoreMembers); + return client.getIntegerReply(); + } + + public Set zrange(final String key, final long start, final long end) { + checkIsInMulti(); + client.zrange(key, start, end); + final List members = client.getMultiBulkReply(); + return new LinkedHashSet(members); } /** @@ -1466,14 +1420,14 @@ public class Jedis extends BinaryJedis implements JedisCommands { * * * @param key - * @param member + * @param members * @return Integer reply, specifically: 1 if the new element was removed 0 * if the new element was not a member of the set */ - public Long zrem(final String key, final String member) { - checkIsInMulti(); - client.zrem(key, member); - return client.getIntegerReply(); + public Long zrem(final String key, final String... members) { + checkIsInMulti(); + client.zrem(key, members); + return client.getIntegerReply(); } /** @@ -1501,11 +1455,11 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return The new score */ public Double zincrby(final String key, final double score, - final String member) { - checkIsInMulti(); - client.zincrby(key, score, member); - String newscore = client.getBulkReply(); - return Double.valueOf(newscore); + final String member) { + checkIsInMulti(); + client.zincrby(key, score, member); + String newscore = client.getBulkReply(); + return Double.valueOf(newscore); } /** @@ -1529,9 +1483,9 @@ public class Jedis extends BinaryJedis implements JedisCommands { * reply if there is no such element. */ public Long zrank(final String key, final String member) { - checkIsInMulti(); - client.zrank(key, member); - return client.getIntegerReply(); + checkIsInMulti(); + client.zrank(key, member); + return client.getIntegerReply(); } /** @@ -1555,33 +1509,33 @@ public class Jedis extends BinaryJedis implements JedisCommands { * reply if there is no such element. */ public Long zrevrank(final String key, final String member) { - checkIsInMulti(); - client.zrevrank(key, member); - return client.getIntegerReply(); + checkIsInMulti(); + client.zrevrank(key, member); + return client.getIntegerReply(); } - public Set zrevrange(final String key, final int start, - final int end) { - checkIsInMulti(); - client.zrevrange(key, start, end); - final List members = client.getMultiBulkReply(); - return new LinkedHashSet(members); + public Set zrevrange(final String key, final long start, + final long end) { + checkIsInMulti(); + client.zrevrange(key, start, end); + final List members = client.getMultiBulkReply(); + return new LinkedHashSet(members); } - public Set zrangeWithScores(final String key, final int start, - final int end) { - checkIsInMulti(); - client.zrangeWithScores(key, start, end); - Set set = getTupledSet(); - return set; + public Set zrangeWithScores(final String key, final long start, + final long end) { + checkIsInMulti(); + client.zrangeWithScores(key, start, end); + Set set = getTupledSet(); + return set; } - public Set zrevrangeWithScores(final String key, final int start, - final int end) { - checkIsInMulti(); - client.zrevrangeWithScores(key, start, end); - Set set = getTupledSet(); - return set; + public Set zrevrangeWithScores(final String key, final long start, + final long end) { + checkIsInMulti(); + client.zrevrangeWithScores(key, start, end); + Set set = getTupledSet(); + return set; } /** @@ -1594,9 +1548,9 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return the cardinality (number of elements) of the set as an integer. */ public Long zcard(final String key) { - checkIsInMulti(); - client.zcard(key); - return client.getIntegerReply(); + checkIsInMulti(); + client.zcard(key); + return client.getIntegerReply(); } /** @@ -1611,15 +1565,15 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return the score */ public Double zscore(final String key, final String member) { - checkIsInMulti(); - client.zscore(key, member); - final String score = client.getBulkReply(); - return (score != null ? new Double(score) : null); + checkIsInMulti(); + client.zscore(key, member); + final String score = client.getBulkReply(); + return (score != null ? new Double(score) : null); } public String watch(final String... keys) { - client.watch(keys); - return client.getStatusCodeReply(); + client.watch(keys); + return client.getStatusCodeReply(); } /** @@ -1640,9 +1594,9 @@ public class Jedis extends BinaryJedis implements JedisCommands { * smallest to the biggest number. */ public List sort(final String key) { - checkIsInMulti(); - client.sort(key); - return client.getMultiBulkReply(); + checkIsInMulti(); + client.sort(key); + return client.getMultiBulkReply(); } /** @@ -1722,10 +1676,10 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return a list of sorted elements. */ public List sort(final String key, - final SortingParams sortingParameters) { - checkIsInMulti(); - client.sort(key, sortingParameters); - return client.getMultiBulkReply(); + final SortingParams sortingParameters) { + checkIsInMulti(); + client.sort(key, sortingParameters); + return client.getMultiBulkReply(); } /** @@ -1801,14 +1755,50 @@ public class Jedis extends BinaryJedis implements JedisCommands { * programming language used. */ public List blpop(final int timeout, final String... keys) { - checkIsInMulti(); - List args = new ArrayList(); - for (String arg : keys) { - args.add(arg); - } - args.add(String.valueOf(timeout)); + checkIsInMulti(); + List args = new ArrayList(); + for (String arg : keys) { + args.add(arg); + } + args.add(String.valueOf(timeout)); - client.blpop(args.toArray(new String[args.size()])); + client.blpop(args.toArray(new String[args.size()])); + client.setTimeoutInfinite(); + final List multiBulkReply = client.getMultiBulkReply(); + client.rollbackTimeout(); + return multiBulkReply; + } + + public List blpop(String... args) { + client.blpop(args); + client.setTimeoutInfinite(); + final List multiBulkReply = client.getMultiBulkReply(); + client.rollbackTimeout(); + return multiBulkReply; + } + + public List brpop(String... args) { + client.brpop(args); + client.setTimeoutInfinite(); + final List multiBulkReply = client.getMultiBulkReply(); + client.rollbackTimeout(); + return multiBulkReply; + } + + public List blpop(String arg) { + String[] args = new String[1]; + args[0] = arg; + client.blpop(args); + client.setTimeoutInfinite(); + final List multiBulkReply = client.getMultiBulkReply(); + client.rollbackTimeout(); + return multiBulkReply; + } + + public List brpop(String arg) { + String[] args = new String[1]; + args[0] = arg; + client.brpop(args); client.setTimeoutInfinite(); final List multiBulkReply = client.getMultiBulkReply(); client.rollbackTimeout(); @@ -1829,10 +1819,10 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return The number of elements of the list at dstkey. */ public Long sort(final String key, final SortingParams sortingParameters, - final String dstkey) { - checkIsInMulti(); - client.sort(key, sortingParameters, dstkey); - return client.getIntegerReply(); + final String dstkey) { + checkIsInMulti(); + client.sort(key, sortingParameters, dstkey); + return client.getIntegerReply(); } /** @@ -1852,9 +1842,9 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return The number of elements of the list at dstkey. */ public Long sort(final String key, final String dstkey) { - checkIsInMulti(); - client.sort(key, dstkey); - return client.getIntegerReply(); + checkIsInMulti(); + client.sort(key, dstkey); + return client.getIntegerReply(); } /** @@ -1930,69 +1920,33 @@ public class Jedis extends BinaryJedis implements JedisCommands { * programming language used. */ public List brpop(final int timeout, final String... keys) { - checkIsInMulti(); - List args = new ArrayList(); - for (String arg : keys) { - args.add(arg); - } - args.add(String.valueOf(timeout)); + checkIsInMulti(); + List args = new ArrayList(); + for (String arg : keys) { + args.add(arg); + } + args.add(String.valueOf(timeout)); - client.brpop(args.toArray(new String[args.size()])); - client.setTimeoutInfinite(); - List multiBulkReply = client.getMultiBulkReply(); - client.rollbackTimeout(); + client.brpop(args.toArray(new String[args.size()])); + client.setTimeoutInfinite(); + List multiBulkReply = client.getMultiBulkReply(); + client.rollbackTimeout(); - return multiBulkReply; + return multiBulkReply; } - /** - * Request for authentication in a password protected Redis server. A Redis - * server can be instructed to require a password before to allow clients to - * issue commands. This is done using the requirepass directive in the Redis - * configuration file. If the password given by the client is correct the - * server replies with an OK status code reply and starts accepting commands - * from the client. Otherwise an error is returned and the clients needs to - * try a new password. Note that for the high performance nature of Redis it - * is possible to try a lot of passwords in parallel in very short time, so - * make sure to generate a strong and very long password so that this attack - * is infeasible. - * - * @param password - * @return Status code reply - */ - public String auth(final String password) { - checkIsInMulti(); - client.auth(password); - return client.getStatusCodeReply(); - } - - public void subscribe(JedisPubSub jedisPubSub, String... channels) { - checkIsInMulti(); - connect(); - client.setTimeoutInfinite(); - jedisPubSub.proceed(client, channels); - client.rollbackTimeout(); - } - - public Long publish(String channel, String message) { - checkIsInMulti(); - client.publish(channel, message); - return client.getIntegerReply(); - } - - public void psubscribe(JedisPubSub jedisPubSub, String... patterns) { - checkIsInMulti(); - connect(); - client.setTimeoutInfinite(); - jedisPubSub.proceedWithPatterns(client, patterns); - client.rollbackTimeout(); - } public Long zcount(final String key, final double min, final double max) { - checkIsInMulti(); - client.zcount(key, min, max); - return client.getIntegerReply(); + checkIsInMulti(); + client.zcount(key, min, max); + return client.getIntegerReply(); + } + + public Long zcount(final String key, final String min, final String max) { + checkIsInMulti(); + client.zcount(key, min, max); + return client.getIntegerReply(); } /** @@ -2053,17 +2007,17 @@ public class Jedis extends BinaryJedis implements JedisCommands { * score range. */ public Set zrangeByScore(final String key, final double min, - final double max) { - checkIsInMulti(); - client.zrangeByScore(key, min, max); - return new LinkedHashSet(client.getMultiBulkReply()); + final double max) { + checkIsInMulti(); + client.zrangeByScore(key, min, max); + return new LinkedHashSet(client.getMultiBulkReply()); } public Set zrangeByScore(final String key, final String min, - final String max) { - checkIsInMulti(); - client.zrangeByScore(key, min, max); - return new LinkedHashSet(client.getMultiBulkReply()); + final String max) { + checkIsInMulti(); + client.zrangeByScore(key, min, max); + return new LinkedHashSet(client.getMultiBulkReply()); } /** @@ -2123,10 +2077,17 @@ public class Jedis extends BinaryJedis implements JedisCommands { * score range. */ public Set zrangeByScore(final String key, final double min, - final double max, final int offset, final int count) { - checkIsInMulti(); - client.zrangeByScore(key, min, max, offset, count); - return new LinkedHashSet(client.getMultiBulkReply()); + final double max, final int offset, final int count) { + checkIsInMulti(); + client.zrangeByScore(key, min, max, offset, count); + return new LinkedHashSet(client.getMultiBulkReply()); + } + + public Set zrangeByScore(final String key, final String min, + final String max, final int offset, final int count) { + checkIsInMulti(); + client.zrangeByScore(key, min, max, offset, count); + return new LinkedHashSet(client.getMultiBulkReply()); } /** @@ -2186,11 +2147,19 @@ public class Jedis extends BinaryJedis implements JedisCommands { * score range. */ public Set zrangeByScoreWithScores(final String key, - final double min, final double max) { - checkIsInMulti(); - client.zrangeByScoreWithScores(key, min, max); - Set set = getTupledSet(); - return set; + final double min, final double max) { + checkIsInMulti(); + client.zrangeByScoreWithScores(key, min, max); + Set set = getTupledSet(); + return set; + } + + public Set zrangeByScoreWithScores(final String key, + final String min, final String max) { + checkIsInMulti(); + client.zrangeByScoreWithScores(key, min, max); + Set set = getTupledSet(); + return set; } /** @@ -2250,63 +2219,94 @@ public class Jedis extends BinaryJedis implements JedisCommands { * score range. */ public Set zrangeByScoreWithScores(final String key, - final double min, final double max, final int offset, - final int count) { - checkIsInMulti(); - client.zrangeByScoreWithScores(key, min, max, offset, count); - Set set = getTupledSet(); - return set; + final double min, final double max, final int offset, + final int count) { + checkIsInMulti(); + client.zrangeByScoreWithScores(key, min, max, offset, count); + Set set = getTupledSet(); + return set; + } + + public Set zrangeByScoreWithScores(final String key, + final String min, final String max, final int offset, + final int count) { + checkIsInMulti(); + client.zrangeByScoreWithScores(key, min, max, offset, count); + Set set = getTupledSet(); + return set; } private Set getTupledSet() { - checkIsInMulti(); - List membersWithScores = client.getMultiBulkReply(); - Set set = new LinkedHashSet(); - Iterator iterator = membersWithScores.iterator(); - while (iterator.hasNext()) { - set - .add(new Tuple(iterator.next(), Double.valueOf(iterator - .next()))); - } - return set; + checkIsInMulti(); + List membersWithScores = client.getMultiBulkReply(); + Set set = new LinkedHashSet(); + Iterator iterator = membersWithScores.iterator(); + while (iterator.hasNext()) { + set.add(new Tuple(iterator.next(), Double.valueOf(iterator.next()))); + } + return set; } public Set zrevrangeByScore(final String key, final double max, - final double min) { - checkIsInMulti(); - client.zrevrangeByScore(key, max, min); - return new LinkedHashSet(client.getMultiBulkReply()); + final double min) { + checkIsInMulti(); + client.zrevrangeByScore(key, max, min); + return new LinkedHashSet(client.getMultiBulkReply()); } public Set zrevrangeByScore(final String key, final String max, - final String min) { - checkIsInMulti(); - client.zrevrangeByScore(key, max, min); - return new LinkedHashSet(client.getMultiBulkReply()); + final String min) { + checkIsInMulti(); + client.zrevrangeByScore(key, max, min); + return new LinkedHashSet(client.getMultiBulkReply()); } public Set zrevrangeByScore(final String key, final double max, - final double min, final int offset, final int count) { - checkIsInMulti(); - client.zrevrangeByScore(key, max, min, offset, count); - return new LinkedHashSet(client.getMultiBulkReply()); + final double min, final int offset, final int count) { + checkIsInMulti(); + client.zrevrangeByScore(key, max, min, offset, count); + return new LinkedHashSet(client.getMultiBulkReply()); } public Set zrevrangeByScoreWithScores(final String key, - final double max, final double min) { - checkIsInMulti(); - client.zrevrangeByScoreWithScores(key, max, min); - Set set = getTupledSet(); - return set; + final double max, final double min) { + checkIsInMulti(); + client.zrevrangeByScoreWithScores(key, max, min); + Set set = getTupledSet(); + return set; } public Set zrevrangeByScoreWithScores(final String key, - final double max, final double min, final int offset, - final int count) { - checkIsInMulti(); - client.zrevrangeByScoreWithScores(key, max, min, offset, count); - Set set = getTupledSet(); - return set; + final double max, final double min, final int offset, + final int count) { + checkIsInMulti(); + client.zrevrangeByScoreWithScores(key, max, min, offset, count); + Set set = getTupledSet(); + return set; + } + + public Set zrevrangeByScoreWithScores(final String key, + final String max, final String min, final int offset, + final int count) { + checkIsInMulti(); + client.zrevrangeByScoreWithScores(key, max, min, offset, count); + Set set = getTupledSet(); + return set; + } + + public Set zrevrangeByScore(final String key, final String max, + final String min, final int offset, final int count) { + checkIsInMulti(); + client.zrevrangeByScore(key, max, min, offset, count); + return new LinkedHashSet(client.getMultiBulkReply()); + } + + public Set zrevrangeByScoreWithScores(final String key, + final String max, final String min) { + checkIsInMulti(); + client.zrevrangeByScoreWithScores(key, max, min); + Set set = getTupledSet(); + return set; } /** @@ -2322,10 +2322,11 @@ public class Jedis extends BinaryJedis implements JedisCommands { * operation * */ - public Long zremrangeByRank(final String key, final int start, final int end) { - checkIsInMulti(); - client.zremrangeByRank(key, start, end); - return client.getIntegerReply(); + public Long zremrangeByRank(final String key, final long start, + final long end) { + checkIsInMulti(); + client.zremrangeByRank(key, start, end); + return client.getIntegerReply(); } /** @@ -2343,10 +2344,17 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Integer reply, specifically the number of elements removed. */ public Long zremrangeByScore(final String key, final double start, - final double end) { - checkIsInMulti(); - client.zremrangeByScore(key, start, end); - return client.getIntegerReply(); + final double end) { + checkIsInMulti(); + client.zremrangeByScore(key, start, end); + return client.getIntegerReply(); + } + + public Long zremrangeByScore(final String key, final String start, + final String end) { + checkIsInMulti(); + client.zremrangeByScore(key, start, end); + return client.getIntegerReply(); } /** @@ -2388,9 +2396,9 @@ public class Jedis extends BinaryJedis implements JedisCommands { * set at dstkey */ public Long zunionstore(final String dstkey, final String... sets) { - checkIsInMulti(); - client.zunionstore(dstkey, sets); - return client.getIntegerReply(); + checkIsInMulti(); + client.zunionstore(dstkey, sets); + return client.getIntegerReply(); } /** @@ -2433,10 +2441,10 @@ public class Jedis extends BinaryJedis implements JedisCommands { * set at dstkey */ public Long zunionstore(final String dstkey, final ZParams params, - final String... sets) { - checkIsInMulti(); - client.zunionstore(dstkey, params, sets); - return client.getIntegerReply(); + final String... sets) { + checkIsInMulti(); + client.zunionstore(dstkey, params, sets); + return client.getIntegerReply(); } /** @@ -2478,9 +2486,9 @@ public class Jedis extends BinaryJedis implements JedisCommands { * set at dstkey */ public Long zinterstore(final String dstkey, final String... sets) { - checkIsInMulti(); - client.zinterstore(dstkey, sets); - return client.getIntegerReply(); + checkIsInMulti(); + client.zinterstore(dstkey, sets); + return client.getIntegerReply(); } /** @@ -2523,20 +2531,20 @@ public class Jedis extends BinaryJedis implements JedisCommands { * set at dstkey */ public Long zinterstore(final String dstkey, final ZParams params, - final String... sets) { - checkIsInMulti(); - client.zinterstore(dstkey, params, sets); - return client.getIntegerReply(); + final String... sets) { + checkIsInMulti(); + client.zinterstore(dstkey, params, sets); + return client.getIntegerReply(); } public Long strlen(final String key) { - client.strlen(key); - return client.getIntegerReply(); + client.strlen(key); + return client.getIntegerReply(); } - public Long lpushx(final String key, final String string) { - client.lpushx(key, string); - return client.getIntegerReply(); + public Long lpushx(final String key, final String... string) { + client.lpushx(key, string); + return client.getIntegerReply(); } /** @@ -2550,24 +2558,24 @@ public class Jedis extends BinaryJedis implements JedisCommands { * key is not persist (only happens when key not set). */ public Long persist(final String key) { - client.persist(key); - return client.getIntegerReply(); + client.persist(key); + return client.getIntegerReply(); } - public Long rpushx(final String key, final String string) { - client.rpushx(key, string); - return client.getIntegerReply(); + public Long rpushx(final String key, final String... string) { + client.rpushx(key, string); + return client.getIntegerReply(); } public String echo(final String string) { - client.echo(string); - return client.getBulkReply(); + client.echo(string); + return client.getBulkReply(); } public Long linsert(final String key, final LIST_POSITION where, - final String pivot, final String value) { - client.linsert(key, where, pivot, value); - return client.getIntegerReply(); + final String pivot, final String value) { + client.linsert(key, where, pivot, value); + return client.getIntegerReply(); } /** @@ -2580,11 +2588,11 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return the element */ public String brpoplpush(String source, String destination, int timeout) { - client.brpoplpush(source, destination, timeout); - client.setTimeoutInfinite(); - String reply = client.getBulkReply(); - client.rollbackTimeout(); - return reply; + client.brpoplpush(source, destination, timeout); + client.setTimeoutInfinite(); + String reply = client.getBulkReply(); + client.rollbackTimeout(); + return reply; } /** @@ -2595,9 +2603,14 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @param value * @return */ - public boolean setbit(String key, long offset, boolean value) { - client.setbit(key, offset, value); - return client.getIntegerReply() == 1; + public Boolean setbit(String key, long offset, boolean value) { + client.setbit(key, offset, value); + return client.getIntegerReply() == 1; + } + + public Boolean setbit(String key, long offset, String value) { + client.setbit(key, offset, value); + return client.getIntegerReply() == 1; } /** @@ -2607,18 +2620,471 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @param offset * @return */ - public boolean getbit(String key, long offset) { - client.getbit(key, offset); - return client.getIntegerReply() == 1; + public Boolean getbit(String key, long offset) { + client.getbit(key, offset); + return client.getIntegerReply() == 1; } - public long setrange(String key, long offset, String value) { - client.setrange(key, offset, value); - return client.getIntegerReply(); + public Long setrange(String key, long offset, String value) { + client.setrange(key, offset, value); + return client.getIntegerReply(); } public String getrange(String key, long startOffset, long endOffset) { - client.getrange(key, startOffset, endOffset); - return client.getBulkReply(); + client.getrange(key, startOffset, endOffset); + return client.getBulkReply(); + } + + /** + * Retrieve the configuration of a running Redis server. Not all the + * configuration parameters are supported. + *

+ * CONFIG GET returns the current configuration parameters. This sub command + * only accepts a single argument, that is glob style pattern. All the + * configuration parameters matching this parameter are reported as a list + * of key-value pairs. + *

+ * Example: + * + *

+     * $ redis-cli config get '*'
+     * 1. "dbfilename"
+     * 2. "dump.rdb"
+     * 3. "requirepass"
+     * 4. (nil)
+     * 5. "masterauth"
+     * 6. (nil)
+     * 7. "maxmemory"
+     * 8. "0\n"
+     * 9. "appendfsync"
+     * 10. "everysec"
+     * 11. "save"
+     * 12. "3600 1 300 100 60 10000"
+     * 
+     * $ redis-cli config get 'm*'
+     * 1. "masterauth"
+     * 2. (nil)
+     * 3. "maxmemory"
+     * 4. "0\n"
+     * 
+ * + * @param pattern + * @return Bulk reply. + */ + public List configGet(final String pattern) { + client.configGet(pattern); + return client.getMultiBulkReply(); + } + + /** + * Alter the configuration of a running Redis server. Not all the + * configuration parameters are supported. + *

+ * The list of configuration parameters supported by CONFIG SET can be + * obtained issuing a {@link #configGet(String) CONFIG GET *} command. + *

+ * The configuration set using CONFIG SET is immediately loaded by the Redis + * server that will start acting as specified starting from the next + * command. + *

+ * + * Parameters value format + *

+ * The value of the configuration parameter is the same as the one of the + * same parameter in the Redis configuration file, with the following + * exceptions: + *

+ *

    + *
  • The save paramter is a list of space-separated integers. Every pair + * of integers specify the time and number of changes limit to trigger a + * save. For instance the command CONFIG SET save "3600 10 60 10000" will + * configure the server to issue a background saving of the RDB file every + * 3600 seconds if there are at least 10 changes in the dataset, and every + * 60 seconds if there are at least 10000 changes. To completely disable + * automatic snapshots just set the parameter as an empty string. + *
  • All the integer parameters representing memory are returned and + * accepted only using bytes as unit. + *
+ * + * @param parameter + * @param value + * @return Status code reply + */ + public String configSet(final String parameter, final String value) { + client.configSet(parameter, value); + return client.getStatusCodeReply(); + } + + public Object eval(String script, int keyCount, String... params) { + client.setTimeoutInfinite(); + client.eval(script, keyCount, params); + + return getEvalResult(); + } + + public void subscribe(final JedisPubSub jedisPubSub, + final String... channels) { + client.setTimeoutInfinite(); + jedisPubSub.proceed(client, channels); + client.rollbackTimeout(); + } + + public Long publish(final String channel, final String message) { + checkIsInMulti(); + connect(); + client.publish(channel, message); + return client.getIntegerReply(); + } + + public void psubscribe(final JedisPubSub jedisPubSub, + final String... patterns) { + checkIsInMulti(); + connect(); + client.setTimeoutInfinite(); + jedisPubSub.proceedWithPatterns(client, patterns); + client.rollbackTimeout(); + } + + private String[] getParams(List keys, List args) { + int keyCount = keys.size(); + int argCount = args.size(); + + String[] params = new String[keyCount + args.size()]; + + for (int i = 0; i < keyCount; i++) + params[i] = keys.get(i); + + for (int i = 0; i < argCount; i++) + params[keyCount + i] = args.get(i); + + return params; + } + + public Object eval(String script, List keys, List args) { + return eval(script, keys.size(), getParams(keys, args)); + } + + public Object eval(String script) { + return eval(script, 0); + } + + public Object evalsha(String script) { + return evalsha(script, 0); + } + + private Object getEvalResult() { + Object result = client.getOne(); + + if (result instanceof byte[]) + return SafeEncoder.encode((byte[]) result); + + if (result instanceof List) { + List list = (List) result; + List listResult = new ArrayList(list.size()); + for (Object bin : list) { + listResult.add((bin == null ? null : SafeEncoder + .encode((byte[]) bin))); + } + + return listResult; + } + + return result; + } + + public Object evalsha(String sha1, List keys, List args) { + return evalsha(sha1, keys.size(), getParams(keys, args)); + } + + public Object evalsha(String sha1, int keyCount, String... params) { + checkIsInMulti(); + client.evalsha(sha1, keyCount, params); + + return getEvalResult(); + } + + public Boolean scriptExists(String sha1) { + String[] a = new String[1]; + a[0] = sha1; + return scriptExists(a).get(0); + } + + public List scriptExists(String... sha1) { + client.scriptExists(sha1); + List result = client.getIntegerMultiBulkReply(); + List exists = new ArrayList(); + + for (Long value : result) + exists.add(value == 1); + + return exists; + } + + public String scriptLoad(String script) { + client.scriptLoad(script); + return client.getBulkReply(); + } + + public List slowlogGet() { + client.slowlogGet(); + return Slowlog.from(client.getObjectMultiBulkReply()); + } + + public List slowlogGet(long entries) { + client.slowlogGet(entries); + return Slowlog.from(client.getObjectMultiBulkReply()); + } + + public Long objectRefcount(String string) { + client.objectRefcount(string); + return client.getIntegerReply(); + } + + public String objectEncoding(String string) { + client.objectEncoding(string); + return client.getBulkReply(); + } + + public Long objectIdletime(String string) { + client.objectIdletime(string); + return client.getIntegerReply(); + } + + public Long bitcount(final String key) { + client.bitcount(key); + return client.getIntegerReply(); + } + + public Long bitcount(final String key, long start, long end) { + client.bitcount(key, start, end); + return client.getIntegerReply(); + } + + public Long bitop(BitOP op, final String destKey, String... srcKeys) { + client.bitop(op, destKey, srcKeys); + return client.getIntegerReply(); + } + + /** + *
+     * redis 127.0.0.1:26381> sentinel masters
+     * 1)  1) "name"
+     *     2) "mymaster"
+     *     3) "ip"
+     *     4) "127.0.0.1"
+     *     5) "port"
+     *     6) "6379"
+     *     7) "runid"
+     *     8) "93d4d4e6e9c06d0eea36e27f31924ac26576081d"
+     *     9) "flags"
+     *    10) "master"
+     *    11) "pending-commands"
+     *    12) "0"
+     *    13) "last-ok-ping-reply"
+     *    14) "423"
+     *    15) "last-ping-reply"
+     *    16) "423"
+     *    17) "info-refresh"
+     *    18) "6107"
+     *    19) "num-slaves"
+     *    20) "1"
+     *    21) "num-other-sentinels"
+     *    22) "2"
+     *    23) "quorum"
+     *    24) "2"
+     * 
+     * 
+ * + * @return + */ + @SuppressWarnings("rawtypes") + public List> sentinelMasters() { + client.sentinel(Protocol.SENTINEL_MASTERS); + final List reply = client.getObjectMultiBulkReply(); + + final List> masters = new ArrayList>(); + for (Object obj : reply) { + masters.add(BuilderFactory.STRING_MAP.build((List) obj)); + } + return masters; + } + + /** + *
+     * redis 127.0.0.1:26381> sentinel get-master-addr-by-name mymaster
+     * 1) "127.0.0.1"
+     * 2) "6379"
+     * 
+ * + * @param masterName + * @return two elements list of strings : host and port. + */ + public List sentinelGetMasterAddrByName(String masterName) { + client.sentinel(Protocol.SENTINEL_GET_MASTER_ADDR_BY_NAME, masterName); + final List reply = client.getObjectMultiBulkReply(); + return BuilderFactory.STRING_LIST.build(reply); + } + + /** + *
+     * redis 127.0.0.1:26381> sentinel reset mymaster
+     * (integer) 1
+     * 
+ * + * @param pattern + * @return + */ + public Long sentinelReset(String pattern) { + client.sentinel(Protocol.SENTINEL_RESET, pattern); + return client.getIntegerReply(); + } + + /** + *
+     * redis 127.0.0.1:26381> sentinel slaves mymaster
+     * 1)  1) "name"
+     *     2) "127.0.0.1:6380"
+     *     3) "ip"
+     *     4) "127.0.0.1"
+     *     5) "port"
+     *     6) "6380"
+     *     7) "runid"
+     *     8) "d7f6c0ca7572df9d2f33713df0dbf8c72da7c039"
+     *     9) "flags"
+     *    10) "slave"
+     *    11) "pending-commands"
+     *    12) "0"
+     *    13) "last-ok-ping-reply"
+     *    14) "47"
+     *    15) "last-ping-reply"
+     *    16) "47"
+     *    17) "info-refresh"
+     *    18) "657"
+     *    19) "master-link-down-time"
+     *    20) "0"
+     *    21) "master-link-status"
+     *    22) "ok"
+     *    23) "master-host"
+     *    24) "localhost"
+     *    25) "master-port"
+     *    26) "6379"
+     *    27) "slave-priority"
+     *    28) "100"
+     * 
+ * + * @param masterName + * @return + */ + @SuppressWarnings("rawtypes") + public List> sentinelSlaves(String masterName) { + client.sentinel(Protocol.SENTINEL_SLAVES, masterName); + final List reply = client.getObjectMultiBulkReply(); + + final List> slaves = new ArrayList>(); + for (Object obj : reply) { + slaves.add(BuilderFactory.STRING_MAP.build((List) obj)); + } + return slaves; + } + + /** + *
+     * redis 127.0.0.1:26381> SENTINEL is-master-down-by-addr 127.0.0.1 1
+     * 1) (integer) 0
+     * 2) "?"
+     * redis 127.0.0.1:26381> SENTINEL is-master-down-by-addr 127.0.0.1 6379
+     * 1) (integer) 0
+     * 2) "aaef11fbb2712346a386078c7f9834e72ed51e96"
+     * 
+ * + * @return Long followed by the String (runid) + */ + @SuppressWarnings("unchecked") + public List sentinelIsMasterDownByAddr(String host, + int port) { + client.sentinel(Protocol.SENTINEL_IS_MASTER_DOWN_BY_ADDR, host, port); + final List reply = client.getObjectMultiBulkReply(); + return Arrays.asList(BuilderFactory.LONG.build(reply.get(0)), + BuilderFactory.STRING.build(reply.get(1))); + } + + public byte[] dump(final String key) { + checkIsInMulti(); + client.dump(key); + return client.getBinaryBulkReply(); + } + + public String restore(final String key, final int ttl, final byte[] serializedValue) { + checkIsInMulti(); + client.restore(key, ttl, serializedValue); + return client.getStatusCodeReply(); + } + + public Long pexpire(final String key, final int milliseconds) { + checkIsInMulti(); + client.pexpire(key, milliseconds); + return client.getIntegerReply(); + } + + public Long pexpireAt(final String key, final long millisecondsTimestamp) { + checkIsInMulti(); + client.pexpireAt(key, millisecondsTimestamp); + return client.getIntegerReply(); + } + + public Long pttl(final String key) { + checkIsInMulti(); + client.pttl(key); + return client.getIntegerReply(); + } + + public Double incrByFloat(final String key, final double increment) { + checkIsInMulti(); + client.incrByFloat(key, increment); + String relpy = client.getBulkReply(); + return (relpy != null ? new Double(relpy) : null); + } + + public String psetex(final String key, final int milliseconds, final String value) { + checkIsInMulti(); + client.psetex(key, milliseconds, value); + return client.getStatusCodeReply(); + } + + public String set(final String key, final String value, final String nxxx) { + checkIsInMulti(); + client.set(key, value, nxxx); + return client.getStatusCodeReply(); + } + + public String set(final String key, final String value, final String nxxx, final String expx, final int time) { + checkIsInMulti(); + client.set(key, value, nxxx, expx, time); + return client.getStatusCodeReply(); + } + + public String clientKill(final String client) { + checkIsInMulti(); + this.client.clientKill(client); + return this.client.getStatusCodeReply(); + } + + public String clientSetname(final String name) { + checkIsInMulti(); + client.clientSetname(name); + return client.getStatusCodeReply(); + } + + public String migrate(final String host, final int port, final String key, final int destinationDb, final int timeout) { + checkIsInMulti(); + client.migrate(host, port, key, destinationDb, timeout); + return client.getStatusCodeReply(); + } + + public Double hincrByFloat(final String key, final String field, double increment) { + checkIsInMulti(); + client.hincrByFloat(key, field, increment); + String relpy = client.getBulkReply(); + return (relpy != null ? new Double(relpy) : null); } } diff --git a/src/main/java/redis/clients/jedis/JedisCommands.java b/src/main/java/redis/clients/jedis/JedisCommands.java index 806bfbb..ea5016b 100644 --- a/src/main/java/redis/clients/jedis/JedisCommands.java +++ b/src/main/java/redis/clients/jedis/JedisCommands.java @@ -14,6 +14,8 @@ public interface JedisCommands { Boolean exists(String key); + Long persist(String key); + String type(String key); Long expire(String key, int seconds); @@ -22,11 +24,13 @@ public interface JedisCommands { Long ttl(String key); - boolean setbit(String key, long offset, boolean value); + Boolean setbit(String key, long offset, boolean value); - boolean getbit(String key, long offset); + Boolean setbit(String key, long offset, String value); - long setrange(String key, long offset, String value); + Boolean getbit(String key, long offset); + + Long setrange(String key, long offset, String value); String getrange(String key, long startOffset, long endOffset); @@ -62,7 +66,7 @@ public interface JedisCommands { Boolean hexists(String key, String field); - Long hdel(String key, String field); + Long hdel(String key, String... field); Long hlen(String key); @@ -72,9 +76,9 @@ public interface JedisCommands { Map hgetAll(String key); - Long rpush(String key, String string); + Long rpush(String key, String... string); - Long lpush(String key, String string); + Long lpush(String key, String... string); Long llen(String key); @@ -92,11 +96,11 @@ public interface JedisCommands { String rpop(String key); - Long sadd(String key, String member); + Long sadd(String key, String... member); Set smembers(String key); - Long srem(String key, String member); + Long srem(String key, String... member); String spop(String key); @@ -106,11 +110,15 @@ public interface JedisCommands { String srandmember(String key); + Long strlen(String key); + Long zadd(String key, double score, String member); + + Long zadd(String key, Map scoreMembers); - Set zrange(String key, int start, int end); + Set zrange(String key, long start, long end); - Long zrem(String key, String member); + Long zrem(String key, String... member); Double zincrby(String key, double score, String member); @@ -118,11 +126,11 @@ public interface JedisCommands { Long zrevrank(String key, String member); - Set zrevrange(String key, int start, int end); + Set zrevrange(String key, long start, long end); - Set zrangeWithScores(String key, int start, int end); + Set zrangeWithScores(String key, long start, long end); - Set zrevrangeWithScores(String key, int start, int end); + Set zrevrangeWithScores(String key, long start, long end); Long zcard(String key); @@ -134,13 +142,22 @@ public interface JedisCommands { Long zcount(String key, double min, double max); + Long zcount(String key, String min, String max); + Set zrangeByScore(String key, double min, double max); + Set zrangeByScore(String key, String min, String max); + Set zrevrangeByScore(String key, double max, double min); Set zrangeByScore(String key, double min, double max, int offset, int count); + Set zrevrangeByScore(String key, String max, String min); + + Set zrangeByScore(String key, String min, String max, int offset, + int count); + Set zrevrangeByScore(String key, double max, double min, int offset, int count); @@ -150,14 +167,47 @@ public interface JedisCommands { Set zrangeByScoreWithScores(String key, double min, double max, int offset, int count); + + Set zrevrangeByScore(String key, String max, String min, + int offset, int count); + + Set zrangeByScoreWithScores(String key, String min, String max); + + Set zrevrangeByScoreWithScores(String key, String max, String min); + + Set zrangeByScoreWithScores(String key, String min, String max, + int offset, int count); Set zrevrangeByScoreWithScores(String key, double max, double min, int offset, int count); + + Set zrevrangeByScoreWithScores(String key, String max, String min, + int offset, int count); - Long zremrangeByRank(String key, int start, int end); + Long zremrangeByRank(String key, long start, long end); Long zremrangeByScore(String key, double start, double end); + + Long zremrangeByScore(String key, String start, String end); Long linsert(String key, Client.LIST_POSITION where, String pivot, String value); + + Long lpushx(String key, String... string); + + Long rpushx(String key, String... string); + + List blpop(String arg); + + List brpop(String arg); + + Long del(String key); + + String echo(String string); + + Long move(String key, int dbIndex); + + Long bitcount(final String key); + + Long bitcount(final String key, long start, long end); } diff --git a/src/main/java/redis/clients/jedis/JedisPool.java b/src/main/java/redis/clients/jedis/JedisPool.java index 0f4fe4b..1e0ead6 100644 --- a/src/main/java/redis/clients/jedis/JedisPool.java +++ b/src/main/java/redis/clients/jedis/JedisPool.java @@ -1,5 +1,7 @@ package redis.clients.jedis; +import java.net.URI; + import org.apache.commons.pool.BasePoolableObjectFactory; import org.apache.commons.pool.impl.GenericObjectPool; import org.apache.commons.pool.impl.GenericObjectPool.Config; @@ -8,32 +10,66 @@ import redis.clients.util.Pool; public class JedisPool extends Pool { - public JedisPool(final GenericObjectPool.Config poolConfig, - final String host) { - this(poolConfig, host, Protocol.DEFAULT_PORT, Protocol.DEFAULT_TIMEOUT, - null); + public JedisPool(final Config poolConfig, final String host) { + this(poolConfig, host, Protocol.DEFAULT_PORT, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE); } public JedisPool(String host, int port) { - super(new Config(), new JedisFactory(host, port, - Protocol.DEFAULT_TIMEOUT, null)); + this(new Config(), host, port, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE); } + public JedisPool(final String host) { + URI uri = URI.create(host); + if (uri.getScheme() != null && uri.getScheme().equals("redis")) { + String h = uri.getHost(); + int port = uri.getPort(); + String password = uri.getUserInfo().split(":", 2)[1]; + int database = Integer.parseInt(uri.getPath().split("/", 2)[1]); + this.internalPool = new GenericObjectPool(new JedisFactory(h, port, + Protocol.DEFAULT_TIMEOUT, password, database), new Config()); + } else { + this.internalPool = new GenericObjectPool(new JedisFactory(host, + Protocol.DEFAULT_PORT, Protocol.DEFAULT_TIMEOUT, null, + Protocol.DEFAULT_DATABASE), new Config()); + } + } + + public JedisPool(final URI uri) { + String h = uri.getHost(); + int port = uri.getPort(); + String password = uri.getUserInfo().split(":", 2)[1]; + int database = Integer.parseInt(uri.getPath().split("/", 2)[1]); + this.internalPool = new GenericObjectPool(new JedisFactory(h, port, + Protocol.DEFAULT_TIMEOUT, password, database), new Config()); + } + public JedisPool(final Config poolConfig, final String host, int port, int timeout, final String password) { - super(poolConfig, new JedisFactory(host, port, timeout, password)); + this(poolConfig, host, port, timeout, password, Protocol.DEFAULT_DATABASE); } - public JedisPool(final GenericObjectPool.Config poolConfig, - final String host, final int port) { - this(poolConfig, host, port, Protocol.DEFAULT_TIMEOUT, null); + public JedisPool(final Config poolConfig, final String host, final int port) { + this(poolConfig, host, port, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE); } - public JedisPool(final GenericObjectPool.Config poolConfig, - final String host, final int port, final int timeout) { - this(poolConfig, host, port, timeout, null); + public JedisPool(final Config poolConfig, final String host, final int port, final int timeout) { + this(poolConfig, host, port, timeout, null, Protocol.DEFAULT_DATABASE); } + public JedisPool(final Config poolConfig, final String host, int port, int timeout, final String password, + final int database) { + super(poolConfig, new JedisFactory(host, port, timeout, password, database)); + } + + + public void returnBrokenResource(final BinaryJedis resource) { + returnBrokenResourceObject(resource); + } + + public void returnResource(final BinaryJedis resource) { + returnResourceObject(resource); + } + /** * PoolableObjectFactory custom impl. */ @@ -42,14 +78,16 @@ public class JedisPool extends Pool { private final int port; private final int timeout; private final String password; + private final int database; public JedisFactory(final String host, final int port, - final int timeout, final String password) { + final int timeout, final String password, final int database) { super(); this.host = host; this.port = port; this.timeout = timeout; this.password = password; + this.database = database; } public Object makeObject() throws Exception { @@ -59,8 +97,22 @@ public class JedisPool extends Pool { if (null != this.password) { jedis.auth(this.password); } + if( database != 0 ) { + jedis.select(database); + } + return jedis; } + + @Override + public void activateObject(Object obj) throws Exception { + if (obj instanceof Jedis) { + final Jedis jedis = (Jedis)obj; + if (jedis.getDB() != database) { + jedis.select(database); + } + } + } public void destroyObject(final Object obj) throws Exception { if (obj instanceof Jedis) { @@ -91,6 +143,5 @@ public class JedisPool extends Pool { return false; } } - } } diff --git a/src/main/java/redis/clients/jedis/JedisShardInfo.java b/src/main/java/redis/clients/jedis/JedisShardInfo.java index 75b71af..3c9f84d 100644 --- a/src/main/java/redis/clients/jedis/JedisShardInfo.java +++ b/src/main/java/redis/clients/jedis/JedisShardInfo.java @@ -1,11 +1,13 @@ package redis.clients.jedis; +import java.net.URI; + import redis.clients.util.ShardInfo; import redis.clients.util.Sharded; public class JedisShardInfo extends ShardInfo { public String toString() { - return host + ":" + port + "*" + getWeight(); + return host + ":" + port + "*" + getWeight(); } private int timeout; @@ -15,67 +17,83 @@ public class JedisShardInfo extends ShardInfo { private String name = null; public String getHost() { - return host; + return host; } public int getPort() { - return port; + return port; } public JedisShardInfo(String host) { - this(host, Protocol.DEFAULT_PORT); + super(Sharded.DEFAULT_WEIGHT); + URI uri = URI.create(host); + if (uri.getScheme() != null && uri.getScheme().equals("redis")) { + this.host = uri.getHost(); + this.port = uri.getPort(); + this.password = uri.getUserInfo().split(":", 2)[1]; + } else { + this.host = host; + this.port = Protocol.DEFAULT_PORT; + } } public JedisShardInfo(String host, String name) { - this(host, Protocol.DEFAULT_PORT, name); + this(host, Protocol.DEFAULT_PORT, name); } public JedisShardInfo(String host, int port) { - this(host, port, 2000); + this(host, port, 2000); } public JedisShardInfo(String host, int port, String name) { - this(host, port, 2000, name); + this(host, port, 2000, name); } public JedisShardInfo(String host, int port, int timeout) { - this(host, port, timeout, Sharded.DEFAULT_WEIGHT); + this(host, port, timeout, Sharded.DEFAULT_WEIGHT); } public JedisShardInfo(String host, int port, int timeout, String name) { - this(host, port, timeout, Sharded.DEFAULT_WEIGHT); - this.name = name; + this(host, port, timeout, Sharded.DEFAULT_WEIGHT); + this.name = name; } public JedisShardInfo(String host, int port, int timeout, int weight) { - super(weight); - this.host = host; - this.port = port; - this.timeout = timeout; + super(weight); + this.host = host; + this.port = port; + this.timeout = timeout; + } + + public JedisShardInfo(URI uri) { + super(Sharded.DEFAULT_WEIGHT); + this.host = uri.getHost(); + this.port = uri.getPort(); + this.password = uri.getUserInfo().split(":", 2)[1]; } public String getPassword() { - return password; + return password; } public void setPassword(String auth) { - this.password = auth; + this.password = auth; } public int getTimeout() { - return timeout; + return timeout; } public void setTimeout(int timeout) { - this.timeout = timeout; + this.timeout = timeout; } public String getName() { - return name; + return name; } @Override public Jedis createResource() { - return new Jedis(this); + return new Jedis(this); } } diff --git a/src/main/java/redis/clients/jedis/MultiKeyBinaryCommands.java b/src/main/java/redis/clients/jedis/MultiKeyBinaryCommands.java new file mode 100644 index 0000000..1b3c158 --- /dev/null +++ b/src/main/java/redis/clients/jedis/MultiKeyBinaryCommands.java @@ -0,0 +1,73 @@ +package redis.clients.jedis; + + +import java.util.List; +import java.util.Set; + +public interface MultiKeyBinaryCommands { + Long del(byte[]... keys); + + List blpop(int timeout, byte[]... keys); + + List brpop(int timeout, byte[]... keys); + + List blpop(byte[]... args); + + List brpop(byte[]... args); + + Set keys(byte[] pattern); + + List mget(byte[]... keys); + + String mset(byte[]... keysvalues); + + Long msetnx(byte[]... keysvalues); + + String rename(byte[] oldkey, byte[] newkey); + + Long renamenx(byte[] oldkey, byte[] newkey); + + byte[] rpoplpush(byte[] srckey, byte[] dstkey); + + Set sdiff(byte[]... keys); + + Long sdiffstore(byte[] dstkey, byte[]... keys); + + Set sinter(byte[]... keys); + + Long sinterstore(byte[] dstkey, byte[]... keys); + + Long smove(byte[] srckey, byte[] dstkey, byte[] member); + + Long sort(byte[] key, SortingParams sortingParameters, byte[] dstkey); + + Long sort(byte[] key, byte[] dstkey); + + Set sunion(byte[]... keys); + + Long sunionstore(byte[] dstkey, byte[]... keys); + + String watch(byte[]... keys); + + String unwatch(); + + Long zinterstore(byte[] dstkey, byte[]... sets); + + Long zinterstore(byte[] dstkey, ZParams params, byte[]... sets); + + Long zunionstore(byte[] dstkey, byte[]... sets); + + Long zunionstore(byte[] dstkey, ZParams params, byte[]... sets); + + byte[] brpoplpush(byte[] source, byte[] destination, int timeout); + + Long publish(byte[] channel, byte[] message); + + void subscribe(BinaryJedisPubSub jedisPubSub, byte[]... channels); + + void psubscribe(BinaryJedisPubSub jedisPubSub, byte[]... patterns); + + byte[] randomBinaryKey(); + + Long bitop(BitOP op, final byte[] destKey, byte[]... srcKeys); +} diff --git a/src/main/java/redis/clients/jedis/MultiKeyBinaryRedisPipeline.java b/src/main/java/redis/clients/jedis/MultiKeyBinaryRedisPipeline.java new file mode 100644 index 0000000..d77ddd9 --- /dev/null +++ b/src/main/java/redis/clients/jedis/MultiKeyBinaryRedisPipeline.java @@ -0,0 +1,67 @@ +package redis.clients.jedis; + + +import java.util.List; +import java.util.Set; + +/** + * Multikey related commands (these are split out because they are non-shardable) + */ +public interface MultiKeyBinaryRedisPipeline { + + Response del(byte[]... keys); + + Response> blpop(byte[]... args); + + Response> brpop(byte[]... args); + + Response> keys(byte[] pattern); + + Response> mget(byte[]... keys); + + Response mset(byte[]... keysvalues); + + Response msetnx(byte[]... keysvalues); + + Response rename(byte[] oldkey, byte[] newkey); + + Response renamenx(byte[] oldkey, byte[] newkey); + + Response rpoplpush(byte[] srckey, byte[] dstkey); + + Response> sdiff(byte[]... keys); + + Response sdiffstore(byte[] dstkey, byte[]... keys); + + Response> sinter(byte[]... keys); + + Response sinterstore(byte[] dstkey, byte[]... keys); + + Response smove(byte[] srckey, byte[] dstkey, byte[] member); + + Response sort(byte[] key, SortingParams sortingParameters, byte[] dstkey); + + Response sort(byte[] key, byte[] dstkey); + + Response> sunion(byte[]... keys); + + Response sunionstore(byte[] dstkey, byte[]... keys); + + Response watch(byte[]... keys); + + Response zinterstore(byte[] dstkey, byte[]... sets); + + Response zinterstore(byte[] dstkey, ZParams params, byte[]... sets); + + Response zunionstore(byte[] dstkey, byte[]... sets); + + Response zunionstore(byte[] dstkey, ZParams params, byte[]... sets); + + Response brpoplpush(byte[] source, byte[] destination, int timeout); + + Response publish(byte[] channel, byte[] message); + + Response randomKeyBinary(); + + Response bitop(BitOP op, final byte[] destKey, byte[]... srcKeys); +} diff --git a/src/main/java/redis/clients/jedis/MultiKeyCommands.java b/src/main/java/redis/clients/jedis/MultiKeyCommands.java new file mode 100644 index 0000000..c98017f --- /dev/null +++ b/src/main/java/redis/clients/jedis/MultiKeyCommands.java @@ -0,0 +1,73 @@ +package redis.clients.jedis; + + +import java.util.List; +import java.util.Set; + +public interface MultiKeyCommands { + Long del(String... keys); + + List blpop(int timeout, String... keys); + + List brpop(int timeout, String... keys); + + List blpop(String... args); + + List brpop(String... args); + + Set keys(String pattern); + + List mget(String... keys); + + String mset(String... keysvalues); + + Long msetnx(String... keysvalues); + + String rename(String oldkey, String newkey); + + Long renamenx(String oldkey, String newkey); + + String rpoplpush(String srckey, String dstkey); + + Set sdiff(String... keys); + + Long sdiffstore(String dstkey, String... keys); + + Set sinter(String... keys); + + Long sinterstore(String dstkey, String... keys); + + Long smove(String srckey, String dstkey, String member); + + Long sort(String key, SortingParams sortingParameters, String dstkey); + + Long sort(String key, String dstkey); + + Set sunion(String... keys); + + Long sunionstore(String dstkey, String... keys); + + String watch(String... keys); + + String unwatch(); + + Long zinterstore(String dstkey, String... sets); + + Long zinterstore(String dstkey, ZParams params, String... sets); + + Long zunionstore(String dstkey, String... sets); + + Long zunionstore(String dstkey, ZParams params, String... sets); + + String brpoplpush(String source, String destination, int timeout); + + Long publish(String channel, String message); + + void subscribe(JedisPubSub jedisPubSub, String... channels); + + void psubscribe(JedisPubSub jedisPubSub, String... patterns); + + String randomKey(); + + Long bitop(BitOP op, final String destKey, String... srcKeys); +} diff --git a/src/main/java/redis/clients/jedis/MultiKeyCommandsPipeline.java b/src/main/java/redis/clients/jedis/MultiKeyCommandsPipeline.java new file mode 100644 index 0000000..ee9af65 --- /dev/null +++ b/src/main/java/redis/clients/jedis/MultiKeyCommandsPipeline.java @@ -0,0 +1,67 @@ +package redis.clients.jedis; + + +import java.util.List; +import java.util.Set; + + +/** + * Multikey related commands (these are split out because they are non-shardable) + */ +public interface MultiKeyCommandsPipeline { + Response del(String... keys); + + Response> blpop(String... args); + + Response> brpop(String... args); + + Response> keys(String pattern); + + Response> mget(String... keys); + + Response mset(String... keysvalues); + + Response msetnx(String... keysvalues); + + Response rename(String oldkey, String newkey); + + Response renamenx(String oldkey, String newkey); + + Response rpoplpush(String srckey, String dstkey); + + Response> sdiff(String... keys); + + Response sdiffstore(String dstkey, String... keys); + + Response> sinter(String... keys); + + Response sinterstore(String dstkey, String... keys); + + Response smove(String srckey, String dstkey, String member); + + Response sort(String key, SortingParams sortingParameters, String dstkey); + + Response sort(String key, String dstkey); + + Response> sunion(String... keys); + + Response sunionstore(String dstkey, String... keys); + + Response watch(String... keys); + + Response zinterstore(String dstkey, String... sets); + + Response zinterstore(String dstkey, ZParams params, String... sets); + + Response zunionstore(String dstkey, String... sets); + + Response zunionstore(String dstkey, ZParams params, String... sets); + + Response brpoplpush(String source, String destination, int timeout); + + Response publish(String channel, String message); + + Response randomKey(); + + Response bitop(BitOP op, final String destKey, String... srcKeys); +} diff --git a/src/main/java/redis/clients/jedis/MultiKeyPipelineBase.java b/src/main/java/redis/clients/jedis/MultiKeyPipelineBase.java new file mode 100644 index 0000000..be9d89a --- /dev/null +++ b/src/main/java/redis/clients/jedis/MultiKeyPipelineBase.java @@ -0,0 +1,401 @@ +package redis.clients.jedis; + +import java.util.List; +import java.util.Map; +import java.util.Set; + +abstract class MultiKeyPipelineBase extends PipelineBase implements + BasicRedisPipeline, + MultiKeyBinaryRedisPipeline, + MultiKeyCommandsPipeline { + + protected Client client = null; + + public Response> brpop(String... args) { + client.brpop(args); + return getResponse(BuilderFactory.STRING_LIST); + } + + public Response> brpop(int timeout, String... keys) { + client.brpop(timeout, keys); + return getResponse(BuilderFactory.STRING_LIST); + } + + public Response> blpop(String... args) { + client.blpop(args); + return getResponse(BuilderFactory.STRING_LIST); + } + + public Response> blpop(int timeout, String... keys) { + client.blpop(timeout, keys); + return getResponse(BuilderFactory.STRING_LIST); + } + + public Response> blpopMap(int timeout, String... keys) { + client.blpop(timeout, keys); + return getResponse(BuilderFactory.STRING_MAP); + } + + public Response> brpop(byte[]... args) { + client.brpop(args); + return getResponse(BuilderFactory.BYTE_ARRAY_LIST); + } + + public Response> brpop(int timeout, byte[]... keys) { + client.brpop(timeout, keys); + return getResponse(BuilderFactory.STRING_LIST); + } + + public Response> brpopMap(int timeout, String... keys) { + client.blpop(timeout, keys); + return getResponse(BuilderFactory.STRING_MAP); + } + + public Response> blpop(byte[]... args) { + client.blpop(args); + return getResponse(BuilderFactory.BYTE_ARRAY_LIST); + } + + public Response> blpop(int timeout, byte[]... keys) { + client.blpop(timeout, keys); + return getResponse(BuilderFactory.STRING_LIST); + } + + public Response del(String... keys) { + client.del(keys); + return getResponse(BuilderFactory.LONG); + } + + public Response del(byte[]... keys) { + client.del(keys); + return getResponse(BuilderFactory.LONG); + } + + public Response> keys(String pattern) { + getClient(pattern).keys(pattern); + return getResponse(BuilderFactory.STRING_SET); + } + + public Response> keys(byte[] pattern) { + getClient(pattern).keys(pattern); + return getResponse(BuilderFactory.BYTE_ARRAY_ZSET); + } + + public Response> mget(String... keys) { + client.mget(keys); + return getResponse(BuilderFactory.STRING_LIST); + } + + public Response> mget(byte[]... keys) { + client.mget(keys); + return getResponse(BuilderFactory.BYTE_ARRAY_LIST); + } + + public Response mset(String... keysvalues) { + client.mset(keysvalues); + return getResponse(BuilderFactory.STRING); + } + + public Response mset(byte[]... keysvalues) { + client.mset(keysvalues); + return getResponse(BuilderFactory.STRING); + } + + public Response msetnx(String... keysvalues) { + client.msetnx(keysvalues); + return getResponse(BuilderFactory.LONG); + } + + public Response msetnx(byte[]... keysvalues) { + client.msetnx(keysvalues); + return getResponse(BuilderFactory.LONG); + } + + public Response rename(String oldkey, String newkey) { + client.rename(oldkey, newkey); + return getResponse(BuilderFactory.STRING); + } + + public Response rename(byte[] oldkey, byte[] newkey) { + client.rename(oldkey, newkey); + return getResponse(BuilderFactory.STRING); + } + + public Response renamenx(String oldkey, String newkey) { + client.renamenx(oldkey, newkey); + return getResponse(BuilderFactory.LONG); + } + + public Response renamenx(byte[] oldkey, byte[] newkey) { + client.renamenx(oldkey, newkey); + return getResponse(BuilderFactory.LONG); + } + + public Response rpoplpush(String srckey, String dstkey) { + client.rpoplpush(srckey, dstkey); + return getResponse(BuilderFactory.STRING); + } + + public Response rpoplpush(byte[] srckey, byte[] dstkey) { + client.rpoplpush(srckey, dstkey); + return getResponse(BuilderFactory.BYTE_ARRAY); + } + + public Response> sdiff(String... keys) { + client.sdiff(keys); + return getResponse(BuilderFactory.STRING_SET); + } + + public Response> sdiff(byte[]... keys) { + client.sdiff(keys); + return getResponse(BuilderFactory.BYTE_ARRAY_ZSET); + } + + public Response sdiffstore(String dstkey, String... keys) { + client.sdiffstore(dstkey, keys); + return getResponse(BuilderFactory.LONG); + } + + public Response sdiffstore(byte[] dstkey, byte[]... keys) { + client.sdiffstore(dstkey, keys); + return getResponse(BuilderFactory.LONG); + } + + public Response> sinter(String... keys) { + client.sinter(keys); + return getResponse(BuilderFactory.STRING_SET); + } + + public Response> sinter(byte[]... keys) { + client.sinter(keys); + return getResponse(BuilderFactory.BYTE_ARRAY_ZSET); + } + + public Response sinterstore(String dstkey, String... keys) { + client.sinterstore(dstkey, keys); + return getResponse(BuilderFactory.LONG); + } + + public Response sinterstore(byte[] dstkey, byte[]... keys) { + client.sinterstore(dstkey, keys); + return getResponse(BuilderFactory.LONG); + } + + public Response smove(String srckey, String dstkey, String member) { + client.smove(srckey, dstkey, member); + return getResponse(BuilderFactory.LONG); + } + + public Response smove(byte[] srckey, byte[] dstkey, byte[] member) { + client.smove(srckey, dstkey, member); + return getResponse(BuilderFactory.LONG); + } + + public Response sort(String key, + SortingParams sortingParameters, String dstkey) { + client.sort(key, sortingParameters, dstkey); + return getResponse(BuilderFactory.LONG); + } + + public Response sort(byte[] key, + SortingParams sortingParameters, byte[] dstkey) { + client.sort(key, sortingParameters, dstkey); + return getResponse(BuilderFactory.LONG); + } + + public Response sort(String key, String dstkey) { + client.sort(key, dstkey); + return getResponse(BuilderFactory.LONG); + } + + public Response sort(byte[] key, byte[] dstkey) { + client.sort(key, dstkey); + return getResponse(BuilderFactory.LONG); + } + + public Response> sunion(String... keys) { + client.sunion(keys); + return getResponse(BuilderFactory.STRING_SET); + } + + public Response> sunion(byte[]... keys) { + client.sunion(keys); + return getResponse(BuilderFactory.BYTE_ARRAY_ZSET); + } + + public Response sunionstore(String dstkey, String... keys) { + client.sunionstore(dstkey, keys); + return getResponse(BuilderFactory.LONG); + } + + public Response sunionstore(byte[] dstkey, byte[]... keys) { + client.sunionstore(dstkey, keys); + return getResponse(BuilderFactory.LONG); + } + + public Response watch(String... keys) { + client.watch(keys); + return getResponse(BuilderFactory.STRING); + } + + public Response watch(byte[]... keys) { + client.watch(keys); + return getResponse(BuilderFactory.STRING); + } + + public Response zinterstore(String dstkey, String... sets) { + client.zinterstore(dstkey, sets); + return getResponse(BuilderFactory.LONG); + } + + public Response zinterstore(byte[] dstkey, byte[]... sets) { + client.zinterstore(dstkey, sets); + return getResponse(BuilderFactory.LONG); + } + + public Response zinterstore(String dstkey, ZParams params, + String... sets) { + client.zinterstore(dstkey, params, sets); + return getResponse(BuilderFactory.LONG); + } + + public Response zinterstore(byte[] dstkey, ZParams params, + byte[]... sets) { + client.zinterstore(dstkey, params, sets); + return getResponse(BuilderFactory.LONG); + } + + public Response zunionstore(String dstkey, String... sets) { + client.zunionstore(dstkey, sets); + return getResponse(BuilderFactory.LONG); + } + + public Response zunionstore(byte[] dstkey, byte[]... sets) { + client.zunionstore(dstkey, sets); + return getResponse(BuilderFactory.LONG); + } + + public Response zunionstore(String dstkey, ZParams params, + String... sets) { + client.zunionstore(dstkey, params, sets); + return getResponse(BuilderFactory.LONG); + } + + public Response zunionstore(byte[] dstkey, ZParams params, + byte[]... sets) { + client.zunionstore(dstkey, params, sets); + return getResponse(BuilderFactory.LONG); + } + + public Response bgrewriteaof() { + client.bgrewriteaof(); + return getResponse(BuilderFactory.STRING); + } + + public Response bgsave() { + client.bgsave(); + return getResponse(BuilderFactory.STRING); + } + + public Response configGet(String pattern) { + client.configGet(pattern); + return getResponse(BuilderFactory.STRING); + } + + public Response configSet(String parameter, String value) { + client.configSet(parameter, value); + return getResponse(BuilderFactory.STRING); + } + + public Response brpoplpush(String source, String destination, + int timeout) { + client.brpoplpush(source, destination, timeout); + return getResponse(BuilderFactory.STRING); + } + + public Response brpoplpush(byte[] source, byte[] destination, + int timeout) { + client.brpoplpush(source, destination, timeout); + return getResponse(BuilderFactory.BYTE_ARRAY); + } + + public Response configResetStat() { + client.configResetStat(); + return getResponse(BuilderFactory.STRING); + } + + public Response save() { + client.save(); + return getResponse(BuilderFactory.STRING); + } + + public Response lastsave() { + client.lastsave(); + return getResponse(BuilderFactory.LONG); + } + + public Response publish(String channel, String message) { + client.publish(channel, message); + return getResponse(BuilderFactory.LONG); + } + + public Response publish(byte[] channel, byte[] message) { + client.publish(channel, message); + return getResponse(BuilderFactory.LONG); + } + + public Response randomKey() { + client.randomKey(); + return getResponse(BuilderFactory.STRING); + } + + public Response randomKeyBinary() { + client.randomKey(); + return getResponse(BuilderFactory.BYTE_ARRAY); + } + + public Response flushDB() { + client.flushDB(); + return getResponse(BuilderFactory.STRING); + } + + public Response flushAll() { + client.flushAll(); + return getResponse(BuilderFactory.STRING); + } + + public Response info() { + client.info(); + return getResponse(BuilderFactory.STRING); + } + + public Response dbSize() { + client.dbSize(); + return getResponse(BuilderFactory.LONG); + } + + public Response shutdown() { + client.shutdown(); + return getResponse(BuilderFactory.STRING); + } + + public Response ping() { + client.ping(); + return getResponse(BuilderFactory.STRING); + } + + public Response select(int index) { + client.select(index); + return getResponse(BuilderFactory.STRING); + } + + public Response bitop(BitOP op, byte[] destKey, byte[]... srcKeys) { + client.bitop(op, destKey, srcKeys); + return getResponse(BuilderFactory.LONG); + } + + public Response bitop(BitOP op, String destKey, String... srcKeys) { + client.bitop(op, destKey, srcKeys); + return getResponse(BuilderFactory.LONG); + } +} diff --git a/src/main/java/redis/clients/jedis/Pipeline.java b/src/main/java/redis/clients/jedis/Pipeline.java old mode 100644 new mode 100755 index 20aa9e5..ee680eb --- a/src/main/java/redis/clients/jedis/Pipeline.java +++ b/src/main/java/redis/clients/jedis/Pipeline.java @@ -1,19 +1,68 @@ package redis.clients.jedis; +import redis.clients.jedis.exceptions.JedisDataException; + import java.util.ArrayList; import java.util.List; -import java.util.Map; -import java.util.Set; -import redis.clients.jedis.BinaryClient.LIST_POSITION; +public class Pipeline extends MultiKeyPipelineBase { + + private MultiResponseBuilder currentMulti; + + private class MultiResponseBuilder extends Builder>{ + private List> responses = new ArrayList>(); + + @Override + public List build(Object data) { + @SuppressWarnings("unchecked") + List list = (List)data; + List values = new ArrayList(); + + if(list.size() != responses.size()){ + throw new JedisDataException("Expected data size " + responses.size() + " but was " + list.size()); + } + + for(int i=0;i response = responses.get(i); + response.set(list.get(i)); + values.add(response.get()); + } + return values; + } -public class Pipeline extends Queable { - private Client client; + public void addResponse(Response response){ + responses.add(response); + } + } + + @Override + protected Response getResponse(Builder builder) { + if(currentMulti != null){ + super.getResponse(BuilderFactory.STRING); //Expected QUEUED + + Response lr = new Response(builder); + currentMulti.addResponse(lr); + return lr; + } + else{ + return super.getResponse(builder); + } + } public void setClient(Client client) { this.client = client; } + @Override + protected Client getClient(byte[] key) { + return client; + } + + @Override + protected Client getClient(String key) { + return client; + } + /** * Syncronize pipeline by reading all responses. This operation close the * pipeline. In order to get return values from pipelined commands, capture @@ -33,1157 +82,38 @@ public class Pipeline extends Queable { * right response type (usually it is a waste of time). * * @return A list of all the responses in the order you executed them. - * @see sync */ public List syncAndReturnAll() { List unformatted = client.getAll(); List formatted = new ArrayList(); + for (Object o : unformatted) { - formatted.add(generateResponse(o).get()); + try { + formatted.add(generateResponse(o).get()); + } catch (JedisDataException e) { + formatted.add(e); + } } return formatted; } - public Response append(String key, String value) { - client.append(key, value); - return getResponse(BuilderFactory.LONG); - } - - public Response append(byte[] key, byte[] value) { - client.append(key, value); - return getResponse(BuilderFactory.LONG); - } - - public Response> blpop(String... args) { - client.blpop(args); - return getResponse(BuilderFactory.STRING_LIST); - } - - public Response> blpop(byte[]... args) { - client.blpop(args); - return getResponse(BuilderFactory.STRING_LIST); - } - - public Response> brpop(String... args) { - client.brpop(args); - return getResponse(BuilderFactory.STRING_LIST); - } - - public Response> brpop(byte[]... args) { - client.brpop(args); - return getResponse(BuilderFactory.STRING_LIST); - } - - public Response decr(String key) { - client.decr(key); - return getResponse(BuilderFactory.LONG); - } - - public Response decr(byte[] key) { - client.decr(key); - return getResponse(BuilderFactory.LONG); - } - - public Response decrBy(String key, long integer) { - client.decrBy(key, integer); - return getResponse(BuilderFactory.LONG); - } - - public Response decrBy(byte[] key, long integer) { - client.decrBy(key, integer); - return getResponse(BuilderFactory.LONG); - } - - public Response del(String... keys) { - client.del(keys); - return getResponse(BuilderFactory.LONG); - } - - public Response del(byte[]... keys) { - client.del(keys); - return getResponse(BuilderFactory.LONG); - } - - public Response echo(String string) { - client.echo(string); - return getResponse(BuilderFactory.STRING); - } - - public Response echo(byte[] string) { - client.echo(string); - return getResponse(BuilderFactory.STRING); - } - - public Response exists(String key) { - client.exists(key); - return getResponse(BuilderFactory.BOOLEAN); - } - - public Response exists(byte[] key) { - client.exists(key); - return getResponse(BuilderFactory.BOOLEAN); - } - - public Response expire(String key, int seconds) { - client.expire(key, seconds); - return getResponse(BuilderFactory.LONG); - } - - public Response expire(byte[] key, int seconds) { - client.expire(key, seconds); - return getResponse(BuilderFactory.LONG); - } - - public Response expireAt(String key, long unixTime) { - client.expireAt(key, unixTime); - return getResponse(BuilderFactory.LONG); - } - - public Response expireAt(byte[] key, long unixTime) { - client.expireAt(key, unixTime); - return getResponse(BuilderFactory.LONG); - } - - public Response get(String key) { - client.get(key); - return getResponse(BuilderFactory.STRING); - } - - public Response get(byte[] key) { - client.get(key); - return getResponse(BuilderFactory.STRING); - } - - public Response getbit(String key, long offset) { - client.getbit(key, offset); - return getResponse(BuilderFactory.BOOLEAN); - } - - public Response getrange(String key, long startOffset, - long endOffset) { - client.getrange(key, startOffset, endOffset); - return getResponse(BuilderFactory.STRING); - } - - public Response getSet(String key, String value) { - client.getSet(key, value); - return getResponse(BuilderFactory.STRING); - } - - public Response getSet(byte[] key, byte[] value) { - client.getSet(key, value); - return getResponse(BuilderFactory.STRING); - } - - public Response hdel(String key, String field) { - client.hdel(key, field); - return getResponse(BuilderFactory.LONG); - } - - public Response hdel(byte[] key, byte[] field) { - client.hdel(key, field); - return getResponse(BuilderFactory.LONG); - } - - public Response hexists(String key, String field) { - client.hexists(key, field); - return getResponse(BuilderFactory.BOOLEAN); - } - - public Response hexists(byte[] key, byte[] field) { - client.hexists(key, field); - return getResponse(BuilderFactory.BOOLEAN); - } - - public Response hget(String key, String field) { - client.hget(key, field); - return getResponse(BuilderFactory.STRING); - } - - public Response hget(byte[] key, byte[] field) { - client.hget(key, field); - return getResponse(BuilderFactory.STRING); - } - - public Response> hgetAll(String key) { - client.hgetAll(key); - return getResponse(BuilderFactory.STRING_MAP); - } - - public Response> hgetAll(byte[] key) { - client.hgetAll(key); - return getResponse(BuilderFactory.STRING_MAP); - } - - public Response hincrBy(String key, String field, long value) { - client.hincrBy(key, field, value); - return getResponse(BuilderFactory.LONG); - } - - public Response hincrBy(byte[] key, byte[] field, long value) { - client.hincrBy(key, field, value); - return getResponse(BuilderFactory.LONG); - } - - public Response> hkeys(String key) { - client.hkeys(key); - return getResponse(BuilderFactory.STRING_SET); - } - - public Response> hkeys(byte[] key) { - client.hkeys(key); - return getResponse(BuilderFactory.STRING_SET); - } - - public Response hlen(String key) { - client.hlen(key); - return getResponse(BuilderFactory.LONG); - } - - public Response hlen(byte[] key) { - client.hlen(key); - return getResponse(BuilderFactory.LONG); - } - - public Response> hmget(String key, String... fields) { - client.hmget(key, fields); - return getResponse(BuilderFactory.STRING_LIST); - } - - public Response> hmget(byte[] key, byte[]... fields) { - client.hmget(key, fields); - return getResponse(BuilderFactory.STRING_LIST); - } - - public Response hmset(String key, Map hash) { - client.hmset(key, hash); - return getResponse(BuilderFactory.STRING); - } - - public Response hmset(byte[] key, Map hash) { - client.hmset(key, hash); - return getResponse(BuilderFactory.STRING); - } - - public Response hset(String key, String field, String value) { - client.hset(key, field, value); - return getResponse(BuilderFactory.LONG); - } - - public Response hset(byte[] key, byte[] field, byte[] value) { - client.hset(key, field, value); - return getResponse(BuilderFactory.LONG); - } - - public Response hsetnx(String key, String field, String value) { - client.hsetnx(key, field, value); - return getResponse(BuilderFactory.LONG); - } - - public Response hsetnx(byte[] key, byte[] field, byte[] value) { - client.hsetnx(key, field, value); - return getResponse(BuilderFactory.LONG); - } - - public Response> hvals(String key) { - client.hvals(key); - return getResponse(BuilderFactory.STRING_LIST); - } - - public Response> hvals(byte[] key) { - client.hvals(key); - return getResponse(BuilderFactory.STRING_LIST); - } - - public Response incr(String key) { - client.incr(key); - return getResponse(BuilderFactory.LONG); - } - - public Response incr(byte[] key) { - client.incr(key); - return getResponse(BuilderFactory.LONG); - } - - public Response incrBy(String key, long integer) { - client.incrBy(key, integer); - return getResponse(BuilderFactory.LONG); - } - - public Response incrBy(byte[] key, long integer) { - client.incrBy(key, integer); - return getResponse(BuilderFactory.LONG); - } - - public Response> keys(String pattern) { - client.keys(pattern); - return getResponse(BuilderFactory.STRING_SET); - } - - public Response> keys(byte[] pattern) { - client.keys(pattern); - return getResponse(BuilderFactory.STRING_SET); - } - - public Response lindex(String key, int index) { - client.lindex(key, index); - return getResponse(BuilderFactory.STRING); - } - - public Response lindex(byte[] key, int index) { - client.lindex(key, index); - return getResponse(BuilderFactory.STRING); - } - - public Response linsert(String key, LIST_POSITION where, - String pivot, String value) { - client.linsert(key, where, pivot, value); - return getResponse(BuilderFactory.LONG); - } - - public Response linsert(byte[] key, LIST_POSITION where, - byte[] pivot, byte[] value) { - client.linsert(key, where, pivot, value); - return getResponse(BuilderFactory.LONG); - } - - public Response llen(String key) { - client.llen(key); - return getResponse(BuilderFactory.LONG); - } - - public Response llen(byte[] key) { - client.llen(key); - return getResponse(BuilderFactory.LONG); - } - - public Response lpop(String key) { - client.lpop(key); - return getResponse(BuilderFactory.STRING); - } - - public Response lpop(byte[] key) { - client.lpop(key); - return getResponse(BuilderFactory.STRING); - } - - public Response lpush(String key, String string) { - client.lpush(key, string); - return getResponse(BuilderFactory.LONG); - } - - public Response lpush(byte[] key, byte[] string) { - client.lpush(key, string); - return getResponse(BuilderFactory.LONG); - } - - public Response lpushx(String key, String string) { - client.lpushx(key, string); - return getResponse(BuilderFactory.LONG); - } - - public Response lpushx(byte[] key, byte[] bytes) { - client.lpushx(key, bytes); - return getResponse(BuilderFactory.LONG); - } - - public Response> lrange(String key, long start, long end) { - client.lrange(key, start, end); - return getResponse(BuilderFactory.STRING_LIST); - } - - public Response> lrange(byte[] key, long start, long end) { - client.lrange(key, start, end); - return getResponse(BuilderFactory.STRING_LIST); - } - - public Response lrem(String key, long count, String value) { - client.lrem(key, count, value); - return getResponse(BuilderFactory.LONG); - } - - public Response lrem(byte[] key, long count, byte[] value) { - client.lrem(key, count, value); - return getResponse(BuilderFactory.LONG); - } - - public Response lset(String key, long index, String value) { - client.lset(key, index, value); - return getResponse(BuilderFactory.STRING); - } - - public Response lset(byte[] key, long index, byte[] value) { - client.lset(key, index, value); - return getResponse(BuilderFactory.STRING); - } - - public Response ltrim(String key, long start, long end) { - client.ltrim(key, start, end); - return getResponse(BuilderFactory.STRING); - } - - public Response ltrim(byte[] key, long start, long end) { - client.ltrim(key, start, end); - return getResponse(BuilderFactory.STRING); - } - - public Response> mget(String... keys) { - client.mget(keys); - return getResponse(BuilderFactory.STRING_LIST); - } - - public Response> mget(byte[]... keys) { - client.mget(keys); - return getResponse(BuilderFactory.STRING_LIST); - } - - public Response move(String key, int dbIndex) { - client.move(key, dbIndex); - return getResponse(BuilderFactory.LONG); - } - - public Response move(byte[] key, int dbIndex) { - client.move(key, dbIndex); - return getResponse(BuilderFactory.LONG); - } - - public Response mset(String... keysvalues) { - client.mset(keysvalues); - return getResponse(BuilderFactory.STRING); - } - - public Response mset(byte[]... keysvalues) { - client.mset(keysvalues); - return getResponse(BuilderFactory.STRING); - } - - public Response msetnx(String... keysvalues) { - client.msetnx(keysvalues); - return getResponse(BuilderFactory.LONG); - } - - public Response msetnx(byte[]... keysvalues) { - client.msetnx(keysvalues); - return getResponse(BuilderFactory.LONG); - } - - public Response persist(String key) { - client.persist(key); - return getResponse(BuilderFactory.LONG); - } - - public Response persist(byte[] key) { - client.persist(key); - return getResponse(BuilderFactory.LONG); - } - - public Response rename(String oldkey, String newkey) { - client.rename(oldkey, newkey); - return getResponse(BuilderFactory.STRING); - } - - public Response rename(byte[] oldkey, byte[] newkey) { - client.rename(oldkey, newkey); - return getResponse(BuilderFactory.STRING); - } - - public Response renamenx(String oldkey, String newkey) { - client.renamenx(oldkey, newkey); - return getResponse(BuilderFactory.LONG); - } - - public Response renamenx(byte[] oldkey, byte[] newkey) { - client.renamenx(oldkey, newkey); - return getResponse(BuilderFactory.LONG); - } - - public Response rpop(String key) { - client.rpop(key); - return getResponse(BuilderFactory.STRING); - } - - public Response rpop(byte[] key) { - client.rpop(key); - return getResponse(BuilderFactory.STRING); - } - - public Response rpoplpush(String srckey, String dstkey) { - client.rpoplpush(srckey, dstkey); - return getResponse(BuilderFactory.STRING); - } - - public Response rpoplpush(byte[] srckey, byte[] dstkey) { - client.rpoplpush(srckey, dstkey); - return getResponse(BuilderFactory.STRING); - } - - public Response rpush(String key, String string) { - client.rpush(key, string); - return getResponse(BuilderFactory.LONG); - } - - public Response rpush(byte[] key, byte[] string) { - client.rpush(key, string); - return getResponse(BuilderFactory.LONG); - } - - public Response rpushx(String key, String string) { - client.rpushx(key, string); - return getResponse(BuilderFactory.LONG); - } - - public Response rpushx(byte[] key, byte[] string) { - client.rpushx(key, string); - return getResponse(BuilderFactory.LONG); - } - - public Response sadd(String key, String member) { - client.sadd(key, member); - return getResponse(BuilderFactory.LONG); - } - - public Response sadd(byte[] key, byte[] member) { - client.sadd(key, member); - return getResponse(BuilderFactory.LONG); - } - - public Response scard(String key) { - client.scard(key); - return getResponse(BuilderFactory.LONG); - } - - public Response scard(byte[] key) { - client.scard(key); - return getResponse(BuilderFactory.LONG); - } - - public Response> sdiff(String... keys) { - client.sdiff(keys); - return getResponse(BuilderFactory.STRING_SET); - } - - public Response> sdiff(byte[]... keys) { - client.sdiff(keys); - return getResponse(BuilderFactory.STRING_SET); - } - - public Response sdiffstore(String dstkey, String... keys) { - client.sdiffstore(dstkey, keys); - return getResponse(BuilderFactory.LONG); - } - - public Response sdiffstore(byte[] dstkey, byte[]... keys) { - client.sdiffstore(dstkey, keys); - return getResponse(BuilderFactory.LONG); - } - - public Response set(String key, String value) { - client.set(key, value); - return getResponse(BuilderFactory.STRING); - } - - public Response set(byte[] key, byte[] value) { - client.set(key, value); - return getResponse(BuilderFactory.STRING); - } - - public Response setbit(String key, long offset, boolean value) { - client.setbit(key, offset, value); - return getResponse(BuilderFactory.BOOLEAN); - } - - public Response setex(String key, int seconds, String value) { - client.setex(key, seconds, value); - return getResponse(BuilderFactory.STRING); - } - - public Response setex(byte[] key, int seconds, byte[] value) { - client.setex(key, seconds, value); - return getResponse(BuilderFactory.STRING); - } - - public Response setnx(String key, String value) { - client.setnx(key, value); - return getResponse(BuilderFactory.LONG); - } - - public Response setnx(byte[] key, byte[] value) { - client.setnx(key, value); - return getResponse(BuilderFactory.LONG); - } - - public Response setrange(String key, long offset, String value) { - client.setrange(key, offset, value); - return getResponse(BuilderFactory.LONG); - } - - public Response> sinter(String... keys) { - client.sinter(keys); - return getResponse(BuilderFactory.STRING_SET); - } - - public Response> sinter(byte[]... keys) { - client.sinter(keys); - return getResponse(BuilderFactory.STRING_SET); - } - - public Response sinterstore(String dstkey, String... keys) { - client.sinterstore(dstkey, keys); - return getResponse(BuilderFactory.LONG); - } - - public Response sinterstore(byte[] dstkey, byte[]... keys) { - client.sinterstore(dstkey, keys); - return getResponse(BuilderFactory.LONG); - } - - public Response sismember(String key, String member) { - client.sismember(key, member); - return getResponse(BuilderFactory.BOOLEAN); - } - - public Response sismember(byte[] key, byte[] member) { - client.sismember(key, member); - return getResponse(BuilderFactory.BOOLEAN); - } - - public Response> smembers(String key) { - client.smembers(key); - return getResponse(BuilderFactory.STRING_SET); - } - - public Response> smembers(byte[] key) { - client.smembers(key); - return getResponse(BuilderFactory.STRING_SET); - } - - public Response smove(String srckey, String dstkey, String member) { - client.smove(srckey, dstkey, member); - return getResponse(BuilderFactory.LONG); - } - - public Response smove(byte[] srckey, byte[] dstkey, byte[] member) { - client.smove(srckey, dstkey, member); - return getResponse(BuilderFactory.LONG); - } - - public Response> sort(String key) { - client.sort(key); - return getResponse(BuilderFactory.STRING_LIST); - } - - public Response> sort(byte[] key) { - client.sort(key); - return getResponse(BuilderFactory.STRING_LIST); - } - - public Response> sort(String key, - SortingParams sortingParameters) { - client.sort(key, sortingParameters); - return getResponse(BuilderFactory.STRING_LIST); - } - - public Response> sort(byte[] key, - SortingParams sortingParameters) { - client.sort(key, sortingParameters); - return getResponse(BuilderFactory.STRING_LIST); - } - - public Response> sort(String key, - SortingParams sortingParameters, String dstkey) { - client.sort(key, sortingParameters, dstkey); - return getResponse(BuilderFactory.STRING_LIST); - } - - public Response> sort(byte[] key, - SortingParams sortingParameters, byte[] dstkey) { - client.sort(key, sortingParameters, dstkey); - return getResponse(BuilderFactory.STRING_LIST); - } - - public Response> sort(String key, String dstkey) { - client.sort(key, dstkey); - return getResponse(BuilderFactory.STRING_LIST); - } - - public Response> sort(byte[] key, byte[] dstkey) { - client.sort(key, dstkey); - return getResponse(BuilderFactory.STRING_LIST); - } - - public Response spop(String key) { - client.spop(key); - return getResponse(BuilderFactory.STRING); - } - - public Response spop(byte[] key) { - client.spop(key); - return getResponse(BuilderFactory.STRING); - } - - public Response srandmember(String key) { - client.srandmember(key); - return getResponse(BuilderFactory.STRING); - } - - public Response srandmember(byte[] key) { - client.srandmember(key); - return getResponse(BuilderFactory.STRING); - } - - public Response srem(String key, String member) { - client.srem(key, member); - return getResponse(BuilderFactory.LONG); - } - - public Response srem(byte[] key, byte[] member) { - client.srem(key, member); - return getResponse(BuilderFactory.LONG); - } - - public Response strlen(String key) { - client.strlen(key); - return getResponse(BuilderFactory.LONG); - } - - public Response strlen(byte[] key) { - client.strlen(key); - return getResponse(BuilderFactory.LONG); - } - - public Response substr(String key, int start, int end) { - client.substr(key, start, end); - return getResponse(BuilderFactory.STRING); - } - - public Response substr(byte[] key, int start, int end) { - client.substr(key, start, end); - return getResponse(BuilderFactory.STRING); - } - - public Response> sunion(String... keys) { - client.sunion(keys); - return getResponse(BuilderFactory.STRING_SET); - } - - public Response> sunion(byte[]... keys) { - client.sunion(keys); - return getResponse(BuilderFactory.STRING_SET); - } - - public Response sunionstore(String dstkey, String... keys) { - client.sunionstore(dstkey, keys); - return getResponse(BuilderFactory.LONG); - } - - public Response sunionstore(byte[] dstkey, byte[]... keys) { - client.sunionstore(dstkey, keys); - return getResponse(BuilderFactory.LONG); - } - - public Response ttl(String key) { - client.ttl(key); - return getResponse(BuilderFactory.LONG); - } - - public Response ttl(byte[] key) { - client.ttl(key); - return getResponse(BuilderFactory.LONG); - } - - public Response type(String key) { - client.type(key); - return getResponse(BuilderFactory.STRING); - } - - public Response type(byte[] key) { - client.type(key); - return getResponse(BuilderFactory.STRING); - } - - public Response watch(String... keys) { - client.watch(keys); - return getResponse(BuilderFactory.STRING); - } - - public Response watch(byte[]... keys) { - client.watch(keys); - return getResponse(BuilderFactory.STRING); - } - - public Response zadd(String key, double score, String member) { - client.zadd(key, score, member); - return getResponse(BuilderFactory.LONG); - } - - public Response zadd(byte[] key, double score, byte[] member) { - client.zadd(key, score, member); - return getResponse(BuilderFactory.LONG); - } - - public Response zcard(String key) { - client.zcard(key); - return getResponse(BuilderFactory.LONG); - } - - public Response zcard(byte[] key) { - client.zcard(key); - return getResponse(BuilderFactory.LONG); - } - - public Response zcount(String key, double min, double max) { - client.zcount(key, min, max); - return getResponse(BuilderFactory.LONG); - } - - public Response zcount(byte[] key, double min, double max) { - client.zcount(key, min, max); - return getResponse(BuilderFactory.LONG); - } - - public Response zincrby(String key, double score, String member) { - client.zincrby(key, score, member); - return getResponse(BuilderFactory.DOUBLE); - } - - public Response zincrby(byte[] key, double score, byte[] member) { - client.zincrby(key, score, member); - return getResponse(BuilderFactory.DOUBLE); - } - - public Response zinterstore(String dstkey, String... sets) { - client.zinterstore(dstkey, sets); - return getResponse(BuilderFactory.LONG); - } - - public Response zinterstore(byte[] dstkey, byte[]... sets) { - client.zinterstore(dstkey, sets); - return getResponse(BuilderFactory.LONG); - } - - public Response zinterstore(String dstkey, ZParams params, - String... sets) { - client.zinterstore(dstkey, params, sets); - return getResponse(BuilderFactory.LONG); - } - - public Response zinterstore(byte[] dstkey, ZParams params, - byte[]... sets) { - client.zinterstore(dstkey, params, sets); - return getResponse(BuilderFactory.LONG); - } - - public Response> zrange(String key, int start, int end) { - client.zrange(key, start, end); - return getResponse(BuilderFactory.STRING_ZSET); - } - - public Response> zrange(byte[] key, int start, int end) { - client.zrange(key, start, end); - return getResponse(BuilderFactory.STRING_ZSET); - } - - public Response> zrangeByScore(String key, double min, - double max) { - client.zrangeByScore(key, min, max); - return getResponse(BuilderFactory.STRING_ZSET); - } - - public Response> zrangeByScore(byte[] key, double min, - double max) { - client.zrangeByScore(key, min, max); - return getResponse(BuilderFactory.STRING_ZSET); - } - - public Response> zrangeByScore(String key, String min, - String max) { - client.zrangeByScore(key, min, max); - return getResponse(BuilderFactory.STRING_ZSET); - } - - public Response> zrangeByScore(byte[] key, byte[] min, - byte[] max) { - client.zrangeByScore(key, min, max); - return getResponse(BuilderFactory.STRING_ZSET); - } - - public Response> zrangeByScore(String key, double min, - double max, int offset, int count) { - client.zrangeByScore(key, min, max, offset, count); - return getResponse(BuilderFactory.STRING_ZSET); - } - - public Response> zrangeByScore(byte[] key, double min, - double max, int offset, int count) { - client.zrangeByScore(key, min, max, offset, count); - return getResponse(BuilderFactory.STRING_ZSET); - } - - public Response> zrangeByScoreWithScores(String key, double min, - double max) { - client.zrangeByScoreWithScores(key, min, max); - return getResponse(BuilderFactory.TUPLE_ZSET); - } - - public Response> zrangeByScoreWithScores(byte[] key, double min, - double max) { - client.zrangeByScoreWithScores(key, min, max); - return getResponse(BuilderFactory.TUPLE_ZSET); - } - - public Response> zrangeByScoreWithScores(String key, double min, - double max, int offset, int count) { - client.zrangeByScoreWithScores(key, min, max, offset, count); - return getResponse(BuilderFactory.TUPLE_ZSET); - } - - public Response> zrangeByScoreWithScores(byte[] key, double min, - double max, int offset, int count) { - client.zrangeByScoreWithScores(key, min, max, offset, count); - return getResponse(BuilderFactory.TUPLE_ZSET); - } - - public Response> zrevrangeByScore(String key, double max, - double min) { - client.zrevrangeByScore(key, max, min); - return getResponse(BuilderFactory.STRING_ZSET); - } - - public Response> zrevrangeByScore(byte[] key, double max, - double min) { - client.zrevrangeByScore(key, max, min); - return getResponse(BuilderFactory.STRING_ZSET); - } - - public Response> zrevrangeByScore(String key, String max, - String min) { - client.zrevrangeByScore(key, max, min); - return getResponse(BuilderFactory.STRING_ZSET); - } - - public Response> zrevrangeByScore(byte[] key, byte[] max, - byte[] min) { - client.zrevrangeByScore(key, max, min); - return getResponse(BuilderFactory.STRING_ZSET); - } - - public Response> zrevrangeByScore(String key, double max, - double min, int offset, int count) { - client.zrevrangeByScore(key, max, min, offset, count); - return getResponse(BuilderFactory.STRING_ZSET); - } - - public Response> zrevrangeByScore(byte[] key, double max, - double min, int offset, int count) { - client.zrevrangeByScore(key, max, min, offset, count); - return getResponse(BuilderFactory.STRING_ZSET); - } - - public Response> zrevrangeByScoreWithScores(String key, - double max, double min) { - client.zrevrangeByScoreWithScores(key, max, min); - return getResponse(BuilderFactory.TUPLE_ZSET); - } - - public Response> zrevrangeByScoreWithScores(byte[] key, - double max, double min) { - client.zrevrangeByScoreWithScores(key, max, min); - return getResponse(BuilderFactory.TUPLE_ZSET); - } - - public Response> zrevrangeByScoreWithScores(String key, - double max, double min, int offset, int count) { - client.zrevrangeByScoreWithScores(key, max, min, offset, count); - return getResponse(BuilderFactory.TUPLE_ZSET); - } - - public Response> zrevrangeByScoreWithScores(byte[] key, - double max, double min, int offset, int count) { - client.zrevrangeByScoreWithScores(key, max, min, offset, count); - return getResponse(BuilderFactory.TUPLE_ZSET); - } - - public Response> zrangeWithScores(String key, int start, int end) { - client.zrangeWithScores(key, start, end); - return getResponse(BuilderFactory.TUPLE_ZSET); - } - - public Response> zrangeWithScores(byte[] key, int start, int end) { - client.zrangeWithScores(key, start, end); - return getResponse(BuilderFactory.TUPLE_ZSET); - } - - public Response zrank(String key, String member) { - client.zrank(key, member); - return getResponse(BuilderFactory.LONG); - } - - public Response zrank(byte[] key, byte[] member) { - client.zrank(key, member); - return getResponse(BuilderFactory.LONG); - } - - public Response zrem(String key, String member) { - client.zrem(key, member); - return getResponse(BuilderFactory.LONG); - } - - public Response zrem(byte[] key, byte[] member) { - client.zrem(key, member); - return getResponse(BuilderFactory.LONG); - } - - public Response zremrangeByRank(String key, int start, int end) { - client.zremrangeByRank(key, start, end); - return getResponse(BuilderFactory.LONG); - } - - public Response zremrangeByRank(byte[] key, int start, int end) { - client.zremrangeByRank(key, start, end); - return getResponse(BuilderFactory.LONG); - } - - public Response zremrangeByScore(String key, double start, double end) { - client.zremrangeByScore(key, start, end); - return getResponse(BuilderFactory.LONG); - } - - public Response zremrangeByScore(byte[] key, double start, double end) { - client.zremrangeByScore(key, start, end); - return getResponse(BuilderFactory.LONG); - } - - public Response> zrevrange(String key, int start, int end) { - client.zrevrange(key, start, end); - return getResponse(BuilderFactory.STRING_ZSET); - } - - public Response> zrevrange(byte[] key, int start, int end) { - client.zrevrange(key, start, end); - return getResponse(BuilderFactory.STRING_ZSET); - } - - public Response> zrevrangeWithScores(String key, int start, - int end) { - client.zrevrangeWithScores(key, start, end); - return getResponse(BuilderFactory.TUPLE_ZSET); - } - - public Response> zrevrangeWithScores(byte[] key, int start, - int end) { - client.zrevrangeWithScores(key, start, end); - return getResponse(BuilderFactory.TUPLE_ZSET); - } - - public Response zrevrank(String key, String member) { - client.zrevrank(key, member); - return getResponse(BuilderFactory.LONG); - } - - public Response zrevrank(byte[] key, byte[] member) { - client.zrevrank(key, member); - return getResponse(BuilderFactory.LONG); - } - - public Response zscore(String key, String member) { - client.zscore(key, member); - return getResponse(BuilderFactory.DOUBLE); - } - - public Response zscore(byte[] key, byte[] member) { - client.zscore(key, member); - return getResponse(BuilderFactory.DOUBLE); - } - - public Response zunionstore(String dstkey, String... sets) { - client.zunionstore(dstkey, sets); - return getResponse(BuilderFactory.LONG); - } - - public Response zunionstore(byte[] dstkey, byte[]... sets) { - client.zunionstore(dstkey, sets); - return getResponse(BuilderFactory.LONG); - } - - public Response zunionstore(String dstkey, ZParams params, - String... sets) { - client.zunionstore(dstkey, params, sets); - return getResponse(BuilderFactory.LONG); - } - - public Response zunionstore(byte[] dstkey, ZParams params, - byte[]... sets) { - client.zunionstore(dstkey, params, sets); - return getResponse(BuilderFactory.LONG); - } - - public Response bgrewriteaof() { - client.bgrewriteaof(); - return getResponse(BuilderFactory.STRING); - } - - public Response bgsave() { - client.bgsave(); - return getResponse(BuilderFactory.STRING); - } - - public Response configGet(String pattern) { - client.configGet(pattern); - return getResponse(BuilderFactory.STRING); - } - - public Response configSet(String parameter, String value) { - client.configSet(parameter, value); - return getResponse(BuilderFactory.STRING); - } - - public Response brpoplpush(String source, String destination, - int timeout) { - client.brpoplpush(source, destination, timeout); - return getResponse(BuilderFactory.STRING); - } - - public Response brpoplpush(byte[] source, byte[] destination, - int timeout) { - client.brpoplpush(source, destination, timeout); - return getResponse(BuilderFactory.STRING); - } - - public Response configResetStat() { - client.configResetStat(); - return getResponse(BuilderFactory.STRING); - } - - public Response save() { - client.save(); - return getResponse(BuilderFactory.STRING); - } - - public Response lastsave() { - client.lastsave(); - return getResponse(BuilderFactory.LONG); - } - public Response discard() { client.discard(); return getResponse(BuilderFactory.STRING); } - public void exec() { + public Response> exec() { client.exec(); + Response> response = super.getResponse(currentMulti); + currentMulti = null; + return response; } - public void multi() { + public Response multi() { client.multi(); + Response response = getResponse(BuilderFactory.STRING); //Expecting OK + currentMulti = new MultiResponseBuilder(); + return response; } - public Response publish(String channel, String message) { - client.publish(channel, message); - return getResponse(BuilderFactory.LONG); - } - - public Response publish(byte[] channel, byte[] message) { - client.publish(channel, message); - return getResponse(BuilderFactory.LONG); - } } diff --git a/src/main/java/redis/clients/jedis/PipelineBase.java b/src/main/java/redis/clients/jedis/PipelineBase.java new file mode 100644 index 0000000..315716c --- /dev/null +++ b/src/main/java/redis/clients/jedis/PipelineBase.java @@ -0,0 +1,1162 @@ +package redis.clients.jedis; + +import redis.clients.jedis.BinaryClient.LIST_POSITION; + +import java.util.List; +import java.util.Map; +import java.util.Set; + +import static redis.clients.jedis.Protocol.toByteArray; + +abstract class PipelineBase extends Queable implements + BinaryRedisPipeline, + RedisPipeline { + + protected abstract Client getClient(String key); + + protected abstract Client getClient(byte[] key); + + public Response append(String key, String value) { + getClient(key).append(key, value); + return getResponse(BuilderFactory.LONG); + } + + public Response append(byte[] key, byte[] value) { + getClient(key).append(key, value); + return getResponse(BuilderFactory.LONG); + } + + public Response> blpop(String key) { + String[] temp = new String[1]; + temp[0] = key; + getClient(key).blpop(temp); + return getResponse(BuilderFactory.STRING_LIST); + } + + public Response> brpop(String key) { + String[] temp = new String[1]; + temp[0] = key; + getClient(key).brpop(temp); + return getResponse(BuilderFactory.STRING_LIST); + } + + public Response> blpop(byte[] key) { + byte[][] temp = new byte[1][]; + temp[0] = key; + getClient(key).blpop(temp); + return getResponse(BuilderFactory.BYTE_ARRAY_LIST); + } + + public Response> brpop(byte[] key) { + byte[][] temp = new byte[1][]; + temp[0] = key; + getClient(key).brpop(temp); + return getResponse(BuilderFactory.BYTE_ARRAY_LIST); + } + + public Response decr(String key) { + getClient(key).decr(key); + return getResponse(BuilderFactory.LONG); + } + + public Response decr(byte[] key) { + getClient(key).decr(key); + return getResponse(BuilderFactory.LONG); + } + + public Response decrBy(String key, long integer) { + getClient(key).decrBy(key, integer); + return getResponse(BuilderFactory.LONG); + } + + public Response decrBy(byte[] key, long integer) { + getClient(key).decrBy(key, integer); + return getResponse(BuilderFactory.LONG); + } + + public Response del(String key) { + getClient(key).del(key); + return getResponse(BuilderFactory.LONG); + } + + public Response del(byte[] key) { + getClient(key).del(key); + return getResponse(BuilderFactory.LONG); + } + + public Response echo(String string) { + getClient(string).echo(string); + return getResponse(BuilderFactory.STRING); + } + + public Response echo(byte[] string) { + getClient(string).echo(string); + return getResponse(BuilderFactory.BYTE_ARRAY); + } + + public Response exists(String key) { + getClient(key).exists(key); + return getResponse(BuilderFactory.BOOLEAN); + } + + public Response exists(byte[] key) { + getClient(key).exists(key); + return getResponse(BuilderFactory.BOOLEAN); + } + + public Response expire(String key, int seconds) { + getClient(key).expire(key, seconds); + return getResponse(BuilderFactory.LONG); + } + + public Response expire(byte[] key, int seconds) { + getClient(key).expire(key, seconds); + return getResponse(BuilderFactory.LONG); + } + + public Response expireAt(String key, long unixTime) { + getClient(key).expireAt(key, unixTime); + return getResponse(BuilderFactory.LONG); + } + + public Response expireAt(byte[] key, long unixTime) { + getClient(key).expireAt(key, unixTime); + return getResponse(BuilderFactory.LONG); + } + + public Response get(String key) { + getClient(key).get(key); + return getResponse(BuilderFactory.STRING); + } + + public Response get(byte[] key) { + getClient(key).get(key); + return getResponse(BuilderFactory.BYTE_ARRAY); + } + + public Response getbit(String key, long offset) { + getClient(key).getbit(key, offset); + return getResponse(BuilderFactory.BOOLEAN); + } + + public Response getbit(byte[] key, long offset) { + getClient(key).getbit(key, offset); + return getResponse(BuilderFactory.BOOLEAN); + } + + public Response getrange(String key, long startOffset, + long endOffset) { + getClient(key).getrange(key, startOffset, endOffset); + return getResponse(BuilderFactory.STRING); + } + + public Response getSet(String key, String value) { + getClient(key).getSet(key, value); + return getResponse(BuilderFactory.STRING); + } + + public Response getSet(byte[] key, byte[] value) { + getClient(key).getSet(key, value); + return getResponse(BuilderFactory.BYTE_ARRAY); + } + + public Response getrange(byte[] key, long startOffset, long endOffset) { + getClient(key).getrange(key, startOffset, endOffset); + return getResponse(BuilderFactory.LONG); + } + + public Response hdel(String key, String... field) { + getClient(key).hdel(key, field); + return getResponse(BuilderFactory.LONG); + } + + public Response hdel(byte[] key, byte[]... field) { + getClient(key).hdel(key, field); + return getResponse(BuilderFactory.LONG); + } + + public Response hexists(String key, String field) { + getClient(key).hexists(key, field); + return getResponse(BuilderFactory.BOOLEAN); + } + + public Response hexists(byte[] key, byte[] field) { + getClient(key).hexists(key, field); + return getResponse(BuilderFactory.BOOLEAN); + } + + public Response hget(String key, String field) { + getClient(key).hget(key, field); + return getResponse(BuilderFactory.STRING); + } + + public Response hget(byte[] key, byte[] field) { + getClient(key).hget(key, field); + return getResponse(BuilderFactory.BYTE_ARRAY); + } + + public Response> hgetAll(String key) { + getClient(key).hgetAll(key); + return getResponse(BuilderFactory.STRING_MAP); + } + + public Response> hgetAll(byte[] key) { + getClient(key).hgetAll(key); + return getResponse(BuilderFactory.BYTE_ARRAY_MAP); + } + + public Response hincrBy(String key, String field, long value) { + getClient(key).hincrBy(key, field, value); + return getResponse(BuilderFactory.LONG); + } + + public Response hincrBy(byte[] key, byte[] field, long value) { + getClient(key).hincrBy(key, field, value); + return getResponse(BuilderFactory.LONG); + } + + public Response> hkeys(String key) { + getClient(key).hkeys(key); + return getResponse(BuilderFactory.STRING_SET); + } + + public Response> hkeys(byte[] key) { + getClient(key).hkeys(key); + return getResponse(BuilderFactory.BYTE_ARRAY_ZSET); + } + + public Response hlen(String key) { + getClient(key).hlen(key); + return getResponse(BuilderFactory.LONG); + } + + public Response hlen(byte[] key) { + getClient(key).hlen(key); + return getResponse(BuilderFactory.LONG); + } + + public Response> hmget(String key, String... fields) { + getClient(key).hmget(key, fields); + return getResponse(BuilderFactory.STRING_LIST); + } + + public Response> hmget(byte[] key, byte[]... fields) { + getClient(key).hmget(key, fields); + return getResponse(BuilderFactory.BYTE_ARRAY_LIST); + } + + public Response hmset(String key, Map hash) { + getClient(key).hmset(key, hash); + return getResponse(BuilderFactory.STRING); + } + + public Response hmset(byte[] key, Map hash) { + getClient(key).hmset(key, hash); + return getResponse(BuilderFactory.STRING); + } + + public Response hset(String key, String field, String value) { + getClient(key).hset(key, field, value); + return getResponse(BuilderFactory.LONG); + } + + public Response hset(byte[] key, byte[] field, byte[] value) { + getClient(key).hset(key, field, value); + return getResponse(BuilderFactory.LONG); + } + + public Response hsetnx(String key, String field, String value) { + getClient(key).hsetnx(key, field, value); + return getResponse(BuilderFactory.LONG); + } + + public Response hsetnx(byte[] key, byte[] field, byte[] value) { + getClient(key).hsetnx(key, field, value); + return getResponse(BuilderFactory.LONG); + } + + public Response> hvals(String key) { + getClient(key).hvals(key); + return getResponse(BuilderFactory.STRING_LIST); + } + + public Response> hvals(byte[] key) { + getClient(key).hvals(key); + return getResponse(BuilderFactory.BYTE_ARRAY_LIST); + } + + public Response incr(String key) { + getClient(key).incr(key); + return getResponse(BuilderFactory.LONG); + } + + public Response incr(byte[] key) { + getClient(key).incr(key); + return getResponse(BuilderFactory.LONG); + } + + public Response incrBy(String key, long integer) { + getClient(key).incrBy(key, integer); + return getResponse(BuilderFactory.LONG); + } + + public Response incrBy(byte[] key, long integer) { + getClient(key).incrBy(key, integer); + return getResponse(BuilderFactory.LONG); + } + + public Response lindex(String key, long index) { + getClient(key).lindex(key, index); + return getResponse(BuilderFactory.STRING); + } + + public Response lindex(byte[] key, long index) { + getClient(key).lindex(key, index); + return getResponse(BuilderFactory.BYTE_ARRAY); + } + + public Response linsert(String key, LIST_POSITION where, + String pivot, String value) { + getClient(key).linsert(key, where, pivot, value); + return getResponse(BuilderFactory.LONG); + } + + public Response linsert(byte[] key, LIST_POSITION where, + byte[] pivot, byte[] value) { + getClient(key).linsert(key, where, pivot, value); + return getResponse(BuilderFactory.LONG); + } + + public Response llen(String key) { + getClient(key).llen(key); + return getResponse(BuilderFactory.LONG); + } + + public Response llen(byte[] key) { + getClient(key).llen(key); + return getResponse(BuilderFactory.LONG); + } + + public Response lpop(String key) { + getClient(key).lpop(key); + return getResponse(BuilderFactory.STRING); + } + + public Response lpop(byte[] key) { + getClient(key).lpop(key); + return getResponse(BuilderFactory.BYTE_ARRAY); + } + + public Response lpush(String key, String... string) { + getClient(key).lpush(key, string); + return getResponse(BuilderFactory.LONG); + } + + public Response lpush(byte[] key, byte[]... string) { + getClient(key).lpush(key, string); + return getResponse(BuilderFactory.LONG); + } + + public Response lpushx(String key, String... string) { + getClient(key).lpushx(key, string); + return getResponse(BuilderFactory.LONG); + } + + public Response lpushx(byte[] key, byte[]... bytes) { + getClient(key).lpushx(key, bytes); + return getResponse(BuilderFactory.LONG); + } + + public Response> lrange(String key, long start, long end) { + getClient(key).lrange(key, start, end); + return getResponse(BuilderFactory.STRING_LIST); + } + + public Response> lrange(byte[] key, long start, long end) { + getClient(key).lrange(key, start, end); + return getResponse(BuilderFactory.BYTE_ARRAY_LIST); + } + + public Response lrem(String key, long count, String value) { + getClient(key).lrem(key, count, value); + return getResponse(BuilderFactory.LONG); + } + + public Response lrem(byte[] key, long count, byte[] value) { + getClient(key).lrem(key, count, value); + return getResponse(BuilderFactory.LONG); + } + + public Response lset(String key, long index, String value) { + getClient(key).lset(key, index, value); + return getResponse(BuilderFactory.STRING); + } + + public Response lset(byte[] key, long index, byte[] value) { + getClient(key).lset(key, index, value); + return getResponse(BuilderFactory.STRING); + } + + public Response ltrim(String key, long start, long end) { + getClient(key).ltrim(key, start, end); + return getResponse(BuilderFactory.STRING); + } + + public Response ltrim(byte[] key, long start, long end) { + getClient(key).ltrim(key, start, end); + return getResponse(BuilderFactory.STRING); + } + + public Response move(String key, int dbIndex) { + getClient(key).move(key, dbIndex); + return getResponse(BuilderFactory.LONG); + } + + public Response move(byte[] key, int dbIndex) { + getClient(key).move(key, dbIndex); + return getResponse(BuilderFactory.LONG); + } + + public Response persist(String key) { + getClient(key).persist(key); + return getResponse(BuilderFactory.LONG); + } + + public Response persist(byte[] key) { + getClient(key).persist(key); + return getResponse(BuilderFactory.LONG); + } + + public Response rpop(String key) { + getClient(key).rpop(key); + return getResponse(BuilderFactory.STRING); + } + + public Response rpop(byte[] key) { + getClient(key).rpop(key); + return getResponse(BuilderFactory.BYTE_ARRAY); + } + + public Response rpush(String key, String... string) { + getClient(key).rpush(key, string); + return getResponse(BuilderFactory.LONG); + } + + public Response rpush(byte[] key, byte[]... string) { + getClient(key).rpush(key, string); + return getResponse(BuilderFactory.LONG); + } + + public Response rpushx(String key, String... string) { + getClient(key).rpushx(key, string); + return getResponse(BuilderFactory.LONG); + } + + public Response rpushx(byte[] key, byte[]... string) { + getClient(key).rpushx(key, string); + return getResponse(BuilderFactory.LONG); + } + + public Response sadd(String key, String... member) { + getClient(key).sadd(key, member); + return getResponse(BuilderFactory.LONG); + } + + public Response sadd(byte[] key, byte[]... member) { + getClient(key).sadd(key, member); + return getResponse(BuilderFactory.LONG); + } + + public Response scard(String key) { + getClient(key).scard(key); + return getResponse(BuilderFactory.LONG); + } + + public Response scard(byte[] key) { + getClient(key).scard(key); + return getResponse(BuilderFactory.LONG); + } + + public Response set(String key, String value) { + getClient(key).set(key, value); + return getResponse(BuilderFactory.STRING); + } + + public Response set(byte[] key, byte[] value) { + getClient(key).set(key, value); + return getResponse(BuilderFactory.STRING); + } + + public Response setbit(String key, long offset, boolean value) { + getClient(key).setbit(key, offset, value); + return getResponse(BuilderFactory.BOOLEAN); + } + + public Response setbit(byte[] key, long offset, byte[] value) { + getClient(key).setbit(key, offset, value); + return getResponse(BuilderFactory.BOOLEAN); + } + + public Response setex(String key, int seconds, String value) { + getClient(key).setex(key, seconds, value); + return getResponse(BuilderFactory.STRING); + } + + public Response setex(byte[] key, int seconds, byte[] value) { + getClient(key).setex(key, seconds, value); + return getResponse(BuilderFactory.STRING); + } + + public Response setnx(String key, String value) { + getClient(key).setnx(key, value); + return getResponse(BuilderFactory.LONG); + } + + public Response setnx(byte[] key, byte[] value) { + getClient(key).setnx(key, value); + return getResponse(BuilderFactory.LONG); + } + + public Response setrange(String key, long offset, String value) { + getClient(key).setrange(key, offset, value); + return getResponse(BuilderFactory.LONG); + } + + public Response setrange(byte[] key, long offset, byte[] value) { + getClient(key).setrange(key, offset, value); + return getResponse(BuilderFactory.LONG); + } + + public Response sismember(String key, String member) { + getClient(key).sismember(key, member); + return getResponse(BuilderFactory.BOOLEAN); + } + + public Response sismember(byte[] key, byte[] member) { + getClient(key).sismember(key, member); + return getResponse(BuilderFactory.BOOLEAN); + } + + public Response> smembers(String key) { + getClient(key).smembers(key); + return getResponse(BuilderFactory.STRING_SET); + } + + public Response> smembers(byte[] key) { + getClient(key).smembers(key); + return getResponse(BuilderFactory.BYTE_ARRAY_ZSET); + } + + public Response> sort(String key) { + getClient(key).sort(key); + return getResponse(BuilderFactory.STRING_LIST); + } + + public Response> sort(byte[] key) { + getClient(key).sort(key); + return getResponse(BuilderFactory.BYTE_ARRAY_LIST); + } + + public Response> sort(String key, + SortingParams sortingParameters) { + getClient(key).sort(key, sortingParameters); + return getResponse(BuilderFactory.STRING_LIST); + } + + public Response> sort(byte[] key, + SortingParams sortingParameters) { + getClient(key).sort(key, sortingParameters); + return getResponse(BuilderFactory.BYTE_ARRAY_LIST); + } + + public Response spop(String key) { + getClient(key).spop(key); + return getResponse(BuilderFactory.STRING); + } + + public Response spop(byte[] key) { + getClient(key).spop(key); + return getResponse(BuilderFactory.BYTE_ARRAY); + } + + public Response srandmember(String key) { + getClient(key).srandmember(key); + return getResponse(BuilderFactory.STRING); + } + + public Response> srandmember(String key, int count) { + getClient(key).srandmember(key, count); + return getResponse(BuilderFactory.STRING_LIST); + } + + public Response srandmember(byte[] key) { + getClient(key).srandmember(key); + return getResponse(BuilderFactory.BYTE_ARRAY); + } + + public Response> srandmember(byte[] key, int count) { + getClient(key).srandmember(key, count); + return getResponse(BuilderFactory.BYTE_ARRAY_LIST); + } + + public Response srem(String key, String... member) { + getClient(key).srem(key, member); + return getResponse(BuilderFactory.LONG); + } + + public Response srem(byte[] key, byte[]... member) { + getClient(key).srem(key, member); + return getResponse(BuilderFactory.LONG); + } + + public Response strlen(String key) { + getClient(key).strlen(key); + return getResponse(BuilderFactory.LONG); + } + + public Response strlen(byte[] key) { + getClient(key).strlen(key); + return getResponse(BuilderFactory.LONG); + } + + public Response substr(String key, int start, int end) { + getClient(key).substr(key, start, end); + return getResponse(BuilderFactory.STRING); + } + + public Response substr(byte[] key, int start, int end) { + getClient(key).substr(key, start, end); + return getResponse(BuilderFactory.STRING); + } + + public Response ttl(String key) { + getClient(key).ttl(key); + return getResponse(BuilderFactory.LONG); + } + + public Response ttl(byte[] key) { + getClient(key).ttl(key); + return getResponse(BuilderFactory.LONG); + } + + public Response type(String key) { + getClient(key).type(key); + return getResponse(BuilderFactory.STRING); + } + + public Response type(byte[] key) { + getClient(key).type(key); + return getResponse(BuilderFactory.STRING); + } + + public Response zadd(String key, double score, String member) { + getClient(key).zadd(key, score, member); + return getResponse(BuilderFactory.LONG); + } + + public Response zadd(String key, Map scoreMembers) { + getClient(key).zadd(key, scoreMembers); + return getResponse(BuilderFactory.LONG); + } + + public Response zadd(byte[] key, double score, byte[] member) { + getClient(key).zadd(key, score, member); + return getResponse(BuilderFactory.LONG); + } + + public Response zcard(String key) { + getClient(key).zcard(key); + return getResponse(BuilderFactory.LONG); + } + + public Response zcard(byte[] key) { + getClient(key).zcard(key); + return getResponse(BuilderFactory.LONG); + } + + public Response zcount(String key, double min, double max) { + getClient(key).zcount(key, min, max); + return getResponse(BuilderFactory.LONG); + } + + public Response zcount(String key, String min, String max) { + getClient(key).zcount(key, min, max); + return getResponse(BuilderFactory.LONG); + } + + public Response zcount(byte[] key, double min, double max) { + getClient(key).zcount(key, toByteArray(min), toByteArray(max)); + return getResponse(BuilderFactory.LONG); + } + + public Response zincrby(String key, double score, String member) { + getClient(key).zincrby(key, score, member); + return getResponse(BuilderFactory.DOUBLE); + } + + public Response zincrby(byte[] key, double score, byte[] member) { + getClient(key).zincrby(key, score, member); + return getResponse(BuilderFactory.DOUBLE); + } + + public Response> zrange(String key, long start, long end) { + getClient(key).zrange(key, start, end); + return getResponse(BuilderFactory.STRING_ZSET); + } + + public Response> zrange(byte[] key, long start, long end) { + getClient(key).zrange(key, start, end); + return getResponse(BuilderFactory.BYTE_ARRAY_ZSET); + } + + public Response> zrangeByScore(String key, double min, + double max) { + getClient(key).zrangeByScore(key, min, max); + return getResponse(BuilderFactory.STRING_ZSET); + } + + public Response> zrangeByScore(byte[] key, double min, + double max) { + return zrangeByScore(key, toByteArray(min), toByteArray(max)); + } + + public Response> zrangeByScore(String key, String min, + String max) { + getClient(key).zrangeByScore(key, min, max); + return getResponse(BuilderFactory.STRING_ZSET); + } + + public Response> zrangeByScore(byte[] key, byte[] min, + byte[] max) { + getClient(key).zrangeByScore(key, min, max); + return getResponse(BuilderFactory.BYTE_ARRAY_ZSET); + } + + public Response> zrangeByScore(String key, double min, + double max, int offset, int count) { + getClient(key).zrangeByScore(key, min, max, offset, count); + return getResponse(BuilderFactory.STRING_ZSET); + } + + public Response> zrangeByScore(String key, String min, String max, int offset, int count) { + getClient(key).zrangeByScore(key, min, max, offset, count); + return getResponse(BuilderFactory.STRING_ZSET); + } + + public Response> zrangeByScore(byte[] key, double min, + double max, int offset, int count) { + return zrangeByScore(key, toByteArray(min), toByteArray(max), offset, count); + } + + public Response> zrangeByScore(byte[] key, byte[] min, + byte[] max, int offset, int count) { + getClient(key).zrangeByScore(key, min, max, offset, count); + return getResponse(BuilderFactory.BYTE_ARRAY_ZSET); + } + + public Response> zrangeByScoreWithScores(String key, double min, + double max) { + getClient(key).zrangeByScoreWithScores(key, min, max); + return getResponse(BuilderFactory.TUPLE_ZSET); + } + + public Response> zrangeByScoreWithScores(String key, String min, + String max) { + getClient(key).zrangeByScoreWithScores(key, min, max); + return getResponse(BuilderFactory.TUPLE_ZSET); + } + + public Response> zrangeByScoreWithScores(byte[] key, double min, + double max) { + return zrangeByScoreWithScores(key, toByteArray(min), toByteArray(max)); + } + + public Response> zrangeByScoreWithScores(byte[] key, byte[] min, + byte[] max) { + getClient(key).zrangeByScoreWithScores(key, min, max); + return getResponse(BuilderFactory.TUPLE_ZSET_BINARY); + } + + public Response> zrangeByScoreWithScores(String key, double min, + double max, int offset, int count) { + getClient(key).zrangeByScoreWithScores(key, min, max, offset, count); + return getResponse(BuilderFactory.TUPLE_ZSET); + } + + public Response> zrangeByScoreWithScores(String key, String min, + String max, int offset, int count) { + getClient(key).zrangeByScoreWithScores(key, min, max, offset, count); + return getResponse(BuilderFactory.TUPLE_ZSET); + } + + public Response> zrangeByScoreWithScores(byte[] key, double min, + double max, int offset, int count) { + getClient(key).zrangeByScoreWithScores(key, toByteArray(min), toByteArray(max), offset, count); + return getResponse(BuilderFactory.TUPLE_ZSET_BINARY); + } + + public Response> zrangeByScoreWithScores(byte[] key, byte[] min, + byte[] max, int offset, int count) { + getClient(key).zrangeByScoreWithScores(key, min, max, offset, count); + return getResponse(BuilderFactory.TUPLE_ZSET_BINARY); + } + + public Response> zrevrangeByScore(String key, double max, + double min) { + getClient(key).zrevrangeByScore(key, max, min); + return getResponse(BuilderFactory.STRING_ZSET); + } + + public Response> zrevrangeByScore(byte[] key, double max, + double min) { + getClient(key).zrevrangeByScore(key, toByteArray(max), toByteArray(min)); + return getResponse(BuilderFactory.BYTE_ARRAY_ZSET); + } + + public Response> zrevrangeByScore(String key, String max, + String min) { + getClient(key).zrevrangeByScore(key, max, min); + return getResponse(BuilderFactory.STRING_ZSET); + } + + public Response> zrevrangeByScore(byte[] key, byte[] max, + byte[] min) { + getClient(key).zrevrangeByScore(key, max, min); + return getResponse(BuilderFactory.BYTE_ARRAY_ZSET); + } + + public Response> zrevrangeByScore(String key, double max, + double min, int offset, int count) { + getClient(key).zrevrangeByScore(key, max, min, offset, count); + return getResponse(BuilderFactory.STRING_ZSET); + } + + public Response> zrevrangeByScore(String key, String max, + String min, int offset, int count) { + getClient(key).zrevrangeByScore(key, max, min, offset, count); + return getResponse(BuilderFactory.STRING_ZSET); + } + + public Response> zrevrangeByScore(byte[] key, double max, + double min, int offset, int count) { + getClient(key).zrevrangeByScore(key, toByteArray(max), toByteArray(min), offset, count); + return getResponse(BuilderFactory.BYTE_ARRAY_ZSET); + } + + public Response> zrevrangeByScore(byte[] key, byte[] max, + byte[] min, int offset, int count) { + getClient(key).zrevrangeByScore(key, max, min, offset, count); + return getResponse(BuilderFactory.BYTE_ARRAY_ZSET); + } + + public Response> zrevrangeByScoreWithScores(String key, + double max, double min) { + getClient(key).zrevrangeByScoreWithScores(key, max, min); + return getResponse(BuilderFactory.TUPLE_ZSET); + } + + public Response> zrevrangeByScoreWithScores(String key, + String max, String min) { + getClient(key).zrevrangeByScoreWithScores(key, max, min); + return getResponse(BuilderFactory.TUPLE_ZSET); + } + + public Response> zrevrangeByScoreWithScores(byte[] key, + double max, double min) { + getClient(key).zrevrangeByScoreWithScores(key, toByteArray(max), toByteArray(min)); + return getResponse(BuilderFactory.TUPLE_ZSET_BINARY); + } + + public Response> zrevrangeByScoreWithScores(byte[] key, + byte[] max, byte[] min) { + getClient(key).zrevrangeByScoreWithScores(key, max, min); + return getResponse(BuilderFactory.TUPLE_ZSET_BINARY); + } + + public Response> zrevrangeByScoreWithScores(String key, + double max, double min, int offset, int count) { + getClient(key).zrevrangeByScoreWithScores(key, max, min, offset, count); + return getResponse(BuilderFactory.TUPLE_ZSET); + } + + public Response> zrevrangeByScoreWithScores(String key, + String max, String min, int offset, int count) { + getClient(key).zrevrangeByScoreWithScores(key, max, min, offset, count); + return getResponse(BuilderFactory.TUPLE_ZSET); + } + + public Response> zrevrangeByScoreWithScores(byte[] key, + double max, double min, int offset, int count) { + getClient(key).zrevrangeByScoreWithScores(key, toByteArray(max), toByteArray(min), offset, count); + return getResponse(BuilderFactory.TUPLE_ZSET_BINARY); + } + + public Response> zrevrangeByScoreWithScores(byte[] key, + byte[] max, byte[] min, int offset, int count) { + getClient(key).zrevrangeByScoreWithScores(key, max, min, offset, count); + return getResponse(BuilderFactory.TUPLE_ZSET_BINARY); + } + + public Response> zrangeWithScores(String key, long start, long end) { + getClient(key).zrangeWithScores(key, start, end); + return getResponse(BuilderFactory.TUPLE_ZSET); + } + + public Response> zrangeWithScores(byte[] key, long start, long end) { + getClient(key).zrangeWithScores(key, start, end); + return getResponse(BuilderFactory.TUPLE_ZSET_BINARY); + } + + public Response zrank(String key, String member) { + getClient(key).zrank(key, member); + return getResponse(BuilderFactory.LONG); + } + + public Response zrank(byte[] key, byte[] member) { + getClient(key).zrank(key, member); + return getResponse(BuilderFactory.LONG); + } + + public Response zrem(String key, String... member) { + getClient(key).zrem(key, member); + return getResponse(BuilderFactory.LONG); + } + + public Response zrem(byte[] key, byte[]... member) { + getClient(key).zrem(key, member); + return getResponse(BuilderFactory.LONG); + } + + public Response zremrangeByRank(String key, long start, long end) { + getClient(key).zremrangeByRank(key, start, end); + return getResponse(BuilderFactory.LONG); + } + + public Response zremrangeByRank(byte[] key, long start, long end) { + getClient(key).zremrangeByRank(key, start, end); + return getResponse(BuilderFactory.LONG); + } + + public Response zremrangeByScore(String key, double start, double end) { + getClient(key).zremrangeByScore(key, start, end); + return getResponse(BuilderFactory.LONG); + } + + public Response zremrangeByScore(String key, String start, String end) { + getClient(key).zremrangeByScore(key, start, end); + return getResponse(BuilderFactory.LONG); + } + + public Response zremrangeByScore(byte[] key, double start, double end) { + getClient(key).zremrangeByScore(key, toByteArray(start), toByteArray(end)); + return getResponse(BuilderFactory.LONG); + } + + public Response zremrangeByScore(byte[] key, byte[] start, byte[] end) { + getClient(key).zremrangeByScore(key, start, end); + return getResponse(BuilderFactory.LONG); + } + + public Response> zrevrange(String key, long start, long end) { + getClient(key).zrevrange(key, start, end); + return getResponse(BuilderFactory.STRING_ZSET); + } + + public Response> zrevrange(byte[] key, long start, long end) { + getClient(key).zrevrange(key, start, end); + return getResponse(BuilderFactory.BYTE_ARRAY_ZSET); + } + + public Response> zrevrangeWithScores(String key, long start, + long end) { + getClient(key).zrevrangeWithScores(key, start, end); + return getResponse(BuilderFactory.TUPLE_ZSET); + } + + public Response> zrevrangeWithScores(byte[] key, long start, + long end) { + getClient(key).zrevrangeWithScores(key, start, end); + return getResponse(BuilderFactory.TUPLE_ZSET); + } + + public Response zrevrank(String key, String member) { + getClient(key).zrevrank(key, member); + return getResponse(BuilderFactory.LONG); + } + + public Response zrevrank(byte[] key, byte[] member) { + getClient(key).zrevrank(key, member); + return getResponse(BuilderFactory.LONG); + } + + public Response zscore(String key, String member) { + getClient(key).zscore(key, member); + return getResponse(BuilderFactory.DOUBLE); + } + + public Response zscore(byte[] key, byte[] member) { + getClient(key).zscore(key, member); + return getResponse(BuilderFactory.DOUBLE); + } + + public Response bitcount(String key) { + getClient(key).bitcount(key); + return getResponse(BuilderFactory.LONG); + } + + public Response bitcount(String key, long start, long end) { + getClient(key).bitcount(key, start, end); + return getResponse(BuilderFactory.LONG); + } + + public Response bitcount(byte[] key) { + getClient(key).bitcount(key); + return getResponse(BuilderFactory.LONG); + } + + public Response bitcount(byte[] key, long start, long end) { + getClient(key).bitcount(key, start, end); + return getResponse(BuilderFactory.LONG); + } + + public Response dump(String key) { + getClient(key).dump(key); + return getResponse(BuilderFactory.BYTE_ARRAY); + } + + public Response dump(byte[] key) { + getClient(key).dump(key); + return getResponse(BuilderFactory.BYTE_ARRAY); + } + + public Response migrate(String host, int port, String key, int destinationDb, int timeout) { + getClient(key).migrate(host, port, key, destinationDb, timeout); + return getResponse(BuilderFactory.STRING); + } + + public Response migrate(byte[] host, int port, byte[] key, int destinationDb, int timeout) { + getClient(key).migrate(host, port, key, destinationDb, timeout); + return getResponse(BuilderFactory.STRING); + } + + public Response objectRefcount(String key) { + getClient(key).objectRefcount(key); + return getResponse(BuilderFactory.LONG); + } + + public Response objectRefcount(byte[] key) { + getClient(key).objectRefcount(key); + return getResponse(BuilderFactory.LONG); + } + + public Response objectEncoding(String key) { + getClient(key).objectEncoding(key); + return getResponse(BuilderFactory.STRING); + } + + public Response objectEncoding(byte[] key) { + getClient(key).objectEncoding(key); + return getResponse(BuilderFactory.BYTE_ARRAY); + } + + public Response objectIdletime(String key) { + getClient(key).objectIdletime(key); + return getResponse(BuilderFactory.LONG); + } + + public Response objectIdletime(byte[] key) { + getClient(key).objectIdletime(key); + return getResponse(BuilderFactory.LONG); + } + + public Response pexpire(String key, int milliseconds) { + getClient(key).pexpire(key, milliseconds); + return getResponse(BuilderFactory.LONG); + } + + public Response pexpire(byte[] key, int milliseconds) { + getClient(key).pexpire(key, milliseconds); + return getResponse(BuilderFactory.LONG); + } + + public Response pexpireAt(String key, long millisecondsTimestamp) { + getClient(key).pexpireAt(key, millisecondsTimestamp); + return getResponse(BuilderFactory.LONG); + } + + public Response pexpireAt(byte[] key, long millisecondsTimestamp) { + getClient(key).pexpireAt(key, millisecondsTimestamp); + return getResponse(BuilderFactory.LONG); + } + + public Response pttl(String key) { + getClient(key).pttl(key); + return getResponse(BuilderFactory.LONG); + } + + public Response pttl(byte[] key) { + getClient(key).pttl(key); + return getResponse(BuilderFactory.LONG); + } + + public Response restore(String key, int ttl, byte[] serializedValue) { + getClient(key).restore(key, ttl, serializedValue); + return getResponse(BuilderFactory.STRING); + } + + public Response restore(byte[] key, int ttl, byte[] serializedValue) { + getClient(key).restore(key, ttl, serializedValue); + return getResponse(BuilderFactory.STRING); + } + + public Response incrByFloat(String key, double increment) { + getClient(key).incrByFloat(key, increment); + return getResponse(BuilderFactory.DOUBLE); + } + + public Response incrByFloat(byte[] key, double increment) { + getClient(key).incrByFloat(key, increment); + return getResponse(BuilderFactory.DOUBLE); + } + + public Response psetex(String key, int milliseconds, String value) { + getClient(key).psetex(key, milliseconds, value); + return getResponse(BuilderFactory.STRING); + } + + public Response psetex(byte[] key, int milliseconds, byte[] value) { + getClient(key).psetex(key, milliseconds, value); + return getResponse(BuilderFactory.STRING); + } + + public Response set(String key, String value, String nxxx) { + getClient(key).set(key, value, nxxx); + return getResponse(BuilderFactory.STRING); + } + + public Response set(byte[] key, byte[] value, byte[] nxxx) { + getClient(key).set(key, value, nxxx); + return getResponse(BuilderFactory.STRING); + } + + public Response set(String key, String value, String nxxx, String expx, int time) { + getClient(key).set(key, value, nxxx, expx, time); + return getResponse(BuilderFactory.STRING); + } + + public Response set(byte[] key, byte[] value, byte[] nxxx, byte[] expx, int time) { + getClient(key).set(key, value, nxxx, expx, time); + return getResponse(BuilderFactory.STRING); + } + + public Response hincrByFloat(String key, String field, double increment) { + getClient(key).hincrByFloat(key, field, increment); + return getResponse(BuilderFactory.DOUBLE); + } + + public Response hincrByFloat(byte[] key, byte[] field, double increment) { + getClient(key).hincrByFloat(key, field, increment); + return getResponse(BuilderFactory.DOUBLE); + } + +} diff --git a/src/main/java/redis/clients/jedis/Protocol.java b/src/main/java/redis/clients/jedis/Protocol.java index c2ad2b5..d167b84 100644 --- a/src/main/java/redis/clients/jedis/Protocol.java +++ b/src/main/java/redis/clients/jedis/Protocol.java @@ -14,6 +14,7 @@ public final class Protocol { public static final int DEFAULT_PORT = 6379; public static final int DEFAULT_TIMEOUT = 2000; + public static final int DEFAULT_DATABASE = 0; public static final String CHARSET = "UTF-8"; @@ -23,134 +24,153 @@ public final class Protocol { public static final byte MINUS_BYTE = '-'; public static final byte COLON_BYTE = ':'; - public void sendCommand(final RedisOutputStream os, final Command command, - final byte[]... args) { - sendCommand(os, command.raw, args); + public static final String SENTINEL_MASTERS = "masters"; + public static final String SENTINEL_GET_MASTER_ADDR_BY_NAME = "get-master-addr-by-name"; + public static final String SENTINEL_RESET = "reset"; + public static final String SENTINEL_SLAVES = "slaves"; + public static final String SENTINEL_IS_MASTER_DOWN_BY_ADDR = "is-master-down-by-addr"; + + private Protocol() { + // this prevent the class from instantiation } - private void sendCommand(final RedisOutputStream os, final byte[] command, - final byte[]... args) { - try { - os.write(ASTERISK_BYTE); - os.writeIntCrLf(args.length + 1); - os.write(DOLLAR_BYTE); - os.writeIntCrLf(command.length); - os.write(command); - os.writeCrLf(); - - for (final byte[] arg : args) { - os.write(DOLLAR_BYTE); - os.writeIntCrLf(arg.length); - os.write(arg); - os.writeCrLf(); - } - } catch (IOException e) { - throw new JedisConnectionException(e); - } + public static void sendCommand(final RedisOutputStream os, + final Command command, final byte[]... args) { + sendCommand(os, command.raw, args); } - private void processError(final RedisInputStream is) { - String message = is.readLine(); - throw new JedisDataException(message); + private static void sendCommand(final RedisOutputStream os, + final byte[] command, final byte[]... args) { + try { + os.write(ASTERISK_BYTE); + os.writeIntCrLf(args.length + 1); + os.write(DOLLAR_BYTE); + os.writeIntCrLf(command.length); + os.write(command); + os.writeCrLf(); + + for (final byte[] arg : args) { + os.write(DOLLAR_BYTE); + os.writeIntCrLf(arg.length); + os.write(arg); + os.writeCrLf(); + } + } catch (IOException e) { + throw new JedisConnectionException(e); + } } - private Object process(final RedisInputStream is) { - try { - byte b = is.readByte(); - if (b == MINUS_BYTE) { - processError(is); - } else if (b == ASTERISK_BYTE) { - return processMultiBulkReply(is); - } else if (b == COLON_BYTE) { - return processInteger(is); - } else if (b == DOLLAR_BYTE) { - return processBulkReply(is); - } else if (b == PLUS_BYTE) { - return processStatusCodeReply(is); - } else { - throw new JedisConnectionException("Unknown reply: " + (char) b); - } - } catch (IOException e) { - throw new JedisConnectionException(e); - } - return null; + private static void processError(final RedisInputStream is) { + String message = is.readLine(); + throw new JedisDataException(message); } - private byte[] processStatusCodeReply(final RedisInputStream is) { - return SafeEncoder.encode(is.readLine()); + private static Object process(final RedisInputStream is) { + try { + byte b = is.readByte(); + if (b == MINUS_BYTE) { + processError(is); + } else if (b == ASTERISK_BYTE) { + return processMultiBulkReply(is); + } else if (b == COLON_BYTE) { + return processInteger(is); + } else if (b == DOLLAR_BYTE) { + return processBulkReply(is); + } else if (b == PLUS_BYTE) { + return processStatusCodeReply(is); + } else { + throw new JedisConnectionException("Unknown reply: " + (char) b); + } + } catch (IOException e) { + throw new JedisConnectionException(e); + } + return null; } - private byte[] processBulkReply(final RedisInputStream is) { - int len = Integer.parseInt(is.readLine()); - if (len == -1) { - return null; - } - byte[] read = new byte[len]; - int offset = 0; - try { - while (offset < len) { - offset += is.read(read, offset, (len - offset)); - } - // read 2 more bytes for the command delimiter - is.readByte(); - is.readByte(); - } catch (IOException e) { - throw new JedisConnectionException(e); - } - - return read; + private static byte[] processStatusCodeReply(final RedisInputStream is) { + return SafeEncoder.encode(is.readLine()); } - private Long processInteger(final RedisInputStream is) { - String num = is.readLine(); - return Long.valueOf(num); + private static byte[] processBulkReply(final RedisInputStream is) { + int len = Integer.parseInt(is.readLine()); + if (len == -1) { + return null; + } + byte[] read = new byte[len]; + int offset = 0; + try { + while (offset < len) { + offset += is.read(read, offset, (len - offset)); + } + // read 2 more bytes for the command delimiter + is.readByte(); + is.readByte(); + } catch (IOException e) { + throw new JedisConnectionException(e); + } + + return read; } - private List processMultiBulkReply(final RedisInputStream is) { - int num = Integer.parseInt(is.readLine()); - if (num == -1) { - return null; - } - List ret = new ArrayList(num); - for (int i = 0; i < num; i++) { - ret.add(process(is)); - } - return ret; + private static Long processInteger(final RedisInputStream is) { + String num = is.readLine(); + return Long.valueOf(num); } - public Object read(final RedisInputStream is) { - return process(is); + private static List processMultiBulkReply(final RedisInputStream is) { + int num = Integer.parseInt(is.readLine()); + if (num == -1) { + return null; + } + List ret = new ArrayList(num); + for (int i = 0; i < num; i++) { + try { + ret.add(process(is)); + } catch (JedisDataException e) { + ret.add(e); + } + } + return ret; + } + + public static Object read(final RedisInputStream is) { + return process(is); + } + + public static final byte[] toByteArray(final boolean value) { + return toByteArray(value ? 1 : 0); } public static final byte[] toByteArray(final int value) { - return SafeEncoder.encode(String.valueOf(value)); + return SafeEncoder.encode(String.valueOf(value)); } public static final byte[] toByteArray(final long value) { - return SafeEncoder.encode(String.valueOf(value)); + return SafeEncoder.encode(String.valueOf(value)); } public static final byte[] toByteArray(final double value) { - return SafeEncoder.encode(String.valueOf(value)); + return SafeEncoder.encode(String.valueOf(value)); } public static enum Command { - PING, SET, GET, QUIT, EXISTS, DEL, TYPE, FLUSHDB, KEYS, RANDOMKEY, RENAME, RENAMENX, RENAMEX, DBSIZE, EXPIRE, EXPIREAT, TTL, SELECT, MOVE, FLUSHALL, GETSET, MGET, SETNX, SETEX, MSET, MSETNX, DECRBY, DECR, INCRBY, INCR, APPEND, SUBSTR, HSET, HGET, HSETNX, HMSET, HMGET, HINCRBY, HEXISTS, HDEL, HLEN, HKEYS, HVALS, HGETALL, RPUSH, LPUSH, LLEN, LRANGE, LTRIM, LINDEX, LSET, LREM, LPOP, RPOP, RPOPLPUSH, SADD, SMEMBERS, SREM, SPOP, SMOVE, SCARD, SISMEMBER, SINTER, SINTERSTORE, SUNION, SUNIONSTORE, SDIFF, SDIFFSTORE, SRANDMEMBER, ZADD, ZRANGE, ZREM, ZINCRBY, ZRANK, ZREVRANK, ZREVRANGE, ZCARD, ZSCORE, MULTI, DISCARD, EXEC, WATCH, UNWATCH, SORT, BLPOP, BRPOP, AUTH, SUBSCRIBE, PUBLISH, UNSUBSCRIBE, PSUBSCRIBE, PUNSUBSCRIBE, ZCOUNT, ZRANGEBYSCORE, ZREVRANGEBYSCORE, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZUNIONSTORE, ZINTERSTORE, SAVE, BGSAVE, BGREWRITEAOF, LASTSAVE, SHUTDOWN, INFO, MONITOR, SLAVEOF, CONFIG, STRLEN, SYNC, LPUSHX, PERSIST, RPUSHX, ECHO, LINSERT, DEBUG, BRPOPLPUSH, SETBIT, GETBIT, SETRANGE, GETRANGE; + PING, SET, GET, QUIT, EXISTS, DEL, TYPE, FLUSHDB, KEYS, RANDOMKEY, RENAME, RENAMENX, RENAMEX, DBSIZE, EXPIRE, EXPIREAT, TTL, SELECT, MOVE, FLUSHALL, GETSET, MGET, SETNX, SETEX, MSET, MSETNX, DECRBY, DECR, INCRBY, INCR, APPEND, SUBSTR, HSET, HGET, HSETNX, HMSET, HMGET, HINCRBY, HEXISTS, HDEL, HLEN, HKEYS, HVALS, HGETALL, RPUSH, LPUSH, LLEN, LRANGE, LTRIM, LINDEX, LSET, LREM, LPOP, RPOP, RPOPLPUSH, SADD, SMEMBERS, SREM, SPOP, SMOVE, SCARD, SISMEMBER, SINTER, SINTERSTORE, SUNION, SUNIONSTORE, SDIFF, SDIFFSTORE, SRANDMEMBER, ZADD, ZRANGE, ZREM, ZINCRBY, ZRANK, ZREVRANK, ZREVRANGE, ZCARD, ZSCORE, MULTI, DISCARD, EXEC, WATCH, UNWATCH, SORT, BLPOP, BRPOP, AUTH, SUBSCRIBE, PUBLISH, UNSUBSCRIBE, PSUBSCRIBE, PUNSUBSCRIBE, ZCOUNT, ZRANGEBYSCORE, ZREVRANGEBYSCORE, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZUNIONSTORE, ZINTERSTORE, SAVE, BGSAVE, BGREWRITEAOF, LASTSAVE, SHUTDOWN, INFO, MONITOR, SLAVEOF, CONFIG, STRLEN, SYNC, LPUSHX, PERSIST, RPUSHX, ECHO, LINSERT, DEBUG, BRPOPLPUSH, SETBIT, GETBIT, SETRANGE, GETRANGE, EVAL, EVALSHA, SCRIPT, SLOWLOG, OBJECT, BITCOUNT, BITOP, SENTINEL, + DUMP, RESTORE, PEXPIRE, PEXPIREAT, PTTL, INCRBYFLOAT, PSETEX, CLIENT, TIME, MIGRATE, HINCRBYFLOAT; - public final byte[] raw; + public final byte[] raw; - Command() { - raw = SafeEncoder.encode(this.name()); - } + Command() { + raw = SafeEncoder.encode(this.name()); + } } public static enum Keyword { - AGGREGATE, ALPHA, ASC, BY, DESC, GET, LIMIT, MESSAGE, NO, NOSORT, PMESSAGE, PSUBSCRIBE, PUNSUBSCRIBE, OK, ONE, QUEUED, SET, STORE, SUBSCRIBE, UNSUBSCRIBE, WEIGHTS, WITHSCORES, RESETSTAT; - public final byte[] raw; - - Keyword() { - raw = SafeEncoder.encode(this.name().toLowerCase()); - } + AGGREGATE, ALPHA, ASC, BY, DESC, GET, LIMIT, MESSAGE, NO, NOSORT, PMESSAGE, PSUBSCRIBE, PUNSUBSCRIBE, OK, ONE, QUEUED, SET, STORE, SUBSCRIBE, UNSUBSCRIBE, WEIGHTS, WITHSCORES, RESETSTAT, RESET, FLUSH, EXISTS, LOAD, KILL, LEN, REFCOUNT, ENCODING, IDLETIME, AND, OR, XOR, NOT, + GETNAME, SETNAME,LIST; + public final byte[] raw; + Keyword() { + raw = SafeEncoder.encode(this.name().toLowerCase()); + } } } diff --git a/src/main/java/redis/clients/jedis/RedisPipeline.java b/src/main/java/redis/clients/jedis/RedisPipeline.java new file mode 100644 index 0000000..ff9e862 --- /dev/null +++ b/src/main/java/redis/clients/jedis/RedisPipeline.java @@ -0,0 +1,200 @@ +package redis.clients.jedis; + +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * @author guy + */ +public interface RedisPipeline { + Response append(String key, String value); + + Response> blpop(String arg); + + Response> brpop(String arg); + + Response decr(String key); + + Response decrBy(String key, long integer); + + Response del(String key); + + Response echo(String string); + + Response exists(String key); + + Response expire(String key, int seconds); + + Response expireAt(String key, long unixTime); + + Response get(String key); + + Response getbit(String key, long offset); + + + + Response getrange(String key, long startOffset, + long endOffset); + + Response getSet(String key, String value); + + Response hdel(String key, String... field); + + Response hexists(String key, String field); + + Response hget(String key, String field); + + Response> hgetAll(String key); + + Response hincrBy(String key, String field, long value); + + Response> hkeys(String key); + + Response hlen(String key); + + Response> hmget(String key, String... fields); + + Response hmset(String key, Map hash); + + Response hset(String key, String field, String value); + + Response hsetnx(String key, String field, String value); + + Response> hvals(String key); + + Response incr(String key); + + Response incrBy(String key, long integer); + + Response lindex(String key, long index); + + Response linsert(String key, BinaryClient.LIST_POSITION where, + String pivot, String value); + + Response llen(String key); + + Response lpop(String key); + + Response lpush(String key, String... string); + + Response lpushx(String key, String... string); + + Response> lrange(String key, long start, long end); + + Response lrem(String key, long count, String value); + + Response lset(String key, long index, String value); + + Response ltrim(String key, long start, long end); + + Response move(String key, int dbIndex); + + Response persist(String key); + + Response rpop(String key); + + Response rpush(String key, String... string); + + Response rpushx(String key, String... string); + + Response sadd(String key, String... member); + + Response scard(String key); + + Response sismember(String key, String member); + + Response set(String key, String value); + + Response setbit(String key, long offset, boolean value); + + Response setex(String key, int seconds, String value); + + Response setnx(String key, String value); + + Response setrange(String key, long offset, String value); + + Response> smembers(String key); + + Response> sort(String key); + + Response> sort(String key, + SortingParams sortingParameters); + + Response spop(String key); + + Response srandmember(String key); + + Response srem(String key, String... member); + + Response strlen(String key); + + Response substr(String key, int start, int end); + + Response ttl(String key); + + Response type(String key); + + Response zadd(String key, double score, String member); + + Response zcard(String key); + + Response zcount(String key, double min, double max); + + Response zincrby(String key, double score, String member); + + Response> zrange(String key, long start, long end); + + Response> zrangeByScore(String key, double min, + double max); + + Response> zrangeByScore(String key, String min, + String max); + + Response> zrangeByScore(String key, double min, + double max, int offset, int count); + + Response> zrangeByScoreWithScores(String key, double min, + double max); + + Response> zrangeByScoreWithScores(String key, double min, + double max, int offset, int count); + + Response> zrevrangeByScore(String key, double max, + double min); + + Response> zrevrangeByScore(String key, String max, + String min); + + Response> zrevrangeByScore(String key, double max, + double min, int offset, int count); + + Response> zrevrangeByScoreWithScores(String key, + double max, double min); + + Response> zrevrangeByScoreWithScores(String key, + double max, double min, int offset, int count); + + Response> zrangeWithScores(String key, long start, long end); + + Response zrank(String key, String member); + + Response zrem(String key, String... member); + + Response zremrangeByRank(String key, long start, long end); + + Response zremrangeByScore(String key, double start, double end); + + Response> zrevrange(String key, long start, long end); + + Response> zrevrangeWithScores(String key, long start, + long end); + + Response zrevrank(String key, String member); + + Response zscore(String key, String member); + + Response bitcount(String key); + + Response bitcount(String key, long start, long end); +} diff --git a/src/main/java/redis/clients/jedis/Response.java b/src/main/java/redis/clients/jedis/Response.java index 062e97d..058985f 100644 --- a/src/main/java/redis/clients/jedis/Response.java +++ b/src/main/java/redis/clients/jedis/Response.java @@ -24,7 +24,12 @@ public class Response { "Please close pipeline or multi block before calling this method."); } if (!built) { - response = builder.build(data); + if(data != null ){ + if (data instanceof JedisDataException){ + throw new JedisDataException((JedisDataException)data); + } + response = builder.build(data); + } this.data = null; built = true; } diff --git a/src/main/java/redis/clients/jedis/ScriptingCommands.java b/src/main/java/redis/clients/jedis/ScriptingCommands.java new file mode 100644 index 0000000..ba693a4 --- /dev/null +++ b/src/main/java/redis/clients/jedis/ScriptingCommands.java @@ -0,0 +1,23 @@ +package redis.clients.jedis; + +import java.util.List; + +public interface ScriptingCommands { + Object eval(String script, int keyCount, String... params); + + Object eval(String script, List keys, List args); + + Object eval(String script); + + Object evalsha(String script); + + Object evalsha(String sha1, List keys, List args); + + Object evalsha(String sha1, int keyCount, String... params); + + Boolean scriptExists(String sha1); + + List scriptExists(String... sha1); + + String scriptLoad(String script); +} diff --git a/src/main/java/redis/clients/jedis/ShardedJedis.java b/src/main/java/redis/clients/jedis/ShardedJedis.java index ca95af9..485f83a 100644 --- a/src/main/java/redis/clients/jedis/ShardedJedis.java +++ b/src/main/java/redis/clients/jedis/ShardedJedis.java @@ -1,416 +1,526 @@ package redis.clients.jedis; +import redis.clients.jedis.BinaryClient.LIST_POSITION; +import redis.clients.util.Hashing; + import java.util.List; import java.util.Map; import java.util.Set; import java.util.regex.Pattern; -import redis.clients.jedis.BinaryClient.LIST_POSITION; -import redis.clients.util.Hashing; - public class ShardedJedis extends BinaryShardedJedis implements JedisCommands { public ShardedJedis(List shards) { - super(shards); + super(shards); } public ShardedJedis(List shards, Hashing algo) { - super(shards, algo); + super(shards, algo); } public ShardedJedis(List shards, Pattern keyTagPattern) { - super(shards, keyTagPattern); + super(shards, keyTagPattern); } public ShardedJedis(List shards, Hashing algo, - Pattern keyTagPattern) { - super(shards, algo, keyTagPattern); - } - - public void disconnect() { - for (Jedis jedis : getAllShards()) { - jedis.quit(); - jedis.disconnect(); - } + Pattern keyTagPattern) { + super(shards, algo, keyTagPattern); } public String set(String key, String value) { - Jedis j = getShard(key); - return j.set(key, value); + Jedis j = getShard(key); + return j.set(key, value); } public String get(String key) { - Jedis j = getShard(key); - return j.get(key); + Jedis j = getShard(key); + return j.get(key); + } + + public String echo(String string) { + Jedis j = getShard(string); + return j.echo(string); } public Boolean exists(String key) { - Jedis j = getShard(key); - return j.exists(key); + Jedis j = getShard(key); + return j.exists(key); } public String type(String key) { - Jedis j = getShard(key); - return j.type(key); + Jedis j = getShard(key); + return j.type(key); } public Long expire(String key, int seconds) { - Jedis j = getShard(key); - return j.expire(key, seconds); + Jedis j = getShard(key); + return j.expire(key, seconds); } public Long expireAt(String key, long unixTime) { - Jedis j = getShard(key); - return j.expireAt(key, unixTime); + Jedis j = getShard(key); + return j.expireAt(key, unixTime); } public Long ttl(String key) { - Jedis j = getShard(key); - return j.ttl(key); + Jedis j = getShard(key); + return j.ttl(key); } - public boolean setbit(String key, long offset, boolean value) { - Jedis j = getShard(key); - return j.setbit(key, offset, value); + public Boolean setbit(String key, long offset, boolean value) { + Jedis j = getShard(key); + return j.setbit(key, offset, value); } - public boolean getbit(String key, long offset) { - Jedis j = getShard(key); - return j.getbit(key, offset); + public Boolean setbit(String key, long offset, String value) { + Jedis j = getShard(key); + return j.setbit(key, offset, value); } - public long setrange(String key, long offset, String value) { - Jedis j = getShard(key); - return j.setrange(key, offset, value); + public Boolean getbit(String key, long offset) { + Jedis j = getShard(key); + return j.getbit(key, offset); + } + + public Long setrange(String key, long offset, String value) { + Jedis j = getShard(key); + return j.setrange(key, offset, value); } public String getrange(String key, long startOffset, long endOffset) { - Jedis j = getShard(key); - return j.getrange(key, startOffset, endOffset); + Jedis j = getShard(key); + return j.getrange(key, startOffset, endOffset); } public String getSet(String key, String value) { - Jedis j = getShard(key); - return j.getSet(key, value); + Jedis j = getShard(key); + return j.getSet(key, value); } public Long setnx(String key, String value) { - Jedis j = getShard(key); - return j.setnx(key, value); + Jedis j = getShard(key); + return j.setnx(key, value); } public String setex(String key, int seconds, String value) { - Jedis j = getShard(key); - return j.setex(key, seconds, value); + Jedis j = getShard(key); + return j.setex(key, seconds, value); + } + + public List blpop(String arg) { + Jedis j = getShard(arg); + return j.blpop(arg); + } + + public List brpop(String arg) { + Jedis j = getShard(arg); + return j.brpop(arg); } public Long decrBy(String key, long integer) { - Jedis j = getShard(key); - return j.decrBy(key, integer); + Jedis j = getShard(key); + return j.decrBy(key, integer); } public Long decr(String key) { - Jedis j = getShard(key); - return j.decr(key); + Jedis j = getShard(key); + return j.decr(key); } public Long incrBy(String key, long integer) { - Jedis j = getShard(key); - return j.incrBy(key, integer); + Jedis j = getShard(key); + return j.incrBy(key, integer); } public Long incr(String key) { - Jedis j = getShard(key); - return j.incr(key); + Jedis j = getShard(key); + return j.incr(key); } public Long append(String key, String value) { - Jedis j = getShard(key); - return j.append(key, value); + Jedis j = getShard(key); + return j.append(key, value); } public String substr(String key, int start, int end) { - Jedis j = getShard(key); - return j.substr(key, start, end); + Jedis j = getShard(key); + return j.substr(key, start, end); } public Long hset(String key, String field, String value) { - Jedis j = getShard(key); - return j.hset(key, field, value); + Jedis j = getShard(key); + return j.hset(key, field, value); } public String hget(String key, String field) { - Jedis j = getShard(key); - return j.hget(key, field); + Jedis j = getShard(key); + return j.hget(key, field); } public Long hsetnx(String key, String field, String value) { - Jedis j = getShard(key); - return j.hsetnx(key, field, value); + Jedis j = getShard(key); + return j.hsetnx(key, field, value); } public String hmset(String key, Map hash) { - Jedis j = getShard(key); - return j.hmset(key, hash); + Jedis j = getShard(key); + return j.hmset(key, hash); } public List hmget(String key, String... fields) { - Jedis j = getShard(key); - return j.hmget(key, fields); + Jedis j = getShard(key); + return j.hmget(key, fields); } public Long hincrBy(String key, String field, long value) { - Jedis j = getShard(key); - return j.hincrBy(key, field, value); + Jedis j = getShard(key); + return j.hincrBy(key, field, value); } public Boolean hexists(String key, String field) { - Jedis j = getShard(key); - return j.hexists(key, field); + Jedis j = getShard(key); + return j.hexists(key, field); } public Long del(String key) { - Jedis j = getShard(key); - return j.del(key); + Jedis j = getShard(key); + return j.del(key); } - public Long hdel(String key, String field) { - Jedis j = getShard(key); - return j.hdel(key, field); + public Long hdel(String key, String... fields) { + Jedis j = getShard(key); + return j.hdel(key, fields); } public Long hlen(String key) { - Jedis j = getShard(key); - return j.hlen(key); + Jedis j = getShard(key); + return j.hlen(key); } public Set hkeys(String key) { - Jedis j = getShard(key); - return j.hkeys(key); + Jedis j = getShard(key); + return j.hkeys(key); } public List hvals(String key) { - Jedis j = getShard(key); - return j.hvals(key); + Jedis j = getShard(key); + return j.hvals(key); } public Map hgetAll(String key) { - Jedis j = getShard(key); - return j.hgetAll(key); + Jedis j = getShard(key); + return j.hgetAll(key); } - public Long rpush(String key, String string) { - Jedis j = getShard(key); - return j.rpush(key, string); + public Long rpush(String key, String... strings) { + Jedis j = getShard(key); + return j.rpush(key, strings); } - public Long lpush(String key, String string) { + public Long lpush(String key, String... strings) { + Jedis j = getShard(key); + return j.lpush(key, strings); + } + + public Long lpushx(String key, String... string) { + Jedis j = getShard(key); + return j.lpushx(key, string); + } + + public Long strlen(final String key) { + Jedis j = getShard(key); + return j.strlen(key); + } + + public Long move(String key, int dbIndex) { Jedis j = getShard(key); - return j.lpush(key, string); + return j.move(key, dbIndex); + } + + public Long rpushx(String key, String... string) { + Jedis j = getShard(key); + return j.rpushx(key, string); + } + + public Long persist(final String key) { + Jedis j = getShard(key); + return j.persist(key); } public Long llen(String key) { - Jedis j = getShard(key); - return j.llen(key); + Jedis j = getShard(key); + return j.llen(key); } public List lrange(String key, long start, long end) { - Jedis j = getShard(key); - return j.lrange(key, start, end); + Jedis j = getShard(key); + return j.lrange(key, start, end); } public String ltrim(String key, long start, long end) { - Jedis j = getShard(key); - return j.ltrim(key, start, end); + Jedis j = getShard(key); + return j.ltrim(key, start, end); } public String lindex(String key, long index) { - Jedis j = getShard(key); - return j.lindex(key, index); + Jedis j = getShard(key); + return j.lindex(key, index); } public String lset(String key, long index, String value) { - Jedis j = getShard(key); - return j.lset(key, index, value); + Jedis j = getShard(key); + return j.lset(key, index, value); } public Long lrem(String key, long count, String value) { - Jedis j = getShard(key); - return j.lrem(key, count, value); + Jedis j = getShard(key); + return j.lrem(key, count, value); } public String lpop(String key) { - Jedis j = getShard(key); - return j.lpop(key); + Jedis j = getShard(key); + return j.lpop(key); } public String rpop(String key) { - Jedis j = getShard(key); - return j.rpop(key); + Jedis j = getShard(key); + return j.rpop(key); } - public Long sadd(String key, String member) { - Jedis j = getShard(key); - return j.sadd(key, member); + public Long sadd(String key, String... members) { + Jedis j = getShard(key); + return j.sadd(key, members); } public Set smembers(String key) { - Jedis j = getShard(key); - return j.smembers(key); + Jedis j = getShard(key); + return j.smembers(key); } - public Long srem(String key, String member) { - Jedis j = getShard(key); - return j.srem(key, member); + public Long srem(String key, String... members) { + Jedis j = getShard(key); + return j.srem(key, members); } public String spop(String key) { - Jedis j = getShard(key); - return j.spop(key); + Jedis j = getShard(key); + return j.spop(key); } public Long scard(String key) { - Jedis j = getShard(key); - return j.scard(key); + Jedis j = getShard(key); + return j.scard(key); } public Boolean sismember(String key, String member) { - Jedis j = getShard(key); - return j.sismember(key, member); + Jedis j = getShard(key); + return j.sismember(key, member); } public String srandmember(String key) { - Jedis j = getShard(key); - return j.srandmember(key); + Jedis j = getShard(key); + return j.srandmember(key); } public Long zadd(String key, double score, String member) { - Jedis j = getShard(key); - return j.zadd(key, score, member); + Jedis j = getShard(key); + return j.zadd(key, score, member); } - public Set zrange(String key, int start, int end) { - Jedis j = getShard(key); - return j.zrange(key, start, end); + public Long zadd(String key, Map scoreMembers) { + Jedis j = getShard(key); + return j.zadd(key, scoreMembers); } - public Long zrem(String key, String member) { - Jedis j = getShard(key); - return j.zrem(key, member); + public Set zrange(String key, long start, long end) { + Jedis j = getShard(key); + return j.zrange(key, start, end); + } + + public Long zrem(String key, String... members) { + Jedis j = getShard(key); + return j.zrem(key, members); } public Double zincrby(String key, double score, String member) { - Jedis j = getShard(key); - return j.zincrby(key, score, member); + Jedis j = getShard(key); + return j.zincrby(key, score, member); } public Long zrank(String key, String member) { - Jedis j = getShard(key); - return j.zrank(key, member); + Jedis j = getShard(key); + return j.zrank(key, member); } public Long zrevrank(String key, String member) { - Jedis j = getShard(key); - return j.zrevrank(key, member); + Jedis j = getShard(key); + return j.zrevrank(key, member); } - public Set zrevrange(String key, int start, int end) { - Jedis j = getShard(key); - return j.zrevrange(key, start, end); + public Set zrevrange(String key, long start, long end) { + Jedis j = getShard(key); + return j.zrevrange(key, start, end); } - public Set zrangeWithScores(String key, int start, int end) { - Jedis j = getShard(key); - return j.zrangeWithScores(key, start, end); + public Set zrangeWithScores(String key, long start, long end) { + Jedis j = getShard(key); + return j.zrangeWithScores(key, start, end); } - public Set zrevrangeWithScores(String key, int start, int end) { - Jedis j = getShard(key); - return j.zrevrangeWithScores(key, start, end); + public Set zrevrangeWithScores(String key, long start, long end) { + Jedis j = getShard(key); + return j.zrevrangeWithScores(key, start, end); } public Long zcard(String key) { - Jedis j = getShard(key); - return j.zcard(key); + Jedis j = getShard(key); + return j.zcard(key); } public Double zscore(String key, String member) { - Jedis j = getShard(key); - return j.zscore(key, member); + Jedis j = getShard(key); + return j.zscore(key, member); } public List sort(String key) { - Jedis j = getShard(key); - return j.sort(key); + Jedis j = getShard(key); + return j.sort(key); } public List sort(String key, SortingParams sortingParameters) { - Jedis j = getShard(key); - return j.sort(key, sortingParameters); + Jedis j = getShard(key); + return j.sort(key, sortingParameters); } public Long zcount(String key, double min, double max) { - Jedis j = getShard(key); - return j.zcount(key, min, max); + Jedis j = getShard(key); + return j.zcount(key, min, max); + } + + public Long zcount(String key, String min, String max) { + Jedis j = getShard(key); + return j.zcount(key, min, max); } public Set zrangeByScore(String key, double min, double max) { - Jedis j = getShard(key); - return j.zrangeByScore(key, min, max); + Jedis j = getShard(key); + return j.zrangeByScore(key, min, max); } public Set zrevrangeByScore(String key, double max, double min) { - Jedis j = getShard(key); - return j.zrevrangeByScore(key, max, min); + Jedis j = getShard(key); + return j.zrevrangeByScore(key, max, min); } public Set zrangeByScore(String key, double min, double max, - int offset, int count) { - Jedis j = getShard(key); - return j.zrangeByScore(key, min, max, offset, count); + int offset, int count) { + Jedis j = getShard(key); + return j.zrangeByScore(key, min, max, offset, count); } public Set zrevrangeByScore(String key, double max, double min, - int offset, int count) { - Jedis j = getShard(key); - return j.zrevrangeByScore(key, max, min, offset, count); + int offset, int count) { + Jedis j = getShard(key); + return j.zrevrangeByScore(key, max, min, offset, count); } public Set zrangeByScoreWithScores(String key, double min, double max) { - Jedis j = getShard(key); - return j.zrangeByScoreWithScores(key, min, max); + Jedis j = getShard(key); + return j.zrangeByScoreWithScores(key, min, max); } public Set zrevrangeByScoreWithScores(String key, double max, - double min) { - Jedis j = getShard(key); - return j.zrevrangeByScoreWithScores(key, max, min); + double min) { + Jedis j = getShard(key); + return j.zrevrangeByScoreWithScores(key, max, min); } public Set zrangeByScoreWithScores(String key, double min, - double max, int offset, int count) { - Jedis j = getShard(key); - return j.zrangeByScoreWithScores(key, min, max, offset, count); + double max, int offset, int count) { + Jedis j = getShard(key); + return j.zrangeByScoreWithScores(key, min, max, offset, count); } public Set zrevrangeByScoreWithScores(String key, double max, - double min, int offset, int count) { - Jedis j = getShard(key); - return j.zrevrangeByScoreWithScores(key, max, min, offset, count); + double min, int offset, int count) { + Jedis j = getShard(key); + return j.zrevrangeByScoreWithScores(key, max, min, offset, count); } - public Long zremrangeByRank(String key, int start, int end) { - Jedis j = getShard(key); - return j.zremrangeByRank(key, start, end); + public Set zrangeByScore(String key, String min, String max) { + Jedis j = getShard(key); + return j.zrangeByScore(key, min, max); + } + + public Set zrevrangeByScore(String key, String max, String min) { + Jedis j = getShard(key); + return j.zrevrangeByScore(key, max, min); + } + + public Set zrangeByScore(String key, String min, String max, + int offset, int count) { + Jedis j = getShard(key); + return j.zrangeByScore(key, min, max, offset, count); + } + + public Set zrevrangeByScore(String key, String max, String min, + int offset, int count) { + Jedis j = getShard(key); + return j.zrevrangeByScore(key, max, min, offset, count); + } + + public Set zrangeByScoreWithScores(String key, String min, String max) { + Jedis j = getShard(key); + return j.zrangeByScoreWithScores(key, min, max); + } + + public Set zrevrangeByScoreWithScores(String key, String max, + String min) { + Jedis j = getShard(key); + return j.zrevrangeByScoreWithScores(key, max, min); + } + + public Set zrangeByScoreWithScores(String key, String min, + String max, int offset, int count) { + Jedis j = getShard(key); + return j.zrangeByScoreWithScores(key, min, max, offset, count); + } + + public Set zrevrangeByScoreWithScores(String key, String max, + String min, int offset, int count) { + Jedis j = getShard(key); + return j.zrevrangeByScoreWithScores(key, max, min, offset, count); + } + + public Long zremrangeByRank(String key, long start, long end) { + Jedis j = getShard(key); + return j.zremrangeByRank(key, start, end); } public Long zremrangeByScore(String key, double start, double end) { - Jedis j = getShard(key); - return j.zremrangeByScore(key, start, end); + Jedis j = getShard(key); + return j.zremrangeByScore(key, start, end); + } + + public Long zremrangeByScore(String key, String start, String end) { + Jedis j = getShard(key); + return j.zremrangeByScore(key, start, end); } public Long linsert(String key, LIST_POSITION where, String pivot, - String value) { - Jedis j = getShard(key); - return j.linsert(key, where, pivot, value); + String value) { + Jedis j = getShard(key); + return j.linsert(key, where, pivot, value); } + + public Long bitcount(final String key) { + Jedis j = getShard(key); + return j.bitcount(key); + } + + public Long bitcount(final String key, long start, long end) { + Jedis j = getShard(key); + return j.bitcount(key, start, end); + } + + } diff --git a/src/main/java/redis/clients/jedis/ShardedJedisPipeline.java b/src/main/java/redis/clients/jedis/ShardedJedisPipeline.java index 544ce73..be6db92 100644 --- a/src/main/java/redis/clients/jedis/ShardedJedisPipeline.java +++ b/src/main/java/redis/clients/jedis/ShardedJedisPipeline.java @@ -1,16 +1,16 @@ package redis.clients.jedis; -import redis.clients.jedis.BinaryClient.LIST_POSITION; - import java.util.ArrayList; +import java.util.LinkedList; import java.util.List; -import java.util.Map; +import java.util.Queue; -public abstract class ShardedJedisPipeline { +public class ShardedJedisPipeline extends PipelineBase { private BinaryShardedJedis jedis; private List results = new ArrayList(); + private Queue clients = new LinkedList(); - private class FutureResult { + private static class FutureResult { private Client client; public FutureResult(Client client) { @@ -26,429 +26,6 @@ public abstract class ShardedJedisPipeline { this.jedis = jedis; } - protected void set(String key, String value) { - Client c = jedis.getShard(key).getClient(); - c.set(key, value); - results.add(new FutureResult(c)); - } - - protected void get(String key) { - Client c = jedis.getShard(key).getClient(); - c.get(key); - results.add(new FutureResult(c)); - } - - protected void exists(String key) { - Client c = jedis.getShard(key).getClient(); - c.exists(key); - results.add(new FutureResult(c)); - } - - protected void type(String key) { - Client c = jedis.getShard(key).getClient(); - c.type(key); - results.add(new FutureResult(c)); - } - - protected void expire(String key, int seconds) { - Client c = jedis.getShard(key).getClient(); - c.expire(key, seconds); - results.add(new FutureResult(c)); - } - - protected void expireAt(String key, long unixTime) { - Client c = jedis.getShard(key).getClient(); - c.expireAt(key, unixTime); - results.add(new FutureResult(c)); - } - - protected void ttl(String key) { - Client c = jedis.getShard(key).getClient(); - c.ttl(key); - results.add(new FutureResult(c)); - } - - protected void getSet(String key, String value) { - Client c = jedis.getShard(key).getClient(); - c.getSet(key, value); - results.add(new FutureResult(c)); - } - - protected void setnx(String key, String value) { - Client c = jedis.getShard(key).getClient(); - c.setnx(key, value); - results.add(new FutureResult(c)); - } - - protected void setex(String key, int seconds, String value) { - Client c = jedis.getShard(key).getClient(); - c.setex(key, seconds, value); - results.add(new FutureResult(c)); - } - - protected void decrBy(String key, int integer) { - Client c = jedis.getShard(key).getClient(); - c.decrBy(key, integer); - results.add(new FutureResult(c)); - } - - protected void decr(String key) { - Client c = jedis.getShard(key).getClient(); - c.decr(key); - results.add(new FutureResult(c)); - } - - protected void incrBy(String key, int integer) { - Client c = jedis.getShard(key).getClient(); - c.incrBy(key, integer); - results.add(new FutureResult(c)); - } - - protected void incr(String key) { - Client c = jedis.getShard(key).getClient(); - c.incr(key); - results.add(new FutureResult(c)); - } - - protected void append(String key, String value) { - Client c = jedis.getShard(key).getClient(); - c.append(key, value); - results.add(new FutureResult(c)); - } - - protected void substr(String key, int start, int end) { - Client c = jedis.getShard(key).getClient(); - c.substr(key, start, end); - results.add(new FutureResult(c)); - } - - protected void hset(String key, String field, String value) { - Client c = jedis.getShard(key).getClient(); - c.hset(key, field, value); - results.add(new FutureResult(c)); - } - - protected void hget(String key, String field) { - Client c = jedis.getShard(key).getClient(); - c.hget(key, field); - results.add(new FutureResult(c)); - } - - protected void hsetnx(String key, String field, String value) { - Client c = jedis.getShard(key).getClient(); - c.hsetnx(key, field, value); - results.add(new FutureResult(c)); - } - - protected void hmset(String key, Map hash) { - Client c = jedis.getShard(key).getClient(); - c.hmset(key, hash); - results.add(new FutureResult(c)); - } - - protected void hmget(String key, String... fields) { - Client c = jedis.getShard(key).getClient(); - c.hmget(key, fields); - results.add(new FutureResult(c)); - } - - protected void hincrBy(String key, String field, int value) { - Client c = jedis.getShard(key).getClient(); - c.hincrBy(key, field, value); - results.add(new FutureResult(c)); - } - - protected void hexists(String key, String field) { - Client c = jedis.getShard(key).getClient(); - c.hexists(key, field); - results.add(new FutureResult(c)); - } - - protected void hdel(String key, String field) { - Client c = jedis.getShard(key).getClient(); - c.hdel(key, field); - results.add(new FutureResult(c)); - } - - protected void hlen(String key) { - Client c = jedis.getShard(key).getClient(); - c.hlen(key); - results.add(new FutureResult(c)); - } - - protected void hkeys(String key) { - Client c = jedis.getShard(key).getClient(); - c.hkeys(key); - results.add(new FutureResult(c)); - } - - protected void hvals(String key) { - Client c = jedis.getShard(key).getClient(); - c.hvals(key); - results.add(new FutureResult(c)); - } - - protected void hgetAll(String key) { - Client c = jedis.getShard(key).getClient(); - c.hgetAll(key); - results.add(new FutureResult(c)); - } - - protected void rpush(String key, String string) { - Client c = jedis.getShard(key).getClient(); - c.rpush(key, string); - results.add(new FutureResult(c)); - } - - protected void lpush(String key, String string) { - Client c = jedis.getShard(key).getClient(); - c.lpush(key, string); - results.add(new FutureResult(c)); - } - - protected void llen(String key) { - Client c = jedis.getShard(key).getClient(); - c.llen(key); - results.add(new FutureResult(c)); - } - - protected void lrange(String key, int start, int end) { - Client c = jedis.getShard(key).getClient(); - c.lrange(key, start, end); - results.add(new FutureResult(c)); - } - - protected void ltrim(String key, int start, int end) { - Client c = jedis.getShard(key).getClient(); - c.ltrim(key, start, end); - results.add(new FutureResult(c)); - } - - protected void lindex(String key, int index) { - Client c = jedis.getShard(key).getClient(); - c.lindex(key, index); - results.add(new FutureResult(c)); - } - - protected void lset(String key, int index, String value) { - Client c = jedis.getShard(key).getClient(); - c.lset(key, index, value); - results.add(new FutureResult(c)); - } - - protected void lrem(String key, int count, String value) { - Client c = jedis.getShard(key).getClient(); - c.lrem(key, count, value); - results.add(new FutureResult(c)); - } - - protected void lpop(String key) { - Client c = jedis.getShard(key).getClient(); - c.lpop(key); - results.add(new FutureResult(c)); - } - - protected void rpop(String key) { - Client c = jedis.getShard(key).getClient(); - c.rpop(key); - results.add(new FutureResult(c)); - } - - protected void sadd(String key, String member) { - Client c = jedis.getShard(key).getClient(); - c.sadd(key, member); - results.add(new FutureResult(c)); - } - - protected void smembers(String key) { - Client c = jedis.getShard(key).getClient(); - c.smembers(key); - results.add(new FutureResult(c)); - } - - protected void srem(String key, String member) { - Client c = jedis.getShard(key).getClient(); - c.srem(key, member); - results.add(new FutureResult(c)); - } - - protected void spop(String key) { - Client c = jedis.getShard(key).getClient(); - c.spop(key); - results.add(new FutureResult(c)); - } - - protected void scard(String key) { - Client c = jedis.getShard(key).getClient(); - c.scard(key); - results.add(new FutureResult(c)); - } - - protected void sismember(String key, String member) { - Client c = jedis.getShard(key).getClient(); - c.sismember(key, member); - results.add(new FutureResult(c)); - } - - protected void srandmember(String key) { - Client c = jedis.getShard(key).getClient(); - c.srandmember(key); - results.add(new FutureResult(c)); - } - - protected void zadd(String key, double score, String member) { - Client c = jedis.getShard(key).getClient(); - c.zadd(key, score, member); - results.add(new FutureResult(c)); - } - - protected void zrange(String key, int start, int end) { - Client c = jedis.getShard(key).getClient(); - c.zrange(key, start, end); - results.add(new FutureResult(c)); - } - - protected void zrem(String key, String member) { - Client c = jedis.getShard(key).getClient(); - c.zrem(key, member); - results.add(new FutureResult(c)); - } - - protected void zincrby(String key, double score, String member) { - Client c = jedis.getShard(key).getClient(); - c.zincrby(key, score, member); - results.add(new FutureResult(c)); - } - - protected void zrank(String key, String member) { - Client c = jedis.getShard(key).getClient(); - c.zrank(key, member); - results.add(new FutureResult(c)); - } - - protected void zrevrank(String key, String member) { - Client c = jedis.getShard(key).getClient(); - c.zrevrank(key, member); - results.add(new FutureResult(c)); - } - - protected void zrevrange(String key, int start, int end) { - Client c = jedis.getShard(key).getClient(); - c.zrevrange(key, start, end); - results.add(new FutureResult(c)); - } - - protected void zrangeWithScores(String key, int start, int end) { - Client c = jedis.getShard(key).getClient(); - c.zrangeWithScores(key, start, end); - results.add(new FutureResult(c)); - } - - protected void zrevrangeWithScores(String key, int start, int end) { - Client c = jedis.getShard(key).getClient(); - c.zrevrangeWithScores(key, start, end); - results.add(new FutureResult(c)); - } - - protected void zcard(String key) { - Client c = jedis.getShard(key).getClient(); - c.zcard(key); - results.add(new FutureResult(c)); - } - - protected void zscore(String key, String member) { - Client c = jedis.getShard(key).getClient(); - c.zscore(key, member); - results.add(new FutureResult(c)); - } - - protected void sort(String key) { - Client c = jedis.getShard(key).getClient(); - c.sort(key); - results.add(new FutureResult(c)); - } - - protected void sort(String key, SortingParams sortingParameters) { - Client c = jedis.getShard(key).getClient(); - c.sort(key, sortingParameters); - results.add(new FutureResult(c)); - } - - protected void zcount(String key, double min, double max) { - Client c = jedis.getShard(key).getClient(); - c.zcount(key, min, max); - results.add(new FutureResult(c)); - } - - protected void zrangeByScore(String key, double min, double max) { - Client c = jedis.getShard(key).getClient(); - c.zrangeByScore(key, min, max); - results.add(new FutureResult(c)); - } - - protected void zrangeByScore(String key, double min, double max, - int offset, int count) { - Client c = jedis.getShard(key).getClient(); - c.zrangeByScore(key, min, max, offset, count); - results.add(new FutureResult(c)); - } - - protected void zrangeByScoreWithScores(String key, double min, double max) { - Client c = jedis.getShard(key).getClient(); - c.zrangeByScoreWithScores(key, min, max); - results.add(new FutureResult(c)); - } - - protected void zrangeByScoreWithScores(String key, double min, double max, - int offset, int count) { - Client c = jedis.getShard(key).getClient(); - c.zrangeByScoreWithScores(key, min, max, offset, count); - results.add(new FutureResult(c)); - } - - protected void zremrangeByRank(String key, int start, int end) { - Client c = jedis.getShard(key).getClient(); - c.zremrangeByRank(key, start, end); - results.add(new FutureResult(c)); - } - - protected void zremrangeByScore(String key, double start, double end) { - Client c = jedis.getShard(key).getClient(); - c.zremrangeByScore(key, start, end); - results.add(new FutureResult(c)); - } - - protected void linsert(String key, LIST_POSITION where, String pivot, - String value) { - Client c = jedis.getShard(key).getClient(); - c.linsert(key, where, pivot, value); - results.add(new FutureResult(c)); - } - - protected void getbit(String key, long offset) { - Client c = jedis.getShard(key).getClient(); - c.getbit(key, offset); - results.add(new FutureResult(c)); - } - - public void setbit(String key, long offset, boolean value) { - Client c = jedis.getShard(key).getClient(); - c.setbit(key, offset, value); - results.add(new FutureResult(c)); - } - - public void setrange(String key, long offset, String value) { - Client c = jedis.getShard(key).getClient(); - c.setrange(key, offset, value); - results.add(new FutureResult(c)); - } - - public void getrange(String key, long startOffset, long endOffset) { - Client c = jedis.getShard(key).getClient(); - c.getrange(key, startOffset, endOffset); - results.add(new FutureResult(c)); - } - public List getResults() { List r = new ArrayList(); for (FutureResult fr : results) { @@ -457,5 +34,54 @@ public abstract class ShardedJedisPipeline { return r; } - public abstract void execute(); + /** + * Syncronize pipeline by reading all responses. This operation closes the + * pipeline. In order to get return values from pipelined commands, capture + * the different Response<?> of the commands you execute. + */ + public void sync() { + for (Client client : clients) { + generateResponse(client.getOne()); + } + } + + /** + * Syncronize pipeline by reading all responses. This operation closes the + * pipeline. Whenever possible try to avoid using this version and use + * ShardedJedisPipeline.sync() as it won't go through all the responses and generate the + * right response type (usually it is a waste of time). + * + * @return A list of all the responses in the order you executed them. + */ + public List syncAndReturnAll() { + List formatted = new ArrayList(); + for (Client client : clients) { + formatted.add(generateResponse(client.getOne()).get()); + } + return formatted; + } + + /** + * This method will be removed in Jedis 3.0. Use the methods that return Response's and call + * sync(). + */ + @Deprecated + public void execute() { + } + + @Override + protected Client getClient(String key) { + Client client = jedis.getShard(key).getClient(); + clients.add(client); + results.add(new FutureResult(client)); + return client; + } + + @Override + protected Client getClient(byte[] key) { + Client client = jedis.getShard(key).getClient(); + clients.add(client); + results.add(new FutureResult(client)); + return client; + } } \ No newline at end of file diff --git a/src/main/java/redis/clients/jedis/Transaction.java b/src/main/java/redis/clients/jedis/Transaction.java index b1dd14a..949f541 100644 --- a/src/main/java/redis/clients/jedis/Transaction.java +++ b/src/main/java/redis/clients/jedis/Transaction.java @@ -1,580 +1,75 @@ package redis.clients.jedis; +import java.util.ArrayList; import java.util.List; -import java.util.Map; -import java.util.Set; -import redis.clients.jedis.BinaryClient.LIST_POSITION; +import redis.clients.jedis.exceptions.JedisDataException; -public class Transaction extends BinaryTransaction { - public Transaction() { +/** + * Transaction is nearly identical to Pipeline, only differences are the multi/discard behaviors + */ +public class Transaction extends MultiKeyPipelineBase { + + protected boolean inTransaction = true; + + protected Transaction(){ + // client will be set later in transaction block } public Transaction(final Client client) { - super(client); - } - - public Response append(String key, String value) { - client.append(key, value); - return getResponse(BuilderFactory.LONG); - } - - public Response> blpop(String... args) { - client.blpop(args); - return getResponse(BuilderFactory.STRING_LIST); - } - - public Response> brpop(String... args) { - client.brpop(args); - return getResponse(BuilderFactory.STRING_LIST); - } - - public Response decr(String key) { - client.decr(key); - return getResponse(BuilderFactory.LONG); - } - - public Response decrBy(String key, long integer) { - client.decrBy(key, integer); - return getResponse(BuilderFactory.LONG); - } - - public Response del(String... keys) { - client.del(keys); - return getResponse(BuilderFactory.LONG); - } - - public Response echo(String string) { - client.echo(string); - return getResponse(BuilderFactory.STRING); - } - - public Response exists(String key) { - client.exists(key); - return getResponse(BuilderFactory.BOOLEAN); - } - - public Response expire(String key, int seconds) { - client.expire(key, seconds); - return getResponse(BuilderFactory.LONG); - } - - public Response expireAt(String key, long unixTime) { - client.expireAt(key, unixTime); - return getResponse(BuilderFactory.LONG); - } - - public Response get(String key) { - client.get(key); - return getResponse(BuilderFactory.STRING); - } - - public Response getbit(String key, long offset) { - client.getbit(key, offset); - return getResponse(BuilderFactory.BOOLEAN); - } - - public Response getrange(String key, long startOffset, - long endOffset) { - client.getrange(key, startOffset, endOffset); - return getResponse(BuilderFactory.STRING); - } - - public Response getSet(String key, String value) { - client.getSet(key, value); - return getResponse(BuilderFactory.STRING); - } - - public Response hdel(String key, String field) { - client.hdel(key, field); - return getResponse(BuilderFactory.LONG); - } - - public Response hexists(String key, String field) { - client.hexists(key, field); - return getResponse(BuilderFactory.BOOLEAN); - } - - public Response hget(String key, String field) { - client.hget(key, field); - return getResponse(BuilderFactory.STRING); - } - - public Response> hgetAll(String key) { - client.hgetAll(key); - return getResponse(BuilderFactory.STRING_MAP); - } - - public Response hincrBy(String key, String field, long value) { - client.hincrBy(key, field, value); - return getResponse(BuilderFactory.LONG); - } - - public Response> hkeys(String key) { - client.hkeys(key); - return getResponse(BuilderFactory.STRING_SET); - } - - public Response hlen(String key) { - client.hlen(key); - return getResponse(BuilderFactory.LONG); - } - - public Response> hmget(String key, String... fields) { - client.hmget(key, fields); - return getResponse(BuilderFactory.STRING_LIST); - } - - public Response hmset(String key, Map hash) { - client.hmset(key, hash); - return getResponse(BuilderFactory.STRING); - } - - public Response hset(String key, String field, String value) { - client.hset(key, field, value); - return getResponse(BuilderFactory.LONG); - } - - public Response hsetnx(String key, String field, String value) { - client.hsetnx(key, field, value); - return getResponse(BuilderFactory.LONG); - } - - public Response> hvals(String key) { - client.hvals(key); - return getResponse(BuilderFactory.STRING_LIST); - } - - public Response incr(String key) { - client.incr(key); - return getResponse(BuilderFactory.LONG); - } - - public Response incrBy(String key, long integer) { - client.incrBy(key, integer); - return getResponse(BuilderFactory.LONG); - } - - public Response> keys(String pattern) { - client.keys(pattern); - return getResponse(BuilderFactory.STRING_SET); - } - - public Response lindex(String key, int index) { - client.lindex(key, index); - return getResponse(BuilderFactory.STRING); - } - - public Response linsert(String key, LIST_POSITION where, - String pivot, String value) { - client.linsert(key, where, pivot, value); - return getResponse(BuilderFactory.LONG); - } - - public Response llen(String key) { - client.llen(key); - return getResponse(BuilderFactory.LONG); - } - - public Response lpop(String key) { - client.lpop(key); - return getResponse(BuilderFactory.STRING); - } - - public Response lpush(String key, String string) { - client.lpush(key, string); - return getResponse(BuilderFactory.LONG); - } - - public Response lpushx(String key, String string) { - client.lpushx(key, string); - return getResponse(BuilderFactory.LONG); - } - - public Response> lrange(String key, long start, long end) { - client.lrange(key, start, end); - return getResponse(BuilderFactory.STRING_LIST); - } - - public Response lrem(String key, long count, String value) { - client.lrem(key, count, value); - return getResponse(BuilderFactory.LONG); - } - - public Response lset(String key, long index, String value) { - client.lset(key, index, value); - return getResponse(BuilderFactory.STRING); - } - - public Response ltrim(String key, long start, long end) { - client.ltrim(key, start, end); - return getResponse(BuilderFactory.STRING); - } - - public Response> mget(String... keys) { - client.mget(keys); - return getResponse(BuilderFactory.STRING_LIST); - } - - public Response move(String key, int dbIndex) { - client.move(key, dbIndex); - return getResponse(BuilderFactory.LONG); - } - - public Response mset(String... keysvalues) { - client.mset(keysvalues); - return getResponse(BuilderFactory.STRING); - } - - public Response msetnx(String... keysvalues) { - client.msetnx(keysvalues); - return getResponse(BuilderFactory.LONG); - } - - public Response persist(String key) { - client.persist(key); - return getResponse(BuilderFactory.LONG); - } - - public Response rename(String oldkey, String newkey) { - client.rename(oldkey, newkey); - return getResponse(BuilderFactory.STRING); - } - - public Response renamenx(String oldkey, String newkey) { - client.renamenx(oldkey, newkey); - return getResponse(BuilderFactory.LONG); - } - - public Response rpop(String key) { - client.rpop(key); - return getResponse(BuilderFactory.STRING); - } - - public Response rpoplpush(String srckey, String dstkey) { - client.rpoplpush(srckey, dstkey); - return getResponse(BuilderFactory.STRING); - } - - public Response rpush(String key, String string) { - client.rpush(key, string); - return getResponse(BuilderFactory.LONG); - } - - public Response rpushx(String key, String string) { - client.rpushx(key, string); - return getResponse(BuilderFactory.LONG); - } - - public Response sadd(String key, String member) { - client.sadd(key, member); - return getResponse(BuilderFactory.LONG); - } - - public Response scard(String key) { - client.scard(key); - return getResponse(BuilderFactory.LONG); - } - - public Response> sdiff(String... keys) { - client.sdiff(keys); - return getResponse(BuilderFactory.STRING_SET); - } - - public Response sdiffstore(String dstkey, String... keys) { - client.sdiffstore(dstkey, keys); - return getResponse(BuilderFactory.LONG); - } - - public Response set(String key, String value) { - client.set(key, value); - return getResponse(BuilderFactory.STRING); - } - - public Response setbit(String key, long offset, boolean value) { - client.setbit(key, offset, value); - return getResponse(BuilderFactory.BOOLEAN); - } - - public Response setex(String key, int seconds, String value) { - client.setex(key, seconds, value); - return getResponse(BuilderFactory.STRING); - } - - public Response setnx(String key, String value) { - client.setnx(key, value); - return getResponse(BuilderFactory.LONG); - } - - public Response setrange(String key, long offset, String value) { - client.setrange(key, offset, value); - return getResponse(BuilderFactory.LONG); - } - - public Response> sinter(String... keys) { - client.sinter(keys); - return getResponse(BuilderFactory.STRING_SET); - } - - public Response sinterstore(String dstkey, String... keys) { - client.sinterstore(dstkey, keys); - return getResponse(BuilderFactory.LONG); - } - - public Response sismember(String key, String member) { - client.sismember(key, member); - return getResponse(BuilderFactory.BOOLEAN); - } - - public Response> smembers(String key) { - client.smembers(key); - return getResponse(BuilderFactory.STRING_SET); - } - - public Response smove(String srckey, String dstkey, String member) { - client.smove(srckey, dstkey, member); - return getResponse(BuilderFactory.LONG); - } - - public Response> sort(String key) { - client.sort(key); - return getResponse(BuilderFactory.STRING_LIST); - } - - public Response> sort(String key, - SortingParams sortingParameters) { - client.sort(key, sortingParameters); - return getResponse(BuilderFactory.STRING_LIST); - } - - public Response> sort(String key, - SortingParams sortingParameters, String dstkey) { - client.sort(key, sortingParameters, dstkey); - return getResponse(BuilderFactory.STRING_LIST); - } - - public Response> sort(String key, String dstkey) { - client.sort(key, dstkey); - return getResponse(BuilderFactory.STRING_LIST); - } - - public Response spop(String key) { - client.spop(key); - return getResponse(BuilderFactory.STRING); - } - - public Response srandmember(String key) { - client.srandmember(key); - return getResponse(BuilderFactory.STRING); - } - - public Response srem(String key, String member) { - client.srem(key, member); - return getResponse(BuilderFactory.LONG); - } - - public Response strlen(String key) { - client.strlen(key); - return getResponse(BuilderFactory.LONG); - } - - public Response substr(String key, int start, int end) { - client.substr(key, start, end); - return getResponse(BuilderFactory.STRING); - } - - public Response> sunion(String... keys) { - client.sunion(keys); - return getResponse(BuilderFactory.STRING_SET); - } - - public Response sunionstore(String dstkey, String... keys) { - client.sunionstore(dstkey, keys); - return getResponse(BuilderFactory.LONG); - } - - public Response ttl(String key) { - client.ttl(key); - return getResponse(BuilderFactory.LONG); - } - - public Response type(String key) { - client.type(key); - return getResponse(BuilderFactory.STRING); - } - - public Response zadd(String key, double score, String member) { - client.zadd(key, score, member); - return getResponse(BuilderFactory.LONG); - } - - public Response zcard(String key) { - client.zcard(key); - return getResponse(BuilderFactory.LONG); - } - - public Response zcount(String key, double min, double max) { - client.zcount(key, min, max); - return getResponse(BuilderFactory.LONG); - } - - public Response zincrby(String key, double score, String member) { - client.zincrby(key, score, member); - return getResponse(BuilderFactory.DOUBLE); - } - - public Response zinterstore(String dstkey, String... sets) { - client.zinterstore(dstkey, sets); - return getResponse(BuilderFactory.LONG); - } - - public Response zinterstore(String dstkey, ZParams params, - String... sets) { - client.zinterstore(dstkey, params, sets); - return getResponse(BuilderFactory.LONG); - } - - public Response> zrange(String key, int start, int end) { - client.zrange(key, start, end); - return getResponse(BuilderFactory.STRING_ZSET); - } - - public Response> zrangeByScore(String key, double min, - double max) { - client.zrangeByScore(key, min, max); - return getResponse(BuilderFactory.STRING_ZSET); - } - - public Response> zrangeByScore(String key, String min, - String max) { - client.zrangeByScore(key, min, max); - return getResponse(BuilderFactory.STRING_ZSET); - } - - public Response> zrangeByScore(String key, double min, - double max, int offset, int count) { - client.zrangeByScore(key, min, max, offset, count); - return getResponse(BuilderFactory.STRING_ZSET); - } - - public Response> zrangeByScoreWithScores(String key, double min, - double max) { - client.zrangeByScoreWithScores(key, min, max); - return getResponse(BuilderFactory.TUPLE_ZSET); - } - - public Response> zrangeByScoreWithScores(String key, double min, - double max, int offset, int count) { - client.zrangeByScoreWithScores(key, min, max, offset, count); - return getResponse(BuilderFactory.TUPLE_ZSET); - } - - public Response> zrangeWithScores(String key, int start, int end) { - client.zrangeWithScores(key, start, end); - return getResponse(BuilderFactory.TUPLE_ZSET); - } - - public Response zrank(String key, String member) { - client.zrank(key, member); - return getResponse(BuilderFactory.LONG); - } - - public Response zrem(String key, String member) { - client.zrem(key, member); - return getResponse(BuilderFactory.LONG); - } - - public Response zremrangeByRank(String key, int start, int end) { - client.zremrangeByRank(key, start, end); - return getResponse(BuilderFactory.LONG); - } - - public Response zremrangeByScore(String key, double start, double end) { - client.zremrangeByScore(key, start, end); - return getResponse(BuilderFactory.LONG); - } - - public Response> zrevrange(String key, int start, int end) { - client.zrevrange(key, start, end); - return getResponse(BuilderFactory.STRING_ZSET); - } - - public Response> zrevrangeWithScores(String key, int start, - int end) { - client.zrevrangeWithScores(key, start, end); - return getResponse(BuilderFactory.TUPLE_ZSET); - } - - public Response zrevrank(String key, String member) { - client.zrevrank(key, member); - return getResponse(BuilderFactory.LONG); - } - - public Response zscore(String key, String member) { - client.zscore(key, member); - return getResponse(BuilderFactory.DOUBLE); - } - - public Response zunionstore(String dstkey, String... sets) { - client.zunionstore(dstkey, sets); - return getResponse(BuilderFactory.LONG); - } - - public Response zunionstore(String dstkey, ZParams params, - String... sets) { - client.zunionstore(dstkey, params, sets); - return getResponse(BuilderFactory.LONG); - } - - public Response bgrewriteaof() { - client.bgrewriteaof(); - return getResponse(BuilderFactory.STRING); - } - - public Response bgsave() { - client.bgsave(); - return getResponse(BuilderFactory.STRING); - } - - public Response configGet(String pattern) { - client.configGet(pattern); - return getResponse(BuilderFactory.STRING); - } - - public Response configSet(String parameter, String value) { - client.configSet(parameter, value); - return getResponse(BuilderFactory.STRING); - } - - public Response brpoplpush(String source, String destination, - int timeout) { - client.brpoplpush(source, destination, timeout); - return getResponse(BuilderFactory.STRING); - } - - public Response configResetStat() { - client.configResetStat(); - return getResponse(BuilderFactory.STRING); - } - - public Response save() { - client.save(); - return getResponse(BuilderFactory.STRING); - } - - public Response lastsave() { - client.lastsave(); - return getResponse(BuilderFactory.LONG); - } - - public Response publish(String channel, String message) { - client.publish(channel, message); - return getResponse(BuilderFactory.LONG); - } - - public Response publish(byte[] channel, byte[] message) { - client.publish(channel, message); - return getResponse(BuilderFactory.LONG); + this.client = client; + } + + @Override + protected Client getClient(String key) { + return client; + } + + @Override + protected Client getClient(byte[] key) { + return client; + } + + public List exec() { + client.exec(); + client.getAll(1); // Discard all but the last reply + + List unformatted = client.getObjectMultiBulkReply(); + if (unformatted == null) { + return null; + } + List formatted = new ArrayList(); + for (Object o : unformatted) { + try { + formatted.add(generateResponse(o).get()); + } catch (JedisDataException e) { + formatted.add(e); + } + } + return formatted; + } + + public List> execGetResponse() { + client.exec(); + client.getAll(1); // Discard all but the last reply + + List unformatted = client.getObjectMultiBulkReply(); + if (unformatted == null) { + return null; + } + List> response = new ArrayList>(); + for (Object o : unformatted) { + response.add(generateResponse(o)); + } + return response; + } + + public String discard() { + client.discard(); + client.getAll(1); // Discard all but the last reply + inTransaction = false; + clean(); + return client.getStatusCodeReply(); } } \ No newline at end of file diff --git a/src/main/java/redis/clients/jedis/TransactionBlock.java b/src/main/java/redis/clients/jedis/TransactionBlock.java index 87df232..e784e19 100644 --- a/src/main/java/redis/clients/jedis/TransactionBlock.java +++ b/src/main/java/redis/clients/jedis/TransactionBlock.java @@ -13,6 +13,6 @@ public abstract class TransactionBlock extends Transaction { public abstract void execute() throws JedisException; public void setClient(Client client) { - this.client = client; + this.client = client; } } diff --git a/src/main/java/redis/clients/util/JedisByteHashMap.java b/src/main/java/redis/clients/util/JedisByteHashMap.java index d9c7866..e13f3b0 100644 --- a/src/main/java/redis/clients/util/JedisByteHashMap.java +++ b/src/main/java/redis/clients/util/JedisByteHashMap.java @@ -88,7 +88,7 @@ public class JedisByteHashMap implements Map, Cloneable, return internalMap.values(); } - private final class ByteArrayWrapper { + private static final class ByteArrayWrapper { private final byte[] data; public ByteArrayWrapper(byte[] data) { @@ -110,7 +110,7 @@ public class JedisByteHashMap implements Map, Cloneable, } } - private final class JedisByteEntry implements Entry { + private static final class JedisByteEntry implements Entry { private byte[] value; private byte[] key; diff --git a/src/main/java/redis/clients/util/Pool.java b/src/main/java/redis/clients/util/Pool.java index 3f74370..ab21d30 100644 --- a/src/main/java/redis/clients/util/Pool.java +++ b/src/main/java/redis/clients/util/Pool.java @@ -7,8 +7,12 @@ import redis.clients.jedis.exceptions.JedisConnectionException; import redis.clients.jedis.exceptions.JedisException; public abstract class Pool { - private final GenericObjectPool internalPool; + protected GenericObjectPool internalPool; + protected Pool() { + this.internalPool = null; + } + public Pool(final GenericObjectPool.Config poolConfig, PoolableObjectFactory factory) { this.internalPool = new GenericObjectPool(factory, poolConfig); @@ -23,8 +27,8 @@ public abstract class Pool { "Could not get a resource from the pool", e); } } - - public void returnResource(final T resource) { + + public void returnResourceObject(final Object resource) { try { internalPool.returnObject(resource); } catch (Exception e) { @@ -32,8 +36,16 @@ public abstract class Pool { "Could not return the resource to the pool", e); } } - + public void returnBrokenResource(final T resource) { + returnBrokenResourceObject(resource); + } + + public void returnResource(final T resource) { + returnResourceObject(resource); + } + + protected void returnBrokenResourceObject(final Object resource) { try { internalPool.invalidateObject(resource); } catch (Exception e) { diff --git a/src/main/java/redis/clients/util/RedisInputStream.java b/src/main/java/redis/clients/util/RedisInputStream.java index f30ddf5..221e756 100644 --- a/src/main/java/redis/clients/util/RedisInputStream.java +++ b/src/main/java/redis/clients/util/RedisInputStream.java @@ -21,7 +21,6 @@ import java.io.IOException; import java.io.InputStream; import redis.clients.jedis.exceptions.JedisConnectionException; -import redis.clients.jedis.exceptions.JedisException; public class RedisInputStream extends FilterInputStream { @@ -84,7 +83,7 @@ public class RedisInputStream extends FilterInputStream { } } } catch (IOException e) { - throw new JedisException(e); + throw new JedisConnectionException(e); } String reply = sb.toString(); if (reply.length() == 0) { diff --git a/src/main/java/redis/clients/util/SafeEncoder.java b/src/main/java/redis/clients/util/SafeEncoder.java index 8e88c40..a00e6c4 100644 --- a/src/main/java/redis/clients/util/SafeEncoder.java +++ b/src/main/java/redis/clients/util/SafeEncoder.java @@ -11,6 +11,14 @@ import redis.clients.jedis.exceptions.JedisException; * */ public class SafeEncoder { + public static byte[][] encodeMany(final String... strs){ + byte[][] many = new byte[strs.length][]; + for(int i=0;i> { public S getShardInfo(byte[] key) { SortedMap tail = nodes.tailMap(algo.hash(key)); - if (tail.size() == 0) { + if (tail.isEmpty()) { return nodes.get(nodes.firstKey()); } return tail.get(tail.firstKey()); diff --git a/src/main/java/redis/clients/util/Slowlog.java b/src/main/java/redis/clients/util/Slowlog.java new file mode 100644 index 0000000..0a0e7be --- /dev/null +++ b/src/main/java/redis/clients/util/Slowlog.java @@ -0,0 +1,53 @@ +package redis.clients.util; + +import java.util.ArrayList; +import java.util.List; + +public class Slowlog { + private final long id; + private final long timeStamp; + private final long executionTime; + private final List args; + + @SuppressWarnings("unchecked") + public static List from(List nestedMultiBulkReply){ + List logs = new ArrayList(nestedMultiBulkReply.size()); + for(Object obj : nestedMultiBulkReply){ + List properties = (List)obj; + logs.add(new Slowlog(properties)); + } + + return logs; + } + + @SuppressWarnings("unchecked") + private Slowlog(List properties) { + super(); + this.id = (Long)properties.get(0); + this.timeStamp = (Long)properties.get(1); + this.executionTime = (Long)properties.get(2); + + List bargs = (List)properties.get(3); + this.args = new ArrayList(bargs.size()); + + for(byte[] barg:bargs){ + this.args.add(SafeEncoder.encode(barg)); + } + } + + public long getId() { + return id; + } + + public long getTimeStamp() { + return timeStamp; + } + + public long getExecutionTime() { + return executionTime; + } + + public List getArgs() { + return args; + } +} diff --git a/src/test/java/redis/clients/jedis/tests/ConnectionTest.java b/src/test/java/redis/clients/jedis/tests/ConnectionTest.java index 84d76b5..eee7ac9 100644 --- a/src/test/java/redis/clients/jedis/tests/ConnectionTest.java +++ b/src/test/java/redis/clients/jedis/tests/ConnectionTest.java @@ -33,4 +33,12 @@ public class ConnectionTest extends Assert { client.setPort(55665); client.connect(); } + + @Test + public void connectIfNotConnectedWhenSettingTimeoutInfinite() { + client.setHost("localhost"); + client.setPort(6379); + client.setTimeoutInfinite(); + } + } \ No newline at end of file diff --git a/src/test/java/redis/clients/jedis/tests/JedisPoolTest.java b/src/test/java/redis/clients/jedis/tests/JedisPoolTest.java index 158adbf..ba9db10 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisPoolTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisPoolTest.java @@ -1,10 +1,14 @@ package redis.clients.jedis.tests; +import java.net.URI; +import java.net.URISyntaxException; + import org.apache.commons.pool.impl.GenericObjectPool; import org.apache.commons.pool.impl.GenericObjectPool.Config; import org.junit.Assert; import org.junit.Test; +import redis.clients.jedis.BinaryJedis; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; @@ -95,4 +99,55 @@ public class JedisPoolTest extends Assert { pool.returnResource(jedis); pool.destroy(); } + + @Test + public void nonDefaultDatabase() { + JedisPool pool0 = new JedisPool(new JedisPoolConfig(), hnp.host, + hnp.port, 2000, "foobared"); + Jedis jedis0 = pool0.getResource(); + jedis0.set("foo", "bar"); + assertEquals( "bar", jedis0.get("foo") ); + pool0.returnResource(jedis0); + pool0.destroy(); + + JedisPool pool1 = new JedisPool(new JedisPoolConfig(), hnp.host, + hnp.port, 2000, "foobared", 1); + Jedis jedis1 = pool1.getResource(); + assertNull( jedis1.get("foo") ); + pool1.returnResource(jedis0); + pool1.destroy(); + } + + @Test + public void returnBinary() { + JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.host, + hnp.port, 2000); + BinaryJedis jedis = pool.getResource(); + pool.returnResource(jedis); + pool.destroy(); + } + + @Test + public void startWithUrlString() { + Jedis j = new Jedis("localhost", 6380); + j.auth("foobared"); + j.select(2); + j.set("foo", "bar"); + JedisPool pool = new JedisPool("redis://:foobared@localhost:6380/2"); + Jedis jedis = pool.getResource(); + assertEquals("PONG", jedis.ping()); + assertEquals("bar", jedis.get("foo")); + } + + @Test + public void startWithUrl() throws URISyntaxException { + Jedis j = new Jedis("localhost", 6380); + j.auth("foobared"); + j.select(2); + j.set("foo", "bar"); + JedisPool pool = new JedisPool(new URI("redis://:foobared@localhost:6380/2")); + Jedis jedis = pool.getResource(); + assertEquals("PONG", jedis.ping()); + assertEquals("bar", jedis.get("foo")); + } } \ No newline at end of file diff --git a/src/test/java/redis/clients/jedis/tests/JedisSentinelTest.java b/src/test/java/redis/clients/jedis/tests/JedisSentinelTest.java new file mode 100644 index 0000000..7cd8188 --- /dev/null +++ b/src/test/java/redis/clients/jedis/tests/JedisSentinelTest.java @@ -0,0 +1,68 @@ +package redis.clients.jedis.tests; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertFalse; +import static junit.framework.Assert.assertTrue; + +import java.util.List; +import java.util.Map; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import redis.clients.jedis.Jedis; + +public class JedisSentinelTest { + private static final String MASTER_NAME = "mymaster"; + + @Before + public void setup() throws InterruptedException { + Jedis j = new Jedis("localhost", 6380); + j.auth("foobared"); + j.configSet("masterauth", "foobared"); + j.slaveof("localhost", 6379); + // TODO: The sleep is to give time to the slave to synchronize with the + // master and also let know the sentinels about this new topology. We + // should find a better way to do this. + Thread.sleep(5000); + } + + @After + public void clear() { + Jedis j = new Jedis("localhost", 6380); + j.auth("foobared"); + j.slaveofNoOne(); + } + + @Test + public void sentinel() { + Jedis j = new Jedis("localhost", 26379); + List> masters = j.sentinelMasters(); + final String masterName = masters.get(0).get("name"); + + assertEquals(MASTER_NAME, masterName); + + List masterHostAndPort = j + .sentinelGetMasterAddrByName(masterName); + assertEquals("127.0.0.1", masterHostAndPort.get(0)); + assertEquals("6379", masterHostAndPort.get(1)); + + List> slaves = j.sentinelSlaves(masterName); + assertEquals("6379", slaves.get(0).get("master-port")); + + List isMasterDownByAddr = j + .sentinelIsMasterDownByAddr("127.0.0.1", 6379); + assertEquals(Long.valueOf(0), (Long) isMasterDownByAddr.get(0)); + assertFalse("?".equals(isMasterDownByAddr.get(1))); + + isMasterDownByAddr = j.sentinelIsMasterDownByAddr("127.0.0.1", 1); + assertEquals(Long.valueOf(0), (Long) isMasterDownByAddr.get(0)); + assertTrue("?".equals(isMasterDownByAddr.get(1))); + + // DO NOT RE-RUN TEST TOO FAST, RESET TAKES SOME TIME TO... RESET + assertEquals(Long.valueOf(1), j.sentinelReset(masterName)); + assertEquals(Long.valueOf(0), j.sentinelReset("woof" + masterName)); + + } +} diff --git a/src/test/java/redis/clients/jedis/tests/JedisTest.java b/src/test/java/redis/clients/jedis/tests/JedisTest.java index 4bd7b59..bfa71ea 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisTest.java @@ -1,6 +1,8 @@ package redis.clients.jedis.tests; import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; import java.util.HashMap; import java.util.Map; @@ -17,56 +19,78 @@ import redis.clients.util.SafeEncoder; public class JedisTest extends JedisCommandTestBase { @Test public void useWithoutConnecting() { - Jedis jedis = new Jedis("localhost"); - jedis.auth("foobared"); - jedis.dbSize(); + Jedis jedis = new Jedis("localhost"); + jedis.auth("foobared"); + jedis.dbSize(); } @Test public void checkBinaryData() { - byte[] bigdata = new byte[1777]; - for (int b = 0; b < bigdata.length; b++) { - bigdata[b] = (byte) ((byte) b % 255); - } - Map hash = new HashMap(); - hash.put("data", SafeEncoder.encode(bigdata)); + byte[] bigdata = new byte[1777]; + for (int b = 0; b < bigdata.length; b++) { + bigdata[b] = (byte) ((byte) b % 255); + } + Map hash = new HashMap(); + hash.put("data", SafeEncoder.encode(bigdata)); - String status = jedis.hmset("foo", hash); - assertEquals("OK", status); - assertEquals(hash, jedis.hgetAll("foo")); + String status = jedis.hmset("foo", hash); + assertEquals("OK", status); + assertEquals(hash, jedis.hgetAll("foo")); } @Test public void connectWithShardInfo() { - JedisShardInfo shardInfo = new JedisShardInfo("localhost", - Protocol.DEFAULT_PORT); - shardInfo.setPassword("foobared"); - Jedis jedis = new Jedis(shardInfo); - jedis.get("foo"); + JedisShardInfo shardInfo = new JedisShardInfo("localhost", + Protocol.DEFAULT_PORT); + shardInfo.setPassword("foobared"); + Jedis jedis = new Jedis(shardInfo); + jedis.get("foo"); } @Test(expected = JedisConnectionException.class) public void timeoutConnection() throws Exception { - jedis = new Jedis("localhost", 6379, 15000); - jedis.auth("foobared"); - jedis.configSet("timeout", "1"); - // we need to sleep a long time since redis check for idle connections - // every 10 seconds or so - Thread.sleep(20000); - jedis.hmget("foobar", "foo"); + jedis = new Jedis("localhost", 6379, 15000); + jedis.auth("foobared"); + jedis.configSet("timeout", "1"); + // we need to sleep a long time since redis check for idle connections + // every 10 seconds or so + Thread.sleep(20000); + jedis.hmget("foobar", "foo"); } @Test(expected = JedisDataException.class) public void failWhenSendingNullValues() { - jedis.set("foo", null); + jedis.set("foo", null); } @Test public void shouldReconnectToSameDB() throws IOException { - jedis.select(1); - jedis.set("foo", "bar"); - jedis.getClient().getSocket().shutdownInput(); - jedis.getClient().getSocket().shutdownOutput(); - assertEquals("bar", jedis.get("foo")); + jedis.select(1); + jedis.set("foo", "bar"); + jedis.getClient().getSocket().shutdownInput(); + jedis.getClient().getSocket().shutdownOutput(); + assertEquals("bar", jedis.get("foo")); + } + + @Test + public void startWithUrlString() { + Jedis j = new Jedis("localhost", 6380); + j.auth("foobared"); + j.select(2); + j.set("foo", "bar"); + Jedis jedis = new Jedis("redis://:foobared@localhost:6380/2"); + assertEquals("PONG", jedis.ping()); + assertEquals("bar", jedis.get("foo")); + } + + @Test + public void startWithUrl() throws URISyntaxException { + Jedis j = new Jedis("localhost", 6380); + j.auth("foobared"); + j.select(2); + j.set("foo", "bar"); + Jedis jedis = new Jedis(new URI("redis://:foobared@localhost:6380/2")); + assertEquals("PONG", jedis.ping()); + assertEquals("bar", jedis.get("foo")); } } \ No newline at end of file diff --git a/src/test/java/redis/clients/jedis/tests/PipeliningTest.java b/src/test/java/redis/clients/jedis/tests/PipeliningTest.java old mode 100644 new mode 100755 index 82d28a4..ffe2c9b --- a/src/test/java/redis/clients/jedis/tests/PipeliningTest.java +++ b/src/test/java/redis/clients/jedis/tests/PipeliningTest.java @@ -8,10 +8,7 @@ import redis.clients.jedis.exceptions.JedisDataException; import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort; import java.io.UnsupportedEncodingException; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.UUID; +import java.util.*; public class PipeliningTest extends Assert { private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0); @@ -82,13 +79,95 @@ public class PipeliningTest extends Assert { assertEquals("foo", zset.get().iterator().next()); assertEquals("foo", set.get()); assertEquals(false, blist.get()); - assertEquals(new Double(2), zincrby.get()); - assertEquals(new Long(1), zcard.get()); + assertEquals(Double.valueOf(2), zincrby.get()); + assertEquals(Long.valueOf(1), zcard.get()); assertEquals(1, lrange.get().size()); assertNotNull(hgetAll.get().get("foo")); assertEquals(1, smembers.get().size()); assertEquals(1, zrangeWithScores.get().size()); } + + @Test + public void pipelineResponseWithData() { + jedis.zadd("zset", 1, "foo"); + + Pipeline p = jedis.pipelined(); + Response score = p.zscore("zset", "foo"); + p.sync(); + + assertNotNull(score.get()); + } + + @Test + public void pipelineBinarySafeHashCommands() { + jedis.hset("key".getBytes(), "f1".getBytes(), "v111".getBytes()); + jedis.hset("key".getBytes(), "f22".getBytes(), "v2222".getBytes()); + + Pipeline p = jedis.pipelined(); + Response> fmap = p.hgetAll("key".getBytes()); + Response> fkeys = p.hkeys("key".getBytes()); + Response> fordered = p.hmget("key".getBytes(), "f22".getBytes(), "f1".getBytes()); + Response> fvals = p.hvals("key".getBytes()); + p.sync(); + + assertNotNull(fmap.get()); + // we have to do these strange contortions because byte[] is not a very good key + // for a java Map. It only works with equality (you need the exact key object to retrieve + // the value) I recommend we switch to using ByteBuffer or something similar: + // http://stackoverflow.com/questions/1058149/using-a-byte-array-as-hashmap-key-java + Map map = fmap.get(); + Set mapKeys = map.keySet(); + Iterator iterMap = mapKeys.iterator(); + byte[] firstMapKey = iterMap.next(); + byte[] secondMapKey = iterMap.next(); + assertFalse(iterMap.hasNext()); + verifyHasBothValues(firstMapKey, secondMapKey, "f1".getBytes(), "f22".getBytes()); + byte[] firstMapValue = map.get(firstMapKey); + byte[] secondMapValue = map.get(secondMapKey); + verifyHasBothValues(firstMapValue, secondMapValue, "v111".getBytes(), "v2222".getBytes()); + + assertNotNull(fkeys.get()); + Iterator iter = fkeys.get().iterator(); + byte[] firstKey = iter.next(); + byte[] secondKey = iter.next(); + assertFalse(iter.hasNext()); + verifyHasBothValues(firstKey, secondKey, "f1".getBytes(), "f22".getBytes()); + + assertNotNull(fordered.get()); + assertArrayEquals("v2222".getBytes(), fordered.get().get(0)); + assertArrayEquals("v111".getBytes(), fordered.get().get(1)); + + assertNotNull(fvals.get()); + assertEquals(2, fvals.get().size()); + byte[] firstValue = fvals.get().get(0); + byte[] secondValue = fvals.get().get(1); + verifyHasBothValues(firstValue, secondValue, "v111".getBytes(), "v2222".getBytes()); + } + + private void verifyHasBothValues(byte[] firstKey, byte[] secondKey, byte[] value1, byte[] value2) { + assertFalse(Arrays.equals(firstKey, secondKey)); + assertTrue(Arrays.equals(firstKey, value1) || Arrays.equals(firstKey, value2)); + assertTrue(Arrays.equals(secondKey, value1) || Arrays.equals(secondKey, value2)); + } + + @Test + public void pipelineSelect() { + Pipeline p = jedis.pipelined(); + p.select(1); + p.sync(); + } + + @Test + public void pipelineResponseWithoutData() { + jedis.zadd("zset", 1, "foo"); + + Pipeline p = jedis.pipelined(); + Response score = p.zscore("zset", "bar"); + p.sync(); + + assertNull(score.get()); + } + @Test(expected = JedisDataException.class) public void pipelineResponseWithinPipeline() { @@ -118,4 +197,49 @@ public class PipeliningTest extends Assert { p.sync(); assertNull(shouldNotExist.get()); } -} \ No newline at end of file + + @Test + public void piplineWithError(){ + Pipeline p = jedis.pipelined(); + p.set("foo", "bar"); + Response> error = p.smembers("foo"); + Response r = p.get("foo"); + p.sync(); + try{ + error.get(); + fail(); + }catch(JedisDataException e){ + //that is fine we should be here + } + assertEquals(r.get(), "bar"); + } + + @Test + public void multi(){ + Pipeline p = jedis.pipelined(); + p.multi(); + Response r1 = p.hincrBy("a", "f1", -1); + Response r2 = p.hincrBy("a", "f1", -2); + Response> r3 = p.exec(); + List result = p.syncAndReturnAll(); + + assertEquals(new Long(-1), r1.get()); + assertEquals(new Long(-3), r2.get()); + + assertEquals(4, result.size()); + + assertEquals("OK", result.get(0)); + assertEquals("QUEUED", result.get(1)); + assertEquals("QUEUED", result.get(2)); + + //4th result is a list with the results from the multi + @SuppressWarnings("unchecked") + List multiResult = (List) result.get(3); + assertEquals(new Long(-1), multiResult.get(0)); + assertEquals(new Long(-3), multiResult.get(1)); + + assertEquals(new Long(-1), r3.get().get(0)); + assertEquals(new Long(-3), r3.get().get(1)); + + } +} diff --git a/src/test/java/redis/clients/jedis/tests/ProtocolTest.java b/src/test/java/redis/clients/jedis/tests/ProtocolTest.java index e0b9fb1..60b82aa 100644 --- a/src/test/java/redis/clients/jedis/tests/ProtocolTest.java +++ b/src/test/java/redis/clients/jedis/tests/ProtocolTest.java @@ -24,8 +24,7 @@ public class ProtocolTest extends JedisTestBase { PipedOutputStream pos = new PipedOutputStream(pis); RedisOutputStream ros = new RedisOutputStream(pos); - Protocol protocol = new Protocol(); - protocol.sendCommand(ros, Protocol.Command.GET, + Protocol.sendCommand(ros, Protocol.Command.GET, "SOMEKEY".getBytes(Protocol.CHARSET)); ros.flush(); pos.close(); @@ -43,8 +42,7 @@ public class ProtocolTest extends JedisTestBase { @Test public void bulkReply() { InputStream is = new ByteArrayInputStream("$6\r\nfoobar\r\n".getBytes()); - Protocol protocol = new Protocol(); - byte[] response = (byte[]) protocol.read(new RedisInputStream(is)); + byte[] response = (byte[]) Protocol.read(new RedisInputStream(is)); assertArrayEquals(SafeEncoder.encode("foobar"), response); } @@ -52,8 +50,7 @@ public class ProtocolTest extends JedisTestBase { public void fragmentedBulkReply() { FragmentedByteArrayInputStream fis = new FragmentedByteArrayInputStream( "$30\r\n012345678901234567890123456789\r\n".getBytes()); - Protocol protocol = new Protocol(); - byte[] response = (byte[]) protocol.read(new RedisInputStream(fis)); + byte[] response = (byte[]) Protocol.read(new RedisInputStream(fis)); assertArrayEquals(SafeEncoder.encode("012345678901234567890123456789"), response); } @@ -61,24 +58,21 @@ public class ProtocolTest extends JedisTestBase { @Test public void nullBulkReply() { InputStream is = new ByteArrayInputStream("$-1\r\n".getBytes()); - Protocol protocol = new Protocol(); - String response = (String) protocol.read(new RedisInputStream(is)); + String response = (String) Protocol.read(new RedisInputStream(is)); assertEquals(null, response); } @Test public void singleLineReply() { InputStream is = new ByteArrayInputStream("+OK\r\n".getBytes()); - Protocol protocol = new Protocol(); - byte[] response = (byte[]) protocol.read(new RedisInputStream(is)); + byte[] response = (byte[]) Protocol.read(new RedisInputStream(is)); assertArrayEquals(SafeEncoder.encode("OK"), response); } @Test public void integerReply() { InputStream is = new ByteArrayInputStream(":123\r\n".getBytes()); - Protocol protocol = new Protocol(); - long response = (Long) protocol.read(new RedisInputStream(is)); + long response = (Long) Protocol.read(new RedisInputStream(is)); assertEquals(123, response); } @@ -88,8 +82,7 @@ public class ProtocolTest extends JedisTestBase { InputStream is = new ByteArrayInputStream( "*4\r\n$3\r\nfoo\r\n$3\r\nbar\r\n$5\r\nHello\r\n$5\r\nWorld\r\n" .getBytes()); - Protocol protocol = new Protocol(); - List response = (List) protocol + List response = (List) Protocol .read(new RedisInputStream(is)); List expected = new ArrayList(); expected.add(SafeEncoder.encode("foo")); @@ -104,8 +97,7 @@ public class ProtocolTest extends JedisTestBase { @Test public void nullMultiBulkReply() { InputStream is = new ByteArrayInputStream("*-1\r\n".getBytes()); - Protocol protocol = new Protocol(); - List response = (List) protocol + List response = (List) Protocol .read(new RedisInputStream(is)); assertNull(response); } diff --git a/src/test/java/redis/clients/jedis/tests/ShardedJedisPipelineTest.java b/src/test/java/redis/clients/jedis/tests/ShardedJedisPipelineTest.java new file mode 100644 index 0000000..7574f08 --- /dev/null +++ b/src/test/java/redis/clients/jedis/tests/ShardedJedisPipelineTest.java @@ -0,0 +1,128 @@ +package redis.clients.jedis.tests; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNull; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; + +import java.io.UnsupportedEncodingException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.UUID; + +import org.junit.Before; +import org.junit.Test; + +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisShardInfo; +import redis.clients.jedis.Response; +import redis.clients.jedis.ShardedJedis; +import redis.clients.jedis.ShardedJedisPipeline; +import redis.clients.jedis.Tuple; +import redis.clients.jedis.exceptions.JedisDataException; + +public class ShardedJedisPipelineTest { + private static HostAndPortUtil.HostAndPort redis1 = HostAndPortUtil + .getRedisServers().get(0); + private static HostAndPortUtil.HostAndPort redis2 = HostAndPortUtil + .getRedisServers().get(1); + + private ShardedJedis jedis; + + @Before + public void setUp() throws Exception { + Jedis jedis = new Jedis(redis1.host, redis1.port); + jedis.auth("foobared"); + jedis.flushAll(); + jedis.disconnect(); + jedis = new Jedis(redis2.host, redis2.port); + jedis.auth("foobared"); + jedis.flushAll(); + jedis.disconnect(); + + JedisShardInfo shardInfo1 = new JedisShardInfo(redis1.host, redis1.port); + JedisShardInfo shardInfo2 = new JedisShardInfo(redis2.host, redis2.port); + shardInfo1.setPassword("foobared"); + shardInfo2.setPassword("foobared"); + List shards = new ArrayList(); + shards.add(shardInfo1); + shards.add(shardInfo2); + this.jedis = new ShardedJedis(shards); + } + + @Test + public void pipeline() throws UnsupportedEncodingException { + ShardedJedisPipeline p = jedis.pipelined(); + p.set("foo", "bar"); + p.get("foo"); + List results = p.syncAndReturnAll(); + + assertEquals(2, results.size()); + assertEquals("OK", results.get(0)); + assertEquals("bar", results.get(1)); + } + + @Test + public void pipelineResponse() { + jedis.set("string", "foo"); + jedis.lpush("list", "foo"); + jedis.hset("hash", "foo", "bar"); + jedis.zadd("zset", 1, "foo"); + jedis.sadd("set", "foo"); + + ShardedJedisPipeline p = jedis.pipelined(); + Response string = p.get("string"); + Response del = p.del("string"); + Response emptyString = p.get("string"); + Response list = p.lpop("list"); + Response hash = p.hget("hash", "foo"); + Response> zset = p.zrange("zset", 0, -1); + Response set = p.spop("set"); + Response blist = p.exists("list"); + Response zincrby = p.zincrby("zset", 1, "foo"); + Response zcard = p.zcard("zset"); + p.lpush("list", "bar"); + Response> lrange = p.lrange("list", 0, -1); + Response> hgetAll = p.hgetAll("hash"); + p.sadd("set", "foo"); + Response> smembers = p.smembers("set"); + Response> zrangeWithScores = p.zrangeWithScores("zset", 0, + -1); + p.sync(); + + assertEquals("foo", string.get()); + assertEquals(Long.valueOf(1), del.get()); + assertNull(emptyString.get()); + assertEquals("foo", list.get()); + assertEquals("bar", hash.get()); + assertEquals("foo", zset.get().iterator().next()); + assertEquals("foo", set.get()); + assertFalse(blist.get()); + assertEquals(Double.valueOf(2), zincrby.get()); + assertEquals(Long.valueOf(1), zcard.get()); + assertEquals(1, lrange.get().size()); + assertNotNull(hgetAll.get().get("foo")); + assertEquals(1, smembers.get().size()); + assertEquals(1, zrangeWithScores.get().size()); + } + + @Test(expected = JedisDataException.class) + public void pipelineResponseWithinPipeline() { + jedis.set("string", "foo"); + + ShardedJedisPipeline p = jedis.pipelined(); + Response string = p.get("string"); + string.get(); + p.sync(); + } + + @Test + public void canRetrieveUnsetKey() { + ShardedJedisPipeline p = jedis.pipelined(); + Response shouldNotExist = p.get(UUID.randomUUID().toString()); + p.sync(); + assertNull(shouldNotExist.get()); + } +} diff --git a/src/test/java/redis/clients/jedis/tests/ShardedJedisPoolTest.java b/src/test/java/redis/clients/jedis/tests/ShardedJedisPoolTest.java index 5444f84..664e767 100644 --- a/src/test/java/redis/clients/jedis/tests/ShardedJedisPoolTest.java +++ b/src/test/java/redis/clients/jedis/tests/ShardedJedisPoolTest.java @@ -1,5 +1,7 @@ package redis.clients.jedis.tests; +import java.net.URI; +import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; @@ -152,8 +154,8 @@ public class ShardedJedisPoolTest extends Assert { shards.set(1, new JedisShardInfo("nohost", 1234)); pool = new ShardedJedisPool(redisConfig, shards); jedis = pool.getResource(); - Long actual = new Long(0); - Long fails = new Long(0); + Long actual = Long.valueOf(0); + Long fails = Long.valueOf(0); for (int i = 0; i < 1000; i++) { try { jedis.get("a-test-" + i); @@ -167,4 +169,60 @@ public class ShardedJedisPoolTest extends Assert { assertEquals(actual, c1); assertEquals(fails, c2); } -} \ No newline at end of file + + @Test + public void startWithUrlString() { + Jedis j = new Jedis("localhost", 6380); + j.auth("foobared"); + j.set("foo", "bar"); + + j = new Jedis("localhost", 6379); + j.auth("foobared"); + j.set("foo", "bar"); + + List shards = new ArrayList(); + shards.add(new JedisShardInfo("redis://:foobared@localhost:6380")); + shards.add(new JedisShardInfo("redis://:foobared@localhost:6379")); + + Config redisConfig = new Config(); + ShardedJedisPool pool = new ShardedJedisPool(redisConfig, shards); + + Jedis[] jedises = pool.getResource().getAllShards().toArray(new Jedis[2]); + + Jedis jedis = jedises[0]; + assertEquals("PONG", jedis.ping()); + assertEquals("bar", jedis.get("foo")); + + jedis = jedises[1]; + assertEquals("PONG", jedis.ping()); + assertEquals("bar", jedis.get("foo")); + } + + @Test + public void startWithUrl() throws URISyntaxException { + Jedis j = new Jedis("localhost", 6380); + j.auth("foobared"); + j.set("foo", "bar"); + + j = new Jedis("localhost", 6379); + j.auth("foobared"); + j.set("foo", "bar"); + + List shards = new ArrayList(); + shards.add(new JedisShardInfo(new URI("redis://:foobared@localhost:6380"))); + shards.add(new JedisShardInfo(new URI("redis://:foobared@localhost:6379"))); + + Config redisConfig = new Config(); + ShardedJedisPool pool = new ShardedJedisPool(redisConfig, shards); + + Jedis[] jedises = pool.getResource().getAllShards().toArray(new Jedis[2]); + + Jedis jedis = jedises[0]; + assertEquals("PONG", jedis.ping()); + assertEquals("bar", jedis.get("foo")); + + jedis = jedises[1]; + assertEquals("PONG", jedis.ping()); + assertEquals("bar", jedis.get("foo")); + } +} diff --git a/src/test/java/redis/clients/jedis/tests/commands/AllKindOfValuesCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/AllKindOfValuesCommandsTest.java index d7df713..390d7ff 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/AllKindOfValuesCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/AllKindOfValuesCommandsTest.java @@ -300,7 +300,7 @@ public class AllKindOfValuesCommandsTest extends JedisCommandTestBase { @Test public void ttl() { long ttl = jedis.ttl("foo"); - assertEquals(-1, ttl); + assertEquals(-2, ttl); jedis.set("foo", "bar"); ttl = jedis.ttl("foo"); @@ -312,7 +312,7 @@ public class AllKindOfValuesCommandsTest extends JedisCommandTestBase { // Binary long bttl = jedis.ttl(bfoo); - assertEquals(-1, bttl); + assertEquals(-2, bttl); jedis.set(bfoo, bbar); bttl = jedis.ttl(bfoo); @@ -457,5 +457,50 @@ public class AllKindOfValuesCommandsTest extends JedisCommandTestBase { byte[] bresult = jedis.echo(SafeEncoder.encode("hello world")); assertArrayEquals(SafeEncoder.encode("hello world"), bresult); } + + @Test + public void dumpAndRestore() { + jedis.set("foo1", "bar1"); + byte[] sv = jedis.dump("foo1"); + jedis.restore("foo2", 0, sv); + assertTrue(jedis.exists("foo2")); + } + + @Test + public void pexpire() { + long status = jedis.pexpire("foo", 10000); + assertEquals(0, status); + + jedis.set("foo", "bar"); + status = jedis.pexpire("foo", 10000); + assertEquals(1, status); + } + + @Test + public void pexpireAt() { + long unixTime = (System.currentTimeMillis()) + 10000; + + long status = jedis.pexpireAt("foo", unixTime); + assertEquals(0, status); + + jedis.set("foo", "bar"); + unixTime = (System.currentTimeMillis()) + 10000; + status = jedis.pexpireAt("foo", unixTime); + assertEquals(1, status); + } + + @Test + public void pttl() { + long pttl = jedis.pttl("foo"); + assertEquals(-1, pttl); + + jedis.set("foo", "bar"); + pttl = jedis.pttl("foo"); + assertEquals(-1, pttl); + + jedis.pexpire("foo", 20000); + pttl = jedis.pttl("foo"); + assertTrue(pttl >= 0 && pttl <= 20000); + } } \ No newline at end of file diff --git a/src/test/java/redis/clients/jedis/tests/commands/BitCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/BitCommandsTest.java index 204caa8..87b044f 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/BitCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/BitCommandsTest.java @@ -2,6 +2,8 @@ package redis.clients.jedis.tests.commands; import org.junit.Test; +import redis.clients.jedis.BitOP; + public class BitCommandsTest extends JedisCommandTestBase { @Test public void setAndgetbit() { @@ -11,11 +13,11 @@ public class BitCommandsTest extends JedisCommandTestBase { bit = jedis.getbit("foo", 0); assertEquals(true, bit); - long bbit = jedis.setbit("bfoo".getBytes(), 0, "1".getBytes()); - assertEquals(0, bbit); + boolean bbit = jedis.setbit("bfoo".getBytes(), 0, "1".getBytes()); + assertFalse(bbit); bbit = jedis.getbit("bfoo".getBytes(), 0); - assertEquals(1, bbit); + assertTrue(bbit); } @Test @@ -29,4 +31,63 @@ public class BitCommandsTest extends JedisCommandTestBase { assertEquals("Hello", jedis.getrange("key1", 0, 4)); assertEquals("Jedis", jedis.getrange("key1", 6, 11)); } -} \ No newline at end of file + + @Test + public void bitCount() { + jedis.del("foo"); + + jedis.setbit("foo", 16, true); + jedis.setbit("foo", 24, true); + jedis.setbit("foo", 40, true); + jedis.setbit("foo", 56, true); + + long c4 = jedis.bitcount("foo"); + assertEquals(4, c4); + + long c3 = jedis.bitcount("foo", 2L, 5L); + assertEquals(3, c3); + + jedis.del("foo"); + } + + @Test + public void bitOp() + { + jedis.set("key1", "\u0060"); + jedis.set("key2", "\u0044"); + + jedis.bitop(BitOP.AND, "resultAnd", "key1", "key2"); + String resultAnd = jedis.get("resultAnd"); + assertEquals("\u0040", resultAnd); + + jedis.bitop(BitOP.OR, "resultOr", "key1", "key2"); + String resultOr = jedis.get("resultOr"); + assertEquals("\u0064", resultOr); + + jedis.bitop(BitOP.XOR, "resultXor", "key1", "key2"); + String resultXor = jedis.get("resultXor"); + assertEquals("\u0024", resultXor); + + jedis.del("resultAnd"); + jedis.del("resultOr"); + jedis.del("resultXor"); + jedis.del("key1"); + jedis.del("key2"); + } + + @Test + public void bitOpNot() + { + jedis.del("key"); + jedis.setbit("key", 0, true); + jedis.setbit("key", 4, true); + + jedis.bitop(BitOP.NOT, "resultNot", "key"); + + String resultNot = jedis.get("resultNot"); + assertEquals("\u0077", resultNot); + + jedis.del("key"); + jedis.del("resultNot"); + } +} diff --git a/src/test/java/redis/clients/jedis/tests/commands/ControlCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/ControlCommandsTest.java index c6b6698..fbc7d8e 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/ControlCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/ControlCommandsTest.java @@ -29,8 +29,13 @@ public class ControlCommandsTest extends JedisCommandTestBase { @Test public void bgrewriteaof() { + String scheduled = "Background append only file rewriting scheduled"; + String started = "Background append only file rewriting started"; + String status = jedis.bgrewriteaof(); - assertEquals("Background append only file rewriting started", status); + + boolean ok = status.equals(scheduled) || status.equals(started); + assertTrue(ok); } @Test @@ -53,6 +58,8 @@ public class ControlCommandsTest extends JedisCommandTestBase { public void info() { String info = jedis.info(); assertNotNull(info); + info = jedis.info("server"); + assertNotNull(info); } @Test diff --git a/src/test/java/redis/clients/jedis/tests/commands/JedisCommandTestBase.java b/src/test/java/redis/clients/jedis/tests/commands/JedisCommandTestBase.java index 842b48e..c88b388 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/JedisCommandTestBase.java +++ b/src/test/java/redis/clients/jedis/tests/commands/JedisCommandTestBase.java @@ -64,8 +64,8 @@ public abstract class JedisCommandTestBase extends JedisTestBase { } } if (!contained) { - throw new ComparisonFailure("element is missing", next - .toString(), actual.toString()); + throw new ComparisonFailure("element is missing", + Arrays.toString(next), actual.toString()); } } } @@ -93,4 +93,4 @@ public abstract class JedisCommandTestBase extends JedisTestBase { } return false; } -} \ No newline at end of file +} diff --git a/src/test/java/redis/clients/jedis/tests/commands/ListCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/ListCommandsTest.java index a2e5a4b..5653687 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/ListCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/ListCommandsTest.java @@ -30,12 +30,17 @@ public class ListCommandsTest extends JedisCommandTestBase { assertEquals(1, size); size = jedis.rpush("foo", "foo"); assertEquals(2, size); + size = jedis.rpush("foo", "bar", "foo"); + assertEquals(4, size); + // Binary long bsize = jedis.rpush(bfoo, bbar); assertEquals(1, bsize); bsize = jedis.rpush(bfoo, bfoo); assertEquals(2, bsize); + bsize = jedis.rpush(bfoo, bbar, bfoo); + assertEquals(4, bsize); } @@ -45,12 +50,16 @@ public class ListCommandsTest extends JedisCommandTestBase { assertEquals(1, size); size = jedis.lpush("foo", "foo"); assertEquals(2, size); + size = jedis.lpush("foo", "bar", "foo"); + assertEquals(4, size); // Binary long bsize = jedis.lpush(bfoo, bbar); assertEquals(1, bsize); bsize = jedis.lpush(bfoo, bfoo); assertEquals(2, bsize); + bsize = jedis.lpush(bfoo, bbar, bfoo); + assertEquals(4, bsize); } @@ -620,4 +629,4 @@ public class ListCommandsTest extends JedisCommandTestBase { assertEquals("a", jedis.lrange("bar", 0, -1).get(0)); } -} \ No newline at end of file +} diff --git a/src/test/java/redis/clients/jedis/tests/commands/ObjectCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/ObjectCommandsTest.java new file mode 100644 index 0000000..0713753 --- /dev/null +++ b/src/test/java/redis/clients/jedis/tests/commands/ObjectCommandsTest.java @@ -0,0 +1,49 @@ +package redis.clients.jedis.tests.commands; + +import org.junit.Test; + +import redis.clients.util.SafeEncoder; + +public class ObjectCommandsTest extends JedisCommandTestBase { + + private String key = "mylist"; + private byte[] binaryKey = SafeEncoder.encode(key); + + @Test + public void objectRefcount() { + jedis.lpush(key, "hello world"); + Long refcount = jedis.objectRefcount(key); + assertEquals(new Long(1), refcount); + + // Binary + refcount = jedis.objectRefcount(binaryKey); + assertEquals(new Long(1), refcount); + + } + + @Test + public void objectEncoding() { + jedis.lpush(key, "hello world"); + String encoding = jedis.objectEncoding(key); + assertEquals("ziplist", encoding); + + // Binary + encoding = SafeEncoder.encode(jedis.objectEncoding(binaryKey)); + assertEquals("ziplist", encoding); + } + + @Test + public void objectIdletime() throws InterruptedException { + jedis.lpush(key, "hello world"); + + // Wait a little bit more than 10 seconds so the idle time is 10 + // seconds. + Thread.sleep(10001); + Long time = jedis.objectIdletime(key); + assertEquals(new Long(10), time); + + // Binary + time = jedis.objectIdletime(binaryKey); + assertEquals(new Long(10), time); + } +} \ No newline at end of file diff --git a/src/test/java/redis/clients/jedis/tests/commands/PublishSubscribeCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/PublishSubscribeCommandsTest.java index 39f8465..c7a94f4 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/PublishSubscribeCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/PublishSubscribeCommandsTest.java @@ -4,6 +4,7 @@ import java.io.IOException; import java.net.UnknownHostException; import java.util.Arrays; +import org.junit.Ignore; import org.junit.Test; import redis.clients.jedis.BinaryJedisPubSub; @@ -471,7 +472,7 @@ public class PublishSubscribeCommandsTest extends JedisCommandTestBase { t.join(); } - @Test + @Test @Ignore public void subscribeWithoutConnecting() { try { Jedis jedis = new Jedis(hnp.host, hnp.port); @@ -527,4 +528,4 @@ public class PublishSubscribeCommandsTest extends JedisCommandTestBase { }; pubsub.unsubscribe(); } -} \ No newline at end of file +} diff --git a/src/test/java/redis/clients/jedis/tests/commands/ScriptingCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/ScriptingCommandsTest.java new file mode 100644 index 0000000..a3675d4 --- /dev/null +++ b/src/test/java/redis/clients/jedis/tests/commands/ScriptingCommandsTest.java @@ -0,0 +1,152 @@ +package redis.clients.jedis.tests.commands; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Test; + +import redis.clients.jedis.exceptions.JedisDataException; +import redis.clients.util.SafeEncoder; + +public class ScriptingCommandsTest extends JedisCommandTestBase { + + @SuppressWarnings("unchecked") + @Test + public void evalMultiBulk() { + String script = "return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}"; + List keys = new ArrayList(); + keys.add("key1"); + keys.add("key2"); + + List args = new ArrayList(); + args.add("first"); + args.add("second"); + + List response = (List) jedis.eval(script, keys, args); + + assertEquals(4, response.size()); + assertEquals("key1", response.get(0)); + assertEquals("key2", response.get(1)); + assertEquals("first", response.get(2)); + assertEquals("second", response.get(3)); + } + + @Test + public void evalBulk() { + String script = "return KEYS[1]"; + List keys = new ArrayList(); + keys.add("key1"); + + List args = new ArrayList(); + args.add("first"); + + String response = (String) jedis.eval(script, keys, args); + + assertEquals("key1", response); + } + + @Test + public void evalInt() { + String script = "return 2"; + List keys = new ArrayList(); + keys.add("key1"); + + Long response = (Long) jedis + .eval(script, keys, new ArrayList()); + + assertEquals(new Long(2), response); + } + + @Test + public void evalNoArgs() { + String script = "return KEYS[1]"; + List keys = new ArrayList(); + keys.add("key1"); + String response = (String) jedis.eval(script, keys, + new ArrayList()); + + assertEquals("key1", response); + } + + @Test + public void evalsha() { + jedis.set("foo", "bar"); + jedis.eval("return redis.call('get','foo')"); + String result = (String) jedis + .evalsha("6b1bf486c81ceb7edf3c093f4c48582e38c0e791"); + + assertEquals("bar", result); + } + + @Test(expected = JedisDataException.class) + public void evalshaShaNotFound() { + jedis.evalsha("ffffffffffffffffffffffffffffffffffffffff"); + } + + @Test + public void scriptFlush() { + jedis.set("foo", "bar"); + jedis.eval("return redis.call('get','foo')"); + jedis.scriptFlush(); + assertFalse(jedis + .scriptExists("6b1bf486c81ceb7edf3c093f4c48582e38c0e791")); + } + + @Test + public void scriptExists() { + jedis.scriptLoad("return redis.call('get','foo')"); + List exists = jedis.scriptExists( + "ffffffffffffffffffffffffffffffffffffffff", + "6b1bf486c81ceb7edf3c093f4c48582e38c0e791"); + assertFalse(exists.get(0)); + assertTrue(exists.get(1)); + } + + @Test + public void scriptExistsBinary() { + jedis.scriptLoad(SafeEncoder.encode("return redis.call('get','foo')")); + List exists = jedis.scriptExists( + SafeEncoder.encode("ffffffffffffffffffffffffffffffffffffffff"), + SafeEncoder.encode("6b1bf486c81ceb7edf3c093f4c48582e38c0e791")); + assertEquals(new Long(0), exists.get(0)); + assertEquals(new Long(1), exists.get(1)); + } + + @Test + public void scriptLoad() { + jedis.scriptLoad("return redis.call('get','foo')"); + assertTrue(jedis + .scriptExists("6b1bf486c81ceb7edf3c093f4c48582e38c0e791")); + } + + @Test + public void scriptLoadBinary() { + jedis.scriptLoad(SafeEncoder.encode("return redis.call('get','foo')")); + List exists = jedis.scriptExists(SafeEncoder + .encode("6b1bf486c81ceb7edf3c093f4c48582e38c0e791")); + assertEquals(new Long(1), exists.get(0)); + } + + @Test + public void scriptKill() { + try { + jedis.scriptKill(); + } catch (JedisDataException e) { + assertTrue(e.getMessage().contains( + "No scripts in execution right now.")); + } + } + + @Test + public void scriptEvalReturnNullValues() { + String script = "return {redis.call('hget',KEYS[1],ARGV[1]),redis.call('hget',KEYS[2],ARGV[2])}"; + jedis.eval(script, 2, "key1", "key2", "1", "1"); + } + + @Test + public void scriptEvalShaReturnNullValues() { + String script = "return {redis.call('hget',KEYS[1],ARGV[1]),redis.call('hget',KEYS[2],ARGV[2])}"; + String sha = jedis.scriptLoad(script); + jedis.evalsha(sha, 2, "key1", "key2", "1", "1"); + } +} \ No newline at end of file diff --git a/src/test/java/redis/clients/jedis/tests/commands/SlowlogCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/SlowlogCommandsTest.java new file mode 100644 index 0000000..b65bfb7 --- /dev/null +++ b/src/test/java/redis/clients/jedis/tests/commands/SlowlogCommandsTest.java @@ -0,0 +1,48 @@ +package redis.clients.jedis.tests.commands; + +import java.util.List; + +import org.junit.Test; + +import redis.clients.util.Slowlog; + +public class SlowlogCommandsTest extends JedisCommandTestBase { + + @Test + public void slowlog() { + //do something + jedis.configSet("slowlog-log-slower-than", "0"); + jedis.set("foo", "bar"); + jedis.set("foo2", "bar2"); + + List reducedLog = jedis.slowlogGet(1); + assertEquals(1, reducedLog.size()); + + Slowlog log = reducedLog.get(0); + assertTrue(log.getId() > 0); + assertTrue(log.getTimeStamp() > 0); + assertTrue(log.getExecutionTime() > 0); + assertNotNull(log.getArgs()); + + List breducedLog = jedis.slowlogGetBinary(1); + assertEquals(1, breducedLog.size()); + + List log1 = jedis.slowlogGet(); + List blog1 = jedis.slowlogGetBinary(); + + assertNotNull(log1); + assertNotNull(blog1); + + long len1 = jedis.slowlogLen(); + + jedis.slowlogReset(); + + List log2 = jedis.slowlogGet(); + List blog2 = jedis.slowlogGetBinary(); + long len2 = jedis.slowlogLen(); + + assertTrue(len1 > len2); + assertTrue(log1.size() > log2.size()); + assertTrue(blog1.size() > blog2.size()); + } +} \ No newline at end of file diff --git a/src/test/java/redis/clients/jedis/tests/commands/SortedSetCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/SortedSetCommandsTest.java index 4135fba..65b8155 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/SortedSetCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/SortedSetCommandsTest.java @@ -373,6 +373,10 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { assertEquals(2, result); + result = jedis.zcount("foo", "(0.01", "+inf"); + + assertEquals(3, result); + // Binary jedis.zadd(bfoo, 1d, ba); jedis.zadd(bfoo, 10d, bb); @@ -383,6 +387,9 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { assertEquals(2, bresult); + bresult = jedis.zcount(bfoo, SafeEncoder.encode("(0.01"), SafeEncoder.encode("+inf")); + + assertEquals(3, bresult); } @Test @@ -655,7 +662,7 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { assertEquals(bexpected, brange); } - + @Test public void zremrangeByRank() { jedis.zadd("foo", 1d, "a"); diff --git a/src/test/java/redis/clients/jedis/tests/commands/StringValuesCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/StringValuesCommandsTest.java index fcf9f99..e34eb7b 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/StringValuesCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/StringValuesCommandsTest.java @@ -190,4 +190,20 @@ public class StringValuesCommandsTest extends JedisCommandTestBase { long value = jedis.incr("foo"); assertEquals(Long.MIN_VALUE, value); } + + @Test + public void incrByFloat() { + double value = jedis.incrByFloat("foo", 10.5); + assertEquals(10.5, value, 0.0); + value = jedis.incrByFloat("foo", 0.1); + assertEquals(10.6, value, 0.0); + } + + @Test + public void psetex() { + String status = jedis.psetex("foo", 20000, "bar"); + assertEquals("OK", status); + long ttl = jedis.ttl("foo"); + assertTrue(ttl > 0 && ttl <= 20000); + } } \ No newline at end of file diff --git a/src/test/java/redis/clients/jedis/tests/commands/TransactionCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/TransactionCommandsTest.java index f7d8d4d..5e03270 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/TransactionCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/TransactionCommandsTest.java @@ -11,6 +11,7 @@ import org.junit.Before; import org.junit.Test; import redis.clients.jedis.Jedis; +import redis.clients.jedis.Pipeline; import redis.clients.jedis.Response; import redis.clients.jedis.Transaction; import redis.clients.jedis.TransactionBlock; @@ -170,7 +171,7 @@ public class TransactionCommandsTest extends JedisCommandTestBase { t.set(bmykey, bval); resp = t.exec(); assertEquals(1, resp.size()); - assertArrayEquals("OK".getBytes(), (byte[]) resp.get(0)); + assertEquals("OK", resp.get(0)); } @Test(expected = JedisDataException.class) @@ -241,4 +242,57 @@ public class TransactionCommandsTest extends JedisCommandTestBase { string.get(); t.exec(); } + + @Test + public void transactionResponseWithError() { + Transaction t = jedis.multi(); + t.set("foo", "bar"); + Response> error = t.smembers("foo"); + Response r = t.get("foo"); + List l = t.exec(); + assertEquals(JedisDataException.class, l.get(1).getClass()); + try{ + error.get(); + fail("We expect exception here!"); + }catch(JedisDataException e){ + //that is fine we should be here + } + assertEquals(r.get(), "bar"); + } + + @Test + public void execGetResponse() { + Transaction t = jedis.multi(); + + t.set("foo", "bar"); + t.smembers("foo"); + t.get("foo"); + + List> lr = t.execGetResponse(); + try{ + lr.get(1).get(); + fail("We expect exception here!"); + }catch(JedisDataException e){ + //that is fine we should be here + } + assertEquals("bar", lr.get(2).get()); + } + + @Test + public void select() { + jedis.select(1); + jedis.set("foo", "bar"); + jedis.watch("foo"); + Transaction t = jedis.multi(); + t.select(0); + t.set("bar", "foo"); + + Jedis jedis2 = createJedis(); + jedis2.select(1); + jedis2.set("foo", "bar2"); + + List results = t.exec(); + + assertNull(results); + } } \ No newline at end of file diff --git a/src/test/java/redis/clients/jedis/tests/commands/VariadicCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/VariadicCommandsTest.java new file mode 100644 index 0000000..fd63859 --- /dev/null +++ b/src/test/java/redis/clients/jedis/tests/commands/VariadicCommandsTest.java @@ -0,0 +1,190 @@ +package redis.clients.jedis.tests.commands; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.junit.Test; + +public class VariadicCommandsTest extends JedisCommandTestBase { + final byte[] bfoo = { 0x01, 0x02, 0x03, 0x04 }; + final byte[] bbar = { 0x05, 0x06, 0x07, 0x08 }; + final byte[] bcar = { 0x09, 0x0A, 0x0B, 0x0C }; + final byte[] bfoo1 = { 0x01, 0x02, 0x03, 0x04, 0x0A }; + final byte[] bfoo2 = { 0x01, 0x02, 0x03, 0x04, 0x0B }; + + @Test + public void hdel() { + Map hash = new HashMap(); + hash.put("bar", "car"); + hash.put("car", "bar"); + hash.put("foo2", "bar"); + jedis.hmset("foo", hash); + + assertEquals(0, jedis.hdel("bar", "foo", "foo1").intValue()); + assertEquals(0, jedis.hdel("foo", "foo", "foo1").intValue()); + assertEquals(2, jedis.hdel("foo", "bar", "foo2").intValue()); + assertEquals(null, jedis.hget("foo", "bar")); + + // Binary + Map bhash = new HashMap(); + bhash.put(bbar, bcar); + bhash.put(bcar, bbar); + bhash.put(bfoo2, bbar); + jedis.hmset(bfoo, bhash); + + assertEquals(0, jedis.hdel(bbar, bfoo, bfoo1).intValue()); + assertEquals(0, jedis.hdel(bfoo, bfoo, bfoo1).intValue()); + assertEquals(2, jedis.hdel(bfoo, bbar, bfoo2).intValue()); + assertEquals(null, jedis.hget(bfoo, bbar)); + + } + + @Test + public void rpush() { + long size = jedis.rpush("foo", "bar", "foo"); + assertEquals(2, size); + + List expected = new ArrayList(); + expected.add("bar"); + expected.add("foo"); + + List values = jedis.lrange("foo",0,-1); + assertEquals(expected, values); + + // Binary + size = jedis.rpush(bfoo, bbar, bfoo); + assertEquals(2, size); + + List bexpected = new ArrayList(); + bexpected.add(bbar); + bexpected.add(bfoo); + + List bvalues = jedis.lrange(bfoo, 0, -1); + assertEquals(bexpected, bvalues); + + } + + @Test + public void lpush() { + long size = jedis.lpush("foo", "bar", "foo"); + assertEquals(2, size); + + List expected = new ArrayList(); + expected.add("foo"); + expected.add("bar"); + + List values = jedis.lrange("foo",0,-1); + assertEquals(expected, values); + + // Binary + size = jedis.lpush(bfoo, bbar, bfoo); + assertEquals(2, size); + + List bexpected = new ArrayList(); + bexpected.add(bfoo); + bexpected.add(bbar); + + List bvalues = jedis.lrange(bfoo, 0, -1); + assertEquals(bexpected, bvalues); + + } + + @Test + public void sadd() { + long status = jedis.sadd("foo", "bar", "foo1"); + assertEquals(2, status); + + status = jedis.sadd("foo", "bar", "car"); + assertEquals(1, status); + + status = jedis.sadd("foo", "bar", "foo1"); + assertEquals(0, status); + + status = jedis.sadd(bfoo, bbar, bfoo1); + assertEquals(2, status); + + status = jedis.sadd(bfoo, bbar, bcar); + assertEquals(1, status); + + status = jedis.sadd(bfoo, bbar, bfoo1); + assertEquals(0, status); + + } + + @Test + public void zadd() { + Map scoreMembers = new HashMap(); + scoreMembers.put(1d, "bar"); + scoreMembers.put(10d, "foo"); + + long status = jedis.zadd("foo", scoreMembers); + assertEquals(2, status); + + scoreMembers.clear(); + scoreMembers.put(0.1d, "car"); + scoreMembers.put(2d, "bar"); + + status = jedis.zadd("foo", scoreMembers); + assertEquals(1, status); + + Map bscoreMembers = new HashMap(); + bscoreMembers.put(1d, bbar); + bscoreMembers.put(10d, bfoo); + + status = jedis.zadd(bfoo, bscoreMembers); + assertEquals(2, status); + + bscoreMembers.clear(); + bscoreMembers.put(0.1d, bcar); + bscoreMembers.put(2d, bbar); + + status = jedis.zadd(bfoo, bscoreMembers); + assertEquals(1, status); + + } + + @Test + public void zrem() { + jedis.zadd("foo", 1d, "bar"); + jedis.zadd("foo", 2d, "car"); + jedis.zadd("foo", 3d, "foo1"); + + long status = jedis.zrem("foo", "bar", "car"); + + Set expected = new LinkedHashSet(); + expected.add("foo1"); + + assertEquals(2, status); + assertEquals(expected, jedis.zrange("foo", 0, 100)); + + status = jedis.zrem("foo", "bar", "car"); + assertEquals(0, status); + + status = jedis.zrem("foo", "bar", "foo1"); + assertEquals(1, status); + + //Binary + jedis.zadd(bfoo, 1d, bbar); + jedis.zadd(bfoo, 2d, bcar); + jedis.zadd(bfoo, 3d, bfoo1); + + status = jedis.zrem(bfoo, bbar, bcar); + + Set bexpected = new LinkedHashSet(); + bexpected.add(bfoo); + + assertEquals(2, status); + assertEquals(bexpected, jedis.zrange(bfoo, 0, 100)); + + status = jedis.zrem(bfoo, bbar, bcar); + assertEquals(0, status); + + status = jedis.zrem(bfoo, bbar, bfoo1); + assertEquals(1, status); + + } +} \ No newline at end of file