mirror of
https://github.com/logos-messaging/go-libp2p-pubsub.git
synced 2026-01-02 12:53:09 +00:00
implement mcache
This commit is contained in:
parent
07875f149e
commit
8fbc4e1c70
44
mcache.go
44
mcache.go
@ -5,25 +5,57 @@ import (
|
||||
)
|
||||
|
||||
func NewMessageCache(gossip, history int) *MessageCache {
|
||||
return &MessageCache{}
|
||||
return &MessageCache{
|
||||
msgs: make(map[string]*pb.Message),
|
||||
history: make([][]CacheEntry, history),
|
||||
gossip: gossip,
|
||||
}
|
||||
}
|
||||
|
||||
type MessageCache struct {
|
||||
msgs map[string]*pb.Message
|
||||
history [][]CacheEntry
|
||||
gossip int
|
||||
}
|
||||
|
||||
type CacheEntry struct {
|
||||
mid string
|
||||
topics []string
|
||||
}
|
||||
|
||||
func (mc *MessageCache) Put(msg *pb.Message) {
|
||||
// TODO
|
||||
mid := msgID(msg)
|
||||
mc.msgs[mid] = msg
|
||||
mc.history[0] = append(mc.history[0], CacheEntry{mid: mid, topics: msg.GetTopicIDs()})
|
||||
}
|
||||
|
||||
func (mc *MessageCache) Get(mid string) (*pb.Message, bool) {
|
||||
// TODO
|
||||
return nil, false
|
||||
m, ok := mc.msgs[mid]
|
||||
return m, ok
|
||||
}
|
||||
|
||||
func (mc *MessageCache) GetGossipIDs(topic string) []string {
|
||||
return nil
|
||||
var mids []string
|
||||
for _, entries := range mc.history[:mc.gossip] {
|
||||
for _, entry := range entries {
|
||||
for _, t := range entry.topics {
|
||||
if t == topic {
|
||||
mids = append(mids, entry.mid)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return mids
|
||||
}
|
||||
|
||||
func (mc *MessageCache) Shift() {
|
||||
// TODO
|
||||
last := mc.history[len(mc.history)-1]
|
||||
for _, entry := range last {
|
||||
delete(mc.msgs, entry.mid)
|
||||
}
|
||||
for i := len(mc.history) - 2; i >= 0; i-- {
|
||||
mc.history[i+1] = mc.history[i]
|
||||
}
|
||||
mc.history[0] = nil
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user