Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.datastore.hazelcast; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 2 | |
| 3 | import java.util.concurrent.CancellationException; |
| 4 | import java.util.concurrent.ExecutionException; |
| 5 | import java.util.concurrent.Future; |
| 6 | |
Pavlin Radoslavov | c9bacee | 2014-04-11 19:02:17 -0700 | [diff] [blame] | 7 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
| 8 | |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 9 | import net.onrc.onos.core.datastore.IKVTableID; |
| 10 | import net.onrc.onos.core.datastore.IMultiEntryOperation; |
| 11 | import net.onrc.onos.core.datastore.hazelcast.HZTable.VersionedValue; |
| 12 | import net.onrc.onos.core.datastore.internal.IModifiableMultiEntryOperation; |
| 13 | import net.onrc.onos.core.datastore.utils.ByteArrayUtil; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 14 | |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 15 | import org.slf4j.Logger; |
| 16 | import org.slf4j.LoggerFactory; |
| 17 | |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 18 | public class HZMultiEntryOperation implements IMultiEntryOperation, IModifiableMultiEntryOperation { |
| 19 | private static final Logger log = LoggerFactory.getLogger(HZMultiEntryOperation.class); |
| 20 | |
| 21 | private final HZTable table; |
| 22 | private final byte[] key; |
| 23 | protected final OPERATION operation; |
| 24 | private STATUS status; |
| 25 | |
| 26 | // for read op |
| 27 | private Future<VersionedValue> future; |
| 28 | // for write op |
| 29 | private VersionedValue writeValue; |
| 30 | |
| 31 | /** |
| 32 | * Constructor for Read/ForceDelete Operation. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 33 | * |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 34 | * @param table |
| 35 | * @param key |
| 36 | * @param operation |
| 37 | */ |
Pavlin Radoslavov | c9bacee | 2014-04-11 19:02:17 -0700 | [diff] [blame] | 38 | @SuppressFBWarnings(value = "EI_EXPOSE_REP2", |
| 39 | justification = "TODO: Store a copy of the object?") |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 40 | public HZMultiEntryOperation(final HZTable table, final byte[] key, final OPERATION operation) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 41 | this.table = table; |
| 42 | this.key = key; |
| 43 | this.status = STATUS.NOT_EXECUTED; |
| 44 | this.operation = operation; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 45 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 46 | this.future = null; |
| 47 | this.writeValue = null; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Constructor for Other Operations. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 52 | * |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 53 | * @param table |
| 54 | * @param key |
| 55 | * @param value |
| 56 | * @param version |
| 57 | * @param operation |
| 58 | */ |
Pavlin Radoslavov | c9bacee | 2014-04-11 19:02:17 -0700 | [diff] [blame] | 59 | @SuppressFBWarnings(value = "EI_EXPOSE_REP2", |
| 60 | justification = "TODO: Store a copy of the object?") |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 61 | public HZMultiEntryOperation(final HZTable table, final byte[] key, final byte[] value, final long version, final OPERATION operation) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 62 | this.table = table; |
| 63 | this.key = key; |
| 64 | this.status = STATUS.NOT_EXECUTED; |
| 65 | this.operation = operation; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 66 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 67 | this.future = null; |
| 68 | this.writeValue = new VersionedValue(value, version); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public boolean hasSucceeded() { |
| 73 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 74 | VersionedValue value = get(); |
| 75 | return (value != null) && (this.status == STATUS.SUCCESS); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public STATUS getStatus() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 80 | get(); |
| 81 | return status; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public IKVTableID getTableId() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 86 | return table; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | @Override |
Pavlin Radoslavov | c9bacee | 2014-04-11 19:02:17 -0700 | [diff] [blame] | 90 | @SuppressFBWarnings(value = "EI_EXPOSE_REP", |
| 91 | justification = "TODO: Return a copy of the object?") |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 92 | public byte[] getKey() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 93 | return key; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public byte[] getValue() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 98 | if (future != null) { |
| 99 | VersionedValue value = get(); |
| 100 | return value.getValue(); |
| 101 | } |
| 102 | return writeValue.getValue(); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public long getVersion() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 107 | if (future != null) { |
| 108 | VersionedValue value = get(); |
| 109 | return value.getVersion(); |
| 110 | } |
| 111 | return writeValue.getVersion(); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public OPERATION getOperation() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 116 | return operation; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Evaluate Future object and set Status and Value+Version. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 121 | * |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 122 | * @return the value read or null on failure. |
| 123 | */ |
| 124 | private VersionedValue get() { |
| 125 | try { |
| 126 | VersionedValue value = future.get(); |
Yuta HIGUCHI | 7a43d5b | 2014-04-15 14:08:17 -0700 | [diff] [blame] | 127 | if (value == null) { |
| 128 | setStatus(STATUS.FAILED); |
| 129 | return null; |
| 130 | } else { |
| 131 | setValue(value.getValue(), value.getVersion()); |
| 132 | setStatus(STATUS.SUCCESS); |
| 133 | return value; |
| 134 | } |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 135 | } catch (CancellationException | InterruptedException | ExecutionException e) { |
| 136 | log.error(this + " has failed.", e); |
| 137 | setStatus(STATUS.FAILED); |
| 138 | return null; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public void setValue(final byte[] value, final long version) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 144 | writeValue = new VersionedValue(value, version); |
| 145 | setVersion(version); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public void setVersion(final long version) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 150 | if (writeValue == null) { |
| 151 | writeValue = new VersionedValue(null, version); |
| 152 | } |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public void setStatus(final STATUS status) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 157 | this.status = status; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public IModifiableMultiEntryOperation getActualOperation() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 162 | return this; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | void setFuture(final Future<VersionedValue> future) { |
| 166 | this.future = future; |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public String toString() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 171 | return "[HZMultiEntryOperation table=" + table + ", key=" |
| 172 | + ByteArrayUtil.toHexStringBuffer(key, ":") + ", operation=" + operation |
| 173 | + ", status=" + status + ", writeValue=" + writeValue + "]"; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 174 | } |
| 175 | } |