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.

123 lines
3.2 KiB

  1. // Copyright (c) 2014 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 document
  15. import (
  16. "fmt"
  17. "reflect"
  18. "github.com/blevesearch/bleve/analysis"
  19. "github.com/blevesearch/bleve/size"
  20. )
  21. var reflectStaticSizeBooleanField int
  22. func init() {
  23. var f BooleanField
  24. reflectStaticSizeBooleanField = int(reflect.TypeOf(f).Size())
  25. }
  26. const DefaultBooleanIndexingOptions = StoreField | IndexField | DocValues
  27. type BooleanField struct {
  28. name string
  29. arrayPositions []uint64
  30. options IndexingOptions
  31. value []byte
  32. numPlainTextBytes uint64
  33. }
  34. func (b *BooleanField) Size() int {
  35. return reflectStaticSizeBooleanField + size.SizeOfPtr +
  36. len(b.name) +
  37. len(b.arrayPositions)*size.SizeOfUint64 +
  38. len(b.value)
  39. }
  40. func (b *BooleanField) Name() string {
  41. return b.name
  42. }
  43. func (b *BooleanField) ArrayPositions() []uint64 {
  44. return b.arrayPositions
  45. }
  46. func (b *BooleanField) Options() IndexingOptions {
  47. return b.options
  48. }
  49. func (b *BooleanField) Analyze() (int, analysis.TokenFrequencies) {
  50. tokens := make(analysis.TokenStream, 0)
  51. tokens = append(tokens, &analysis.Token{
  52. Start: 0,
  53. End: len(b.value),
  54. Term: b.value,
  55. Position: 1,
  56. Type: analysis.Boolean,
  57. })
  58. fieldLength := len(tokens)
  59. tokenFreqs := analysis.TokenFrequency(tokens, b.arrayPositions, b.options.IncludeTermVectors())
  60. return fieldLength, tokenFreqs
  61. }
  62. func (b *BooleanField) Value() []byte {
  63. return b.value
  64. }
  65. func (b *BooleanField) Boolean() (bool, error) {
  66. if len(b.value) == 1 {
  67. return b.value[0] == 'T', nil
  68. }
  69. return false, fmt.Errorf("boolean field has %d bytes", len(b.value))
  70. }
  71. func (b *BooleanField) GoString() string {
  72. return fmt.Sprintf("&document.BooleanField{Name:%s, Options: %s, Value: %s}", b.name, b.options, b.value)
  73. }
  74. func (b *BooleanField) NumPlainTextBytes() uint64 {
  75. return b.numPlainTextBytes
  76. }
  77. func NewBooleanFieldFromBytes(name string, arrayPositions []uint64, value []byte) *BooleanField {
  78. return &BooleanField{
  79. name: name,
  80. arrayPositions: arrayPositions,
  81. value: value,
  82. options: DefaultNumericIndexingOptions,
  83. numPlainTextBytes: uint64(len(value)),
  84. }
  85. }
  86. func NewBooleanField(name string, arrayPositions []uint64, b bool) *BooleanField {
  87. return NewBooleanFieldWithIndexingOptions(name, arrayPositions, b, DefaultNumericIndexingOptions)
  88. }
  89. func NewBooleanFieldWithIndexingOptions(name string, arrayPositions []uint64, b bool, options IndexingOptions) *BooleanField {
  90. numPlainTextBytes := 5
  91. v := []byte("F")
  92. if b {
  93. numPlainTextBytes = 4
  94. v = []byte("T")
  95. }
  96. return &BooleanField{
  97. name: name,
  98. arrayPositions: arrayPositions,
  99. value: v,
  100. options: options,
  101. numPlainTextBytes: uint64(numPlainTextBytes),
  102. }
  103. }