View difference between Paste ID: G8mMurtV and 4MrgwD1K
SHOW: | | - or go back to the newest paste.
1
 public void reorganization() throws ExitException, IOException {
2
3-
        RandomAccessFile new_file = new RandomAccessFile("tmp.txt", "rw");
3+
        RandomAccessFile new_file = new RandomAccessFile("tmp.txt", "rw");	//file to write sorted data
4
        new_file.setLength(0);
5
        int main_page_counter = 0;
6
        byte[] overflow_page = new byte[PAGE_SIZE];
7
        int overflow_off = 0;
8
        byte[] new_page = new byte[PAGE_SIZE];
9
        int new_off = 0;
10
        int tmp;
11
        ByteBuffer new_wrapper = ByteBuffer.wrap(new_page);
12
        ByteBuffer wrapper = ByteBuffer.wrap(this.databaseFile.getBuffer());
13
        ByteBuffer overflow_wrapper = ByteBuffer.wrap(overflow_page);
14
        this.databaseFile.setBufferOffset(0);
15
        this.databaseFile.seek(MAX_MAIN);
16
17
        try{
18-
            this.databaseFile.read(overflow_page);
18+
            this.databaseFile.read(overflow_page);	//reading overflow area
19
            this.read_counter++;
20-
            this.databaseFile.seek(0);
20+
            this.databaseFile.seek(0);		//setting file to start (main area)
21-
            while(main_page_counter < pageAmount && this.databaseFile.read(this.databaseFile.getBuffer()) != -1) {
21+
            while(main_page_counter < pageAmount && this.databaseFile.read(this.databaseFile.getBuffer()) != -1) {	//reading main area until the end (not to go to overflow)
22
                main_page_counter++;
23
                this.read_counter++;
24-
                while(this.databaseFile.getBuffer().length - this.databaseFile.getBufferOffset() >= RECORD_SIZE) {
24+
                while(this.databaseFile.getBuffer().length - this.databaseFile.getBufferOffset() >= RECORD_SIZE) { //until we exceed one page
25-
                    if(new_off == PAGE_SIZE - ALFA * RECORD_SIZE) {
25+
                    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
26
                        new_off = 0;
27
                        new_file.write(new_page);
28
                        write_counter++;
29
                        new_page = new byte[PAGE_SIZE];
30
                        new_wrapper = ByteBuffer.wrap(new_page);
31
                    }
32-
                    if(overflow_off / RECORD_SIZE == this.overflow_counter || wrapper.getInt(this.databaseFile.getBufferOffset()) < overflow_wrapper.getInt(overflow_off)) {
32+
                    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
33
                        new_wrapper.putInt(new_off, wrapper.getInt(this.databaseFile.getBufferOffset()));
34
                        new_wrapper.putFloat(new_off + 12, wrapper.getFloat(this.databaseFile.getBufferOffset() + 12));
35
                        new_wrapper.putDouble(new_off + 4, wrapper.getDouble(this.databaseFile.getBufferOffset() + 4));
36
                        new_wrapper.putInt(new_off + 16, wrapper.getInt(this.databaseFile.getBufferOffset() + 16));
37
                        new_off += RECORD_SIZE;
38
                        this.databaseFile.setBufferOffset(this.databaseFile.getBufferOffset() + RECORD_SIZE);
39-
                    } else {
39+
                    } else { //otherwise we write to new file the record from overflow area
40
                        new_wrapper.putInt(new_off, overflow_wrapper.getInt(overflow_off));
41
                        new_wrapper.putFloat(new_off + 12, overflow_wrapper.getFloat(overflow_off + 12));
42
                        new_wrapper.putDouble(new_off + 4, overflow_wrapper.getDouble(overflow_off + 4));
43
                        new_wrapper.putInt(new_off + 16, overflow_wrapper.getInt(overflow_off + 16));
44
                        new_off += RECORD_SIZE;
45
                        overflow_off += RECORD_SIZE;
46
                    }
47-
                    tmp = new_wrapper.getInt(new_off - 4);      //latest pointer
47+
                    tmp = new_wrapper.getInt(new_off - 4);      //get the pointer of the latest written to new file record
48
                        //if there's a list of elements
49-
                    while(tmp != 0 && overflow_off / RECORD_SIZE < this.overflow_counter) {
49+
                    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
50-
                        new_wrapper.putInt(new_off - 4, 0);
50+
                        new_wrapper.putInt(new_off - 4, 0);	//we write 0 to the place of the pointer 
51-
                        if(new_off == PAGE_SIZE - ALFA * RECORD_SIZE) {
51+
                        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
52
                            new_file.write(new_page);
53
                            this.write_counter++;
54
                            new_off = 0;
55
                            new_page = new byte[PAGE_SIZE];
56
                            new_wrapper = ByteBuffer.wrap(new_page);
57
                        }
58-
                        while(overflow_wrapper.getInt(overflow_off) != tmp) {
58+
                        while(overflow_wrapper.getInt(overflow_off) != tmp) {	//we get through the the overflow area until we got the right key
59
                            overflow_off += RECORD_SIZE;
60
                        }
61
                        new_wrapper.putInt(new_off, overflow_wrapper.getInt(overflow_off));
62
                        new_wrapper.putFloat(new_off + 12, overflow_wrapper.getFloat(overflow_off + 12));
63
                        new_wrapper.putDouble(new_off + 4, overflow_wrapper.getDouble(overflow_off + 4));
64
                        new_wrapper.putInt(new_off + 16, overflow_wrapper.getInt(overflow_off + 16));
65
                        new_off += RECORD_SIZE;
66
                        overflow_off += RECORD_SIZE;
67
                        tmp = new_wrapper.getInt(new_off - 4);
68
                    }
69
                }
70
                this.databaseFile.setBufferOffset(0);
71
            }
72
        }
73
        catch (Exception e) {
74
            //End of file
75
        }
76
        new_file.write(new_page);
77
        this.write_counter++;
78
        new_page = new byte[PAGE_SIZE];
79
        new_wrapper = ByteBuffer.wrap(new_page);
80
81
        //rewriting files and creating new index file
82
        this.indexFile.seek(0);
83
        new_file.seek(0);
84
        this.databaseFile.seek(0);
85
        int i = 1;
86
        int index_off = 0;
87
        this.indexFile.setBuffer(new byte[PAGE_SIZE]);
88
        ByteBuffer index_wrapper = ByteBuffer.wrap(this.indexFile.getBuffer());
89
        pageAmount = 0;
90
        this.databaseFile.setLength(0);
91-
        while(new_file.read(new_page) != -1) {
91+
        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
92
            pageAmount++;
93
            this.read_counter++;
94
            index_wrapper.putInt(index_off, new_wrapper.getInt(0));
95
            index_wrapper.putInt(index_off + 4, i++);
96
            index_off += INDEX_RECORD_SIZE;
97
            this.databaseFile.write(new_page);
98
            this.write_counter++;
99
        }
100
        this.indexFile.write(this.indexFile.getBuffer());
101
        this.databaseFile.write(new byte[PAGE_SIZE]);
102
        //overflow area
103
        new_file.close();
104
105
        this.MAX_MAIN = PAGE_SIZE * pageAmount;
106
        this.overflow_counter = 0;
107
        this.OVERFLOW_SIZE = 2 * pageAmount * RECORD_SIZE;
108
109
    }