Put gzip writer on top of bufio writer.

This commit is contained in:
Max Schaefer
2020-03-12 08:30:14 +00:00
parent 8901ba62e0
commit 6b0ba750e6
2 changed files with 16 additions and 15 deletions

View File

@@ -55,7 +55,7 @@ func (l *Labeler) GlobalID(key string) Label {
label, exists := l.keyLabels[key]
if !exists {
id := l.nextID()
fmt.Fprintf(l.tw.w, "%s=@\"%s\"\n", id, escapeString(key))
fmt.Fprintf(l.tw.zip, "%s=@\"%s\"\n", id, escapeString(key))
label = Label{id}
l.keyLabels[key] = label
}
@@ -83,7 +83,7 @@ func (l *Labeler) LocalID(nd interface{}) Label {
// FreshID creates a fresh label and returns it
func (l *Labeler) FreshID() Label {
id := l.nextID()
fmt.Fprintf(l.tw.w, "%s=*\n", id)
fmt.Fprintf(l.tw.zip, "%s=*\n", id)
return Label{id}
}

View File

@@ -17,8 +17,8 @@ import (
// A Writer provides methods for writing data to a TRAP file
type Writer struct {
w *bufio.Writer
zip *gzip.Writer
w *bufio.Writer
file *os.File
Labeler *Labeler
path string
@@ -43,10 +43,11 @@ func NewWriter(path string, pkg *packages.Package) (*Writer, error) {
if err != nil {
return nil, err
}
zipWriter := gzip.NewWriter(tmpFile)
bufioWriter := bufio.NewWriter(tmpFile)
zipWriter := gzip.NewWriter(bufioWriter)
tw := &Writer{
bufio.NewWriter(zipWriter),
zipWriter,
bufioWriter,
tmpFile,
nil,
path,
@@ -74,15 +75,15 @@ func trapFolder() (string, error) {
// Close the underlying file writer
func (tw *Writer) Close() error {
err := tw.w.Flush()
err := tw.zip.Close()
if err != nil {
// throw away close error because write errors are likely to be more important
// return zip-close error, but ignore file-close error
tw.file.Close()
return err
}
err = tw.zip.Close()
err = tw.w.Flush()
if err != nil {
// return zip-close error, but ignore file-close error
// throw away close error because write errors are likely to be more important
tw.file.Close()
return err
}
@@ -120,22 +121,22 @@ func capStringLength(s string) string {
// Emit writes out a tuple of values for the given `table`
func (tw *Writer) Emit(table string, values []interface{}) error {
fmt.Fprintf(tw.w, "%s(", table)
fmt.Fprintf(tw.zip, "%s(", table)
for i, value := range values {
if i > 0 {
fmt.Fprint(tw.w, ", ")
fmt.Fprint(tw.zip, ", ")
}
switch value := value.(type) {
case Label:
fmt.Fprint(tw.w, value.id)
fmt.Fprint(tw.zip, value.id)
case string:
fmt.Fprintf(tw.w, "\"%s\"", escapeString(capStringLength(value)))
fmt.Fprintf(tw.zip, "\"%s\"", escapeString(capStringLength(value)))
case int:
fmt.Fprintf(tw.w, "%d", value)
fmt.Fprintf(tw.zip, "%d", value)
default:
return errors.New("Cannot emit value")
}
}
fmt.Fprintf(tw.w, ")\n")
fmt.Fprintf(tw.zip, ")\n")
return nil
}