coding.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #include "header.h"
  19. #include "search_slice.h"
  20. // Borrowed from rocksdb or level db
  21. namespace wwsearch {
  22. /* Notice : This is the set of helper functions. Support for :
  23. * 1. encode&decode converts uint32/uint64 to string and reverse.
  24. * 2. Big-end/little-end keep in same.
  25. * 3. Variable length coding.
  26. */
  27. #define MAXVARINT64LENGTH 10
  28. namespace internal {
  29. void AppendFixed8(std::string& key, uint8_t i);
  30. void AppendFixed64(std::string& key, uint64_t i);
  31. void AppendBuffer(std::string& key, const char* buffer, uint32_t size);
  32. void RemoveFixed8(Slice& key, uint8_t& i);
  33. void RemoveFixed64(Slice& key, uint64_t& i);
  34. void RemoveBuffer(Slice& key, char* buffer, uint32_t size);
  35. } // namespace internal
  36. uint64_t ntohll(uint64_t i);
  37. uint64_t htonll(uint64_t i);
  38. void AppendFixed8(std::string& key, uint8_t i);
  39. void AppendFixed16(std::string& key, uint16_t i);
  40. void AppendFixed32(std::string& key, uint32_t i);
  41. void AppendFixed64(std::string& key, uint64_t i);
  42. void AppendBuffer(std::string& key, const char* buffer, uint32_t size);
  43. void RemoveFixed8(Slice& key, uint8_t& i);
  44. void RemoveFixed16(Slice& key, uint16_t& i);
  45. void RemoveFixed32(Slice& key, uint32_t& i);
  46. void RemoveFixed64(Slice& key, uint64_t& i);
  47. void RemoveBuffer(Slice& key, char* buffer, uint32_t size);
  48. const char* GetVarint32PtrFallback(const char* p, const char* limit,
  49. uint32_t* value);
  50. const char* GetVarint64Ptr(const char* p, const char* limit, uint64_t* value);
  51. const char* GetVarint32Ptr(const char* p, const char* limit, uint32_t* value);
  52. char* EncodeVarint32(char* dst, uint32_t v);
  53. char* EncodeVarint64(char* dst, uint64_t v);
  54. void PutVarint32(std::string* dst, uint32_t v);
  55. void PutVarint32Varint32(std::string* dst, uint32_t v1, uint32_t v2);
  56. void PutVarint32Varint32Varint32(std::string* dst, uint32_t v1, uint32_t v2,
  57. uint32_t v3);
  58. void PutVarint64(std::string* dst, uint64_t v);
  59. void PutVarint64Varint64(std::string* dst, uint64_t v1, uint64_t v2);
  60. void PutVarint32Varint64(std::string* dst, uint32_t v1, uint64_t v2);
  61. void PutVarint32Varint32Varint64(std::string* dst, uint32_t v1, uint32_t v2,
  62. uint64_t v3);
  63. bool GetVarint32(Slice* input, uint32_t* value);
  64. bool GetVarint64(Slice* input, uint64_t* value);
  65. } // namespace wwsearch