File: expected_value.cc

package info (click to toggle)
rocksdb 9.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 46,052 kB
  • sloc: cpp: 500,768; java: 42,992; ansic: 9,789; python: 8,373; perl: 5,822; sh: 4,921; makefile: 2,386; asm: 550; xml: 342
file content (122 lines) | stat: -rw-r--r-- 3,952 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
122
//  Copyright (c) 2021-present, Facebook, Inc.  All rights reserved.
//  This source code is licensed under both the GPLv2 (found in the
//  COPYING file in the root directory) and Apache 2.0 License
//  (found in the LICENSE.Apache file in the root directory).

#ifdef GFLAGS

#include "db_stress_tool/expected_value.h"

#include <atomic>

namespace ROCKSDB_NAMESPACE {
void ExpectedValue::Put(bool pending) {
  if (pending) {
    SetPendingWrite();
  } else {
    SetValueBase(NextValueBase());
    ClearDeleted();
    ClearPendingWrite();
  }
}

bool ExpectedValue::Delete(bool pending) {
  if (pending && !Exists()) {
    return false;
  }
  if (pending) {
    SetPendingDel();
  } else {
    SetDelCounter(NextDelCounter());
    SetDeleted();
    ClearPendingDel();
  }
  return true;
}

void ExpectedValue::SyncPut(uint32_t value_base) {
  assert(ExpectedValue::IsValueBaseValid(value_base));

  SetValueBase(value_base);
  ClearDeleted();
  ClearPendingWrite();

  // This is needed in case crash happens during a pending delete of the key
  // assocated with this expected value
  ClearPendingDel();
}

void ExpectedValue::SyncPendingPut() { Put(true /* pending */); }

void ExpectedValue::SyncDelete() {
  Delete(false /* pending */);
  // This is needed in case crash happens during a pending write of the key
  // assocated with this expected value
  ClearPendingWrite();
}

uint32_t ExpectedValue::GetFinalValueBase() const {
  return PendingWrite() ? NextValueBase() : GetValueBase();
}

uint32_t ExpectedValue::GetFinalDelCounter() const {
  return PendingDelete() ? NextDelCounter() : GetDelCounter();
}

bool ExpectedValueHelper::MustHaveNotExisted(
    ExpectedValue pre_read_expected_value,
    ExpectedValue post_read_expected_value) {
  const bool pre_read_expected_deleted = pre_read_expected_value.IsDeleted();

  const uint32_t pre_read_expected_value_base =
      pre_read_expected_value.GetValueBase();

  const uint32_t post_read_expected_final_value_base =
      post_read_expected_value.GetFinalValueBase();

  const bool during_read_no_write_happened =
      (pre_read_expected_value_base == post_read_expected_final_value_base);
  return pre_read_expected_deleted && during_read_no_write_happened;
}

bool ExpectedValueHelper::MustHaveExisted(
    ExpectedValue pre_read_expected_value,
    ExpectedValue post_read_expected_value) {
  const bool pre_read_expected_not_deleted =
      !pre_read_expected_value.IsDeleted();

  const uint32_t pre_read_expected_del_counter =
      pre_read_expected_value.GetDelCounter();
  const uint32_t post_read_expected_final_del_counter =
      post_read_expected_value.GetFinalDelCounter();

  const bool during_read_no_delete_happened =
      (pre_read_expected_del_counter == post_read_expected_final_del_counter);

  return pre_read_expected_not_deleted && during_read_no_delete_happened;
}

bool ExpectedValueHelper::InExpectedValueBaseRange(
    uint32_t value_base, ExpectedValue pre_read_expected_value,
    ExpectedValue post_read_expected_value) {
  assert(ExpectedValue::IsValueBaseValid(value_base));

  const uint32_t pre_read_expected_value_base =
      pre_read_expected_value.GetValueBase();
  const uint32_t post_read_expected_final_value_base =
      post_read_expected_value.GetFinalValueBase();

  if (pre_read_expected_value_base <= post_read_expected_final_value_base) {
    const uint32_t lower_bound = pre_read_expected_value_base;
    const uint32_t upper_bound = post_read_expected_final_value_base;
    return lower_bound <= value_base && value_base <= upper_bound;
  } else {
    const uint32_t upper_bound_1 = post_read_expected_final_value_base;
    const uint32_t lower_bound_2 = pre_read_expected_value_base;
    const uint32_t upper_bound_2 = ExpectedValue::GetValueBaseMask();
    return (value_base <= upper_bound_1) ||
           (lower_bound_2 <= value_base && value_base <= upper_bound_2);
  }
}
}  // namespace ROCKSDB_NAMESPACE
#endif  // GFLAGS