Fork me on GitHub

Design Log Storage System

Description

You are given several logs that each log contains a unique id and timestamp. Timestamp is a string that has the following format: Year:Month:Day:Hour:Minute:Second, for example, 2017:01:01:23:59:59. All domains are zero-padded decimal numbers.

Design a log storage system to implement the following functions:

void Put(int id, string timestamp): Given a log’s unique id and timestamp, store the log in your storage system.

int[] Retrieve(String start, String end, String granularity): Return the id of logs whose timestamps are within the range from start to end. Start and end all have the same format as timestamp. However, granularity means the time level for consideration. For example, start = “2017:01:01:23:59:59”, end = “2017:01:02:23:59:59”, granularity = “Day”, it means that we need to find the logs within the range from Jan. 1st 2017 to Jan. 2nd 2017.

Example 1:

1
2
3
4
5
put(1, "2017:01:01:23:59:59");
put(2, "2017:01:01:22:59:59");
put(3, "2016:01:01:00:00:00");
retrieve("2016:01:01:01:01:01","2017:01:01:23:00:00","Year"); // return [1,2,3], because you need to return all logs within 2016 and 2017.
retrieve("2016:01:01:01:01:01","2017:01:01:23:00:00","Hour"); // return [1,2], because you need to return all logs start from 2016:01:01:01 to 2017:01:01:23, where log 3 is left outside the range.

Note:

  1. There will be at most 300 operations of Put or Retrieve.
  2. Year ranges from [2000,2017]. Hour ranges from [00,23].
  3. Output for Retrieve has no order required.

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
class Time {
public:
int year;
int month;
int day;
int hour;
int minute;
int second;
bool operator<(const Time& t2) const {
if (year != t2.year) return year < t2.year;
if (month != t2.month) return month < t2.month;
if (day != t2.day) return day < t2.day;
if (hour != t2.hour) return hour < t2.hour;
if (minute != t2.minute) return minute < t2.minute;
if (second != t2.second) return second < t2.second;
return false;
}
Time(string& s) {
year = stoi(s.substr(0,4));
month = stoi(s.substr(5, 2));
day = stoi(s.substr(8, 2));
hour = stoi(s.substr(11, 2));
minute = stoi(s.substr(14, 2));
second = stoi(s.substr(17, 2));
}
};



class LogSystem {
private:
map<Time, int> mapping;
public:
LogSystem() {

}

void put(int id, string timestamp) {
Time time(timestamp);
mapping[time] = id;
}

vector<int> retrieve(string s, string e, string gra) {
Time start(s);
Time end(e);
if (gra == "Second") return findKeys(start, end);
start.second = 0;
end.second = 59;
if (gra == "Minute") return findKeys(start, end);
start.minute = 0;
end.minute = 59;
if (gra == "Hour") return findKeys(start, end);
start.hour = 0;
end.hour = 23;
if (gra == "Day") return findKeys(start, end);
start.day = 0;
end.day = 30;
if (gra == "Month") return findKeys(start, end);
start.month = 0;
end.month = 12;

if (gra == "Year") return findKeys(start, end);
start.year = 2000;
end.year = 2018;
return vector<int>();
}

vector<int> findKeys(Time& start, Time& end) {
vector<int> ret;
auto startIter = mapping.lower_bound(start);
auto endIter = mapping.upper_bound(end);
for(auto iter = startIter; iter != endIter; ++iter) ret.push_back(iter->second);
return ret;
}
};

/**
* Your LogSystem object will be instantiated and called as such:
* LogSystem obj = new LogSystem();
* obj.put(id,timestamp);
* vector<int> param_2 = obj.retrieve(s,e,gra);
*/