Skip to content

Commit 118de84

Browse files
committed
feat(buffer): LRU buffer replacement strategy
1 parent 0236e5a commit 118de84

File tree

3 files changed

+267
-195
lines changed

3 files changed

+267
-195
lines changed

simpledb/buffer/Buffer.java

Lines changed: 88 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -4,97 +4,107 @@
44
import simpledb.log.LogMgr;
55

66
/**
7-
* An individual buffer. A databuffer wraps a page
7+
* An individual buffer. A databuffer wraps a page
88
* and stores information about its status,
99
* such as the associated disk block,
1010
* the number of times the buffer has been pinned,
1111
* whether its contents have been modified,
1212
* and if so, the id and lsn of the modifying transaction.
13+
*
1314
* @author Edward Sciore
1415
*/
1516
public class Buffer {
16-
private FileMgr fm;
17-
private LogMgr lm;
18-
private Page contents;
19-
private BlockId blk = null;
20-
private int pins = 0;
21-
private int txnum = -1;
22-
private int lsn = -1;
17+
private FileMgr fm;
18+
private LogMgr lm;
19+
private int id; // Added for HW3
20+
private Page contents;
21+
private BlockId blk = null;
22+
private int pins = 0;
23+
private int txnum = -1;
24+
private int lsn = -1;
2325

24-
public Buffer(FileMgr fm, LogMgr lm) {
25-
this.fm = fm;
26-
this.lm = lm;
27-
contents = new Page(fm.blockSize());
28-
}
29-
30-
public Page contents() {
31-
return contents;
32-
}
26+
public Buffer(FileMgr fm, LogMgr lm, int id) {
27+
this.fm = fm;
28+
this.lm = lm;
29+
this.id = id;
30+
contents = new Page(fm.blockSize());
31+
}
3332

34-
/**
35-
* Returns a reference to the disk block
36-
* allocated to the buffer.
37-
* @return a reference to a disk block
38-
*/
39-
public BlockId block() {
40-
return blk;
41-
}
33+
public int getId() {
34+
return id;
35+
}
4236

43-
public void setModified(int txnum, int lsn) {
44-
this.txnum = txnum;
45-
if (lsn >= 0)
46-
this.lsn = lsn;
47-
}
37+
public Page contents() {
38+
return contents;
39+
}
4840

49-
/**
50-
* Return true if the buffer is currently pinned
51-
* (that is, if it has a nonzero pin count).
52-
* @return true if the buffer is pinned
53-
*/
54-
public boolean isPinned() {
55-
return pins > 0;
56-
}
57-
58-
public int modifyingTx() {
59-
return txnum;
60-
}
41+
/**
42+
* Returns a reference to the disk block
43+
* allocated to the buffer.
44+
*
45+
* @return a reference to a disk block
46+
*/
47+
public BlockId block() {
48+
return blk;
49+
}
6150

62-
/**
63-
* Reads the contents of the specified block into
64-
* the contents of the buffer.
65-
* If the buffer was dirty, then its previous contents
66-
* are first written to disk.
67-
* @param b a reference to the data block
68-
*/
69-
void assignToBlock(BlockId b) {
70-
flush();
71-
blk = b;
72-
fm.read(blk, contents);
73-
pins = 0;
74-
}
75-
76-
/**
77-
* Write the buffer to its disk block if it is dirty.
78-
*/
79-
void flush() {
80-
if (txnum >= 0) {
81-
lm.flush(lsn);
82-
fm.write(blk, contents);
83-
txnum = -1;
84-
}
85-
}
51+
public void setModified(int txnum, int lsn) {
52+
this.txnum = txnum;
53+
if (lsn >= 0)
54+
this.lsn = lsn;
55+
}
8656

87-
/**
88-
* Increase the buffer's pin count.
89-
*/
90-
void pin() {
91-
pins++;
92-
}
57+
/**
58+
* Return true if the buffer is currently pinned
59+
* (that is, if it has a nonzero pin count).
60+
*
61+
* @return true if the buffer is pinned
62+
*/
63+
public boolean isPinned() {
64+
return pins > 0;
65+
}
9366

94-
/**
95-
* Decrease the buffer's pin count.
96-
*/
97-
void unpin() {
98-
pins--;
99-
}
67+
public int modifyingTx() {
68+
return txnum;
69+
}
70+
71+
/**
72+
* Reads the contents of the specified block into
73+
* the contents of the buffer.
74+
* If the buffer was dirty, then its previous contents
75+
* are first written to disk.
76+
*
77+
* @param b a reference to the data block
78+
*/
79+
void assignToBlock(BlockId b) {
80+
flush();
81+
blk = b;
82+
fm.read(blk, contents);
83+
pins = 0;
84+
}
85+
86+
/**
87+
* Write the buffer to its disk block if it is dirty.
88+
*/
89+
void flush() {
90+
if (txnum >= 0) {
91+
lm.flush(lsn);
92+
fm.write(blk, contents);
93+
txnum = -1;
94+
}
95+
}
96+
97+
/**
98+
* Increase the buffer's pin count.
99+
*/
100+
void pin() {
101+
pins++;
102+
}
103+
104+
/**
105+
* Decrease the buffer's pin count.
106+
*/
107+
void unpin() {
108+
pins--;
109+
}
100110
}

0 commit comments

Comments
 (0)