You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
1.8 KiB

  1. // Copyright (c) 2015 Couchbase, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package index
  15. import (
  16. "github.com/blevesearch/bleve/analysis"
  17. "github.com/blevesearch/bleve/document"
  18. )
  19. type IndexRow interface {
  20. KeySize() int
  21. KeyTo([]byte) (int, error)
  22. Key() []byte
  23. ValueSize() int
  24. ValueTo([]byte) (int, error)
  25. Value() []byte
  26. }
  27. type AnalysisResult struct {
  28. DocID string
  29. Rows []IndexRow
  30. // scorch
  31. Document *document.Document
  32. Analyzed []analysis.TokenFrequencies
  33. Length []int
  34. }
  35. type AnalysisWork struct {
  36. i Index
  37. d *document.Document
  38. rc chan *AnalysisResult
  39. }
  40. func NewAnalysisWork(i Index, d *document.Document, rc chan *AnalysisResult) *AnalysisWork {
  41. return &AnalysisWork{
  42. i: i,
  43. d: d,
  44. rc: rc,
  45. }
  46. }
  47. type AnalysisQueue struct {
  48. queue chan *AnalysisWork
  49. done chan struct{}
  50. }
  51. func (q *AnalysisQueue) Queue(work *AnalysisWork) {
  52. q.queue <- work
  53. }
  54. func (q *AnalysisQueue) Close() {
  55. close(q.done)
  56. }
  57. func NewAnalysisQueue(numWorkers int) *AnalysisQueue {
  58. rv := AnalysisQueue{
  59. queue: make(chan *AnalysisWork),
  60. done: make(chan struct{}),
  61. }
  62. for i := 0; i < numWorkers; i++ {
  63. go AnalysisWorker(rv)
  64. }
  65. return &rv
  66. }
  67. func AnalysisWorker(q AnalysisQueue) {
  68. // read work off the queue
  69. for {
  70. select {
  71. case <-q.done:
  72. return
  73. case w := <-q.queue:
  74. r := w.i.Analyze(w.d)
  75. w.rc <- r
  76. }
  77. }
  78. }