00001
00002
00003
00004
00005 #include <algorithm>
00006 #include <functional>
00007 #include <iostream>
00008
00009 #include <ace/OS_main.h>
00010
00011 #include <oess_1/io/h/mem_buf.hpp>
00012
00013 #include <oess_1/db/cln/h/local_db.hpp>
00014
00015
00016
00017
00018
00019 class slice_copier_t
00020 : public std::unary_function< const std::string &, void >
00021 {
00022 private :
00023
00024 oess_1::db::cln::db_t & m_source_db;
00025
00026 oess_1::db::cln::db_t & m_dest_db;
00027
00028 void
00029 copy_slice_content( const std::string & slice_name )
00030 {
00031 oess_1::io::mem_buf_t buffer;
00032 unsigned int objects_copied = 0;
00033 unsigned int bytes_copied = 0;
00034
00035 oess_1::ent_id_t ent;
00036 while( ( ent = m_source_db.ent_find_next(
00037 slice_name,
00038 ent ) ) )
00039 {
00040 buffer.change_size( 0 );
00041 oess_1::uint_t ent_size = m_source_db.ent_raw_load(
00042 ent, buffer );
00043
00044 buffer.set_pos( 0 );
00045 oess_1::ent_id_t copied_ent = m_dest_db.ent_raw_create(
00046 slice_name,
00047 buffer,
00048 ent_size );
00049
00050 ++objects_copied;
00051 bytes_copied += ent_size;
00052
00053 std::cout << slice_name << ": " << ent
00054 << " (" << ent_size << " bytes) -> "
00055 << copied_ent << std::endl;
00056 }
00057
00058 std::cout << slice_name << ": "
00059 << objects_copied << " objects, "
00060 << bytes_copied << " bytes copied"
00061 << std::endl;
00062 }
00063
00064 public :
00065 slice_copier_t(
00066 oess_1::db::cln::db_t & source_db,
00067 oess_1::db::cln::db_t & dest_db )
00068 : m_source_db( source_db )
00069 , m_dest_db( dest_db )
00070 {}
00071
00072 result_type
00073 operator()( argument_type slice_name )
00074 {
00075 std::cout << slice_name << ": creating" << std::endl;
00076 m_dest_db.slice_create( slice_name );
00077
00078 copy_slice_content( slice_name );
00079 }
00080 };
00081
00082 void
00083 copy_opened_db_content(
00084 oess_1::db::cln::db_t & source_db,
00085 oess_1::db::cln::db_t & dest_db )
00086 {
00087
00088 std::set< std::string > slices = source_db.slice_names();
00089 if( !slices.empty() )
00090 {
00091
00092 std::for_each(
00093 slices.begin(),
00094 slices.end(),
00095 slice_copier_t( source_db, dest_db ) );
00096 }
00097 else
00098 std::cout << "no slices to copy" << std::endl;
00099 }
00100
00101 void
00102 copy_db_content(
00103 const std::string & source_db_name,
00104 const std::string & dest_db_name )
00105 {
00106
00107 oess_1::db::cln::local_db_t source_db(
00108 source_db_name,
00109 oess_1::db::cln::local_db_t::no_overwrite |
00110 oess_1::db::cln::local_db_t::read_only_mode );
00111
00112
00113
00114
00115 oess_1::db::storage::config_t empty_config;
00116 oess_1::db::cln::local_db_t dest_db(
00117 dest_db_name,
00118 oess_1::db::storage::load_cfg_file(
00119 empty_config.make_cfg_file_name( source_db_name ) ),
00120 oess_1::db::cln::local_db_t::auto_create );
00121
00122 copy_opened_db_content(
00123 *source_db.query_db(),
00124 *dest_db.query_db() );
00125 }
00126
00127 int
00128 main( int argc, char ** argv )
00129 {
00130 if( 3 != argc )
00131 {
00132 std::cerr << "sample_db_content_copy <old_db> <new_db>"
00133 << std::endl;
00134 return 1;
00135 }
00136
00137 try
00138 {
00139 copy_db_content( argv[ 1 ], argv[ 2 ] );
00140 }
00141 catch( const std::exception & x )
00142 {
00143 std::cerr << "exception caght: " << x.what() << std::endl;
00144 return 2;
00145 }
00146
00147 return 0;
00148 }
00149