blob: bc2f527c40a5a862c0ccf227b718460b3bcdc962 [file] [log] [blame]
Madan Jampani08822c42014-11-04 17:17:46 -08001package org.onlab.onos.store.service;
2
3import static com.google.common.base.Preconditions.checkArgument;
Yuta HIGUCHI361664e2014-11-06 17:28:47 -08004import static com.google.common.base.Preconditions.checkNotNull;
5import static org.onlab.onos.store.service.WriteRequest.Type.*;
Yuta HIGUCHI6b38ee32014-11-14 02:02:59 -08006import static org.onlab.util.HexString.toHexString;
Madan Jampani08822c42014-11-04 17:17:46 -08007
8import java.util.Objects;
9
Yuta HIGUCHI5e1cfe02014-11-04 21:22:45 -080010import com.google.common.base.MoreObjects;
11
Madan Jampani08822c42014-11-04 17:17:46 -080012/**
13 * Database write request.
14 */
15public class WriteRequest {
16
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080017 public static final int ANY_VERSION = -1;
18
Madan Jampani08822c42014-11-04 17:17:46 -080019 private final String tableName;
20 private final String key;
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080021
22 private final Type type;
23
Madan Jampani08822c42014-11-04 17:17:46 -080024 private final byte[] newValue;
25 private final long previousVersion;
26 private final byte[] oldValue;
27
Yuta HIGUCHI47eb0d42014-11-05 14:31:20 -080028 /**
29 * Creates a write request, which will
30 * put the specified value to the table regardless of the previous value.
31 *
32 * @param tableName name of the table
33 * @param key key in the table
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080034 * @param newValue value to write, must not be null
Yuta HIGUCHI47eb0d42014-11-05 14:31:20 -080035 * @return WriteRequest
36 */
37 public static WriteRequest put(String tableName, String key,
38 byte[] newValue) {
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080039 return new WriteRequest(PUT, tableName, key,
40 checkNotNull(newValue), ANY_VERSION, null);
Madan Jampani08822c42014-11-04 17:17:46 -080041 }
42
Yuta HIGUCHI47eb0d42014-11-05 14:31:20 -080043 /**
44 * Creates a write request, which will
45 * put the specified value to the table if the previous version matches.
46 *
47 * @param tableName name of the table
48 * @param key key in the table
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080049 * @param newValue value to write, must not be null
Yuta HIGUCHI47eb0d42014-11-05 14:31:20 -080050 * @param previousVersion previous version expected
51 * @return WriteRequest
52 */
53 public static WriteRequest putIfVersionMatches(String tableName, String key,
54 byte[] newValue,
55 long previousVersion) {
Madan Jampani08822c42014-11-04 17:17:46 -080056 checkArgument(previousVersion >= 0);
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080057 return new WriteRequest(PUT_IF_VERSION, tableName, key,
58 checkNotNull(newValue), previousVersion, null);
Madan Jampani08822c42014-11-04 17:17:46 -080059 }
60
Yuta HIGUCHI47eb0d42014-11-05 14:31:20 -080061 /**
62 * Creates a write request, which will
63 * put the specified value to the table if the previous value matches.
64 *
65 * @param tableName name of the table
66 * @param key key in the table
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080067 * @param oldValue previous value expected, must not be null
Yuta HIGUCHI35242292014-11-12 18:53:15 -080068 * @param newValue value to write, must not be null
Yuta HIGUCHI47eb0d42014-11-05 14:31:20 -080069 * @return WriteRequest
70 */
71 public static WriteRequest putIfValueMatches(String tableName, String key,
Yuta HIGUCHI35242292014-11-12 18:53:15 -080072 byte[] oldValue,
73 byte[] newValue) {
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080074 return new WriteRequest(PUT_IF_VALUE, tableName, key,
75 checkNotNull(newValue), ANY_VERSION,
76 checkNotNull(oldValue));
Madan Jampani08822c42014-11-04 17:17:46 -080077 }
78
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080079 /**
80 * Creates a write request, which will
81 * put the specified value to the table if the previous value does not exist.
82 *
83 * @param tableName name of the table
84 * @param key key in the table
85 * @param newValue value to write, must not be null
86 * @return WriteRequest
87 */
88 public static WriteRequest putIfAbsent(String tableName, String key,
89 byte[] newValue) {
90 return new WriteRequest(PUT_IF_ABSENT, tableName, key,
91 checkNotNull(newValue), ANY_VERSION, null);
92 }
93
94 /**
95 * Creates a write request, which will
96 * remove the specified entry from the table regardless of the previous value.
97 *
98 * @param tableName name of the table
99 * @param key key in the table
100 * @return WriteRequest
101 */
102 public static WriteRequest remove(String tableName, String key) {
103 return new WriteRequest(REMOVE, tableName, key,
104 null, ANY_VERSION, null);
105 }
106
107 /**
108 * Creates a write request, which will
109 * remove the specified entry from the table if the previous version matches.
110 *
111 * @param tableName name of the table
112 * @param key key in the table
113 * @param previousVersion previous version expected
114 * @return WriteRequest
115 */
Madan Jampani12390c12014-11-12 00:35:56 -0800116 public static WriteRequest removeIfVersionMatches(String tableName, String key,
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800117 long previousVersion) {
Madan Jampanif5d263b2014-11-13 10:04:40 -0800118 return new WriteRequest(REMOVE_IF_VERSION, tableName, key,
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800119 null, previousVersion, null);
120 }
121
122 /**
123 * Creates a write request, which will
124 * remove the specified entry from the table if the previous value matches.
125 *
126 * @param tableName name of the table
127 * @param key key in the table
128 * @param oldValue previous value expected, must not be null
129 * @return WriteRequest
130 */
Madan Jampani12390c12014-11-12 00:35:56 -0800131 public static WriteRequest removeIfValueMatches(String tableName, String key,
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800132 byte[] oldValue) {
Madan Jampanif5d263b2014-11-13 10:04:40 -0800133 return new WriteRequest(REMOVE_IF_VALUE, tableName, key,
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800134 null, ANY_VERSION, checkNotNull(oldValue));
135 }
136
137 public enum Type {
138 PUT,
139 PUT_IF_VERSION,
140 PUT_IF_VALUE,
141 PUT_IF_ABSENT,
142 REMOVE,
143 REMOVE_IF_VERSION,
144 REMOVE_IF_VALUE,
145 }
Yuta HIGUCHI47eb0d42014-11-05 14:31:20 -0800146
Yuta HIGUCHI3a3ac962014-11-04 18:05:08 -0800147 // hidden constructor
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800148 protected WriteRequest(Type type, String tableName, String key,
149 byte[] newValue,
150 long previousVersion, byte[] oldValue) {
Madan Jampani08822c42014-11-04 17:17:46 -0800151
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800152 checkNotNull(tableName);
153 checkNotNull(key);
Madan Jampani08822c42014-11-04 17:17:46 -0800154
155 this.tableName = tableName;
156 this.key = key;
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800157 this.type = type;
Madan Jampani08822c42014-11-04 17:17:46 -0800158 this.newValue = newValue;
159 this.previousVersion = previousVersion;
160 this.oldValue = oldValue;
161 }
162
163 public String tableName() {
164 return tableName;
165 }
166
167 public String key() {
168 return key;
169 }
170
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800171 public WriteRequest.Type type() {
172 return type;
173 }
174
Madan Jampani08822c42014-11-04 17:17:46 -0800175 public byte[] newValue() {
176 return newValue;
177 }
178
179 public long previousVersion() {
180 return previousVersion;
181 }
182
183 public byte[] oldValue() {
184 return oldValue;
185 }
186
187 @Override
188 public String toString() {
Yuta HIGUCHI5e1cfe02014-11-04 21:22:45 -0800189 return MoreObjects.toStringHelper(getClass())
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800190 .add("type", type)
Yuta HIGUCHI5e1cfe02014-11-04 21:22:45 -0800191 .add("tableName", tableName)
192 .add("key", key)
Yuta HIGUCHI6b38ee32014-11-14 02:02:59 -0800193 .add("newValue", toHexString(newValue))
Yuta HIGUCHI5e1cfe02014-11-04 21:22:45 -0800194 .add("previousVersion", previousVersion)
Yuta HIGUCHI6b38ee32014-11-14 02:02:59 -0800195 .add("oldValue", toHexString(oldValue))
Yuta HIGUCHI5e1cfe02014-11-04 21:22:45 -0800196 .toString();
Madan Jampani08822c42014-11-04 17:17:46 -0800197 }
198
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800199 // TODO: revisit hashCode, equals condition
Madan Jampani08822c42014-11-04 17:17:46 -0800200 @Override
201 public int hashCode() {
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800202 return Objects.hash(type, key, tableName, previousVersion);
Madan Jampani08822c42014-11-04 17:17:46 -0800203 }
204
205 @Override
206 public boolean equals(Object obj) {
207 if (this == obj) {
208 return true;
209 }
210 if (obj == null) {
211 return false;
212 }
213 if (getClass() != obj.getClass()) {
214 return false;
215 }
216 WriteRequest other = (WriteRequest) obj;
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800217 return Objects.equals(this.type, other.type) &&
218 Objects.equals(this.key, other.key) &&
Madan Jampani08822c42014-11-04 17:17:46 -0800219 Objects.equals(this.tableName, other.tableName) &&
220 Objects.equals(this.previousVersion, other.previousVersion);
221 }
222}