OLD | NEW |
1 # Copyright 2016 The Chromium Authors. All rights reserved. | 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Represents a database of on-disk traces.""" | 5 """Represents a database of on-disk traces.""" |
6 | 6 |
7 import json | 7 import json |
8 from google_storage_util import ReadFromGoogleStorage | 8 from google_storage_util import ReadFromGoogleStorage |
9 | 9 |
10 | 10 |
11 class LoadingTraceDatabase(object): | 11 class LoadingTraceDatabase(object): |
12 def __init__(self, traces_dict): | 12 def __init__(self, traces_dict): |
13 """traces_dict is a dictionary mapping filenames of traces to metadata | 13 """traces_dict is a dictionary mapping filenames of traces to metadata |
14 about those traces.""" | 14 about those traces.""" |
15 self._traces_dict = traces_dict | 15 self._traces_dict = traces_dict |
16 | 16 |
17 def AddTrace(self, filename, trace_dict): | 17 def SetTrace(self, filename, trace_dict): |
18 """Adds a mapping from |filename| to |trace_dict| into the database.""" | 18 """Sets a mapping from |filename| to |trace_dict| into the database. |
19 assert filename not in self._traces_dict | 19 If there is an existing mapping for filename, it is replaced. |
| 20 """ |
20 self._traces_dict[filename] = trace_dict | 21 self._traces_dict[filename] = trace_dict |
21 | 22 |
22 def GetTraceFilesForURL(self, url): | 23 def GetTraceFilesForURL(self, url): |
23 """Given a URL, returns the set of filenames of traces that were generated | 24 """Given a URL, returns the set of filenames of traces that were generated |
24 for this URL.""" | 25 for this URL.""" |
25 trace_files = [f for f in self._traces_dict.keys() | 26 trace_files = [f for f in self._traces_dict.keys() |
26 if self._traces_dict[f]["url"] == url] | 27 if self._traces_dict[f]["url"] == url] |
27 return trace_files | 28 return trace_files |
28 | 29 |
29 def ToJsonDict(self): | 30 def ToJsonDict(self): |
(...skipping 16 matching lines...) Expand all Loading... |
46 """Returns an instance from a json file saved by ToJsonFile().""" | 47 """Returns an instance from a json file saved by ToJsonFile().""" |
47 with open(json_path) as input_file: | 48 with open(json_path) as input_file: |
48 return cls.FromJsonDict(json.load(input_file)) | 49 return cls.FromJsonDict(json.load(input_file)) |
49 | 50 |
50 @classmethod | 51 @classmethod |
51 def FromJsonFileInGoogleStorage(cls, json_google_storage_path): | 52 def FromJsonFileInGoogleStorage(cls, json_google_storage_path): |
52 """Returns an instance from a json file in Google Storage whose contents | 53 """Returns an instance from a json file in Google Storage whose contents |
53 were generated by ToJsonFile().""" | 54 were generated by ToJsonFile().""" |
54 json_string = ReadFromGoogleStorage(json_google_storage_path) | 55 json_string = ReadFromGoogleStorage(json_google_storage_path) |
55 return cls.FromJsonDict(json.loads(json_string)) | 56 return cls.FromJsonDict(json.loads(json_string)) |
OLD | NEW |