| 73 | // --------------------------------------------------------------------------- |
| 74 | |
| 75 | Expected<DatasetId> DataEngine::createDataset(DatasetDescriptor descriptor, DatasetId requested_id) { |
| 76 | std::unique_lock<std::recursive_mutex> lock(impl_->mutex_); |
| 77 | DatasetId id = requested_id != 0 ? requested_id : impl_->next_dataset_id; |
| 78 | if (requested_id != 0) { |
| 79 | if (impl_->datasets.find(requested_id) != impl_->datasets.end()) { |
| 80 | return PJ::unexpected(fmt::format("Dataset {} already exists", requested_id)); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | // Verify time domain exists if specified |
| 85 | if (descriptor.time_domain_id != 0) { |
| 86 | auto it = impl_->time_domains.find(descriptor.time_domain_id); |
| 87 | if (it == impl_->time_domains.end()) { |
| 88 | return PJ::unexpected(fmt::format("Time domain {} not found", descriptor.time_domain_id)); |
| 89 | } |
| 90 | } |
| 91 | if (requested_id != 0) { |
| 92 | if (impl_->next_dataset_id <= id) { |
| 93 | impl_->next_dataset_id = id + 1; |
| 94 | } |
| 95 | } else { |
| 96 | ++impl_->next_dataset_id; |
| 97 | } |
| 98 | |
| 99 | DatasetInfo info; |
| 100 | info.id = id; |
| 101 | info.source_name = std::move(descriptor.source_name); |
| 102 | if (descriptor.time_domain_id != 0) { |
| 103 | info.time_domain = impl_->time_domains.at(descriptor.time_domain_id); |
| 104 | } |
| 105 | impl_->datasets.emplace(id, std::move(info)); |
| 106 | return id; |
| 107 | } |
| 108 | |
| 109 | const DatasetInfo* DataEngine::getDataset(DatasetId id) const { |
| 110 | auto it = impl_->datasets.find(id); |