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. |
Jonathan Hart | 99ff20a | 2014-06-15 16:53:00 -0700 | [diff] [blame] | 154 | * <p/> |
| 155 | * Note: Will not trigger deserialization. |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 156 | * |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 157 | * @return Will return null, if never been set, or was not deserialized |
| 158 | */ |
| 159 | protected Map<Object, Object> getPropertyMap() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 160 | return this.propertyMap; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 163 | /** |
| 164 | * Replaces the user-defined object properties. |
| 165 | * |
| 166 | * @param newMap new user-defined object properties |
| 167 | * @return old user-defined object properties |
| 168 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 169 | protected Map<Object, Object> replacePropertyMap(final Map<Object, Object> newMap) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 170 | Map<Object, Object> oldMap = this.propertyMap; |
| 171 | this.propertyMap = newMap; |
| 172 | return oldMap; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Serialize object. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 177 | * <p/> |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 178 | * sub-classes should override this method to customize serialization. |
| 179 | * |
| 180 | * @return serialized byte array |
| 181 | */ |
| 182 | public byte[] serialize() { |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 183 | return serializePropertyMap(DEFAULT_KRYO.get(), this.propertyMap); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 186 | /** |
| 187 | * Serialize user-defined object properties. |
| 188 | * |
| 189 | * @param kryo {@link Kryo} instance to use for serialization |
| 190 | * @param propMap user-defined object properties |
| 191 | * @return serialized byte array |
| 192 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 193 | protected byte[] serializePropertyMap(final Kryo kryo, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 194 | final Map<Object, Object> propMap) { |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 195 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 196 | // value |
| 197 | byte[] rcTemp = new byte[1024 * 1024]; |
| 198 | Output output = new Output(rcTemp); |
| 199 | kryo.writeObject(output, propMap); |
| 200 | return output.toBytes(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | |
| 204 | /** |
| 205 | * Deserialize using value and version stored in data store. |
| 206 | * |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 207 | * @param bytes serialized bytes |
Ray Milkey | 5df613b | 2014-04-15 10:50:56 -0700 | [diff] [blame] | 208 | * @param dataStoreVersion version of this {@code bytes} |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 209 | * @return true if success |
| 210 | */ |
Ray Milkey | 5df613b | 2014-04-15 10:50:56 -0700 | [diff] [blame] | 211 | public boolean deserialize(final byte[] bytes, |
| 212 | final long dataStoreVersion) { |
| 213 | version = dataStoreVersion; |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 214 | return deserialize(bytes); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Deserialize object. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 219 | * <p/> |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 220 | * sub-classes should override this method to customize deserialization. |
| 221 | * |
| 222 | * @param bytes serialized byte array |
| 223 | * @return true if success |
| 224 | */ |
| 225 | protected boolean deserialize(final byte[] bytes) { |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 226 | deserializePropertyMap(DEFAULT_KRYO.get(), bytes); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 227 | return true; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | /** |
Jonathan Hart | 99ff20a | 2014-06-15 16:53:00 -0700 | [diff] [blame] | 231 | * Deserialize and set {@link #propertyMap}. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 232 | * |
| 233 | * @param kryo serializer to use |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 234 | * @param bytes Kryo serialized Map object |
| 235 | * @return true if success |
| 236 | */ |
| 237 | protected boolean deserializePropertyMap(final Kryo kryo, final byte[] bytes) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 238 | @SuppressWarnings("unchecked") |
| 239 | Map<Object, Object> map = deserializePropertyMap(kryo, bytes, HashMap.class); |
| 240 | if (map == null) { |
| 241 | map = new HashMap<>(); |
| 242 | } |
| 243 | this.propertyMap = map; |
| 244 | return true; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 247 | /** |
| 248 | * Deserialize user-defined object properties. |
| 249 | * |
| 250 | * @param kryo {@link Kryo} instance to use for deserialization |
| 251 | * @param bytes serialized byte array |
| 252 | * @param type Type of user-defined object properties |
| 253 | * @return user-defined object properties |
| 254 | */ |
| 255 | protected static <T extends Map<?, ?>> T deserializePropertyMap(final Kryo kryo, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 256 | final byte[] bytes, final Class<T> type) { |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 257 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 258 | if (bytes == null || bytes.length == 0) { |
| 259 | return null; |
| 260 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 261 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 262 | Input input = new Input(bytes); |
| 263 | T map = kryo.readObject(input, type); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 264 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 265 | return map; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | |
| 269 | /** |
| 270 | * Create an Object in DataStore. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 271 | * <p/> |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 272 | * Fails if the Object with same key already exists. |
| 273 | * |
| 274 | * @throws ObjectExistsException |
| 275 | */ |
| 276 | public void create() throws ObjectExistsException { |
| 277 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 278 | if (this.propertyMap == null) { |
| 279 | log.warn("No object map was set. Setting empty Map."); |
| 280 | replacePropertyMap(new HashMap<Object, Object>()); |
| 281 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 282 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 283 | this.version = table.create(key, this.serialize()); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | public void forceCreate() { |
| 287 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 288 | if (this.propertyMap == null) { |
| 289 | log.warn("No object map was set. Setting empty Map."); |
| 290 | replacePropertyMap(new HashMap<Object, Object>()); |
| 291 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 292 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 293 | this.version = table.forceCreate(key, this.serialize()); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Read an Object from DataStore. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 298 | * <p/> |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 299 | * Fails if the Object with the key does not exist. |
| 300 | * |
| 301 | * @throws ObjectDoesntExistException |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 302 | */ |
| 303 | public void read() throws ObjectDoesntExistException { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 304 | IKVEntry e = table.read(key); |
| 305 | deserialize(e.getValue(), e.getVersion()); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Update an existing Object in DataStore checking versions. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 310 | * <p/> |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 311 | * Fails if the Object with key does not exists, or conditional failure. |
| 312 | * |
| 313 | * @throws WrongVersionException |
| 314 | * @throws ObjectDoesntExistException |
| 315 | */ |
| 316 | public void update() throws ObjectDoesntExistException, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 317 | WrongVersionException { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 318 | if (this.propertyMap == null) { |
| 319 | replacePropertyMap(new HashMap<Object, Object>()); |
| 320 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 321 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 322 | this.version = table.update(key, this.serialize(), version); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | /** |
| 326 | * Remove an existing Object in DataStore. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 327 | * <p/> |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 328 | * Fails if the Object with key does not exists. |
| 329 | * |
| 330 | * @throws ObjectDoesntExistException |
| 331 | * @throws WrongVersionException |
| 332 | */ |
| 333 | public void delete() throws ObjectDoesntExistException, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 334 | WrongVersionException { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 335 | this.version = table.delete(key, this.version); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | public void forceDelete() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 339 | this.version = table.forceDelete(key); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | public WriteOp forceCreateOp(IKVClient client) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 343 | return new WriteOp(client.forceCreateOp(getTableId(), getKey(), serialize()), this); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | public WriteOp createOp(IKVClient client) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 347 | return new WriteOp(client.createOp(getTableId(), getKey(), serialize()), this); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | // this might not be needed? |
| 351 | public WriteOp readOp(IKVClient client) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 352 | return new WriteOp(client.readOp(getTableId(), getKey()), this); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | public WriteOp updateOp(IKVClient client) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 356 | return new WriteOp(client.updateOp(getTableId(), getKey(), serialize(), getVersion()), this); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | public WriteOp deleteOp(IKVClient client) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 360 | return new WriteOp(client.deleteOp(getTableId(), getKey(), serialize(), getVersion()), this); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | public WriteOp forceDeleteOp(IKVClient client) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 364 | return new WriteOp(client.forceDeleteOp(getTableId(), getKey()), this); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | /** |
| 368 | * Multi-read RCObjects. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 369 | * <p/> |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 370 | * If the blob value was read successfully, RCObject will deserialize them. |
| 371 | * |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 372 | * @param objects RCObjects to read |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 373 | * @return true if there exist a failed read. |
| 374 | */ |
| 375 | public static boolean multiRead(final List<? extends KVObject> objects) { |
| 376 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 377 | final IKVClient client = DataStoreClient.getClient(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 378 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 379 | final ArrayList<IMultiEntryOperation> readOps = new ArrayList<>(objects.size()); |
| 380 | for (KVObject o : objects) { |
| 381 | readOps.add(o.readOp(client)); |
| 382 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 383 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 384 | boolean failExists = client.multiRead(readOps); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 385 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 386 | for (int i = 0; i < readOps.size(); ++i) { |
| 387 | KVObject obj = objects.get(i); |
| 388 | IMultiEntryOperation entry = readOps.get(i); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 389 | if (entry.hasSucceeded()) { |
| 390 | if (!obj.deserialize(entry.getValue(), entry.getVersion())) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 391 | //deserialize return true on success |
| 392 | failExists = true; |
| 393 | log.error("MultiRead error, failed to deserialize {}, {}", obj.getTable(), obj); |
| 394 | } |
| 395 | } else { |
| 396 | log.error("MultiRead error, skipping {}, {}", obj.getTable(), obj); |
Ray Milkey | 7531a34 | 2014-04-11 15:08:12 -0700 | [diff] [blame] | 397 | obj.version = obj.getTable().getVersionNonexistant(); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 398 | failExists = true; |
| 399 | } |
| 400 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 401 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 402 | return failExists; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | /** |
Ray Milkey | b41100a | 2014-04-10 10:42:15 -0700 | [diff] [blame] | 406 | * TODO Extract common interface. |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 407 | */ |
| 408 | public static class WriteOp implements IMultiObjectOperation, IModifiableMultiEntryOperation { |
| 409 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 410 | private final IModifiableMultiEntryOperation base; |
| 411 | private final KVObject obj; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 412 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 413 | public WriteOp(IMultiEntryOperation base, final KVObject obj) { |
| 414 | this.base = (IModifiableMultiEntryOperation) base; |
| 415 | this.obj = obj; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 416 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 417 | // switch (base.getOperation()) { |
| 418 | // case CREATE: |
| 419 | // case FORCE_CREATE: |
| 420 | // case UPDATE: |
| 421 | // break; |
| 422 | // default: |
| 423 | // throw new UnsupportedOperationException("Unexpected OPERATION:"+base.getOperation()); |
| 424 | // } |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 425 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 426 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 427 | @Override |
| 428 | public KVObject getObject() { |
| 429 | return obj; |
| 430 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 431 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 432 | @Override |
| 433 | public boolean hasSucceeded() { |
| 434 | return base.hasSucceeded(); |
| 435 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 436 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 437 | @Override |
| 438 | public STATUS getStatus() { |
| 439 | return base.getStatus(); |
| 440 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 441 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 442 | @Override |
| 443 | public IKVTableID getTableId() { |
| 444 | return base.getTableId(); |
| 445 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 446 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 447 | @Override |
| 448 | public byte[] getKey() { |
| 449 | return base.getKey(); |
| 450 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 451 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 452 | @Override |
| 453 | public byte[] getValue() { |
| 454 | return base.getValue(); |
| 455 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 456 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 457 | @Override |
| 458 | public long getVersion() { |
| 459 | return base.getVersion(); |
| 460 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 461 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 462 | @Override |
| 463 | public OPERATION getOperation() { |
| 464 | return base.getOperation(); |
| 465 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 466 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 467 | @Override |
| 468 | public void setStatus(STATUS status) { |
| 469 | base.setStatus(status); |
| 470 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 471 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 472 | @Override |
| 473 | public void setValue(byte[] value, long version) { |
| 474 | base.setValue(value, version); |
| 475 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 476 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 477 | @Override |
| 478 | public void setVersion(long version) { |
| 479 | base.setVersion(version); |
| 480 | this.obj.version = version; |
| 481 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 482 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 483 | @Override |
| 484 | public IModifiableMultiEntryOperation getActualOperation() { |
| 485 | return base; |
| 486 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | public static boolean multiWrite(final List<WriteOp> objects) { |
| 490 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 491 | final IKVClient client = DataStoreClient.getClient(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 492 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 493 | final ArrayList<IMultiEntryOperation> writeOps = new ArrayList<>(objects.size()); |
| 494 | for (WriteOp o : objects) { |
| 495 | writeOps.add(o); |
| 496 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 497 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 498 | return client.multiWrite(writeOps); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | public abstract static class AbstractObjectIterator<E extends KVObject> implements |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 502 | Iterator<E> { |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 503 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 504 | protected Iterator<IKVEntry> enumerator; |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 505 | protected final String namespace; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 506 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 507 | @Deprecated |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 508 | public AbstractObjectIterator(final IKVTable table) { |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 509 | this(table, DEFAULT_NAMESPACE); |
| 510 | } |
| 511 | |
| 512 | public AbstractObjectIterator(final IKVTable table, final String namespace) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 513 | this.enumerator = table.getAllEntries().iterator(); |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 514 | this.namespace = namespace; |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 515 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 516 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 517 | @Override |
| 518 | public boolean hasNext() { |
| 519 | return enumerator.hasNext(); |
| 520 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 521 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 522 | // Implement something similar to below to realize Iterator |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 523 | // @Override |
| 524 | // public E next() { |
| 525 | // IKVTable.IKVEntry o = enumerator.next(); |
| 526 | // E obj = E.createFromKey(o.getKey()); |
| 527 | // obj.deserialize(o.getValue(), o.getVersion()); |
| 528 | // return obj; |
| 529 | // } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 530 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 531 | /** |
| 532 | * Not implemented. |
| 533 | */ |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 534 | @Deprecated |
| 535 | @Override |
| 536 | public void remove() { |
| 537 | // TODO Not implemented, as I cannot find a use-case for it. |
| 538 | throw new UnsupportedOperationException("Not implemented yet"); |
| 539 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 540 | |
| 541 | } |
| 542 | |
| 543 | } |