Skip to content

Commit

Permalink
fixed add/remove of duplicate elements (use udid instead of time)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nexen23 committed Aug 26, 2015
1 parent fbf8b34 commit 33dcd48
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 21 deletions.
19 changes: 13 additions & 6 deletions app/I_am_here/cloud/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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";

Expand All @@ -48,7 +54,7 @@ function DeleteDeadSessions() {
.each(function(obj)
{
Log(obj, "Delete dead session");
obj.destroy();
DeleteSession(obj);
}
);
return promise;
Expand Down Expand Up @@ -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();} );
};

Expand All @@ -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
Expand Down
6 changes: 1 addition & 5 deletions app/src/main/java/alex/imhere/util/TemporaryElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand All @@ -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;
Expand Down
53 changes: 43 additions & 10 deletions app/src/main/java/alex/imhere/util/TemporarySet.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -49,11 +50,9 @@ private synchronized void _clear() {

private synchronized boolean _add(TemporaryElement<T> 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();
Expand All @@ -66,16 +65,14 @@ private synchronized boolean _add(TemporaryElement<T> isertingElement) {
notifyCollectionChanged(Notification.ADD, isertingElement.object);
}

return wasAdded;
return wasInserted;
}

private synchronized boolean _remove(TemporaryElement<T> 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();
Expand All @@ -84,7 +81,7 @@ private synchronized boolean _remove(TemporaryElement<T> deletingElement) {
notifyCollectionChanged(Notification.REMOVE, deletingElement.object);
}

return wasRemoved;
return wasDeleted;
}

private synchronized void openNextDeath() {
Expand Down Expand Up @@ -115,4 +112,40 @@ private synchronized void cancelNextDeath() {
nextElementToDie = null;
timerTask = null;
}

private synchronized Iterator<TemporaryElement<T>> findElement(TemporaryElement<T> searchingElement) {
Iterator<TemporaryElement<T>> resultIterator = null;
for (Iterator<TemporaryElement<T>> iterator = sortedElementsSet.iterator(); iterator.hasNext() && resultIterator == null;) {
if (iterator.next().equals(searchingElement)) {
resultIterator = iterator;
}
}
return resultIterator;
}

private synchronized boolean _insertElementUnique(TemporaryElement<T> element) {
boolean wasInserted = false;

Iterator<TemporaryElement<T>> iterator = findElement(element);
if (iterator == null) {
wasInserted = true;
sortedElementsSet.add(element);
list.add(element.object);
}

return wasInserted;
}

private synchronized boolean _deleteElementByObject(TemporaryElement<T> element) {
boolean wasDeleted = false;

Iterator<TemporaryElement<T>> iterator = findElement(element);
if (iterator != null) {
wasDeleted = true;
iterator.remove();
list.remove(element.object);
}

return wasDeleted;
}
}

0 comments on commit 33dcd48

Please sign in to comment.