00001 #include "Basis/CmdOptions.h"
00002 #include "Util/Channel.h"
00003 #include "Util/ChannelPool.h"
00004 #include "Util/IOBuffer.h"
00005 #include "Util/IOBufferFile.h"
00006 #include "Util/Database.h"
00007
00008
00009 #include "Link/ImpalaLib.cpp"
00010
00011 namespace Impala
00012 {
00013 namespace Application
00014 {
00015 namespace DataTransfer
00016 {
00017
00018
00019 static const int BUF_SIZE = 1048576;
00020
00021
00022 void
00023 TransferLocalToServer(std::string localFilename, std::string serverFilename)
00024 {
00025 ILOG_VAR(Impala.Application.DataTransfer.TransferLocalToServer);
00026
00027
00028 Util::IOBuffer* zip = new Util::IOBufferFile(localFilename, true, false);
00029 Impala::Util::Database& database(Impala::Util::Database::GetInstance());
00030 Util::IOBuffer* destination = database.GetIOBuffer(serverFilename, false, false, "", zip->AvailableInt32());
00031 ILOG_INFO("File to copy to dataserver has " << zip->AvailableInt32() << " bytes");
00032 static unsigned char buffer[BUF_SIZE];
00033 while(true)
00034 {
00035 int bytesread = zip->Read(buffer, BUF_SIZE);
00036 if(bytesread == 0) break;
00037 destination->Write(buffer, bytesread);
00038 }
00039 delete zip;
00040
00041 delete destination;
00042 }
00043
00044
00045 void
00046 TransferServerToLocal(std::string serverFilename, std::string localFilename)
00047 {
00048 ILOG_VAR(Impala.Application.DataTransfer.TransferServerToLocal);
00049
00050
00051 Util::IOBuffer* destination = new Util::IOBufferFile(localFilename, false, false);
00052 Impala::Util::Database& database(Impala::Util::Database::GetInstance());
00053 Util::IOBuffer* server = database.GetIOBuffer(serverFilename, true, false, "");
00054 ILOG_INFO("File to copy from dataserver has " << server->AvailableInt32() << " bytes");
00055 static unsigned char buffer[BUF_SIZE];
00056 while(true)
00057 {
00058 int bytesread = server->Read(buffer, BUF_SIZE);
00059 if(bytesread == 0) break;
00060 destination->Write(buffer, bytesread);
00061 }
00062 delete server;
00063 delete destination;
00064 }
00065
00066
00067 int
00068 mainDataTransfer(int argc, char* argv[])
00069 {
00070 CmdOptions& options = CmdOptions::GetInstance();
00071 options.Initialise(false, false, true);
00072 std::string usageStr = "cmd file1 file2\n\n";
00073 usageStr += " cmd = upload|download\n";
00074 if (! options.ParseArgs(argc, argv, usageStr, 3))
00075 return 1;
00076
00077 ILOG_VAR(Impala.Application.DataTransfer.mainDataTransfer);
00078
00079 std::string cmd = options.GetArg(0);
00080 if (cmd == std::string("upload"))
00081 {
00082 TransferLocalToServer(options.GetArg(1), options.GetArg(2));
00083 }
00084 else if (cmd == std::string("download"))
00085 {
00086 TransferServerToLocal(options.GetArg(1), options.GetArg(2));
00087 }
00088
00089 return 0;
00090 }
00091
00092
00093 }
00094 }
00095 }
00096
00097
00098 int
00099 main(int argc, char* argv[])
00100 {
00101 return Impala::Application::DataTransfer::mainDataTransfer(argc, argv);
00102 }