blob: 105fc1621e8230dbc2223f434acc97d87fb623d4 [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.
31 * @param table
32 * @param key
33 * @param operation
34 */
35 public HZMultiEntryOperation(final HZTable table, final byte[] key, final OPERATION operation) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070036 this.table = table;
37 this.key = key;
38 this.status = STATUS.NOT_EXECUTED;
39 this.operation = operation;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070040
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070041 this.future = null;
42 this.writeValue = null;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070043 }
44
45 /**
46 * Constructor for Other Operations.
47 * @param table
48 * @param key
49 * @param value
50 * @param version
51 * @param operation
52 */
53 public HZMultiEntryOperation(final HZTable table, final byte[] key, final byte[] value, final long version, final OPERATION operation) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070054 this.table = table;
55 this.key = key;
56 this.status = STATUS.NOT_EXECUTED;
57 this.operation = operation;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070058
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070059 this.future = null;
60 this.writeValue = new VersionedValue(value, version);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070061 }
62
63 @Override
64 public boolean hasSucceeded() {
65
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070066 VersionedValue value = get();
67 return (value != null) && (this.status == STATUS.SUCCESS);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070068 }
69
70 @Override
71 public STATUS getStatus() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070072 get();
73 return status;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070074 }
75
76 @Override
77 public IKVTableID getTableId() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070078 return table;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070079 }
80
81 @Override
82 public byte[] getKey() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070083 return key;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070084 }
85
86 @Override
87 public byte[] getValue() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070088 if (future != null) {
89 VersionedValue value = get();
90 return value.getValue();
91 }
92 return writeValue.getValue();
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070093 }
94
95 @Override
96 public long getVersion() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070097 if (future != null) {
98 VersionedValue value = get();
99 return value.getVersion();
100 }
101 return writeValue.getVersion();
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700102 }
103
104 @Override
105 public OPERATION getOperation() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700106 return operation;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700107 }
108
109 /**
110 * Evaluate Future object and set Status and Value+Version.
111 * @return the value read or null on failure.
112 */
113 private VersionedValue get() {
114 try {
115 VersionedValue value = future.get();
116 setValue(value.getValue(), value.getVersion());
117 setStatus(STATUS.SUCCESS);
118 return value;
119 } catch (CancellationException | InterruptedException | ExecutionException e) {
120 log.error(this + " has failed.", e);
121 setStatus(STATUS.FAILED);
122 return null;
123 }
124 }
125
126 @Override
127 public void setValue(final byte[] value, final long version) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700128 writeValue = new VersionedValue(value, version);
129 setVersion(version);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700130 }
131
132 @Override
133 public void setVersion(final long version) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700134 if (future != null) {
135 // no-op on read
136 }
137 if (writeValue == null) {
138 writeValue = new VersionedValue(null, version);
139 }
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700140 }
141
142 @Override
143 public void setStatus(final STATUS status) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700144 this.status = status;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700145 }
146
147 @Override
148 public IModifiableMultiEntryOperation getActualOperation() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700149 return this;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700150 }
151
152 void setFuture(final Future<VersionedValue> future) {
153 this.future = future;
154 }
155
156 @Override
157 public String toString() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700158 return "[HZMultiEntryOperation table=" + table + ", key="
159 + ByteArrayUtil.toHexStringBuffer(key, ":") + ", operation=" + operation
160 + ", status=" + status + ", writeValue=" + writeValue + "]";
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700161 }
162}