Skip to content
This repository was archived by the owner on Oct 4, 2021. It is now read-only.

Commit d4e76f9

Browse files
author
fhupfeld
committed
Support for erasing records from the log
git-svn-id: https://babudb.googlecode.com/svn/trunk@359 d4c7dd96-b7b2-11dd-893d-d98698f66bbf
1 parent 5954e59 commit d4e76f9

File tree

6 files changed

+86
-2
lines changed

6 files changed

+86
-2
lines changed

cpp/TODO

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@
99

1010
== LogIndex
1111
- Put keys in tree nodes (better cache locality)
12+
13+
== Log
14+
- remove LSN support in the log (should be done at user-level)

cpp/include/babudb/log/log_iterator.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,21 @@ class LogIterator {
3030

3131
Buffer& operator * () const;
3232
Buffer* operator -> () const;
33-
Buffer asData() const { return this->operator *(); }
33+
Buffer asData() const {
34+
return this->operator *();
35+
}
3436
Buffer getOperationWithFrame() const;
3537

3638
record_type_t getType() const;
37-
lsn_t GetLSN() const { return lsn; };
39+
lsn_t GetLSN() const {
40+
return lsn;
41+
}
42+
RecordIterator getCurrentRecord() const {
43+
return current_record;
44+
}
45+
LogSectionIterator getCurrentSection() const {
46+
return current_section;
47+
}
3848

3949
static RecordIterator section_begin(const LogSectionIterator& section);
4050
static RecordIterator section_end(const LogSectionIterator& section);

cpp/include/babudb/log/log_section.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ class LogSection : public SequentialFile {
5050
// Commit an empty transaction with a new LSN
5151
void SeekForwardTo(babudb::lsn_t);
5252

53+
// Erase record pointed to by iterator. Iterator becomes
54+
// valid again after increment or decrement.
55+
void Erase(SequentialFile::iterator it);
56+
5357
private:
5458
bool in_transaction;
5559
lsn_t first_lsn; // the first lsn in this file

cpp/include/babudb/log/record_iterator.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ class RecordIterator
4848
}
4949

5050
void reverse();
51+
bool isForwardIterator() const {
52+
return is_forward_iterator;
53+
}
5154

5255
bool operator != ( const RecordIterator& other ) const;
5356
bool operator == ( const RecordIterator& other ) const;

cpp/src/log/log_iterator_test.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,55 @@ TEST_TMPDIR(LogIterator,babudb)
146146
log.close();
147147
}
148148
}
149+
150+
TEST_TMPDIR(LogIteratorAndErase,babudb)
151+
{
152+
Log log(testPath("testlog"));
153+
LogSection* tail = log.getTail();
154+
155+
tail->Append(DummyOperation('A')); tail->Commit();
156+
tail->Append(DummyOperation('B')); tail->Commit();
157+
tail->Append(DummyOperation('C')); tail->Commit();
158+
159+
DummyOperation op(0);
160+
Log::iterator i = log.begin();
161+
++i;
162+
EXPECT_TRUE(op.Deserialize(*i).value == 'B');
163+
EXPECT_TRUE(i.GetLSN() == 2);
164+
165+
tail->Erase(i.getCurrentRecord());
166+
167+
++i;
168+
EXPECT_TRUE(op.Deserialize(*i).value == 'C');
169+
EXPECT_TRUE(i.GetLSN() == 3);
170+
171+
--i;
172+
EXPECT_TRUE(op.Deserialize(*i).value == 'A');
173+
174+
log.close();
175+
}
176+
177+
TEST_TMPDIR(LogIteratorAndEraseReverse,babudb)
178+
{
179+
Log log(testPath("testlog"));
180+
LogSection* tail = log.getTail();
181+
182+
tail->Append(DummyOperation('A')); tail->Commit();
183+
tail->Append(DummyOperation('B')); tail->Commit();
184+
tail->Append(DummyOperation('C')); tail->Commit();
185+
186+
DummyOperation op(0);
187+
Log::iterator i = log.rbegin();
188+
++i;
189+
EXPECT_TRUE(op.Deserialize(*i).value == 'B');
190+
191+
tail->Erase(i.getCurrentRecord());
192+
193+
++i;
194+
EXPECT_TRUE(op.Deserialize(*i).value == 'A');
195+
196+
--i;
197+
EXPECT_TRUE(op.Deserialize(*i).value == 'C');
198+
199+
log.close();
200+
}

cpp/src/log/log_section.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@ void LogSection::SeekForwardTo(babudb::lsn_t new_lsn) {
7373
Commit();
7474
}
7575

76+
void LogSection::Erase(SequentialFile::iterator it) {
77+
ASSERT_TRUE(it.getType() != LSN_RECORD_TYPE);
78+
erase(record2offset(it.getRecord()));
79+
// TODO: support erase of multi-record transactions
80+
if (!it.isForwardIterator()) {
81+
it.reverse();
82+
}
83+
--it;
84+
ASSERT_TRUE(it.getType() == LSN_RECORD_TYPE);
85+
erase(record2offset(it.getRecord()));
86+
}
87+
7688

7789
LogSectionIterator::LogSectionIterator(std::vector<LogSection*>& sections)
7890
: sections(sections) {}

0 commit comments

Comments
 (0)