-
Notifications
You must be signed in to change notification settings - Fork 0
/
BufferPool.java
82 lines (70 loc) · 2.19 KB
/
BufferPool.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package datamining;
import java.nio.ByteBuffer;
/**
*
* @author travis
*/
public class BufferPool {
private final long[] bufferTable = new long[131072];
private static ByteBuffer[] pool = new ByteBuffer[131072];
private static int nodeByteSize; // (2t * 8) + ((2t-1) * 49) + 20
public BufferPool(int t) {
nodeByteSize = ((2 * t) * 8) + (((2 * t) - 1) * 57) + 20;
for (int i = 0; i < bufferTable.length; i++) {
bufferTable[i] = -1;
}
}
static int hash(int hashCode) {
hashCode ^= (hashCode >>> 20) ^ (hashCode >>> 12);
return hashCode ^ (hashCode >>> 7) ^ (hashCode >>> 4);
}
public ByteBuffer createBuffer() {
return ByteBuffer.allocate(nodeByteSize);
}
public ByteBuffer createBuffer(long child, int h) {
bufferTable[h] = child;
pool[h] = createBuffer();
return pool[h];
}
public ByteBuffer getBuffer(long child) {
int h = hash((int)child) & (bufferTable.length - 1);
if (bufferTable[h] == -1) { // buffer is uninitialized
return createBuffer(child, h);
} else if (isBufferAvailable(child, h)) { // buffer matched requested node
return pool[h];
} else {
pool[h].flip();
Node n = new Node(pool[h]);
n.diskWrite();
pool[h].clear();
return pool[h];
}
}
boolean isBufferAvailable(long child, int h) {
if (bufferTable[h] == child) {
return true;
} else {
bufferTable[h] = child;
return false;
}
}
public void closeBuffers() {
int used = 0, unused = 0;
for (ByteBuffer b : pool) {
if (b != null) {
b.flip();
Node n = new Node(b);
n.diskWrite();
used++;
} else {
unused++;
}
}
System.out.println(used+"/131072 buffers used, "+unused+"/131072 buffers unused.");
}
}