blob: dcf22e9b2361a27d2ac3f9eb854fa7cffe3dc015 [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;
4
5import java.util.Objects;
6
Yuta HIGUCHI5e1cfe02014-11-04 21:22:45 -08007import com.google.common.base.MoreObjects;
8
Madan Jampani08822c42014-11-04 17:17:46 -08009/**
10 * Database write request.
11 */
12public class WriteRequest {
13
14 private final String tableName;
15 private final String key;
16 private final byte[] newValue;
17 private final long previousVersion;
18 private final byte[] oldValue;
19
Yuta HIGUCHI47eb0d42014-11-05 14:31:20 -080020 /**
21 * Creates a write request, which will
22 * put the specified value to the table regardless of the previous value.
23 *
24 * @param tableName name of the table
25 * @param key key in the table
26 * @param newValue value to write
27 * @return WriteRequest
28 */
29 public static WriteRequest put(String tableName, String key,
30 byte[] newValue) {
31 return new WriteRequest(tableName, key, newValue, -1, null);
Madan Jampani08822c42014-11-04 17:17:46 -080032 }
33
Yuta HIGUCHI47eb0d42014-11-05 14:31:20 -080034 // FIXME: Is there a special version value to realize putIfAbsent?
35 /**
36 * Creates a write request, which will
37 * put the specified value to the table if the previous version matches.
38 *
39 * @param tableName name of the table
40 * @param key key in the table
41 * @param newValue value to write
42 * @param previousVersion previous version expected
43 * @return WriteRequest
44 */
45 public static WriteRequest putIfVersionMatches(String tableName, String key,
46 byte[] newValue,
47 long previousVersion) {
Madan Jampani08822c42014-11-04 17:17:46 -080048 checkArgument(previousVersion >= 0);
Yuta HIGUCHI47eb0d42014-11-05 14:31:20 -080049 return new WriteRequest(tableName, key, newValue, previousVersion, null);
Madan Jampani08822c42014-11-04 17:17:46 -080050 }
51
Yuta HIGUCHI47eb0d42014-11-05 14:31:20 -080052 // FIXME: What is the behavior of oldValue=null? putIfAbsent?
53 /**
54 * Creates a write request, which will
55 * put the specified value to the table if the previous value matches.
56 *
57 * @param tableName name of the table
58 * @param key key in the table
59 * @param newValue value to write
60 * @param oldValue previous value expected
61 * @return WriteRequest
62 */
63 public static WriteRequest putIfValueMatches(String tableName, String key,
64 byte[] newValue,
65 byte[] oldValue) {
66 return new WriteRequest(tableName, key, newValue, -1, oldValue);
Madan Jampani08822c42014-11-04 17:17:46 -080067 }
68
Yuta HIGUCHI47eb0d42014-11-05 14:31:20 -080069 // FIXME: How do we remove value? newValue=null?
70
Yuta HIGUCHI3a3ac962014-11-04 18:05:08 -080071 // hidden constructor
Yuta HIGUCHI47eb0d42014-11-05 14:31:20 -080072 protected WriteRequest(String tableName, String key, byte[] newValue, long previousVersion, byte[] oldValue) {
Madan Jampani08822c42014-11-04 17:17:46 -080073
74 checkArgument(tableName != null);
75 checkArgument(key != null);
76 checkArgument(newValue != null);
77
78 this.tableName = tableName;
79 this.key = key;
80 this.newValue = newValue;
81 this.previousVersion = previousVersion;
82 this.oldValue = oldValue;
83 }
84
85 public String tableName() {
86 return tableName;
87 }
88
89 public String key() {
90 return key;
91 }
92
93 public byte[] newValue() {
94 return newValue;
95 }
96
97 public long previousVersion() {
98 return previousVersion;
99 }
100
101 public byte[] oldValue() {
102 return oldValue;
103 }
104
105 @Override
106 public String toString() {
Yuta HIGUCHI5e1cfe02014-11-04 21:22:45 -0800107 return MoreObjects.toStringHelper(getClass())
108 .add("tableName", tableName)
109 .add("key", key)
110 .add("newValue", newValue)
111 .add("previousVersion", previousVersion)
112 .add("oldValue", oldValue)
113 .toString();
Madan Jampani08822c42014-11-04 17:17:46 -0800114 }
115
116 @Override
117 public int hashCode() {
118 return Objects.hash(key, tableName, previousVersion);
119 }
120
121 @Override
122 public boolean equals(Object obj) {
123 if (this == obj) {
124 return true;
125 }
126 if (obj == null) {
127 return false;
128 }
129 if (getClass() != obj.getClass()) {
130 return false;
131 }
132 WriteRequest other = (WriteRequest) obj;
133 return Objects.equals(this.key, other.key) &&
134 Objects.equals(this.tableName, other.tableName) &&
135 Objects.equals(this.previousVersion, other.previousVersion);
136 }
137}