-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
44 lines (37 loc) · 1.03 KB
/
writer.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
package termdict
import (
"bytes"
"encoding/gob"
"fmt"
"github.com/k-yomo/ostrich/directory"
"github.com/k-yomo/ostrich/schema"
)
type TermWriter struct {
termInfos map[schema.FieldID]map[string]*TermInfo
termDictFile directory.WriteCloseSyncer
}
func NewTermWriter(termDictFile directory.WriteCloseSyncer) *TermWriter {
return &TermWriter{
termInfos: map[schema.FieldID]map[string]*TermInfo{},
termDictFile: termDictFile,
}
}
func (t *TermWriter) AddTermInfo(fieldID schema.FieldID, termInfo *TermInfo) {
if _, ok := t.termInfos[fieldID]; !ok {
t.termInfos[fieldID] = map[string]*TermInfo{}
}
t.termInfos[fieldID][termInfo.Term] = termInfo
}
func (t *TermWriter) Serialize() error {
b := bytes.NewBuffer([]byte{})
if err := gob.NewEncoder(b).Encode(t.termInfos); err != nil {
return fmt.Errorf("encode termInfos: %w", err)
}
if _, err := t.termDictFile.Write(b.Bytes()); err != nil {
return fmt.Errorf("write termdict: %w", err)
}
return nil
}
func (t *TermWriter) Close() error {
return t.termDictFile.Close()
}