blob: 270d1a7f246b8acee8f59f2e0811974b268b987e [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
Jonathan Hart6df90172014-04-03 10:13:11 -07007import net.onrc.onos.core.datastore.IKVTableID;
8import net.onrc.onos.core.datastore.IMultiEntryOperation;
9import net.onrc.onos.core.datastore.hazelcast.HZTable.VersionedValue;
10import net.onrc.onos.core.datastore.internal.IModifiableMultiEntryOperation;
11import net.onrc.onos.core.datastore.utils.ByteArrayUtil;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070012
Jonathan Harta99ec672014-04-03 11:30:34 -070013import org.slf4j.Logger;
14import org.slf4j.LoggerFactory;
15
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070016public class HZMultiEntryOperation implements IMultiEntryOperation, IModifiableMultiEntryOperation {
17 private static final Logger log = LoggerFactory.getLogger(HZMultiEntryOperation.class);
18
19 private final HZTable table;
20 private final byte[] key;
21 protected final OPERATION operation;
22 private STATUS status;
23
24 // for read op
25 private Future<VersionedValue> future;
26 // for write op
27 private VersionedValue writeValue;
28
29 /**
30 * Constructor for Read/ForceDelete Operation.
Ray Milkey269ffb92014-04-03 14:43:30 -070031 *
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070032 * @param table
33 * @param key
34 * @param operation
35 */
36 public HZMultiEntryOperation(final HZTable table, final byte[] key, final OPERATION operation) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070037 this.table = table;
Yuta HIGUCHIce7e7f82014-04-15 21:37:38 -070038 this.key = key.clone();
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070039 this.status = STATUS.NOT_EXECUTED;
40 this.operation = operation;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070041
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070042 this.future = null;
43 this.writeValue = null;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070044 }
45
46 /**
47 * Constructor for Other Operations.
Ray Milkey269ffb92014-04-03 14:43:30 -070048 *
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070049 * @param table
50 * @param key
51 * @param value
52 * @param version
53 * @param operation
54 */
Jonathan Hartc00f5c22014-06-10 15:14:40 -070055 public HZMultiEntryOperation(final HZTable table, final byte[] key,
56 final byte[] value, final long version, final OPERATION operation) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070057 this.table = table;
Yuta HIGUCHIce7e7f82014-04-15 21:37:38 -070058 this.key = key.clone();
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070059 this.status = STATUS.NOT_EXECUTED;
60 this.operation = operation;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070061
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070062 this.future = null;
63 this.writeValue = new VersionedValue(value, version);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070064 }
65
66 @Override
67 public boolean hasSucceeded() {
68
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070069 VersionedValue value = get();
70 return (value != null) && (this.status == STATUS.SUCCESS);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070071 }
72
73 @Override
74 public STATUS getStatus() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070075 get();
76 return status;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070077 }
78
79 @Override
80 public IKVTableID getTableId() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070081 return table;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070082 }
83
84 @Override
85 public byte[] getKey() {
Yuta HIGUCHIce7e7f82014-04-15 21:37:38 -070086 return key.clone();
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070087 }
88
89 @Override
90 public byte[] getValue() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070091 if (future != null) {
92 VersionedValue value = get();
93 return value.getValue();
94 }
95 return writeValue.getValue();
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070096 }
97
98 @Override
99 public long getVersion() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700100 if (future != null) {
101 VersionedValue value = get();
102 return value.getVersion();
103 }
104 return writeValue.getVersion();
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700105 }
106
107 @Override
108 public OPERATION getOperation() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700109 return operation;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700110 }
111
112 /**
113 * Evaluate Future object and set Status and Value+Version.
Ray Milkey269ffb92014-04-03 14:43:30 -0700114 *
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700115 * @return the value read or null on failure.
116 */
117 private VersionedValue get() {
118 try {
119 VersionedValue value = future.get();
Yuta HIGUCHI7a43d5b2014-04-15 14:08:17 -0700120 if (value == null) {
121 setStatus(STATUS.FAILED);
122 return null;
123 } else {
124 setValue(value.getValue(), value.getVersion());
125 setStatus(STATUS.SUCCESS);
126 return value;
127 }
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700128 } catch (CancellationException | InterruptedException | ExecutionException e) {
129 log.error(this + " has failed.", e);
130 setStatus(STATUS.FAILED);
131 return null;
132 }
133 }
134
135 @Override
136 public void setValue(final byte[] value, final long version) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700137 writeValue = new VersionedValue(value, version);
138 setVersion(version);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700139 }
140
141 @Override
142 public void setVersion(final long version) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700143 if (writeValue == null) {
144 writeValue = new VersionedValue(null, version);
145 }
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700146 }
147
148 @Override
149 public void setStatus(final STATUS status) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700150 this.status = status;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700151 }
152
153 @Override
154 public IModifiableMultiEntryOperation getActualOperation() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700155 return this;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700156 }
157
158 void setFuture(final Future<VersionedValue> future) {
159 this.future = future;
160 }
161
162 @Override
163 public String toString() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700164 return "[HZMultiEntryOperation table=" + table + ", key="
Yuta HIGUCHI805bc8f2014-04-16 11:51:43 -0700165 + ByteArrayUtil.toHexStringBuilder(key, ":") + ", operation=" + operation
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700166 + ", status=" + status + ", writeValue=" + writeValue + "]";
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700167 }
168}