提交 2bd33848 authored 作者: Thomas Mueller's avatar Thomas Mueller

Page store: respect cache size at startup

上级 13dcb511
......@@ -396,6 +396,21 @@ public class ConnectionInfo implements Cloneable {
return value.toString();
}
/**
* Get the value of the given property.
*
* @param key the property key
* @param defaultValue the default value
* @return the value as a String
*/
public int getProperty(String key, int defaultValue) {
if (SysProperties.CHECK && !isKnownSetting(key)) {
Message.throwInternalError(key);
}
String s = getProperty(key);
return s == null ? defaultValue : Integer.parseInt(s);
}
/**
* Get the value of the given property.
*
......
......@@ -169,6 +169,7 @@ public class Database implements DataHandler {
private Properties reconnectLastLock;
private volatile long reconnectCheckNext;
private volatile boolean reconnectChangePending;
private int cacheSize;
public Database(String name, ConnectionInfo ci, String cipher) throws SQLException {
this.compareMode = CompareMode.getInstance(null, 0);
......@@ -182,6 +183,7 @@ public class Database implements DataHandler {
this.accessModeData = ci.getProperty("ACCESS_MODE_DATA", "rw").toLowerCase();
this.autoServerMode = ci.getProperty("AUTO_SERVER", false);
this.usePageStore = ci.getProperty("PAGE_STORE", SysProperties.getPageStore());
this.cacheSize = ci.getProperty("CACHE_SIZE", SysProperties.CACHE_SIZE_DEFAULT);
if ("r".equals(accessModeData)) {
readOnly = true;
accessModeLog = "r";
......@@ -1767,11 +1769,15 @@ public class Database implements DataHandler {
}
public synchronized void setCacheSize(int kb) throws SQLException {
cacheSize = kb;
if (fileData != null) {
fileData.getCache().setMaxSize(kb);
int valueIndex = kb <= 32 ? kb : (kb >>> SysProperties.CACHE_SIZE_INDEX_SHIFT);
fileIndex.getCache().setMaxSize(valueIndex);
}
if (pageStore != null) {
pageStore.getCache().setMaxSize(kb);
}
}
public synchronized void setMasterUser(User user) throws SQLException {
......@@ -2250,8 +2256,7 @@ public class Database implements DataHandler {
public PageStore getPageStore() throws SQLException {
if (pageStore == null && usePageStore) {
pageStore = new PageStore(this, databaseName + Constants.SUFFIX_PAGE_FILE, accessModeData,
SysProperties.CACHE_SIZE_DEFAULT);
pageStore = new PageStore(this, databaseName + Constants.SUFFIX_PAGE_FILE, accessModeData, cacheSize);
pageStore.open();
}
return pageStore;
......
......@@ -208,6 +208,10 @@ public class PageDataLeaf extends PageData {
// free up the space used by the row
rowRef = new SoftReference<Row>(rows[0]);
rows[0] = null;
Data all = index.getPageStore().createData();
all.checkCapacity(data.length());
all.write(data.getBytes(), 0, data.length());
data.truncate(index.getPageStore().getPageSize());
do {
int type, size, next;
if (remaining <= pageSize - PageDataOverflow.START_LAST) {
......@@ -219,14 +223,13 @@ public class PageDataLeaf extends PageData {
size = pageSize - PageDataOverflow.START_MORE;
next = index.getPageStore().allocatePage();
}
PageDataOverflow overflow = new PageDataOverflow(index, page, type, previous, next, data, dataOffset, size);
PageDataOverflow overflow = new PageDataOverflow(index, page, type, previous, next, all, dataOffset, size);
index.getPageStore().updateRecord(overflow, true, null);
dataOffset += size;
remaining -= size;
previous = page;
page = next;
} while (remaining > 0);
data.truncate(index.getPageStore().getPageSize());
}
return -1;
}
......
......@@ -167,7 +167,6 @@ public class PageStore implements CacheWriter {
private long writeCount;
private int logFirstTrunkPage, logFirstDataPage;
private int cacheSize;
private Cache cache;
private int freeListPagesPerList;
......@@ -218,9 +217,8 @@ public class PageStore implements CacheWriter {
trace = database.getTrace(Trace.PAGE_STORE);
// int test;
// trace.setLevel(TraceSystem.DEBUG);
this.cacheSize = cacheSizeDefault;
String cacheType = database.getCacheType();
this.cache = CacheLRU.getCache(this, cacheType, cacheSize);
this.cache = CacheLRU.getCache(this, cacheType, cacheSizeDefault);
systemSession = new Session(database, null, 0);
}
......@@ -1130,7 +1128,7 @@ public class PageStore implements CacheWriter {
private void removeMeta(int logPos, Row row) throws SQLException {
int id = row.getValue(0).getInt();
Index index = metaObjects.remove(id);
Index index = metaObjects.get(id);
int headPos = index.getHeadPos();
index.getTable().removeIndex(index);
if (index instanceof PageBtreeIndex) {
......@@ -1143,6 +1141,7 @@ public class PageStore implements CacheWriter {
index.getSchema().remove(index);
}
index.remove(systemSession);
metaObjects.remove(id);
if (reservedPages != null && reservedPages.containsKey(headPos)) {
// re-allocate the page if it is used later on again
int latestPos = reservedPages.get(headPos);
......@@ -1398,6 +1397,10 @@ public class PageStore implements CacheWriter {
return metaRootPageId.get(indexId);
}
public Cache getCache() {
return cache;
}
// TODO implement checksum
// private void updateChecksum(byte[] d, int pos) {
// int ps = pageSize;
......
......@@ -87,6 +87,8 @@ public class Recover extends Tool implements DataHandler {
private long pageDataEmpty;
private int pageDataRows;
private int pageDataHead;
private int pageSize;
private FileStore store;
/**
* Options are case sensitive. Supported options are:
......@@ -723,7 +725,6 @@ public class Recover extends Tool implements DataHandler {
private void dumpPageStore(String fileName) {
setDatabaseName(fileName.substring(0, fileName.length() - Constants.SUFFIX_PAGE_FILE.length()));
FileStore store = null;
PrintWriter writer = null;
int[] pageTypeCount = new int[Page.TYPE_STREAM_DATA + 2];
int emptyPages = 0;
......@@ -746,7 +747,7 @@ public class Recover extends Tool implements DataHandler {
store.seek(0);
store.readFully(s.getBytes(), 0, 128);
s.setPos(48);
int pageSize = s.readInt();
pageSize = s.readInt();
int writeVersion = s.readByte();
int readVersion = s.readByte();
writer.println("-- pageSize: " + pageSize +
......@@ -815,7 +816,7 @@ public class Recover extends Tool implements DataHandler {
int columnCount = s.readVarInt();
int entries = s.readShortInt();
writer.println("-- page " + page + ": data leaf " + (last ? "(last)" : "") + " table: " + storageId + " entries: " + entries + " columns: " + columnCount);
dumpPageDataLeaf(store, pageSize, writer, s, last, page, columnCount, entries);
dumpPageDataLeaf(writer, s, last, page, columnCount, entries);
break;
}
// type 2
......@@ -856,7 +857,7 @@ public class Recover extends Tool implements DataHandler {
case Page.TYPE_FREE_LIST:
pageTypeCount[type]++;
writer.println("-- page " + page + ": free list " + (last ? "(last)" : ""));
free += dumpPageFreeList(writer, s, pageSize, page, pageCount);
free += dumpPageFreeList(writer, s, page, pageCount);
break;
// type 7
case Page.TYPE_STREAM_TRUNK:
......@@ -875,7 +876,7 @@ public class Recover extends Tool implements DataHandler {
}
writeSchema(writer);
try {
dumpPageLogStream(writer, store, logFirstTrunkPage, logFirstDataPage, pageSize);
dumpPageLogStream(writer, logFirstTrunkPage, logFirstDataPage);
} catch (EOFException e) {
// ignore
}
......@@ -896,7 +897,7 @@ public class Recover extends Tool implements DataHandler {
}
}
private void dumpPageLogStream(PrintWriter writer, FileStore store, int logFirstTrunkPage, int logFirstDataPage, int pageSize) throws IOException, SQLException {
private void dumpPageLogStream(PrintWriter writer, int logFirstTrunkPage, int logFirstDataPage) throws IOException, SQLException {
Data s = Data.create(this, pageSize);
DataInputStream in = new DataInputStream(
new PageInputStream(writer, this, store, logFirstTrunkPage, logFirstDataPage, pageSize)
......@@ -1115,7 +1116,7 @@ public class Recover extends Tool implements DataHandler {
writer.println("-- [" + entryCount + "] child: " + children[entryCount] + " rowCount: " + rowCount);
}
private int dumpPageFreeList(PrintWriter writer, Data s, int pageSize, long pageId, long pageCount) {
private int dumpPageFreeList(PrintWriter writer, Data s, long pageId, long pageCount) {
int pagesAddressed = PageFreeList.getPagesAddressed(pageSize);
BitField used = new BitField();
for (int i = 0; i < pagesAddressed; i += 8) {
......@@ -1171,7 +1172,7 @@ public class Recover extends Tool implements DataHandler {
}
}
private void dumpPageDataLeaf(FileStore store, int pageSize, PrintWriter writer, Data s, boolean last, long pageId, int columnCount, int entryCount) throws SQLException {
private void dumpPageDataLeaf(PrintWriter writer, Data s, boolean last, long pageId, int columnCount, int entryCount) throws SQLException {
long[] keys = new long[entryCount];
int[] offsets = new int[entryCount];
long next = 0;
......
......@@ -32,6 +32,7 @@ public class TestPageStore extends TestBase {
}
public void test() throws Exception {
testRecoverDropIndex();
testDropPk();
testCreatePkLater();
testTruncate();
......@@ -41,6 +42,30 @@ public class TestPageStore extends TestBase {
testFuzzOperations();
}
private void testRecoverDropIndex() throws SQLException {
if (config.memory) {
return;
}
deleteDb("pageStore");
Connection conn = getConnection("pageStore");
Statement stat = conn.createStatement();
stat.execute("set write_delay 0");
stat.execute("create table test(id int, name varchar) as select x, x from system_range(1, 1400)");
stat.execute("create index idx_name on test(name)");
conn.close();
conn = getConnection("pageStore");
stat = conn.createStatement();
stat.execute("drop index idx_name");
stat.execute("shutdown immediately");
try {
conn.close();
} catch (SQLException e) {
// ignore
}
conn = getConnection("pageStore;cache_size=1");
conn.close();
}
private void testDropPk() throws SQLException {
if (config.memory) {
return;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论