-
Notifications
You must be signed in to change notification settings - Fork 248
/
Copy pathDatabase.java
81 lines (70 loc) · 2.46 KB
/
Database.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
package simpledb.common;
import simpledb.storage.BufferPool;
import simpledb.storage.LogFile;
import java.io.*;
import java.util.concurrent.atomic.AtomicReference;
/**
* Database is a class that initializes several static variables used by the
* database system (the catalog, the buffer pool, and the log files, in
* particular.)
* <p>
* Provides a set of methods that can be used to access these variables from
* anywhere.
*
* @Threadsafe
*/
public class Database {
private static final AtomicReference<Database> _instance = new AtomicReference<>(new Database());
private final Catalog _catalog;
private final BufferPool _bufferpool;
private final static String LOGFILENAME = "log";
private final LogFile _logfile;
private Database() {
_catalog = new Catalog();
_bufferpool = new BufferPool(BufferPool.DEFAULT_PAGES);
LogFile tmp = null;
try {
tmp = new LogFile(new File(LOGFILENAME));
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
_logfile = tmp;
// startControllerThread();
}
/** Return the log file of the static Database instance */
public static LogFile getLogFile() {
return _instance.get()._logfile;
}
/** Return the buffer pool of the static Database instance */
public static BufferPool getBufferPool() {
return _instance.get()._bufferpool;
}
/** Return the catalog of the static Database instance */
public static Catalog getCatalog() {
return _instance.get()._catalog;
}
/**
* Method used for testing -- create a new instance of the buffer pool and
* return it
*/
public static BufferPool resetBufferPool(int pages) {
java.lang.reflect.Field bufferPoolF=null;
try {
bufferPoolF = Database.class.getDeclaredField("_bufferpool");
bufferPoolF.setAccessible(true);
bufferPoolF.set(_instance.get(), new BufferPool(pages));
} catch (NoSuchFieldException | IllegalAccessException | IllegalArgumentException | SecurityException e) {
e.printStackTrace();
}
// _instance._bufferpool = new BufferPool(pages);
return _instance.get()._bufferpool;
}
// reset the database, used for unit tests only.
public static void reset() {
if (_instance != null) {
_instance.get()._logfile.close();
}
_instance.set(new Database());
}
}