Browse Source

improved search

pull/103/head
Lunny Xiao 11 years ago
parent
commit
39ca84fcfe
  1. 9
      cmd/search.go
  2. 74
      cmd/serve.go
  3. 7
      gopm.go

9
cmd/search.go

@ -76,6 +76,11 @@ func runSearch(cmd *Command, args []string) {
} }
} }
type searchRes struct {
Pkg string
Desc string
}
/* /*
request local or remote search service to find packages according to keyword inputed request local or remote search service to find packages according to keyword inputed
*/ */
@ -98,14 +103,14 @@ func search(host, port, keyword string, isExactly bool) {
return return
} }
pkgs := make([]string, 0) pkgs := make([]searchRes, 0)
err = json.Unmarshal(contents, &pkgs) err = json.Unmarshal(contents, &pkgs)
if err != nil { if err != nil {
com.ColorLog(err.Error()) com.ColorLog(err.Error())
return return
} }
for i, pkg := range pkgs { for i, pkg := range pkgs {
fmt.Println(i+1, pkg) fmt.Println(i+1, pkg.Pkg, "\t", pkg.Desc)
} }
} else { } else {
com.ColorLog(resp.Status) com.ColorLog(resp.Status)

74
cmd/serve.go

@ -29,6 +29,7 @@ import (
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
"time"
) )
var ( var (
@ -106,7 +107,10 @@ func runServe(cmd *Command, args []string) {
port = args[0] port = args[0]
} }
startService(listen, port) err := startService(listen, port)
if err != nil {
com.ColorLog("[ERRO] %v\n", err)
}
} }
func splitWord(word string, res *map[string]bool) { func splitWord(word string, res *map[string]bool) {
@ -126,7 +130,7 @@ func splitPkgName(pkgName string) (res map[string]bool) {
ps = ps[1:] ps = ps[1:]
} }
res = make(map[string]bool, 0) res = make(map[string]bool)
res[strings.Join(ps, "/")] = true res[strings.Join(ps, "/")] = true
for _, w := range ps { for _, w := range ps {
splitWord(w, &res) splitWord(w, &res)
@ -134,6 +138,15 @@ func splitPkgName(pkgName string) (res map[string]bool) {
return return
} }
func splitSynopsis(synopsis string) map[string]bool {
res := make(map[string]bool)
ss := strings.Fields(synopsis)
for _, s := range ss {
res[s] = true
}
return res
}
var ( var (
ro *opt.ReadOptions = &opt.ReadOptions{} ro *opt.ReadOptions = &opt.ReadOptions{}
wo *opt.WriteOptions = &opt.WriteOptions{} wo *opt.WriteOptions = &opt.WriteOptions{}
@ -145,12 +158,12 @@ func dbGet(key string) (string, error) {
} }
func dbPut(key string, value string) error { func dbPut(key string, value string) error {
fmt.Println("put ", key, ": ", value) //fmt.Println("put ", key, ": ", value)
return db.Put([]byte(key), []byte(value), wo) return db.Put([]byte(key), []byte(value), wo)
} }
func batchPut(batch *leveldb.Batch, key string, value string) error { func batchPut(batch *leveldb.Batch, key string, value string) error {
fmt.Println("put ", key, ": ", value) //fmt.Println("put ", key, ": ", value)
batch.Put([]byte(key), []byte(value)) batch.Put([]byte(key), []byte(value))
return nil return nil
} }
@ -269,7 +282,7 @@ func addNode(nod *doc.Node) error {
} }
if vers == "" { if vers == "" {
fmt.Println(nod) //fmt.Println(nod)
vers = nod.VerString() vers = nod.VerString()
} else { } else {
if !strings.Contains(vers, nod.VerString()) { if !strings.Contains(vers, nod.VerString()) {
@ -291,15 +304,20 @@ func addNode(nod *doc.Node) error {
// indexing package name // indexing package name
keys := splitPkgName(nod.ImportPath) keys := splitPkgName(nod.ImportPath)
for key, _ := range keys { for key, _ := range keys {
err = batchPut(batch, fmt.Sprintf("key:%v:%v", key, id), "") err = batchPut(batch, fmt.Sprintf("key:%v:%v", strings.ToLower(key), id), "")
if err != nil { if err != nil {
return err return err
} }
} }
// TODO: indexing desc
if nod.Synopsis != "" { if nod.Synopsis != "" {
//fields := strings.FieldsFunc(nod.Synopsis, f) fields := splitSynopsis(nod.Synopsis)
for field, _ := range fields {
err = batchPut(batch, fmt.Sprintf("key:%v:%v", strings.ToLower(field), id), "")
if err != nil {
return err
}
}
} }
return db.Write(batch, wo) return db.Write(batch, wo)
@ -322,9 +340,10 @@ func AutoRun() error {
} }
attr := &os.ProcAttr{ attr := &os.ProcAttr{
Dir: curPath, Dir: curPath,
Env: os.Environ(), Env: os.Environ(),
Files: []*os.File{nil, nil, nil}, //Files: []*os.File{nil, nil, nil},
Files: []*os.File{os.Stdin, os.Stdout, os.Stderr},
} }
p, err := exePath() p, err := exePath()
@ -332,10 +351,12 @@ func AutoRun() error {
return err return err
} }
//com.ColorLog("[INFO] now is starting search daemon ...\n")
_, err = os.StartProcess(p, []string{"gopm", "serve", "-l"}, attr) _, err = os.StartProcess(p, []string{"gopm", "serve", "-l"}, attr)
if err != nil { if err != nil {
return err return err
} }
time.Sleep(time.Second)
} }
return nil return nil
} }
@ -405,12 +426,15 @@ func startService(listen, port string) error {
return err return err
} }
f, err := os.OpenFile(pFile, os.O_CREATE, 0700) f, err := os.OpenFile(pFile, os.O_RDWR|os.O_CREATE, 0700)
if err != nil { if err != nil {
return err return err
} }
defer f.Close() defer f.Close()
f.WriteString(fmt.Sprintf("%v,%v,%v", RUNNING, os.Getpid(), port)) _, err = f.WriteString(fmt.Sprintf("%v,%v,%v", RUNNING, os.Getpid(), port))
if err != nil {
return err
}
dbDir = strings.Replace(dbDir, "~", homeDir, -1) dbDir = strings.Replace(dbDir, "~", homeDir, -1)
@ -436,7 +460,7 @@ func searchHandler(w http.ResponseWriter, r *http.Request) {
ids := make(map[string]bool) ids := make(map[string]bool)
for key, _ := range r.Form { for key, _ := range r.Form {
iter := db.NewIterator(ro) iter := db.NewIterator(ro)
rkey := fmt.Sprintf("key:%v:", key) rkey := fmt.Sprintf("key:%v:", strings.ToLower(key))
if iter.Seek([]byte(rkey)) { if iter.Seek([]byte(rkey)) {
k := iter.Key() k := iter.Key()
if !strings.HasPrefix(string(k), rkey) { if !strings.HasPrefix(string(k), rkey) {
@ -459,16 +483,21 @@ func searchHandler(w http.ResponseWriter, r *http.Request) {
for id, _ := range ids { for id, _ := range ids {
idkeys := strings.SplitN(id, ":", -1) idkeys := strings.SplitN(id, ":", -1)
rId := idkeys[len(idkeys)-1] rId := idkeys[len(idkeys)-1]
fmt.Println(rId) //fmt.Println(rId)
pkg, err := dbGet(fmt.Sprintf("pkg:%v", rId)) pkg, err := dbGet(fmt.Sprintf("pkg:%v", rId))
if err != nil { if err != nil {
com.ColorLog(err.Error()) com.ColorLog(err.Error())
continue continue
} }
pkgs = append(pkgs, pkg) desc, err := dbGet(fmt.Sprintf("desc:%v", rId))
if err != nil {
com.ColorLog(err.Error())
continue
}
pkgs = append(pkgs, fmt.Sprintf(`{"pkg":"%v", "desc":"%v"}`, pkg, desc))
} }
w.Write([]byte("[\"" + strings.Join(pkgs, "\", \"") + "\"]")) w.Write([]byte("[" + strings.Join(pkgs, ", ") + "]"))
} }
func searcheHandler(w http.ResponseWriter, r *http.Request) { func searcheHandler(w http.ResponseWriter, r *http.Request) {
@ -476,17 +505,22 @@ func searcheHandler(w http.ResponseWriter, r *http.Request) {
r.ParseForm() r.ParseForm()
pkgs := make([]string, 0) pkgs := make([]string, 0)
for key, _ := range r.Form { for key, _ := range r.Form {
_, err := dbGet("index:" + key) rId, err := dbGet("index:" + key)
if err != nil {
com.ColorLog(err.Error())
continue
}
desc, err := dbGet(fmt.Sprintf("desc:%v", rId))
if err != nil { if err != nil {
com.ColorLog(err.Error()) com.ColorLog(err.Error())
continue continue
} }
pkgs = append(pkgs, key) pkgs = append(pkgs, fmt.Sprintf(`{"pkg":"%v", "desc":"%v"}`, key, desc))
} }
w.Write([]byte("[\"" + strings.Join(pkgs, "\", \"") + "\"]")) w.Write([]byte("[" + strings.Join(pkgs, ", ") + "]"))
//} //}
} }

7
gopm.go

@ -17,6 +17,8 @@ package main
import ( import (
"fmt" "fmt"
"github.com/Unknwon/com"
"github.com/gpmgo/gopm/cmd"
"io" "io"
"os" "os"
"runtime" "runtime"
@ -25,9 +27,6 @@ import (
"text/template" "text/template"
"unicode" "unicode"
"unicode/utf8" "unicode/utf8"
"github.com/Unknwon/com"
"github.com/gpmgo/gopm/cmd"
) )
// +build go1.1 // +build go1.1
@ -94,7 +93,7 @@ func main() {
if err == nil { if err == nil {
comm.Run(comm, args[1:]) comm.Run(comm, args[1:])
} else { } else {
com.ColorLog("[ERRO] %v", err) com.ColorLog("[ERRO] %v\n", err)
} }
} else { } else {
comm.Run(comm, args[1:]) comm.Run(comm, args[1:])

Loading…
Cancel
Save