]> Cypherpunks repositories - gostls13.git/commitdiff
godoc: use a bufio.Buffer to read search index
authorRobert Griesemer <gri@golang.org>
Fri, 7 Oct 2011 00:36:00 +0000 (17:36 -0700)
committerRobert Griesemer <gri@golang.org>
Fri, 7 Oct 2011 00:36:00 +0000 (17:36 -0700)
Also: Minor refactoring for cleanliness and symmetry.

Fixes #2286.

R=r
CC=golang-dev
https://golang.org/cl/5232041

src/cmd/godoc/index.go

index 480ef621e9cac06b7ac85e6c9a6ceeefcd674869..4f687ea8310d1c29046b4b4ccf3d132f031b5ef9 100644 (file)
@@ -38,6 +38,7 @@
 package main
 
 import (
+       "bufio"
        "bytes"
        "go/ast"
        "go/parser"
@@ -840,6 +841,14 @@ type fileIndex struct {
        Fulltext bool
 }
 
+func (x *fileIndex) Write(w io.Writer) os.Error {
+       return gob.NewEncoder(w).Encode(x)
+}
+
+func (x *fileIndex) Read(r io.Reader) os.Error {
+       return gob.NewDecoder(r).Decode(x)
+}
+
 // Write writes the index x to w.
 func (x *Index) Write(w io.Writer) os.Error {
        fulltext := false
@@ -852,7 +861,7 @@ func (x *Index) Write(w io.Writer) os.Error {
                x.snippets,
                fulltext,
        }
-       if err := gob.NewEncoder(w).Encode(fx); err != nil {
+       if err := fx.Write(w); err != nil {
                return err
        }
        if fulltext {
@@ -867,9 +876,14 @@ func (x *Index) Write(w io.Writer) os.Error {
 }
 
 // Read reads the index from r into x; x must not be nil.
+// If r does not also implement io.ByteReader, it will be wrapped in a bufio.Reader.
 func (x *Index) Read(r io.Reader) os.Error {
+       // We use the ability to read bytes as a plausible surrogate for buffering.
+       if _, ok := r.(io.ByteReader); !ok {
+               r = bufio.NewReader(r)
+       }
        var fx fileIndex
-       if err := gob.NewDecoder(r).Decode(&fx); err != nil {
+       if err := fx.Read(r); err != nil {
                return err
        }
        x.words = fx.Words