Created
October 10, 2018 19:27
-
-
Save 1st1/eee88be8735f27b0c523584f9b4cf1c3 to your computer and use it in GitHub Desktop.
LRUIndex & MappedDeque datastructures
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# This source file is part of the EdgeDB open source project. | |
# | |
# Copyright 2016-present MagicStack Inc. and the EdgeDB authors. | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
# | |
import collections | |
class MappedDeque: | |
"""A deque-like object with an O(1) discard operation.""" | |
def __init__(self, source=None): | |
if source is None: | |
self._list = collections.OrderedDict() | |
else: | |
self._list = collections.OrderedDict.fromkeys(source) | |
def __contains__(self, item): | |
return item in self._list | |
def __getitem__(self, item): | |
return self._list[item] | |
def discard(self, item): | |
try: | |
self._list.pop(item) | |
except KeyError: | |
raise LookupError(f'{item!r} is not in {self!r}') from None | |
def append(self, item, val=None): | |
if item in self._list: | |
raise ValueError(f'{item!r} is already in the list {self!r}') | |
self._list[item] = val | |
def pop(self): | |
item, _ = self._list.popitem(last=True) | |
return item | |
def popleft(self): | |
item, _ = self._list.popitem(last=False) | |
return item | |
def popleftitem(self): | |
return self._list.popitem(last=False) | |
def __len__(self): | |
return len(self._list) | |
def __bool__(self): | |
return bool(self._list) | |
def __iter__(self): | |
return iter(self._list) | |
def __repr__(self): | |
return f'<{type(self).__name__} {list(self)!r} {id(self):#x}>' | |
class LRUIndex: | |
"""A multidict-like mapping with internal LRU lists. | |
Key properties: | |
* One key can be mapped to a list of objects. | |
* Objects must be unique for the entire mapping. It's an error | |
if two different keys point to one object, or of one object is | |
referenced by the same key more than once. | |
* Every key maps to a LIFO list of objects internally. LIFO | |
is essential to make the global LRU list of connections work: | |
if a DB has too many open connections some of them will become | |
unused for long enough period of time to be closed. | |
* There's a global LIFO list of objects, accessible via the | |
"lru()" method. The "count()" method returns the total number | |
of objects in the index. | |
""" | |
def __init__(self): | |
self._index = {} | |
self._lru_list = MappedDeque() | |
def pop(self, key): | |
try: | |
items = self._index[key] | |
except KeyError: | |
return None | |
o = items.pop() | |
self._lru_list.discard(o) | |
if not items: | |
del self._index[key] | |
return o | |
def append(self, key, o): | |
if o in self._lru_list: | |
raise ValueError(f'{key!r}:{o!r} is already in the index {self!r}') | |
try: | |
items = self._index[key] | |
except KeyError: | |
items = self._index[key] = MappedDeque() | |
items.append(o) | |
self._lru_list.append(o, key) | |
def popleft(self): | |
o, key = self._lru_list.popleftitem() | |
items = self._index[key] | |
items.discard(o) | |
if not items: | |
del self._index[key] | |
return o | |
def discard(self, o): | |
try: | |
key = self._lru_list[o] | |
except KeyError: | |
return False | |
items = self._index[key] | |
items.discard(o) | |
self._lru_list.discard(o) | |
if not items: | |
del self._index[key] | |
return True | |
def count_keys(self): | |
return len(self._index) | |
def count(self): | |
return len(self._lru_list) | |
def lru(self): | |
return iter(self._lru_list) |
Author
1st1
commented
Oct 10, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment