From 33dcd480c0b32981cfe9e7015ecb129b6d67b210 Mon Sep 17 00:00:00 2001 From: Nexen23 Date: Wed, 26 Aug 2015 15:23:36 +0700 Subject: [PATCH] fixed add/remove of duplicate elements (use udid instead of time) --- app/I_am_here/cloud/main.js | 19 ++++--- .../alex/imhere/util/TemporaryElement.java | 6 +-- .../java/alex/imhere/util/TemporarySet.java | 53 +++++++++++++++---- 3 files changed, 57 insertions(+), 21 deletions(-) diff --git a/app/I_am_here/cloud/main.js b/app/I_am_here/cloud/main.js index 950f544..d0da06b 100644 --- a/app/I_am_here/cloud/main.js +++ b/app/I_am_here/cloud/main.js @@ -6,7 +6,7 @@ var moment = require('moment'); // jshint ignore:line // Constants var sessionObjName = "IMH_Session"; -var sessionLifetimeSec = 10; +var sessionLifetimeSec = 13; var channelName = "events"; var publishKey = "pub-c-6271f363-519a-432d-9059-e65a7203ce0e", @@ -40,6 +40,12 @@ var errorHandler = function(error) { Log(error.message, "error"); }; +function DeleteSession(obj) { + obj.set("loginedAt", obj.get("aliveTo")); + SendEvent(obj); + obj.destroy(); +} + function DeleteDeadSessions() { "use strict"; @@ -48,7 +54,7 @@ function DeleteDeadSessions() { .each(function(obj) { Log(obj, "Delete dead session"); - obj.destroy(); + DeleteSession(obj); } ); return promise; @@ -199,7 +205,7 @@ var API_Logout = function(request, response) { query.each( function(obj) { Log(obj, "Logout:destroy"); - obj.destroy(); + DeleteSession(obj); }).done( function() {response.success();} ); }; @@ -211,12 +217,13 @@ Parse.Cloud.afterSave(sessionObjName, function(request) { // jshint ignore:line SendEvent(request.object); }); -Parse.Cloud.afterDelete(sessionObjName, function(request) { // jshint ignore:line +/*Parse.Cloud.afterDelete(sessionObjName, function(request) { // jshint ignore:line "use strict"; - request.object.set("aliveTo", request.object.get("loginedAt")); + //request.object.set("aliveTo", request.object.get("loginedAt")); + request.object.set("loginedAt", request.object.get("aliveTo")); SendEvent(request.object); -}); +});*/ // API definitions diff --git a/app/src/main/java/alex/imhere/util/TemporaryElement.java b/app/src/main/java/alex/imhere/util/TemporaryElement.java index ae0e0e0..2a0ed94 100644 --- a/app/src/main/java/alex/imhere/util/TemporaryElement.java +++ b/app/src/main/java/alex/imhere/util/TemporaryElement.java @@ -13,7 +13,7 @@ public TemporaryElement(@NonNull T object, @NonNull LocalDateTime deathTime) { this.object = object; } - protected TemporaryElement(@NonNull T object) { + public TemporaryElement(@NonNull T object) { this(object, new LocalDateTime(0)); } @@ -38,10 +38,6 @@ public int compareTo(Object another) { TemporaryElement a = this, b = (TemporaryElement) another; - if (a.object.equals(b.object)) { - return 0; - } - int datesComparisionResult = a.deathTime.compareTo(b.deathTime); int objectsComparisionResult = a.hashCode() - b.hashCode(); return (datesComparisionResult != 0) ? datesComparisionResult : objectsComparisionResult; diff --git a/app/src/main/java/alex/imhere/util/TemporarySet.java b/app/src/main/java/alex/imhere/util/TemporarySet.java index 40241b5..3b97de2 100644 --- a/app/src/main/java/alex/imhere/util/TemporarySet.java +++ b/app/src/main/java/alex/imhere/util/TemporarySet.java @@ -5,6 +5,7 @@ import java.util.ArrayList; import java.util.Collections; +import java.util.Iterator; import java.util.List; import java.util.SortedSet; import java.util.Timer; @@ -49,11 +50,9 @@ private synchronized void _clear() { private synchronized boolean _add(TemporaryElement isertingElement) { // TODO: 18.08.2015 implement unique add to List - boolean wasAdded = sortedElementsSet.add(isertingElement); - - if (wasAdded) { - list.add(0, isertingElement.object); + boolean wasInserted = _insertElementUnique(isertingElement); + if (wasInserted) { if (nextElementToDie != null && nextElementToDie.deathTime.isAfter(isertingElement.deathTime)) { cancelNextDeath(); @@ -66,16 +65,14 @@ private synchronized boolean _add(TemporaryElement isertingElement) { notifyCollectionChanged(Notification.ADD, isertingElement.object); } - return wasAdded; + return wasInserted; } private synchronized boolean _remove(TemporaryElement deletingElement) { // TODO: 18.08.2015 implement unique add to List - boolean wasRemoved = sortedElementsSet.remove(deletingElement); - - if (wasRemoved) { - list.remove(deletingElement.object); + boolean wasDeleted = _deleteElementByObject(deletingElement); + if (wasDeleted) { if (nextElementToDie.equals(deletingElement)) { cancelNextDeath(); openNextDeath(); @@ -84,7 +81,7 @@ private synchronized boolean _remove(TemporaryElement deletingElement) { notifyCollectionChanged(Notification.REMOVE, deletingElement.object); } - return wasRemoved; + return wasDeleted; } private synchronized void openNextDeath() { @@ -115,4 +112,40 @@ private synchronized void cancelNextDeath() { nextElementToDie = null; timerTask = null; } + + private synchronized Iterator> findElement(TemporaryElement searchingElement) { + Iterator> resultIterator = null; + for (Iterator> iterator = sortedElementsSet.iterator(); iterator.hasNext() && resultIterator == null;) { + if (iterator.next().equals(searchingElement)) { + resultIterator = iterator; + } + } + return resultIterator; + } + + private synchronized boolean _insertElementUnique(TemporaryElement element) { + boolean wasInserted = false; + + Iterator> iterator = findElement(element); + if (iterator == null) { + wasInserted = true; + sortedElementsSet.add(element); + list.add(element.object); + } + + return wasInserted; + } + + private synchronized boolean _deleteElementByObject(TemporaryElement element) { + boolean wasDeleted = false; + + Iterator> iterator = findElement(element); + if (iterator != null) { + wasDeleted = true; + iterator.remove(); + list.remove(element.object); + } + + return wasDeleted; + } }