Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Data Structure  RSS feed

 
Sean Riley
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am wanting to read a log file for several configurable strings. Ie. "ALERT", "CRITICAL", and "DOWN" for testing I simply want to count each occurrance of these.

I am not ready to step through the file as yet as I am still trying to figure out how to structure my data, let me explain.

I would like to have a hashmap with the 3 keys, the value associated with each key is an array consisting of the compiled pattern, the matcher and the count as an integer. So I would like it to look something like the following;



I would like matchingArray[] to contain something like



so from a config file I want to read the 3 keys (ALERT, CRITICAL and DOWN) loop through each of these and build the map with empty arrays then loop through each key of the map and add the compiled pattern, the relevant matcher and a 0 for the count.

Too complicated... what would be better... the reason I want to put the precompiled pattern and matcher into an array is so that I can FOR EACH LINE step through the matchers and add 1 to the last element of the array when a matcher.find returns true.
 
Sean Riley
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My current code looks as follows

 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is your question? Are you simply looking for a better way to do this?

First, I would create a LineMatcher class that held the compiled pattern and a count variable. It would have a method that would take a line as a parameter and would determine if there is a match. If so, then it should increment the count variable. Let's call this method "handleLine"

Then, I would create a class that reads a file line-by-line. It would have a collection of LineMatchers, and would call the "handleLine" method of each LineMatcher for each line. Let's call this class "LineMatcherFileReader"

Now all that's left is a couple of incidental things:
1). Creating LineMatchers (probably from a configuration file) and adding them to the LineMatcherFileReader's collection of LineMatchers.

2). Displaying the results at the end.

If you wanter to get beyond Beginners (which this is coming close too, anyway), you could have the LineMatcher as an Interface and define a class to implement it that does the counting.

The beauty of this design is that you could define other classes that implement LineMatcher that do more than counting (like sending an eMail if a CRITICAL line is discovered or some such).
 
Sean Riley
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joel you are a ledgend. Thank you. I fully understand your suggestions. Initially I was looking at doing it all in one class.

I was initially looking at how to structure collections of compiled patterns and counters etc. I will try your way now.

Thanks again.
Sean
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!