Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 1 | package org.onosproject.store.service; |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 2 | |
| 3 | import static com.google.common.base.Preconditions.checkArgument; |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame] | 4 | import static com.google.common.base.Preconditions.checkNotNull; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 5 | import static org.onosproject.store.service.WriteRequest.Type.*; |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 6 | |
| 7 | import java.util.Objects; |
| 8 | |
Yuta HIGUCHI | 75fb1f4 | 2014-11-19 13:56:19 -0800 | [diff] [blame] | 9 | import org.onlab.util.ByteArraySizeHashPrinter; |
| 10 | |
Yuta HIGUCHI | 5e1cfe0 | 2014-11-04 21:22:45 -0800 | [diff] [blame] | 11 | import com.google.common.base.MoreObjects; |
| 12 | |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 13 | /** |
| 14 | * Database write request. |
| 15 | */ |
| 16 | public class WriteRequest { |
| 17 | |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame] | 18 | public static final int ANY_VERSION = -1; |
| 19 | |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 20 | private final String tableName; |
| 21 | private final String key; |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame] | 22 | |
| 23 | private final Type type; |
| 24 | |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 25 | private final byte[] newValue; |
| 26 | private final long previousVersion; |
| 27 | private final byte[] oldValue; |
| 28 | |
Yuta HIGUCHI | 47eb0d4 | 2014-11-05 14:31:20 -0800 | [diff] [blame] | 29 | /** |
| 30 | * Creates a write request, which will |
| 31 | * put the specified value to the table regardless of the previous value. |
| 32 | * |
| 33 | * @param tableName name of the table |
| 34 | * @param key key in the table |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame] | 35 | * @param newValue value to write, must not be null |
Yuta HIGUCHI | 47eb0d4 | 2014-11-05 14:31:20 -0800 | [diff] [blame] | 36 | * @return WriteRequest |
| 37 | */ |
| 38 | public static WriteRequest put(String tableName, String key, |
| 39 | byte[] newValue) { |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame] | 40 | return new WriteRequest(PUT, tableName, key, |
| 41 | checkNotNull(newValue), ANY_VERSION, null); |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 42 | } |
| 43 | |
Yuta HIGUCHI | 47eb0d4 | 2014-11-05 14:31:20 -0800 | [diff] [blame] | 44 | /** |
| 45 | * Creates a write request, which will |
| 46 | * put the specified value to the table if the previous version matches. |
| 47 | * |
| 48 | * @param tableName name of the table |
| 49 | * @param key key in the table |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame] | 50 | * @param newValue value to write, must not be null |
Yuta HIGUCHI | 47eb0d4 | 2014-11-05 14:31:20 -0800 | [diff] [blame] | 51 | * @param previousVersion previous version expected |
| 52 | * @return WriteRequest |
| 53 | */ |
| 54 | public static WriteRequest putIfVersionMatches(String tableName, String key, |
| 55 | byte[] newValue, |
| 56 | long previousVersion) { |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 57 | checkArgument(previousVersion >= 0); |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame] | 58 | return new WriteRequest(PUT_IF_VERSION, tableName, key, |
| 59 | checkNotNull(newValue), previousVersion, null); |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Yuta HIGUCHI | 47eb0d4 | 2014-11-05 14:31:20 -0800 | [diff] [blame] | 62 | /** |
| 63 | * Creates a write request, which will |
| 64 | * put the specified value to the table if the previous value matches. |
| 65 | * |
| 66 | * @param tableName name of the table |
| 67 | * @param key key in the table |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame] | 68 | * @param oldValue previous value expected, must not be null |
Yuta HIGUCHI | 3524229 | 2014-11-12 18:53:15 -0800 | [diff] [blame] | 69 | * @param newValue value to write, must not be null |
Yuta HIGUCHI | 47eb0d4 | 2014-11-05 14:31:20 -0800 | [diff] [blame] | 70 | * @return WriteRequest |
| 71 | */ |
| 72 | public static WriteRequest putIfValueMatches(String tableName, String key, |
Yuta HIGUCHI | 3524229 | 2014-11-12 18:53:15 -0800 | [diff] [blame] | 73 | byte[] oldValue, |
| 74 | byte[] newValue) { |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame] | 75 | return new WriteRequest(PUT_IF_VALUE, tableName, key, |
| 76 | checkNotNull(newValue), ANY_VERSION, |
| 77 | checkNotNull(oldValue)); |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame] | 80 | /** |
| 81 | * Creates a write request, which will |
| 82 | * put the specified value to the table if the previous value does not exist. |
| 83 | * |
| 84 | * @param tableName name of the table |
| 85 | * @param key key in the table |
| 86 | * @param newValue value to write, must not be null |
| 87 | * @return WriteRequest |
| 88 | */ |
| 89 | public static WriteRequest putIfAbsent(String tableName, String key, |
| 90 | byte[] newValue) { |
| 91 | return new WriteRequest(PUT_IF_ABSENT, tableName, key, |
| 92 | checkNotNull(newValue), ANY_VERSION, null); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Creates a write request, which will |
| 97 | * remove the specified entry from the table regardless of the previous value. |
| 98 | * |
| 99 | * @param tableName name of the table |
| 100 | * @param key key in the table |
| 101 | * @return WriteRequest |
| 102 | */ |
| 103 | public static WriteRequest remove(String tableName, String key) { |
| 104 | return new WriteRequest(REMOVE, tableName, key, |
| 105 | null, ANY_VERSION, null); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Creates a write request, which will |
| 110 | * remove the specified entry from the table if the previous version matches. |
| 111 | * |
| 112 | * @param tableName name of the table |
| 113 | * @param key key in the table |
| 114 | * @param previousVersion previous version expected |
| 115 | * @return WriteRequest |
| 116 | */ |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 117 | public static WriteRequest removeIfVersionMatches(String tableName, String key, |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame] | 118 | long previousVersion) { |
Madan Jampani | f5d263b | 2014-11-13 10:04:40 -0800 | [diff] [blame] | 119 | return new WriteRequest(REMOVE_IF_VERSION, tableName, key, |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame] | 120 | null, previousVersion, null); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Creates a write request, which will |
| 125 | * remove the specified entry from the table if the previous value matches. |
| 126 | * |
| 127 | * @param tableName name of the table |
| 128 | * @param key key in the table |
| 129 | * @param oldValue previous value expected, must not be null |
| 130 | * @return WriteRequest |
| 131 | */ |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 132 | public static WriteRequest removeIfValueMatches(String tableName, String key, |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame] | 133 | byte[] oldValue) { |
Madan Jampani | f5d263b | 2014-11-13 10:04:40 -0800 | [diff] [blame] | 134 | return new WriteRequest(REMOVE_IF_VALUE, tableName, key, |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame] | 135 | null, ANY_VERSION, checkNotNull(oldValue)); |
| 136 | } |
| 137 | |
| 138 | public enum Type { |
| 139 | PUT, |
| 140 | PUT_IF_VERSION, |
| 141 | PUT_IF_VALUE, |
| 142 | PUT_IF_ABSENT, |
| 143 | REMOVE, |
| 144 | REMOVE_IF_VERSION, |
| 145 | REMOVE_IF_VALUE, |
| 146 | } |
Yuta HIGUCHI | 47eb0d4 | 2014-11-05 14:31:20 -0800 | [diff] [blame] | 147 | |
Yuta HIGUCHI | 3a3ac96 | 2014-11-04 18:05:08 -0800 | [diff] [blame] | 148 | // hidden constructor |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame] | 149 | protected WriteRequest(Type type, String tableName, String key, |
| 150 | byte[] newValue, |
| 151 | long previousVersion, byte[] oldValue) { |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 152 | |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame] | 153 | checkNotNull(tableName); |
| 154 | checkNotNull(key); |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 155 | |
| 156 | this.tableName = tableName; |
| 157 | this.key = key; |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame] | 158 | this.type = type; |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 159 | this.newValue = newValue; |
| 160 | this.previousVersion = previousVersion; |
| 161 | this.oldValue = oldValue; |
| 162 | } |
| 163 | |
| 164 | public String tableName() { |
| 165 | return tableName; |
| 166 | } |
| 167 | |
| 168 | public String key() { |
| 169 | return key; |
| 170 | } |
| 171 | |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame] | 172 | public WriteRequest.Type type() { |
| 173 | return type; |
| 174 | } |
| 175 | |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 176 | public byte[] newValue() { |
| 177 | return newValue; |
| 178 | } |
| 179 | |
| 180 | public long previousVersion() { |
| 181 | return previousVersion; |
| 182 | } |
| 183 | |
| 184 | public byte[] oldValue() { |
| 185 | return oldValue; |
| 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public String toString() { |
Yuta HIGUCHI | 75fb1f4 | 2014-11-19 13:56:19 -0800 | [diff] [blame] | 190 | return MoreObjects.toStringHelper(getClass()) |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame] | 191 | .add("type", type) |
Yuta HIGUCHI | 5e1cfe0 | 2014-11-04 21:22:45 -0800 | [diff] [blame] | 192 | .add("tableName", tableName) |
| 193 | .add("key", key) |
Yuta HIGUCHI | 75fb1f4 | 2014-11-19 13:56:19 -0800 | [diff] [blame] | 194 | .add("newValue", ByteArraySizeHashPrinter.orNull(newValue)) |
Yuta HIGUCHI | 5e1cfe0 | 2014-11-04 21:22:45 -0800 | [diff] [blame] | 195 | .add("previousVersion", previousVersion) |
Yuta HIGUCHI | 75fb1f4 | 2014-11-19 13:56:19 -0800 | [diff] [blame] | 196 | .add("oldValue", ByteArraySizeHashPrinter.orNull(oldValue)) |
Yuta HIGUCHI | 5e1cfe0 | 2014-11-04 21:22:45 -0800 | [diff] [blame] | 197 | .toString(); |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 198 | } |
| 199 | |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame] | 200 | // TODO: revisit hashCode, equals condition |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 201 | @Override |
| 202 | public int hashCode() { |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame] | 203 | return Objects.hash(type, key, tableName, previousVersion); |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public boolean equals(Object obj) { |
| 208 | if (this == obj) { |
| 209 | return true; |
| 210 | } |
| 211 | if (obj == null) { |
| 212 | return false; |
| 213 | } |
| 214 | if (getClass() != obj.getClass()) { |
| 215 | return false; |
| 216 | } |
| 217 | WriteRequest other = (WriteRequest) obj; |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame] | 218 | return Objects.equals(this.type, other.type) && |
| 219 | Objects.equals(this.key, other.key) && |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 220 | Objects.equals(this.tableName, other.tableName) && |
| 221 | Objects.equals(this.previousVersion, other.previousVersion); |
| 222 | } |
| 223 | } |