use of org.h2.mvstore.db.MVTableEngine.Store in project h2database by h2database.
the class ScriptBase method initStore.
private void initStore() {
Database db = session.getDatabase();
byte[] key = null;
if (cipher != null && password != null) {
char[] pass = password.optimize(session).getValue(session).getString().toCharArray();
key = SHA256.getKeyPasswordHash("script", pass);
}
String file = getFileName();
store = FileStore.open(db, file, "rw", cipher, key);
store.setCheckedWriting(false);
store.init();
}
use of org.h2.mvstore.db.MVTableEngine.Store in project h2database by h2database.
the class ScriptBase method openOutput.
/**
* Open the output stream.
*/
void openOutput() {
String file = getFileName();
if (file == null) {
return;
}
if (isEncrypted()) {
initStore();
out = new FileStoreOutputStream(store, this, compressionAlgorithm);
// always use a big buffer, otherwise end-of-block is written a lot
out = new BufferedOutputStream(out, Constants.IO_BUFFER_SIZE_COMPRESS);
} else {
OutputStream o;
try {
o = FileUtils.newOutputStream(file, false);
} catch (IOException e) {
throw DbException.convertIOException(e, null);
}
out = new BufferedOutputStream(o, Constants.IO_BUFFER_SIZE);
out = CompressTool.wrapOutputStream(out, compressionAlgorithm, SCRIPT_SQL);
}
}
use of org.h2.mvstore.db.MVTableEngine.Store in project h2database by h2database.
the class Database method open.
private synchronized void open(int traceLevelFile, int traceLevelSystemOut) {
if (persistent) {
String dataFileName = databaseName + Constants.SUFFIX_OLD_DATABASE_FILE;
boolean existsData = FileUtils.exists(dataFileName);
String pageFileName = databaseName + Constants.SUFFIX_PAGE_FILE;
String mvFileName = databaseName + Constants.SUFFIX_MV_FILE;
boolean existsPage = FileUtils.exists(pageFileName);
boolean existsMv = FileUtils.exists(mvFileName);
if (existsData && (!existsPage && !existsMv)) {
throw DbException.get(ErrorCode.FILE_VERSION_ERROR_1, "Old database: " + dataFileName + " - please convert the database " + "to a SQL script and re-create it.");
}
if (existsPage && !FileUtils.canWrite(pageFileName)) {
readOnly = true;
}
if (existsMv && !FileUtils.canWrite(mvFileName)) {
readOnly = true;
}
if (existsPage && !existsMv) {
dbSettings.mvStore = false;
}
if (readOnly) {
if (traceLevelFile >= TraceSystem.DEBUG) {
String traceFile = Utils.getProperty("java.io.tmpdir", ".") + "/" + "h2_" + System.currentTimeMillis();
traceSystem = new TraceSystem(traceFile + Constants.SUFFIX_TRACE_FILE);
} else {
traceSystem = new TraceSystem(null);
}
} else {
traceSystem = new TraceSystem(databaseName + Constants.SUFFIX_TRACE_FILE);
}
traceSystem.setLevelFile(traceLevelFile);
traceSystem.setLevelSystemOut(traceLevelSystemOut);
trace = traceSystem.getTrace(Trace.DATABASE);
trace.info("opening {0} (build {1})", databaseName, Constants.BUILD_ID);
if (autoServerMode) {
if (readOnly || fileLockMethod == FileLockMethod.NO || fileLockMethod == FileLockMethod.SERIALIZED || fileLockMethod == FileLockMethod.FS || !persistent) {
throw DbException.getUnsupportedException("autoServerMode && (readOnly || " + "fileLockMethod == NO || " + "fileLockMethod == SERIALIZED || " + "fileLockMethod == FS || " + "inMemory)");
}
}
String lockFileName = databaseName + Constants.SUFFIX_LOCK_FILE;
if (readOnly) {
if (FileUtils.exists(lockFileName)) {
throw DbException.get(ErrorCode.DATABASE_ALREADY_OPEN_1, "Lock file exists: " + lockFileName);
}
}
if (!readOnly && fileLockMethod != FileLockMethod.NO) {
if (fileLockMethod != FileLockMethod.FS) {
lock = new FileLock(traceSystem, lockFileName, Constants.LOCK_SLEEP);
lock.lock(fileLockMethod);
if (autoServerMode) {
startServer(lock.getUniqueId());
}
}
}
if (SysProperties.MODIFY_ON_WRITE) {
while (isReconnectNeeded()) {
// wait until others stopped writing
}
} else {
while (isReconnectNeeded() && !beforeWriting()) {
// wait until others stopped writing and
// until we can write (the file is not yet open -
// no need to re-connect)
}
}
deleteOldTempFiles();
starting = true;
if (SysProperties.MODIFY_ON_WRITE) {
try {
getPageStore();
} catch (DbException e) {
if (e.getErrorCode() != ErrorCode.DATABASE_IS_READ_ONLY) {
throw e;
}
pageStore = null;
while (!beforeWriting()) {
// wait until others stopped writing and
// until we can write (the file is not yet open -
// no need to re-connect)
}
getPageStore();
}
} else {
getPageStore();
}
starting = false;
if (mvStore == null) {
writer = WriterThread.create(this, writeDelay);
} else {
setWriteDelay(writeDelay);
}
} else {
if (autoServerMode) {
throw DbException.getUnsupportedException("autoServerMode && inMemory");
}
traceSystem = new TraceSystem(null);
trace = traceSystem.getTrace(Trace.DATABASE);
if (dbSettings.mvStore) {
getPageStore();
}
}
systemUser = new User(this, 0, SYSTEM_USER_NAME, true);
mainSchema = new Schema(this, 0, Constants.SCHEMA_MAIN, systemUser, true);
infoSchema = new Schema(this, -1, "INFORMATION_SCHEMA", systemUser, true);
schemas.put(mainSchema.getName(), mainSchema);
schemas.put(infoSchema.getName(), infoSchema);
publicRole = new Role(this, 0, Constants.PUBLIC_ROLE_NAME, true);
roles.put(Constants.PUBLIC_ROLE_NAME, publicRole);
systemUser.setAdmin(true);
systemSession = new Session(this, systemUser, ++nextSessionId);
lobSession = new Session(this, systemUser, ++nextSessionId);
CreateTableData data = new CreateTableData();
ArrayList<Column> cols = data.columns;
Column columnId = new Column("ID", Value.INT);
columnId.setNullable(false);
cols.add(columnId);
cols.add(new Column("HEAD", Value.INT));
cols.add(new Column("TYPE", Value.INT));
cols.add(new Column("SQL", Value.STRING));
boolean create = true;
if (pageStore != null) {
create = pageStore.isNew();
}
data.tableName = "SYS";
data.id = 0;
data.temporary = false;
data.persistData = persistent;
data.persistIndexes = persistent;
data.create = create;
data.isHidden = true;
data.session = systemSession;
meta = mainSchema.createTable(data);
IndexColumn[] pkCols = IndexColumn.wrap(new Column[] { columnId });
metaIdIndex = meta.addIndex(systemSession, "SYS_ID", 0, pkCols, IndexType.createPrimaryKey(false, false), true, null);
objectIds.set(0);
starting = true;
Cursor cursor = metaIdIndex.find(systemSession, null, null);
ArrayList<MetaRecord> records = New.arrayList();
while (cursor.next()) {
MetaRecord rec = new MetaRecord(cursor.get());
objectIds.set(rec.getId());
records.add(rec);
}
Collections.sort(records);
synchronized (systemSession) {
for (MetaRecord rec : records) {
rec.execute(this, systemSession, eventListener);
}
}
if (mvStore != null) {
mvStore.initTransactions();
mvStore.removeTemporaryMaps(objectIds);
}
recompileInvalidViews(systemSession);
starting = false;
if (!readOnly) {
// set CREATE_BUILD in a new database
String name = SetTypes.getTypeName(SetTypes.CREATE_BUILD);
if (settings.get(name) == null) {
Setting setting = new Setting(this, allocateObjectId(), name);
setting.setIntValue(Constants.BUILD_ID);
lockMeta(systemSession);
addDatabaseObject(systemSession, setting);
}
// mark all ids used in the page store
if (pageStore != null) {
BitSet f = pageStore.getObjectIds();
for (int i = 0, len = f.length(); i < len; i++) {
if (f.get(i) && !objectIds.get(i)) {
trace.info("unused object id: " + i);
objectIds.set(i);
}
}
}
}
getLobStorage().init();
systemSession.commit(true);
trace.info("opened {0}", databaseName);
if (checkpointAllowed > 0) {
afterWriting();
}
}
use of org.h2.mvstore.db.MVTableEngine.Store in project h2database by h2database.
the class SessionRemote method openFile.
@Override
public FileStore openFile(String name, String mode, boolean mustExist) {
if (mustExist && !FileUtils.exists(name)) {
throw DbException.get(ErrorCode.FILE_NOT_FOUND_1, name);
}
FileStore store;
if (cipher == null) {
store = FileStore.open(this, name, mode);
} else {
store = FileStore.open(this, name, mode, cipher, fileEncryptionKey, 0);
}
store.setCheckedWriting(false);
try {
store.init();
} catch (DbException e) {
store.closeSilently();
throw e;
}
return store;
}
use of org.h2.mvstore.db.MVTableEngine.Store in project h2database by h2database.
the class Recover method dumpMVStoreFile.
private void dumpMVStoreFile(PrintWriter writer, String fileName) {
writer.println("-- MVStore");
writer.println("CREATE ALIAS IF NOT EXISTS READ_BLOB FOR \"" + this.getClass().getName() + ".readBlob\";");
writer.println("CREATE ALIAS IF NOT EXISTS READ_CLOB FOR \"" + this.getClass().getName() + ".readClob\";");
writer.println("CREATE ALIAS IF NOT EXISTS READ_BLOB_DB FOR \"" + this.getClass().getName() + ".readBlobDb\";");
writer.println("CREATE ALIAS IF NOT EXISTS READ_CLOB_DB FOR \"" + this.getClass().getName() + ".readClobDb\";");
writer.println("CREATE ALIAS IF NOT EXISTS READ_BLOB_MAP FOR \"" + this.getClass().getName() + ".readBlobMap\";");
writer.println("CREATE ALIAS IF NOT EXISTS READ_CLOB_MAP FOR \"" + this.getClass().getName() + ".readClobMap\";");
resetSchema();
setDatabaseName(fileName.substring(0, fileName.length() - Constants.SUFFIX_MV_FILE.length()));
MVStore mv = new MVStore.Builder().fileName(fileName).readOnly().open();
dumpLobMaps(writer, mv);
writer.println("-- Meta");
dumpMeta(writer, mv);
writer.println("-- Tables");
TransactionStore store = new TransactionStore(mv);
try {
store.init();
} catch (Throwable e) {
writeError(writer, e);
}
try {
for (String mapName : mv.getMapNames()) {
if (!mapName.startsWith("table.")) {
continue;
}
String tableId = mapName.substring("table.".length());
ValueDataType keyType = new ValueDataType(null, this, null);
ValueDataType valueType = new ValueDataType(null, this, null);
TransactionMap<Value, Value> dataMap = store.begin().openMap(mapName, keyType, valueType);
Iterator<Value> dataIt = dataMap.keyIterator(null);
boolean init = false;
while (dataIt.hasNext()) {
Value rowId = dataIt.next();
Value[] values = ((ValueArray) dataMap.get(rowId)).getList();
recordLength = values.length;
if (!init) {
setStorage(Integer.parseInt(tableId));
// init the column types
for (valueId = 0; valueId < recordLength; valueId++) {
String columnName = storageName + "." + valueId;
getSQL(columnName, values[valueId]);
}
createTemporaryTable(writer);
init = true;
}
StringBuilder buff = new StringBuilder();
buff.append("INSERT INTO O_").append(tableId).append(" VALUES(");
for (valueId = 0; valueId < recordLength; valueId++) {
if (valueId > 0) {
buff.append(", ");
}
String columnName = storageName + "." + valueId;
buff.append(getSQL(columnName, values[valueId]));
}
buff.append(");");
writer.println(buff.toString());
if (storageId == 0) {
try {
SimpleRow r = new SimpleRow(values);
MetaRecord meta = new MetaRecord(r);
schema.add(meta);
if (meta.getObjectType() == DbObject.TABLE_OR_VIEW) {
String sql = values[3].getString();
String name = extractTableOrViewName(sql);
tableMap.put(meta.getId(), name);
}
} catch (Throwable t) {
writeError(writer, t);
}
}
}
}
writeSchema(writer);
writer.println("DROP ALIAS READ_BLOB_MAP;");
writer.println("DROP ALIAS READ_CLOB_MAP;");
writer.println("DROP TABLE IF EXISTS INFORMATION_SCHEMA.LOB_BLOCKS;");
} catch (Throwable e) {
writeError(writer, e);
} finally {
mv.close();
}
}
Aggregations