diff --git a/kv_store/frameworks/libs/CMakeLists.txt b/kv_store/frameworks/libs/CMakeLists.txt index f6e7f30221a0aa47d847e12aeab5a53f6e451b4a..1265148084cc4946225bb6de552b3b4fa4483350 100644 --- a/kv_store/frameworks/libs/CMakeLists.txt +++ b/kv_store/frameworks/libs/CMakeLists.txt @@ -25,6 +25,7 @@ aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/distributeddb/storage/src/upgra aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/distributeddb/syncer/src distributeddbSrc) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/distributeddb/include) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/distributeddb/interfaces/include) +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/distributeddb/interfaces/include/cloud) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/distributeddb/interfaces/include/relational) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/distributeddb/interfaces/src) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/distributeddb/interfaces/src/relational) diff --git a/kv_store/frameworks/libs/distributeddb/include/query.h b/kv_store/frameworks/libs/distributeddb/include/query.h index 657e93df3eef3fa1a309822bddcece3649fb9ec0..1a1f1d90069712b001ae49572e157336b9209c95 100644 --- a/kv_store/frameworks/libs/distributeddb/include/query.h +++ b/kv_store/frameworks/libs/distributeddb/include/query.h @@ -33,6 +33,8 @@ public: DB_API static Query Select(); DB_API static Query Select(const std::string &tableName); + DB_API Query FromTable(const std::vector &tableNames); + template DB_API Query &EqualTo(const std::string &field, const T &value) { diff --git a/kv_store/frameworks/libs/distributeddb/interfaces/include/cloud/cloud_store_types.h b/kv_store/frameworks/libs/distributeddb/interfaces/include/cloud/cloud_store_types.h new file mode 100644 index 0000000000000000000000000000000000000000..7d54fa980a065c18a8e39e392562c09b791f48c2 --- /dev/null +++ b/kv_store/frameworks/libs/distributeddb/interfaces/include/cloud/cloud_store_types.h @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef CLOUD_STORE_TYPE_H +#define CLOUD_STORE_TYPE_H + +#include +#include +#include +#include +#include + +#include "store_types.h" + +namespace DistributedDB { +const std::string CLOUD_EXTEND_FIELD[] = { "GID_FIELD", "CREATE_FIELD", "MODIFY_FIELD" }; +const uint32_t MAX_UPLOAD_SIZE = 1024 * 1024 * 8; +enum TableSyncType { + DEVICE_COOPERATION = 0, + CLOUD_COOPERATION = 1, +}; + +enum ClearMode { + DATA_INCLUDE = 0, + FLAG_ONLY = 1, +}; + +struct Asset { + uint32_t version = 0; + std::string name; + std::string uri; + std::string modifyTime; + std::string createTime; + std::string size; + std::string hash; +}; + +using Assets = std::vector; +using Bytes = std::vector; +using CloudValue = std::variant; +using VBucket = std::map; + +enum ProcessStatus { + PREPARED = 0, + PROCESSING = 1, + FINISHED = 2, +}; + +struct Info { + uint32_t batchIndex = 0; + uint32_t total = 0; + uint32_t successCount = 0; // merge or upload success count + uint32_t failCount = 0; +}; + +struct TableProcessInfo { + ProcessStatus process = PREPARED; + Info downLoadInfo; + Info upLoadInfo; +}; + +struct SyncProcess { + ProcessStatus process = PREPARED; + DBStatus errCode = OK; + std::map tableProcess; +}; + +struct Field { + std::string colName; + int32_t type; // get value from TYPE_INDEX; + bool primary = false; + bool nullable = true; +}; + +struct TableSchema { + std::string name; + std::vector fields; +}; + +struct DataBaseSchema { + std::vector tables; +}; + +struct CloudSyncBatch { + std::vector record; + std::vector extend; + std::vector rowid; +}; + +struct CloudSyncData { + const std::string tablename; + CloudSyncBatch insData; + CloudSyncBatch updData; + CloudSyncBatch delData; +}; +} // namespace DistributedDB +#endif // CLOUD_STORE_TYPE_H diff --git a/kv_store/frameworks/libs/distributeddb/interfaces/include/cloud/icloud_data_translate.h b/kv_store/frameworks/libs/distributeddb/interfaces/include/cloud/icloud_data_translate.h new file mode 100644 index 0000000000000000000000000000000000000000..5604c0e4ef38e3cbf7b7714984ff430ec0c19fad --- /dev/null +++ b/kv_store/frameworks/libs/distributeddb/interfaces/include/cloud/icloud_data_translate.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ICLOUD_DATA_TRANSLATE_H +#define ICLOUD_DATA_TRANSLATE_H + +#include +#include +#include +#include +#include +#include "cloud_store_types.h" + +namespace DistributedDB { +class ICloudDataTranslate { +public: + virtual std::vector AssetToBlob(const Asset &asset) = 0; + virtual std::vector AssetsToBlob(const Assets &assets) = 0; + virtual Asset BlobToAsset(const std::vector &blob) = 0; + virtual Assets BlobToAssets(std::vector &blob) = 0; +}; +} // namespace DistributedDB + +#endif // ICLOUD_DATA_TRANSLATE_H diff --git a/kv_store/frameworks/libs/distributeddb/interfaces/include/cloud/icloud_db.h b/kv_store/frameworks/libs/distributeddb/interfaces/include/cloud/icloud_db.h new file mode 100644 index 0000000000000000000000000000000000000000..d9ca944511702535e721c0e1faee79a3c065d786 --- /dev/null +++ b/kv_store/frameworks/libs/distributeddb/interfaces/include/cloud/icloud_db.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ICLOUD_DB_H +#define ICLOUD_DB_H + +#include +#include +#include +#include +#include +#include "cloud_store_types.h" + +namespace DistributedDB { +class ICloudDb { +public: + /** + ** param[in & out] extend: will fill gid after insert ok + **/ + virtual DBStatus BatchInsert(const std::string &tableName, std::vector &&record, + std::vector &extend) = 0; + virtual DBStatus BatchUpdate(const std::string &tableName, std::vector &&record, + std::vector &extend) = 0; + virtual DBStatus BatchDelete(const std::string &tableName, std::vector &&record, + std::vector &extend) = 0; + /** + ** param[out] data: query data + **/ + virtual DBStatus Query(const std::string &tableName, VBucket &extend, std::vector &data) = 0; + virtual std::pair Lock() = 0; + virtual DBStatus UnLock() = 0; + virtual DBStatus HeartBeat() = 0; + virtual DBStatus Close() = 0; +}; +} // namespace DistributedDB + +#endif // ICLOUD_DB_H diff --git a/kv_store/frameworks/libs/distributeddb/interfaces/include/relational/relational_store_delegate.h b/kv_store/frameworks/libs/distributeddb/interfaces/include/relational/relational_store_delegate.h index 3405f055bf5096516f454fcfb0b29be32a295406..3e1714887e1d8b9a95318fb0bdcd40516b66f1cf 100644 --- a/kv_store/frameworks/libs/distributeddb/interfaces/include/relational/relational_store_delegate.h +++ b/kv_store/frameworks/libs/distributeddb/interfaces/include/relational/relational_store_delegate.h @@ -19,6 +19,9 @@ #include #include #include "distributeddb/result_set.h" +#include "cloud/cloud_store_types.h" +#include "cloud/icloud_db.h" +#include "cloud/icloud_data_translate.h" #include "query.h" #include "store_types.h" #include "store_observer.h" @@ -38,12 +41,13 @@ public: uint32_t iterateTimes = 0; }; - DB_API virtual DBStatus CreateDistributedTable(const std::string &tableName) = 0; + DB_API virtual DBStatus CreateDistributedTable(const std::string &tableName, + TableSyncType = DEVICE_COOPERATION) = 0; DB_API virtual DBStatus Sync(const std::vector &devices, SyncMode mode, const Query &query, const SyncStatusCallback &onComplete, bool wait) = 0; - DB_API virtual DBStatus RemoveDeviceData(const std::string &device) = 0; + DB_API virtual DBStatus RemoveDeviceData(const std::string &device, ClearMode = DATA_INCLUDE) = 0; DB_API virtual DBStatus RemoveDeviceData(const std::string &device, const std::string &tableName) = 0; @@ -53,6 +57,14 @@ public: // remove all device data DB_API virtual DBStatus RemoveDeviceData() = 0; + + DB_API virtual DBStatus Sync(const std::vector &devices, SyncMode mode, + const Query &query, const std::function &onProcess, + int64_t waitTime) = 0; + + DB_API virtual DBStatus SetCloudDB(const std::shared_ptr &cloudDb) = 0; + + DB_API virtual DBStatus SetCloudDbSchema(const DataBaseSchema &schema) = 0; }; } // namespace DistributedDB #endif // RELATIONAL_STORE_DELEGATE_H \ No newline at end of file diff --git a/kv_store/frameworks/libs/distributeddb/interfaces/include/store_observer.h b/kv_store/frameworks/libs/distributeddb/interfaces/include/store_observer.h index ea753a3ed89041ea0f3fb66859b0ecdea4a485e5..0228b2da9b1e7b04b97d9db7de2aac038f1798cb 100644 --- a/kv_store/frameworks/libs/distributeddb/interfaces/include/store_observer.h +++ b/kv_store/frameworks/libs/distributeddb/interfaces/include/store_observer.h @@ -17,14 +17,39 @@ #define STORE_OBSERVER_H #include "store_changed_data.h" +#include "cloud/cloud_store_types.h" namespace DistributedDB { + +enum ChangeType : uint32_t { + OP_INSERT = 0, + OP_UPDATE, + OP_DELETE, + OP_BUTT, +}; +struct ChangedData { + std::string tableName; + // CLOUD_COOPERATION mode, primaryData store primary keys + // primayData store row id if have no data + std::vector> primaryData[OP_BUTT]; + std::vector field; +}; + +enum Origin : int32_t { + ORIGIN_CLOUD, + ORIGIN_LOCAL, + ORIGIN_REMOTE, + ORIGIN_ALL, + ORIGIN_BUTT +}; class StoreObserver { public: virtual ~StoreObserver() {} // Data change callback virtual void OnChange(const StoreChangedData &data) = 0; + + virtual void OnChange(Origin origin, const std::string &originalId, ChangedData &&data) const {}; }; } // namespace DistributedDB diff --git a/kv_store/frameworks/libs/distributeddb/interfaces/src/relational/relational_store_delegate_impl.cpp b/kv_store/frameworks/libs/distributeddb/interfaces/src/relational/relational_store_delegate_impl.cpp index 02a18d3daa43e5827ea4150019b5bb7f2db54d54..d92004ed1f6ebfb5309ef846fd9c9fa80393af97 100644 --- a/kv_store/frameworks/libs/distributeddb/interfaces/src/relational/relational_store_delegate_impl.cpp +++ b/kv_store/frameworks/libs/distributeddb/interfaces/src/relational/relational_store_delegate_impl.cpp @@ -38,12 +38,12 @@ RelationalStoreDelegateImpl::~RelationalStoreDelegateImpl() conn_ = nullptr; }; -DBStatus RelationalStoreDelegateImpl::RemoveDeviceData(const std::string &device) +DBStatus RelationalStoreDelegateImpl::RemoveDeviceData(const std::string &device, ClearMode mode) { - return RemoveDeviceData(device, {}); + return RemoveDeviceData(device, ""); } -DBStatus RelationalStoreDelegateImpl::CreateDistributedTable(const std::string &tableName) +DBStatus RelationalStoreDelegateImpl::CreateDistributedTable(const std::string &tableName, TableSyncType type) { if (!ParamCheckUtils::CheckRelationalTableName(tableName)) { LOGE("invalid table name."); @@ -175,5 +175,21 @@ DBStatus RelationalStoreDelegateImpl::RemoveDeviceData() } return OK; } + +DBStatus RelationalStoreDelegateImpl::Sync(const std::vector &devices, SyncMode mode, const Query &query, + const std::function &onProcess, int64_t waitTime) +{ + return OK; +} + +DBStatus RelationalStoreDelegateImpl::SetCloudDB(const std::shared_ptr &cloudDb) +{ + return OK; +} + +DBStatus RelationalStoreDelegateImpl::SetCloudDbSchema(const DataBaseSchema &schema) +{ + return OK; +} } // namespace DistributedDB #endif \ No newline at end of file diff --git a/kv_store/frameworks/libs/distributeddb/interfaces/src/relational/relational_store_delegate_impl.h b/kv_store/frameworks/libs/distributeddb/interfaces/src/relational/relational_store_delegate_impl.h index a5a2a7373fcbf3ceecbf51859a753fbb4072e566..b3d0b1ee1b40860cb08353fc1042a138e31d890b 100644 --- a/kv_store/frameworks/libs/distributeddb/interfaces/src/relational/relational_store_delegate_impl.h +++ b/kv_store/frameworks/libs/distributeddb/interfaces/src/relational/relational_store_delegate_impl.h @@ -32,9 +32,9 @@ public: DBStatus Sync(const std::vector &devices, SyncMode mode, const Query &query, const SyncStatusCallback &onComplete, bool wait) override; - DBStatus RemoveDeviceData(const std::string &device) override; + DBStatus RemoveDeviceData(const std::string &device, ClearMode = DATA_INCLUDE) override; - DBStatus CreateDistributedTable(const std::string &tableName) override; + DBStatus CreateDistributedTable(const std::string &tableName, TableSyncType = DEVICE_COOPERATION) override; DBStatus RemoveDeviceData(const std::string &device, const std::string &tableName) override; @@ -48,6 +48,13 @@ public: DBStatus RemoveDeviceData() override; + DBStatus Sync(const std::vector &devices, SyncMode mode, const Query &query, + const std::function &onProcess, int64_t waitTime) override; + + DBStatus SetCloudDB(const std::shared_ptr &cloudDb) override; + + DBStatus SetCloudDbSchema(const DataBaseSchema &schema) override; + private: static void OnSyncComplete(const std::map> &devicesStatus, const SyncStatusCallback &onComplete);