blob: a62d38236a750060e2297d78146b3d3f08a2b069 [file] [log] [blame]
Madan Jampanibff6d8f2015-03-31 16:53:47 -07001/*
2 * Copyright 2015 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 */
16
17package org.onosproject.store.service;
18
19import static com.google.common.base.Preconditions.checkArgument;
20import static com.google.common.base.Preconditions.checkNotNull;
21import static com.google.common.base.Preconditions.checkState;
22
23import com.google.common.base.MoreObjects;
24
25/**
26 * Database update operation.
27 *
28 */
29public final class DatabaseUpdate {
30
31 /**
32 * Type of database update operation.
33 */
Sho SHIMIZUe2952e42015-09-11 17:11:21 -070034 public enum Type {
Madan Jampanibff6d8f2015-03-31 16:53:47 -070035 /**
36 * Insert/Update entry without any checks.
37 */
38 PUT,
39 /**
40 * Insert an entry iff there is no existing entry for that key.
41 */
42 PUT_IF_ABSENT,
43
44 /**
45 * Update entry if the current version matches specified version.
46 */
47 PUT_IF_VERSION_MATCH,
48
49 /**
50 * Update entry if the current value matches specified value.
51 */
52 PUT_IF_VALUE_MATCH,
53
54 /**
55 * Remove entry without any checks.
56 */
57 REMOVE,
58
59 /**
60 * Remove entry if the current version matches specified version.
61 */
62 REMOVE_IF_VERSION_MATCH,
63
64 /**
65 * Remove entry if the current value matches specified value.
66 */
67 REMOVE_IF_VALUE_MATCH,
68 }
69
70 private Type type;
Madan Jampani7804c992015-07-20 13:20:19 -070071 private String mapName;
Madan Jampanibff6d8f2015-03-31 16:53:47 -070072 private String key;
73 private byte[] value;
74 private byte[] currentValue;
75 private long currentVersion = -1;
76
77 /**
78 * Returns the type of update operation.
79 * @return type of update.
80 */
81 public Type type() {
82 return type;
83 }
84
85 /**
Madan Jampani7804c992015-07-20 13:20:19 -070086 * Returns the name of map being updated.
87 * @return map name.
Madan Jampanibff6d8f2015-03-31 16:53:47 -070088 */
Madan Jampani7804c992015-07-20 13:20:19 -070089 public String mapName() {
90 return mapName;
Madan Jampanibff6d8f2015-03-31 16:53:47 -070091 }
92
93 /**
94 * Returns the item key being updated.
95 * @return item key
96 */
97 public String key() {
98 return key;
99 }
100
101 /**
102 * Returns the new value.
103 * @return item's target value.
104 */
105 public byte[] value() {
106 return value;
107 }
108
109 /**
110 * Returns the expected current value in the database value for the key.
111 * @return current value in database.
112 */
113 public byte[] currentValue() {
114 return currentValue;
115 }
116
117 /**
118 * Returns the expected current version in the database for the key.
119 * @return expected version.
120 */
121 public long currentVersion() {
122 return currentVersion;
123 }
124
125 @Override
126 public String toString() {
127 return MoreObjects.toStringHelper(this)
128 .add("type", type)
Madan Jampani7804c992015-07-20 13:20:19 -0700129 .add("mapName", mapName)
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700130 .add("key", key)
131 .add("value", value)
132 .add("currentValue", currentValue)
133 .add("currentVersion", currentVersion)
134 .toString();
135 }
136
137 /**
138 * Creates a new builder instance.
139 *
140 * @return builder.
141 */
142 public static Builder newBuilder() {
143 return new Builder();
144 }
145
146 /**
147 * DatabaseUpdate builder.
148 *
149 */
150 public static final class Builder {
151
152 private DatabaseUpdate update = new DatabaseUpdate();
153
154 public DatabaseUpdate build() {
155 validateInputs();
156 return update;
157 }
158
159 public Builder withType(Type type) {
160 update.type = checkNotNull(type, "type cannot be null");
161 return this;
162 }
163
Madan Jampani7804c992015-07-20 13:20:19 -0700164 public Builder withMapName(String mapName) {
165 update.mapName = checkNotNull(mapName, "mapName cannot be null");
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700166 return this;
167 }
168
169 public Builder withKey(String key) {
170 update.key = checkNotNull(key, "key cannot be null");
171 return this;
172 }
173
174 public Builder withCurrentValue(byte[] value) {
175 update.currentValue = checkNotNull(value, "currentValue cannot be null");
176 return this;
177 }
178
179 public Builder withValue(byte[] value) {
180 update.value = checkNotNull(value, "value cannot be null");
181 return this;
182 }
183
184 public Builder withCurrentVersion(long version) {
185 checkArgument(version >= 0, "version cannot be negative");
186 update.currentVersion = version;
187 return this;
188 }
189
190 private void validateInputs() {
191 checkNotNull(update.type, "type must be specified");
Madan Jampani7804c992015-07-20 13:20:19 -0700192 checkNotNull(update.mapName, "map name must be specified");
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700193 checkNotNull(update.key, "key must be specified");
194 switch (update.type) {
195 case PUT:
196 case PUT_IF_ABSENT:
197 checkNotNull(update.value, "value must be specified.");
198 break;
199 case PUT_IF_VERSION_MATCH:
200 checkNotNull(update.value, "value must be specified.");
201 checkState(update.currentVersion >= 0, "current version must be specified");
202 break;
203 case PUT_IF_VALUE_MATCH:
204 checkNotNull(update.value, "value must be specified.");
205 checkNotNull(update.currentValue, "currentValue must be specified.");
206 break;
207 case REMOVE:
208 break;
209 case REMOVE_IF_VERSION_MATCH:
210 checkState(update.currentVersion >= 0, "current version must be specified");
211 break;
212 case REMOVE_IF_VALUE_MATCH:
213 checkNotNull(update.currentValue, "currentValue must be specified.");
214 break;
215 default:
216 throw new IllegalStateException("Unknown operation type");
217 }
218 }
219 }
220}