📄 backuprestore.java
字号:
// You can redistribute this software and/or modify it under the terms of
// the Ozone Library License version 1 published by ozone-db.org.
//
// The original code and portions created by SMB are
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
//
// $Id: BackupRestore.java,v 1.8 2004/01/03 10:39:41 per_nyfelt Exp $
package org.ozoneDB.core.admin;
import org.ozoneDB.DxLib.DxIterator;
import org.ozoneDB.OzoneCompatible;
import org.ozoneDB.OzoneInterface;
import org.ozoneDB.core.*;
import org.ozoneDB.core.xml.*;
import org.ozoneDB.util.LogWriter;
import org.ozoneDB.xml.util.*;
import org.xml.sax.helpers.AttributesImpl;
import java.io.*;
/**
* This class handles all backup/restore related operations of the ozone admin
* system.<p>
*
* Note: The backup/restore does not use the object specific object to XML
* mapping. The backup always uses the standard mapping defined by {@link
* org.ozoneDB.core.xml.Object2XML}.
*
* @version $Revision: 1.8 $ $Date: 2004/01/03 10:39:41 $
* @author <a href="http://www.smb-tec.com">SMB</a>
* @see Admin
*/
public class BackupRestore implements XML2ObjectDelegate {
protected final static String TMP_FILE_NAME = "oids.tmp";
protected final static String BACKUP_ELEMENT_NAME = "ozone-database-backup";
protected transient Env env;
protected transient SAXChunkProducer backupProducer;
protected transient DataInputStream backupIn;
protected transient SAXChunkConsumer restoreConsumer;
protected transient int idcount;
public BackupRestore( Env _env ) {
env = _env;
}
public void beginRestore() throws Exception {
env.logWriter.newEntry( this, "beginRestore()...", LogWriter.INFO );
if (restoreConsumer != null) {
throw new IllegalStateException( "There is a restore process already in progress." );
}
XML2ObjectContentHandler restoreHandler = new XML2ObjectContentHandler( this );
restoreConsumer = new SAXChunkConsumer( restoreHandler );
}
public void processRestoreChunk( byte[] chunk ) throws Exception {
if (restoreConsumer == null) {
throw new IllegalStateException( "Start the restore process first." );
}
if (chunk == null) {
restoreConsumer = null;
}
else {
System.out.println( "" );
env.logWriter.newEntry( this, "processRestoreChunk(): size=" + chunk.length, LogWriter.INFO );
restoreConsumer.processChunk( chunk );
}
}
/**
* This method is inherited from XML2ObjectDelegate. It is called when an
* entire object is re-constructed from the XML stream.
*/
public void handleObject( ObjElement element ) {
System.out.print( '.' );
OzoneCompatible obj = (OzoneCompatible)element.getObject();
ObjectID id = new ObjectID( Long.parseLong( (String)element.getOzoneObjectId() ) );
String name = (String)element.getOzoneObjectName();
env.logWriter.newEntry( this, "handleObject(): " + element.getClassName() + ": oid=" + id + ", target=" + obj, LogWriter.DEBUG3 );
try {
ObjectContainer container = env.transactionManager.currentTA().createObject(null,OzoneInterface.Public,name,null,null,id);
try {
container.setTarget( obj );
} finally {
container.unpin();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void beginBackup() throws Exception {
if (backupIn != null) {
throw new IllegalStateException( "There is a backup process already in progress." );
}
backupProducer = new SAXChunkProducer( (SAXChunkProducerDelegate)null );
backupProducer.startDocument();
backupProducer.startElement( "", BACKUP_ELEMENT_NAME, BACKUP_ELEMENT_NAME, new AttributesImpl() );
env.logWriter.newEntry( this, "preparing list of object IDs...", LogWriter.INFO );
idcount = 0;
DataOutputStream out = new DataOutputStream( new BufferedOutputStream( new FileOutputStream( env.getDatabaseDir() + TMP_FILE_NAME ), 4096 ) );
DxIterator backupIt = env.storeManager.objectIDIterator();
while (backupIt.next() != null) {
out.writeLong( ((ObjectID)backupIt.key()).value() );
idcount ++;
}
out.close();
env.logWriter.newEntry( this, "preparing list of object IDs... ready. (" + idcount + " IDs)", LogWriter.INFO );
idcount = 0;
backupIn = new DataInputStream( new BufferedInputStream( new FileInputStream( env.getDatabaseDir() + TMP_FILE_NAME ), 4096 ) );
}
private ObjectID nextBackupID() throws Exception {
if (backupIn == null) {
throw new IllegalStateException( "Start backup process first." );
}
try {
return new ObjectID( backupIn.readLong() );
}
catch (Exception e) {
backupIn.close();
backupIn = null;
new File( env.getDatabaseDir() + TMP_FILE_NAME ).delete();
if (e instanceof EOFException) {
return null;
} else {
throw e;
}
}
}
public byte[] nextBackupChunk() throws Exception {
Transaction ta = env.transactionManager.currentTA();
if (env.logWriter.hasTarget(LogWriter.DEBUG3)) {
env.logWriter.newEntry(this, "current transaction: " + ta, LogWriter.DEBUG3 );
}
if (backupProducer == null) {
return null;
}
ChunkOutputStream cos = backupProducer.chunkStream();
ObjectID id = null;
// this order of the expressions is important
while (cos.getState() != ChunkOutputStream.STATE_OVERFLOW && (id=nextBackupID()) != null) {
if (env.logWriter.hasTarget(LogWriter.DEBUG3)) {
env.logWriter.newEntry(this, "chunk size: " + cos.size() + " oid: " + id, LogWriter.DEBUG3 );
}
ObjectContainer container = ta.acquireObject( id, Lock.LEVEL_READ );
try {
OzoneCompatible target = container.target();
if (env.logWriter.hasTarget(LogWriter.DEBUG3)) {
env.logWriter.newEntry(this, "target: " + target + "[" + target.getClass().getName() + "]", LogWriter.DEBUG3 );
}
if (target instanceof org.ozoneDB.core.admin.AdminImpl) {
continue;
}
Object2XML o2x = new Object2XML( backupProducer, true );
o2x.toXML( target );
} finally {
container.unpin();
}
}
if (id == null) {
backupProducer.endElement( "", BACKUP_ELEMENT_NAME, BACKUP_ELEMENT_NAME );
backupProducer.endDocument();
cos.setEndFlag();
// backupIt = null;
backupProducer = null;
}
byte[] temp = cos.toByteArray();
cos.reset();
env.logWriter.newEntry( this, "nextBackupChunk(): size=" + temp.length, LogWriter.DEBUG );
return temp;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -