Java Programming: Must implement a storage manager that maintains a series of data objects in
each data file. This assignment provides you with a set of incomplete classes (, see SlottedPage,
FileManager, and BufferedFileManager which contain methods currently throwing an
UnsupportedOperationException). You will need to write code for these classes. Your code will be
graded by running a set of unit tests and then examining your code (see SlottedPageTest and
FileManagerTest which use JUnit1, as well as BufferedFileManagerTest which produces output
messages). Note that passing unit tests does NOT necessarily guarantee that your
implementation is correct and efficient. Please make sure that your code will not cause any
problem in other cases not covered by the unit tests. Below are the files and make sure there are
no errors and show the output as well.
BufferedFileManager.java
package mypack;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
public class BufferedFileManager extends FileManager {
private int bufferSize;
private Map<Integer, SlottedPage> buffer;
public BufferedFileManager(int bufferSize) {
this.bufferSize = bufferSize;
buffer = new LinkedHashMap<Integer, SlottedPage>(bufferSize);
}
@Override
public void put(int fileID, Long location, Object o) throws IOException, InvalidLocationException {
if (buffer.containsKey(first(location))) {
SlottedPage p = buffer.get(first(location));
p.put(second(location), o);
buffer.put(first(location), p);
} else {
if (buffer.size() == bufferSize) {
// Evict the least recently used page
buffer.remove(buffer.keySet().iterator().next());
}
SlottedPage p = page(fileID, first(location));
p.put(second(location), o);
updated(p, fileID);
buffer.put(first(location), p);
}
}
@Override
public Object get(int fileID, Long location) throws IOException, InvalidLocationException {
if (buffer.containsKey(first(location))) {
return buffer.get(first(location)).get(second(location));
}
SlottedPage p = page(fileID, first(location));
if (buffer.size() == bufferSize) {
// Evict the least recently used page
buffer.remove(buffer.keySet().iterator().next());
}
buffer.put(first(location), p);
return p.get(second(location));
}
@Override
public Object remove(int fileID, Long location) throws IOException, InvalidLocationException {
if (buffer.containsKey(first(location))) {
SlottedPage p = buffer.get(first(location));
Object o = p.remove(second(location));
buffer.put(first(location), p);
return o;
}
SlottedPage p = page(fileID, first(location));
Object o = p.remove(second(location));
updated(p, fileID);
if (buffer.size() == bufferSize) {
// Evict the least recently used page
buffer.remove(buffer.keySet().iterator().next());
}
buffer.put(first(location), p);
return o;
}
@Override
public Iterator<Object> iterator(int fileID) throws IOException {
SlottedPage[] pages = pages(fileID);
return new BufferedPageIterator(pages, buffer);
}
}
BufferedPageIterator.java
package mypack;.
Java Programming Must implement a storage manager that main.pdf
1. Java Programming: Must implement a storage manager that maintains a series of data objects in
each data file. This assignment provides you with a set of incomplete classes (, see SlottedPage,
FileManager, and BufferedFileManager which contain methods currently throwing an
UnsupportedOperationException). You will need to write code for these classes. Your code will be
graded by running a set of unit tests and then examining your code (see SlottedPageTest and
FileManagerTest which use JUnit1, as well as BufferedFileManagerTest which produces output
messages). Note that passing unit tests does NOT necessarily guarantee that your
implementation is correct and efficient. Please make sure that your code will not cause any
problem in other cases not covered by the unit tests. Below are the files and make sure there are
no errors and show the output as well.
BufferedFileManager.java
package mypack;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
public class BufferedFileManager extends FileManager {
private int bufferSize;
private Map<Integer, SlottedPage> buffer;
public BufferedFileManager(int bufferSize) {
this.bufferSize = bufferSize;
buffer = new LinkedHashMap<Integer, SlottedPage>(bufferSize);
}
@Override
public void put(int fileID, Long location, Object o) throws IOException, InvalidLocationException {
if (buffer.containsKey(first(location))) {
SlottedPage p = buffer.get(first(location));
p.put(second(location), o);
buffer.put(first(location), p);
} else {
if (buffer.size() == bufferSize) {
// Evict the least recently used page
buffer.remove(buffer.keySet().iterator().next());
}
SlottedPage p = page(fileID, first(location));
p.put(second(location), o);
updated(p, fileID);
buffer.put(first(location), p);
}
}
@Override
public Object get(int fileID, Long location) throws IOException, InvalidLocationException {
2. if (buffer.containsKey(first(location))) {
return buffer.get(first(location)).get(second(location));
}
SlottedPage p = page(fileID, first(location));
if (buffer.size() == bufferSize) {
// Evict the least recently used page
buffer.remove(buffer.keySet().iterator().next());
}
buffer.put(first(location), p);
return p.get(second(location));
}
@Override
public Object remove(int fileID, Long location) throws IOException, InvalidLocationException {
if (buffer.containsKey(first(location))) {
SlottedPage p = buffer.get(first(location));
Object o = p.remove(second(location));
buffer.put(first(location), p);
return o;
}
SlottedPage p = page(fileID, first(location));
Object o = p.remove(second(location));
updated(p, fileID);
if (buffer.size() == bufferSize) {
// Evict the least recently used page
buffer.remove(buffer.keySet().iterator().next());
}
buffer.put(first(location), p);
return o;
}
@Override
public Iterator<Object> iterator(int fileID) throws IOException {
SlottedPage[] pages = pages(fileID);
return new BufferedPageIterator(pages, buffer);
}
}
BufferedPageIterator.java
package mypack;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
public class BufferedPageIterator implements Iterator<Object>{
private SlottedPage[] pages;
3. private int currPage = 0;
private int currIndex = 0;
private Map<Integer, SlottedPage> buffer;
public BufferedPageIterator(SlottedPage[] pages, Map<Integer, SlottedPage> buffer) {
this.pages = pages;
this.buffer = buffer;
}
@Override
public boolean hasNext() {
if (currPage == pages.length) {
return false;
}
return !(currIndex == pages[currPage].entryCount() && currPage == pages.length - 1);
}
@Override
public Object next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
Object o;
if (buffer.containsKey(currPage)) {
o = buffer.get(currPage).get(currIndex);
} else {
o = pages[currPage].get(currIndex);
}
currIndex++;
if (currIndex == pages[currPage].entryCount()) {
currPage++;
currIndex = 0;
}
return o;
}
}
FileManager.java
package mypack;
import java.io.IOException;
import java.util.Iterator;
public class FileManager {
public void put(int fileID, Long location, Object o) throws IOException, InvalidLocationException {
if (first(location) < 0) {
throw new InvalidLocationException();
}
4. SlottedPage p = page(fileID, first(location));
p.put(second(location), o);
updated(p, fileID);
}
public Object get(int fileID, Long location) throws IOException, InvalidLocationException {
if (first(location) < 0) {
throw new InvalidLocationException();
}
SlottedPage p = page(fileID, first(location));
return p.get(second(location));
}
public Object remove(int fileID, Long location) throws IOException, InvalidLocationException {
if (first(location) < 0) {
throw new InvalidLocationException();
}
SlottedPage p = page(fileID, first(location));
return p.remove(second(location));
}
public Iterator<Object> iterator(int fileID) throws IOException {
SlottedPage[] pages = pages(fileID);
return new PageIterator(pages);
}
}
InvalidLocationException.java
package mypack;
public class InvalidLocationException extends Exception {
}
PageIterator.java
package mypack;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class PageIterator implements Iterator<Object> {
private SlottedPage[] pages;
private int currPage = 0;
private int currIndex = 0;
public PageIterator(SlottedPage[] pages) {
this.pages = pages;
}
@Override
public boolean hasNext() {
if (currPage == pages.length) {
return false;
5. }
return !(currIndex == pages[currPage].entryCount() && currPage == pages.length - 1);
}
@Override
public Object next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
Object o = pages[currPage].get(currIndex);
currIndex++;
if (currIndex == pages[currPage].entryCount()) {
currPage++;
currIndex = 0;
}
return o;
}
}
SlottedPage.java
package mypack;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class SlottedPage {
public void add(Object o) throws IOException, NotEnoughSpaceException {
int loc = save(o);
setEntryCount(entryCount() + 1);
saveLocation(entryCount() - 1, loc);
}
public Object get(int index) {
int loc = getLocation(index);
if (loc == -1) {
return null;
}
return toObject(b, loc);
}
public Object remove(int index) {
int loc = getLocation(index);
if (loc == -1) {
return null;
}
Object o = toObject(b, loc);
6. saveLocation(index, -1);
return o;
}
public Iterator<Object> iterator() {
return new SlottedPageIterator(this);
}
public void compact() {
int offset = 0;
for (int i = 0; i < entryCount(); i++) {
int loc = getLocation(i);
if (loc != -1) {
int size = size(toObject(b, loc));
if (offset + size >= b.length) {
throw new NotEnoughSpaceException();
}
move(loc, offset);
saveLocation(i, offset);
offset += size;
}
}
}
public int entryCount() {
// TODO Auto-generated method stub
return 0;
}
}
SlottedPageIterator.java
package mypack;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class SlottedPageIterator implements Iterator<Object>{
private SlottedPage page;
private int currIndex = 0;
public SlottedPageIterator(SlottedPage page) {
this.page = page;
}
@Override
public boolean hasNext() {
return !(currIndex == page.entryCount());
}
@Override
7. public Object next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
Object o = page.get(currIndex);
currIndex++;
return o;
}
}