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