Commits

Zemian Deng  committed 7f335fc

Sort EventHistory list by createTime then by id fields.

  • Participants
  • Parent commits 8878cb8

Comments (0)

Files changed (2)

File timemachine-hibernate/src/main/java/timemachine/scheduler/hibernate/HibernateDataStore.java

 			@Override
 			public Object onSession(Session session) {
 				String query = "select e from EventHistory e where e.schedulerNodeId = :schedulerNodeId " + 
-						" order by e.createTime desc, e.type asc, e.name asc";
+						" order by e.createTime desc, e.id desc, e.type asc, e.name asc";
 				List<?> list = session.createQuery(query)
 						.setParameter("schedulerNodeId", schedulerNodeId).list();
 				return list;
 			public Object onSession(Session session) {
 				String query = "select e from EventHistory e where e.schedulerNodeId = :schedulerNodeId " + 
 						" and e.createTime between :fromTime and :toTime " +
-						" order by e.createTime desc, e.type asc, e.name asc";
+						" order by e.createTime desc, e.id desc, e.type asc, e.name asc";
 				List<?> list = session.createQuery(query)
 						.setParameter("schedulerNodeId", schedulerNodeId)
 						.setParameter("fromTime", fromTime)
 			@Override
 			public void onSession(Session session) {
 				String query = "delete EventHistory e where e.schedulerNodeId = :schedulerNodeId " + 
-						" and e.createTime < :olderThanTime " +
-						" order by e.createTime desc, e.type asc, e.name asc";
+						" and e.createTime < :olderThanTime";
 				int result = session.createQuery(query)
 						.setParameter("schedulerNodeId", schedulerNodeId)
 						.setParameter("olderThanTime", olderThanTime).executeUpdate();

File timemachine-scheduler/src/main/java/timemachine/scheduler/service/MemoryDataStore.java

 		SchedulerNodeStore nodeStore = getSchedulerNodeStore(schedulerNodeId);
 		result.addAll(nodeStore.eventHistories.values());
 		// Sort the events
-		Collections.sort(result, new Comparator<EventHistory>() {
-			@Override
-			public int compare(EventHistory o1, EventHistory o2) {
-				return o2.getCreateTime().compareTo(o1.getCreateTime());
-			}
-		});
+		Collections.sort(result, new EventHistoryComparator());
 		return result;
 	}
 	@Override
 				result.add(eh);
 		}
 		// Sort the events
-		Collections.sort(result, new Comparator<EventHistory>() {
-			@Override
-			public int compare(EventHistory o1, EventHistory o2) {
-				return o2.getCreateTime().compareTo(o1.getCreateTime());
-			}
-		});
+		Collections.sort(result, new EventHistoryComparator());
 		return result;
 	}
 	@Override
 			nodeStore.eventHistories.remove(eh.getId());
 		}
 	}
+
+	/** Comparator for sorting EventHistory in descending order by createTime then id. */
+	public static class EventHistoryComparator implements Comparator<EventHistory> {
+
+		@Override
+		public int compare(EventHistory o1, EventHistory o2) {
+			int result = o2.getCreateTime().compareTo(o1.getCreateTime());
+			if (result == 0) {
+				result = o2.getId().compareTo(o1.getId());
+			}
+			return result;
+		}
+		
+	}
 }