I am searching for a data structure that works similar to a Apache Kafka topic with one partition:
If there is not suitable existing structure I will implement it myself but I would prefer to use something existing.
我认为没有这样的现有数据结构,您必须编写自定义逻辑以在一定时间后删除元素,根据我的理解,您可以使用任何列表系列的集合,而无需进行任何自定义。
I was able to find a suitable structure based on ConcurrentNavigableMap:
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentNavigableMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.atomic.AtomicLong;
public class Journal<T> {
private static final long INITIAL_OFFSET = 0;
private AtomicLong nextOffset = new AtomicLong();
private ConcurrentNavigableMap<Long, T> messages = new ConcurrentSkipListMap<>();
public long append(T message) {
Long offset = nextOffset.getAndIncrement();
messages.put(offset, message);
return offset;
}
public long getFirstOffset() {
Iterator<Long> it = messages.keySet().iterator();
return it.hasNext() ? it.next() : INITIAL_OFFSET;
}
public Entry<Long, T> getNext(long offset) {
return this.messages.higherEntry(offset);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.