@Override public void setSingle(String name, Object value) { put(name, (Serializable) value); }
@Override public void setArray(String name, Object[] value) { put(name, value); }
@Override public State readPartialState(String id, Collection<String> keys) { if (id == null) { return null; } State state = states.get(id); if (state != null) { if (keys != null && !keys.isEmpty()) { State partialState = new State(); for (String key : keys) { Serializable value = state.get(key); if (value != null) { partialState.put(key, value); } } state = partialState; } if (log.isTraceEnabled()) { log.trace("Mem: READ " + id + ": " + state); } } return state; }
/** * Makes a deep copy of a {@link State} map, optionally thread-safe. * * @param threadSafe if {@code true}, then thread-safe datastructures are used */ public static State deepCopy(State state, boolean threadSafe) { State copy = new State(state.size(), threadSafe); for (Entry<String, Serializable> en : state.entrySet()) { copy.put(en.getKey(), deepCopy(en.getValue(), threadSafe)); } return copy; }
@Override public synchronized Lock setLock(String id, Lock lock) { State state = states.get(id); if (state == null) { // document not found throw new DocumentNotFoundException(id); } String owner = (String) state.get(KEY_LOCK_OWNER); if (owner != null) { // return old lock Calendar created = (Calendar) state.get(KEY_LOCK_CREATED); return new Lock(owner, created); } state.put(KEY_LOCK_OWNER, lock.getOwner()); state.put(KEY_LOCK_CREATED, lock.getCreated()); return null; }
/** * Changes the deltas stored into actual full values. * * @since 6.0 */ public static void resetDeltas(State state) { if (Boolean.parseBoolean(Framework.getProperty(DISABLED_DELTA_PROP, "false"))) { return; } for (Entry<String, Serializable> en : state.entrySet()) { Serializable value = en.getValue(); if (value instanceof State) { resetDeltas((State) value); } else if (value instanceof Delta) { state.put(en.getKey(), ((Delta) value).getFullValue()); } } }
@Override public synchronized Lock removeLock(String id, String owner) { State state = states.get(id); if (state == null) { // document not found throw new DocumentNotFoundException(id); } String oldOwner = (String) state.get(KEY_LOCK_OWNER); if (oldOwner == null) { // no previous lock return null; } Calendar oldCreated = (Calendar) state.get(KEY_LOCK_CREATED); if (!LockManager.canLockBeRemoved(oldOwner, owner)) { // existing mismatched lock, flag failure return new Lock(oldOwner, oldCreated, true); } // remove lock state.put(KEY_LOCK_OWNER, null); state.put(KEY_LOCK_CREATED, null); // return old lock return new Lock(oldOwner, oldCreated); }
if (old == null) { old = new State(true); // thread-safe state.put(key, old); state.put(key, applyDiff(state.get(key), (ListDiff) value)); } else if (value instanceof Delta) { Delta delta = (Delta) value; newValue = delta.add(oldValue); state.put(key, newValue); } else { state.put(key, StateHelper.deepCopy(value, true)); // thread-safe
public State bsonToState(Document doc) { if (doc == null) { return null; } State state = new State(doc.keySet().size()); for (String key : doc.keySet()) { if (idKey == null && MONGODB_ID.equals(key)) { // skip native id if it's not mapped to something continue; } state.put(bsonToKey(key), bsonToValue(doc.get(key))); } return state; }