-
Notifications
You must be signed in to change notification settings - Fork 270
/
filestore_msg.go
187 lines (158 loc) · 5.48 KB
/
filestore_msg.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package wkstore
import (
"fmt"
"sync"
"github.com/WuKongIM/WuKongIM/pkg/wklog"
"github.com/WuKongIM/WuKongIM/pkg/wkutil"
wkproto "github.com/WuKongIM/WuKongIMGoProto"
lru "github.com/hashicorp/golang-lru/v2"
)
var segmentCache *lru.Cache[string, *segment]
func init() {
var err error
segmentCache, err = lru.NewWithEvict(10000, func(key string, value *segment) {
value.release()
})
if err != nil {
panic(err)
}
}
type FileStoreForMsg struct {
cfg *StoreConfig
slotMap map[uint32]*slot
wklog.Log
slotMapLock sync.RWMutex
}
func NewFileStoreForMsg(cfg *StoreConfig) *FileStoreForMsg {
f := &FileStoreForMsg{
cfg: cfg,
slotMap: make(map[uint32]*slot),
Log: wklog.NewWKLog("FileStoreForMsg"),
}
return f
}
func (f *FileStoreForMsg) AppendMessages(channelID string, channelType uint8, msgs []Message) (seqs []uint32, err error) {
seqs, _, err = f.getTopic(channelID, channelType).appendMessages(msgs)
return
}
func (f *FileStoreForMsg) AppendMessagesOfUser(uid string, msgs []Message) (seqs []uint32, err error) {
seqs, _, err = f.getTopic(fmt.Sprintf("%s%s", UserQueuePrefix, uid), wkproto.ChannelTypePerson).appendMessages(msgs)
return
}
func (f *FileStore) SaveStreamMeta(meta *StreamMeta) error {
return f.getTopic(meta.ChannelID, meta.ChannelType).saveStreamMeta(meta)
}
func (f *FileStoreForMsg) LoadMsg(channelID string, channelType uint8, messageSeq uint32) (Message, error) {
return f.getTopic(channelID, channelType).readMessageAt(messageSeq)
}
func (f *FileStoreForMsg) LoadLastMsgs(channelID string, channelType uint8, limit int) ([]Message, error) {
var messages = make([]Message, 0, limit)
tp := f.getTopic(channelID, channelType)
err := tp.readLastMessages(uint64(limit), func(message Message) error {
messages = append(messages, message)
return nil
})
if err != nil {
return nil, err
}
return messages, nil
}
func (f *FileStoreForMsg) LoadLastMsgsWithEnd(channelID string, channelType uint8, endMessageSeq uint32, limit int) ([]Message, error) {
var messages = make([]Message, 0, limit)
tp := f.getTopic(channelID, channelType)
err := tp.readLastMessages(uint64(limit), func(message Message) error {
if endMessageSeq != 0 && message.GetSeq() <= endMessageSeq {
return nil
}
messages = append(messages, message)
return nil
})
if err != nil {
return nil, err
}
return messages, nil
}
func (f *FileStoreForMsg) GetLastMsgSeq(channelID string, channelType uint8) (uint32, error) {
return f.getTopic(channelID, channelType).getLastMsgSeq(), nil
}
func (f *FileStoreForMsg) LoadPrevRangeMsgs(channelID string, channelType uint8, startMessageSeq, endMessageSeq uint32, limit int) ([]Message, error) {
if startMessageSeq == 0 {
return nil, fmt.Errorf("start messageSeq must be greater than 0")
}
actLimit := limit
var actStartMessageSeq uint32
if startMessageSeq < uint32(limit) {
actLimit = int(startMessageSeq)
actStartMessageSeq = 0
} else {
actStartMessageSeq = startMessageSeq - uint32(limit) + 1
}
tp := f.getTopic(channelID, channelType)
var messages = make([]Message, 0, limit)
err := tp.readMessages(actStartMessageSeq, uint64(actLimit), func(message Message) error {
if endMessageSeq != 0 && message.GetSeq() <= endMessageSeq {
return nil
}
messages = append(messages, message)
return nil
})
return messages, err
}
func (f *FileStoreForMsg) LoadNextRangeMsgs(channelID string, channelType uint8, startMessageSeq, endMessageSeq uint32, limit int) ([]Message, error) {
var messages = make([]Message, 0, limit)
tp := f.getTopic(channelID, channelType)
err := tp.readMessages(startMessageSeq, uint64(limit), func(message Message) error {
if endMessageSeq != 0 && message.GetSeq() >= endMessageSeq {
return nil
}
messages = append(messages, message)
return nil
})
return messages, err
}
func (f *FileStoreForMsg) DeleteChannelAndClearMessages(channelID string, channelType uint8) error {
f.Warn("暂未实现DeleteChannelAndClearMessages")
return nil
}
func (f *FileStoreForMsg) SaveStreamMeta(meta *StreamMeta) error {
return f.getTopic(meta.ChannelID, meta.ChannelType).saveStreamMeta(meta)
}
func (f *FileStoreForMsg) GetStreamMeta(channelID string, channelType uint8, streamNo string) (*StreamMeta, error) {
tp := f.getTopic(channelID, channelType)
return tp.readStreamMeta(streamNo)
}
func (f *FileStoreForMsg) AppendStreamItem(channelID string, channelType uint8, streamNo string, item *StreamItem) (uint32, error) {
return f.getTopic(channelID, channelType).appendStreamItem(streamNo, item)
}
func (f *FileStoreForMsg) GetStreamItems(channelID string, channelType uint8, streamNo string) ([]*StreamItem, error) {
return f.getTopic(channelID, channelType).readItems(streamNo)
}
func (f *FileStoreForMsg) StreamEnd(channelID string, channelType uint8, streamNo string) error {
return f.getTopic(channelID, channelType).streamEnd(streamNo)
}
func (f *FileStoreForMsg) Close() error {
if len(f.slotMap) == 0 {
return nil
}
for _, s := range f.slotMap {
s.close()
}
return nil
}
func (f *FileStoreForMsg) topicName(channelID string, channelType uint8) string {
return fmt.Sprintf("%d-%s", channelType, channelID)
}
func (f *FileStoreForMsg) getTopic(channelID string, channelType uint8) *topic {
topic := f.topicName(channelID, channelType)
slotNum := wkutil.GetSlotNum(f.cfg.SlotNum, topic)
f.slotMapLock.RLock()
slot := f.slotMap[slotNum]
f.slotMapLock.RUnlock()
if slot == nil {
slot = newSlot(slotNum, f.cfg)
f.slotMapLock.Lock()
f.slotMap[slotNum] = slot
f.slotMapLock.Unlock()
}
return slot.getTopic(topic)
}