blob: e5a6675445f53be410a374ccafc1f77484183030 [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
9import org.slf4j.Logger;
10import org.slf4j.LoggerFactory;
11
12/**
13 * Class to represent a Table in RAMCloud
14 */
15public class RCTable implements IKVTable {
16 @SuppressWarnings("unused")
17 private static final Logger log = LoggerFactory.getLogger(RCTable.class);
18
19 public static class Entry implements IKVEntry {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070020 final byte[] key;
21 byte[] value;
22 long version;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070023
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070024 public Entry(final byte[] key, final byte[] value, final long version) {
25 this.key = key;
26 this.setValue(value);
27 this.setVersion(version);
28 }
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070029
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070030 public Entry(final byte[] key) {
31 this(key, null, RCClient.VERSION_NONEXISTENT);
32 }
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070033
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070034 @Override
35 public byte[] getKey() {
36 return key;
37 }
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070038
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070039 @Override
40 public byte[] getValue() {
41 return value;
42 }
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070043
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070044 @Override
45 public long getVersion() {
46 return version;
47 }
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070048
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070049 void setValue(byte[] value) {
50 this.value = value;
51 }
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070052
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070053 void setVersion(long version) {
54 this.version = version;
55 }
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070056 }
57
58 private final RCTableID rcTableId;
59
60 /**
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070061 * {@code rcTableName} must be unique cluster wide.
Ray Milkey269ffb92014-04-03 14:43:30 -070062 *
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070063 * @param rcTableName RAMCloud table name
64 */
65 RCTable(final String rcTableName) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070066 this.rcTableId = new RCTableID(rcTableName);
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070067
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070068 // Trigger RAMCloud ID allocation. If lazy allocation is OK, remove.
69 this.rcTableId.getTableID();
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070070 }
71
72 @Override
73 public IKVTableID getTableId() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070074 return this.rcTableId;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070075 }
76
77 public String getTableName() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070078 return this.rcTableId.getTableName();
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070079 }
80
81 @Override
82 public long create(final byte[] key, final byte[] value)
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070083 throws ObjectExistsException {
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070084
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070085 return RCClient.getClient().create(this.rcTableId, key, value);
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070086 }
87
88 @Override
89 public long forceCreate(final byte[] key, final byte[] value) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070090 return RCClient.getClient().forceCreate(rcTableId, key, value);
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070091 }
92
93 @Override
94 public IKVEntry read(final byte[] key) throws ObjectDoesntExistException {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070095 return RCClient.getClient().read(rcTableId, key);
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070096 }
97
98 @Override
99 public long update(final byte[] key, final byte[] value, final long version)
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700100 throws ObjectDoesntExistException, WrongVersionException {
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700101
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700102 return RCClient.getClient().update(rcTableId, key, value, version);
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700103 }
104
105 @Override
106 public long update(final byte[] key, final byte[] value)
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700107 throws ObjectDoesntExistException {
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700108
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700109 return RCClient.getClient().update(rcTableId, key, value);
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700110 }
111
112 @Override
113 public long delete(final byte[] key, final long version)
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700114 throws ObjectDoesntExistException, WrongVersionException {
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700115
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700116 return RCClient.getClient().delete(rcTableId, key, version);
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700117 }
118
119 @Override
120 public long forceDelete(final byte[] key) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700121 return RCClient.getClient().forceDelete(rcTableId, key);
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700122 }
123
124 @Override
125 public Iterable<IKVEntry> getAllEntries() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700126 return RCClient.getClient().getAllEntries(this.getTableId());
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700127 }
128
129 @Override
130 public long VERSION_NONEXISTENT() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700131 return RCClient.VERSION_NONEXISTENT;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700132 }
133
134}