Skip to content

Commit

Permalink
addressed slow queries on initialization of the database; added sever…
Browse files Browse the repository at this point in the history
…al minor bulk import optimizations
  • Loading branch information
P-T-I committed Nov 21, 2023
1 parent 7448f6c commit 4758d51
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion CveXplore/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.3.17.dev12
0.3.17.dev13
4 changes: 3 additions & 1 deletion CveXplore/database/maintenance/DownloadHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def chunk_list(self, lst: list, number: int) -> list:
Yield successive n-sized chunks from lst.
"""
for i in range(0, len(lst), number):
yield lst[i: i + number]
yield lst[i : i + number]

def _db_bulk_writer(self, batch: list):
"""
Expand All @@ -162,6 +162,8 @@ def _db_bulk_writer(self, batch: list):
try:
if self.feed_type.lower() == "epss":
self.database["cves"].bulk_write(batch, ordered=False)
elif self.feed_type.lower() == "cves" or self.feed_type.lower() == "cpe":
self.database[self.feed_type.lower()].insert_many(batch, ordered=False)
else:
self.database[self.feed_type.lower()].bulk_write(batch, ordered=False)
except BulkWriteError as err:
Expand Down
1 change: 1 addition & 0 deletions CveXplore/database/maintenance/Sources_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,7 @@ def __init__(self):
MongoAddIndex(
index=[("padded_version", ASCENDING)], name="padded_version"
),
MongoAddIndex(index=[("lastModified", ASCENDING)], name="lastModified"),
],
"cpeother": [
MongoUniqueIndex(index=[("id", ASCENDING)], name="id", unique=True)
Expand Down
9 changes: 5 additions & 4 deletions CveXplore/database/maintenance/api_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ def process_item(self, item: dict):
doc=item,
).entry
else:
return DatabaseAction(
action=DatabaseAction.actions.InsertOne,
doc=item,
).entry
# return DatabaseAction(
# action=DatabaseAction.actions.InsertOne,
# doc=item,
# ).entry
return item

@abstractmethod
def process_the_item(self, *args):
Expand Down
12 changes: 9 additions & 3 deletions CveXplore/database/maintenance/main_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ def update(self, update_source: str | list = None):
self.datasource.set_handlers_for_collections()

self.logger.info(f"Database update complete!")
self.logger.info(f"Update Total duration: {timedelta(seconds=time.time() - start_time)}")
self.logger.info(
f"Update Total duration: {timedelta(seconds=time.time() - start_time)}"
)

def populate(self, populate_source: str | list = None):
"""
Expand Down Expand Up @@ -159,7 +161,9 @@ def populate(self, populate_source: str | list = None):
self.datasource.set_handlers_for_collections()

self.logger.info(f"Database population complete!")
self.logger.info(f"Populate total duration: {timedelta(seconds=time.time() - start_time)}")
self.logger.info(
f"Populate total duration: {timedelta(seconds=time.time() - start_time)}"
)

def initialize(self):
"""
Expand All @@ -183,4 +187,6 @@ def initialize(self):
self.update()

self.logger.info(f"Database initialization complete!")
self.logger.info(f"Initialization total duration: {timedelta(seconds=time.time() - start_time)}")
self.logger.info(
f"Initialization total duration: {timedelta(seconds=time.time() - start_time)}"
)

0 comments on commit 4758d51

Please sign in to comment.