blob: fb736e54a538d5c4fa1541ea9cde29b9623cd040 [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.*;
Madan Jampani08822c42014-11-04 17:17:46 -08006
7import java.util.Objects;
8
Yuta HIGUCHI5e1cfe02014-11-04 21:22:45 -08009import com.google.common.base.MoreObjects;
10
Madan Jampani08822c42014-11-04 17:17:46 -080011/**
12 * Database write request.
13 */
14public class WriteRequest {
15
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080016 public static final int ANY_VERSION = -1;
17
Madan Jampani08822c42014-11-04 17:17:46 -080018 private final String tableName;
19 private final String key;
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080020
21 private final Type type;
22
Madan Jampani08822c42014-11-04 17:17:46 -080023 private final byte[] newValue;
24 private final long previousVersion;
25 private final byte[] oldValue;
26
Yuta HIGUCHI47eb0d42014-11-05 14:31:20 -080027 /**
28 * Creates a write request, which will
29 * put the specified value to the table regardless of the previous value.
30 *
31 * @param tableName name of the table
32 * @param key key in the table
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080033 * @param newValue value to write, must not be null
Yuta HIGUCHI47eb0d42014-11-05 14:31:20 -080034 * @return WriteRequest
35 */
36 public static WriteRequest put(String tableName, String key,
37 byte[] newValue) {
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080038 return new WriteRequest(PUT, tableName, key,
39 checkNotNull(newValue), ANY_VERSION, null);
Madan Jampani08822c42014-11-04 17:17:46 -080040 }
41
Yuta HIGUCHI47eb0d42014-11-05 14:31:20 -080042 /**
43 * Creates a write request, which will
44 * put the specified value to the table if the previous version matches.
45 *
46 * @param tableName name of the table
47 * @param key key in the table
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080048 * @param newValue value to write, must not be null
Yuta HIGUCHI47eb0d42014-11-05 14:31:20 -080049 * @param previousVersion previous version expected
50 * @return WriteRequest
51 */
52 public static WriteRequest putIfVersionMatches(String tableName, String key,
53 byte[] newValue,
54 long previousVersion) {
Madan Jampani08822c42014-11-04 17:17:46 -080055 checkArgument(previousVersion >= 0);
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080056 return new WriteRequest(PUT_IF_VERSION, tableName, key,
57 checkNotNull(newValue), previousVersion, null);
Madan Jampani08822c42014-11-04 17:17:46 -080058 }
59
Yuta HIGUCHI47eb0d42014-11-05 14:31:20 -080060 /**
61 * Creates a write request, which will
62 * put the specified value to the table if the previous value matches.
63 *
64 * @param tableName name of the table
65 * @param key key in the table
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080066 * @param newValue value to write, must not be null
67 * @param oldValue previous value expected, must not be null
Yuta HIGUCHI47eb0d42014-11-05 14:31:20 -080068 * @return WriteRequest
69 */
70 public static WriteRequest putIfValueMatches(String tableName, String key,
71 byte[] newValue,
72 byte[] oldValue) {
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080073 return new WriteRequest(PUT_IF_VALUE, tableName, key,
74 checkNotNull(newValue), ANY_VERSION,
75 checkNotNull(oldValue));
Madan Jampani08822c42014-11-04 17:17:46 -080076 }
77
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080078 /**
79 * Creates a write request, which will
80 * put the specified value to the table if the previous value does not exist.
81 *
82 * @param tableName name of the table
83 * @param key key in the table
84 * @param newValue value to write, must not be null
85 * @return WriteRequest
86 */
87 public static WriteRequest putIfAbsent(String tableName, String key,
88 byte[] newValue) {
89 return new WriteRequest(PUT_IF_ABSENT, tableName, key,
90 checkNotNull(newValue), ANY_VERSION, null);
91 }
92
93 /**
94 * Creates a write request, which will
95 * remove the specified entry from the table regardless of the previous value.
96 *
97 * @param tableName name of the table
98 * @param key key in the table
99 * @return WriteRequest
100 */
101 public static WriteRequest remove(String tableName, String key) {
102 return new WriteRequest(REMOVE, tableName, key,
103 null, ANY_VERSION, null);
104 }
105
106 /**
107 * Creates a write request, which will
108 * remove the specified entry from the table if the previous version matches.
109 *
110 * @param tableName name of the table
111 * @param key key in the table
112 * @param previousVersion previous version expected
113 * @return WriteRequest
114 */
115 public static WriteRequest remove(String tableName, String key,
116 long previousVersion) {
117 return new WriteRequest(REMOVE_IF_VALUE, tableName, key,
118 null, previousVersion, null);
119 }
120
121 /**
122 * Creates a write request, which will
123 * remove the specified entry from the table if the previous value matches.
124 *
125 * @param tableName name of the table
126 * @param key key in the table
127 * @param oldValue previous value expected, must not be null
128 * @return WriteRequest
129 */
130 public static WriteRequest remove(String tableName, String key,
131 byte[] oldValue) {
132 return new WriteRequest(Type.REMOVE_IF_VALUE, tableName, key,
133 null, ANY_VERSION, checkNotNull(oldValue));
134 }
135
136 public enum Type {
137 PUT,
138 PUT_IF_VERSION,
139 PUT_IF_VALUE,
140 PUT_IF_ABSENT,
141 REMOVE,
142 REMOVE_IF_VERSION,
143 REMOVE_IF_VALUE,
144 }
Yuta HIGUCHI47eb0d42014-11-05 14:31:20 -0800145
Yuta HIGUCHI3a3ac962014-11-04 18:05:08 -0800146 // hidden constructor
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800147 protected WriteRequest(Type type, String tableName, String key,
148 byte[] newValue,
149 long previousVersion, byte[] oldValue) {
Madan Jampani08822c42014-11-04 17:17:46 -0800150
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800151 checkNotNull(tableName);
152 checkNotNull(key);
Madan Jampani08822c42014-11-04 17:17:46 -0800153
154 this.tableName = tableName;
155 this.key = key;
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800156 this.type = type;
Madan Jampani08822c42014-11-04 17:17:46 -0800157 this.newValue = newValue;
158 this.previousVersion = previousVersion;
159 this.oldValue = oldValue;
160 }
161
162 public String tableName() {
163 return tableName;
164 }
165
166 public String key() {
167 return key;
168 }
169
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800170 public WriteRequest.Type type() {
171 return type;
172 }
173
Madan Jampani08822c42014-11-04 17:17:46 -0800174 public byte[] newValue() {
175 return newValue;
176 }
177
178 public long previousVersion() {
179 return previousVersion;
180 }
181
182 public byte[] oldValue() {
183 return oldValue;
184 }
185
186 @Override
187 public String toString() {
Yuta HIGUCHI5e1cfe02014-11-04 21:22:45 -0800188 return MoreObjects.toStringHelper(getClass())
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800189 .add("type", type)
Yuta HIGUCHI5e1cfe02014-11-04 21:22:45 -0800190 .add("tableName", tableName)
191 .add("key", key)
192 .add("newValue", newValue)
193 .add("previousVersion", previousVersion)
194 .add("oldValue", oldValue)
195 .toString();
Madan Jampani08822c42014-11-04 17:17:46 -0800196 }
197
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800198 // TODO: revisit hashCode, equals condition
Madan Jampani08822c42014-11-04 17:17:46 -0800199 @Override
200 public int hashCode() {
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800201 return Objects.hash(type, key, tableName, previousVersion);
Madan Jampani08822c42014-11-04 17:17:46 -0800202 }
203
204 @Override
205 public boolean equals(Object obj) {
206 if (this == obj) {
207 return true;
208 }
209 if (obj == null) {
210 return false;
211 }
212 if (getClass() != obj.getClass()) {
213 return false;
214 }
215 WriteRequest other = (WriteRequest) obj;
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800216 return Objects.equals(this.type, other.type) &&
217 Objects.equals(this.key, other.key) &&
Madan Jampani08822c42014-11-04 17:17:46 -0800218 Objects.equals(this.tableName, other.tableName) &&
219 Objects.equals(this.previousVersion, other.previousVersion);
220 }
221}