blob: 6ffb2cc8d1f22902b26c0d914715ce0f29b8a096 [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 HIGUCHIf148aac2014-05-05 14:59:06 -07008import net.onrc.onos.core.datastore.utils.ByteArrayUtil;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -07009
Yuta HIGUCHI498e1532014-08-20 21:30:28 -070010import org.apache.commons.lang3.ArrayUtils;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070011import org.slf4j.Logger;
12import org.slf4j.LoggerFactory;
13
14/**
Ray Milkeyb41100a2014-04-10 10:42:15 -070015 * Class to represent a Table in RAMCloud.
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070016 */
17public class RCTable implements IKVTable {
18 @SuppressWarnings("unused")
19 private static final Logger log = LoggerFactory.getLogger(RCTable.class);
20
21 public static class Entry implements IKVEntry {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070022 final byte[] key;
23 byte[] value;
24 long version;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070025
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070026 public Entry(final byte[] key, final byte[] value, final long version) {
Yuta HIGUCHIce7e7f82014-04-15 21:37:38 -070027 this.key = key.clone();
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070028 this.setValue(value);
29 this.setVersion(version);
30 }
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070031
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070032 public Entry(final byte[] key) {
33 this(key, null, RCClient.VERSION_NONEXISTENT);
34 }
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070035
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070036 @Override
37 public byte[] getKey() {
Yuta HIGUCHIce7e7f82014-04-15 21:37:38 -070038 return key.clone();
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070039 }
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070040
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070041 @Override
42 public byte[] getValue() {
Yuta HIGUCHIce7e7f82014-04-15 21:37:38 -070043 return ArrayUtils.clone(value);
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070044 }
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070045
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070046 @Override
47 public long getVersion() {
48 return version;
49 }
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070050
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070051 void setValue(byte[] value) {
Yuta HIGUCHIce7e7f82014-04-15 21:37:38 -070052 this.value = ArrayUtils.clone(value);
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070053 }
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070054
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070055 void setVersion(long version) {
56 this.version = version;
57 }
Yuta HIGUCHIf148aac2014-05-05 14:59:06 -070058
59 @Override
60 public String toString() {
61 return "[Entry key=" + ByteArrayUtil.toHexStringBuilder(key, ":") + ", value="
62 + ByteArrayUtil.toHexStringBuilder(value, ":") + ", version=" + version + "]";
63 }
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070064 }
65
66 private final RCTableID rcTableId;
67
68 /**
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070069 * {@code rcTableName} must be unique cluster wide.
Ray Milkey269ffb92014-04-03 14:43:30 -070070 *
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070071 * @param rcTableName RAMCloud table name
72 */
73 RCTable(final String rcTableName) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070074 this.rcTableId = new RCTableID(rcTableName);
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070075
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070076 // Trigger RAMCloud ID allocation. If lazy allocation is OK, remove.
77 this.rcTableId.getTableID();
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070078 }
79
80 @Override
81 public IKVTableID getTableId() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070082 return this.rcTableId;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070083 }
84
85 public String getTableName() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070086 return this.rcTableId.getTableName();
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070087 }
88
89 @Override
90 public long create(final byte[] key, final byte[] value)
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070091 throws ObjectExistsException {
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070092
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070093 return RCClient.getClient().create(this.rcTableId, key, value);
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070094 }
95
96 @Override
97 public long forceCreate(final byte[] key, final byte[] value) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070098 return RCClient.getClient().forceCreate(rcTableId, key, value);
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070099 }
100
101 @Override
102 public IKVEntry read(final byte[] key) throws ObjectDoesntExistException {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700103 return RCClient.getClient().read(rcTableId, key);
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700104 }
105
106 @Override
107 public long update(final byte[] key, final byte[] value, final long version)
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700108 throws ObjectDoesntExistException, WrongVersionException {
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700109
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700110 return RCClient.getClient().update(rcTableId, key, value, version);
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700111 }
112
113 @Override
114 public long update(final byte[] key, final byte[] value)
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700115 throws ObjectDoesntExistException {
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700116
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700117 return RCClient.getClient().update(rcTableId, key, value);
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700118 }
119
120 @Override
121 public long delete(final byte[] key, final long version)
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700122 throws ObjectDoesntExistException, WrongVersionException {
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700123
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700124 return RCClient.getClient().delete(rcTableId, key, version);
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700125 }
126
127 @Override
128 public long forceDelete(final byte[] key) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700129 return RCClient.getClient().forceDelete(rcTableId, key);
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700130 }
131
132 @Override
133 public Iterable<IKVEntry> getAllEntries() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700134 return RCClient.getClient().getAllEntries(this.getTableId());
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700135 }
136
137 @Override
Ray Milkey7531a342014-04-11 15:08:12 -0700138 public long getVersionNonexistant() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700139 return RCClient.VERSION_NONEXISTENT;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700140 }
141
142}