blob: b2ae100600db10f973d4954f647830537461ddca [file] [log] [blame]
alshabibab984662014-12-04 18:56:18 -08001/*
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.store.service;
Madan Jampani08822c42014-11-04 17:17:46 -080017
18import static com.google.common.base.Preconditions.checkArgument;
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080019import static com.google.common.base.Preconditions.checkNotNull;
Brian O'Connorabafb502014-12-02 22:26:20 -080020import static org.onosproject.store.service.WriteRequest.Type.*;
Madan Jampani08822c42014-11-04 17:17:46 -080021
22import java.util.Objects;
23
Yuta HIGUCHI75fb1f42014-11-19 13:56:19 -080024import org.onlab.util.ByteArraySizeHashPrinter;
25
Yuta HIGUCHI5e1cfe02014-11-04 21:22:45 -080026import com.google.common.base.MoreObjects;
27
Madan Jampani08822c42014-11-04 17:17:46 -080028/**
29 * Database write request.
30 */
31public class WriteRequest {
32
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080033 public static final int ANY_VERSION = -1;
34
Madan Jampani08822c42014-11-04 17:17:46 -080035 private final String tableName;
36 private final String key;
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080037
38 private final Type type;
39
Madan Jampani08822c42014-11-04 17:17:46 -080040 private final byte[] newValue;
41 private final long previousVersion;
42 private final byte[] oldValue;
43
Yuta HIGUCHI47eb0d42014-11-05 14:31:20 -080044 /**
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 HIGUCHI361664e2014-11-06 17:28:47 -080050 * @param newValue value to write, must not be null
Yuta HIGUCHI47eb0d42014-11-05 14:31:20 -080051 * @return WriteRequest
52 */
53 public static WriteRequest put(String tableName, String key,
54 byte[] newValue) {
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080055 return new WriteRequest(PUT, tableName, key,
56 checkNotNull(newValue), ANY_VERSION, null);
Madan Jampani08822c42014-11-04 17:17:46 -080057 }
58
Yuta HIGUCHI47eb0d42014-11-05 14:31:20 -080059 /**
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 HIGUCHI361664e2014-11-06 17:28:47 -080065 * @param newValue value to write, must not be null
Yuta HIGUCHI47eb0d42014-11-05 14:31:20 -080066 * @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 Jampani08822c42014-11-04 17:17:46 -080072 checkArgument(previousVersion >= 0);
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080073 return new WriteRequest(PUT_IF_VERSION, tableName, key,
74 checkNotNull(newValue), previousVersion, null);
Madan Jampani08822c42014-11-04 17:17:46 -080075 }
76
Yuta HIGUCHI47eb0d42014-11-05 14:31:20 -080077 /**
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 HIGUCHI361664e2014-11-06 17:28:47 -080083 * @param oldValue previous value expected, must not be null
Yuta HIGUCHI35242292014-11-12 18:53:15 -080084 * @param newValue value to write, must not be null
Yuta HIGUCHI47eb0d42014-11-05 14:31:20 -080085 * @return WriteRequest
86 */
87 public static WriteRequest putIfValueMatches(String tableName, String key,
Yuta HIGUCHI35242292014-11-12 18:53:15 -080088 byte[] oldValue,
89 byte[] newValue) {
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080090 return new WriteRequest(PUT_IF_VALUE, tableName, key,
91 checkNotNull(newValue), ANY_VERSION,
92 checkNotNull(oldValue));
Madan Jampani08822c42014-11-04 17:17:46 -080093 }
94
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080095 /**
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 Jampani12390c12014-11-12 00:35:56 -0800132 public static WriteRequest removeIfVersionMatches(String tableName, String key,
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800133 long previousVersion) {
Madan Jampanif5d263b2014-11-13 10:04:40 -0800134 return new WriteRequest(REMOVE_IF_VERSION, tableName, key,
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800135 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 Jampani12390c12014-11-12 00:35:56 -0800147 public static WriteRequest removeIfValueMatches(String tableName, String key,
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800148 byte[] oldValue) {
Madan Jampanif5d263b2014-11-13 10:04:40 -0800149 return new WriteRequest(REMOVE_IF_VALUE, tableName, key,
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800150 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 HIGUCHI47eb0d42014-11-05 14:31:20 -0800162
Yuta HIGUCHI3a3ac962014-11-04 18:05:08 -0800163 // hidden constructor
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800164 protected WriteRequest(Type type, String tableName, String key,
165 byte[] newValue,
166 long previousVersion, byte[] oldValue) {
Madan Jampani08822c42014-11-04 17:17:46 -0800167
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800168 checkNotNull(tableName);
169 checkNotNull(key);
Madan Jampani08822c42014-11-04 17:17:46 -0800170
171 this.tableName = tableName;
172 this.key = key;
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800173 this.type = type;
Madan Jampani08822c42014-11-04 17:17:46 -0800174 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 HIGUCHI361664e2014-11-06 17:28:47 -0800187 public WriteRequest.Type type() {
188 return type;
189 }
190
Madan Jampani08822c42014-11-04 17:17:46 -0800191 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 HIGUCHI75fb1f42014-11-19 13:56:19 -0800205 return MoreObjects.toStringHelper(getClass())
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800206 .add("type", type)
Yuta HIGUCHI5e1cfe02014-11-04 21:22:45 -0800207 .add("tableName", tableName)
208 .add("key", key)
Yuta HIGUCHI75fb1f42014-11-19 13:56:19 -0800209 .add("newValue", ByteArraySizeHashPrinter.orNull(newValue))
Yuta HIGUCHI5e1cfe02014-11-04 21:22:45 -0800210 .add("previousVersion", previousVersion)
Yuta HIGUCHI75fb1f42014-11-19 13:56:19 -0800211 .add("oldValue", ByteArraySizeHashPrinter.orNull(oldValue))
Yuta HIGUCHI5e1cfe02014-11-04 21:22:45 -0800212 .toString();
Madan Jampani08822c42014-11-04 17:17:46 -0800213 }
214
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800215 // TODO: revisit hashCode, equals condition
Madan Jampani08822c42014-11-04 17:17:46 -0800216 @Override
217 public int hashCode() {
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800218 return Objects.hash(type, key, tableName, previousVersion);
Madan Jampani08822c42014-11-04 17:17:46 -0800219 }
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 HIGUCHI361664e2014-11-06 17:28:47 -0800233 return Objects.equals(this.type, other.type) &&
234 Objects.equals(this.key, other.key) &&
Madan Jampani08822c42014-11-04 17:17:46 -0800235 Objects.equals(this.tableName, other.tableName) &&
236 Objects.equals(this.previousVersion, other.previousVersion);
237 }
238}