File: leveldb_scopes_tasks.h

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 (120 lines) | stat: -rw-r--r-- 4,110 bytes parent folder | download | duplicates (8)
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
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_SERVICES_STORAGE_INDEXED_DB_SCOPES_LEVELDB_SCOPES_TASKS_H_
#define COMPONENTS_SERVICES_STORAGE_INDEXED_DB_SCOPES_LEVELDB_SCOPES_TASKS_H_

#include <stdint.h>
#include <vector>

#include "base/functional/callback.h"
#include "base/memory/scoped_refptr.h"
#include "base/sequence_checker.h"
#include "components/services/storage/indexed_db/leveldb/leveldb_state.h"
#include "third_party/leveldatabase/src/include/leveldb/status.h"
#include "third_party/leveldatabase/src/include/leveldb/write_batch.h"

namespace leveldb {
struct ReadOptions;
struct WriteOptions;
}  // namespace leveldb

namespace content::indexed_db {

// This base class is thread-compatible, and is designed to be constructed on
// one thread, and then used & destroyed on another.
class LevelDBScopesTask {
 public:
  explicit LevelDBScopesTask(scoped_refptr<LevelDBState> level_db,
                             size_t max_write_batch_size_bytes);

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

  ~LevelDBScopesTask();

 protected:
  // Submits the in-progress WriteBatch to LevelDB, no matter what size the
  // batch is.
  [[nodiscard]] leveldb::Status SubmitWriteBatch(
      const leveldb::WriteOptions& options);
  // Submits thein-progress WriteBatch to LevelDB only if the approximate size
  // of the batch is > |max_write_batch_size_|.
  [[nodiscard]] leveldb::Status MaybeSubmitWriteBatch(
      const leveldb::WriteOptions& options);

  [[nodiscard]] leveldb::Status DeleteRange(
      leveldb::Slice range_start,
      leveldb::Slice range_end,
      const leveldb::ReadOptions& read_options,
      const leveldb::WriteOptions& write_options);

  SEQUENCE_CHECKER(sequence_checker_);

  const scoped_refptr<LevelDBState> level_db_;
  const size_t max_write_batch_size_bytes_;
  leveldb::WriteBatch write_batch_;
};

// Deletes the undo log for a given scope, and optionally executes the cleanup
// tasks recorded in the scope.
//
// Note: Tasks are constructed on one thread and then Run() and destroyed on a
// separate thread.
class CleanupScopeTask : private LevelDBScopesTask {
 public:
  enum class CleanupMode {
    // Used after a scope is committed.
    kExecuteCleanupTasks,
    // Used after a scope is reverted.
    kIgnoreCleanupTasks,
  };

  CleanupScopeTask(scoped_refptr<LevelDBState> level_db,
                   std::vector<uint8_t> metadata_prefix,
                   int64_t scope_number,
                   CleanupMode mode,
                   size_t max_write_batch_size_bytes);
  ~CleanupScopeTask();

  [[nodiscard]] leveldb::Status Run();

 private:
  [[nodiscard]] leveldb::Status ExecuteAndDeleteCleanupTasks(
      const leveldb::ReadOptions& read_options,
      const leveldb::WriteOptions& write_options);
  [[nodiscard]] leveldb::Status DeletePrefixedRange(
      leveldb::Slice prefix,
      const leveldb::ReadOptions& read_options,
      const leveldb::WriteOptions& write_options);

  const std::vector<uint8_t> metadata_prefix_;
  const int64_t scope_number_;
  const CleanupMode mode_;
};

// This task executes & deletes the undo tasks for the given scope, and then
// marks the scope as committed. It should then be cleaned up by a
// CleanupScopeTask that ignores the cleanup tasks (kIgnoreCleanupTasks).
//
// Note: Tasks are constructed on one thread and then |Run| and destroyed on a
// separate thread.
class RevertScopeTask : private LevelDBScopesTask {
 public:
  RevertScopeTask(scoped_refptr<LevelDBState> level_db,
                  std::vector<uint8_t> metadata_prefix,
                  int64_t scope_number,
                  size_t max_write_batch_size_bytes);
  ~RevertScopeTask();

  [[nodiscard]] leveldb::Status Run();

 private:
  const std::vector<uint8_t> metadata_prefix_;
  const int64_t scope_number_;
};

}  // namespace content::indexed_db

#endif  // COMPONENTS_SERVICES_STORAGE_INDEXED_DB_SCOPES_LEVELDB_SCOPES_TASKS_H_