-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinternalProcesses.go
More file actions
100 lines (96 loc) · 2.07 KB
/
internalProcesses.go
File metadata and controls
100 lines (96 loc) · 2.07 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
package jac
import (
"encoding/json"
"fmt"
"time"
)
// working file write handler
func writeHandler(consolidateTimers map[string]int64) {
defer func() {
if r := recover(); r != nil {
writeHandler(consolidateTimers)
}
}()
if consolidateTimers == nil {
consolidateTimers = make(map[string]int64)
}
if verbose {
println("start writeHandler")
}
for {
select {
case <-writeRstChannel:
if verbose {
fmt.Println("writeHandler closing")
}
writeRstChannel <- nil
case nw := <-writeChannel:
//fmt.Println("received", nw)
if nw.file != nil {
if nw.c != nil {
name := fmt.Sprintf("%v", nw.file.Name())
tm, skip := consolidateTimers[name]
if skip {
skip = time.Now().Unix()-tm < int64(options.IntervalCompacting)
} else {
consolidateTimers[name] = time.Now().Unix() - 1
}
if !skip {
// consolidation
if e := nw.file.Truncate(0); e == nil {
if _, e = nw.file.Seek(0, 0); e == nil {
for i, v := range nw.c.bucketItems() {
if fmt.Sprintf("%v", v.Object) != "" {
if data, err := json.Marshal(FileData{
Key: i,
Value: fmt.Sprintf("%v", v.Object),
}); err == nil {
_, _ = nw.file.WriteString(string(data) + "\n")
}
}
}
}
}
continue
}
}
// Update
if data, err := json.Marshal(FileData{
Key: nw.data[0],
Value: nw.data[1],
}); err == nil {
_, _ = nw.file.WriteString(string(data) + "\n")
}
}
}
}
}
// working file compact handler
func compactHandler(c Bucket) {
defer func() {
if r := recover(); r != nil {
compactHandler(c)
}
}()
if verbose {
println("start compactHandler for " + c.name)
}
for {
select {
case <-c.cr:
if verbose {
fmt.Println("compactHandler closing for", c.name)
}
case <-time.After(time.Duration(options.IntervalCompacting) * time.Second):
// in case of a zombie
if c.file == nil {
return
}
c.bucket.deleteExpired()
c.writer <- backupData{
c: c.bucket,
file: c.file,
}
}
}
}