Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: clean up term map #67

Merged
merged 1 commit into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ Sharing
+------------------------------------+---------------+-----------------------------------------------------------------------------------+-----------+
| sharing.directories | array[object] | List of shared directories: | <not set> |
+------------------------------------+---------------+-----------------------------------------------------------------------------------+-----------+
| sharing.index.store_interval | integer | Shared items index automatically gets stored, this parameter defines the interval | 120 |
+------------------------------------+---------------+-----------------------------------------------------------------------------------+-----------+

The `sharing.directories` list contains objects which have the following parameters:

Expand Down
5 changes: 0 additions & 5 deletions src/aioslsk/resources/default_settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ network:
upnp:
enabled: true
lease_duration: 0
timeout:
peer: 10
distributed: 120
reconnect:
auto: true
timeout: 10
Expand All @@ -35,8 +32,6 @@ sharing:
upload_speed_kbps: 0
download_speed_kbps: 0
download: null
index:
store_interval: 120
directories: []

chats:
Expand Down
19 changes: 17 additions & 2 deletions src/aioslsk/shares/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,13 @@ async def create_directory(self, absolute_path: str) -> str:
logger.info(f"creating directory : {absolute_path}")
await asyncos.makedirs(absolute_path, exist_ok=True)

def build_term_map(self, shared_directory: SharedDirectory):
def rebuild_term_map(self):
logger.info("rebuilding term map")
self._term_map = {}
for shared_directory in self.shared_directories:
self._build_term_map(shared_directory)

def _build_term_map(self, shared_directory: SharedDirectory):
"""Builds a list of valid terms for the given shared directory"""
for item in shared_directory.items:
self._add_item_to_term_map(item)
Expand Down Expand Up @@ -312,6 +318,8 @@ def remove_shared_directory(self, directory: SharedDirectory):
parent = parents[-1]
parent.items |= directory.items

self._cleanup_term_map()

async def scan_directory_files(self, shared_directory: SharedDirectory):
"""Scans the files for the given `shared_directory`

Expand Down Expand Up @@ -347,7 +355,8 @@ async def scan_directory_files(self, shared_directory: SharedDirectory):
# set
shared_directory.items -= (shared_directory.items ^ shared_items)

self.build_term_map(shared_directory)
self._build_term_map(shared_directory)
self._cleanup_term_map()

async def scan_directory_file_attributes(self, shared_directory: SharedDirectory):
"""Scans the file attributes for files in the given `shared_directory`.
Expand Down Expand Up @@ -637,6 +646,12 @@ def _add_item_to_term_map(self, item: SharedItem):
self._term_map[term] = WeakSet()
self._term_map[term].add(item)

def _cleanup_term_map(self):
self._term_map = {
term: values for term, values in self._term_map.items()
if len(values) > 0
}

def _get_parent_directories(self, shared_directory: SharedDirectory) -> List[SharedDirectory]:
"""Returns a list of parent shared directories. The parent directories
will be sorted by length of the absolute path (longest last)
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/shares/test_shares_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def manager(tmp_path):
def manager_query(tmp_path):
manager = SharesManager(Settings(DEFAULT_SETTINGS), InternalEventBus())
manager._shared_directories = [SHARED_DIRECTORY]
manager.build_term_map(SHARED_DIRECTORY)
manager._build_term_map(SHARED_DIRECTORY)

return manager

Expand Down