Show More
@@ -1,110 +1,111 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 |
|
2 | |||
3 | # Copyright (C) 2010-2020 RhodeCode GmbH |
|
3 | # Copyright (C) 2010-2020 RhodeCode GmbH | |
4 | # |
|
4 | # | |
5 | # This program is free software: you can redistribute it and/or modify |
|
5 | # This program is free software: you can redistribute it and/or modify | |
6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
6 | # it under the terms of the GNU Affero General Public License, version 3 | |
7 | # (only), as published by the Free Software Foundation. |
|
7 | # (only), as published by the Free Software Foundation. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU Affero General Public License |
|
14 | # You should have received a copy of the GNU Affero General Public License | |
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | # |
|
16 | # | |
17 | # This program is dual-licensed. If you wish to learn more about the |
|
17 | # This program is dual-licensed. If you wish to learn more about the | |
18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
18 | # RhodeCode Enterprise Edition, including its added features, Support services, | |
19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ | |
20 |
|
20 | |||
21 | """ |
|
21 | """ | |
22 | Custom LRU memory manager for debugging purposes. It allows to track the keys |
|
22 | Custom LRU memory manager for debugging purposes. It allows to track the keys | |
23 | and the state of LRU dict. |
|
23 | and the state of LRU dict. | |
24 |
|
24 | |||
25 | inrae.cache is licensed under LRUDict is licensed under ZPL license |
|
25 | inrae.cache is licensed under LRUDict is licensed under ZPL license | |
26 | This software is Copyright (c) Zope Corporation (tm) and |
|
26 | This software is Copyright (c) Zope Corporation (tm) and | |
27 | Contributors. All rights reserved. |
|
27 | Contributors. All rights reserved. | |
28 | """ |
|
28 | """ | |
29 |
|
29 | |||
30 | import logging |
|
30 | import logging | |
31 |
|
31 | |||
32 | from repoze.lru import LRUCache |
|
32 | from repoze.lru import LRUCache | |
33 | from beaker.container import MemoryNamespaceManager, AbstractDictionaryNSManager |
|
33 | from beaker.container import MemoryNamespaceManager, AbstractDictionaryNSManager | |
34 | from rhodecode.lib.utils2 import safe_str |
|
34 | from rhodecode.lib.utils2 import safe_str | |
35 |
|
35 | |||
36 | log = logging.getLogger(__name__) |
|
36 | log = logging.getLogger(__name__) | |
37 |
|
37 | |||
38 |
|
38 | |||
39 | class LRUDict(LRUCache): |
|
39 | class LRUDict(LRUCache): | |
40 | """ |
|
40 | """ | |
41 | Wrapper to provide partial dict access |
|
41 | Wrapper to provide partial dict access | |
42 | """ |
|
42 | """ | |
43 |
|
43 | |||
44 | def __setitem__(self, key, value): |
|
44 | def __setitem__(self, key, value): | |
45 | return self.put(key, value) |
|
45 | return self.put(key, value) | |
46 |
|
46 | |||
47 | def __getitem__(self, key): |
|
47 | def __getitem__(self, key): | |
48 | return self.get(key) |
|
48 | return self.get(key) | |
49 |
|
49 | |||
50 | def __contains__(self, key): |
|
50 | def __contains__(self, key): | |
51 | return bool(self.get(key)) |
|
51 | return bool(self.get(key)) | |
52 |
|
52 | |||
53 | def __delitem__(self, key): |
|
53 | def __delitem__(self, key): | |
54 | del self.data[key] |
|
54 | del self.data[key] | |
55 |
|
55 | |||
56 | def keys(self): |
|
56 | def keys(self): | |
57 | return self.data.keys() |
|
57 | return list(self.data.keys()) | |
58 |
|
58 | |||
59 |
|
59 | |||
60 | class LRUDictDebug(LRUDict): |
|
60 | class LRUDictDebug(LRUDict): | |
61 | """ |
|
61 | """ | |
62 | Wrapper to provide some debug options |
|
62 | Wrapper to provide some debug options | |
63 | """ |
|
63 | """ | |
64 | def _report_keys(self): |
|
64 | def _report_keys(self): | |
65 | elems_cnt = '%s/%s' % (len(self.keys()), self.size) |
|
65 | ||
66 | # trick for pformat print it more nicely |
|
66 | # trick for pformat print it more nicely | |
67 | fmt = '\n' |
|
67 | fmt = '\n' | |
68 | for cnt, elem in enumerate(self.keys()): |
|
68 | for cnt, elem in enumerate(self.keys()): | |
69 |
fmt += ' |
|
69 | fmt += f'{cnt+1} - {safe_str(elem)}\n' | |
70 | log.debug('current LRU keys (%s):%s', elems_cnt, fmt) |
|
70 | ||
|
71 | log.debug('current LRU keys (%s/%s):%s', len(self.keys()), self.size, fmt) | |||
71 |
|
72 | |||
72 | def __getitem__(self, key): |
|
73 | def __getitem__(self, key): | |
73 | self._report_keys() |
|
74 | self._report_keys() | |
74 | return self.get(key) |
|
75 | return self.get(key) | |
75 |
|
76 | |||
76 |
|
77 | |||
77 | class MemoryLRUNamespaceManagerBase(MemoryNamespaceManager): |
|
78 | class MemoryLRUNamespaceManagerBase(MemoryNamespaceManager): | |
78 | default_max_items = 10000 |
|
79 | default_max_items = 10000 | |
79 |
|
80 | |||
80 | def _get_factory(self, max_items): |
|
81 | def _get_factory(self, max_items): | |
81 |
|
82 | |||
82 | def Factory(): |
|
83 | def Factory(): | |
83 | return LRUDict(int(max_items)) |
|
84 | return LRUDict(int(max_items)) | |
84 | return Factory |
|
85 | return Factory | |
85 |
|
86 | |||
86 | def __init__(self, namespace, **kwargs): |
|
87 | def __init__(self, namespace, **kwargs): | |
87 | AbstractDictionaryNSManager.__init__(self, namespace) |
|
88 | AbstractDictionaryNSManager.__init__(self, namespace) | |
88 | if 'max_items' in kwargs: |
|
89 | if 'max_items' in kwargs: | |
89 | max_items = kwargs['max_items'] |
|
90 | max_items = kwargs['max_items'] | |
90 | else: |
|
91 | else: | |
91 | max_items = self.default_max_items |
|
92 | max_items = self.default_max_items | |
92 |
|
93 | |||
93 | Factory = self._get_factory(max_items) |
|
94 | Factory = self._get_factory(max_items) | |
94 |
|
95 | |||
95 | self.dictionary = MemoryNamespaceManager.namespaces.get( |
|
96 | self.dictionary = MemoryNamespaceManager.namespaces.get( | |
96 | self.namespace, Factory) |
|
97 | self.namespace, Factory) | |
97 |
|
98 | |||
98 |
|
99 | |||
99 | class MemoryLRUNamespaceManagerDebug(MemoryLRUNamespaceManagerBase): |
|
100 | class MemoryLRUNamespaceManagerDebug(MemoryLRUNamespaceManagerBase): | |
100 | """ |
|
101 | """ | |
101 | A memory namespace manager that return with LRU dicts backend, |
|
102 | A memory namespace manager that return with LRU dicts backend, | |
102 | special debug for testing |
|
103 | special debug for testing | |
103 | """ |
|
104 | """ | |
104 | default_max_items = 10000 |
|
105 | default_max_items = 10000 | |
105 |
|
106 | |||
106 | def _get_factory(self, max_items): |
|
107 | def _get_factory(self, max_items): | |
107 |
|
108 | |||
108 | def Factory(): |
|
109 | def Factory(): | |
109 | return LRUDictDebug(int(max_items)) |
|
110 | return LRUDictDebug(int(max_items)) | |
110 | return Factory |
|
111 | return Factory |
General Comments 0
You need to be logged in to leave comments.
Login now