Skip to content

Commit

Permalink
make from,to parameters are omittable.
Browse files Browse the repository at this point in the history
  • Loading branch information
tristar committed Aug 17, 2014
1 parent 1d9b706 commit a9ad8f4
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 6 deletions.
25 changes: 20 additions & 5 deletions src/grep_command.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,35 @@ namespace tigrep {
std::string date_string_buffer;
boost::cmatch matches;

while(status_ != kSTATE_END && !ist_->eof()) {
while(status_ != kSTATE_END) {

ist_->getline(read_buffer, buffer_size);
if(ist_->bad()) {
throw std::runtime_error("read file error.");
}
if(ist_->eof() && read_buffer[0] == 0) {
//If EOF found, and current line is empty, stop log scanning.
//(Prevent double empty line sending to ost_ stream)
break;
}
current_line_++;

//現在の行に含まれている時刻を取得する
//Get a date part from current line.
if(!boost::regex_search(read_buffer, matches, config_.pattern)) {
//取得できない場合は現在の状態に応じた処理を実行
//If current line does not include a date part,
//continue processing according to the status_ variable.
dispatch(read_buffer);
continue;
}
//取得できた場合は時刻を元に現在の状態を判定
//If current line includes a date part,
//update status_ variable responding to the date part.
date_string_buffer = matches.str(1);
updateStatus(date_string_buffer);

//現在の状態に応じた処理を実行
//Processing action according to the status_ variable.
dispatch(read_buffer);
}
status_ = kSTATE_END;

delete[] read_buffer;
}
Expand Down Expand Up @@ -125,13 +133,20 @@ namespace tigrep {

switch(status_) {
case kSTATE_SCAN:
if(config_.start_date_time == 0) {
status_ = kSTATE_OUTPUT;
break;
}
//Update status to kSTATE_OUTPUT if date_string exceeds config_.start_date_time.
date_compare.setBaseDateTime(config_.start_date_time);
if(date_compare.compare(date_string.c_str(), config_.format.c_str(), util::DateCompare::kIS_GTE)) {
status_ = kSTATE_OUTPUT;
}
break;
case kSTATE_OUTPUT:
if(config_.end_date_time == 0) {
break;
}
//Update status to kSTATE_END if date_string exceeds config_.end_date_time.
date_compare.setBaseDateTime(config_.end_date_time);
if(date_compare.compare(date_string.c_str(), config_.format.c_str(), util::DateCompare::kIS_GT)) {
Expand Down
108 changes: 107 additions & 1 deletion src/tests/grep_command_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
namespace tigrep {


//check that GrepCommand recognize valid start/end position.
//-----------------------------------------------------------------------
//check that GrepCommand recognize valid start/end position.
TEST(grep_command, basic) {

std::istringstream ist(
Expand Down Expand Up @@ -52,4 +53,109 @@ namespace tigrep {
ASSERT_TRUE(ist_for_check.eof());

}

//-----------------------------------------------------------------------
//check that scan start from first line when "from" parameter was omitted.
TEST(grep_command, omit_from_parameter) {

std::istringstream ist(
"abcdefg\n"
"[2014-08-08 10:00:00] some-text1\n"
"[2014-08-08 11:00:00] some-text2\n"
"[2014-08-08 12:00:00] some-text3\n"
"[2014-08-08 13:00:00] some-text4\n"
"[2014-08-08 14:00:00] some-text5\n"
);
std::ostringstream ost;

GrepConfig config;

config.pattern = boost::regex("^\\[([0-9\\-:\\s]+?)\\]");
config.format = "%Y-%m-%d %H:%M:%S";
config.start_date_time = 0; //0 means that parameter omitted.
config.end_date_time = 1407470400; //date -d "2014-08-08 13:00:00" +%s
GrepCommand grep_command(&ist, &ost, config);

grep_command.execute();


std::istringstream ist_for_check(ost.str().c_str());
char buffer[4096];
memset(buffer, 0, 4096);

//check grep_command.execute() result.
//
// When "from" parameter was omitted, the line that does not include a date part are skipped
// until find the first line that include a date part.
// This is current restriction.
//
ist_for_check.getline(buffer, 4096);
ASSERT_STREQ("[2014-08-08 10:00:00] some-text1", buffer);

//read next line.
ist_for_check.getline(buffer, 4096);
ASSERT_STREQ("[2014-08-08 11:00:00] some-text2", buffer);

//read next line.
ist_for_check.getline(buffer, 4096);
ASSERT_STREQ("[2014-08-08 12:00:00] some-text3", buffer);

//read next line.
ist_for_check.getline(buffer, 4096);
ASSERT_STREQ("[2014-08-08 13:00:00] some-text4", buffer);

//expect no lines left.
ist_for_check.getline(buffer, 4096);
ASSERT_STREQ("", buffer);
ASSERT_TRUE(ist_for_check.eof());
}

//-----------------------------------------------------------------------
//check that scan reaches EOF when "to" parameter was omitted.
TEST(grep_command, omit_to_parameter) {

std::istringstream ist(
"abcdefg\n"
"[2014-08-08 10:00:00] some-text1\n"
"[2014-08-08 11:00:00] some-text2\n"
"[2014-08-08 12:00:00] some-text3\n"
"[2014-08-08 13:00:00] some-text4\n"
"[2014-08-08 14:00:00] some-text5\n"
);
std::ostringstream ost;

GrepConfig config;

config.pattern = boost::regex("^\\[([0-9\\-:\\s]+?)\\]");
config.format = "%Y-%m-%d %H:%M:%S";
config.start_date_time = 1407463200; //date -d "2014-08-08 11:00:00" +%s
config.end_date_time = 0;
GrepCommand grep_command(&ist, &ost, config);

grep_command.execute();


std::istringstream ist_for_check(ost.str().c_str());
char buffer[4096];
memset(buffer, 0, 4096);

//check grep_command.execute() result.
ist_for_check.getline(buffer, 4096);
ASSERT_STREQ("[2014-08-08 11:00:00] some-text2", buffer);

ist_for_check.getline(buffer, 4096);
ASSERT_STREQ("[2014-08-08 12:00:00] some-text3", buffer);

ist_for_check.getline(buffer, 4096);
ASSERT_STREQ("[2014-08-08 13:00:00] some-text4", buffer);

ist_for_check.getline(buffer, 4096);
ASSERT_STREQ("[2014-08-08 14:00:00] some-text5", buffer);

//expect no lines left.
ist_for_check.getline(buffer, 4096);
ASSERT_STREQ("", buffer);
ASSERT_TRUE(ist_for_check.eof());
}

} //namespace

0 comments on commit a9ad8f4

Please sign in to comment.