Clean up ChunkPool
This commit is contained in:
parent
72cb273849
commit
c364faf8ff
1 changed files with 37 additions and 13 deletions
50
chunks.py
50
chunks.py
|
|
@ -85,9 +85,12 @@ class Chunk():
|
||||||
self.touch()
|
self.touch()
|
||||||
|
|
||||||
def commit_changes(self):
|
def commit_changes(self):
|
||||||
self._content.apply(self._modifications)
|
self.commit_diff(self._modifications)
|
||||||
self._content.clear_deletions()
|
|
||||||
self._modifications = ChunkDiff()
|
self._modifications = ChunkDiff()
|
||||||
|
|
||||||
|
def commit_diff(self, diff):
|
||||||
|
self._content.apply(diff)
|
||||||
|
self._content.clear_deletions()
|
||||||
self.touch()
|
self.touch()
|
||||||
|
|
||||||
def drop_changes(self):
|
def drop_changes(self):
|
||||||
|
|
@ -131,29 +134,50 @@ class ChunkPool():
|
||||||
def __exit__(self, type, value, tb):
|
def __exit__(self, type, value, tb):
|
||||||
self._lock.release()
|
self._lock.release()
|
||||||
|
|
||||||
|
def get(self, pos):
|
||||||
|
return self._chunks.get(pos)
|
||||||
|
|
||||||
def create(self, pos):
|
def create(self, pos):
|
||||||
self._chunks[pos] = Chunk()
|
chunk = Chunk()
|
||||||
return self._chunks[pos]
|
self._chunks[pos] = chunk
|
||||||
|
return chunk
|
||||||
|
|
||||||
|
def commit_changes(self):
|
||||||
|
changes = []
|
||||||
|
|
||||||
|
for pos, chunk in self._chunks.items():
|
||||||
|
changes.append((pos, chunk.get_changes()))
|
||||||
|
chunk.commit_changes()
|
||||||
|
|
||||||
|
return changes
|
||||||
|
|
||||||
|
def save_changes(self):
|
||||||
|
self.commit_changes()
|
||||||
|
|
||||||
def load(self, pos):
|
def load(self, pos):
|
||||||
if not pos in self._chunks:
|
if not pos in self._chunks:
|
||||||
self.create(pos)
|
self.create(pos)
|
||||||
|
|
||||||
def unload(self, pos):
|
|
||||||
if pos in self._chunks:
|
|
||||||
del self._chunks[pos]
|
|
||||||
|
|
||||||
def load_list(self, coords):
|
def load_list(self, coords):
|
||||||
for pos in coords:
|
for pos in coords:
|
||||||
self.load(pos)
|
self.load(pos)
|
||||||
|
|
||||||
|
def unload(self, pos):
|
||||||
|
if pos in self._chunks:
|
||||||
|
del self._chunks[pos]
|
||||||
|
|
||||||
def unload_list(self, coords):
|
def unload_list(self, coords):
|
||||||
for pos in coords:
|
for pos in coords:
|
||||||
self.unload(pos)
|
self.unload(pos)
|
||||||
|
|
||||||
def clean_up(self, except_for=[]):
|
def clean_up(self, except_for=[], condition=lambda chunk: True):
|
||||||
coords = [pos for pos in self._chunks if not pos in except_for]
|
# old list comprehension which became too long:
|
||||||
self.unload_list(coords)
|
#coords = [pos for pos, chunk in self._chunks.items() if not pos in except_for and condition(chunk)]
|
||||||
|
|
||||||
def get(self, pos):
|
coords = []
|
||||||
return self._chunks.get(pos)
|
|
||||||
|
for pos, chunk in self._chunks.items():
|
||||||
|
if not pos in except_for and condition(chunk):
|
||||||
|
coords.append(pos)
|
||||||
|
|
||||||
|
self.unload_list(coords)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue