Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void reorganization() throws ExitException, IOException {
- RandomAccessFile new_file = new RandomAccessFile("tmp.txt", "rw"); //file to write sorted data
- new_file.setLength(0);
- int main_page_counter = 0;
- byte[] overflow_page = new byte[PAGE_SIZE];
- int overflow_off = 0;
- byte[] new_page = new byte[PAGE_SIZE];
- int new_off = 0;
- int tmp;
- ByteBuffer new_wrapper = ByteBuffer.wrap(new_page);
- ByteBuffer wrapper = ByteBuffer.wrap(this.databaseFile.getBuffer());
- ByteBuffer overflow_wrapper = ByteBuffer.wrap(overflow_page);
- this.databaseFile.setBufferOffset(0);
- this.databaseFile.seek(MAX_MAIN);
- try{
- this.databaseFile.read(overflow_page); //reading overflow area
- this.read_counter++;
- this.databaseFile.seek(0); //setting file to start (main area)
- while(main_page_counter < pageAmount && this.databaseFile.read(this.databaseFile.getBuffer()) != -1) { //reading main area until the end (not to go to overflow)
- main_page_counter++;
- this.read_counter++;
- while(this.databaseFile.getBuffer().length - this.databaseFile.getBufferOffset() >= RECORD_SIZE) { //until we exceed one page
- if(new_off == PAGE_SIZE - ALFA * RECORD_SIZE) { //if in new file with sorted data we go to the end of page (page size - alfa) we write the page to file and clear buffer
- new_off = 0;
- new_file.write(new_page);
- write_counter++;
- new_page = new byte[PAGE_SIZE];
- new_wrapper = ByteBuffer.wrap(new_page);
- }
- if(overflow_off / RECORD_SIZE == this.overflow_counter || wrapper.getInt(this.databaseFile.getBufferOffset()) < overflow_wrapper.getInt(overflow_off)) { //if we get to the end of overflow area or the key in main area is smaller than the one in overflow we write to new file the one from main area
- new_wrapper.putInt(new_off, wrapper.getInt(this.databaseFile.getBufferOffset()));
- new_wrapper.putFloat(new_off + 12, wrapper.getFloat(this.databaseFile.getBufferOffset() + 12));
- new_wrapper.putDouble(new_off + 4, wrapper.getDouble(this.databaseFile.getBufferOffset() + 4));
- new_wrapper.putInt(new_off + 16, wrapper.getInt(this.databaseFile.getBufferOffset() + 16));
- new_off += RECORD_SIZE;
- this.databaseFile.setBufferOffset(this.databaseFile.getBufferOffset() + RECORD_SIZE);
- } else { //otherwise we write to new file the record from overflow area
- new_wrapper.putInt(new_off, overflow_wrapper.getInt(overflow_off));
- new_wrapper.putFloat(new_off + 12, overflow_wrapper.getFloat(overflow_off + 12));
- new_wrapper.putDouble(new_off + 4, overflow_wrapper.getDouble(overflow_off + 4));
- new_wrapper.putInt(new_off + 16, overflow_wrapper.getInt(overflow_off + 16));
- new_off += RECORD_SIZE;
- overflow_off += RECORD_SIZE;
- }
- tmp = new_wrapper.getInt(new_off - 4); //get the pointer of the latest written to new file record
- //if there's a list of elements
- while(tmp != 0 && overflow_off / RECORD_SIZE < this.overflow_counter) { // we go with the flow of the pointers until the end or we get to the end of overflow
- new_wrapper.putInt(new_off - 4, 0); //we write 0 to the place of the pointer
- if(new_off == PAGE_SIZE - ALFA * RECORD_SIZE) { //if in new file with sorted data we go to the end of page (page size - alfa) we write the page to file and clear buffer
- new_file.write(new_page);
- this.write_counter++;
- new_off = 0;
- new_page = new byte[PAGE_SIZE];
- new_wrapper = ByteBuffer.wrap(new_page);
- }
- while(overflow_wrapper.getInt(overflow_off) != tmp) { //we get through the the overflow area until we got the right key
- overflow_off += RECORD_SIZE;
- }
- new_wrapper.putInt(new_off, overflow_wrapper.getInt(overflow_off));
- new_wrapper.putFloat(new_off + 12, overflow_wrapper.getFloat(overflow_off + 12));
- new_wrapper.putDouble(new_off + 4, overflow_wrapper.getDouble(overflow_off + 4));
- new_wrapper.putInt(new_off + 16, overflow_wrapper.getInt(overflow_off + 16));
- new_off += RECORD_SIZE;
- overflow_off += RECORD_SIZE;
- tmp = new_wrapper.getInt(new_off - 4);
- }
- }
- this.databaseFile.setBufferOffset(0);
- }
- }
- catch (Exception e) {
- //End of file
- }
- new_file.write(new_page);
- this.write_counter++;
- new_page = new byte[PAGE_SIZE];
- new_wrapper = ByteBuffer.wrap(new_page);
- //rewriting files and creating new index file
- this.indexFile.seek(0);
- new_file.seek(0);
- this.databaseFile.seek(0);
- int i = 1;
- int index_off = 0;
- this.indexFile.setBuffer(new byte[PAGE_SIZE]);
- ByteBuffer index_wrapper = ByteBuffer.wrap(this.indexFile.getBuffer());
- pageAmount = 0;
- this.databaseFile.setLength(0);
- while(new_file.read(new_page) != -1) { //we write new file with sorted data to the main file and we create index file from the scratch
- pageAmount++;
- this.read_counter++;
- index_wrapper.putInt(index_off, new_wrapper.getInt(0));
- index_wrapper.putInt(index_off + 4, i++);
- index_off += INDEX_RECORD_SIZE;
- this.databaseFile.write(new_page);
- this.write_counter++;
- }
- this.indexFile.write(this.indexFile.getBuffer());
- this.databaseFile.write(new byte[PAGE_SIZE]);
- //overflow area
- new_file.close();
- this.MAX_MAIN = PAGE_SIZE * pageAmount;
- this.overflow_counter = 0;
- this.OVERFLOW_SIZE = 2 * pageAmount * RECORD_SIZE;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement