blob: 123b5c8ab245d3422bce382b4a38d3c3173342f3 [file] [log] [blame]
Brian O'Connorabafb502014-12-02 22:26:20 -08001package org.onosproject.store.service;
Madan Jampani08822c42014-11-04 17:17:46 -08002
3import static com.google.common.base.Preconditions.checkArgument;
Yuta HIGUCHI361664e2014-11-06 17:28:47 -08004import static com.google.common.base.Preconditions.checkNotNull;
Brian O'Connorabafb502014-12-02 22:26:20 -08005import static org.onosproject.store.service.WriteRequest.Type.*;
Madan Jampani08822c42014-11-04 17:17:46 -08006
7import java.util.Objects;
8
Yuta HIGUCHI75fb1f42014-11-19 13:56:19 -08009import org.onlab.util.ByteArraySizeHashPrinter;
10
Yuta HIGUCHI5e1cfe02014-11-04 21:22:45 -080011import com.google.common.base.MoreObjects;
12
Madan Jampani08822c42014-11-04 17:17:46 -080013/**
14 * Database write request.
15 */
16public class WriteRequest {
17
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080018 public static final int ANY_VERSION = -1;
19
Madan Jampani08822c42014-11-04 17:17:46 -080020 private final String tableName;
21 private final String key;
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080022
23 private final Type type;
24
Madan Jampani08822c42014-11-04 17:17:46 -080025 private final byte[] newValue;
26 private final long previousVersion;
27 private final byte[] oldValue;
28
Yuta HIGUCHI47eb0d42014-11-05 14:31:20 -080029 /**
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 HIGUCHI361664e2014-11-06 17:28:47 -080035 * @param newValue value to write, must not be null
Yuta HIGUCHI47eb0d42014-11-05 14:31:20 -080036 * @return WriteRequest
37 */
38 public static WriteRequest put(String tableName, String key,
39 byte[] newValue) {
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080040 return new WriteRequest(PUT, tableName, key,
41 checkNotNull(newValue), ANY_VERSION, null);
Madan Jampani08822c42014-11-04 17:17:46 -080042 }
43
Yuta HIGUCHI47eb0d42014-11-05 14:31:20 -080044 /**
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 HIGUCHI361664e2014-11-06 17:28:47 -080050 * @param newValue value to write, must not be null
Yuta HIGUCHI47eb0d42014-11-05 14:31:20 -080051 * @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 Jampani08822c42014-11-04 17:17:46 -080057 checkArgument(previousVersion >= 0);
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080058 return new WriteRequest(PUT_IF_VERSION, tableName, key,
59 checkNotNull(newValue), previousVersion, null);
Madan Jampani08822c42014-11-04 17:17:46 -080060 }
61
Yuta HIGUCHI47eb0d42014-11-05 14:31:20 -080062 /**
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 HIGUCHI361664e2014-11-06 17:28:47 -080068 * @param oldValue previous value expected, must not be null
Yuta HIGUCHI35242292014-11-12 18:53:15 -080069 * @param newValue value to write, must not be null
Yuta HIGUCHI47eb0d42014-11-05 14:31:20 -080070 * @return WriteRequest
71 */
72 public static WriteRequest putIfValueMatches(String tableName, String key,
Yuta HIGUCHI35242292014-11-12 18:53:15 -080073 byte[] oldValue,
74 byte[] newValue) {
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080075 return new WriteRequest(PUT_IF_VALUE, tableName, key,
76 checkNotNull(newValue), ANY_VERSION,
77 checkNotNull(oldValue));
Madan Jampani08822c42014-11-04 17:17:46 -080078 }
79
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080080 /**
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 Jampani12390c12014-11-12 00:35:56 -0800117 public static WriteRequest removeIfVersionMatches(String tableName, String key,
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800118 long previousVersion) {
Madan Jampanif5d263b2014-11-13 10:04:40 -0800119 return new WriteRequest(REMOVE_IF_VERSION, tableName, key,
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800120 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 Jampani12390c12014-11-12 00:35:56 -0800132 public static WriteRequest removeIfValueMatches(String tableName, String key,
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800133 byte[] oldValue) {
Madan Jampanif5d263b2014-11-13 10:04:40 -0800134 return new WriteRequest(REMOVE_IF_VALUE, tableName, key,
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800135 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 HIGUCHI47eb0d42014-11-05 14:31:20 -0800147
Yuta HIGUCHI3a3ac962014-11-04 18:05:08 -0800148 // hidden constructor
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800149 protected WriteRequest(Type type, String tableName, String key,
150 byte[] newValue,
151 long previousVersion, byte[] oldValue) {
Madan Jampani08822c42014-11-04 17:17:46 -0800152
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800153 checkNotNull(tableName);
154 checkNotNull(key);
Madan Jampani08822c42014-11-04 17:17:46 -0800155
156 this.tableName = tableName;
157 this.key = key;
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800158 this.type = type;
Madan Jampani08822c42014-11-04 17:17:46 -0800159 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 HIGUCHI361664e2014-11-06 17:28:47 -0800172 public WriteRequest.Type type() {
173 return type;
174 }
175
Madan Jampani08822c42014-11-04 17:17:46 -0800176 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 HIGUCHI75fb1f42014-11-19 13:56:19 -0800190 return MoreObjects.toStringHelper(getClass())
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800191 .add("type", type)
Yuta HIGUCHI5e1cfe02014-11-04 21:22:45 -0800192 .add("tableName", tableName)
193 .add("key", key)
Yuta HIGUCHI75fb1f42014-11-19 13:56:19 -0800194 .add("newValue", ByteArraySizeHashPrinter.orNull(newValue))
Yuta HIGUCHI5e1cfe02014-11-04 21:22:45 -0800195 .add("previousVersion", previousVersion)
Yuta HIGUCHI75fb1f42014-11-19 13:56:19 -0800196 .add("oldValue", ByteArraySizeHashPrinter.orNull(oldValue))
Yuta HIGUCHI5e1cfe02014-11-04 21:22:45 -0800197 .toString();
Madan Jampani08822c42014-11-04 17:17:46 -0800198 }
199
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800200 // TODO: revisit hashCode, equals condition
Madan Jampani08822c42014-11-04 17:17:46 -0800201 @Override
202 public int hashCode() {
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800203 return Objects.hash(type, key, tableName, previousVersion);
Madan Jampani08822c42014-11-04 17:17:46 -0800204 }
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 HIGUCHI361664e2014-11-06 17:28:47 -0800218 return Objects.equals(this.type, other.type) &&
219 Objects.equals(this.key, other.key) &&
Madan Jampani08822c42014-11-04 17:17:46 -0800220 Objects.equals(this.tableName, other.tableName) &&
221 Objects.equals(this.previousVersion, other.previousVersion);
222 }
223}