File: table.h

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (121 lines) | stat: -rw-r--r-- 4,217 bytes parent folder | download
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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef SQL_RECOVER_MODULE_TABLE_H_
#define SQL_RECOVER_MODULE_TABLE_H_

#include <atomic>
#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "base/check_op.h"
#include "base/memory/raw_ptr.h"
#include "sql/recover_module/parsing.h"
#include "third_party/sqlite/sqlite3.h"

namespace sql {
namespace recover {

class VirtualCursor;

// Represents a virtual table created by CREATE VIRTUAL TABLE recover(...).
//
// Instances are allocated on the heap using the C++ new operator, and passed to
// SQLite via pointers to the sqlite_vtab members. SQLite is responsible for
// managing the instances' lifetimes. SQLite will call xDisconnect() for every
// successful xConnect(), and xDestroy() for every successful xCreate().
//
// Instances are thread-safe.
class VirtualTable {
 public:
  // Returns a SQLite status and a VirtualTable instance.
  //
  // The VirtualTable is non-null iff the status is SQLITE_OK.
  //
  // SQLite is trusted to keep |sqlite_db| alive for as long as this instance
  // lives.
  static std::pair<int, std::unique_ptr<VirtualTable>> Create(
      sqlite3* sqlite_db,
      TargetTableSpec backing_table_spec,
      std::vector<RecoveredColumnSpec> column_specs);

  // Use Create() instead of calling the constructor directly.
  explicit VirtualTable(sqlite3* sqlite_db,
                        sqlite3_file* sqlite_file,
                        int root_page,
                        int page_size,
                        std::vector<RecoveredColumnSpec> column_specs);
  ~VirtualTable();

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

  // Returns the embedded SQLite virtual table.
  //
  // This getter is not const because SQLite wants a non-const pointer to the
  // structure.
  sqlite3_vtab* SqliteTable() { return &sqlite_table_; }

  // Returns SQLite VFS file used to access the backing table's database.
  //
  // This getter is not const because it must return a non-const pointer.
  sqlite3_file* SqliteFile() { return sqlite_file_; }

  // Returns the page number of the root page for the table's B-tree.
  int root_page_id() const { return root_page_id_; }

  // Returns the page size used by the backing table's database.
  int page_size() const { return page_size_; }

  // Returns the schema of the corrupted table being recovered.
  const std::vector<RecoveredColumnSpec> column_specs() const {
    return column_specs_;
  }

  // Returns a SQL statement describing the virtual table's schema.
  //
  // The return value is suitable to be passed to sqlite3_declare_vtab().
  std::string ToCreateTableSql() const;

  // The VirtualTable instance that embeds a given SQLite virtual table.
  //
  // |sqlite_table| must have been returned by VirtualTable::SqliteTable().
  static inline VirtualTable* FromSqliteTable(sqlite3_vtab* sqlite_table) {
    static_assert(std::is_standard_layout<VirtualTable>::value,
                  "needed for the reinterpret_cast below");
    static_assert(offsetof(VirtualTable, sqlite_table_) == 0,
                  "sqlite_table_ must be the first member of the class");
    VirtualTable* const result = reinterpret_cast<VirtualTable*>(sqlite_table);
    DCHECK_EQ(sqlite_table, &result->sqlite_table_);
    return result;
  }

  // Creates a new cursor for iterating over this table.
  VirtualCursor* CreateCursor();

  // Called right before a cursor belonging to this table will be destroyed.
  void WillDeleteCursor(VirtualCursor* cursor);

 private:
  // SQLite handle for this table. The struct is populated and used by SQLite.
  sqlite3_vtab sqlite_table_;

  // See the corresponding getters for these members' purpose.
  const raw_ptr<sqlite3_file> sqlite_file_;
  const int root_page_id_;
  const int page_size_;
  const std::vector<RecoveredColumnSpec> column_specs_;

#if DCHECK_IS_ON()
  // Number of cursors that are still opened.
  std::atomic<int> open_cursor_count_{0};
#endif  // DCHECK_IS_ON()
};

}  // namespace recover
}  // namespace sql

#endif  // SQL_RECOVER_MODULE_TABLE_H_