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-used.h"
16
17 #include <cstdint>
18 #include <memory>
19
20 #include "icing/text_classifier/lib3/utils/base/statusor.h"
21 #include "icing/absl_ports/canonical_errors.h"
22 #include "icing/file/posting_list/posting-list-utils.h"
23 #include "icing/legacy/core/icing-string-util.h"
24 #include "icing/util/status-macros.h"
25
26 namespace icing {
27 namespace lib {
28
29 libtextclassifier3::StatusOr<PostingListUsed>
CreateFromPreexistingPostingListUsedRegion(PostingListSerializer * serializer,std::unique_ptr<uint8_t[]> posting_list_buffer,uint32_t size_in_bytes)30 PostingListUsed::CreateFromPreexistingPostingListUsedRegion(
31 PostingListSerializer* serializer,
32 std::unique_ptr<uint8_t[]> posting_list_buffer, uint32_t size_in_bytes) {
33 ICING_RETURN_ERROR_IF_NULL(serializer);
34 ICING_RETURN_ERROR_IF_NULL(posting_list_buffer);
35
36 if (!posting_list_utils::IsValidPostingListSize(
37 size_in_bytes, serializer->GetDataTypeBytes(),
38 serializer->GetMinPostingListSize())) {
39 return absl_ports::InvalidArgumentError(IcingStringUtil::StringPrintf(
40 "Requested posting list size %d is invalid!", size_in_bytes));
41 }
42 return PostingListUsed(std::move(posting_list_buffer), size_in_bytes);
43 }
44
45 libtextclassifier3::StatusOr<PostingListUsed>
CreateFromUnitializedRegion(PostingListSerializer * serializer,uint32_t size_in_bytes)46 PostingListUsed::CreateFromUnitializedRegion(PostingListSerializer* serializer,
47 uint32_t size_in_bytes) {
48 ICING_ASSIGN_OR_RETURN(
49 PostingListUsed posting_list_used,
50 CreateFromPreexistingPostingListUsedRegion(
51 serializer, std::make_unique<uint8_t[]>(size_in_bytes),
52 size_in_bytes));
53 serializer->Clear(&posting_list_used);
54 return posting_list_used;
55 }
56
57 } // namespace lib
58 } // namespace icing
59