1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
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 #define LOG_TAG "DataVBuckets"
16 #include "oh_data_values_buckets.h"
17 #include "relational_store_error_code.h"
18 #include "relational_values_bucket.h"
19 #include "oh_data_define.h"
20 #include "logger.h"
21
22 using namespace OHOS::RdbNdk;
23
IsValidVBuckets(const OH_Data_VBuckets * vbuckets)24 static bool IsValidVBuckets(const OH_Data_VBuckets *vbuckets)
25 {
26 if (vbuckets == nullptr) {
27 LOG_ERROR("vbuckets is null.");
28 return false;
29 }
30 bool ret = vbuckets->IsValid();
31 if (!ret) {
32 LOG_ERROR("invalid data value buckets object.");
33 }
34 return ret;
35 }
36
OH_VBuckets_Create(void)37 OH_Data_VBuckets *OH_VBuckets_Create(void)
38 {
39 OH_Data_VBuckets *vbuckets = new (std::nothrow) OH_Data_VBuckets;
40 if (vbuckets == nullptr) {
41 LOG_ERROR("create vbuckets fail.");
42 }
43 return vbuckets;
44 }
45
OH_VBuckets_Destroy(OH_Data_VBuckets * buckets)46 int OH_VBuckets_Destroy(OH_Data_VBuckets *buckets)
47 {
48 if (!IsValidVBuckets(buckets)) {
49 return RDB_E_INVALID_ARGS;
50 }
51 delete buckets;
52 return RDB_OK;
53 }
54
OH_VBuckets_PutRow(OH_Data_VBuckets * buckets,const OH_VBucket * row)55 int OH_VBuckets_PutRow(OH_Data_VBuckets *buckets, const OH_VBucket *row)
56 {
57 if (!IsValidVBuckets(buckets) || RelationalValuesBucket::GetSelf(const_cast<OH_VBucket *>(row)) == nullptr) {
58 return RDB_E_INVALID_ARGS;
59 }
60 buckets->rows_.push_back(const_cast<OH_VBucket *>(row));
61 return RDB_OK;
62 }
63
OH_VBuckets_PutRows(OH_Data_VBuckets * buckets,const OH_Data_VBuckets * rows)64 int OH_VBuckets_PutRows(OH_Data_VBuckets *buckets, const OH_Data_VBuckets *rows)
65 {
66 if (!IsValidVBuckets(buckets) || !IsValidVBuckets(rows)) {
67 return RDB_E_INVALID_ARGS;
68 }
69 buckets->rows_.insert(buckets->rows_.end(), rows->rows_.begin(), rows->rows_.end());
70 return RDB_OK;
71 }
72
OH_VBuckets_RowCount(OH_Data_VBuckets * buckets,size_t * count)73 int OH_VBuckets_RowCount(OH_Data_VBuckets *buckets, size_t *count)
74 {
75 if (!IsValidVBuckets(buckets) || count == nullptr) {
76 return RDB_E_INVALID_ARGS;
77 }
78 *count = buckets->rows_.size();
79 return RDB_OK;
80 }
81
IsValid() const82 bool OH_Data_VBuckets::IsValid() const
83 {
84 return id == OH_VBUCKETS_ID;
85 }