
In the presented scenario, you have been tasked with designing a system called LogSystem that efficiently handles time-based queries on logs. Each log in this system is characterized by a unique identifier and a timestamp formatted as Year:Month:Day:Hour:Minute:Second, where all numeric values are zero-padded to a standard length. The main functionalities of LogSystem include storing logs and retrieving logs based on a time range with varying granularity. Granularity in this context refers to the precision of the time range, which could be as broad as a year or as specific as a second. Thus, when retrieving logs, the system can ignore more specific time components based on the specified granularity, efficiently filtering logs that fall within the time bounds from start to end.
Input:
Output:
Explanation:
1 <= id <= 5002000 <= Year <= 20171 <= Month <= 121 <= Day <= 310 <= Hour <= 230 <= Minute, Second <= 59granularity is one of the values ["Year", "Month", "Day", "Hour", "Minute", "Second"].500 calls will be made to put and retrieve.LogSystem to prepare it for accepting logs through the LogSystem() function.put(id, timestamp) method, where each log is identified uniquely by an id and tagged with a timestamp.retrieve(start, end, granularity) method. This method will filter logs based on their timestamps constrained by start, end, and the defined granularity.LogSystem object is first instantiated. put) and queries (retrieve) remain efficient up to 500 operations.Here is the breakdown of the provided Java class, EventLogger, which is designed to manage a system for storing and querying event logs:
This class utilizes a TreeMap called eventMap where the keys are timestamps, and the values are event IDs.
Constructor (EventLogger): Initialize eventMap.
Method (storeEvent): Converts the time string timePoint into an integer array, then uses timeConverter to transform this into a unique long timestamp, which it uses as the key to store the eventId in eventMap.
Method (timeConverter): Adjusts the given time array elements for month, and day (if they are in the initial position of months or days of the month, respectively), then calculates and returns a long value representing the time in seconds since a fixed epoch adjusted by given values for month and day.
Method (fetchEvents): Fetches events that occurred between startTime and endTime using a granularity parameter that specifies how specific the time query should be. It pads upper and lower time limits based on granularity to include or exclude the end time and uses adjustGranularity to generate timestamps for querying the eventMap.
Method (adjustGranularity): Adjusts the parts of the input time according to the specified granularity, ensuring that, in the case of end-time adjustment, the search boundary includes all possible timestamps within the granularity level. After adjusting the time components, timeConverter is called to produce the granularity-adjusted timestamp.
This EventLogger class can store event IDs with specific timestamps and query them based on a range with specific granularity, making it highly suited for log management systems where precise event time querying is necessary.
0 Comments
Be the first to comment and share your perspective with the community.