Design a log storage system that stores logs using a unique integer id and a timestamp.
Each timestamp is a string in the format Year:Month:Day:Hour:Minute:Second.
For example, "2017:01:01:23:59:59" is a valid timestamp.
Every timestamp field is a zero-padded decimal number.
The system must support adding logs and retrieving the ids of logs whose timestamps fall inside a given time range, using a requested time granularity.
Class Definition
Implement the class LogStorageSystem.
Method Signatures
Constructor
LogStorageSystem()
Initializes an empty log storage system.
Store A Log
void put(int id, String timestamp)
Stores a log with the given unique id and timestamp.
Each id is unique across all calls to put.
Retrieve Logs
List<Integer> retrieve(String start, String end, String granularity)
Returns the ids of all logs whose timestamps are inside the inclusive range from start to end, considering only the timestamp fields up to the given granularity.
The returned list must be sorted in ascending order by log id.
Granularity
The value of
granularity determines how much of each timestamp is considered during comparison.
Valid granularity values are:
"Year": compare only Year
"Month": compare Year:Month
"Day": compare Year:Month:Day
"Hour": compare Year:Month:Day:Hour
"Minute": compare Year:Month:Day:Hour:Minute
"Second": compare the full timestamp
Range Rules
A log should be returned if its timestamp falls inside the inclusive range from start to end after applying the requested granularity.
For example, if granularity = "Day", then only the Year, Month, and Day fields are compared.
If a log has timestamp "2016:04:15:23:59:59", it is treated as "2016:04:15" for "Day" granularity.
Constraints
- There will be at most
300 total calls to put and retrieve.
2000 ≤ Year ≤ 2017
01 ≤ Month ≤ 12
01 ≤ Day ≤ 31
00 ≤ Hour ≤ 23
00 ≤ Minute ≤ 59
00 ≤ Second ≤ 59
- Each timestamp is always valid and follows the exact format
Year:Month:Day:Hour:Minute:Second.
- Each log id passed to
put is unique.
start and end are valid timestamps in the same format as stored log timestamps.
start is less than or equal to end after applying the requested granularity.
granularity is one of "Year", "Month", "Day", "Hour", "Minute", or "Second".
- Never use
null as a parameter value.
Examples
Example 1
LogStorageSystem logStorageSystem = new LogStorageSystem()
Output: LogStorageSystem object created
logStorageSystem.put(id = 10, timestamp = "2017:03:05:10:20:30")
Output: void
logStorageSystem.put(id = 5, timestamp = "2016:12:31:23:59:59")
Output: void
logStorageSystem.put(id = 8, timestamp = "2017:01:01:00:00:00")
Output: void
logStorageSystem.retrieve(start = "2016:01:01:00:00:00", end = "2017:12:31:23:59:59", granularity = "Year")
Output: [5, 8, 10]
Explanation:
With "Year" granularity, all logs from years 2016 and 2017 are included. The matching ids are returned in ascending order.
Example 2
LogStorageSystem logStorageSystem = new LogStorageSystem()
Output: LogStorageSystem object created
logStorageSystem.put(id = 4, timestamp = "2015:06:10:08:30:00")
Output: void
logStorageSystem.put(id = 2, timestamp = "2015:06:10:09:15:45")
Output: void
logStorageSystem.put(id = 7, timestamp = "2015:06:11:08:00:00")
Output: void
logStorageSystem.retrieve(start = "2015:06:10:08:00:00", end = "2015:06:10:09:59:59", granularity = "Hour")
Output: [2, 4]
Explanation:
With "Hour" granularity, the range includes logs from "2015:06:10:08" through "2015:06:10:09". Logs with ids 4 and 2 match, and the returned output is sorted by id.
Example 3
LogStorageSystem logStorageSystem = new LogStorageSystem()
Output: LogStorageSystem object created
logStorageSystem.put(id = 6, timestamp = "2014:02:20:10:10:10")
Output: void
logStorageSystem.put(id = 1, timestamp = "2014:02:20:10:10:11")
Output: void
logStorageSystem.put(id = 3, timestamp = "2014:02:20:10:11:00")
Output: void
logStorageSystem.retrieve(start = "2014:02:20:10:10:10", end = "2014:02:20:10:10:10", granularity = "Second")
Output: [6]
Explanation:
With "Second" granularity, the full timestamp is compared. Only the log with id 6 exactly matches the given second.