blob: 1da7f1affe9b0d9b494ff62071e51f51c8cbd8e3 [file] [log] [blame]
Yuta HIGUCHI6a643132014-03-18 22:39:27 -07001package net.onrc.onos.datastore.hazelcast;
2
3import java.util.concurrent.CancellationException;
4import java.util.concurrent.ExecutionException;
5import java.util.concurrent.Future;
6
7import org.slf4j.Logger;
8import org.slf4j.LoggerFactory;
9
10import net.onrc.onos.datastore.IKVTableID;
11import net.onrc.onos.datastore.IMultiEntryOperation;
12import net.onrc.onos.datastore.hazelcast.HZTable.VersionedValue;
13import net.onrc.onos.datastore.internal.IModifiableMultiEntryOperation;
14import net.onrc.onos.datastore.utils.ByteArrayUtil;
15
16public 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) {
36 this.table = table;
37 this.key = key;
38 this.status = STATUS.NOT_EXECUTED;
39 this.operation = operation;
40
41 this.future = null;
42 this.writeValue = null;
43 }
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) {
54 this.table = table;
55 this.key = key;
56 this.status = STATUS.NOT_EXECUTED;
57 this.operation = operation;
58
59 this.future = null;
60 this.writeValue = new VersionedValue(value, version);
61 }
62
63 @Override
64 public boolean hasSucceeded() {
65
66 VersionedValue value = get();
67 return (value != null) && (this.status == STATUS.SUCCESS);
68 }
69
70 @Override
71 public STATUS getStatus() {
72 get();
73 return status;
74 }
75
76 @Override
77 public IKVTableID getTableId() {
78 return table;
79 }
80
81 @Override
82 public byte[] getKey() {
83 return key;
84 }
85
86 @Override
87 public byte[] getValue() {
88 if (future != null) {
89 VersionedValue value = get();
90 return value.getValue();
91 }
92 return writeValue.getValue();
93 }
94
95 @Override
96 public long getVersion() {
97 if (future != null) {
98 VersionedValue value = get();
99 return value.getVersion();
100 }
101 return writeValue.getVersion();
102 }
103
104 @Override
105 public OPERATION getOperation() {
106 return operation;
107 }
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) {
128 writeValue = new VersionedValue(value, version);
129 setVersion(version);
130 }
131
132 @Override
133 public void setVersion(final long version) {
134 if (future != null) {
135 // no-op on read
136 }
137 if (writeValue == null) {
138 writeValue = new VersionedValue(null, version);
139 }
140 }
141
142 @Override
143 public void setStatus(final STATUS status) {
144 this.status = status;
145 }
146
147 @Override
148 public IModifiableMultiEntryOperation getActualOperation() {
149 return this;
150 }
151
152 void setFuture(final Future<VersionedValue> future) {
153 this.future = future;
154 }
155
156 @Override
157 public String toString() {
158 return "[HZMultiEntryOperation table=" + table + ", key="
159 + ByteArrayUtil.toHexStringBuffer(key, ":") + ", operation=" + operation
160 + ", status=" + status + ", writeValue=" + writeValue + "]";
161 }
162}