|
| 1 | +/** |
| 2 | + * Copyright (C) 2006-2009 Dustin Sallings |
| 3 | + * Copyright (C) 2009-2011 Couchbase, Inc. |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of this software and associated documentation files (the "Software"), to deal |
| 7 | + * in the Software without restriction, including without limitation the rights |
| 8 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be included in |
| 13 | + * all copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALING |
| 21 | + * IN THE SOFTWARE. |
| 22 | + */ |
| 23 | + |
| 24 | +package net.spy.memcached.ops; |
| 25 | + |
| 26 | +/** |
| 27 | + * Represents status and error codes from the binary protocol. |
| 28 | + */ |
| 29 | +public enum StatusCode { |
| 30 | + |
| 31 | + SUCCESS, |
| 32 | + ERR_NOT_FOUND, |
| 33 | + ERR_EXISTS, |
| 34 | + ERR_2BIG, |
| 35 | + ERR_INVAL, |
| 36 | + ERR_NOT_STORED, |
| 37 | + ERR_DELTA_BADVAL, |
| 38 | + ERR_NOT_MY_VBUCKET, |
| 39 | + ERR_UNKNOWN_COMMAND, |
| 40 | + ERR_NO_MEM, |
| 41 | + ERR_NOT_SUPPORTED, |
| 42 | + ERR_INTERNAL, |
| 43 | + ERR_BUSY, |
| 44 | + ERR_TEMP_FAIL, |
| 45 | + CANCELLED, |
| 46 | + INTERRUPTED, |
| 47 | + TIMEDOUT, |
| 48 | + ERR_CLIENT; |
| 49 | + |
| 50 | + public static StatusCode fromBinaryCode(int code) { |
| 51 | + switch(code) { |
| 52 | + case 0x00: |
| 53 | + return SUCCESS; |
| 54 | + case 0x01: |
| 55 | + return ERR_NOT_FOUND; |
| 56 | + case 0x02: |
| 57 | + return ERR_EXISTS; |
| 58 | + case 0x03: |
| 59 | + return ERR_2BIG; |
| 60 | + case 0x04: |
| 61 | + return ERR_INVAL; |
| 62 | + case 0x05: |
| 63 | + return ERR_NOT_STORED; |
| 64 | + case 0x06: |
| 65 | + return ERR_DELTA_BADVAL; |
| 66 | + case 0x07: |
| 67 | + return ERR_NOT_MY_VBUCKET; |
| 68 | + case 0x81: |
| 69 | + return ERR_UNKNOWN_COMMAND; |
| 70 | + case 0x82: |
| 71 | + return ERR_NO_MEM; |
| 72 | + case 0x83: |
| 73 | + return ERR_NOT_SUPPORTED; |
| 74 | + case 0x84: |
| 75 | + return ERR_INTERNAL; |
| 76 | + case 0x85: |
| 77 | + return ERR_BUSY; |
| 78 | + case 0x86: |
| 79 | + return ERR_TEMP_FAIL; |
| 80 | + default: |
| 81 | + return ERR_CLIENT; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + public static StatusCode fromAsciiLine(String line) { |
| 86 | + if (line.equals("STORED") || line.equals("DELETED")) { |
| 87 | + return SUCCESS; |
| 88 | + } else if (line.equals("NOT_STORED")) { |
| 89 | + return ERR_NOT_STORED; |
| 90 | + } else if (line.equals("EXISTS")) { |
| 91 | + return ERR_EXISTS; |
| 92 | + } else if (line.equals("NOT_FOUND")) { |
| 93 | + return ERR_NOT_FOUND; |
| 94 | + } else if (line.equals("ERROR") |
| 95 | + || line.matches("^(CLIENT|SERVER)_ERROR.*")) { |
| 96 | + return ERR_INTERNAL; |
| 97 | + } else { |
| 98 | + return ERR_CLIENT; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | +} |
0 commit comments