File: leveldb_scopes_coding.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 (109 lines) | stat: -rw-r--r-- 4,707 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
// 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_CODING_H_
#define COMPONENTS_SERVICES_STORAGE_INDEXED_DB_SCOPES_LEVELDB_SCOPES_CODING_H_

#include <stdint.h>
#include <limits>
#include <string>
#include <tuple>

#include "base/containers/span.h"
#include "third_party/leveldatabase/src/include/leveldb/slice.h"

namespace content::indexed_db {
namespace leveldb_scopes {

inline constexpr uint8_t kGlobalMetadataByte = 0x00;
inline constexpr uint8_t kScopesMetadataByte = 0x01;
inline constexpr uint8_t kLogByte = 0x02;

// One of these bytes follows the |kLogByte| to specify whether the log is for
// undo tasks or a cleanup tasks.
inline constexpr uint8_t kUndoTasksByte = 0x00;
inline constexpr uint8_t kCleanupTasksByte = 0x01;

inline constexpr int64_t kMinSupportedVersion = 1;
inline constexpr int64_t kCurrentVersion = 1;

inline constexpr int64_t kFirstScopeNumber = 0;
inline constexpr int64_t kFirstSequenceNumberToWrite =
    std::numeric_limits<int64_t>::max();

std::tuple<bool /*success*/, int64_t /*scope_id*/> ParseScopeMetadataId(
    leveldb::Slice key,
    base::span<const uint8_t> scopes_prefix);

std::string KeyToDebugString(base::span<const uint8_t> key_without_prefix);

}  // namespace leveldb_scopes

// This class helps the re-use of a common std::string buffer. All calls modify
// the internal std::string buffer and return a slice to it.
// Important: Every call to this class will invalidate any 'old' slices that
// were returned by previous calls.
class ScopesEncoder {
 public:
  ScopesEncoder() = default;
  ~ScopesEncoder() = default;

  // The value on disk is expected to be a LevelDBScopesMetadata.
  leveldb::Slice GlobalMetadataKey(base::span<const uint8_t> scopes_prefix);

  // The value on disk  is expected to be a LevelDBScopesScopeMetadata.
  leveldb::Slice ScopeMetadataKey(base::span<const uint8_t> scopes_prefix,
                                  int64_t scope_number);

  // Returns a key prefix that only scope metadata keys use. This is intended to
  // be used to initialize a LevelDB iterator, where advancing the iterator
  // enumerates all metadata entries. Each metadata entry value is expected to
  // be a LevelDBScopesScopeMetadata.
  leveldb::Slice ScopeMetadataPrefix(base::span<const uint8_t> scopes_prefix);

  // Returns a key prefix for all scope tasks for all scopes. This is intended
  // to be used for unit tests which test for the presence of any scope task
  // data after all cleanup has finished.
  leveldb::Slice TasksKeyPrefix(base::span<const uint8_t> scopes_prefix);

  // Returns a key prefix that only scope tasks keys for the given
  // |scope_number| use. This is intended to be used to delete all tasks, or
  // create an iterator for all tasks. There are two task types under this
  // prefix, undo tasks and cleanup tasks.
  leveldb::Slice TasksKeyPrefix(base::span<const uint8_t> scopes_prefix,
                                int64_t scope_number);

  // Returns a key prefix is only scoped to 'undo' tasks keys for the given
  // |scope_number| use. This is intended to be used to initialize a LevelDB
  // iterator, where advancing the iterator enumerates all of the tasks for the
  // given |scope_number|. The task value is expected to be a
  // LevelDBScopesUndoTask.
  leveldb::Slice UndoTaskKeyPrefix(base::span<const uint8_t> scopes_prefix,
                                   int64_t scope_number);

  // Returns a key prefix is only scoped to 'cleanup' tasks keys for the given
  // |scope_number| use. This is intended to be used to initialize a LevelDB
  // iterator, where advancing the iterator enumerates all of the tasks for the
  // given |scope_number|.  The task value is expected to be a
  // LevelDBScopesCleanupTask.
  leveldb::Slice CleanupTaskKeyPrefix(base::span<const uint8_t> scopes_prefix,
                                      int64_t scope_number);

  // The value on disk is expected to be a LevelDBScopesUndoTask.
  leveldb::Slice UndoTaskKey(base::span<const uint8_t> scopes_prefix,
                             int64_t scope_number,
                             int64_t undo_sequence_number);

  // The value on disk is expected to be a LevelDBScopesCleanupTask.
  leveldb::Slice CleanupTaskKey(base::span<const uint8_t> scopes_prefix,
                                int64_t scope_number,
                                int64_t cleanup_sequence_number);

 private:
  std::string key_buffer_;
};

}  // namespace content::indexed_db

#endif  // COMPONENTS_SERVICES_STORAGE_INDEXED_DB_SCOPES_LEVELDB_SCOPES_CODING_H_