diff --git a/p2/.idea/workspace.xml b/p2/.idea/workspace.xml index f33fa25..c8cb6f3 100644 --- a/p2/.idea/workspace.xml +++ b/p2/.idea/workspace.xml @@ -2,7 +2,6 @@ - @@ -32,7 +31,7 @@ - + @@ -43,8 +42,8 @@ - - + + @@ -70,17 +69,17 @@ - - + + - - - - - - - - + + + + + + + + @@ -308,34 +307,34 @@ - - - - + - + - + - + - + - + - + - + - + + + + @@ -381,9 +380,15 @@ file://$PROJECT_DIR$/src/tests/BMTest.java - 769 + 575 - + + file://$PROJECT_DIR$/src/tests/BMTest.java + 581 + + @@ -461,24 +466,24 @@ - - + + - - + + - - - - - - - - + + + + + + + + diff --git a/p2/src/bufmgr/BufMgr.java b/p2/src/bufmgr/BufMgr.java index 64f4470..3d49629 100644 --- a/p2/src/bufmgr/BufMgr.java +++ b/p2/src/bufmgr/BufMgr.java @@ -106,27 +106,17 @@ public class BufMgr { Page foundPage = new Page(); if (isFull) { int index = -1; + Integer head = fifo.poll(); - for (Integer item : fifo) { - Integer pointer = hashTable.get(item); - if (pointer != null && bufDescr[pointer].PinCount <= 0) { - index = pointer; - break; + if (head != null) { + index = hashTable.get(head); + if (bufDescr[index].PinCount > 0) { + throw new BufferPoolExceededException("No head found in FIFO Queue"); } + } else { + throw new BufferPoolExceededException("No head found in FIFO Queue"); } - if (index <= -1) { - throw new BufferPoolExceededException("No item found with 0 pin count"); - } - -// Integer head = fifo.poll(); -// -// if (head != null) { -// index = hashTable.get(head); -// } else { -// throw new BufferPoolExceededException("No head found in FIFO Queue"); -// } - if (bufDescr[index].DirtyBit) { try { // TODO: May be wrong error. @@ -175,7 +165,7 @@ public class BufMgr { page.setPage(foundPage); } - fifo.remove(foundEntry); + fifo.remove(hashTable.get(pageno.pid)); } /** @@ -283,10 +273,25 @@ public class BufMgr { } if (nextIndex > 0) { - int move = nextIndex; + int relocate = nextIndex; nextIndex--; fifo.remove(hashTable.get(globalPageId.pid)); + + int destroy = hashTable.get(globalPageId.pid); + hashTable.remove(globalPageId.pid); + if (destroy == relocate) { + bufFrames[relocate] = null; + bufDescr[relocate] = null; + } else { + bufFrames[destroy] = bufFrames[relocate]; + bufDescr[destroy] = bufDescr[relocate]; + bufFrames[relocate] = null; + bufDescr[relocate] = null; + + hashTable.put(bufDescr[destroy].PageNumber, destroy); + } + } // then delete from disk