blob: 99f73c11d8140f90599ce4690e75ba41f10ddbcc [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 HIGUCHI3a3ac962014-11-04 18:05:08 -080020 // put regardless of previous value
Madan Jampani08822c42014-11-04 17:17:46 -080021 public WriteRequest(String tableName, String key, byte[] newValue) {
22 this(tableName, key, newValue, -1, null);
23 }
24
Yuta HIGUCHI3a3ac962014-11-04 18:05:08 -080025 // put if version matches
Madan Jampani08822c42014-11-04 17:17:46 -080026 public WriteRequest(String tableName, String key, byte[] newValue, long previousVersion) {
27 this(tableName, key, newValue, previousVersion, null);
28 checkArgument(previousVersion >= 0);
29 }
30
Yuta HIGUCHI3a3ac962014-11-04 18:05:08 -080031 // put if value matches
Madan Jampani08822c42014-11-04 17:17:46 -080032 public WriteRequest(String tableName, String key, byte[] newValue, byte[] oldValue) {
33 this(tableName, key, newValue, -1, oldValue);
34 }
35
Yuta HIGUCHI3a3ac962014-11-04 18:05:08 -080036 // hidden constructor
Madan Jampani08822c42014-11-04 17:17:46 -080037 private WriteRequest(String tableName, String key, byte[] newValue, long previousVersion, byte[] oldValue) {
38
39 checkArgument(tableName != null);
40 checkArgument(key != null);
41 checkArgument(newValue != null);
42
43 this.tableName = tableName;
44 this.key = key;
45 this.newValue = newValue;
46 this.previousVersion = previousVersion;
47 this.oldValue = oldValue;
48 }
49
50 public String tableName() {
51 return tableName;
52 }
53
54 public String key() {
55 return key;
56 }
57
58 public byte[] newValue() {
59 return newValue;
60 }
61
62 public long previousVersion() {
63 return previousVersion;
64 }
65
66 public byte[] oldValue() {
67 return oldValue;
68 }
69
70 @Override
71 public String toString() {
Yuta HIGUCHI5e1cfe02014-11-04 21:22:45 -080072 return MoreObjects.toStringHelper(getClass())
73 .add("tableName", tableName)
74 .add("key", key)
75 .add("newValue", newValue)
76 .add("previousVersion", previousVersion)
77 .add("oldValue", oldValue)
78 .toString();
Madan Jampani08822c42014-11-04 17:17:46 -080079 }
80
81 @Override
82 public int hashCode() {
83 return Objects.hash(key, tableName, previousVersion);
84 }
85
86 @Override
87 public boolean equals(Object obj) {
88 if (this == obj) {
89 return true;
90 }
91 if (obj == null) {
92 return false;
93 }
94 if (getClass() != obj.getClass()) {
95 return false;
96 }
97 WriteRequest other = (WriteRequest) obj;
98 return Objects.equals(this.key, other.key) &&
99 Objects.equals(this.tableName, other.tableName) &&
100 Objects.equals(this.previousVersion, other.previousVersion);
101 }
102}