static TreeSet<UUID> add(TreeSet<UUID> a, UUID uuid, boolean reversed, int limit) {

    if (a == null) {
      a = new TreeSet<UUID>(new UUIDComparator());
    }

    if (uuid == null) {
      return a;
    }

    // if we have less than the limit, just add it
    if (a.size() < limit) {
      a.add(uuid);
    } else if (reversed) {
      // if reversed, we want to add more recent messages
      // and eject the oldest
      if (UUIDComparator.staticCompare(uuid, a.first()) > 0) {
        a.pollFirst();
        a.add(uuid);
      }
    } else {
      // add older messages and eject the newset
      if (UUIDComparator.staticCompare(uuid, a.last()) < 0) {
        a.pollLast();
        a.add(uuid);
      }
    }

    return a;
  }
  @Test
  public void getReversedEntries() throws ConnectionException {

    final Id applicationId = new SimpleId("application");

    ApplicationScope context = new ApplicationScopeImpl(applicationId);

    final Id id = new SimpleId("test");

    int count = 10;

    final UUID[] versions = new UUID[count];
    final Stage COMPLETE = Stage.COMPLETE;
    final MvccLogEntry[] entries = new MvccLogEntry[count];

    for (int i = 0; i < count; i++) {
      versions[i] = UUIDGenerator.newTimeUUID();

      entries[i] = new MvccLogEntryImpl(id, versions[i], COMPLETE, MvccLogEntry.State.COMPLETE);
      logEntryStrategy.write(context, entries[i]).execute();

      // Read it back

      MvccLogEntry returned =
          logEntryStrategy.load(context, Collections.singleton(id), versions[i]).getMaxVersion(id);

      assertNotNull("Returned value should not be null", returned);

      assertEquals("Returned should equal the saved", entries[i], returned);
    }

    final UUID[] assertVersions = Arrays.copyOf(versions, versions.length);

    Arrays.sort(assertVersions, (v1, v2) -> UUIDComparator.staticCompare(v1, v2) * -1);

    // now do a range scan from the end

    final int half = count / 2;

    final List<MvccLogEntry> results =
        logEntryStrategy.loadReversed(context, id, versions[0], half);

    assertEquals(half, results.size());

    for (int i = 0; i < count / 2; i++) {
      final MvccLogEntry saved = entries[i];
      final MvccLogEntry returned = results.get(i);

      assertEquals("Entry was not equal to the saved value", saved, returned);
    }

    // now get the next batch
    final List<MvccLogEntry> results2 =
        logEntryStrategy.loadReversed(context, id, versions[half], count);

    assertEquals(half, results2.size());

    for (int i = 0; i < half; i++) {
      final MvccLogEntry saved = entries[half + i];
      final MvccLogEntry returned = results2.get(i);

      assertEquals("Entry was not equal to the saved value", saved, returned);
    }

    // now delete them all and ensure we get no results back
    for (int i = 0; i < count; i++) {
      logEntryStrategy.delete(context, id, versions[i]).execute();
    }

    final List<MvccLogEntry> results3 =
        logEntryStrategy.loadReversed(context, id, null, versions.length);

    assertEquals("All log entries were deleted", 0, results3.size());
  }