From 83fe770065c0f8dfb524a7e6aff0e98103b1224c Mon Sep 17 00:00:00 2001 From: dsandersoremutah Date: Tue, 19 Sep 2006 21:47:16 +0000 Subject: [PATCH] Added DbSystem.dbRename. Also added unit test for it. git-svn-id: https://svn.code.sf.net/p/flaim/code/trunk@875 0109f412-320b-0410-ab79-c3e0c5ffbbe6 --- xflaim/csharp/cstest/cstest.cs | 80 +++++++++++++++++++++++- xflaim/csharp/xflaim/DbRenameStatus.cs | 60 ++++++++++++++++++ xflaim/csharp/xflaim/DbSystem.cpp | 75 +++++++++++++++++++++++ xflaim/csharp/xflaim/DbSystem.cs | 85 ++++++++++++++++++++++++++ 4 files changed, 297 insertions(+), 3 deletions(-) create mode 100644 xflaim/csharp/xflaim/DbRenameStatus.cs diff --git a/xflaim/csharp/cstest/cstest.cs b/xflaim/csharp/cstest/cstest.cs index bd1998b..d09405a 100644 --- a/xflaim/csharp/cstest/cstest.cs +++ b/xflaim/csharp/cstest/cstest.cs @@ -33,6 +33,7 @@ namespace cstest { private const string CREATE_DB_NAME = "create.db"; private const string COPY_DB_NAME = "copy.db"; + private const string COPY2_DB_NAME = "copy2.db"; private const string RENAME_DB_NAME = "rename.db"; private const string RESTORE_DB_NAME = "restore.db"; private const string BACKUP_PATH = "backup"; @@ -183,6 +184,8 @@ namespace cstest // Copy database test. //-------------------------------------------------------------------------- static bool copyDbTest( + string sSrcDbName, + string sDestDbName, DbSystem dbSystem) { @@ -190,10 +193,10 @@ namespace cstest MyDbCopyStatus copyStatus = new MyDbCopyStatus(); - beginTest( "Copy Database Test (" + CREATE_DB_NAME + " --> " + COPY_DB_NAME + ")"); + beginTest( "Copy Database Test (" + sSrcDbName + " --> " + sDestDbName + ")"); try { - dbSystem.dbCopy( CREATE_DB_NAME, null, null, COPY_DB_NAME, null, null, copyStatus); + dbSystem.dbCopy( sSrcDbName, null, null, sDestDbName, null, null, copyStatus); } catch (XFlaimException ex) { @@ -377,6 +380,33 @@ namespace cstest return( true); } + //-------------------------------------------------------------------------- + // Rename database test. + //-------------------------------------------------------------------------- + static bool renameDbTest( + string sSrcDbName, + string sDestDbName, + DbSystem dbSystem) + { + + // Try renaming the database + + MyDbRenameStatus renameStatus = new MyDbRenameStatus(); + + beginTest( "Rename Database Test (" + sSrcDbName + " --> " + sDestDbName + ")"); + try + { + dbSystem.dbRename( sSrcDbName, null, null, sDestDbName, true, renameStatus); + } + catch (XFlaimException ex) + { + endTest( renameStatus.outputLines(), ex, "renaming database"); + return( false); + } + endTest( renameStatus.outputLines(), true); + return( true); + } + //-------------------------------------------------------------------------- // Main for tester program //-------------------------------------------------------------------------- @@ -400,7 +430,18 @@ namespace cstest // Database copy test - if (!copyDbTest( dbSystem)) + if (!copyDbTest( CREATE_DB_NAME, COPY_DB_NAME, dbSystem)) + { + return; + } + if (!copyDbTest( CREATE_DB_NAME, COPY2_DB_NAME, dbSystem)) + { + return; + } + + // Database rename test + + if (!renameDbTest( COPY2_DB_NAME, RENAME_DB_NAME, dbSystem)) { return; } @@ -433,6 +474,10 @@ namespace cstest { return; } + if (!checkDbTest( RENAME_DB_NAME, dbSystem)) + { + return; + } // Database remove test @@ -448,6 +493,10 @@ namespace cstest { return; } + if (!removeDbTest( dbSystem, RENAME_DB_NAME)) + { + return; + } } } @@ -882,4 +931,29 @@ namespace cstest private bool m_bOutputLines; } + + public class MyDbRenameStatus : DbRenameStatus + { + public MyDbRenameStatus() + { + m_bOutputLines = false; + System.Console.Write( "\n"); + } + + public RCODE dbRenameStatus( + string sSrcFileName, + string sDestFileName) + { + System.Console.WriteLine( "Renaming {0} to {1}", sSrcFileName, sDestFileName); + m_bOutputLines = true; + return( RCODE.NE_XFLM_OK); + } + + public bool outputLines() + { + return( m_bOutputLines); + } + + private bool m_bOutputLines; + } } diff --git a/xflaim/csharp/xflaim/DbRenameStatus.cs b/xflaim/csharp/xflaim/DbRenameStatus.cs new file mode 100644 index 0000000..50a490f --- /dev/null +++ b/xflaim/csharp/xflaim/DbRenameStatus.cs @@ -0,0 +1,60 @@ +//------------------------------------------------------------------------------ +// Desc: Db Rename Status +// +// Tabs: 3 +// +// Copyright (c) 2006 Novell, Inc. All Rights Reserved. +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of version 2 of the GNU General Public +// License as published by the Free Software Foundation. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, contact Novell, Inc. +// +// To contact Novell about this file by physical or electronic mail, +// you may find current contact information at www.novell.com +// +// $Id$ +//------------------------------------------------------------------------------ + +using System; +using System.Runtime.InteropServices; + +namespace xflaim +{ + + /// + /// This interface allows XFlaim to periodically pass information back to the + /// client about the status of an ongoing database copy operation. The + /// implementor may do anything it wants with the information, such as write + /// it to a log file or display it on the screen. + /// + public interface DbRenameStatus + { + + /// + /// The operation calls this method periodically to + /// inform the implementation object about the status of the rename operation. + /// + /// + /// The name of the file that is currently being renamed. + /// + /// + /// The name the source file is being renamed to. + /// + /// + /// If the implementation object returns anything but RCODE.NE_XFLM_OK + /// the operation will abort and throw an + /// + /// + RCODE dbRenameStatus( + string sSrcFileName, + string sDestFileName); + } +} diff --git a/xflaim/csharp/xflaim/DbSystem.cpp b/xflaim/csharp/xflaim/DbSystem.cpp index 1dec163..2feb784 100644 --- a/xflaim/csharp/xflaim/DbSystem.cpp +++ b/xflaim/csharp/xflaim/DbSystem.cpp @@ -806,3 +806,78 @@ Exit: return( rc); } + +typedef RCODE (FLMAPI * DB_RENAME_STATUS)( + const char * pszSrcFileName, + const char * pszDestFileName); + +/**************************************************************************** +Desc: +****************************************************************************/ +class CS_DbRenameStatus : public IF_DbRenameStatus +{ +public: + + CS_DbRenameStatus( + DB_RENAME_STATUS fnRenameStatus) + { + m_fnRenameStatus = fnRenameStatus; + } + + virtual ~CS_DbRenameStatus() + { + } + + RCODE FLMAPI dbRenameStatus( + const char * pszSrcFileName, + const char * pszDestFileName) + { + return( m_fnRenameStatus( pszSrcFileName, pszDestFileName)); + } + +private: + + DB_RENAME_STATUS m_fnRenameStatus; + +}; + +/**************************************************************************** +Desc: +****************************************************************************/ +FLMEXTC FLMEXP RCODE FLMAPI xflaim_DbSystem_dbRename( + FLMUINT64 ui64This, + const char * pszSrcDbName, + const char * pszSrcDataDir, + const char * pszSrcRflDir, + const char * pszDestDbName, + FLMBOOL bOverwriteDestOk, + DB_RENAME_STATUS fnRenameStatus) +{ + RCODE rc = NE_XFLM_OK; + IF_DbSystem * pDbSystem = ((IF_DbSystem *)(FLMUINT)ui64This); + IF_DbRenameStatus * pDbRenameStatus = NULL; + + if (fnRenameStatus) + { + if ((pDbRenameStatus = f_new CS_DbRenameStatus( fnRenameStatus)) == NULL) + { + rc = RC_SET( NE_XFLM_MEM); + goto Exit; + } + } + + if (RC_BAD( rc = pDbSystem->dbRename( pszSrcDbName, pszSrcDataDir, pszSrcRflDir, pszDestDbName, + bOverwriteDestOk, pDbRenameStatus))) + { + goto Exit; + } + +Exit: + + if (pDbRenameStatus) + { + pDbRenameStatus->Release(); + } + + return( rc); +} diff --git a/xflaim/csharp/xflaim/DbSystem.cs b/xflaim/csharp/xflaim/DbSystem.cs index 8522638..900181f 100644 --- a/xflaim/csharp/xflaim/DbSystem.cs +++ b/xflaim/csharp/xflaim/DbSystem.cs @@ -916,6 +916,81 @@ namespace xflaim private DbCopyStatus m_dbCopyStatus; } + /// + /// Rename a database. + /// + /// + /// The name of the control file of the database to be renamed. + /// + /// + /// The data file directory. See for more information. + /// + /// + /// The roll-forward log file directory. See for more information. + /// + /// + /// The new control file name for the database. + /// + /// + /// If true, then if the data specified in sNewDbName already exists, it will be overwritten. + /// + /// + /// If non-null this is an object that implements the + /// interface. It is a callback object that is used to report rename progress. + /// + public void dbRename( + string sDbName, + string sDataDir, + string sRflDir, + string sNewDbName, + bool bOverwriteDestOk, + DbRenameStatus renameStatus) + { + int rc; + DbRenameStatusDelegate dbRenameStatus = null; + DbRenameStatusCallback fnDbRenameStatus = null; + + if (renameStatus != null) + { + dbRenameStatus = new DbRenameStatusDelegate( renameStatus); + fnDbRenameStatus = new DbRenameStatusCallback( dbRenameStatus.funcDbRenameStatus); + } + + if ((rc = xflaim_DbSystem_dbRename( m_pDbSystem, sDbName, sDataDir, sRflDir, sNewDbName, + (int)(bOverwriteDestOk ? 1 : 0), fnDbRenameStatus)) != 0) + { + throw new XFlaimException( rc); + } + } + + private delegate RCODE DbRenameStatusCallback( + IntPtr pszSrcFileName, + IntPtr pszDestFileName); + + private class DbRenameStatusDelegate + { + public DbRenameStatusDelegate( + DbRenameStatus dbRenameStatus) + { + m_dbRenameStatus = dbRenameStatus; + } + + ~DbRenameStatusDelegate() + { + } + + public RCODE funcDbRenameStatus( + IntPtr pszSrcFileName, + IntPtr pszDestFileName) + { + return( m_dbRenameStatus.dbRenameStatus( + Marshal.PtrToStringAnsi( pszSrcFileName), + Marshal.PtrToStringAnsi( pszDestFileName))); + } + + private DbRenameStatus m_dbRenameStatus; + } + // PRIVATE METHODS THAT ARE IMPLEMENTED IN C AND C++ [DllImport("xflaim")] @@ -988,6 +1063,16 @@ namespace xflaim [MarshalAs(UnmanagedType.LPStr)] string pszDestRflDir, DbCopyStatusCallback fnDbCopyStatus); + [DllImport("xflaim",CharSet=CharSet.Ansi)] + private static extern int xflaim_DbSystem_dbRename( + ulong pDbSystem, + [MarshalAs(UnmanagedType.LPStr)] string pszSrcDbName, + [MarshalAs(UnmanagedType.LPStr)] string pszSrcDataDir, + [MarshalAs(UnmanagedType.LPStr)] string pszSrcRflDir, + [MarshalAs(UnmanagedType.LPStr)] string pszDestDbName, + int bOverwriteDestOk, + DbRenameStatusCallback fnDbRenameStatus); + private ulong m_pDbSystem; } }