codec_doclist_impl.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Tencent is pleased to support the open source community by making wwsearch
  3. * available.
  4. *
  5. * Copyright (C) 2018-present Tencent. All Rights Reserved.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  8. * use this file except in compliance with the License. You may obtain a copy of
  9. * the License at
  10. *
  11. * https://opensource.org/licenses/Apache-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OF ANY KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations under the License.
  17. */
  18. #pragma once
  19. #include <memory>
  20. #include "codec.h"
  21. #include "codec_doclist.h"
  22. #include "doclist_compression.h"
  23. #include "storage_type.h"
  24. namespace wwsearch {
  25. typedef struct DocListHeader DocListHeader;
  26. // Attetion : use this DocListWriterCodec must insert DocID in order.
  27. class DocListOrderWriterCodecImpl : public DocListWriterCodec {
  28. private:
  29. std::unique_ptr<std::string> data_;
  30. public:
  31. DocListOrderWriterCodecImpl() : data_(new std::string) {
  32. DocListHeader header;
  33. assert(sizeof(header) == 1);
  34. AppendBuffer(*data_, (char*)&header, sizeof(header));
  35. }
  36. virtual ~DocListOrderWriterCodecImpl() {}
  37. // Must keep order doc_id < previous doc_id which added
  38. virtual void AddDocID(const DocumentID& doc_id, DocumentState state) override;
  39. virtual std::string DebugString() override;
  40. virtual bool SerializeToBytes(std::string& buffer, int mode = 0) override;
  41. virtual bool DeSerializeFromByte(const char* buffer,
  42. uint32_t buffer_len) override;
  43. private:
  44. };
  45. // Attetion : use this DocListWriterCodec must insert DocID in order.
  46. class CompressionWriterCodecImpl : public DocListWriterCodec {
  47. private:
  48. DocListCompressionEncoder encoder_;
  49. public:
  50. CompressionWriterCodecImpl() {}
  51. virtual ~CompressionWriterCodecImpl() {}
  52. // Must keep order doc_id < previous doc_id which added
  53. virtual void AddDocID(const DocumentID& doc_id, DocumentState state) override;
  54. virtual std::string DebugString() {
  55. // not support
  56. return std::string("NOT SUPPORT COMPRESSION ENCODED!!!");
  57. }
  58. virtual bool SerializeToBytes(std::string& buffer, int mode = 0) override;
  59. virtual bool DeSerializeFromByte(const char* buffer, uint32_t buffer_len) {
  60. assert(false);
  61. return false;
  62. }
  63. private:
  64. };
  65. class CodecImpl;
  66. class DocListReaderCodecImpl : public DocListReaderCodec {
  67. private:
  68. Slice slice_;
  69. size_t pos_;
  70. // for compression
  71. std::string buffer_;
  72. int field_id_;
  73. public:
  74. friend class CodecImpl;
  75. friend class DocListOrderWriterCodecImpl;
  76. virtual ~DocListReaderCodecImpl();
  77. virtual DocumentID DocID() override;
  78. virtual DocumentID NextDoc() override;
  79. virtual DocumentID Advance(DocumentID target) override;
  80. virtual CostType Cost() override;
  81. virtual DocumentState& State() override;
  82. virtual int FieldId() override { return field_id_; };
  83. private:
  84. DocListReaderCodecImpl(const char* data, size_t data_len, int field_id = -1);
  85. };
  86. // Attetion : must place doc list in decrease order
  87. inline bool DocListReaderCodecImplGreater(DocListReaderCodec* left,
  88. DocListReaderCodec* right) {
  89. assert(left->GetPriority() != right->GetPriority());
  90. if (left->DocID() > right->DocID())
  91. return false;
  92. else if (left->DocID() < right->DocID())
  93. return true;
  94. return left->GetPriority() < right->GetPriority();
  95. }
  96. } // namespace wwsearch