Esempio n. 1
0
func main() {
	flag.Parse()
	if *indexPath == "" {
		log.Fatal("You need to specify a non-empty -index_path")
	}
	fmt.Println("Debian Code Search index-backend")

	id = path.Base(*indexPath)

	ix = index.Open(*indexPath)

	http.HandleFunc("/index", Index)
	log.Fatal(http.ListenAndServe(*listenAddress, nil))
}
Esempio n. 2
0
func main() {
	flag.Parse()
	if *indexPath == "" {
		log.Fatal("You need to specify a non-empty -index_path")
	}
	fmt.Println("Debian Code Search index-backend")

	http.Handle("/metrics", prometheus.Handler())

	log.Fatal(grpcutil.ListenAndServeTLS(*listenAddress,
		*tlsCertPath,
		*tlsKeyPath,
		func(s *grpc.Server) {
			proto.RegisterIndexBackendServer(s, &server{
				id: filepath.Base(*indexPath),
				ix: index.Open(*indexPath),
			})
		}))
}
Esempio n. 3
0
func Replace(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	newShard := r.Form.Get("shard")

	file, err := os.Open(filepath.Dir(*indexPath))
	if err != nil {
		log.Fatal(err)
	}
	defer file.Close()
	names, err := file.Readdirnames(-1)
	if err != nil {
		log.Fatal(err)
	}

	for _, name := range names {
		if name == newShard {
			newShard = filepath.Join(filepath.Dir(*indexPath), name)
			// We verified the given argument refers to an index shard within
			// this directory, so let’s load this shard.
			oldIndex := ix
			log.Printf("Trying to load %q\n", newShard)
			ixMutex.Lock()
			ix = index.Open(newShard)
			ixMutex.Unlock()
			// Overwrite the old full shard with the new one. This is necessary
			// so that the state is persistent across restarts and has the nice
			// side-effect of cleaning up the old full shard.
			if err := os.Rename(newShard, *indexPath); err != nil {
				log.Fatal(err)
			}
			oldIndex.Close()
			return
		}
	}

	http.Error(w, "No such shard.", http.StatusInternalServerError)
}
Esempio n. 4
0
func (s *server) ReplaceIndex(ctx context.Context, in *proto.ReplaceIndexRequest) (*proto.ReplaceIndexReply, error) {
	newShard := in.ReplacementPath

	file, err := os.Open(filepath.Dir(*indexPath))
	if err != nil {
		log.Fatal(err)
	}
	defer file.Close()
	names, err := file.Readdirnames(-1)
	if err != nil {
		log.Fatal(err)
	}

	for _, name := range names {
		if name == newShard {
			newShard = filepath.Join(filepath.Dir(*indexPath), name)
			// We verified the given argument refers to an index shard within
			// this directory, so let’s load this shard.
			oldIndex := s.ix
			log.Printf("Trying to load %q\n", newShard)
			s.ixMutex.Lock()
			s.ix = index.Open(newShard)
			s.ixMutex.Unlock()
			// Overwrite the old full shard with the new one. This is necessary
			// so that the state is persistent across restarts and has the nice
			// side-effect of cleaning up the old full shard.
			if err := os.Rename(newShard, *indexPath); err != nil {
				log.Fatal(err)
			}
			oldIndex.Close()
			return &proto.ReplaceIndexReply{}, nil
		}
	}

	return nil, fmt.Errorf("No such shard.")
}