File: segment_info_database.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (275 lines) | stat: -rw-r--r-- 10,132 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/segmentation_platform/internal/database/segment_info_database.h"

#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "base/task/single_thread_task_runner.h"
#include "components/segmentation_platform/internal/logging.h"
#include "components/segmentation_platform/internal/stats.h"

#include <string>

namespace segmentation_platform {

namespace {

std::string ToString(SegmentId segment_id, ModelSource model_source) {
  std::string prefix =
      (model_source == ModelSource::DEFAULT_MODEL_SOURCE ? "DEFAULT_" : "");
  return prefix + base::NumberToString(static_cast<int>(segment_id));
}

ModelSource GetModelSource(ModelSource model_source) {
  // If model source is not set in some segment info present in database, we
  // consider it to be from server models.
  if (model_source == ModelSource::UNKNOWN_MODEL_SOURCE) {
    model_source = ModelSource::SERVER_MODEL_SOURCE;
  }
  return model_source;
}

}  // namespace

SegmentInfoDatabase::SegmentInfoDatabase(
    std::unique_ptr<SegmentInfoProtoDb> database,
    std::unique_ptr<SegmentInfoCache> cache)
    : database_(std::move(database)), cache_(std::move(cache)) {}

SegmentInfoDatabase::~SegmentInfoDatabase() = default;

void SegmentInfoDatabase::Initialize(SuccessCallback callback) {
  database_->Init(
      leveldb_proto::CreateSimpleOptions(),
      base::BindOnce(&SegmentInfoDatabase::OnDatabaseInitialized,
                     weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}

std::unique_ptr<SegmentInfoDatabase::SegmentInfoList>
SegmentInfoDatabase::GetSegmentInfoForBothModels(
    const base::flat_set<SegmentId>& segment_ids) {
  return cache_->GetSegmentInfoForBothModels(segment_ids);
}

void SegmentInfoDatabase::GetSegmentInfoForSegments(
    const base::flat_set<SegmentId>& segment_ids,
    MultipleSegmentInfoCallback callback) {
  auto segments_found = cache_->GetSegmentInfoForSegments(
      segment_ids, ModelSource::SERVER_MODEL_SOURCE);

  base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE,
      base::BindOnce(std::move(callback), std::move(segments_found)));
}

void SegmentInfoDatabase::GetSegmentInfo(SegmentId segment_id,
                                         proto::ModelSource model_source,
                                         SegmentInfoCallback callback) {
  std::optional<SegmentInfo> result;
  const auto* cached = GetCachedSegmentInfo(segment_id, model_source);
  if (cached) {
    result = *cached;
  }
  std::move(callback).Run(std::move(result));
}

const SegmentInfo* SegmentInfoDatabase::GetCachedSegmentInfo(
    SegmentId segment_id,
    proto::ModelSource model_source) {
  return cache_->GetSegmentInfo(segment_id, model_source);
}

void SegmentInfoDatabase::GetTrainingData(SegmentId segment_id,
                                          ModelSource model_source,
                                          TrainingRequestId request_id,
                                          bool delete_from_db,
                                          TrainingDataCallback callback) {
  std::optional<proto::TrainingData> result;
  const auto* cached = cache_->GetSegmentInfo(segment_id, model_source);

  // Ignore results if the metadata no longer exists.
  if (!cached) {
    std::move(callback).Run(std::move(result));
    return;
  }
  SegmentInfo segment_info = *cached;  // Make a copy for update.

  for (int i = 0; i < segment_info.training_data_size(); i++) {
    if (segment_info.training_data(i).request_id() ==
        request_id.GetUnsafeValue()) {
      result = segment_info.training_data(i);
      break;
    }
  }

  if (delete_from_db) {
    // Delete the training data from cache and then post update to delete from
    // database.
    for (int i = 0; i < segment_info.training_data_size(); i++) {
      if (segment_info.training_data(i).request_id() ==
          request_id.GetUnsafeValue()) {
        segment_info.mutable_training_data()->DeleteSubrange(i, 1);
      }
    }
    UpdateSegment(segment_id, model_source, std::move(segment_info),
                  base::DoNothing());
  }

  // Notify the client with the result.
  std::move(callback).Run(std::move(result));
}

void SegmentInfoDatabase::UpdateSegment(
    SegmentId segment_id,
    ModelSource model_source,
    std::optional<proto::SegmentInfo> segment_info,
    SuccessCallback callback) {
  model_source = GetModelSource(model_source);
  cache_->UpdateSegmentInfo(segment_id, model_source, segment_info);

  // The cache has been updated now. We can notify the client synchronously.
  std::move(callback).Run(/*success=*/true);

  // Now write to the database asyncrhonously.
  auto entries_to_save = std::make_unique<
      std::vector<std::pair<std::string, proto::SegmentInfo>>>();
  auto keys_to_delete = std::make_unique<std::vector<std::string>>();
  if (segment_info.has_value()) {
    segment_info->set_model_source(model_source);
    entries_to_save->emplace_back(std::make_pair(
        ToString(segment_id, model_source), std::move(segment_info.value())));
  } else {
    keys_to_delete->emplace_back(ToString(segment_id, model_source));
  }
  database_->UpdateEntries(
      std::move(entries_to_save), std::move(keys_to_delete),
      base::BindOnce(&stats::RecordSegmentInfoDatabaseUpdateEntriesResult,
                     segment_id));
}

void SegmentInfoDatabase::UpdateMultipleSegments(
    const SegmentInfoList& segments_to_update,
    const std::vector<std::pair<proto::SegmentId, ModelSource>>&
        segments_to_delete,
    SuccessCallback callback) {
  auto entries_to_save = std::make_unique<
      std::vector<std::pair<std::string, proto::SegmentInfo>>>();
  auto entries_to_delete = std::make_unique<std::vector<std::string>>();
  for (auto& segment : segments_to_update) {
    const proto::SegmentId segment_id = segment.first;
    const auto* segment_info = segment.second;
    ModelSource model_source = GetModelSource(segment_info->model_source());
    // Updating the cache.
    cache_->UpdateSegmentInfo(segment_id, model_source, *segment_info);

    // Determining entries to save for database.
    proto::SegmentInfo copy = *segment_info;
    if (!copy.has_model_source()) {
      copy.set_model_source(model_source);
    }
    entries_to_save->emplace_back(
        std::make_pair(ToString(segment_id, model_source), std::move(copy)));
  }

  // The cache has been updated now. We can notify the client synchronously.
  std::move(callback).Run(/*success=*/true);

  // TODO (ritikagup@) : Add handling for default models, if required.
  // Now write to the database asyncrhonously.
  for (auto& segment_id_and_model_source : segments_to_delete) {
    entries_to_delete->emplace_back(ToString(
        segment_id_and_model_source.first, segment_id_and_model_source.second));
  }

  database_->UpdateEntries(std::move(entries_to_save),
                           std::move(entries_to_delete), base::DoNothing());
}

void SegmentInfoDatabase::SaveSegmentResult(
    SegmentId segment_id,
    ModelSource model_source,
    std::optional<proto::PredictionResult> result,
    SuccessCallback callback) {
  const auto* cached = cache_->GetSegmentInfo(segment_id, model_source);

  // Ignore results if the metadata no longer exists.
  if (!cached) {
    std::move(callback).Run(false);
    return;
  }
  SegmentInfo segment_info = *cached;  // Make a copy for update.

  // Update results.
  if (result.has_value()) {
    VLOG(1) << "SaveSegmentResult: saving: "
            << segmentation_platform::PredictionResultToDebugString(
                   result.value())
            << " for segment id: " << proto::SegmentId_Name(segment_id);
    segment_info.mutable_prediction_result()->Swap(&(*result));
  } else {
    VLOG(1) << "SaveSegmentResult: clearing prediction result for segment "
            << proto::SegmentId_Name(segment_id);
    segment_info.clear_prediction_result();
  }

  UpdateSegment(segment_id, model_source, std::move(segment_info),
                std::move(callback));
}

void SegmentInfoDatabase::SaveTrainingData(SegmentId segment_id,
                                           ModelSource model_source,
                                           const proto::TrainingData& data,
                                           SuccessCallback callback) {
  const auto* cached = cache_->GetSegmentInfo(segment_id, model_source);

  // Ignore results if the metadata no longer exists.
  if (!cached) {
    std::move(callback).Run(false);
    return;
  }
  SegmentInfo segment_info = *cached;  // Make a copy for update.

  // Update training data.
  segment_info.add_training_data()->CopyFrom(data);

  UpdateSegment(segment_id, model_source, std::move(segment_info),
                std::move(callback));
}

void SegmentInfoDatabase::OnDatabaseInitialized(
    SuccessCallback callback,
    leveldb_proto::Enums::InitStatus status) {
  bool success = (status == leveldb_proto::Enums::InitStatus::kOK);

  if (!success) {
    std::move(callback).Run(success);
    return;
  }

  // Initialize the cache by reading the database into the in-memory cache to
  // be accessed hereafter.
  database_->LoadEntries(base::BindOnce(&SegmentInfoDatabase::OnLoadAllEntries,
                                        weak_ptr_factory_.GetWeakPtr(),
                                        std::move(callback)));
}

void SegmentInfoDatabase::OnLoadAllEntries(
    SuccessCallback callback,
    bool success,
    std::unique_ptr<std::vector<proto::SegmentInfo>> all_infos) {
  if (success) {
    // Add all the entries to the cache on startup.
    for (auto& info : *all_infos.get()) {
      ModelSource model_source = GetModelSource(info.model_source());
      proto::SegmentId segment_id = info.segment_id();
      cache_->UpdateSegmentInfo(segment_id, model_source, std::move(info));
    }
  }
  std::move(callback).Run(success);
}

}  // namespace segmentation_platform