fix vcard import concurrent problem

Signed-off-by: mayuecheng <mayuecheng@huawei.com>
This commit is contained in:
mayuecheng 2024-11-13 01:31:00 +00:00
parent 4a8566a23a
commit d5c8f0539c
7 changed files with 18 additions and 22 deletions

View File

@ -633,7 +633,7 @@ HWTEST_F(ContactDataTest, VCardRdbHelper, Function | MediumTest | Level3)
{ {
VCardRdbHelper::GetInstance().SetDataHelper(nullptr); VCardRdbHelper::GetInstance().SetDataHelper(nullptr);
EXPECT_EQ(VCardRdbHelper::GetInstance().QueryRawContactMaxId(), DB_FAILD); EXPECT_EQ(VCardRdbHelper::GetInstance().QueryRawContactMaxId(0), DB_FAILD);
std::vector<DataShare::DataShareValuesBucket> rawContactValues; std::vector<DataShare::DataShareValuesBucket> rawContactValues;
OHOS::DataShare::DataShareValuesBucket rawContactValue; OHOS::DataShare::DataShareValuesBucket rawContactValue;

View File

@ -229,6 +229,7 @@ public:
static constexpr const char *ACCOUNT_ID = "account_id"; static constexpr const char *ACCOUNT_ID = "account_id";
static constexpr const char *CONTACT_ID = "contact_id"; static constexpr const char *CONTACT_ID = "contact_id";
static constexpr const char *IS_DELETED = "is_deleted"; static constexpr const char *IS_DELETED = "is_deleted";
static constexpr const char *PRIMARY_CONTACT = "primary_contact";
}; };
// contact_data // contact_data

View File

@ -43,7 +43,7 @@ public:
static void SetDataHelper(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper); static void SetDataHelper(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper);
static void Release(); static void Release();
static VCardRdbHelper &GetInstance(); static VCardRdbHelper &GetInstance();
static int32_t QueryRawContactMaxId(); static int32_t QueryRawContactMaxId(int32_t queryNum);
static int32_t BatchInsertRawContact(const std::vector<DataShare::DataShareValuesBucket> &rawContactValues); static int32_t BatchInsertRawContact(const std::vector<DataShare::DataShareValuesBucket> &rawContactValues);
static int32_t BatchInsertContactData(const std::vector<DataShare::DataShareValuesBucket> &contactsDataValues); static int32_t BatchInsertContactData(const std::vector<DataShare::DataShareValuesBucket> &contactsDataValues);
static int32_t QueryGroupId(std::string groupName); static int32_t QueryGroupId(std::string groupName);

View File

@ -113,8 +113,6 @@ void VCardContact::AddRemainDatas(std::string name, std::string rawValue, std::v
AddIms(name, rawValue, propValue, values, parasMap); AddIms(name, rawValue, propValue, values, parasMap);
} else if (name == VCARD_TYPE_X_GROUP) { } else if (name == VCARD_TYPE_X_GROUP) {
AddGroups(rawValue); AddGroups(rawValue);
} else {
TELEPHONY_LOGI("No need to do anything");
} }
} }

View File

@ -39,6 +39,7 @@ std::string VCardEncoder::ContructVCard(std::shared_ptr<DataShare::DataShareResu
std::vector<std::string> columns; std::vector<std::string> columns;
DataShare::DataSharePredicates predicates; DataShare::DataSharePredicates predicates;
predicates.EqualTo(RawContact::CONTACT_ID, id)->And()->EqualTo(RawContact::IS_DELETED, CONTACTS_NOT_DELETED); predicates.EqualTo(RawContact::CONTACT_ID, id)->And()->EqualTo(RawContact::IS_DELETED, CONTACTS_NOT_DELETED);
predicates.NotEqualTo(RawContact::PRIMARY_CONTACT, TELEPHONY_ERROR);
auto rawResultSet = VCardRdbHelper::GetInstance().QueryRawContact(columns, predicates); auto rawResultSet = VCardRdbHelper::GetInstance().QueryRawContact(columns, predicates);
if (rawResultSet == nullptr) { if (rawResultSet == nullptr) {
TELEPHONY_LOGE("QueryContactData failed"); TELEPHONY_LOGE("QueryContactData failed");

View File

@ -176,7 +176,7 @@ void VCardManager::BatchInsertContactDbAbility(int32_t accountId, int32_t &error
void VCardManager::BatchInsertRawContact( void VCardManager::BatchInsertRawContact(
int32_t accountId, uint32_t size, std::vector<int32_t> &rawIds, int32_t &errorCode) int32_t accountId, uint32_t size, std::vector<int32_t> &rawIds, int32_t &errorCode)
{ {
int32_t rawContactMaxId = VCardRdbHelper::GetInstance().QueryRawContactMaxId(); int32_t rawContactMaxId = VCardRdbHelper::GetInstance().QueryRawContactMaxId(static_cast<int32_t>(size));
std::vector<DataShare::DataShareValuesBucket> rawContactValues; std::vector<DataShare::DataShareValuesBucket> rawContactValues;
for (uint32_t i = 0; i < size; i++) { for (uint32_t i = 0; i < size; i++) {
OHOS::DataShare::DataShareValuesBucket valuesBucket; OHOS::DataShare::DataShareValuesBucket valuesBucket;
@ -184,8 +184,10 @@ void VCardManager::BatchInsertRawContact(
if (IsContactsIdExit(accountId)) { if (IsContactsIdExit(accountId)) {
valuesBucket.Put(RawContact::CONTACT_ID, accountId); valuesBucket.Put(RawContact::CONTACT_ID, accountId);
} }
int32_t newRawId = rawContactMaxId + i + 1;
valuesBucket.Put(RawContact::ID, newRawId);
rawContactValues.push_back(valuesBucket); rawContactValues.push_back(valuesBucket);
rawIds.push_back(rawContactMaxId + i + 1); rawIds.push_back(newRawId);
} }
VCardRdbHelper::GetInstance().BatchInsertRawContact(rawContactValues); VCardRdbHelper::GetInstance().BatchInsertRawContact(rawContactValues);
} }

View File

@ -25,8 +25,8 @@ OHOS::Uri uriRawContact("datashare:///com.ohos.contactsdataability/contacts/raw_
OHOS::Uri uriContactData("datashare:///com.ohos.contactsdataability/contacts/contact_data"); OHOS::Uri uriContactData("datashare:///com.ohos.contactsdataability/contacts/contact_data");
OHOS::Uri uriAccount("datashare:///com.ohos.contactsdataability/contacts/account"); OHOS::Uri uriAccount("datashare:///com.ohos.contactsdataability/contacts/account");
OHOS::Uri uriContact("datashare:///com.ohos.contactsdataability/contacts/contact"); OHOS::Uri uriContact("datashare:///com.ohos.contactsdataability/contacts/contact");
OHOS::Uri uriRawContactMaxId("datashare:///com.ohos.contactsdataability/raw_contact/get_inc_id");
OHOS::Uri uriGroup("datashare:///com.ohos.contactsdataability/contacts/groups"); OHOS::Uri uriGroup("datashare:///com.ohos.contactsdataability/contacts/groups");
OHOS::Uri uriRawContactMaxId("datashare:///com.ohos.contactsdataability/raw_contact/place_holder");
} // namespace } // namespace
@ -40,29 +40,23 @@ VCardRdbHelper &VCardRdbHelper::GetInstance()
return instance; return instance;
} }
int32_t VCardRdbHelper::QueryRawContactMaxId() int32_t VCardRdbHelper::QueryRawContactMaxId(int32_t queryNum)
{ {
if (dataShareHelper_ == nullptr) { if (dataShareHelper_ == nullptr) {
TELEPHONY_LOGE("dataShareHelper is nullptr"); TELEPHONY_LOGE("dataShareHelper is nullptr");
return DB_FAILD; return DB_FAILD;
} }
Uri uriRawContactMaxIdQuery(uriRawContactMaxId.ToString() + "?isFromBatch=true"); Uri uriRawContactMaxIdQuery(uriRawContactMaxId.ToString() + "?isFromBatch=true");
std::vector<std::string> columns; DataShare::DataShareValuesBucket dbQueryNum;
DataShare::DataSharePredicates predicates; dbQueryNum.Put("importContactNum", queryNum);
std::shared_ptr<DataShare::DataShareResultSet> resultSet = int32_t queryResult = dataShareHelper_->Insert(uriRawContactMaxIdQuery, dbQueryNum);
dataShareHelper_->Query(uriRawContactMaxIdQuery, predicates, columns); if (queryResult < 0) {
if (resultSet == nullptr) { TELEPHONY_LOGE("query RawContactMaxId failed %{public}d", queryResult);
TELEPHONY_LOGE("resultSet is nullptr");
return DB_FAILD; return DB_FAILD;
} }
int rawMaxId = 0; TELEPHONY_LOGW("batchInsert rawId: %{public}d", queryResult);
if (resultSet->GoToFirstRow() == TELEPHONY_ERR_SUCCESS) { return queryResult;
int curValueIndex;
resultSet->GetColumnIndex("seq", curValueIndex);
resultSet->GetInt(curValueIndex, rawMaxId);
}
TELEPHONY_LOGI("batchInsert rawId: %{public}d", rawMaxId);
return rawMaxId;
} }
int32_t VCardRdbHelper::BatchInsertRawContact(const std::vector<DataShare::DataShareValuesBucket> &rawContactValues) int32_t VCardRdbHelper::BatchInsertRawContact(const std::vector<DataShare::DataShareValuesBucket> &rawContactValues)