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

227 lines
5.9KB

  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 upsidedown
  15. import (
  16. "reflect"
  17. "github.com/blevesearch/bleve/document"
  18. "github.com/blevesearch/bleve/index"
  19. "github.com/blevesearch/bleve/index/store"
  20. )
  21. var reflectStaticSizeIndexReader int
  22. func init() {
  23. var ir IndexReader
  24. reflectStaticSizeIndexReader = int(reflect.TypeOf(ir).Size())
  25. }
  26. type IndexReader struct {
  27. index *UpsideDownCouch
  28. kvreader store.KVReader
  29. docCount uint64
  30. }
  31. func (i *IndexReader) TermFieldReader(term []byte, fieldName string, includeFreq, includeNorm, includeTermVectors bool) (index.TermFieldReader, error) {
  32. fieldIndex, fieldExists := i.index.fieldCache.FieldNamed(fieldName, false)
  33. if fieldExists {
  34. return newUpsideDownCouchTermFieldReader(i, term, uint16(fieldIndex), includeFreq, includeNorm, includeTermVectors)
  35. }
  36. return newUpsideDownCouchTermFieldReader(i, []byte{ByteSeparator}, ^uint16(0), includeFreq, includeNorm, includeTermVectors)
  37. }
  38. func (i *IndexReader) FieldDict(fieldName string) (index.FieldDict, error) {
  39. return i.FieldDictRange(fieldName, nil, nil)
  40. }
  41. func (i *IndexReader) FieldDictRange(fieldName string, startTerm []byte, endTerm []byte) (index.FieldDict, error) {
  42. fieldIndex, fieldExists := i.index.fieldCache.FieldNamed(fieldName, false)
  43. if fieldExists {
  44. return newUpsideDownCouchFieldDict(i, uint16(fieldIndex), startTerm, endTerm)
  45. }
  46. return newUpsideDownCouchFieldDict(i, ^uint16(0), []byte{ByteSeparator}, []byte{})
  47. }
  48. func (i *IndexReader) FieldDictPrefix(fieldName string, termPrefix []byte) (index.FieldDict, error) {
  49. return i.FieldDictRange(fieldName, termPrefix, termPrefix)
  50. }
  51. func (i *IndexReader) DocIDReaderAll() (index.DocIDReader, error) {
  52. return newUpsideDownCouchDocIDReader(i)
  53. }
  54. func (i *IndexReader) DocIDReaderOnly(ids []string) (index.DocIDReader, error) {
  55. return newUpsideDownCouchDocIDReaderOnly(i, ids)
  56. }
  57. func (i *IndexReader) Document(id string) (doc *document.Document, err error) {
  58. // first hit the back index to confirm doc exists
  59. var backIndexRow *BackIndexRow
  60. backIndexRow, err = backIndexRowForDoc(i.kvreader, []byte(id))
  61. if err != nil {
  62. return
  63. }
  64. if backIndexRow == nil {
  65. return
  66. }
  67. doc = document.NewDocument(id)
  68. storedRow := NewStoredRow([]byte(id), 0, []uint64{}, 'x', nil)
  69. storedRowScanPrefix := storedRow.ScanPrefixForDoc()
  70. it := i.kvreader.PrefixIterator(storedRowScanPrefix)
  71. defer func() {
  72. if cerr := it.Close(); err == nil && cerr != nil {
  73. err = cerr
  74. }
  75. }()
  76. key, val, valid := it.Current()
  77. for valid {
  78. safeVal := make([]byte, len(val))
  79. copy(safeVal, val)
  80. var row *StoredRow
  81. row, err = NewStoredRowKV(key, safeVal)
  82. if err != nil {
  83. doc = nil
  84. return
  85. }
  86. if row != nil {
  87. fieldName := i.index.fieldCache.FieldIndexed(row.field)
  88. field := decodeFieldType(row.typ, fieldName, row.arrayPositions, row.value)
  89. if field != nil {
  90. doc.AddField(field)
  91. }
  92. }
  93. it.Next()
  94. key, val, valid = it.Current()
  95. }
  96. return
  97. }
  98. func (i *IndexReader) DocumentVisitFieldTerms(id index.IndexInternalID, fields []string, visitor index.DocumentFieldTermVisitor) error {
  99. fieldsMap := make(map[uint16]string, len(fields))
  100. for _, f := range fields {
  101. id, ok := i.index.fieldCache.FieldNamed(f, false)
  102. if ok {
  103. fieldsMap[id] = f
  104. }
  105. }
  106. tempRow := BackIndexRow{
  107. doc: id,
  108. }
  109. keyBuf := GetRowBuffer()
  110. if tempRow.KeySize() > len(keyBuf) {
  111. keyBuf = make([]byte, 2*tempRow.KeySize())
  112. }
  113. defer PutRowBuffer(keyBuf)
  114. keySize, err := tempRow.KeyTo(keyBuf)
  115. if err != nil {
  116. return err
  117. }
  118. value, err := i.kvreader.Get(keyBuf[:keySize])
  119. if err != nil {
  120. return err
  121. }
  122. if value == nil {
  123. return nil
  124. }
  125. return visitBackIndexRow(value, func(field uint32, term []byte) {
  126. if field, ok := fieldsMap[uint16(field)]; ok {
  127. visitor(field, term)
  128. }
  129. })
  130. }
  131. func (i *IndexReader) Fields() (fields []string, err error) {
  132. fields = make([]string, 0)
  133. it := i.kvreader.PrefixIterator([]byte{'f'})
  134. defer func() {
  135. if cerr := it.Close(); err == nil && cerr != nil {
  136. err = cerr
  137. }
  138. }()
  139. key, val, valid := it.Current()
  140. for valid {
  141. var row UpsideDownCouchRow
  142. row, err = ParseFromKeyValue(key, val)
  143. if err != nil {
  144. fields = nil
  145. return
  146. }
  147. if row != nil {
  148. fieldRow, ok := row.(*FieldRow)
  149. if ok {
  150. fields = append(fields, fieldRow.name)
  151. }
  152. }
  153. it.Next()
  154. key, val, valid = it.Current()
  155. }
  156. return
  157. }
  158. func (i *IndexReader) GetInternal(key []byte) ([]byte, error) {
  159. internalRow := NewInternalRow(key, nil)
  160. return i.kvreader.Get(internalRow.Key())
  161. }
  162. func (i *IndexReader) DocCount() (uint64, error) {
  163. return i.docCount, nil
  164. }
  165. func (i *IndexReader) Close() error {
  166. return i.kvreader.Close()
  167. }
  168. func (i *IndexReader) ExternalID(id index.IndexInternalID) (string, error) {
  169. return string(id), nil
  170. }
  171. func (i *IndexReader) InternalID(id string) (index.IndexInternalID, error) {
  172. return index.IndexInternalID(id), nil
  173. }
  174. func incrementBytes(in []byte) []byte {
  175. rv := make([]byte, len(in))
  176. copy(rv, in)
  177. for i := len(rv) - 1; i >= 0; i-- {
  178. rv[i] = rv[i] + 1
  179. if rv[i] != 0 {
  180. // didn't overflow, so stop
  181. break
  182. }
  183. }
  184. return rv
  185. }
  186. func (i *IndexReader) DocValueReader(fields []string) (index.DocValueReader, error) {
  187. return &DocValueReader{i: i, fields: fields}, nil
  188. }
  189. type DocValueReader struct {
  190. i *IndexReader
  191. fields []string
  192. }
  193. func (dvr *DocValueReader) VisitDocValues(id index.IndexInternalID,
  194. visitor index.DocumentFieldTermVisitor) error {
  195. return dvr.i.DocumentVisitFieldTerms(id, dvr.fields, visitor)
  196. }
上海开阖软件有限公司 沪ICP备12045867号-1