Skip to content

Commit

Permalink
Support listeners in KvinPartitioned.
Browse files Browse the repository at this point in the history
  • Loading branch information
kenwenzel committed Sep 11, 2024
1 parent ca1b5ec commit affb9d2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private void scheduleCyclicArchival() {
public boolean addListener(KvinListener listener) {
try {
listeners.add(listener);
return true;
return hotStore.addListener(listener);
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand All @@ -138,7 +138,7 @@ public boolean addListener(KvinListener listener) {
public boolean removeListener(KvinListener listener) {
try {
listeners.remove(listener);
return true;
return hotStore.removeListener(listener);
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand All @@ -164,6 +164,10 @@ public void createNewHotDataStore() throws IOException {
FileUtils.deleteDirectory(this.currentStoreArchivePath);
FileUtils.moveDirectory(this.currentStorePath, this.currentStoreArchivePath);
hotStore = new KvinLevelDb(currentStorePath);
for (KvinListener listener : listeners) {
// register listeners on new hot store
hotStore.addListener(listener);
}
hotStoreArchive = new KvinLevelDb(this.currentStoreArchivePath);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,11 @@ class KvinLevelDb(path: File) extends KvinLevelDbBase with Kvin {
}

override def put(entries: java.lang.Iterable[KvinTuple]): Unit = {
var notifyTuples = Option.empty[mutable.ArrayBuffer[KvinTuple]]
if (! this.listeners.isEmpty && entries.isInstanceOf[IExtendedIterator[_]]) {
notifyTuples = Some(new mutable.ArrayBuffer[KvinTuple]())
}

val idsBatch = ids.createWriteBatch()
val batch = values.createWriteBatch()
activeWrites.incrementAndGet()
Expand All @@ -614,6 +619,8 @@ class KvinLevelDb(path: File) extends KvinLevelDbBase with Kvin {
// remove timed-out entries
ttl(entry.item) map (asyncRemoveByTtl(values, prefix, _))
}
// buffer tuples if entries are given via iterator
notifyTuples.foreach(_.addOne(entry))
}
var writeIds: Future[_] = null
if (idsBatch.size() > 0) {
Expand All @@ -632,7 +639,7 @@ class KvinLevelDb(path: File) extends KvinLevelDbBase with Kvin {
uriToIdCacheWrite.invalidateAll()
}
}
entries.asScala.foreach { entry =>
notifyTuples.getOrElse(entries.asScala).foreach { entry =>
for (l <- listeners.asScala) l.valueAdded(entry.item, entry.property, entry.context, entry.time, entry.seqNr, entry.value)
}
}
Expand Down

0 comments on commit affb9d2

Please sign in to comment.