-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
170 lines (136 loc) · 3.68 KB
/
cache.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
// Copyright 2020 Ondrej Holecek <ondrej@holecek.eu>. All rights reserved. Use of this source code
// is governed by the CC BY-ND 4.0 license that can be found in the LICENSE.txt file.
// Experimental cache support.
//
// Filters can be applied on the original plain text file and results
// can be stored in the cache file.
// Further reads can be performed from this cache file with increased
// speed.
package main
import (
hprose "github.com/hprose/hprose-golang/io"
"encoding/binary"
"compress/gzip"
"sync"
"strings"
"os"
"math/rand"
"fmt"
"foset/fortisession"
"io"
)
type CacheFile struct {
writer []*gzip.Writer
reader []*gzip.Reader
lock []*sync.Mutex
parts int
}
func CacheInit(filename string, mode string, parts int) (*CacheFile, error) {
var c CacheFile
var file *os.File
var err error = nil
c.writer = make([]*gzip.Writer, parts)
c.reader = make([]*gzip.Reader, parts)
c.lock = make([]*sync.Mutex, parts)
c.parts = parts
for part := 0; part < c.parts; part++ {
partname := fmt.Sprintf("%s.%d", filename, part)
if strings.Contains(mode, "w") {
file, err = os.OpenFile(partname, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err == nil {
c.writer[part], err = gzip.NewWriterLevel(file, gzip.BestSpeed)
}
} else {
file, err = os.OpenFile(partname, os.O_RDONLY, 0)
if err == nil {
c.reader[part], err = gzip.NewReader(file)
}
}
if err != nil {
err = fmt.Errorf("Unable to open cache file (mode \"%s\"): %s", mode, err)
break
}
c.lock[part] = &sync.Mutex{}
}
return &c, err
}
func (c *CacheFile) Finalize() {
for part := 0; part < c.parts; part++ {
c.lock[part].Lock()
if c.writer[part] != nil { c.writer[part].Close() }
if c.reader[part] != nil { c.reader[part].Close() }
c.lock[part].Unlock()
}
}
func (c *CacheFile) Write(session *fortisession.Session) (error) {
data := hprose.Serialize(session, true)
b := make([]byte, 8)
binary.LittleEndian.PutUint64(b, uint64(len(data)))
var err error
part := rand.Intn(c.parts)
c.lock[part].Lock()
err = write_exactly(c.writer[part], b)
if err == nil { err = write_exactly(c.writer[part], data) }
c.lock[part].Unlock()
if err != nil {
return fmt.Errorf("Unable to serialize to cache file: %s", err)
} else {
return nil
}
}
func (c *CacheFile) ReadAll(sessions chan *fortisession.Session) (error) {
done := make(chan error, c.parts)
for part := 0; part < c.parts; part++ {
go c.read_part(part, sessions, done)
}
// wait for all gorutines to finish
var err error = nil
for part := 0; part < c.parts; part++ {
tmp := <-done
if tmp != nil { err = tmp }
}
return err
}
func (c *CacheFile) read_part(part int, sessions chan *fortisession.Session, done chan error) {
b := make([]byte, 8)
var err error
for err != io.EOF {
// c.lock.Lock()
err = read_exactly(c.reader[part], b)
length := int(binary.LittleEndian.Uint64(b))
data := make ([]byte, length)
err = read_exactly(c.reader[part], data)
// c.lock.Unlock()
if err != nil && err != io.EOF { break }
var session fortisession.Session
hprose.Unserialize(data, &session, true)
sessions <- &session
}
if err != nil && err != io.EOF {
done <- fmt.Errorf("Unable to deserialize from cache file (part %d): %s", part, err)
} else {
done <- nil
}
}
func read_exactly(reader *gzip.Reader, buf []byte) (error) {
var r, total int
var err error
total = 0
for total < len(buf) {
r, err = reader.Read(buf[total:])
if err != nil { break }
total += r
}
return err
}
func write_exactly(writer *gzip.Writer, buf []byte) (error) {
var w, total int
var err error
total = 0
for total < len(buf) {
w, err = writer.Write(buf[total:])
if err != nil { break }
total += w
}
return err
}