Medical Imaging Interaction Toolkit  2026.06.00
Medical Imaging Interaction Toolkit
mitkDataStorageBridge.h
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #ifndef mitkDataStorageBridge_h
14 #define mitkDataStorageBridge_h
15 
16 #include <mitkDataStorage.h>
17 #include <mitkWeakPointer.h>
18 #include <mitkBaseData.h>
19 #include <mitkNodePredicateBase.h>
21 #include <mitkNodeUidMapper.h>
22 #include <mitkNodeQueryParams.h>
23 
24 #include <nlohmann/json.hpp>
25 
26 #include <mutex>
27 #include <optional>
28 #include <string>
29 #include <memory>
30 #include <vector>
31 
32 #include <MitkRESTAPIExports.h>
33 
34 namespace mitk
35 {
75  {
76  public:
78 
80  static constexpr const char* INTERNAL_PROPERTY_PREFIX = "restapi.";
81 
88  static constexpr const char* MODIFIED_PROPERTY_KEY = "restapi.modified";
89 
91  static constexpr const char* LAST_MODIFICATION_PROPERTY_KEY = "restapi.lastmodification";
92 
95 
96  // Non-copyable, non-movable
101 
116 
122  void SetDataStorage(DataStorage* dataStorage);
123 
129  DataStorage::Pointer GetDataStorage() const;
130 
141  enum class OperationStatus
142  {
143  Success,
144  NodeNotFound,
145  PropertyNotFound,
146  InvalidInput,
147  InternalError
148  };
149 
155  bool HasDataStorage() const;
156 
166  std::string GetNodeUid (DataNode* node) const;
167 
168  // Node operations
169 
174  {
177  int limit;
178  int offset;
179  };
180 
188 
195  std::optional<Json> GetNode(const std::string& uid) const;
196 
201  {
202  bool success;
203  std::string uid;
204  std::vector<std::string> failedProperties;
205  };
206 
218  CreateNodeResult CreateNode(const Json& nodeData, const std::optional<std::string>& parentUid = std::nullopt);
219 
231  bool UpdateNode(const std::string& uid, const Json& updates);
232 
237  {
238  bool success;
239  std::string deletedUid;
240  std::vector<std::string> deletedChildren;
242  };
243 
251  DeleteResult DeleteNode(const std::string& uid, bool recursive = false);
252 
253  // Data operations
254 
259  {
260  bool nodeFound;
261  BaseData::Pointer data;
262  };
263 
281  GetNodeDataResult GetNodeData(const std::string& uid) const;
282 
296  DataNode::ConstPointer FindDataNode(const std::string& uid) const;
297 
313  OperationStatus SetNodeData(const std::string& uid, BaseData* data);
314 
315  // Property operations
316 
324  std::optional<Json> GetNodeProperties(const std::string& uid, const PropertyQueryParams& params = {}) const;
325 
338  std::optional<Json> GetNodeProperty(
339  const std::string& uid,
340  const std::string& key,
341  const PropertyQueryParams& params = {}) const;
342 
353  const std::string& uid,
354  const std::string& key,
355  const Json& value,
356  const PropertyQueryParams& params);
357 
367  const std::string& uid,
368  const std::string& key,
369  const PropertyQueryParams& params);
370 
375  {
376  OperationStatus status = OperationStatus::InternalError;
377  Json result = {};
378  };
379 
389  const std::string& uid,
390  const Json& properties,
391  const PropertyQueryParams& params);
392 
402  std::optional<Json> GetNodeAvailableContexts(const std::string& uid) const;
403 
404  private:
414  NodePredicateBase::Pointer BuildNodePredicate(const NodeQueryParams& params) const;
415 
416  Json NodeToJson(DataNode* node) const;
417  std::string BuildNodePath(const DataNode* node) const;
418  int GetChildrenCount(const DataNode* node) const;
419 
426  void DispatchTask(std::function<void()> task) const;
427 
434  template <typename R>
435  R DispatchTask(std::function<R()> task) const;
436 
437  WeakPointer<DataStorage> m_DataStorage;
439  std::unique_ptr<NodeUidMapper> m_UidMapper;
440 
443  mutable std::mutex m_Mutex;
444  };
445 }
446 
447 #endif
#define MITKRESTAPI_EXPORT
Base class of all data objects.
Definition: mitkBaseData.h:60
Central node class of the MITK data tree, associating a data object with properties and mappers.
Definition: mitkDataNode.h:72
Thread-safe bridge between the REST API layer and MITK DataStorage.
DeleteResult DeleteNode(const std::string &uid, bool recursive=false)
Delete a node.
DataStorageBridge(const DataStorageBridge &)=delete
std::optional< Json > GetNodeProperty(const std::string &uid, const std::string &key, const PropertyQueryParams &params={}) const
Get a single property of a node.
std::optional< Json > GetNodeProperties(const std::string &uid, const PropertyQueryParams &params={}) const
Get properties of a node with scope filtering.
void SetDataStorage(DataStorage *dataStorage)
Set the DataStorage to operate on.
DataStorage::Pointer GetDataStorage() const
Get the current DataStorage.
std::optional< Json > GetNodeAvailableContexts(const std::string &uid) const
Get available property contexts for a node.
NodeQueryResult GetNodes(const NodeQueryParams &params) const
Get nodes with full query parameter support.
OperationStatus SetNodeData(const std::string &uid, BaseData *data)
Set or replace the data on a node.
OperationStatus DeleteNodeProperty(const std::string &uid, const std::string &key, const PropertyQueryParams &params)
Delete a property from a node.
bool UpdateNode(const std::string &uid, const Json &updates)
Update a node.
DataStorageBridge(DataStorageBridge &&)=delete
ReplacePropertiesResult ReplaceNodeProperties(const std::string &uid, const Json &properties, const PropertyQueryParams &params)
Replace all properties on a node (PUT semantics).
DataNode::ConstPointer FindDataNode(const std::string &uid) const
Look up a DataNode by UID.
std::string GetNodeUid(DataNode *node) const
Get the UID for a DataNode.
OperationStatus SetNodeProperty(const std::string &uid, const std::string &key, const Json &value, const PropertyQueryParams &params)
Set a single property on a node.
bool HasDataStorage() const
Check if a DataStorage is connected.
CreateNodeResult CreateNode(const Json &nodeData, const std::optional< std::string > &parentUid=std::nullopt)
Create a new node.
DataStorageBridge & operator=(DataStorageBridge &&)=delete
DataStorageBridge & operator=(const DataStorageBridge &)=delete
GetNodeDataResult GetNodeData(const std::string &uid) const
Get a clone of the node's data for thread-safe processing.
OperationStatus
Status codes for mutation operations.
std::optional< Json > GetNode(const std::string &uid) const
Get a single node by UID.
void SetDispatcher(StorageThreadDispatcherBase *dispatcher)
Set the dispatcher for thread-safe DataStorage operations.
Abstract data management class that organizes DataNode objects in a directed acyclic graph.
Abstract base class for dispatching tasks to the thread that owns DataStorage.
A weak pointer that observes an itk::Object without preventing its deletion.
nlohmann::json json
Find image slices visible on a given plane.
Result of a node creation operation.
std::string uid
UID of the created node (empty if failed)
bool success
Whether the node was created.
std::vector< std::string > failedProperties
Property keys that failed to deserialize.
Result of a node deletion operation.
std::string deletedUid
UID of the deleted node.
std::vector< std::string > deletedChildren
UIDs of deleted children (if recursive)
int childrenCount
Number of children (for error reporting)
bool success
Whether deletion succeeded.
Result of a GetNodeData operation.
BaseData::Pointer data
Clone of the data, or nullptr if node has no data.
bool nodeFound
Whether the node with the given UID exists.
Result of a node query with pagination metadata.
int totalCount
Total number of matching nodes (before pagination)
Result of a ReplaceNodeProperties operation.
Query parameters for the GET /datastorage/nodes endpoint.
Query parameters for the GET /datastorage/nodes/{uid}/properties endpoint.