-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBTreeIndex.java
More file actions
174 lines (127 loc) · 5.68 KB
/
Copy pathBTreeIndex.java
File metadata and controls
174 lines (127 loc) · 5.68 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package storage;
import memory.BufferPoolManager;
import memory.Page;
import java.util.logging.Logger;
public class BTreeIndex {
private static final Logger logger = Logger.getLogger(BTreeIndex.class.getName());
private static final int PAGE_HEADER_SIZE = 64;
private static final int MAX_TUPLES_PER_PAGE = (Page.PAGE_SIZE - PAGE_HEADER_SIZE) / Tuple.TUPLE_SIZE;
private final BufferPoolManager bufferPool;
private final int rootPageId;
public BTreeIndex(BufferPoolManager bufferPool) {
this.bufferPool = bufferPool;
this.rootPageId = 0;
logger.info("BTreeIndex initialized. Max tuples per leaf page: " + MAX_TUPLES_PER_PAGE);
}
public boolean insert(Tuple tuple) {
Page targetPage = bufferPool.fetchPage(rootPageId);
try {
targetPage.getData().position(0);
int currentRecords = targetPage.getData().getInt();
if (currentRecords >= MAX_TUPLES_PER_PAGE) {
logger.warning("B-Tree Leaf Node is full. Split required (Out of scope for POC).");
return false;
}
int insertOffset = PAGE_HEADER_SIZE + (currentRecords * Tuple.TUPLE_SIZE);
tuple.serialize(targetPage.getData(), insertOffset);
targetPage.getData().position(0);
targetPage.getData().putInt(currentRecords + 1);
logger.info("Inserted Tuple ID: " + tuple.getId() + " at offset: " + insertOffset);
return true;
} finally {
bufferPool.unpinPage(rootPageId, true);
}
}
public boolean delete(long targetId) {
Page targetPage = bufferPool.fetchPage(rootPageId);
try {
targetPage.getData().position(0);
int currentRecords = targetPage.getData().getInt();
Tuple tempTuple = new Tuple();
for (int i = 0; i < currentRecords; i++) {
int readOffset = PAGE_HEADER_SIZE + (i * Tuple.TUPLE_SIZE);
tempTuple.deserialize(targetPage.getData(), readOffset);
if (tempTuple.getId() == targetId) {
targetPage.getData().position(readOffset);
for (int b = 0; b < Tuple.TUPLE_SIZE; b++) {
targetPage.getData().put((byte) 0x00);
}
logger.info("Tombstone placed for ID: " + targetId + " at offset: " + readOffset);
return true;
}
}
logger.info("Delete failed: ID " + targetId + " not found.");
return false;
} finally {
bufferPool.unpinPage(rootPageId, true);
}
}
public boolean update(long targetId, String newPayloadStr) {
Page targetPage = bufferPool.fetchPage(rootPageId);
try {
targetPage.getData().position(0);
int currentRecords = targetPage.getData().getInt();
Tuple tempTuple = new Tuple();
for (int i = 0; i < currentRecords; i++) {
int readOffset = PAGE_HEADER_SIZE + (i * Tuple.TUPLE_SIZE);
tempTuple.deserialize(targetPage.getData(), readOffset);
if (tempTuple.getId() == targetId) {
long newTimestamp = System.currentTimeMillis();
Tuple updatedTuple = new Tuple(targetId, newTimestamp, newPayloadStr);
updatedTuple.serialize(targetPage.getData(), readOffset);
logger.info("In-Place Update executed for ID: " + targetId + " at offset: " + readOffset);
return true;
}
}
logger.warning("Update failed: ID " + targetId + " not found.");
return false;
} finally {
bufferPool.unpinPage(rootPageId, true);
}
}
public String sequentialScan() {
Page targetPage = bufferPool.fetchPage(rootPageId);
StringBuilder result = new StringBuilder();
int activeRecords = 0;
try {
targetPage.getData().position(0);
int currentRecords = targetPage.getData().getInt();
Tuple tempTuple = new Tuple();
for (int i = 0; i < currentRecords; i++) {
int readOffset = PAGE_HEADER_SIZE + (i * Tuple.TUPLE_SIZE);
tempTuple.deserialize(targetPage.getData(), readOffset);
if (tempTuple.getId() != -1L) {
result.append("\n [ID: ").append(tempTuple.getId()).append("] => ")
.append(tempTuple.getPayloadAsString());
activeRecords++;
}
}
if (activeRecords == 0) {
return "TABLE IS EMPTY";
}
return "Found " + activeRecords + " active records:" + result.toString();
} finally {
bufferPool.unpinPage(rootPageId, false);
}
}
public Tuple pointQuery(long searchId) {
Page targetPage = bufferPool.fetchPage(rootPageId);
try {
targetPage.getData().position(0);
int currentRecords = targetPage.getData().getInt();
Tuple tempTuple = new Tuple();
for (int i = 0; i < currentRecords; i++) {
int readOffset = PAGE_HEADER_SIZE + (i * Tuple.TUPLE_SIZE);
tempTuple.deserialize(targetPage.getData(), readOffset);
if (tempTuple.getId() == searchId) {
logger.info("Point Query HIT for ID: " + searchId);
return tempTuple;
}
}
logger.info("Point Query MISS for ID: " + searchId);
return null;
} finally {
bufferPool.unpinPage(rootPageId, false);
}
}
}