15
15
# ########################################################################
16
16
import logging
17
17
import os
18
+ import time
18
19
19
20
import pymongo
20
21
21
22
logger = logging .getLogger (__name__ )
22
23
23
24
24
25
class MongoRepository :
25
- def __init__ (self , mongo_config ):
26
+ def __init__ (self ):
26
27
self ._client = pymongo .MongoClient (
27
28
os .environ ["MONGO_SERVICE_SERVICE_HOST" ],
28
29
int (os .environ ["MONGO_SERVICE_SERVICE_PORT" ]),
@@ -36,25 +37,43 @@ def __init__(self, mongo_config):
36
37
37
38
38
39
class MibsRepository (MongoRepository ):
40
+ is_text_index_created = False
41
+
39
42
def __init__ (self , mongo_config ):
40
- super ().__init__ (mongo_config )
43
+ super ().__init__ ()
41
44
self ._mibs = self ._client [mongo_config ["database" ]][mongo_config ["collection" ]]
42
45
43
46
def upload_files (self , mib_files_dir ):
47
+ tic = time .perf_counter ()
44
48
for filename in os .listdir (mib_files_dir ):
45
49
file_path = mib_files_dir + "/" + filename
46
50
with open (file_path , "r" ) as mib_file :
47
51
try :
48
52
self ._mibs .insert_one (
49
53
dict (content = mib_file .read (), filename = filename , _id = filename )
50
54
)
51
- except Exception as e :
52
- logger .error (
53
- f "Error happened during insert mib files { filename } into mongo: { e } "
55
+ except Exception :
56
+ logger .exception (
57
+ "Error happened during insert mib files %s into mongo" , filename
54
58
)
59
+ toc = time .perf_counter ()
60
+ logger .info (f"Uploading files took - { toc - tic :0.4f} seconds" )
61
+
62
+ def create_text_index (self ):
63
+ tic = time .perf_counter ()
64
+ try :
65
+ self ._mibs .create_index ([("content" , pymongo .TEXT )], name = "oid_index" )
66
+ except Exception :
67
+ logger .exception (
68
+ "Failed to create the index, searches will be performed with regex"
69
+ )
70
+ return
71
+ toc = time .perf_counter ()
72
+ MibsRepository .is_text_index_created = True
73
+ logger .info (f"Creating index took - { toc - tic :0.4f} seconds" )
55
74
56
75
def search_oid (self , oid ):
57
- data = self ._mibs . find ({ "content" : { "$regex" : oid }} )
76
+ data = self .perform_correct_search ( oid )
58
77
if data :
59
78
mib_list = []
60
79
for item in data :
@@ -63,6 +82,19 @@ def search_oid(self, oid):
63
82
else :
64
83
return None
65
84
85
+ def perform_correct_search (self , oid ):
86
+ tic = time .perf_counter ()
87
+ if MibsRepository .is_text_index_created :
88
+ data = self ._mibs .find ({"$text" : {"$search" : f'"{ oid } "' }})
89
+ else :
90
+ data = self ._mibs .find ({"content" : {"$regex" : oid }})
91
+ toc = time .perf_counter ()
92
+ logger .debug (
93
+ f"We searched with { 'Index' if MibsRepository .is_text_index_created else 'Regex' } "
94
+ f"and the search took - { toc - tic :0.7f} seconds"
95
+ )
96
+ return data
97
+
66
98
def delete_mib (self , filename ):
67
99
self ._mibs .delete_many ({"filename" : {"$regex" : filename }})
68
100
@@ -72,7 +104,7 @@ def clear(self):
72
104
73
105
class OidsRepository (MongoRepository ):
74
106
def __init__ (self , mongo_config ):
75
- super ().__init__ (mongo_config )
107
+ super ().__init__ ()
76
108
self ._oids = self ._client [mongo_config ["database" ]][mongo_config ["collection" ]]
77
109
78
110
def contains_oid (self , oid ):
0 commit comments