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 | |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 3 | import java.util.Collection; |
| 4 | import java.util.List; |
| 5 | |
Yuta HIGUCHI | 6dfba39 | 2014-05-28 15:45:44 -0700 | [diff] [blame] | 6 | import net.onrc.onos.core.datagrid.HazelcastDatagrid; |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 7 | import net.onrc.onos.core.datastore.IKVClient; |
| 8 | import net.onrc.onos.core.datastore.IKVTable; |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 9 | import net.onrc.onos.core.datastore.IKVTable.IKVEntry; |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 10 | import net.onrc.onos.core.datastore.IKVTableID; |
| 11 | import net.onrc.onos.core.datastore.IMultiEntryOperation; |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 12 | import net.onrc.onos.core.datastore.IMultiEntryOperation.OPERATION; |
| 13 | import net.onrc.onos.core.datastore.IMultiEntryOperation.STATUS; |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 14 | import net.onrc.onos.core.datastore.ObjectDoesntExistException; |
| 15 | import net.onrc.onos.core.datastore.ObjectExistsException; |
| 16 | import net.onrc.onos.core.datastore.WrongVersionException; |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 17 | import net.onrc.onos.core.datastore.hazelcast.HZTable.VersionedValue; |
| 18 | import net.onrc.onos.core.datastore.internal.IModifiableMultiEntryOperation; |
Yuta HIGUCHI | d47eac3 | 2014-04-07 13:44:47 -0700 | [diff] [blame] | 19 | import net.onrc.onos.core.datastore.utils.ByteArrayUtil; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 20 | import org.slf4j.Logger; |
| 21 | import org.slf4j.LoggerFactory; |
| 22 | |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 23 | import com.hazelcast.config.Config; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 24 | import com.hazelcast.core.Hazelcast; |
| 25 | import com.hazelcast.core.HazelcastInstance; |
Yuta HIGUCHI | d47eac3 | 2014-04-07 13:44:47 -0700 | [diff] [blame] | 26 | import com.hazelcast.core.IAtomicLong; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 27 | import com.hazelcast.core.IMap; |
| 28 | |
Yuta HIGUCHI | 6dfba39 | 2014-05-28 15:45:44 -0700 | [diff] [blame] | 29 | /** |
| 30 | * Hazelcast implementation of datastore IKVClient. |
| 31 | */ |
Ray Milkey | 1584ec8 | 2014-04-10 11:58:30 -0700 | [diff] [blame] | 32 | public final class HZClient implements IKVClient { |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 33 | private static final Logger log = LoggerFactory.getLogger(HZClient.class); |
| 34 | |
| 35 | static final long VERSION_NONEXISTENT = 0L; |
| 36 | |
| 37 | private static final String MAP_PREFIX = "datastore://"; |
| 38 | |
Jonathan Hart | c00f5c2 | 2014-06-10 15:14:40 -0700 | [diff] [blame] | 39 | private static final String BASE_CONFIG_FILENAME = |
| 40 | System.getProperty("net.onrc.onos.core.datastore.hazelcast.baseConfig", "conf/hazelcast.xml"); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 41 | |
| 42 | private final HazelcastInstance hazelcastInstance; |
| 43 | |
| 44 | private static final HZClient THE_INSTANCE = new HZClient(); |
| 45 | |
Yuta HIGUCHI | 8447c36 | 2014-05-30 11:32:49 -0700 | [diff] [blame] | 46 | /** |
| 47 | * Get DataStoreClient implemented on Hazelcast. |
| 48 | * |
| 49 | * @return HZClient |
| 50 | */ |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 51 | public static HZClient getClient() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 52 | return THE_INSTANCE; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Yuta HIGUCHI | 6dfba39 | 2014-05-28 15:45:44 -0700 | [diff] [blame] | 55 | /** |
| 56 | * Default constructor. |
| 57 | * <p/> |
| 58 | * Get or create the Hazelcast Instance to use for datastore. |
| 59 | */ |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 60 | private HZClient() { |
Yuta HIGUCHI | 448bca0 | 2014-08-13 17:08:00 -0700 | [diff] [blame] | 61 | Config config = HazelcastDatagrid.loadHazelcastConfig(BASE_CONFIG_FILENAME); |
Yuta HIGUCHI | 6dfba39 | 2014-05-28 15:45:44 -0700 | [diff] [blame] | 62 | // Try to get the existing HZ instance in JVM if possible. |
Yuta HIGUCHI | fde382d | 2014-08-20 17:22:17 -0700 | [diff] [blame] | 63 | hazelcastInstance = Hazelcast.getOrCreateHazelcastInstance(config); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Yuta HIGUCHI | 6dfba39 | 2014-05-28 15:45:44 -0700 | [diff] [blame] | 66 | /** |
Yuta HIGUCHI | 18e0db8 | 2014-06-16 09:44:21 -0700 | [diff] [blame] | 67 | * Gets the HazelcastInstance object. |
| 68 | * |
| 69 | * @return HazelcastInstance |
| 70 | */ |
| 71 | HazelcastInstance getHZInstance() { |
| 72 | return hazelcastInstance; |
| 73 | } |
| 74 | |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 75 | @Override |
| 76 | public IKVTable getTable(final String tableName) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 77 | IMap<byte[], VersionedValue> map = hazelcastInstance.getMap(MAP_PREFIX + tableName); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 78 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 79 | return new HZTable(tableName, map); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public void dropTable(final IKVTable table) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 84 | ((HZTable) table).getBackendMap().clear(); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public long create(final IKVTableID tableId, final byte[] key, final byte[] value) |
| 89 | throws ObjectExistsException { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 90 | IKVTable table = (IKVTable) tableId; |
| 91 | return table.create(key, value); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public long forceCreate(final IKVTableID tableId, final byte[] key, final byte[] value) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 96 | IKVTable table = (IKVTable) tableId; |
| 97 | return table.forceCreate(key, value); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public IKVEntry read(final IKVTableID tableId, final byte[] key) |
| 102 | throws ObjectDoesntExistException { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 103 | IKVTable table = (IKVTable) tableId; |
| 104 | return table.read(key); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public long update(final IKVTableID tableId, final byte[] key, final byte[] value, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 109 | final long version) throws ObjectDoesntExistException, |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 110 | WrongVersionException { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 111 | IKVTable table = (IKVTable) tableId; |
| 112 | return table.update(key, value, version); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public long update(final IKVTableID tableId, final byte[] key, final byte[] value) |
| 117 | throws ObjectDoesntExistException { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 118 | IKVTable table = (IKVTable) tableId; |
| 119 | return table.update(key, value); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public long delete(final IKVTableID tableId, final byte[] key, final long version) |
| 124 | throws ObjectDoesntExistException, WrongVersionException { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 125 | IKVTable table = (IKVTable) tableId; |
| 126 | return table.delete(key, version); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public long forceDelete(final IKVTableID tableId, final byte[] key) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 131 | IKVTable table = (IKVTable) tableId; |
| 132 | return table.forceDelete(key); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public Iterable<IKVEntry> getAllEntries(final IKVTableID tableId) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 137 | IKVTable table = (IKVTable) tableId; |
| 138 | return table.getAllEntries(); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public IMultiEntryOperation createOp(final IKVTableID tableId, final byte[] key, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 143 | final byte[] value) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 144 | return new HZMultiEntryOperation((HZTable) tableId, key, value, HZClient.VERSION_NONEXISTENT, OPERATION.CREATE); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public IMultiEntryOperation forceCreateOp(final IKVTableID tableId, final byte[] key, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 149 | final byte[] value) { |
Jonathan Hart | c00f5c2 | 2014-06-10 15:14:40 -0700 | [diff] [blame] | 150 | return new HZMultiEntryOperation((HZTable) tableId, key, value, |
| 151 | HZClient.VERSION_NONEXISTENT, OPERATION.FORCE_CREATE); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public IMultiEntryOperation readOp(final IKVTableID tableId, final byte[] key) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 156 | return new HZMultiEntryOperation((HZTable) tableId, key, OPERATION.READ); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public IMultiEntryOperation updateOp(final IKVTableID tableId, final byte[] key, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 161 | final byte[] value, final long version) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 162 | return new HZMultiEntryOperation((HZTable) tableId, key, value, version, OPERATION.UPDATE); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public IMultiEntryOperation deleteOp(final IKVTableID tableId, final byte[] key, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 167 | final byte[] value, final long version) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 168 | return new HZMultiEntryOperation((HZTable) tableId, key, value, version, OPERATION.DELETE); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public IMultiEntryOperation forceDeleteOp(final IKVTableID tableId, final byte[] key) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 173 | return new HZMultiEntryOperation((HZTable) tableId, key, OPERATION.FORCE_DELETE); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public boolean multiDelete(final Collection<IMultiEntryOperation> ops) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 178 | boolean failExists = false; |
| 179 | for (IMultiEntryOperation op : ops) { |
| 180 | HZMultiEntryOperation mop = (HZMultiEntryOperation) op; |
| 181 | switch (mop.getOperation()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 182 | case DELETE: |
| 183 | try { |
| 184 | final long version = delete(mop.getTableId(), mop.getKey(), mop.getVersion()); |
| 185 | mop.setVersion(version); |
| 186 | mop.setStatus(STATUS.SUCCESS); |
| 187 | } catch (ObjectDoesntExistException | WrongVersionException e) { |
| 188 | log.error(mop + " failed.", e); |
| 189 | mop.setStatus(STATUS.FAILED); |
| 190 | failExists = true; |
| 191 | } |
| 192 | break; |
| 193 | case FORCE_DELETE: |
| 194 | final long version = forceDelete(mop.getTableId(), mop.getKey()); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 195 | mop.setVersion(version); |
| 196 | mop.setStatus(STATUS.SUCCESS); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 197 | break; |
| 198 | default: |
| 199 | throw new UnsupportedOperationException(mop.toString()); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | return failExists; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public boolean multiWrite(final List<IMultiEntryOperation> ops) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 207 | // there may be room to batch to improve performance |
| 208 | boolean failExists = false; |
| 209 | for (IMultiEntryOperation op : ops) { |
| 210 | IModifiableMultiEntryOperation mop = (IModifiableMultiEntryOperation) op; |
| 211 | switch (mop.getOperation()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 212 | case CREATE: |
| 213 | try { |
| 214 | long version = create(mop.getTableId(), mop.getKey(), mop.getValue()); |
| 215 | mop.setVersion(version); |
| 216 | mop.setStatus(STATUS.SUCCESS); |
| 217 | } catch (ObjectExistsException e) { |
| 218 | log.error(mop + " failed.", e); |
| 219 | mop.setStatus(STATUS.FAILED); |
| 220 | failExists = true; |
| 221 | } |
| 222 | break; |
| 223 | case FORCE_CREATE: { |
| 224 | final long version = forceCreate(mop.getTableId(), mop.getKey(), mop.getValue()); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 225 | mop.setVersion(version); |
| 226 | mop.setStatus(STATUS.SUCCESS); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 227 | break; |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 228 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 229 | case UPDATE: |
| 230 | try { |
| 231 | long version = update(mop.getTableId(), mop.getKey(), mop.getValue(), mop.getVersion()); |
| 232 | mop.setVersion(version); |
| 233 | mop.setStatus(STATUS.SUCCESS); |
| 234 | } catch (ObjectDoesntExistException | WrongVersionException e) { |
| 235 | log.error(mop + " failed.", e); |
| 236 | mop.setStatus(STATUS.FAILED); |
| 237 | failExists = true; |
| 238 | } |
| 239 | break; |
| 240 | default: |
| 241 | throw new UnsupportedOperationException(mop.toString()); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | return failExists; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | @Override |
| 248 | public boolean multiRead(final Collection<IMultiEntryOperation> ops) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 249 | boolean failExists = false; |
| 250 | for (IMultiEntryOperation op : ops) { |
| 251 | IModifiableMultiEntryOperation mop = (IModifiableMultiEntryOperation) op; |
| 252 | HZTable table = (HZTable) op.getTableId(); |
| 253 | ((HZMultiEntryOperation) mop.getActualOperation()).setFuture(table.getBackendMap().getAsync(op.getKey())); |
| 254 | } |
| 255 | for (IMultiEntryOperation op : ops) { |
| 256 | IModifiableMultiEntryOperation mop = (IModifiableMultiEntryOperation) op; |
Ray Milkey | 1aa71f8 | 2014-04-08 16:23:24 -0700 | [diff] [blame] | 257 | if (!mop.hasSucceeded()) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 258 | failExists = true; |
| 259 | } |
| 260 | } |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 261 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 262 | return failExists; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | @Override |
Ray Milkey | 7531a34 | 2014-04-11 15:08:12 -0700 | [diff] [blame] | 266 | public long getVersionNonexistant() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 267 | return VERSION_NONEXISTENT; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Yuta HIGUCHI | d47eac3 | 2014-04-07 13:44:47 -0700 | [diff] [blame] | 270 | private String getCounterName(final IKVTableID tableId, final byte[] key) { |
Yuta HIGUCHI | 805bc8f | 2014-04-16 11:51:43 -0700 | [diff] [blame] | 271 | StringBuilder buf = new StringBuilder(tableId.getTableName()); |
Yuta HIGUCHI | d47eac3 | 2014-04-07 13:44:47 -0700 | [diff] [blame] | 272 | buf.append('@'); |
Yuta HIGUCHI | 805bc8f | 2014-04-16 11:51:43 -0700 | [diff] [blame] | 273 | ByteArrayUtil.toHexStringBuilder(key, ":", buf); |
Yuta HIGUCHI | d47eac3 | 2014-04-07 13:44:47 -0700 | [diff] [blame] | 274 | return buf.toString(); |
| 275 | } |
| 276 | |
| 277 | private IAtomicLong getAtomicLong(final IKVTableID tableId, final byte[] key) { |
| 278 | // TODO we probably want to implement some sort of caching |
| 279 | return hazelcastInstance.getAtomicLong(getCounterName(tableId, key)); |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * {@inheritDoc} |
| 284 | * <p /> |
| 285 | * Warning: The counter is a different object from {@code key} entry on |
| 286 | * IKVTable with {@code tableId}. You cannot use table API to read/write |
| 287 | * counters. |
| 288 | * |
| 289 | * @param tableId Only getTableName() will be used. |
| 290 | * @param key tableId + key will be used as Counter name |
| 291 | */ |
| 292 | @Override |
| 293 | public void createCounter(final IKVTableID tableId, |
| 294 | final byte[] key, final long initialValue) |
| 295 | throws ObjectExistsException { |
| 296 | |
| 297 | IAtomicLong counter = getAtomicLong(tableId, key); |
| 298 | // Assumption here is that AtomicLong is initialized to 0L |
| 299 | final boolean success = counter.compareAndSet(0L, initialValue); |
| 300 | if (!success) { |
| 301 | throw new ObjectExistsException("Atomic counter " |
| 302 | + getCounterName(tableId, key) |
| 303 | + " already exist with value:" + counter.get()); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | @Override |
| 308 | public void setCounter(final IKVTableID tableId, |
| 309 | final byte[] key, final long value) { |
| 310 | |
| 311 | IAtomicLong counter = getAtomicLong(tableId, key); |
| 312 | counter.set(value); |
| 313 | } |
| 314 | |
| 315 | @Override |
| 316 | public long incrementCounter(final IKVTableID tableId, |
| 317 | final byte[] key, final long incrementValue) { |
| 318 | |
| 319 | IAtomicLong counter = getAtomicLong(tableId, key); |
| 320 | return counter.addAndGet(incrementValue); |
| 321 | } |
| 322 | |
| 323 | @Override |
| 324 | public void destroyCounter(final IKVTableID tableId, final byte[] key) { |
| 325 | IAtomicLong counter = getAtomicLong(tableId, key); |
| 326 | counter.destroy(); |
| 327 | |
| 328 | } |
| 329 | |
| 330 | @Override |
| 331 | public long getCounter(final IKVTableID tableId, final byte[] key) |
| 332 | throws ObjectDoesntExistException { |
| 333 | |
| 334 | IAtomicLong counter = getAtomicLong(tableId, key); |
| 335 | return counter.get(); |
| 336 | } |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 337 | |
| 338 | } |