blob: d3df22ce63b706d4296296ca3e9a7920e535ba9f [file] [log] [blame]
Jonathan Hart6df90172014-04-03 10:13:11 -07001package net.onrc.onos.core.datastore.hazelcast;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -07002
3import java.util.concurrent.CancellationException;
4import java.util.concurrent.ExecutionException;
5import java.util.concurrent.Future;
6
Pavlin Radoslavovc9bacee2014-04-11 19:02:17 -07007import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
8
Jonathan Hart6df90172014-04-03 10:13:11 -07009import net.onrc.onos.core.datastore.IKVTableID;
10import net.onrc.onos.core.datastore.IMultiEntryOperation;
11import net.onrc.onos.core.datastore.hazelcast.HZTable.VersionedValue;
12import net.onrc.onos.core.datastore.internal.IModifiableMultiEntryOperation;
13import net.onrc.onos.core.datastore.utils.ByteArrayUtil;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070014
Jonathan Harta99ec672014-04-03 11:30:34 -070015import org.slf4j.Logger;
16import org.slf4j.LoggerFactory;
17
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070018public class HZMultiEntryOperation implements IMultiEntryOperation, IModifiableMultiEntryOperation {
19 private static final Logger log = LoggerFactory.getLogger(HZMultiEntryOperation.class);
20
21 private final HZTable table;
22 private final byte[] key;
23 protected final OPERATION operation;
24 private STATUS status;
25
26 // for read op
27 private Future<VersionedValue> future;
28 // for write op
29 private VersionedValue writeValue;
30
31 /**
32 * Constructor for Read/ForceDelete Operation.
Ray Milkey269ffb92014-04-03 14:43:30 -070033 *
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070034 * @param table
35 * @param key
36 * @param operation
37 */
Pavlin Radoslavovc9bacee2014-04-11 19:02:17 -070038 @SuppressFBWarnings(value = "EI_EXPOSE_REP2",
39 justification = "TODO: Store a copy of the object?")
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070040 public HZMultiEntryOperation(final HZTable table, final byte[] key, final OPERATION operation) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070041 this.table = table;
42 this.key = key;
43 this.status = STATUS.NOT_EXECUTED;
44 this.operation = operation;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070045
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070046 this.future = null;
47 this.writeValue = null;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070048 }
49
50 /**
51 * Constructor for Other Operations.
Ray Milkey269ffb92014-04-03 14:43:30 -070052 *
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070053 * @param table
54 * @param key
55 * @param value
56 * @param version
57 * @param operation
58 */
Pavlin Radoslavovc9bacee2014-04-11 19:02:17 -070059 @SuppressFBWarnings(value = "EI_EXPOSE_REP2",
60 justification = "TODO: Store a copy of the object?")
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070061 public HZMultiEntryOperation(final HZTable table, final byte[] key, final byte[] value, final long version, final OPERATION operation) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070062 this.table = table;
63 this.key = key;
64 this.status = STATUS.NOT_EXECUTED;
65 this.operation = operation;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070066
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070067 this.future = null;
68 this.writeValue = new VersionedValue(value, version);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070069 }
70
71 @Override
72 public boolean hasSucceeded() {
73
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070074 VersionedValue value = get();
75 return (value != null) && (this.status == STATUS.SUCCESS);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070076 }
77
78 @Override
79 public STATUS getStatus() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070080 get();
81 return status;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070082 }
83
84 @Override
85 public IKVTableID getTableId() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070086 return table;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070087 }
88
89 @Override
Pavlin Radoslavovc9bacee2014-04-11 19:02:17 -070090 @SuppressFBWarnings(value = "EI_EXPOSE_REP",
91 justification = "TODO: Return a copy of the object?")
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070092 public byte[] getKey() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070093 return key;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070094 }
95
96 @Override
97 public byte[] getValue() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070098 if (future != null) {
99 VersionedValue value = get();
100 return value.getValue();
101 }
102 return writeValue.getValue();
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700103 }
104
105 @Override
106 public long getVersion() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700107 if (future != null) {
108 VersionedValue value = get();
109 return value.getVersion();
110 }
111 return writeValue.getVersion();
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700112 }
113
114 @Override
115 public OPERATION getOperation() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700116 return operation;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700117 }
118
119 /**
120 * Evaluate Future object and set Status and Value+Version.
Ray Milkey269ffb92014-04-03 14:43:30 -0700121 *
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700122 * @return the value read or null on failure.
123 */
124 private VersionedValue get() {
125 try {
126 VersionedValue value = future.get();
Yuta HIGUCHI7a43d5b2014-04-15 14:08:17 -0700127 if (value == null) {
128 setStatus(STATUS.FAILED);
129 return null;
130 } else {
131 setValue(value.getValue(), value.getVersion());
132 setStatus(STATUS.SUCCESS);
133 return value;
134 }
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700135 } catch (CancellationException | InterruptedException | ExecutionException e) {
136 log.error(this + " has failed.", e);
137 setStatus(STATUS.FAILED);
138 return null;
139 }
140 }
141
142 @Override
143 public void setValue(final byte[] value, final long version) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700144 writeValue = new VersionedValue(value, version);
145 setVersion(version);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700146 }
147
148 @Override
149 public void setVersion(final long version) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700150 if (writeValue == null) {
151 writeValue = new VersionedValue(null, version);
152 }
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700153 }
154
155 @Override
156 public void setStatus(final STATUS status) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700157 this.status = status;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700158 }
159
160 @Override
161 public IModifiableMultiEntryOperation getActualOperation() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700162 return this;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700163 }
164
165 void setFuture(final Future<VersionedValue> future) {
166 this.future = future;
167 }
168
169 @Override
170 public String toString() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700171 return "[HZMultiEntryOperation table=" + table + ", key="
172 + ByteArrayUtil.toHexStringBuffer(key, ":") + ", operation=" + operation
173 + ", status=" + status + ", writeValue=" + writeValue + "]";
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700174 }
175}