/* * (non-Javadoc) * @see de.tuberlin.dima.minidb.io.ResourceManager#reserveNewPage(byte[]) */ @Override public CacheableData reserveNewPage(byte[] ioBuffer) throws IOException, PageFormatException { // create disk representation of data byte[] diskCopy = new byte[this.pageSize.getNumberOfBytes()]; // copy current contents of buffer to disk array System.arraycopy(ioBuffer, 0, diskCopy, 0, ioBuffer.length); TablePage page = AbstractExtensionFactory.getExtensionFactory() .initTablePage(this.schema, ioBuffer, this.pageNumber++); this.diskBuffer.add(diskCopy); return page; }
/* * (non-Javadoc) * @see de.tuberlin.dima.minidb.io.ResourceManager#readPageFromResource(byte[], int) */ @Override public CacheableData readPageFromResource(byte[] buffer, int pageNumber) throws IOException { // add the request to the list of read requests this.readRequests.add(pageNumber); // check if thread is different than the creator if (Thread.currentThread() == this.creator) { throw new IOException(readingSeparateThread); } // check if reader thread is already set if (this.reader == null) { this.reader = Thread.currentThread(); } // check if it is always the same reading thread if (this.reader != Thread.currentThread()) { throw new IOException(readingSameThread); } // generate page to return TablePage page = null; if (pageNumber < this.pageNumber && pageNumber >= firstPageNumber) { // read contents from "disk" System.arraycopy(this.diskBuffer.get(pageNumber - 1), 0, buffer, 0, buffer.length); try { page = AbstractExtensionFactory.getExtensionFactory().createTablePage(this.schema, buffer); } catch (Exception e) { throw new IOException("Error initializing page: " + e.getMessage()); } } else { throw new IOException("This page does not exist."); } // wait to test prefetching if (this.prefetch) { try { Thread.sleep(200); } catch (InterruptedException e) { // do nothing } } return page; }