blob: 58df13b771bd9235a88213932f187b518aaa4a68 [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 */
55 public HZMultiEntryOperation(final HZTable table, final byte[] key, final byte[] value, final long version, final OPERATION operation) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070056 this.table = table;
Yuta HIGUCHIce7e7f82014-04-15 21:37:38 -070057 this.key = key.clone();
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070058 this.status = STATUS.NOT_EXECUTED;
59 this.operation = operation;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070060
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070061 this.future = null;
62 this.writeValue = new VersionedValue(value, version);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070063 }
64
65 @Override
66 public boolean hasSucceeded() {
67
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070068 VersionedValue value = get();
69 return (value != null) && (this.status == STATUS.SUCCESS);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070070 }
71
72 @Override
73 public STATUS getStatus() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070074 get();
75 return status;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070076 }
77
78 @Override
79 public IKVTableID getTableId() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070080 return table;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070081 }
82
83 @Override
84 public byte[] getKey() {
Yuta HIGUCHIce7e7f82014-04-15 21:37:38 -070085 return key.clone();
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070086 }
87
88 @Override
89 public byte[] getValue() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070090 if (future != null) {
91 VersionedValue value = get();
92 return value.getValue();
93 }
94 return writeValue.getValue();
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070095 }
96
97 @Override
98 public long getVersion() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070099 if (future != null) {
100 VersionedValue value = get();
101 return value.getVersion();
102 }
103 return writeValue.getVersion();
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700104 }
105
106 @Override
107 public OPERATION getOperation() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700108 return operation;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700109 }
110
111 /**
112 * Evaluate Future object and set Status and Value+Version.
Ray Milkey269ffb92014-04-03 14:43:30 -0700113 *
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700114 * @return the value read or null on failure.
115 */
116 private VersionedValue get() {
117 try {
118 VersionedValue value = future.get();
Yuta HIGUCHI7a43d5b2014-04-15 14:08:17 -0700119 if (value == null) {
120 setStatus(STATUS.FAILED);
121 return null;
122 } else {
123 setValue(value.getValue(), value.getVersion());
124 setStatus(STATUS.SUCCESS);
125 return value;
126 }
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700127 } catch (CancellationException | InterruptedException | ExecutionException e) {
128 log.error(this + " has failed.", e);
129 setStatus(STATUS.FAILED);
130 return null;
131 }
132 }
133
134 @Override
135 public void setValue(final byte[] value, final long version) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700136 writeValue = new VersionedValue(value, version);
137 setVersion(version);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700138 }
139
140 @Override
141 public void setVersion(final long version) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700142 if (writeValue == null) {
143 writeValue = new VersionedValue(null, version);
144 }
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700145 }
146
147 @Override
148 public void setStatus(final STATUS status) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700149 this.status = status;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700150 }
151
152 @Override
153 public IModifiableMultiEntryOperation getActualOperation() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700154 return this;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700155 }
156
157 void setFuture(final Future<VersionedValue> future) {
158 this.future = future;
159 }
160
161 @Override
162 public String toString() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700163 return "[HZMultiEntryOperation table=" + table + ", key="
Yuta HIGUCHI805bc8f2014-04-16 11:51:43 -0700164 + ByteArrayUtil.toHexStringBuilder(key, ":") + ", operation=" + operation
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700165 + ", status=" + status + ", writeValue=" + writeValue + "]";
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700166 }
167}