我想用 C 對 google-cloud-spanner 進行編程。
我使用的 api-package 來自https://github.com/googleapis/google-cloud-cpp。
在第 1677ff 行的代碼https://github.com/googleapis/google-cloud-cpp/blob/main/google/cloud/spanner/samples/samples.cc中,可以從“InsertOrUpdateMutationBuilder”中看到“EmplaceRow”方法,或者從第 1739 行到 1743 行的不太簡單的“MakeInsertOrUpdateMutation”。
在
https://googleapis.dev/cpp/google-cloud-spanner/latest/namespacegoogle_1_1cloud_1_1spanner.html
中查看“EmplaceRow”和“MakeInsertOrUpdateMutation”的示例代碼看得到。更準確的你可以看到“EmplaceRow”示例代碼
https://googleapis.dev/cpp/google-cloud-spanner/latest/namespacegoogle_1_1cloud_1_1spanner.html#a3451b640c4ee19df694fa0539047fcdc和https://googleapis.dev/cpp/google-cloud-spanner/latest/namespacegoogle_1_1cloud_1_1spanner
上的“MakeInsertOrUpdateMutation”示例代碼
.html#aced68ba9bc789f44693ad29962fc6ed6
我的問題是:
如何創建一個變數(!)引數串列以將它們作為“Ts &&...值”傳遞到“EmplaceRow”或“MakeInsertOrUpdateMutation”中。
并且“引數的變數串列”意味著這個“串列”(不是 std::list)可以以上述方式用作“Ts && ... values”串列。
到目前為止,我做了很多尋找解決方案的作業,但沒有找到合適的解決方案:
- Two template functions by using a std::tuple to expand its tuple elements to the "Ts &&... values"-list.
- Two template functions by using a std::array to expand its array elements to the "Ts &&... values"-list.
Both solutions are "nice" but not useful!
I want process different tables with different number of columns during runtime but the size of std::tuple and std::array have to be fixed at compile-time (!).
Thus, creating a "Ts &&... values"-list during runtime can't be solved by std::tuple and std::array.
So ... how can I create from a std::vector, std::list, or other container-types, which could be created, resized and filled with "spanner::Value"-values, that "Ts &&... values"-list to pass it to "EmplaceRow" or "MakeInsertOrUpdateMutation".
Here is my code for expanding the std::tuple to an "Ts &&... values"-list:
template <typename Tuple, std::size_t... I>
int myInsrtOrUpdMutBldEmplaceRowBuild_Tup(confDataProc::processConfigData testA,
Tuple const& tuple,
std::index_sequence<I...>)
{
namespace spanner = ::google::cloud::spanner;
//std::cout << "the second 'myInsrtOrUpdMutBldEmplaceRowBuild_Tup'" << std::endl;
auto client = GoClSp::MakeSampleClient(testA.getProjectID(),
testA.getInstanceID(),
testA.getDatabaseID());
auto commit = client.Commit(spanner::Mutations{
spanner::InsertOrUpdateMutationBuilder(
testA.getTableName(),
testA.getColumnNamesOfTable())
.EmplaceRow(std::get<I>(tuple)...)
.Build()});
if (!commit)
{
throw std::runtime_error(commit.status().message());
}
return 0;
}
template <typename Tuple>
int myInsrtOrUpdMutBldEmplaceRowBuild_Tup(confDataProc::processConfigData testA,
Tuple const& tuple)
{
//std::cout << "the first 'myInsrtOrUpdMutBldEmplaceRowBuild_Tup'" << std::endl;
int iRetVal = myInsrtOrUpdMutBldEmplaceRowBuild_Tup(testA,
tuple,
std::make_index_sequence<std::tuple_size<Tuple>::value>());
return iRetVal;
}
This code works, but the big problem is that the size of the std::tuple must be known during compile time and can't be created during runtime with variable size/length. The size/length is in this context the number of columns of the processed table.
Maybe the solution is very obvious for other (better) programmers than me or maybe it is difficult. But anyway a helpful code snipped or a useful link is always welcome.
By the way:
In my experience this is very different to the 'classic' variadic arguments.
I have no possibility to change the behaviour of the method "EmplaceRow" or "MakeInsertOrUpdateMutation" - if I had I would like to change the "Ts &&... values"-list to a simpler "std::vectorspanner::Value values".
On the other hand:
If there is a other C function to insert or update table rows in a cloud-spanner table which I do not know, please inform me.
I "only" need a solution for insert or update a row of a table in C , I do not insist on the way described above.
uj5u.com熱心網友回復:
替換EmplaceRow為AddRow應該可以解決問題。
您無法在此處以實用的方式將運行時引數轉換為模板引數。(我無法向您展示如何做,但相信我,這里的計劃不好;我只是不說“不可能”,因為我不喜歡撒謊。)
我發現谷歌的 C 檔案需要愿意并且能夠閱讀原始標題,有時甚至是源代碼。在這里,我找到了EmplaceRow定義的類,并尋找它的作用或替代方法。
Emplace在 C 術語中的意思是“完美的存盤”;當模板妨礙時,附近通常有一個不完美的轉發方法(例如push_backvs emplace_back)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/437750.html
標籤:c c 11 c 17 c 14 google-cloud-spanner
上一篇:如何清除C 中未初始化的記憶體?
