xref: /aosp_15_r20/external/icing/icing/file/posting_list/posting-list-utils.cc (revision 8b6cd535a057e39b3b86660c4aa06c99747c2136)
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/file/posting_list/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,uint32_t data_type_bytes,uint32_t min_posting_list_size)25 bool IsValidPostingListSize(uint32_t size_in_bytes, uint32_t data_type_bytes,
26                             uint32_t min_posting_list_size) {
27   // size must be data_type_bytes aligned. Otherwise, we can have serious
28   // wasted space in the worst case.
29   if (size_in_bytes % data_type_bytes != 0) {
30     ICING_LOG(ERROR) << "Size " << size_in_bytes << " data " << data_type_bytes;
31     return false;
32   }
33 
34   // Must be able to store the min information.
35   if (size_in_bytes < min_posting_list_size) {
36     ICING_LOG(ERROR) << "Size " << size_in_bytes << " is less than min size "
37                      << min_posting_list_size;
38     return false;
39   }
40 
41   // We re-use the first two data as pointers into the posting list
42   // so the posting list size must fit in data_type_bytes.
43   if (BitsToStore(size_in_bytes) > data_type_bytes * 8) {
44     ICING_LOG(ERROR)
45         << "Posting list size must be small enough to store the offset in "
46         << data_type_bytes << " bytes.";
47     return false;
48   }
49 
50   return true;
51 }
52 
53 }  // namespace posting_list_utils
54 
55 }  // namespace lib
56 }  // namespace icing
57