0
0

upgraded beer-search to index in background

this allows the app to be usable while indexing takes place
also prints out indexing performace stats to console
This commit is contained in:
Marty Schoch 2014-08-11 13:20:32 -04:00
parent 42895649de
commit cac707b5b7

View File

@ -14,6 +14,7 @@ import (
"log" "log"
"net/http" "net/http"
"path/filepath" "path/filepath"
"time"
"github.com/couchbaselabs/bleve" "github.com/couchbaselabs/bleve"
bleveHttp "github.com/couchbaselabs/bleve/http" bleveHttp "github.com/couchbaselabs/bleve/http"
@ -38,11 +39,13 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
// index data // index data in the background
err = indexBeer(beerIndex) go func() {
if err != nil { err = indexBeer(beerIndex)
log.Fatal(err) if err != nil {
} log.Fatal(err)
}
}()
// create a router to serve static files // create a router to serve static files
router := staticFileRouter() router := staticFileRouter()
@ -71,8 +74,10 @@ func indexBeer(i bleve.Index) error {
return err return err
} }
// walk the directory entries // walk the directory entries for indexing
log.Printf("Indexing...")
count := 0 count := 0
startTime := time.Now()
for _, dirEntry := range dirEntries { for _, dirEntry := range dirEntries {
filename := dirEntry.Name() filename := dirEntry.Name()
// read the bytes // read the bytes
@ -88,7 +93,16 @@ func indexBeer(i bleve.Index) error {
return err return err
} }
count++ count++
if count%1000 == 0 {
indexDuration := time.Since(startTime)
indexDurationSeconds := float64(indexDuration) / float64(time.Second)
timePerDoc := float64(indexDuration) / float64(count)
log.Printf("Indexed %d documents, in %.2fs (average %.2fms/doc)", count, indexDurationSeconds, timePerDoc/float64(time.Millisecond))
}
} }
log.Printf("Indexed %d documents", count) indexDuration := time.Since(startTime)
indexDurationSeconds := float64(indexDuration) / float64(time.Second)
timePerDoc := float64(indexDuration) / float64(count)
log.Printf("Indexed %d documents, in %.2fs (average %.2fms/doc)", count, indexDurationSeconds, timePerDoc/float64(time.Millisecond))
return nil return nil
} }