lib/gibson/protocol.rb in gibson-1.0.2 vs lib/gibson/protocol.rb in gibson-1.0.3
- old
+ new
@@ -23,17 +23,23 @@
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
module Gibson
+ # A generic protocol error.
class GenericError < RuntimeError; end
+ # Key or prefix not found.
class NotFoundError < RuntimeError; end
+ # Specified value is not a number.
class NaNError < RuntimeError; end
+ # The server is out of memory.
class OutOfMemoryError < RuntimeError; end
+ # The object is locked and can't be modified.
class LockedError < RuntimeError; end
class Protocol
+ # Query opcodes.
COMMANDS = {
:set => 1,
:ttl => 2,
:get => 3,
:del => 4,
@@ -55,10 +61,11 @@
:meta => 20,
:keys => 21,
:end => 0xff
}
+ # Server replies opcodes.
REPLIES = {
:error => 0, # Generic error
:not_found => 1, # Key/Prefix not found
:nan => 2, # Not a number
:mem => 3, # Out of memory
@@ -66,23 +73,30 @@
:ok => 5, # Ok, no data follows
:val => 6, # Ok, scalar value follows
:kval => 7 # Ok, [ key => value, ... ] follows
}
+ # Error code to exception map.
ERRORS = {
0 => GenericError,
1 => NotFoundError,
2 => NaNError,
3 => OutOfMemoryError,
4 => LockedError
}
+ # Incoming data encodings.
ENCODINGS = {
- :plain => 0x00, # the item is in plain encoding and data points to its buffer
- :lzf => 0x01, # PLAIN but compressed data with lzf
- :number => 0x02 # the item contains a number and data pointer is actually that number
+ # the item is in plain encoding and data points to its buffer
+ :plain => 0x00,
+ # PLAIN but compressed data with lzf
+ :lzf => 0x01,
+ # the item contains a number and data pointer is actually that number
+ :number => 0x02
}
+ ##
+ # Return true if the specified code is an error code, otherwise false.
def self.error? (code)
code >= REPLIES[:error] && code <= REPLIES[:locked]
end
end
end