
sceloglauxalbifacies
   
Профиль
Группа: Экс. модератор
Сообщений: 2929
Регистрация: 16.6.2006
Репутация: нет Всего: 158
|
RayR, с таким подходом ты апокалипсиса дождешься быстрее, чем твой код на 150-мегах отработает...  инсертить надо пачками, причем побольше... да еще и прогресс-индикатор на каждой строке дергать - негоже. вобщем, вот тебе набросок: Unit1.h: | Код | //---------------------------------------------------------------------------
#ifndef Unit1H #define Unit1H //--------------------------------------------------------------------------- #include <Classes.hpp> #include <Controls.hpp> #include <StdCtrls.hpp> #include <Forms.hpp> #include <ADODB.hpp> #include <DB.hpp> #include <Dialogs.hpp> #include "CGAUGES.h" //--------------------------------------------------------------------------- class TForm1 : public TForm { __published: // IDE-managed Components TButton *Button1; TMemo *Memo1; TADOQuery *ADOQuery1; TOpenDialog *OpenDialog1; TCGauge *CGauge1; void __fastcall Button1Click(TObject *Sender); private: // User declarations public: // User declarations __fastcall TForm1(TComponent* Owner);
int RecordsPerInsert; unsigned int __fastcall ImportLogFromFile(AnsiString AFileName); }; //--------------------------------------------------------------------------- extern PACKAGE TForm1 *Form1; //--------------------------------------------------------------------------- #endif
|
Unit1.cpp: | Код | //--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include <stdio.h> #include <DateUtils.hpp>
#include "Unit1.h" //---------------------------------------------------------------------------
struct TSquidLogRecord { char time[32], duration[32], ip_adr[64], squid_result_code[64], http_status[4], bytes[16], request_method[64], url[1024], user_name[64], hierarchy_code[64], host[1024], type[64]; }; //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma link "CGAUGES" #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { RecordsPerInsert = 100; } //---------------------------------------------------------------------------
unsigned int __fastcall TForm1::ImportLogFromFile(AnsiString AFileName) { static char sb[4096]; static TSquidLogRecord sr; FILE *log = fopen(AFileName.c_str(), "r"); if (!log) return -1; fseek(log, 0, SEEK_END); // ставим указатель в конец файла unsigned int LogFileSize = ftell(log); // узнаем позицию указателя fseek(log, 0, SEEK_SET); // ставим указатель на начало файла TDateTime start = Now(); //TInsertCache *ic = new TInsertCache(ADOQuery1, RecordsPerInsert); TStringList *InsertCache = new TStringList(); InsertCache->Add("INSERT INTO PLAData VALUES"); CGauge1->Progress = 0; CGauge1->MaxValue = 100; unsigned int RecCount = 0, ProcessedSize = 0; while (fgets(sb, sizeof(sb), log)) { sscanf(sb, "%s %s %s %[A-Z_]/%s %s %s %s %s %[A-Z_]/%s %s", sr.time, sr.duration, sr.ip_adr, sr.squid_result_code, sr.http_status, sr.bytes, sr.request_method, sr.url, sr.user_name, sr.hierarchy_code, sr.host, sr.type); RecCount++; ProcessedSize += strlen(sb); AnsiString s; s.printf("%c('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')", (InsertCache->Count > 1 ? ',' : ' '), sr.time,sr.duration,sr.ip_adr,sr.squid_result_code,sr.http_status,sr.bytes, sr.request_method,sr.url,sr.user_name,sr.hierarchy_code,sr.host,sr.type); InsertCache->Add(s); if (InsertCache->Count - 1 == RecordsPerInsert) { ADOQuery1->SQL = InsertCache; ADOQuery1->ExecSQL(); int InsCount = LogFileSize / (ProcessedSize / RecCount) / RecordsPerInsert; if (CGauge1->MaxValue != InsCount) CGauge1->MaxValue = InsCount; CGauge1->Progress++; Application->ProcessMessages(); InsertCache->Clear(); InsertCache->Add("INSERT INTO PLAData VALUES"); } } if (InsertCache->Count > 1) { ADOQuery1->SQL = InsertCache; ADOQuery1->ExecSQL(); } CGauge1->Progress = CGauge1->MaxValue; delete InsertCache; fclose(log); int run = MilliSecondSpan(Now(), start); ShowMessage( IntToStr(run) + "ms, count=" + IntToStr(RecCount) ); } //---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender) { if (OpenDialog1->Execute()) { ImportLogFromFile(OpenDialog1->FileName); } } //---------------------------------------------------------------------------
|
|