-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore.go
More file actions
344 lines (290 loc) · 9.21 KB
/
store.go
File metadata and controls
344 lines (290 loc) · 9.21 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package debugmonitor
import (
"container/list"
"sync"
)
// DataEntry represents a single data record with its ID.
type DataEntry struct {
Id int64 `json:"id"`
Payload any `json:"payload"`
}
// AddEvent represents a subscription to Add events.
// Use the C channel to receive notifications when new data is added.
// Call Close() when done to clean up resources.
type AddEvent struct {
C <-chan *DataEntry // Channel to receive Add events
store *Store
ch chan *DataEntry
closed bool
mu sync.Mutex
}
// Close unsubscribes from the Store and closes the event channel.
// After calling Close, the C channel will be closed and no more events will be received.
func (e *AddEvent) Close() {
e.mu.Lock()
defer e.mu.Unlock()
if e.closed {
return
}
e.closed = true
e.store.unsubscribeAdd(e)
close(e.ch)
}
// ClearEvent represents a subscription to Clear events.
// Use the C channel to receive notifications when the store is cleared.
// Call Close() when done to clean up resources.
type ClearEvent struct {
C <-chan struct{} // Channel to receive Clear events
store *Store
ch chan struct{}
closed bool
mu sync.Mutex
}
// Close unsubscribes from the Store and closes the event channel.
// After calling Close, the C channel will be closed and no more events will be received.
func (e *ClearEvent) Close() {
e.mu.Lock()
defer e.mu.Unlock()
if e.closed {
return
}
e.closed = true
e.store.unsubscribeClear(e)
close(e.ch)
}
// Store is an in-memory data store that provides O(1) access by ID
// while maintaining insertion order like a linked hash map.
// It automatically removes old records when the maximum capacity is reached.
// It uses Snowflake-style int64 IDs to guarantee uniqueness and ordering.
// Store supports channel-based event subscriptions for Add and Clear events.
type Store struct {
mu sync.RWMutex
maxRecords int
idGen *IDGenerator // Snowflake-style ID generator
entries map[int64]*list.Element // map for O(1) access by ID
order *list.List // doubly linked list to maintain insertion order
addEventsMu sync.RWMutex // protects addEvents slice
addEvents []*AddEvent // active Add event subscriptions
clearEventsMu sync.RWMutex // protects clearEvents slice
clearEvents []*ClearEvent // active Clear event subscriptions
}
// NewStore creates a new Store with the specified maximum number of records.
// When the limit is reached, the oldest records are automatically removed.
func NewStore(maxRecords int) *Store {
if maxRecords <= 0 {
maxRecords = 1000 // Default maximum
}
return &Store{
maxRecords: maxRecords,
idGen: NewIDGenerator(),
entries: make(map[int64]*list.Element),
order: list.New(),
addEvents: make([]*AddEvent, 0),
clearEvents: make([]*ClearEvent, 0),
}
}
// Add adds a new record to the store with a Snowflake-style int64 ID.
// The ID is generated using a time-based algorithm for uniqueness and ordering.
// If the store is at capacity, the oldest record is removed.
// After adding, all registered listeners are notified with the new entry.
func (s *Store) Add(payload any) {
s.mu.Lock()
// Generate Snowflake-style ID
id := s.idGen.Generate()
entry := &DataEntry{
Id: id,
Payload: payload,
}
// Add to the end of the list for O(1) insertion
element := s.order.PushBack(entry)
s.entries[entry.Id] = element
// Remove the oldest record if we exceed maxRecords
if s.order.Len() > s.maxRecords {
oldest := s.order.Front()
if oldest != nil {
oldEntry := oldest.Value.(*DataEntry)
delete(s.entries, oldEntry.Id)
s.order.Remove(oldest)
}
}
s.mu.Unlock()
// Notify add event subscribers outside the lock to prevent deadlocks
s.notifyAddEvents(entry)
}
// GetLatest returns all data entries in reverse chronological order (newest first).
func (s *Store) GetLatest() []*DataEntry {
s.mu.RLock()
defer s.mu.RUnlock()
result := make([]*DataEntry, 0, s.order.Len())
element := s.order.Back()
for element != nil {
entry := element.Value.(*DataEntry)
result = append(result, entry)
element = element.Prev()
}
return result
}
// GetLatestWithLimit returns the N most recent data entries in reverse chronological order (newest first).
// If n is greater than the number of records, all records are returned.
func (s *Store) GetLatestWithLimit(n int) []*DataEntry {
s.mu.RLock()
defer s.mu.RUnlock()
if n <= 0 {
return []*DataEntry{}
}
count := n
if count > s.order.Len() {
count = s.order.Len()
}
result := make([]*DataEntry, 0, count)
element := s.order.Back()
for i := 0; i < count && element != nil; i++ {
entry := element.Value.(*DataEntry)
result = append(result, entry)
element = element.Prev()
}
return result
}
// GetSince returns all data entries with ID greater than the specified ID,
// in chronological order (oldest first).
// This is optimized for cursor-based pagination in log streaming.
// Time complexity: O(m) where m is the number of results.
func (s *Store) GetSince(sinceID int64) []*DataEntry {
s.mu.RLock()
defer s.mu.RUnlock()
result := make([]*DataEntry, 0)
var startElement *list.Element
if sinceID == 0 {
// Start from the beginning if sinceID is 0
startElement = s.order.Front()
} else {
// Find the element with sinceID and start from the next one
if element, exists := s.entries[sinceID]; exists {
startElement = element.Next()
} else {
// If sinceID doesn't exist, find the first element with ID > sinceID
// This handles the case where sinceID was already removed from the store
for element := s.order.Front(); element != nil; element = element.Next() {
entry := element.Value.(*DataEntry)
if entry.Id > sinceID {
startElement = element
break
}
}
}
}
// Collect all records from startElement to the end
for element := startElement; element != nil; element = element.Next() {
entry := element.Value.(*DataEntry)
result = append(result, entry)
}
return result
}
// GetById returns a single data entry by its ID.
// Returns nil if the entry is not found.
// Time complexity: O(1).
func (s *Store) GetById(id int64) *DataEntry {
s.mu.RLock()
defer s.mu.RUnlock()
if element, exists := s.entries[id]; exists {
return element.Value.(*DataEntry)
}
return nil
}
// Len returns the current number of records in the store.
func (s *Store) Len() int {
s.mu.RLock()
defer s.mu.RUnlock()
return s.order.Len()
}
// Clear removes all records from the store.
// After clearing, all registered clear listeners are notified.
func (s *Store) Clear() {
s.mu.Lock()
s.idGen = NewIDGenerator()
s.entries = make(map[int64]*list.Element)
s.order.Init()
s.mu.Unlock()
// Notify clear event subscribers outside the lock to prevent deadlocks
s.notifyClearEvents()
}
// NewAddEvent creates a new subscription to Add events.
// The returned AddEvent provides a channel that will receive notifications
// when new data is added to the Store.
// Call Close() on the returned AddEvent when done to clean up resources.
func (s *Store) NewAddEvent() *AddEvent {
ch := make(chan *DataEntry, 10) // Buffered to prevent blocking
event := &AddEvent{
C: ch,
store: s,
ch: ch,
}
s.addEventsMu.Lock()
s.addEvents = append(s.addEvents, event)
s.addEventsMu.Unlock()
return event
}
// NewClearEvent creates a new subscription to Clear events.
// The returned ClearEvent provides a channel that will receive notifications
// when the Store is cleared.
// Call Close() on the returned ClearEvent when done to clean up resources.
func (s *Store) NewClearEvent() *ClearEvent {
ch := make(chan struct{}, 1) // Buffered to prevent blocking
event := &ClearEvent{
C: ch,
store: s,
ch: ch,
}
s.clearEventsMu.Lock()
s.clearEvents = append(s.clearEvents, event)
s.clearEventsMu.Unlock()
return event
}
// unsubscribeAdd removes an AddEvent from the active subscriptions.
func (s *Store) unsubscribeAdd(event *AddEvent) {
s.addEventsMu.Lock()
defer s.addEventsMu.Unlock()
for i, e := range s.addEvents {
if e == event {
s.addEvents = append(s.addEvents[:i], s.addEvents[i+1:]...)
break
}
}
}
// unsubscribeClear removes a ClearEvent from the active subscriptions.
func (s *Store) unsubscribeClear(event *ClearEvent) {
s.clearEventsMu.Lock()
defer s.clearEventsMu.Unlock()
for i, e := range s.clearEvents {
if e == event {
s.clearEvents = append(s.clearEvents[:i], s.clearEvents[i+1:]...)
break
}
}
}
// notifyAddEvents sends notifications to all active Add event subscribers.
// Non-blocking sends are used to prevent slow consumers from blocking the Store.
func (s *Store) notifyAddEvents(entry *DataEntry) {
s.addEventsMu.RLock()
defer s.addEventsMu.RUnlock()
for _, event := range s.addEvents {
select {
case event.ch <- entry:
default:
// Channel is full, skip this subscriber to avoid blocking
}
}
}
// notifyClearEvents sends notifications to all active Clear event subscribers.
// Non-blocking sends are used to prevent slow consumers from blocking the Store.
func (s *Store) notifyClearEvents() {
s.clearEventsMu.RLock()
defer s.clearEventsMu.RUnlock()
for _, event := range s.clearEvents {
select {
case event.ch <- struct{}{}:
default:
// Channel is full, skip this subscriber to avoid blocking
}
}
}