/
TableCache.java
executable file
·146 lines (131 loc) · 4.87 KB
/
TableCache.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* Copyright (C) 2011 the original author or authors.
* See the notice.md file distributed with this work for additional
* information regarding copyright ownership.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.iq80.leveldb.impl;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalNotification;
import org.iq80.leveldb.table.FileChannelTable;
import org.iq80.leveldb.table.MMapTable;
import org.iq80.leveldb.table.Table;
import org.iq80.leveldb.table.UserComparator;
import org.iq80.leveldb.util.Closeables;
import org.iq80.leveldb.util.Finalizer;
import org.iq80.leveldb.util.InternalTableIterator;
import org.iq80.leveldb.util.Slice;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.concurrent.ExecutionException;
import static java.util.Objects.requireNonNull;
public class TableCache
{
private final LoadingCache<Long, TableAndFile> cache;
private final Finalizer<Table> finalizer = new Finalizer<>(1);
public TableCache(final File databaseDir, int tableCacheSize, final UserComparator userComparator, final boolean verifyChecksums)
{
requireNonNull(databaseDir, "databaseName is null");
cache = CacheBuilder.newBuilder()
.maximumSize(tableCacheSize)
.removalListener(new RemovalListener<Long, TableAndFile>()
{
@Override
public void onRemoval(RemovalNotification<Long, TableAndFile> notification)
{
Table table = notification.getValue().getTable();
finalizer.addCleanup(table, table.closer());
}
})
.build(new CacheLoader<Long, TableAndFile>()
{
@Override
public TableAndFile load(Long fileNumber)
throws IOException
{
return new TableAndFile(databaseDir, fileNumber, userComparator, verifyChecksums);
}
});
}
public InternalTableIterator newIterator(FileMetaData file)
{
return newIterator(file.getNumber());
}
public InternalTableIterator newIterator(long number)
{
return new InternalTableIterator(getTable(number).iterator());
}
public long getApproximateOffsetOf(FileMetaData file, Slice key)
{
return getTable(file.getNumber()).getApproximateOffsetOf(key);
}
private Table getTable(long number)
{
Table table;
try {
table = cache.get(number).getTable();
}
catch (ExecutionException e) {
Throwable cause = e;
if (e.getCause() != null) {
cause = e.getCause();
}
throw new RuntimeException("Could not open table " + number, cause);
}
return table;
}
public void close()
{
cache.invalidateAll();
finalizer.destroy();
}
public void evict(long number)
{
cache.invalidate(number);
}
private static final class TableAndFile
{
private final Table table;
private TableAndFile(File databaseDir, long fileNumber, UserComparator userComparator, boolean verifyChecksums)
throws IOException
{
String tableFileName = Filename.tableFileName(fileNumber);
File tableFile = new File(databaseDir, tableFileName);
FileInputStream fis = null;
try {
fis = new FileInputStream(tableFile);
FileChannel fileChannel = fis.getChannel();
if (Iq80DBFactory.USE_MMAP) {
table = new MMapTable(tableFile.getAbsolutePath(), fileChannel, userComparator, verifyChecksums);
}
else {
table = new FileChannelTable(tableFile.getAbsolutePath(), fileChannel, userComparator, verifyChecksums);
}
}
catch (IOException ioe) {
Closeables.closeQuietly(fis);
throw ioe;
}
}
public Table getTable()
{
return table;
}
}
}