• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2019 Google LLC
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 
15 #include "icing/index/main/posting-list-utils.h"
16 
17 #include "icing/legacy/index/icing-bit-util.h"
18 #include "icing/util/logging.h"
19 
20 namespace icing {
21 namespace lib {
22 
23 namespace posting_list_utils {
24 
IsValidPostingListSize(uint32_t size_in_bytes)25 bool IsValidPostingListSize(uint32_t size_in_bytes) {
26   // size must be sizeof(Hit) aligned. Otherwise, we can have serious
27   // wasted space in the worst case.
28   if (size_in_bytes % sizeof(Hit) != 0) {
29     ICING_LOG(ERROR) << "Size " << size_in_bytes << " hit " << sizeof(Hit);
30     return false;
31   }
32 
33   // Must be able to store the min information.
34   if (size_in_bytes < min_posting_list_size()) {
35     ICING_LOG(ERROR) << "Size " << size_in_bytes << " is less than min size "
36                      << min_posting_list_size();
37     return false;
38   }
39 
40   // We re-use the first two hits as pointers into the posting list
41   // so the posting list size must fit in sizeof(Hit).
42   if (BitsToStore(size_in_bytes) > sizeof(Hit::Value) * 8) {
43     ICING_LOG(ERROR)
44         << "Posting list size must be small enough to store the offset in "
45         << sizeof(Hit::Value) * 8 << " bytes.";
46     return false;
47   }
48 
49   return true;
50 }
51 
52 }  // namespace posting_list_utils
53 
54 }  // namespace lib
55 }  // namespace icing
56