File: context_database.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (265 lines) | stat: -rw-r--r-- 8,111 bytes parent folder | download | duplicates (4)
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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/file_system_provider/content_cache/context_database.h"

#include <memory>
#include <optional>
#include <sstream>

#include "base/sequence_checker.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "sql/statement.h"
#include "sql/transaction.h"

namespace ash::file_system_provider {

namespace {

static constexpr char kItemsCreateTableSql[] =
    // clang-format off
    "CREATE TABLE IF NOT EXISTS items ("
        "id INTEGER NOT NULL UNIQUE, "
        "fsp_path TEXT NOT NULL, "
        "version_tag TEXT NOT NULL, "
        "accessed_time INTEGER NOT NULL, "
        "UNIQUE(fsp_path, version_tag) ON CONFLICT REPLACE, "
        "PRIMARY KEY(id AUTOINCREMENT))";
// clang-format on

static constexpr char kInsertItemSql[] =
    // clang-format off
    "INSERT INTO items "
    "(fsp_path, version_tag, accessed_time) VALUES (?, ?, ?) "
    "RETURNING id";
// clang-format on

static constexpr char kSelectItemByIdSql[] =
    // clang-format off
    "SELECT fsp_path, version_tag, accessed_time FROM items WHERE id=? LIMIT 1";
// clang-format on

static constexpr char kSelectAllItemsSql[] =
    // clang-format off
    "SELECT id, fsp_path, version_tag, accessed_time FROM items";
// clang-format on

static constexpr char kUpdateAccessedTimeByIdSql[] =
    // clang-format off
    "UPDATE items SET accessed_time = ? WHERE id = ?";
// clang-format on

}  // namespace

ContextDatabase::ContextDatabase(const base::FilePath& db_path)
    : db_path_(db_path), db_(/*tag=*/"FSPContextDatabase") {
  // Can be constructed on any sequence, the first call to `Initialize` should
  // be made on the blocking task runner.
  DETACH_FROM_SEQUENCE(sequence_checker_);
}

// The current database version number.
constexpr int ContextDatabase::kCurrentVersionNumber = 1;

// The oldest version that is still compatible with `kCurrentVersionNumber`.
constexpr int ContextDatabase::kCompatibleVersionNumber = 1;

ContextDatabase::Item::Item(int64_t id,
                            const std::string& fsp_path,
                            const std::string& version_tag,
                            base::Time accessed_time)
    : id(id),
      fsp_path(fsp_path),
      version_tag(version_tag),
      accessed_time(accessed_time) {}

bool ContextDatabase::Initialize() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  // TODO(b/332636364): Once the logic for the database has landed, let's stop
  // removing the database on every `Initialize` call.
  if (!Raze()) {
    LOG(ERROR) << "Failed to remove old database";
    return false;
  }

  DCHECK(!db_.is_open()) << "Database is already open";

  if (db_path_.empty() && !db_.OpenInMemory()) {
    LOG(ERROR) << "In memory database initialization failed";
    return false;
  } else if (!db_path_.empty() && !db_.Open(db_path_)) {
    LOG(ERROR) << "Initialization of '" << db_path_ << "' failed";
    Raze();
    return false;
  }

  sql::Transaction committer(&db_);
  if (!committer.Begin()) {
    LOG(ERROR) << "Can't start SQL transaction";
    return false;
  }

  if (!db_.Execute(kItemsCreateTableSql)) {
    LOG(ERROR) << "Can't setup items table";
    Raze();
    return false;
  }

  if (!meta_table_.Init(&db_, kCurrentVersionNumber,
                        kCompatibleVersionNumber)) {
    Raze();
    return false;
  }

  return committer.Commit();
}

bool ContextDatabase::AddItem(const base::FilePath& fsp_path,
                              const std::string& version_tag,
                              base::Time accessed_time,
                              int64_t* inserted_id) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  if (fsp_path.empty() || version_tag.empty() || accessed_time.is_null()) {
    return false;
  }

  std::unique_ptr<sql::Statement> statement = std::make_unique<sql::Statement>(
      db_.GetCachedStatement(SQL_FROM_HERE, kInsertItemSql));

  statement->BindString(0, fsp_path.value());
  statement->BindString(1, version_tag);
  statement->BindInt64(2, accessed_time.InMillisecondsSinceUnixEpoch());
  if (!statement->Step()) {
    LOG(ERROR) << "Couldn't execute statement";
    return false;
  }

  *inserted_id = statement->ColumnInt64(0);
  return true;
}

std::unique_ptr<std::optional<ContextDatabase::Item>>
ContextDatabase::GetItemById(int64_t item_id) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  if (item_id < 0) {
    return nullptr;
  }

  std::unique_ptr<sql::Statement> statement = std::make_unique<sql::Statement>(
      db_.GetCachedStatement(SQL_FROM_HERE, kSelectItemByIdSql));
  if (!statement) {
    LOG(ERROR) << "Couldn't create SQL statement";
    return nullptr;
  }

  statement->BindInt64(0, item_id);
  if (!statement->Step()) {
    // In the event the `Step()` failed, this could simply mean there is no item
    // for the `item_id`. `Succeeded` will return true in this case.
    if (statement->Succeeded()) {
      return std::make_unique<std::optional<Item>>(std::nullopt);
    }
    LOG(ERROR) << "Couldn't execute statement";
    return nullptr;
  }

  return std::make_unique<std::optional<ContextDatabase::Item>>(Item(
      item_id,
      /*fsp_path=*/statement->ColumnString(0),
      /*version_tag=*/statement->ColumnString(1),
      /*accessed_time=*/
      base::Time::FromMillisecondsSinceUnixEpoch(statement->ColumnInt64(2))));
}

bool ContextDatabase::UpdateAccessedTime(int64_t item_id,
                                         base::Time new_accessed_time) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  std::unique_ptr<sql::Statement> statement = std::make_unique<sql::Statement>(
      db_.GetCachedStatement(SQL_FROM_HERE, kUpdateAccessedTimeByIdSql));
  if (!statement) {
    LOG(ERROR) << "Couldn't create SQL statement";
    return false;
  }

  statement->BindInt64(0, new_accessed_time.InMillisecondsSinceUnixEpoch());
  statement->BindInt64(1, item_id);
  return statement->Run();
}

ContextDatabase::IdToItemMap ContextDatabase::GetAllItems() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  std::unique_ptr<sql::Statement> statement = std::make_unique<sql::Statement>(
      db_.GetCachedStatement(SQL_FROM_HERE, kSelectAllItemsSql));
  if (!statement) {
    LOG(ERROR) << "Couldn't create SQL statement";
    return {};
  }

  std::map<int64_t, Item> items;
  while (statement->Step()) {
    items.try_emplace(
        statement->ColumnInt64(0), statement->ColumnInt64(0),
        statement->ColumnString(1), statement->ColumnString(2),
        base::Time::FromMillisecondsSinceUnixEpoch(statement->ColumnInt64(3)));
  }

  if (!statement->Succeeded()) {
    return {};
  }

  return items;
}

bool ContextDatabase::RemoveItemsByIds(std::vector<int64_t> item_ids) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  std::stringstream delete_in_clause;
  for (size_t i = 0; i < item_ids.size(); i++) {
    delete_in_clause << base::NumberToString(item_ids.at(i));
    if (i < item_ids.size() - 1) {
      delete_in_clause << "','";
    }
  }

  const std::string remove_items_by_id_sql = base::StrCat(
      {"DELETE FROM items WHERE id IN ('", delete_in_clause.str(), "')"});
  CHECK(db_.IsSQLValid(remove_items_by_id_sql));

  // TODO(b/341833149): Cache the statement.
  std::unique_ptr<sql::Statement> statement = std::make_unique<sql::Statement>(
      db_.GetUniqueStatement(remove_items_by_id_sql));
  if (!statement) {
    LOG(ERROR) << "Couldn't create SQL statement";
    return {};
  }

  return statement->Run();
}

base::WeakPtr<ContextDatabase> ContextDatabase::GetWeakPtr() {
  return weak_ptr_factory_.GetWeakPtr();
}

bool ContextDatabase::Raze() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  meta_table_.Reset();
  if (!db_.is_open()) {
    return true;
  }

  db_.Poison();
  return sql::Database::Delete(db_path_);
}

ContextDatabase::~ContextDatabase() = default;

}  // namespace ash::file_system_provider