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.

segment.go 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. // Copyright (c) 2017 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 zap
  15. import (
  16. "bytes"
  17. "encoding/binary"
  18. "fmt"
  19. "io"
  20. "os"
  21. "sync"
  22. "unsafe"
  23. "github.com/RoaringBitmap/roaring"
  24. mmap "github.com/blevesearch/mmap-go"
  25. segment "github.com/blevesearch/scorch_segment_api/v2"
  26. "github.com/blevesearch/vellum"
  27. "github.com/golang/snappy"
  28. )
  29. var reflectStaticSizeSegmentBase int
  30. func init() {
  31. var sb SegmentBase
  32. reflectStaticSizeSegmentBase = int(unsafe.Sizeof(sb))
  33. }
  34. // Open returns a zap impl of a segment
  35. func (*ZapPlugin) Open(path string) (segment.Segment, error) {
  36. f, err := os.Open(path)
  37. if err != nil {
  38. return nil, err
  39. }
  40. mm, err := mmap.Map(f, mmap.RDONLY, 0)
  41. if err != nil {
  42. // mmap failed, try to close the file
  43. _ = f.Close()
  44. return nil, err
  45. }
  46. rv := &Segment{
  47. SegmentBase: SegmentBase{
  48. mem: mm[0 : len(mm)-FooterSize],
  49. fieldsMap: make(map[string]uint16),
  50. fieldDvReaders: make(map[uint16]*docValueReader),
  51. fieldFSTs: make(map[uint16]*vellum.FST),
  52. },
  53. f: f,
  54. mm: mm,
  55. path: path,
  56. refs: 1,
  57. }
  58. rv.SegmentBase.updateSize()
  59. err = rv.loadConfig()
  60. if err != nil {
  61. _ = rv.Close()
  62. return nil, err
  63. }
  64. err = rv.loadFields()
  65. if err != nil {
  66. _ = rv.Close()
  67. return nil, err
  68. }
  69. err = rv.loadDvReaders()
  70. if err != nil {
  71. _ = rv.Close()
  72. return nil, err
  73. }
  74. return rv, nil
  75. }
  76. // SegmentBase is a memory only, read-only implementation of the
  77. // segment.Segment interface, using zap's data representation.
  78. type SegmentBase struct {
  79. mem []byte
  80. memCRC uint32
  81. chunkFactor uint32
  82. fieldsMap map[string]uint16 // fieldName -> fieldID+1
  83. fieldsInv []string // fieldID -> fieldName
  84. numDocs uint64
  85. storedIndexOffset uint64
  86. fieldsIndexOffset uint64
  87. docValueOffset uint64
  88. dictLocs []uint64
  89. fieldDvReaders map[uint16]*docValueReader // naive chunk cache per field
  90. fieldDvNames []string // field names cached in fieldDvReaders
  91. size uint64
  92. m sync.Mutex
  93. fieldFSTs map[uint16]*vellum.FST
  94. }
  95. func (sb *SegmentBase) Size() int {
  96. return int(sb.size)
  97. }
  98. func (sb *SegmentBase) updateSize() {
  99. sizeInBytes := reflectStaticSizeSegmentBase +
  100. cap(sb.mem)
  101. // fieldsMap
  102. for k := range sb.fieldsMap {
  103. sizeInBytes += (len(k) + SizeOfString) + SizeOfUint16
  104. }
  105. // fieldsInv, dictLocs
  106. for _, entry := range sb.fieldsInv {
  107. sizeInBytes += len(entry) + SizeOfString
  108. }
  109. sizeInBytes += len(sb.dictLocs) * SizeOfUint64
  110. // fieldDvReaders
  111. for _, v := range sb.fieldDvReaders {
  112. sizeInBytes += SizeOfUint16 + SizeOfPtr
  113. if v != nil {
  114. sizeInBytes += v.size()
  115. }
  116. }
  117. sb.size = uint64(sizeInBytes)
  118. }
  119. func (sb *SegmentBase) AddRef() {}
  120. func (sb *SegmentBase) DecRef() (err error) { return nil }
  121. func (sb *SegmentBase) Close() (err error) { return nil }
  122. // Segment implements a persisted segment.Segment interface, by
  123. // embedding an mmap()'ed SegmentBase.
  124. type Segment struct {
  125. SegmentBase
  126. f *os.File
  127. mm mmap.MMap
  128. path string
  129. version uint32
  130. crc uint32
  131. m sync.Mutex // Protects the fields that follow.
  132. refs int64
  133. }
  134. func (s *Segment) Size() int {
  135. // 8 /* size of file pointer */
  136. // 4 /* size of version -> uint32 */
  137. // 4 /* size of crc -> uint32 */
  138. sizeOfUints := 16
  139. sizeInBytes := (len(s.path) + SizeOfString) + sizeOfUints
  140. // mutex, refs -> int64
  141. sizeInBytes += 16
  142. // do not include the mmap'ed part
  143. return sizeInBytes + s.SegmentBase.Size() - cap(s.mem)
  144. }
  145. func (s *Segment) AddRef() {
  146. s.m.Lock()
  147. s.refs++
  148. s.m.Unlock()
  149. }
  150. func (s *Segment) DecRef() (err error) {
  151. s.m.Lock()
  152. s.refs--
  153. if s.refs == 0 {
  154. err = s.closeActual()
  155. }
  156. s.m.Unlock()
  157. return err
  158. }
  159. func (s *Segment) loadConfig() error {
  160. crcOffset := len(s.mm) - 4
  161. s.crc = binary.BigEndian.Uint32(s.mm[crcOffset : crcOffset+4])
  162. verOffset := crcOffset - 4
  163. s.version = binary.BigEndian.Uint32(s.mm[verOffset : verOffset+4])
  164. if s.version != Version {
  165. return fmt.Errorf("unsupported version %d", s.version)
  166. }
  167. chunkOffset := verOffset - 4
  168. s.chunkFactor = binary.BigEndian.Uint32(s.mm[chunkOffset : chunkOffset+4])
  169. docValueOffset := chunkOffset - 8
  170. s.docValueOffset = binary.BigEndian.Uint64(s.mm[docValueOffset : docValueOffset+8])
  171. fieldsIndexOffset := docValueOffset - 8
  172. s.fieldsIndexOffset = binary.BigEndian.Uint64(s.mm[fieldsIndexOffset : fieldsIndexOffset+8])
  173. storedIndexOffset := fieldsIndexOffset - 8
  174. s.storedIndexOffset = binary.BigEndian.Uint64(s.mm[storedIndexOffset : storedIndexOffset+8])
  175. numDocsOffset := storedIndexOffset - 8
  176. s.numDocs = binary.BigEndian.Uint64(s.mm[numDocsOffset : numDocsOffset+8])
  177. return nil
  178. }
  179. func (s *SegmentBase) loadFields() error {
  180. // NOTE for now we assume the fields index immediately precedes
  181. // the footer, and if this changes, need to adjust accordingly (or
  182. // store explicit length), where s.mem was sliced from s.mm in Open().
  183. fieldsIndexEnd := uint64(len(s.mem))
  184. // iterate through fields index
  185. var fieldID uint64
  186. for s.fieldsIndexOffset+(8*fieldID) < fieldsIndexEnd {
  187. addr := binary.BigEndian.Uint64(s.mem[s.fieldsIndexOffset+(8*fieldID) : s.fieldsIndexOffset+(8*fieldID)+8])
  188. dictLoc, read := binary.Uvarint(s.mem[addr:fieldsIndexEnd])
  189. n := uint64(read)
  190. s.dictLocs = append(s.dictLocs, dictLoc)
  191. var nameLen uint64
  192. nameLen, read = binary.Uvarint(s.mem[addr+n : fieldsIndexEnd])
  193. n += uint64(read)
  194. name := string(s.mem[addr+n : addr+n+nameLen])
  195. s.fieldsInv = append(s.fieldsInv, name)
  196. s.fieldsMap[name] = uint16(fieldID + 1)
  197. fieldID++
  198. }
  199. return nil
  200. }
  201. // Dictionary returns the term dictionary for the specified field
  202. func (s *SegmentBase) Dictionary(field string) (segment.TermDictionary, error) {
  203. dict, err := s.dictionary(field)
  204. if err == nil && dict == nil {
  205. return emptyDictionary, nil
  206. }
  207. return dict, err
  208. }
  209. func (sb *SegmentBase) dictionary(field string) (rv *Dictionary, err error) {
  210. fieldIDPlus1 := sb.fieldsMap[field]
  211. if fieldIDPlus1 > 0 {
  212. rv = &Dictionary{
  213. sb: sb,
  214. field: field,
  215. fieldID: fieldIDPlus1 - 1,
  216. }
  217. dictStart := sb.dictLocs[rv.fieldID]
  218. if dictStart > 0 {
  219. var ok bool
  220. sb.m.Lock()
  221. if rv.fst, ok = sb.fieldFSTs[rv.fieldID]; !ok {
  222. // read the length of the vellum data
  223. vellumLen, read := binary.Uvarint(sb.mem[dictStart : dictStart+binary.MaxVarintLen64])
  224. fstBytes := sb.mem[dictStart+uint64(read) : dictStart+uint64(read)+vellumLen]
  225. rv.fst, err = vellum.Load(fstBytes)
  226. if err != nil {
  227. sb.m.Unlock()
  228. return nil, fmt.Errorf("dictionary field %s vellum err: %v", field, err)
  229. }
  230. sb.fieldFSTs[rv.fieldID] = rv.fst
  231. }
  232. sb.m.Unlock()
  233. rv.fstReader, err = rv.fst.Reader()
  234. if err != nil {
  235. return nil, fmt.Errorf("dictionary field %s vellum reader err: %v", field, err)
  236. }
  237. }
  238. }
  239. return rv, nil
  240. }
  241. // visitDocumentCtx holds data structures that are reusable across
  242. // multiple VisitDocument() calls to avoid memory allocations
  243. type visitDocumentCtx struct {
  244. buf []byte
  245. reader bytes.Reader
  246. arrayPos []uint64
  247. }
  248. var visitDocumentCtxPool = sync.Pool{
  249. New: func() interface{} {
  250. reuse := &visitDocumentCtx{}
  251. return reuse
  252. },
  253. }
  254. // VisitStoredFields invokes the StoredFieldValueVisitor for each stored field
  255. // for the specified doc number
  256. func (s *SegmentBase) VisitStoredFields(num uint64, visitor segment.StoredFieldValueVisitor) error {
  257. vdc := visitDocumentCtxPool.Get().(*visitDocumentCtx)
  258. defer visitDocumentCtxPool.Put(vdc)
  259. return s.visitStoredFields(vdc, num, visitor)
  260. }
  261. func (s *SegmentBase) visitStoredFields(vdc *visitDocumentCtx, num uint64,
  262. visitor segment.StoredFieldValueVisitor) error {
  263. // first make sure this is a valid number in this segment
  264. if num < s.numDocs {
  265. meta, compressed := s.getDocStoredMetaAndCompressed(num)
  266. vdc.reader.Reset(meta)
  267. // handle _id field special case
  268. idFieldValLen, err := binary.ReadUvarint(&vdc.reader)
  269. if err != nil {
  270. return err
  271. }
  272. idFieldVal := compressed[:idFieldValLen]
  273. keepGoing := visitor("_id", byte('t'), idFieldVal, nil)
  274. if !keepGoing {
  275. visitDocumentCtxPool.Put(vdc)
  276. return nil
  277. }
  278. // handle non-"_id" fields
  279. compressed = compressed[idFieldValLen:]
  280. uncompressed, err := snappy.Decode(vdc.buf[:cap(vdc.buf)], compressed)
  281. if err != nil {
  282. return err
  283. }
  284. for keepGoing {
  285. field, err := binary.ReadUvarint(&vdc.reader)
  286. if err == io.EOF {
  287. break
  288. }
  289. if err != nil {
  290. return err
  291. }
  292. typ, err := binary.ReadUvarint(&vdc.reader)
  293. if err != nil {
  294. return err
  295. }
  296. offset, err := binary.ReadUvarint(&vdc.reader)
  297. if err != nil {
  298. return err
  299. }
  300. l, err := binary.ReadUvarint(&vdc.reader)
  301. if err != nil {
  302. return err
  303. }
  304. numap, err := binary.ReadUvarint(&vdc.reader)
  305. if err != nil {
  306. return err
  307. }
  308. var arrayPos []uint64
  309. if numap > 0 {
  310. if cap(vdc.arrayPos) < int(numap) {
  311. vdc.arrayPos = make([]uint64, numap)
  312. }
  313. arrayPos = vdc.arrayPos[:numap]
  314. for i := 0; i < int(numap); i++ {
  315. ap, err := binary.ReadUvarint(&vdc.reader)
  316. if err != nil {
  317. return err
  318. }
  319. arrayPos[i] = ap
  320. }
  321. }
  322. value := uncompressed[offset : offset+l]
  323. keepGoing = visitor(s.fieldsInv[field], byte(typ), value, arrayPos)
  324. }
  325. vdc.buf = uncompressed
  326. }
  327. return nil
  328. }
  329. // DocID returns the value of the _id field for the given docNum
  330. func (s *SegmentBase) DocID(num uint64) ([]byte, error) {
  331. if num >= s.numDocs {
  332. return nil, nil
  333. }
  334. vdc := visitDocumentCtxPool.Get().(*visitDocumentCtx)
  335. meta, compressed := s.getDocStoredMetaAndCompressed(num)
  336. vdc.reader.Reset(meta)
  337. // handle _id field special case
  338. idFieldValLen, err := binary.ReadUvarint(&vdc.reader)
  339. if err != nil {
  340. return nil, err
  341. }
  342. idFieldVal := compressed[:idFieldValLen]
  343. visitDocumentCtxPool.Put(vdc)
  344. return idFieldVal, nil
  345. }
  346. // Count returns the number of documents in this segment.
  347. func (s *SegmentBase) Count() uint64 {
  348. return s.numDocs
  349. }
  350. // DocNumbers returns a bitset corresponding to the doc numbers of all the
  351. // provided _id strings
  352. func (s *SegmentBase) DocNumbers(ids []string) (*roaring.Bitmap, error) {
  353. rv := roaring.New()
  354. if len(s.fieldsMap) > 0 {
  355. idDict, err := s.dictionary("_id")
  356. if err != nil {
  357. return nil, err
  358. }
  359. postingsList := emptyPostingsList
  360. sMax, err := idDict.fst.GetMaxKey()
  361. if err != nil {
  362. return nil, err
  363. }
  364. sMaxStr := string(sMax)
  365. filteredIds := make([]string, 0, len(ids))
  366. for _, id := range ids {
  367. if id <= sMaxStr {
  368. filteredIds = append(filteredIds, id)
  369. }
  370. }
  371. for _, id := range filteredIds {
  372. postingsList, err = idDict.postingsList([]byte(id), nil, postingsList)
  373. if err != nil {
  374. return nil, err
  375. }
  376. postingsList.OrInto(rv)
  377. }
  378. }
  379. return rv, nil
  380. }
  381. // Fields returns the field names used in this segment
  382. func (s *SegmentBase) Fields() []string {
  383. return s.fieldsInv
  384. }
  385. // Path returns the path of this segment on disk
  386. func (s *Segment) Path() string {
  387. return s.path
  388. }
  389. // Close releases all resources associated with this segment
  390. func (s *Segment) Close() (err error) {
  391. return s.DecRef()
  392. }
  393. func (s *Segment) closeActual() (err error) {
  394. if s.mm != nil {
  395. err = s.mm.Unmap()
  396. }
  397. // try to close file even if unmap failed
  398. if s.f != nil {
  399. err2 := s.f.Close()
  400. if err == nil {
  401. // try to return first error
  402. err = err2
  403. }
  404. }
  405. return
  406. }
  407. // some helpers i started adding for the command-line utility
  408. // Data returns the underlying mmaped data slice
  409. func (s *Segment) Data() []byte {
  410. return s.mm
  411. }
  412. // CRC returns the CRC value stored in the file footer
  413. func (s *Segment) CRC() uint32 {
  414. return s.crc
  415. }
  416. // Version returns the file version in the file footer
  417. func (s *Segment) Version() uint32 {
  418. return s.version
  419. }
  420. // ChunkFactor returns the chunk factor in the file footer
  421. func (s *Segment) ChunkFactor() uint32 {
  422. return s.chunkFactor
  423. }
  424. // FieldsIndexOffset returns the fields index offset in the file footer
  425. func (s *Segment) FieldsIndexOffset() uint64 {
  426. return s.fieldsIndexOffset
  427. }
  428. // StoredIndexOffset returns the stored value index offset in the file footer
  429. func (s *Segment) StoredIndexOffset() uint64 {
  430. return s.storedIndexOffset
  431. }
  432. // DocValueOffset returns the docValue offset in the file footer
  433. func (s *Segment) DocValueOffset() uint64 {
  434. return s.docValueOffset
  435. }
  436. // NumDocs returns the number of documents in the file footer
  437. func (s *Segment) NumDocs() uint64 {
  438. return s.numDocs
  439. }
  440. // DictAddr is a helper function to compute the file offset where the
  441. // dictionary is stored for the specified field.
  442. func (s *Segment) DictAddr(field string) (uint64, error) {
  443. fieldIDPlus1, ok := s.fieldsMap[field]
  444. if !ok {
  445. return 0, fmt.Errorf("no such field '%s'", field)
  446. }
  447. return s.dictLocs[fieldIDPlus1-1], nil
  448. }
  449. func (s *SegmentBase) loadDvReaders() error {
  450. if s.docValueOffset == fieldNotUninverted || s.numDocs == 0 {
  451. return nil
  452. }
  453. var read uint64
  454. for fieldID, field := range s.fieldsInv {
  455. var fieldLocStart, fieldLocEnd uint64
  456. var n int
  457. fieldLocStart, n = binary.Uvarint(s.mem[s.docValueOffset+read : s.docValueOffset+read+binary.MaxVarintLen64])
  458. if n <= 0 {
  459. return fmt.Errorf("loadDvReaders: failed to read the docvalue offset start for field %d", fieldID)
  460. }
  461. read += uint64(n)
  462. fieldLocEnd, n = binary.Uvarint(s.mem[s.docValueOffset+read : s.docValueOffset+read+binary.MaxVarintLen64])
  463. if n <= 0 {
  464. return fmt.Errorf("loadDvReaders: failed to read the docvalue offset end for field %d", fieldID)
  465. }
  466. read += uint64(n)
  467. fieldDvReader, err := s.loadFieldDocValueReader(field, fieldLocStart, fieldLocEnd)
  468. if err != nil {
  469. return err
  470. }
  471. if fieldDvReader != nil {
  472. s.fieldDvReaders[uint16(fieldID)] = fieldDvReader
  473. s.fieldDvNames = append(s.fieldDvNames, field)
  474. }
  475. }
  476. return nil
  477. }