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