WIP: Dirty in-place implementation
authorKai Moritz <kai@juplo.de>
Thu, 14 Oct 2021 18:39:22 +0000 (20:39 +0200)
committerKai Moritz <kai@juplo.de>
Thu, 14 Oct 2021 18:39:22 +0000 (20:39 +0200)
src/main/java/de/juplo/kafka/wordcount/top10/Entry.java [deleted file]
src/main/java/de/juplo/kafka/wordcount/top10/Key.java [deleted file]
src/main/java/de/juplo/kafka/wordcount/top10/Ranking.java [deleted file]
src/main/java/de/juplo/kafka/wordcount/top10/Top10StreamProcessor.java

diff --git a/src/main/java/de/juplo/kafka/wordcount/top10/Entry.java b/src/main/java/de/juplo/kafka/wordcount/top10/Entry.java
deleted file mode 100644 (file)
index 67f45f2..0000000
+++ /dev/null
@@ -1,11 +0,0 @@
-package de.juplo.kafka.wordcount.top10;
-
-import lombok.Value;
-
-
-@Value(staticConstructor = "of")
-public class Entry
-{
-  private final String word;
-  private final Long count;
-}
diff --git a/src/main/java/de/juplo/kafka/wordcount/top10/Key.java b/src/main/java/de/juplo/kafka/wordcount/top10/Key.java
deleted file mode 100644 (file)
index d09dbcc..0000000
+++ /dev/null
@@ -1,13 +0,0 @@
-package de.juplo.kafka.wordcount.top10;
-
-import lombok.Getter;
-import lombok.Setter;
-
-
-@Getter
-@Setter
-public class Key
-{
-  private String username;
-  private String word;
-}
diff --git a/src/main/java/de/juplo/kafka/wordcount/top10/Ranking.java b/src/main/java/de/juplo/kafka/wordcount/top10/Ranking.java
deleted file mode 100644 (file)
index b748fe5..0000000
+++ /dev/null
@@ -1,53 +0,0 @@
-package de.juplo.kafka.wordcount.top10;
-
-import lombok.Getter;
-import lombok.Setter;
-
-import java.util.Arrays;
-import java.util.LinkedList;
-import java.util.List;
-
-
-@Getter
-@Setter
-public class Ranking
-{
-  private Entry[] entries = new Entry[0];
-
-  public void add(Entry newEntry)
-  {
-    if (entries.length == 0)
-    {
-      entries = new Entry[1];
-      entries[0] = newEntry;
-      return;
-    }
-
-    List<Entry> list = new LinkedList<>(Arrays.asList(entries));
-    for (int i = 0; i < list.size(); i++)
-    {
-      Entry entry;
-
-      entry = list.get(i);
-      if (entry.getCount() <= newEntry.getCount())
-      {
-        list.add(i, newEntry);
-        for (int j = i+1; j < list.size(); j++)
-        {
-          entry = list.get(j);
-          if(entry.getWord().equals(newEntry.getWord()))
-          {
-            list.remove(j);
-            break;
-          }
-        }
-        if (list.size() > 10)
-        {
-          list = list.subList(0,10);
-        }
-        entries = list.toArray(num -> new Entry[num]);
-        return;
-      }
-    }
-  }
-}
index 811cf98..2bc1da7 100644 (file)
@@ -21,7 +21,9 @@ import org.springframework.stereotype.Component;
 
 import javax.annotation.PostConstruct;
 import javax.annotation.PreDestroy;
+import java.util.Arrays;
 import java.util.LinkedList;
+import java.util.List;
 import java.util.Properties;
 import java.util.concurrent.CompletableFuture;
 import java.util.regex.Pattern;
@@ -53,8 +55,39 @@ public class Top10StreamProcessor
                                .groupByKey(Grouped.keySerde(Serdes.String()))
                                .aggregate(
                                                () -> Ranking.newBuilder().setEntries(new LinkedList<Entry>()).build(),
-                                               (username, entry, ranking) -> {
-                                                       ranking.getEntries().add(entry);
+                                               (username, newEntry, ranking) -> {
+                                                       List<Entry> entries = new LinkedList<>(ranking.getEntries());
+
+                                                       if (entries.isEmpty())
+                                                       {
+                                                               entries.add(newEntry);
+                                                       }
+                                                       else
+                                                       {
+                                                               for (int i = 0; i < entries.size(); i++)
+                                                               {
+                                                                       Entry entry = entries.get(i);
+                                                                       if (entry.getCount() <= newEntry.getCount())
+                                                                       {
+                                                                               entries.add(i, newEntry);
+                                                                               for (int j = i + 1; j < entries.size(); j++)
+                                                                               {
+                                                                                       entry = entries.get(j);
+                                                                                       if (entry.getWord().equals(newEntry.getWord()))
+                                                                                       {
+                                                                                               entries.remove(j);
+                                                                                               break;
+                                                                                       }
+                                                                               }
+                                                                               if (entries.size() > 10)
+                                                                               {
+                                                                                       entries = entries.subList(0, 10);
+                                                                               }
+                                                                       }
+                                                               }
+                                                       }
+
+                                                       ranking.setEntries(entries);
                                                        return ranking;
                                                })
                                .toStream()