protected static void getLocalPointersIn(Object container, ArrayList<JSONObject> localPointers) throws JSONException { if (container instanceof JSONObject) { JSONObject object = (JSONObject) container; if ("Pointer".equals(object.opt("__type")) && object.has("localId")) { localPointers.add((JSONObject) container); return; } Iterator<String> keyIterator = object.keys(); while (keyIterator.hasNext()) { String key = keyIterator.next(); getLocalPointersIn(object.get(key), localPointers); } } if (container instanceof JSONArray) { JSONArray array = (JSONArray) container; for (int i = 0; i < array.length(); ++i) { getLocalPointersIn(array.get(i), localPointers); } } }
public void resolveLocalIds() { try { ArrayList<JSONObject> localPointers = new ArrayList<>(); getLocalPointersIn(jsonParameters, localPointers); for (JSONObject pointer : localPointers) { String localId = (String) pointer.get("localId"); String objectId = getLocalIdManager().getObjectId(localId); if (objectId == null) { throw new IllegalStateException( "Tried to serialize a command referencing a new, unsaved object."); } pointer.put("objectId", objectId); pointer.remove("localId"); } maybeChangeServerOperation(); } catch (JSONException e) { // Well, nothing to do here... } }
/** * Finds all of the local ids in this command and decrements their retain counts in the on-disk * store. This should be called when removing a serialized command from the disk, when we know * that we will never need to resolve these local ids for this command again in the future. */ public void releaseLocalIds() { if (localId != null) { getLocalIdManager().releaseLocalIdOnDisk(localId); } try { ArrayList<JSONObject> localPointers = new ArrayList<>(); getLocalPointersIn(jsonParameters, localPointers); for (JSONObject pointer : localPointers) { String localId = (String) pointer.get("localId"); getLocalIdManager().releaseLocalIdOnDisk(localId); } } catch (JSONException e) { // Well, nothing to do here... } }
/** * Finds all of the local ids in this command and increments their retain counts in the on-disk * store. This should be called immediately before serializing the command to disk, so that we * know we might need to resolve these local ids at some point in the future. */ public void retainLocalIds() { if (localId != null) { getLocalIdManager().retainLocalIdOnDisk(localId); } try { ArrayList<JSONObject> localPointers = new ArrayList<>(); getLocalPointersIn(jsonParameters, localPointers); for (JSONObject pointer : localPointers) { String localId = (String) pointer.get("localId"); getLocalIdManager().retainLocalIdOnDisk(localId); } } catch (JSONException e) { // Well, nothing to do here... } }