forked from benrowe/books
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.go
166 lines (145 loc) · 3.59 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
package main
import (
"bufio"
"fmt"
"os"
"path/filepath"
"github.com/kjk/siser"
"github.com/kjk/u"
)
const (
recNameGlotOutput = "glotoutput"
recNameGlotID = "glotid"
)
type Cache struct {
// path of the cache file
path string
sha1ToGlotOutput map[string]*EvalOutput
// id of glot.id code snippet for this code, if exists
sha1ToGlotID map[string]string
// id of https://goplay.space snippet for this code, if exists
sha1ToGoPlayID map[string]string
}
func NewCache(path string) *Cache {
return &Cache{
path: path,
sha1ToGlotOutput: map[string]*EvalOutput{},
sha1ToGlotID: map[string]string{},
sha1ToGoPlayID: map[string]string{},
}
}
func (c *Cache) saveRecord(rec *siser.Record) error {
f := openForAppend(c.path)
defer u.CloseNoError(f)
w := siser.NewWriter(f)
_, err := w.WriteRecord(rec)
return err
}
type EvalOutput struct {
Lang string
FileName string
CodeFull string
CodeToRun string
RunCmd string
Output string
// sha1 of CodeFull, calculated on demand
sha1 string
}
func (c *EvalOutput) Sha1() string {
if c.sha1 == "" {
c.sha1 = u.Sha1HexOfBytes([]byte(c.CodeFull))
}
return c.sha1
}
func (c *Cache) saveGlotOutput(code *EvalOutput) {
u.PanicIf(code.CodeFull == "")
u.PanicIf(c.sha1ToGlotOutput[code.Sha1()] != nil)
rec := &siser.Record{
Name: recNameGlotOutput,
}
rec.Append("Sha1", code.Sha1())
rec.Append("Lang", code.Lang)
rec.Append("FileName", code.FileName)
rec.Append("CodeFull", code.CodeFull)
rec.Append("CodeToRun", code.CodeToRun)
if code.RunCmd != "" {
rec.Append("RunCmd", code.RunCmd)
}
rec.Append("Output", code.Output)
err := c.saveRecord(rec)
must(err)
c.sha1ToGlotOutput[code.Sha1()] = code
}
func (c *Cache) loadGlotOutput(rec *siser.Record) {
u.PanicIf(rec.Name != recNameGlotOutput)
sha1, ok := rec.Get("Sha1")
u.PanicIf(!ok || sha1 == "")
u.PanicIf(c.sha1ToGlotOutput[sha1] != nil)
o := &EvalOutput{}
o.Lang, ok = rec.Get("Lang")
u.PanicIf(!ok)
o.FileName, ok = rec.Get("FileName")
u.PanicIf(!ok)
o.CodeFull, ok = rec.Get("CodeFull")
u.PanicIf(!ok)
o.CodeToRun, ok = rec.Get("CodeToRun")
u.PanicIf(!ok)
o.RunCmd, ok = rec.Get("RunCmd")
o.Output, ok = rec.Get("Output")
u.PanicIf(!ok)
u.PanicIf(o.CodeFull == "")
u.PanicIf(sha1 != o.Sha1(), "sha1 != code.Sha1() (%s != %s)", sha1, o.Sha1())
c.sha1ToGlotOutput[sha1] = o
}
func (c *Cache) saveGlotID(sha1, glotID string) {
u.PanicIf(c.sha1ToGlotID[sha1] != "")
rec := &siser.Record{
Name: recNameGlotID,
}
u.PanicIf(sha1 == "")
rec.Append("Sha1", sha1)
rec.Append("GlotID", glotID)
err := c.saveRecord(rec)
must(err)
c.sha1ToGlotID[sha1] = glotID
}
func (c *Cache) loadGlotID(rec *siser.Record) {
u.PanicIf(rec.Name != recNameGlotID)
sha1, ok := rec.Get("Sha1")
u.PanicIf(!ok || sha1 == "")
u.PanicIf(c.sha1ToGlotID[sha1] != "")
glotid, ok := rec.Get("GlotID")
u.PanicIf(!ok)
c.sha1ToGlotID[sha1] = glotid
}
func loadCache(path string) *Cache {
logf("loadCache: %s\n", path)
dir := filepath.Dir(path)
// the directory must exist
_, err := os.Stat(dir)
must(err)
c := NewCache(path)
f, err := os.Open(path)
if err != nil {
// it's ok if file doesn't exist
logf(" cache file %s doesn't exist\n", path)
return c
}
defer u.CloseNoError(f)
nRecords := 0
r := siser.NewReader(bufio.NewReader(f))
for r.ReadNextRecord() {
rec := r.Record
if rec.Name == recNameGlotOutput {
c.loadGlotOutput(rec)
} else if rec.Name == recNameGlotID {
c.loadGlotID(rec)
} else {
panic(fmt.Errorf("unknown record type: '%s'", rec.Name))
}
nRecords++
}
must(r.Err())
logf(" got %d cache records\n", nRecords)
return c
}