blob: 88649e7a9a3e3f96c403bf4a91579f62f08e93ce [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;
38 this.key = key;
39 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;
57 this.key = key;
58 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 HIGUCHI826b4a42014-03-24 13:10:33 -070085 return key;
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();
119 setValue(value.getValue(), value.getVersion());
120 setStatus(STATUS.SUCCESS);
121 return value;
122 } catch (CancellationException | InterruptedException | ExecutionException e) {
123 log.error(this + " has failed.", e);
124 setStatus(STATUS.FAILED);
125 return null;
126 }
127 }
128
129 @Override
130 public void setValue(final byte[] value, final long version) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700131 writeValue = new VersionedValue(value, version);
132 setVersion(version);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700133 }
134
135 @Override
136 public void setVersion(final long version) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700137 if (future != null) {
138 // no-op on read
139 }
140 if (writeValue == null) {
141 writeValue = new VersionedValue(null, version);
142 }
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700143 }
144
145 @Override
146 public void setStatus(final STATUS status) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700147 this.status = status;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700148 }
149
150 @Override
151 public IModifiableMultiEntryOperation getActualOperation() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700152 return this;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700153 }
154
155 void setFuture(final Future<VersionedValue> future) {
156 this.future = future;
157 }
158
159 @Override
160 public String toString() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700161 return "[HZMultiEntryOperation table=" + table + ", key="
162 + ByteArrayUtil.toHexStringBuffer(key, ":") + ", operation=" + operation
163 + ", status=" + status + ", writeValue=" + writeValue + "]";
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700164 }
165}