-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog_manager.go
More file actions
102 lines (86 loc) · 2.06 KB
/
Copy pathlog_manager.go
File metadata and controls
102 lines (86 loc) · 2.06 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
package main
import (
"io"
"sync"
)
type logManager struct {
cache map[logKey]*logVal
lock sync.RWMutex
ls LogStore
cs ConfStore
}
func newLogManager(ls LogStore, cs ConfStore) logManager {
return logManager{
cache: make(map[logKey]*logVal),
lock: sync.RWMutex{},
ls: ls,
cs: cs,
}
}
// OpenLog tracks an open log with 'key' and state defined in 'val'.
func (lm *logManager) OpenLog(key logKey, val *logVal) error {
lm.lock.Lock()
defer lm.lock.Unlock()
_, exists := lm.cache[key]
if exists {
return ErrKeyExists
}
lm.cache[key] = val
return nil
}
// GetOpenLogReader returns an io.Reader for an open log with 'key'.
func (lm *logManager) GetOpenLogReader(key logKey) (io.Reader, error) {
val, err := lm.getLogVal(key)
if err != nil {
return nil, err
}
return &val.rb, nil
}
// CloseLog transitions 'key' from an open to a closed state.
// An open log is an actively draining that is cached in memory.
// A closed log is one that is done draining and is immutable.
// This log is persisted to a logStore and removed from the cache.
func (lm *logManager) CloseLog(key logKey) error {
val, err := lm.getLogVal(key)
if err != nil {
return err
}
// Depending on the implementation of Commit, it may or may not block.
// To avoid starving other potentially much faster operations, we do not
// grab a lock for this section.
err = lm.ls.CommitLog(key, *val)
if err != nil {
return err
}
lm.lock.RLock()
defer lm.lock.RUnlock()
delete(lm.cache, key)
return nil
}
func (lm *logManager) SetLabel(key logKey, label string) error {
lm.lock.Lock()
defer lm.lock.Unlock()
val, exists := lm.cache[key]
if !exists {
return ErrInvalidKey
}
val.label = label
return nil
}
func (lm *logManager) GetLabel(key logKey) (string, error) {
val, err := lm.getLogVal(key)
if err != nil {
return "", err
}
return val.label, nil
}
func (lm *logManager) getLogVal(key logKey) (*logVal, error) {
lm.lock.RLock()
defer lm.lock.RUnlock()
val, exists := lm.cache[key]
if exists {
return val, nil
} else {
return nil, ErrLogNotFound
}
}