Files
ortools-clone/ortools/math_opt/cpp/update_tracker.cc

72 lines
2.4 KiB
C++
Raw Permalink Normal View History

2025-01-10 11:35:44 +01:00
// Copyright 2010-2025 Google LLC
2022-01-12 16:01:42 +01:00
// 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.
#include "ortools/math_opt/cpp/update_tracker.h"
#include <memory>
#include <optional>
2025-12-18 22:38:22 +01:00
#include "absl/log/die_if_null.h"
2022-03-02 22:10:23 +01:00
#include "absl/status/status.h"
#include "absl/status/statusor.h"
2022-01-12 16:01:42 +01:00
#include "ortools/base/logging.h"
#include "ortools/math_opt/model.pb.h"
2022-05-25 17:24:29 +02:00
#include "ortools/math_opt/storage/model_storage.h"
2022-01-12 16:01:42 +01:00
namespace operations_research {
namespace math_opt {
UpdateTracker::~UpdateTracker() {
const std::shared_ptr<ModelStorage> storage = storage_.lock();
// If the model has already been destroyed, the update tracker has been
// automatically cleaned.
if (storage != nullptr) {
storage->DeleteUpdateTracker(update_tracker_);
}
}
UpdateTracker::UpdateTracker(const std::shared_ptr<ModelStorage>& storage)
: storage_(ABSL_DIE_IF_NULL(storage)),
update_tracker_(storage->NewUpdateTracker()) {}
2022-03-02 22:10:23 +01:00
absl::StatusOr<std::optional<ModelUpdateProto>>
2023-09-01 14:01:19 +02:00
UpdateTracker::ExportModelUpdate(const bool remove_names) {
2022-01-12 16:01:42 +01:00
const std::shared_ptr<ModelStorage> storage = storage_.lock();
2022-03-02 22:10:23 +01:00
if (storage == nullptr) {
return absl::InvalidArgumentError(internal::kModelIsDestroyed);
}
2023-09-01 14:01:19 +02:00
return storage->ExportModelUpdate(update_tracker_, remove_names);
2022-01-12 16:01:42 +01:00
}
2022-09-05 18:30:25 +02:00
absl::Status UpdateTracker::AdvanceCheckpoint() {
2022-01-12 16:01:42 +01:00
const std::shared_ptr<ModelStorage> storage = storage_.lock();
2022-03-02 22:10:23 +01:00
if (storage == nullptr) {
return absl::InvalidArgumentError(internal::kModelIsDestroyed);
}
2022-09-05 18:30:25 +02:00
storage->AdvanceCheckpoint(update_tracker_);
2022-03-02 22:10:23 +01:00
return absl::OkStatus();
2022-01-12 16:01:42 +01:00
}
2023-09-01 14:01:19 +02:00
absl::StatusOr<ModelProto> UpdateTracker::ExportModel(
const bool remove_names) const {
2022-01-12 16:01:42 +01:00
const std::shared_ptr<ModelStorage> storage = storage_.lock();
2022-03-02 22:10:23 +01:00
if (storage == nullptr) {
return absl::InvalidArgumentError(internal::kModelIsDestroyed);
}
2023-09-01 14:01:19 +02:00
return storage->ExportModel(remove_names);
2022-01-12 16:01:42 +01:00
}
} // namespace math_opt
} // namespace operations_research