Skip to content
Snippets Groups Projects
Commit f81f7d0c authored by Daniel Hornung's avatar Daniel Hornung
Browse files

ENH: Updating entities.

parent b906bc9b
No related branches found
No related tags found
1 merge request!3Full functionality of libcaosdb
Pipeline #12837 failed
...@@ -189,6 +189,47 @@ classdef Caosdb < handle ...@@ -189,6 +189,47 @@ classdef Caosdb < handle
end end
end end
%%
% Update an entity
%
% updated = Caosdb.update(entities)
%
% Parameters
% ----------
%
% entities: cell array
% A cell array with the entities to be updated.
%
% Returns
% -------
% updated : cell array
% The resulting updated entities.
function updated = update(obj, entities)
% Ensure that entities is a cell array.
assert(nargin == 2, "maox:InvalidArgument", "This method accepts exactly 1 argument.");
assert(iscell(entities), "maox:InvalidArgument", ...
"ENTITIES must be a cell array, was:\n%s", disp(entities));
assert(numel(entities) <= 1, "maox:NotImplementedError", ...
"Multi-update has not been implemented yet.");
% Convert the entities
mx_entities = struct("", {});
% disp("UPDATE")
for entity = entities
mx_entities(end + 1) = entity{1}.to_struct();
% disp(mx_entities(end))
end
assert(numel(mx_entities) == numel(entities));
% Create an run transaction
try
[updated, count_results] = maox_run_transaction(obj.connection, {}, {}, {}, mx_entities);
% disp(updated);
assert(count_results == -1, "maox:RuntimeError", "There should be no count results.");
catch
rethrow(lasterror());
end
end
%% %%
% Delete entities by IDs. % Delete entities by IDs.
% %
......
...@@ -147,6 +147,10 @@ classdef Entity < handle ...@@ -147,6 +147,10 @@ classdef Entity < handle
% Convert to a struct which has all the fields that may be needed for interaction with the % Convert to a struct which has all the fields that may be needed for interaction with the
% maoxdb library. % maoxdb library.
%
% If the datatype indicates a list value, the value is interpreted as such. It is an error if
% the value is list-like (cell string or more than one numeric element) while the datatype
% indicates a scalar value.
function struct_array = to_struct(obj) function struct_array = to_struct(obj)
res = struct(); res = struct();
% Scalars first % Scalars first
...@@ -157,9 +161,13 @@ classdef Entity < handle ...@@ -157,9 +161,13 @@ classdef Entity < handle
res.description = obj.description; res.description = obj.description;
res.datatype = obj.datatype_; res.datatype = obj.datatype_;
res.unit = obj.unit; res.unit = obj.unit;
res.value = obj.value; if (isempty(obj.datatype_))
if (isempty(res.value) && ~ischar(res.value)) warning(["Trying to transmit an Entity with value but without Datatype. List-iness is", ...
" unknown, so the value will be omitted."])
res.datatype = struct("dtypeName", "UNSPECIFIED", "isList", false, "isReference", false)
res.value = sparse([]); res.value = sparse([]);
else
res.value = maox_pack_value(obj.value, obj.datatype_.isList);
end end
% parents and properties % parents and properties
res.parents = cellfun(@(x)(x.to_struct()), obj.get_parents()); res.parents = cellfun(@(x)(x.to_struct()), obj.get_parents());
......
...@@ -61,7 +61,14 @@ classdef Property < handle ...@@ -61,7 +61,14 @@ classdef Property < handle
res.importance = obj.importance; res.importance = obj.importance;
res.datatype = obj.datatype_; res.datatype = obj.datatype_;
res.unit = obj.unit; res.unit = obj.unit;
res.value = obj.value; if (isempty(obj.datatype_))
warning(["Trying to transmit an Entity with value but without Datatype. List-iness is", ...
" unknown, so the value will be omitted."])
res.datatype = struct("dtypeName", "UNSPECIFIED", "isList", false, "isReference", false)
res.value = sparse([]);
else
res.value = maox_pack_value(obj.value, obj.datatype_.isList);
end
if (isempty(res.value) && ~ischar(res.value)) if (isempty(res.value) && ~ischar(res.value))
res.value = sparse([]); res.value = sparse([]);
end end
......
...@@ -414,8 +414,6 @@ auto entitiesFromMx(const mxArray *array, bool for_update, string conn_name) ...@@ -414,8 +414,6 @@ auto entitiesFromMx(const mxArray *array, bool for_update, string conn_name)
CAOSDB_LOG_TRACE(logger_name) << "numel results " << results.size(); CAOSDB_LOG_TRACE(logger_name) << "numel results " << results.size();
entities = std::vector<ce::Entity>(); entities = std::vector<ce::Entity>();
for (ce::Entity entity : results) { for (ce::Entity entity : results) {
ce::Entity foo = entity;
std::cout << foo.ToString() << std::endl;
auto index = idMap[entity.GetId()]; auto index = idMap[entity.GetId()];
auto remote_version = entity.GetVersionId(); auto remote_version = entity.GetVersionId();
auto local_version = mxGetStdString(mxGetField(array, index, "versionId")); auto local_version = mxGetStdString(mxGetField(array, index, "versionId"));
...@@ -512,28 +510,28 @@ auto valueFromMx(const mxArray *array) -> caosdb::entity::Value { ...@@ -512,28 +510,28 @@ auto valueFromMx(const mxArray *array) -> caosdb::entity::Value {
} }
array = mxGetCell(array, 0); array = mxGetCell(array, 0);
numel = mxGetNumberOfElements(array); numel = mxGetNumberOfElements(array);
if (mxIsCell(array)) { // string cell array if (mxIsCell(array)) { // string cell array
auto content = std::vector<string>(numel); auto content = std::vector<string>(numel);
for (size_t i = 0; i < numel; ++i) { for (size_t i = 0; i < numel; ++i) {
auto value = mxGetStdString(mxGetCell(array, i)); auto value = mxGetStdString(mxGetCell(array, i));
content[i] = value; content[i] = value;
} }
result = Value(content); result = Value(content);
} else if (mxIsLogical(array)) { // bool } else if (mxIsLogical(array)) { // bool
auto content = std::vector<bool>(numel); auto content = std::vector<bool>(numel);
auto data = static_cast<bool *>(mxGetData(array)); auto data = static_cast<bool *>(mxGetData(array));
for (size_t i = 0; i < numel; ++i) { for (size_t i = 0; i < numel; ++i) {
content[i] = data[i]; content[i] = data[i];
} }
result = Value(content); result = Value(content);
} else if (mxIsInt64(array)) { // int } else if (mxIsInt64(array)) { // int
auto content = std::vector<INT64_T>(numel); auto content = std::vector<INT64_T>(numel);
auto data = static_cast<INT64_T *>(mxGetData(array)); auto data = static_cast<INT64_T *>(mxGetData(array));
for (size_t i = 0; i < numel; ++i) { for (size_t i = 0; i < numel; ++i) {
content[i] = data[i]; content[i] = data[i];
} }
result = Value(content); result = Value(content);
} else if (mxIsDouble(array)) { // double } else if (mxIsDouble(array)) { // double
auto content = std::vector<double>(numel); auto content = std::vector<double>(numel);
auto data = static_cast<double *>(mxGetData(array)); auto data = static_cast<double *>(mxGetData(array));
for (size_t i = 0; i < numel; ++i) { for (size_t i = 0; i < numel; ++i) {
...@@ -545,15 +543,15 @@ auto valueFromMx(const mxArray *array) -> caosdb::entity::Value { ...@@ -545,15 +543,15 @@ auto valueFromMx(const mxArray *array) -> caosdb::entity::Value {
boost::lexical_cast<std::string>((mxGetClassID(array))) + ")"); boost::lexical_cast<std::string>((mxGetClassID(array))) + ")");
} }
} else { // scalar value (no cell array) } else { // scalar value (no cell array)
if (mxIsChar(array)) { // string cell array if (mxIsChar(array)) { // string
result = Value(mxGetStdString(array)); result = Value(mxGetStdString(array));
} else if (mxIsLogical(array)) { // bool } else if (mxIsLogical(array)) { // bool
auto data = *static_cast<bool *>(mxGetData(array)); auto data = *static_cast<bool *>(mxGetData(array));
result = Value(data); result = Value(data);
} else if (mxIsInt64(array)) { // int } else if (mxIsInt64(array)) { // int
auto data = *static_cast<INT64_T *>(mxGetData(array)); auto data = *static_cast<INT64_T *>(mxGetData(array));
result = Value(data); result = Value(data);
} else if (mxIsDouble(array)) { // double } else if (mxIsDouble(array)) { // double
auto data = *static_cast<double *>(mxGetData(array)); auto data = *static_cast<double *>(mxGetData(array));
result = Value(data); result = Value(data);
} else { } else {
......
...@@ -28,13 +28,22 @@ function entities = maox_convert_collection(collection) ...@@ -28,13 +28,22 @@ function entities = maox_convert_collection(collection)
ent = Entity(data); ent = Entity(data);
entities{end + 1} = ent; entities{end + 1} = ent;
if ent.has_errors() if ent.has_errors()
warning("caosdb:transactionError", ["Entity `" ent.id "` has error(s)."]); warning("caosdb:transactionError", ["Entity `" ent.id "` has error(s):"]);
for msg = ent.get_errors()
disp(msg{1})
end
end end
if ent.has_warnings() if ent.has_warnings()
warning("caosdb:transactionWarning", ["Entity `" ent.id "` has warning(s)."]); warning("caosdb:transactionWarning", ["Entity `" ent.id "` has warning(s):"]);
end for msg = ent.get_warnings()
if ent.has_infos() disp(msg{1})
warning("caosdb:transactionInfo", ["Entity `" ent.id "` has info(s)."]); end
end end
% if ent.has_infos()
% warning("caosdb:transactionInfo", ["Entity `" ent.id "` has info(s):"]);
% for msg = ent.get_infos()
% disp(msg{1})
% end
% end
end end
end end
...@@ -187,13 +187,15 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { ...@@ -187,13 +187,15 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
CAOSDB_LOG_TRACE(logger_name) << "ResultSet contents: "; CAOSDB_LOG_TRACE(logger_name) << "ResultSet contents: ";
CAOSDB_LOG_TRACE(logger_name) << "size: " << results.size(); CAOSDB_LOG_TRACE(logger_name) << "size: " << results.size();
for (const auto &result : results) { for (const auto &result : results) {
CAOSDB_LOG_TRACE(logger_name) << result.HasErrors(); CAOSDB_LOG_TRACE(logger_name) << "errors = " << result.HasErrors();
CAOSDB_LOG_TRACE(logger_name) << result.ToString(); CAOSDB_LOG_TRACE(logger_name) << result.ToString();
} }
auto *mxResults = maoxdb::mxFromResultSet(results); auto *mxResults = maoxdb::mxFromResultSet(results);
CAOSDB_LOG_TRACE(logger_name) << "results converted.";
auto *mxCountResults = maoxdb::mxScalarINT64(transaction->GetCountResult()); auto *mxCountResults = maoxdb::mxScalarINT64(transaction->GetCountResult());
CAOSDB_LOG_TRACE(logger_name) << "counts converted.";
plhs[0] = mxDuplicateArray(mxResults); plhs[0] = mxDuplicateArray(mxResults);
plhs[1] = mxCountResults; plhs[1] = mxCountResults;
CAOSDB_LOG_TRACE(logger_name) << std::endl; CAOSDB_LOG_TRACE(logger_name) << "\n---" << std::endl;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment