diff --git a/webapi/Search Dispatch.go b/webapi/Search Dispatch.go index c62d69f..cd636a4 100644 --- a/webapi/Search Dispatch.go +++ b/webapi/Search Dispatch.go @@ -23,6 +23,7 @@ func (api *WebapiInstance) dispatchSearch(input SearchRequest) (job *SearchJob) job = CreateSearchJob(Timeout, input.MaxResults, Filter) // todo: create actual search clients! + job.Status = SearchStatusLive go job.localSearch(api, input.Term) @@ -33,6 +34,7 @@ func (api *WebapiInstance) dispatchSearch(input SearchRequest) (job *SearchJob) func (job *SearchJob) localSearch(api *WebapiInstance, term string) { if api.backend.SearchIndex == nil || api.backend.GlobalBlockchainCache == nil { + job.Status = SearchStatusNoIndex return } @@ -75,6 +77,8 @@ resultLoop: job.statsAdd(&newFile) } + job.Status = SearchStatusTerminated + job.ResultSync.Unlock() job.Terminate() } diff --git a/webapi/Search Job.go b/webapi/Search Job.go index 9d83502..80f1f9f 100644 --- a/webapi/Search Job.go +++ b/webapi/Search Job.go @@ -48,6 +48,9 @@ type SearchJob struct { // -- result data -- + // Status indicates the overall search status. This will be removed later when relying on search clients. + Status int + // runtime data //clients []*SearchClient // all search clients clientsMutex sync.Mutex // mutex for manipulating client list @@ -67,10 +70,18 @@ type SearchJob struct { currentOffset int // for always getting the next results } +const ( + SearchStatusNotStarted = iota // Search was not yet started + SearchStatusLive // Search running + SearchStatusTerminated // Search is terminated. No more results are expected. + SearchStatusNoIndex // Search is terminated. No search index to use. +) + // CreateSearchJob creates a new search job and adds it to the lookup list. // Timeout and MaxResults must be set and must not be 0. func CreateSearchJob(Timeout time.Duration, MaxResults int, Filter SearchFilter) (job *SearchJob) { job = &SearchJob{} + job.Status = SearchStatusNotStarted job.id = uuid.New() job.timeout = Timeout job.maxResult = MaxResults diff --git a/webapi/Search.go b/webapi/Search.go index dcf1bc2..3c44761 100644 --- a/webapi/Search.go +++ b/webapi/Search.go @@ -169,12 +169,23 @@ func apiSearchResult(w http.ResponseWriter, r *http.Request) { result.Files = append(result.Files, *resultFiles[n]) } - if len(result.Files) == 0 { - result.Status = 3 // No results yet available keep trying + // set the status + if len(result.Files) > 0 { + result.Status = 0 // 0 = Success with results + } else { + switch job.Status { + case SearchStatusLive: + result.Status = 3 // No results yet available keep trying + + case SearchStatusTerminated: + result.Status = 1 // No more results to expect + + default: // SearchStatusNoIndex, SearchStatusNotStarted + result.Status = 1 // No more results to expect + } } //if !job.IsSearchResults() { // if no fresh results to be expected - result.Status = 1 // No more results to expect // embedded statistics? if returnStats, _ := strconv.ParseBool(r.Form.Get("stats")); returnStats {