Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.datastore.utils; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.HashMap; |
| 5 | import java.util.Iterator; |
| 6 | import java.util.List; |
| 7 | import java.util.Map; |
| 8 | |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 9 | import net.onrc.onos.core.datastore.DataStoreClient; |
| 10 | import net.onrc.onos.core.datastore.IKVClient; |
| 11 | import net.onrc.onos.core.datastore.IKVTable; |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 12 | import net.onrc.onos.core.datastore.IKVTable.IKVEntry; |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 13 | import net.onrc.onos.core.datastore.IKVTableID; |
| 14 | import net.onrc.onos.core.datastore.IMultiEntryOperation; |
| 15 | import net.onrc.onos.core.datastore.IMultiObjectOperation; |
| 16 | import net.onrc.onos.core.datastore.ObjectDoesntExistException; |
| 17 | import net.onrc.onos.core.datastore.ObjectExistsException; |
| 18 | import net.onrc.onos.core.datastore.WrongVersionException; |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 19 | import net.onrc.onos.core.datastore.internal.IModifiableMultiEntryOperation; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 20 | |
| 21 | import org.slf4j.Logger; |
| 22 | import org.slf4j.LoggerFactory; |
| 23 | |
| 24 | import com.esotericsoftware.kryo.Kryo; |
| 25 | import com.esotericsoftware.kryo.io.Input; |
| 26 | import com.esotericsoftware.kryo.io.Output; |
| 27 | |
| 28 | /** |
| 29 | * Class to represent an Object represented as a single K-V pair Value blob. |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 30 | */ |
| 31 | public class KVObject { |
| 32 | private static final Logger log = LoggerFactory.getLogger(KVObject.class); |
| 33 | |
| 34 | // Default Kryo serializer. |
| 35 | // each sub-class should prepare their own serializer, which has required |
| 36 | // objects registered for better performance. |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 37 | private static final ThreadLocal<Kryo> DEFAULT_KRYO = new ThreadLocal<Kryo>() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 38 | @Override |
| 39 | protected Kryo initialValue() { |
| 40 | Kryo kryo = new Kryo(); |
| 41 | // kryo.setRegistrationRequired(true); |
| 42 | // kryo.setReferences(false); |
| 43 | return kryo; |
| 44 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame^] | 47 | /** |
| 48 | * Default namespace name for KVObject. |
| 49 | */ |
| 50 | public static final String DEFAULT_NAMESPACE = "G"; |
| 51 | |
| 52 | private final String namespace; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 53 | private final IKVTable table; |
| 54 | private final byte[] key; |
| 55 | |
| 56 | /** |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame^] | 57 | * Serialized-value version stored on data store or |
| 58 | * {@link IKVTable.getVersionNonexistant()} if it is a new object. |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 59 | */ |
| 60 | private long version; |
| 61 | |
| 62 | /** |
Ray Milkey | b41100a | 2014-04-10 10:42:15 -0700 | [diff] [blame] | 63 | * Map to store user-defined properties. |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 64 | */ |
| 65 | private Map<Object, Object> propertyMap; |
| 66 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame^] | 67 | /** |
| 68 | * KVObject constructor without value secified for specified namespace. |
| 69 | * |
| 70 | * @param table table where this object resides |
| 71 | * @param key Key or ID of this object |
| 72 | * @param namespace namespace where this object resides |
| 73 | */ |
| 74 | public KVObject(final IKVTable table, final byte[] key, final String namespace) { |
| 75 | this(table, key, null, table.getVersionNonexistant(), namespace); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame^] | 78 | /** |
| 79 | * KVObject constructor for specified namespace. |
| 80 | * |
| 81 | * @param table table where this object resides |
| 82 | * @param key Key or ID of this object |
| 83 | * @param value Value blob representation of this object |
| 84 | * @param version version of this Value blob |
| 85 | * @param namespace namespace where this object resides |
| 86 | */ |
| 87 | public KVObject(final IKVTable table, final byte[] key, |
| 88 | final byte[] value, final long version, |
| 89 | final String namespace) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 90 | if (table == null) { |
| 91 | throw new IllegalArgumentException("table cannot be null"); |
| 92 | } |
| 93 | if (key == null) { |
| 94 | throw new IllegalArgumentException("key cannot be null"); |
| 95 | } |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame^] | 96 | this.namespace = namespace; |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 97 | this.table = table; |
Yuta HIGUCHI | ce7e7f8 | 2014-04-15 21:37:38 -0700 | [diff] [blame] | 98 | this.key = key.clone(); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 99 | this.version = version; |
| 100 | this.propertyMap = new HashMap<Object, Object>(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 101 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 102 | if (value != null) { |
| 103 | deserialize(value); |
| 104 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame^] | 107 | /** |
| 108 | * Gets the namespace which this object reside. |
| 109 | * |
| 110 | * @return the namespace which this object reside |
| 111 | */ |
| 112 | public String getNamespace() { |
| 113 | return namespace; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame^] | 116 | /** |
| 117 | * Gets the table where this object resides. |
| 118 | * |
| 119 | * @return table where this object resides |
| 120 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 121 | public IKVTable getTable() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 122 | return table; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame^] | 125 | /** |
| 126 | * Gets the table ID of the table where this object resides. |
| 127 | * |
| 128 | * @return the table ID of the table where this object resides |
| 129 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 130 | public IKVTableID getTableId() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 131 | return table.getTableId(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame^] | 134 | /** |
| 135 | * Gets the Key or ID of this object. |
| 136 | * |
| 137 | * @return Key or ID of this object |
| 138 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 139 | public byte[] getKey() { |
Yuta HIGUCHI | ce7e7f8 | 2014-04-15 21:37:38 -0700 | [diff] [blame] | 140 | return key.clone(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame^] | 143 | /** |
| 144 | * Gets the version of this objects value blob. |
| 145 | * |
| 146 | * @return version of this objects value blob |
| 147 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 148 | public long getVersion() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 149 | return version; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Return user-defined object properties. |
| 154 | * |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 155 | * @return Will return null, if never been set, or was not deserialized |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 156 | * @note Will not trigger deserialization |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 157 | */ |
| 158 | protected Map<Object, Object> getPropertyMap() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 159 | return this.propertyMap; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame^] | 162 | /** |
| 163 | * Replaces the user-defined object properties. |
| 164 | * |
| 165 | * @param newMap new user-defined object properties |
| 166 | * @return old user-defined object properties |
| 167 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 168 | protected Map<Object, Object> replacePropertyMap(final Map<Object, Object> newMap) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 169 | Map<Object, Object> oldMap = this.propertyMap; |
| 170 | this.propertyMap = newMap; |
| 171 | return oldMap; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Serialize object. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 176 | * <p/> |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 177 | * sub-classes should override this method to customize serialization. |
| 178 | * |
| 179 | * @return serialized byte array |
| 180 | */ |
| 181 | public byte[] serialize() { |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 182 | return serializePropertyMap(DEFAULT_KRYO.get(), this.propertyMap); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame^] | 185 | /** |
| 186 | * Serialize user-defined object properties. |
| 187 | * |
| 188 | * @param kryo {@link Kryo} instance to use for serialization |
| 189 | * @param propMap user-defined object properties |
| 190 | * @return serialized byte array |
| 191 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 192 | protected byte[] serializePropertyMap(final Kryo kryo, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 193 | final Map<Object, Object> propMap) { |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 194 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 195 | // value |
| 196 | byte[] rcTemp = new byte[1024 * 1024]; |
| 197 | Output output = new Output(rcTemp); |
| 198 | kryo.writeObject(output, propMap); |
| 199 | return output.toBytes(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | |
| 203 | /** |
| 204 | * Deserialize using value and version stored in data store. |
| 205 | * |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 206 | * @param bytes serialized bytes |
Ray Milkey | 5df613b | 2014-04-15 10:50:56 -0700 | [diff] [blame] | 207 | * @param dataStoreVersion version of this {@code bytes} |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 208 | * @return true if success |
| 209 | */ |
Ray Milkey | 5df613b | 2014-04-15 10:50:56 -0700 | [diff] [blame] | 210 | public boolean deserialize(final byte[] bytes, |
| 211 | final long dataStoreVersion) { |
| 212 | version = dataStoreVersion; |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 213 | return deserialize(bytes); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Deserialize object. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 218 | * <p/> |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 219 | * sub-classes should override this method to customize deserialization. |
| 220 | * |
| 221 | * @param bytes serialized byte array |
| 222 | * @return true if success |
| 223 | */ |
| 224 | protected boolean deserialize(final byte[] bytes) { |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 225 | deserializePropertyMap(DEFAULT_KRYO.get(), bytes); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 226 | return true; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Deserialize and set {@link propertyMap}. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 231 | * |
| 232 | * @param kryo serializer to use |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 233 | * @param bytes Kryo serialized Map object |
| 234 | * @return true if success |
| 235 | */ |
| 236 | protected boolean deserializePropertyMap(final Kryo kryo, final byte[] bytes) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 237 | @SuppressWarnings("unchecked") |
| 238 | Map<Object, Object> map = deserializePropertyMap(kryo, bytes, HashMap.class); |
| 239 | if (map == null) { |
| 240 | map = new HashMap<>(); |
| 241 | } |
| 242 | this.propertyMap = map; |
| 243 | return true; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame^] | 246 | /** |
| 247 | * Deserialize user-defined object properties. |
| 248 | * |
| 249 | * @param kryo {@link Kryo} instance to use for deserialization |
| 250 | * @param bytes serialized byte array |
| 251 | * @param type Type of user-defined object properties |
| 252 | * @return user-defined object properties |
| 253 | */ |
| 254 | protected static <T extends Map<?, ?>> T deserializePropertyMap(final Kryo kryo, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 255 | final byte[] bytes, final Class<T> type) { |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 256 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 257 | if (bytes == null || bytes.length == 0) { |
| 258 | return null; |
| 259 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 260 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 261 | Input input = new Input(bytes); |
| 262 | T map = kryo.readObject(input, type); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 263 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 264 | return map; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | |
| 268 | /** |
| 269 | * Create an Object in DataStore. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 270 | * <p/> |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 271 | * Fails if the Object with same key already exists. |
| 272 | * |
| 273 | * @throws ObjectExistsException |
| 274 | */ |
| 275 | public void create() throws ObjectExistsException { |
| 276 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 277 | if (this.propertyMap == null) { |
| 278 | log.warn("No object map was set. Setting empty Map."); |
| 279 | replacePropertyMap(new HashMap<Object, Object>()); |
| 280 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 281 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 282 | this.version = table.create(key, this.serialize()); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | public void forceCreate() { |
| 286 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 287 | if (this.propertyMap == null) { |
| 288 | log.warn("No object map was set. Setting empty Map."); |
| 289 | replacePropertyMap(new HashMap<Object, Object>()); |
| 290 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 291 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 292 | this.version = table.forceCreate(key, this.serialize()); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Read an Object from DataStore. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 297 | * <p/> |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 298 | * Fails if the Object with the key does not exist. |
| 299 | * |
| 300 | * @throws ObjectDoesntExistException |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 301 | */ |
| 302 | public void read() throws ObjectDoesntExistException { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 303 | IKVEntry e = table.read(key); |
| 304 | deserialize(e.getValue(), e.getVersion()); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Update an existing Object in DataStore checking versions. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 309 | * <p/> |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 310 | * Fails if the Object with key does not exists, or conditional failure. |
| 311 | * |
| 312 | * @throws WrongVersionException |
| 313 | * @throws ObjectDoesntExistException |
| 314 | */ |
| 315 | public void update() throws ObjectDoesntExistException, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 316 | WrongVersionException { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 317 | if (this.propertyMap == null) { |
| 318 | replacePropertyMap(new HashMap<Object, Object>()); |
| 319 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 320 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 321 | this.version = table.update(key, this.serialize(), version); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | /** |
| 325 | * Remove an existing Object in DataStore. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 326 | * <p/> |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 327 | * Fails if the Object with key does not exists. |
| 328 | * |
| 329 | * @throws ObjectDoesntExistException |
| 330 | * @throws WrongVersionException |
| 331 | */ |
| 332 | public void delete() throws ObjectDoesntExistException, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 333 | WrongVersionException { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 334 | this.version = table.delete(key, this.version); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | public void forceDelete() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 338 | this.version = table.forceDelete(key); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | public WriteOp forceCreateOp(IKVClient client) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 342 | return new WriteOp(client.forceCreateOp(getTableId(), getKey(), serialize()), this); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | public WriteOp createOp(IKVClient client) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 346 | return new WriteOp(client.createOp(getTableId(), getKey(), serialize()), this); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | // this might not be needed? |
| 350 | public WriteOp readOp(IKVClient client) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 351 | return new WriteOp(client.readOp(getTableId(), getKey()), this); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | public WriteOp updateOp(IKVClient client) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 355 | return new WriteOp(client.updateOp(getTableId(), getKey(), serialize(), getVersion()), this); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | public WriteOp deleteOp(IKVClient client) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 359 | return new WriteOp(client.deleteOp(getTableId(), getKey(), serialize(), getVersion()), this); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | public WriteOp forceDeleteOp(IKVClient client) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 363 | return new WriteOp(client.forceDeleteOp(getTableId(), getKey()), this); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | /** |
| 367 | * Multi-read RCObjects. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 368 | * <p/> |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 369 | * If the blob value was read successfully, RCObject will deserialize them. |
| 370 | * |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 371 | * @param objects RCObjects to read |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 372 | * @return true if there exist a failed read. |
| 373 | */ |
| 374 | public static boolean multiRead(final List<? extends KVObject> objects) { |
| 375 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 376 | final IKVClient client = DataStoreClient.getClient(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 377 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 378 | final ArrayList<IMultiEntryOperation> readOps = new ArrayList<>(objects.size()); |
| 379 | for (KVObject o : objects) { |
| 380 | readOps.add(o.readOp(client)); |
| 381 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 382 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 383 | boolean failExists = client.multiRead(readOps); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 384 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 385 | for (int i = 0; i < readOps.size(); ++i) { |
| 386 | KVObject obj = objects.get(i); |
| 387 | IMultiEntryOperation entry = readOps.get(i); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 388 | if (entry.hasSucceeded()) { |
| 389 | if (!obj.deserialize(entry.getValue(), entry.getVersion())) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 390 | //deserialize return true on success |
| 391 | failExists = true; |
| 392 | log.error("MultiRead error, failed to deserialize {}, {}", obj.getTable(), obj); |
| 393 | } |
| 394 | } else { |
| 395 | log.error("MultiRead error, skipping {}, {}", obj.getTable(), obj); |
Ray Milkey | 7531a34 | 2014-04-11 15:08:12 -0700 | [diff] [blame] | 396 | obj.version = obj.getTable().getVersionNonexistant(); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 397 | failExists = true; |
| 398 | } |
| 399 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 400 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 401 | return failExists; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | /** |
Ray Milkey | b41100a | 2014-04-10 10:42:15 -0700 | [diff] [blame] | 405 | * TODO Extract common interface. |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 406 | */ |
| 407 | public static class WriteOp implements IMultiObjectOperation, IModifiableMultiEntryOperation { |
| 408 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 409 | private final IModifiableMultiEntryOperation base; |
| 410 | private final KVObject obj; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 411 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 412 | public WriteOp(IMultiEntryOperation base, final KVObject obj) { |
| 413 | this.base = (IModifiableMultiEntryOperation) base; |
| 414 | this.obj = obj; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 415 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 416 | // switch (base.getOperation()) { |
| 417 | // case CREATE: |
| 418 | // case FORCE_CREATE: |
| 419 | // case UPDATE: |
| 420 | // break; |
| 421 | // default: |
| 422 | // throw new UnsupportedOperationException("Unexpected OPERATION:"+base.getOperation()); |
| 423 | // } |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 424 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 425 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 426 | @Override |
| 427 | public KVObject getObject() { |
| 428 | return obj; |
| 429 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 430 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 431 | @Override |
| 432 | public boolean hasSucceeded() { |
| 433 | return base.hasSucceeded(); |
| 434 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 435 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 436 | @Override |
| 437 | public STATUS getStatus() { |
| 438 | return base.getStatus(); |
| 439 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 440 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 441 | @Override |
| 442 | public IKVTableID getTableId() { |
| 443 | return base.getTableId(); |
| 444 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 445 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 446 | @Override |
| 447 | public byte[] getKey() { |
| 448 | return base.getKey(); |
| 449 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 450 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 451 | @Override |
| 452 | public byte[] getValue() { |
| 453 | return base.getValue(); |
| 454 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 455 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 456 | @Override |
| 457 | public long getVersion() { |
| 458 | return base.getVersion(); |
| 459 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 460 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 461 | @Override |
| 462 | public OPERATION getOperation() { |
| 463 | return base.getOperation(); |
| 464 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 465 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 466 | @Override |
| 467 | public void setStatus(STATUS status) { |
| 468 | base.setStatus(status); |
| 469 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 470 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 471 | @Override |
| 472 | public void setValue(byte[] value, long version) { |
| 473 | base.setValue(value, version); |
| 474 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 475 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 476 | @Override |
| 477 | public void setVersion(long version) { |
| 478 | base.setVersion(version); |
| 479 | this.obj.version = version; |
| 480 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 481 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 482 | @Override |
| 483 | public IModifiableMultiEntryOperation getActualOperation() { |
| 484 | return base; |
| 485 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | public static boolean multiWrite(final List<WriteOp> objects) { |
| 489 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 490 | final IKVClient client = DataStoreClient.getClient(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 491 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 492 | final ArrayList<IMultiEntryOperation> writeOps = new ArrayList<>(objects.size()); |
| 493 | for (WriteOp o : objects) { |
| 494 | writeOps.add(o); |
| 495 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 496 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 497 | return client.multiWrite(writeOps); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | public abstract static class AbstractObjectIterator<E extends KVObject> implements |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 501 | Iterator<E> { |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 502 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 503 | protected Iterator<IKVEntry> enumerator; |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame^] | 504 | protected final String namespace; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 505 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame^] | 506 | @Deprecated |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 507 | public AbstractObjectIterator(final IKVTable table) { |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame^] | 508 | this(table, DEFAULT_NAMESPACE); |
| 509 | } |
| 510 | |
| 511 | public AbstractObjectIterator(final IKVTable table, final String namespace) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 512 | this.enumerator = table.getAllEntries().iterator(); |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame^] | 513 | this.namespace = namespace; |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 514 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 515 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 516 | @Override |
| 517 | public boolean hasNext() { |
| 518 | return enumerator.hasNext(); |
| 519 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 520 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 521 | // Implement something similar to below to realize Iterator |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 522 | // @Override |
| 523 | // public E next() { |
| 524 | // IKVTable.IKVEntry o = enumerator.next(); |
| 525 | // E obj = E.createFromKey(o.getKey()); |
| 526 | // obj.deserialize(o.getValue(), o.getVersion()); |
| 527 | // return obj; |
| 528 | // } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 529 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame^] | 530 | /** |
| 531 | * Not implemented. |
| 532 | */ |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 533 | @Deprecated |
| 534 | @Override |
| 535 | public void remove() { |
| 536 | // TODO Not implemented, as I cannot find a use-case for it. |
| 537 | throw new UnsupportedOperationException("Not implemented yet"); |
| 538 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 539 | |
| 540 | } |
| 541 | |
| 542 | } |