Commit cd6f319a authored by Robert Griesemer's avatar Robert Griesemer

godoc: minor tweaks for app-engine use

- read search index files in groutine to avoid
  start-up failure on app engine because reading
  the files takes too long
- permit usage of search index files and indexer
- minor cosmetic cleanups

R=dsymonds
CC=golang-dev
https://golang.org/cl/4952050
parent 25171439
...@@ -17,9 +17,11 @@ const ( ...@@ -17,9 +17,11 @@ const (
// in the .zip file. // in the .zip file.
zipGoroot = "/home/user/go" zipGoroot = "/home/user/go"
// indexFilenames is a glob pattern specifying // If indexFilenames != "", the search index is
// files containing the search index served by // initialized with the index stored in these
// godoc. The files are concatenated in sorted // files (otherwise it will be built at run-time,
// eventually). indexFilenames is a glob pattern;
// the specified files are concatenated in sorted
// order (by filename). // order (by filename).
// app-engine limit: file sizes must be <= 10MB; // app-engine limit: file sizes must be <= 10MB;
// use "split -b8m indexfile index.split." to get // use "split -b8m indexfile index.split." to get
......
...@@ -88,9 +88,7 @@ func init() { ...@@ -88,9 +88,7 @@ func init() {
// initialize search index // initialize search index
if *indexEnabled { if *indexEnabled {
if err := initIndex(); err != nil { go indexer()
log.Fatalf("error initializing index: %s", err)
}
} }
log.Println("godoc initialization complete") log.Println("godoc initialization complete")
......
...@@ -1065,12 +1065,8 @@ func lookup(query string) (result SearchResult) { ...@@ -1065,12 +1065,8 @@ func lookup(query string) (result SearchResult) {
if *indexEnabled { if *indexEnabled {
if _, ts := fsModified.get(); timestamp < ts { if _, ts := fsModified.get(); timestamp < ts {
// The index is older than the latest file system change under godoc's observation. // The index is older than the latest file system change under godoc's observation.
if *indexFiles != "" {
result.Alert = "Index not automatically updated: result may be inaccurate"
} else {
result.Alert = "Indexing in progress: result may be inaccurate" result.Alert = "Indexing in progress: result may be inaccurate"
} }
}
} else { } else {
result.Alert = "Search index disabled: no results available" result.Alert = "Search index disabled: no results available"
} }
...@@ -1145,6 +1141,29 @@ func fsDirnames() <-chan string { ...@@ -1145,6 +1141,29 @@ func fsDirnames() <-chan string {
return c return c
} }
func readIndex(filenames string) os.Error {
matches, err := filepath.Glob(filenames)
if err != nil {
return err
}
sort.Strings(matches) // make sure files are in the right order
files := make([]io.Reader, 0, len(matches))
for _, filename := range matches {
f, err := os.Open(filename)
if err != nil {
return err
}
defer f.Close()
files = append(files, f)
}
x := new(Index)
if err := x.Read(io.MultiReader(files...)); err != nil {
return err
}
searchIndex.set(x)
return nil
}
func updateIndex() { func updateIndex() {
if *verbose { if *verbose {
log.Printf("updating index...") log.Printf("updating index...")
...@@ -1165,6 +1184,14 @@ func updateIndex() { ...@@ -1165,6 +1184,14 @@ func updateIndex() {
} }
func indexer() { func indexer() {
// initialize the index from disk if possible
if *indexFiles != "" {
if err := readIndex(*indexFiles); err != nil {
log.Printf("error reading index: %s", err)
}
}
// repeatedly update the index when it goes out of date
for { for {
if !indexUpToDate() { if !indexUpToDate() {
// index possibly out of date - make a new one // index possibly out of date - make a new one
...@@ -1178,33 +1205,3 @@ func indexer() { ...@@ -1178,33 +1205,3 @@ func indexer() {
time.Sleep(delay) time.Sleep(delay)
} }
} }
func initIndex() os.Error {
if *indexFiles == "" {
// run periodic indexer
go indexer()
return nil
}
// get search index from files
matches, err := filepath.Glob(*indexFiles)
if err != nil {
return err
}
sort.Strings(matches) // make sure files are in the right order
files := make([]io.Reader, 0, len(matches))
for _, filename := range matches {
f, err := os.Open(filename)
if err != nil {
return err
}
defer f.Close()
files = append(files, f)
}
x := new(Index)
if err := x.Read(io.MultiReader(files...)); err != nil {
return err
}
searchIndex.set(x)
return nil
}
...@@ -248,6 +248,7 @@ func main() { ...@@ -248,6 +248,7 @@ func main() {
if err != nil { if err != nil {
log.Fatalf("%s: %s\n", *zipfile, err) log.Fatalf("%s: %s\n", *zipfile, err)
} }
defer rc.Close() // be nice (e.g., -writeIndex mode)
*goroot = path.Join("/", *goroot) // fsHttp paths are relative to '/' *goroot = path.Join("/", *goroot) // fsHttp paths are relative to '/'
fs = NewZipFS(rc) fs = NewZipFS(rc)
fsHttp = NewHttpZipFS(rc, *goroot) fsHttp = NewHttpZipFS(rc, *goroot)
...@@ -262,8 +263,9 @@ func main() { ...@@ -262,8 +263,9 @@ func main() {
} }
if *writeIndex { if *writeIndex {
// Write search index and exit.
if *indexFiles == "" { if *indexFiles == "" {
log.Fatal("no index files specified") log.Fatal("no index file specified")
} }
log.Println("initialize file systems") log.Println("initialize file systems")
...@@ -342,9 +344,7 @@ func main() { ...@@ -342,9 +344,7 @@ func main() {
// Initialize search index. // Initialize search index.
if *indexEnabled { if *indexEnabled {
if err := initIndex(); err != nil { go indexer()
log.Fatalf("error initializing index: %s", err)
}
} }
// Start http server. // Start http server.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment