Skip to content

Commit

Permalink
feat(file): support boolean data type
Browse files Browse the repository at this point in the history
  • Loading branch information
amrdb committed Oct 17, 2024
1 parent 28ac8b7 commit 40e87df
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ My contributions/tasks solutions to the educational database SimpleDB from the D
statistics ([commit](https://github.com/Amr2812/simpledb/commit/4948b9ea0b37703d6af37259be9dc8f18a428d24))
- feat(file): handle data not fitting in
page ([commit](https://github.com/Amr2812/simpledb/commit/c2e44635a5f48d98e758c0760114e499cce0762e))
- feat(file): support boolean data type ([commit]())

For general information about simpledb:

Expand Down
13 changes: 13 additions & 0 deletions simpledb/file/Page.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ public static int maxLength(int strlen) {
return Integer.BYTES + (strlen * (int) bytesPerChar);
}

public boolean getBoolean(int offset) {
return bb.get(offset) == 1;
}

public void setBoolean(int offset, boolean b) {
if (bb.capacity() - offset < 1) {
System.out.println("ERROR: boolean " + (b ? "true" : "false") + " does not fit at location " + offset + " of the page");
} else {
bb.position(offset);
bb.put((byte) (b ? 1 : 0));
}
}

// a package private method, needed by FileMgr
ByteBuffer contents() {
bb.position(0);
Expand Down
16 changes: 13 additions & 3 deletions simpledb/file/PageFitsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,26 @@ public static void main(String[] args) throws IOException {
System.out.println("*** You didn't test for invalid string");
}

int pos3 = 4 + stringsize;
boolean bool = true;
int pos3 = fm.blockSize();
try {
p.setBoolean(pos3, bool);
} catch (Exception e) {
System.out.println("*** You didn't test for invalid boolean");
}

int pos = 4 + stringsize;
p.setInt(0, 1234);
p.setString(4, s);
p.setInt(pos3, 5678);
p.setInt(pos, 5678);
p.setBoolean(pos + 4, true);
fm.write(blk, p);

Page p2 = new Page(fm.blockSize());
fm.read(blk, p2);
System.out.println("offset 0 contains " + p2.getInt(0));
System.out.println("offset 4 contains " + p2.getString(4));
System.out.println("offset " + pos3 + " contains " + p2.getInt(pos3));
System.out.println("offset " + pos + " contains " + p2.getInt(pos));
System.out.println("offset " + pos + 4 + " contains " + p2.getBoolean(pos + 4));
}
}

0 comments on commit 40e87df

Please sign in to comment.