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 | |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 7 | import net.onrc.onos.core.datastore.IKVTableID; |
| 8 | import net.onrc.onos.core.datastore.IMultiEntryOperation; |
| 9 | import net.onrc.onos.core.datastore.hazelcast.HZTable.VersionedValue; |
| 10 | import net.onrc.onos.core.datastore.internal.IModifiableMultiEntryOperation; |
| 11 | import net.onrc.onos.core.datastore.utils.ByteArrayUtil; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 12 | |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 13 | import org.slf4j.Logger; |
| 14 | import org.slf4j.LoggerFactory; |
| 15 | |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 16 | public 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 31 | * |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 32 | * @param table |
| 33 | * @param key |
| 34 | * @param operation |
| 35 | */ |
| 36 | public HZMultiEntryOperation(final HZTable table, final byte[] key, final OPERATION operation) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 37 | this.table = table; |
| 38 | this.key = key; |
| 39 | this.status = STATUS.NOT_EXECUTED; |
| 40 | this.operation = operation; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 41 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 42 | this.future = null; |
| 43 | this.writeValue = null; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Constructor for Other Operations. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 48 | * |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 49 | * @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 HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 56 | this.table = table; |
| 57 | this.key = key; |
| 58 | this.status = STATUS.NOT_EXECUTED; |
| 59 | this.operation = operation; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 60 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 61 | this.future = null; |
| 62 | this.writeValue = new VersionedValue(value, version); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public boolean hasSucceeded() { |
| 67 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 68 | VersionedValue value = get(); |
| 69 | return (value != null) && (this.status == STATUS.SUCCESS); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public STATUS getStatus() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 74 | get(); |
| 75 | return status; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public IKVTableID getTableId() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 80 | return table; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public byte[] getKey() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 85 | return key; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public byte[] getValue() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 90 | if (future != null) { |
| 91 | VersionedValue value = get(); |
| 92 | return value.getValue(); |
| 93 | } |
| 94 | return writeValue.getValue(); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public long getVersion() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 99 | if (future != null) { |
| 100 | VersionedValue value = get(); |
| 101 | return value.getVersion(); |
| 102 | } |
| 103 | return writeValue.getVersion(); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public OPERATION getOperation() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 108 | return operation; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Evaluate Future object and set Status and Value+Version. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 113 | * |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 114 | * @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 HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 131 | writeValue = new VersionedValue(value, version); |
| 132 | setVersion(version); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public void setVersion(final long version) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 137 | if (writeValue == null) { |
| 138 | writeValue = new VersionedValue(null, version); |
| 139 | } |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public void setStatus(final STATUS status) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 144 | this.status = status; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public IModifiableMultiEntryOperation getActualOperation() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 149 | return this; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | void setFuture(final Future<VersionedValue> future) { |
| 153 | this.future = future; |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public String toString() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 158 | return "[HZMultiEntryOperation table=" + table + ", key=" |
| 159 | + ByteArrayUtil.toHexStringBuffer(key, ":") + ", operation=" + operation |
| 160 | + ", status=" + status + ", writeValue=" + writeValue + "]"; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 161 | } |
| 162 | } |