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 | |
| 47 | private final IKVTable table; |
| 48 | private final byte[] key; |
| 49 | |
| 50 | /** |
| 51 | * serialized value version stored on data store or |
| 52 | * {@link IKVTable.VERSION_NONEXISTENT()} if is a new object. |
| 53 | */ |
| 54 | private long version; |
| 55 | |
| 56 | /** |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 57 | * Map to store user-defined properties |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 58 | */ |
| 59 | private Map<Object, Object> propertyMap; |
| 60 | |
| 61 | public KVObject(final IKVTable table, final byte[] key) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 62 | this(table, key, null, table.VERSION_NONEXISTENT()); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | public KVObject(final IKVTable table, final byte[] key, final byte[] value, final long version) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 66 | if (table == null) { |
| 67 | throw new IllegalArgumentException("table cannot be null"); |
| 68 | } |
| 69 | if (key == null) { |
| 70 | throw new IllegalArgumentException("key cannot be null"); |
| 71 | } |
| 72 | this.table = table; |
| 73 | this.key = key; |
| 74 | this.version = version; |
| 75 | this.propertyMap = new HashMap<Object, Object>(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 76 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 77 | if (value != null) { |
| 78 | deserialize(value); |
| 79 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | protected static KVObject createFromKey(final byte[] key) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 83 | // Equivalent of this method is expected to be implemented by SubClasses |
| 84 | throw new UnsupportedOperationException( |
| 85 | "createFromKey() is not expected to be called for RCObject"); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | public IKVTable getTable() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 89 | return table; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | public IKVTableID getTableId() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 93 | return table.getTableId(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | public byte[] getKey() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 97 | return key; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | public long getVersion() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 101 | return version; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Return user-defined object properties. |
| 106 | * |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 107 | * @return Will return null, if never been set, or was not deserialized |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 108 | * @note Will not trigger deserialization |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 109 | */ |
| 110 | protected Map<Object, Object> getPropertyMap() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 111 | return this.propertyMap; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | protected Map<Object, Object> replacePropertyMap(final Map<Object, Object> newMap) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 115 | Map<Object, Object> oldMap = this.propertyMap; |
| 116 | this.propertyMap = newMap; |
| 117 | return oldMap; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Serialize object. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 122 | * <p/> |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 123 | * sub-classes should override this method to customize serialization. |
| 124 | * |
| 125 | * @return serialized byte array |
| 126 | */ |
| 127 | public byte[] serialize() { |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 128 | return serializePropertyMap(DEFAULT_KRYO.get(), this.propertyMap); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | protected byte[] serializePropertyMap(final Kryo kryo, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 132 | final Map<Object, Object> propMap) { |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 133 | |
| 134 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 135 | // value |
| 136 | byte[] rcTemp = new byte[1024 * 1024]; |
| 137 | Output output = new Output(rcTemp); |
| 138 | kryo.writeObject(output, propMap); |
| 139 | return output.toBytes(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | |
| 143 | /** |
| 144 | * Deserialize using value and version stored in data store. |
| 145 | * |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 146 | * @param bytes serialized bytes |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 147 | * @param version version of this {@code bytes} |
| 148 | * @return true if success |
| 149 | */ |
| 150 | public boolean deserialize(final byte[] bytes, final long version) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 151 | this.version = version; |
| 152 | return deserialize(bytes); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Deserialize object. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 157 | * <p/> |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 158 | * sub-classes should override this method to customize deserialization. |
| 159 | * |
| 160 | * @param bytes serialized byte array |
| 161 | * @return true if success |
| 162 | */ |
| 163 | protected boolean deserialize(final byte[] bytes) { |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 164 | deserializePropertyMap(DEFAULT_KRYO.get(), bytes); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 165 | return true; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Deserialize and set {@link propertyMap}. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 170 | * |
| 171 | * @param kryo serializer to use |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 172 | * @param bytes Kryo serialized Map object |
| 173 | * @return true if success |
| 174 | */ |
| 175 | protected boolean deserializePropertyMap(final Kryo kryo, final byte[] bytes) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 176 | @SuppressWarnings("unchecked") |
| 177 | Map<Object, Object> map = deserializePropertyMap(kryo, bytes, HashMap.class); |
| 178 | if (map == null) { |
| 179 | map = new HashMap<>(); |
| 180 | } |
| 181 | this.propertyMap = map; |
| 182 | return true; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | protected <T extends Map<?, ?>> T deserializePropertyMap(final Kryo kryo, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 186 | final byte[] bytes, final Class<T> type) { |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 187 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 188 | if (bytes == null || bytes.length == 0) { |
| 189 | return null; |
| 190 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 191 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 192 | Input input = new Input(bytes); |
| 193 | T map = kryo.readObject(input, type); |
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 | return map; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | |
| 199 | /** |
| 200 | * Create an Object in DataStore. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 201 | * <p/> |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 202 | * Fails if the Object with same key already exists. |
| 203 | * |
| 204 | * @throws ObjectExistsException |
| 205 | */ |
| 206 | public void create() throws ObjectExistsException { |
| 207 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 208 | if (this.propertyMap == null) { |
| 209 | log.warn("No object map was set. Setting empty Map."); |
| 210 | replacePropertyMap(new HashMap<Object, Object>()); |
| 211 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 212 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 213 | this.version = table.create(key, this.serialize()); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | public void forceCreate() { |
| 217 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 218 | if (this.propertyMap == null) { |
| 219 | log.warn("No object map was set. Setting empty Map."); |
| 220 | replacePropertyMap(new HashMap<Object, Object>()); |
| 221 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 222 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 223 | this.version = table.forceCreate(key, this.serialize()); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Read an Object from DataStore. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 228 | * <p/> |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 229 | * Fails if the Object with the key does not exist. |
| 230 | * |
| 231 | * @throws ObjectDoesntExistException |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 232 | */ |
| 233 | public void read() throws ObjectDoesntExistException { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 234 | IKVEntry e = table.read(key); |
| 235 | deserialize(e.getValue(), e.getVersion()); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Update an existing Object in DataStore checking versions. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 240 | * <p/> |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 241 | * Fails if the Object with key does not exists, or conditional failure. |
| 242 | * |
| 243 | * @throws WrongVersionException |
| 244 | * @throws ObjectDoesntExistException |
| 245 | */ |
| 246 | public void update() throws ObjectDoesntExistException, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 247 | WrongVersionException { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 248 | if (this.propertyMap == null) { |
| 249 | replacePropertyMap(new HashMap<Object, Object>()); |
| 250 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 251 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 252 | this.version = table.update(key, this.serialize(), version); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Remove an existing Object in DataStore. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 257 | * <p/> |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 258 | * Fails if the Object with key does not exists. |
| 259 | * |
| 260 | * @throws ObjectDoesntExistException |
| 261 | * @throws WrongVersionException |
| 262 | */ |
| 263 | public void delete() throws ObjectDoesntExistException, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 264 | WrongVersionException { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 265 | this.version = table.delete(key, this.version); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | public void forceDelete() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 269 | this.version = table.forceDelete(key); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | public WriteOp forceCreateOp(IKVClient client) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 273 | return new WriteOp(client.forceCreateOp(getTableId(), getKey(), serialize()), this); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | public WriteOp createOp(IKVClient client) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 277 | return new WriteOp(client.createOp(getTableId(), getKey(), serialize()), this); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | // this might not be needed? |
| 281 | public WriteOp readOp(IKVClient client) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 282 | return new WriteOp(client.readOp(getTableId(), getKey()), this); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | public WriteOp updateOp(IKVClient client) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 286 | return new WriteOp(client.updateOp(getTableId(), getKey(), serialize(), getVersion()), this); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | public WriteOp deleteOp(IKVClient client) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 290 | return new WriteOp(client.deleteOp(getTableId(), getKey(), serialize(), getVersion()), this); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | public WriteOp forceDeleteOp(IKVClient client) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 294 | return new WriteOp(client.forceDeleteOp(getTableId(), getKey()), this); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Multi-read RCObjects. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 299 | * <p/> |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 300 | * If the blob value was read successfully, RCObject will deserialize them. |
| 301 | * |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 302 | * @param objects RCObjects to read |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 303 | * @return true if there exist a failed read. |
| 304 | */ |
| 305 | public static boolean multiRead(final List<? extends KVObject> objects) { |
| 306 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 307 | final IKVClient client = DataStoreClient.getClient(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 308 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 309 | final ArrayList<IMultiEntryOperation> readOps = new ArrayList<>(objects.size()); |
| 310 | for (KVObject o : objects) { |
| 311 | readOps.add(o.readOp(client)); |
| 312 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 313 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 314 | boolean failExists = client.multiRead(readOps); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 315 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 316 | for (int i = 0; i < readOps.size(); ++i) { |
| 317 | KVObject obj = objects.get(i); |
| 318 | IMultiEntryOperation entry = readOps.get(i); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 319 | if (entry.hasSucceeded()) { |
| 320 | if (!obj.deserialize(entry.getValue(), entry.getVersion())) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 321 | //deserialize return true on success |
| 322 | failExists = true; |
| 323 | log.error("MultiRead error, failed to deserialize {}, {}", obj.getTable(), obj); |
| 324 | } |
| 325 | } else { |
| 326 | log.error("MultiRead error, skipping {}, {}", obj.getTable(), obj); |
| 327 | obj.version = obj.getTable().VERSION_NONEXISTENT(); |
| 328 | failExists = true; |
| 329 | } |
| 330 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 331 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 332 | return failExists; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | /** |
| 336 | * TODO Extract common interface |
| 337 | */ |
| 338 | public static class WriteOp implements IMultiObjectOperation, IModifiableMultiEntryOperation { |
| 339 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 340 | private final IModifiableMultiEntryOperation base; |
| 341 | private final KVObject obj; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 342 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 343 | public WriteOp(IMultiEntryOperation base, final KVObject obj) { |
| 344 | this.base = (IModifiableMultiEntryOperation) base; |
| 345 | this.obj = obj; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 346 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 347 | // switch (base.getOperation()) { |
| 348 | // case CREATE: |
| 349 | // case FORCE_CREATE: |
| 350 | // case UPDATE: |
| 351 | // break; |
| 352 | // default: |
| 353 | // throw new UnsupportedOperationException("Unexpected OPERATION:"+base.getOperation()); |
| 354 | // } |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 355 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 356 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 357 | @Override |
| 358 | public KVObject getObject() { |
| 359 | return obj; |
| 360 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 361 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 362 | @Deprecated |
| 363 | public OPERATION getOp() { |
| 364 | return this.getOperation(); |
| 365 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 366 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 367 | @Override |
| 368 | public boolean hasSucceeded() { |
| 369 | return base.hasSucceeded(); |
| 370 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 371 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 372 | @Override |
| 373 | public STATUS getStatus() { |
| 374 | return base.getStatus(); |
| 375 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 376 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 377 | @Override |
| 378 | public IKVTableID getTableId() { |
| 379 | return base.getTableId(); |
| 380 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 381 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 382 | @Override |
| 383 | public byte[] getKey() { |
| 384 | return base.getKey(); |
| 385 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 386 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 387 | @Override |
| 388 | public byte[] getValue() { |
| 389 | return base.getValue(); |
| 390 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 391 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 392 | @Override |
| 393 | public long getVersion() { |
| 394 | return base.getVersion(); |
| 395 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 396 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 397 | @Override |
| 398 | public OPERATION getOperation() { |
| 399 | return base.getOperation(); |
| 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 | @Override |
| 403 | public void setStatus(STATUS status) { |
| 404 | base.setStatus(status); |
| 405 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 406 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 407 | @Override |
| 408 | public void setValue(byte[] value, long version) { |
| 409 | base.setValue(value, version); |
| 410 | } |
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 | @Override |
| 413 | public void setVersion(long version) { |
| 414 | base.setVersion(version); |
| 415 | this.obj.version = version; |
| 416 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 417 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 418 | @Override |
| 419 | public IModifiableMultiEntryOperation getActualOperation() { |
| 420 | return base; |
| 421 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | public static boolean multiWrite(final List<WriteOp> objects) { |
| 425 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 426 | final IKVClient client = DataStoreClient.getClient(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 427 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 428 | final ArrayList<IMultiEntryOperation> writeOps = new ArrayList<>(objects.size()); |
| 429 | for (WriteOp o : objects) { |
| 430 | writeOps.add(o); |
| 431 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 432 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 433 | return client.multiWrite(writeOps); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | public abstract static class AbstractObjectIterator<E extends KVObject> implements |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 437 | Iterator<E> { |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 438 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 439 | protected Iterator<IKVEntry> enumerator; |
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 | public AbstractObjectIterator(final IKVTable table) { |
| 442 | this.enumerator = table.getAllEntries().iterator(); |
| 443 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 444 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 445 | @Override |
| 446 | public boolean hasNext() { |
| 447 | return enumerator.hasNext(); |
| 448 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 449 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 450 | // Implement something similar to below to realize Iterator |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 451 | // @Override |
| 452 | // public E next() { |
| 453 | // IKVTable.IKVEntry o = enumerator.next(); |
| 454 | // E obj = E.createFromKey(o.getKey()); |
| 455 | // obj.deserialize(o.getValue(), o.getVersion()); |
| 456 | // return obj; |
| 457 | // } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 458 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 459 | @Deprecated |
| 460 | @Override |
| 461 | public void remove() { |
| 462 | // TODO Not implemented, as I cannot find a use-case for it. |
| 463 | throw new UnsupportedOperationException("Not implemented yet"); |
| 464 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 465 | |
| 466 | } |
| 467 | |
| 468 | } |