File: text_table.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; 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,806; 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 (84 lines) | stat: -rw-r--r-- 3,651 bytes parent folder | download | duplicates (7)
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
// 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.

#ifndef CHROMEOS_ASH_COMPONENTS_FILE_MANAGER_INDEXING_TEXT_TABLE_H_
#define CHROMEOS_ASH_COMPONENTS_FILE_MANAGER_INDEXING_TEXT_TABLE_H_

#include <memory>
#include <optional>
#include <string>

#include "base/component_export.h"
#include "base/memory/raw_ptr.h"
#include "sql/database.h"
#include "sql/statement.h"

namespace ash::file_manager {

// A table that maintains a mapping from a unique value ID to the value text.
// This is the base class for two tables that are built on top of it: term
// table and URL table. This class is not meant to be used by itself.
class COMPONENT_EXPORT(FILE_MANAGER) TextTable {
 public:
  // Initializes the table. Returns true on success, and false on failure.
  bool Init();

 protected:
  // Creates a new table and passes the pointer to the SQL database to it. The
  // caller must make sure it owns both the sql::Database object and this table.
  // The caller also must make sure that the sql::Database outlives the table.
  TextTable(sql::Database* db, const std::string& table_name);
  virtual ~TextTable();

  TextTable(const TextTable&) = delete;
  TextTable& operator=(const TextTable&) = delete;

  // Deletes the given value from the table. Returns -1, if the value was not
  // found. Otherwise, returns the ID that the value was assigned.
  int64_t DeleteValue(const std::string& term);

  // Gets the value ID for the given value. If the value does not exists, this
  // method returns -1. Otherwise it returns the unique ID assigned to the
  // value.
  int64_t GetValueId(const std::string& value) const;

  // Gets or creates an ID for the given value. If the value does not exist,
  // it is inserted into the table and the unique key assigned to is returned.
  int64_t GetOrCreateValueId(const std::string& value);

  // Populates the value with the value for the given ID, if found. If not
  // found, returns -1 and leaves `value` unchanged.
  std::optional<std::string> GetValue(int64_t value_id) const;

  // Without changing the ID associated with the value, it changes it from
  // from string, to to string.
  int64_t ChangeValue(const std::string& from, const std::string& to);

  // To be overridden by the extending classes.

  // Returns a statement that gets text ID by value.
  virtual std::unique_ptr<sql::Statement> MakeGetValueIdStatement() const = 0;
  // Returns a statement that gets text value by its ID.
  virtual std::unique_ptr<sql::Statement> MakeGetValueStatement() const = 0;
  // Returns a statement that inserts text value into the table.
  virtual std::unique_ptr<sql::Statement> MakeInsertStatement() const = 0;
  // Returns a statement that deletes a value by its ID.
  virtual std::unique_ptr<sql::Statement> MakeDeleteStatement() const = 0;
  // Returns a statement that creates a table that maps text ID to text value.
  virtual std::unique_ptr<sql::Statement> MakeCreateTableStatement() const = 0;
  // Returns a statement that creates an index for the table; may return null.
  virtual std::unique_ptr<sql::Statement> MakeCreateIndexStatement() const = 0;
  // Returns a statement that changes a value to a new value.
  virtual std::unique_ptr<sql::Statement> MakeChangeValueStatement() const = 0;

  // The name of the concrete table built on top of this class.
  const std::string table_name_;

  // The pointer to a database owned by the whoever created this table.
  raw_ptr<sql::Database> db_;
};

}  // namespace ash::file_manager

#endif  // CHROMEOS_ASH_COMPONENTS_FILE_MANAGER_INDEXING_TEXT_TABLE_H_