本站源代码
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.

111 lines
2.2KB

  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. "reflect"
  17. "github.com/blevesearch/bleve/analysis"
  18. "github.com/blevesearch/bleve/document"
  19. "github.com/blevesearch/bleve/size"
  20. )
  21. var reflectStaticSizeAnalysisResult int
  22. func init() {
  23. var ar AnalysisResult
  24. reflectStaticSizeAnalysisResult = int(reflect.TypeOf(ar).Size())
  25. }
  26. type IndexRow interface {
  27. KeySize() int
  28. KeyTo([]byte) (int, error)
  29. Key() []byte
  30. ValueSize() int
  31. ValueTo([]byte) (int, error)
  32. Value() []byte
  33. }
  34. type AnalysisResult struct {
  35. DocID string
  36. Rows []IndexRow
  37. // scorch
  38. Document *document.Document
  39. Analyzed []analysis.TokenFrequencies
  40. Length []int
  41. }
  42. func (a *AnalysisResult) Size() int {
  43. rv := reflectStaticSizeAnalysisResult
  44. for _, analyzedI := range a.Analyzed {
  45. rv += analyzedI.Size()
  46. }
  47. rv += len(a.Length) * size.SizeOfInt
  48. return rv
  49. }
  50. type AnalysisWork struct {
  51. i Index
  52. d *document.Document
  53. rc chan *AnalysisResult
  54. }
  55. func NewAnalysisWork(i Index, d *document.Document, rc chan *AnalysisResult) *AnalysisWork {
  56. return &AnalysisWork{
  57. i: i,
  58. d: d,
  59. rc: rc,
  60. }
  61. }
  62. type AnalysisQueue struct {
  63. queue chan *AnalysisWork
  64. done chan struct{}
  65. }
  66. func (q *AnalysisQueue) Queue(work *AnalysisWork) {
  67. q.queue <- work
  68. }
  69. func (q *AnalysisQueue) Close() {
  70. close(q.done)
  71. }
  72. func NewAnalysisQueue(numWorkers int) *AnalysisQueue {
  73. rv := AnalysisQueue{
  74. queue: make(chan *AnalysisWork),
  75. done: make(chan struct{}),
  76. }
  77. for i := 0; i < numWorkers; i++ {
  78. go AnalysisWorker(rv)
  79. }
  80. return &rv
  81. }
  82. func AnalysisWorker(q AnalysisQueue) {
  83. // read work off the queue
  84. for {
  85. select {
  86. case <-q.done:
  87. return
  88. case w := <-q.queue:
  89. r := w.i.Analyze(w.d)
  90. w.rc <- r
  91. }
  92. }
  93. }
上海开阖软件有限公司 沪ICP备12045867号-1