@@ -27,11 +27,16 @@ class TOperationLog {
27
27
private:
28
28
constexpr static ui32 Capacity = S;
29
29
std::array<std::atomic<TString*>, S> Records;
30
- // RecordIdx is shifted to prevent underflow on decrement
30
+ // RecordPosition is shifted to prevent underflow on decrement
31
31
std::atomic<ui64> NextRecordPosition = Capacity;
32
+ std::atomic<ui64> CommittedRecordPosition = Capacity;
32
33
33
34
public:
34
- TOperationLog () = default ;
35
+ TOperationLog (bool initializeRecords = true , TString defaultValue = " Race occured, try again" ) {
36
+ if (initializeRecords) {
37
+ InitializeRecords (defaultValue);
38
+ }
39
+ };
35
40
36
41
TOperationLog (const TOperationLog& other) = delete ;
37
42
TOperationLog& operator =(const TOperationLog& other) = delete ;
@@ -45,6 +50,12 @@ class TOperationLog {
45
50
}
46
51
}
47
52
53
+ bool InitializeRecords (TString value = " Race occured, try again" ) {
54
+ for (auto & record : Records) {
55
+ record.store (new TString (value))
56
+ }
57
+ }
58
+
48
59
BorrowedRecord BorrowByIdx (ui32 idx) {
49
60
ui32 position = (NextRecordPosition.load () - 1 - idx) % Capacity;
50
61
if (idx >= Size ()) {
@@ -68,11 +79,12 @@ class TOperationLog {
68
79
}
69
80
70
81
ui32 Size () const {
71
- return Min (NextRecordPosition .load () - Capacity, static_cast <ui64>(Capacity));
82
+ return Min (CommittedRecordPosition .load () - Capacity, static_cast <ui64>(Capacity));
72
83
}
73
84
74
85
void AddRecord (std::unique_ptr<TString>& value) {
75
86
TString* prev = Records[NextRecordPosition.fetch_add (1 ) % Capacity].exchange (value.release ());
87
+ CommittedRecordPosition.fetch_add (1 );
76
88
if (prev) {
77
89
delete prev;
78
90
}
0 commit comments