blob: 4a92b4f0f30175e756177e65b2c61c2e7d7f4fad [file] [log] [blame]
Jonathan Hart6df90172014-04-03 10:13:11 -07001package net.onrc.onos.core.datastore.ramcloud;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -07002
Jonathan Hart6df90172014-04-03 10:13:11 -07003import net.onrc.onos.core.datastore.IKVTable;
4import net.onrc.onos.core.datastore.IKVTableID;
5import net.onrc.onos.core.datastore.ObjectDoesntExistException;
6import net.onrc.onos.core.datastore.ObjectExistsException;
7import net.onrc.onos.core.datastore.WrongVersionException;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -07008
Yuta HIGUCHIce7e7f82014-04-15 21:37:38 -07009import org.apache.commons.lang.ArrayUtils;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070010import org.slf4j.Logger;
11import org.slf4j.LoggerFactory;
12
13/**
Ray Milkeyb41100a2014-04-10 10:42:15 -070014 * Class to represent a Table in RAMCloud.
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070015 */
16public class RCTable implements IKVTable {
17 @SuppressWarnings("unused")
18 private static final Logger log = LoggerFactory.getLogger(RCTable.class);
19
20 public static class Entry implements IKVEntry {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070021 final byte[] key;
22 byte[] value;
23 long version;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070024
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070025 public Entry(final byte[] key, final byte[] value, final long version) {
Yuta HIGUCHIce7e7f82014-04-15 21:37:38 -070026 this.key = key.clone();
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070027 this.setValue(value);
28 this.setVersion(version);
29 }
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070030
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070031 public Entry(final byte[] key) {
32 this(key, null, RCClient.VERSION_NONEXISTENT);
33 }
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070034
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070035 @Override
36 public byte[] getKey() {
Yuta HIGUCHIce7e7f82014-04-15 21:37:38 -070037 return key.clone();
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070038 }
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070039
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070040 @Override
41 public byte[] getValue() {
Yuta HIGUCHIce7e7f82014-04-15 21:37:38 -070042 return ArrayUtils.clone(value);
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070043 }
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070044
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070045 @Override
46 public long getVersion() {
47 return version;
48 }
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070049
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070050 void setValue(byte[] value) {
Yuta HIGUCHIce7e7f82014-04-15 21:37:38 -070051 this.value = ArrayUtils.clone(value);
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070052 }
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070053
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070054 void setVersion(long version) {
55 this.version = version;
56 }
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070057 }
58
59 private final RCTableID rcTableId;
60
61 /**
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070062 * {@code rcTableName} must be unique cluster wide.
Ray Milkey269ffb92014-04-03 14:43:30 -070063 *
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070064 * @param rcTableName RAMCloud table name
65 */
66 RCTable(final String rcTableName) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070067 this.rcTableId = new RCTableID(rcTableName);
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070068
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070069 // Trigger RAMCloud ID allocation. If lazy allocation is OK, remove.
70 this.rcTableId.getTableID();
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070071 }
72
73 @Override
74 public IKVTableID getTableId() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070075 return this.rcTableId;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070076 }
77
78 public String getTableName() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070079 return this.rcTableId.getTableName();
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070080 }
81
82 @Override
83 public long create(final byte[] key, final byte[] value)
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070084 throws ObjectExistsException {
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070085
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070086 return RCClient.getClient().create(this.rcTableId, key, value);
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070087 }
88
89 @Override
90 public long forceCreate(final byte[] key, final byte[] value) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070091 return RCClient.getClient().forceCreate(rcTableId, key, value);
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070092 }
93
94 @Override
95 public IKVEntry read(final byte[] key) throws ObjectDoesntExistException {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070096 return RCClient.getClient().read(rcTableId, key);
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070097 }
98
99 @Override
100 public long update(final byte[] key, final byte[] value, final long version)
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700101 throws ObjectDoesntExistException, WrongVersionException {
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700102
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700103 return RCClient.getClient().update(rcTableId, key, value, version);
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700104 }
105
106 @Override
107 public long update(final byte[] key, final byte[] value)
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700108 throws ObjectDoesntExistException {
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700109
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700110 return RCClient.getClient().update(rcTableId, key, value);
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700111 }
112
113 @Override
114 public long delete(final byte[] key, final long version)
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700115 throws ObjectDoesntExistException, WrongVersionException {
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700116
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700117 return RCClient.getClient().delete(rcTableId, key, version);
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700118 }
119
120 @Override
121 public long forceDelete(final byte[] key) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700122 return RCClient.getClient().forceDelete(rcTableId, key);
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700123 }
124
125 @Override
126 public Iterable<IKVEntry> getAllEntries() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700127 return RCClient.getClient().getAllEntries(this.getTableId());
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700128 }
129
130 @Override
Ray Milkey7531a342014-04-11 15:08:12 -0700131 public long getVersionNonexistant() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700132 return RCClient.VERSION_NONEXISTENT;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700133 }
134
135}