/
arangodb.py
403 lines (310 loc) · 13.5 KB
/
arangodb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# Standard Library
import re
from dataclasses import dataclass
from typing import List, Mapping, Optional
# Third Party
# Third Party Imports
import arango
import arango.client
import arango.database
import arango.exceptions
import boltons.iterutils
from loguru import logger
# Local
# Local Imports
import bel.core.settings as settings
from bel.core.utils import _create_hash
resources_db_name = settings.RESOURCES_DB # BEL Resources (Namespaces, etc)
bel_db_name = settings.BEL_DB # Misc BEL
# BEL Resources
equiv_nodes_name = "equivalence_nodes" # equivalence node collection name
equiv_edges_name = "equivalence_edges" # equivalence edge collection name
ortholog_nodes_name = "ortholog_nodes" # ortholog node collection name
ortholog_edges_name = "ortholog_edges" # ortholog edge collection name
resources_metadata_name = "resources_metadata" # BEL Resources metadata
terms_coll_name = "terms" # BEL Namespaces/Terms collection name
# BEL database collections
bel_config_name = "bel_config" # BEL settings and configuration
bel_namespaces_name = "bel_namespaces" # BEL namespaces
bel_validations_name = "validations" # BEL Assertion/Annotation validations
def get_user_credentials(username, password):
"""Get username/password
Use provided username and password OR in config OR blank in that order
"""
username = boltons.iterutils.first([username, settings.ARANGO_USER], default="")
password = boltons.iterutils.first(
[password, settings.ARANGO_PASSWORD],
default="",
)
return username, password
def get_client(url=None, port=None, username=None, password=None):
"""Get arango client db handle"""
url = boltons.iterutils.first([url, settings.ARANGO_URL, "http://localhost:8529"])
(username, password) = get_user_credentials(username, password)
try:
client = arango.ArangoClient(hosts=url)
client.db(verify=True)
return client
except Exception:
logger.error(f"Cannot access arangodb at {url}")
return None
# Index mgmt #################################################################################
@dataclass
class IndexDefinition:
"""Class for defining collection indexes"""
fields: List[str] # ordered list of fields to be indexed
id: Optional[str] = None # ID is provided by arangodb
type: str = "persistent" # primary or edge indexes are ignored
unique: bool = False
sparse: Optional[bool] = None
deduplicate: Optional[bool] = None
name: str = None
in_background: bool = True
def add_index(collection, index: IndexDefinition):
"""Add index"""
# add_persistent_index(fields, unique=None, sparse=None, deduplicate=None, name=None, in_background=None)
if index.type == "persistent":
collection.add_persistent_index(
index.fields,
unique=index.unique,
sparse=index.sparse,
name=index.name,
in_background=index.in_background,
)
else:
logger.error(f"Cannot add index type: {index.type}")
def remove_old_indexes(
collection,
current_indexes: Mapping[str, IndexDefinition],
desired_indexes: Mapping[str, IndexDefinition],
):
"""Remove out of date indexes"""
for key in current_indexes:
if key not in desired_indexes:
print(f"Removing index {key} id: {current_indexes[key].id} from {collection}")
collection.delete_index(current_indexes[key].id)
def update_index_state(collection, desired_indexes: List[IndexDefinition]):
"""Update index state
desired_indexes keys = f"{index_type}_{'_'.join(sorted(fields))}", e.g. persistent_firstname_lastname
Remove indices that are not specified and add indices that are missing
"""
new = {}
for index in desired_indexes:
index_key = f"{index.type}_{'_'.join(sorted(index.fields))}"
new[index_key] = index
desired_indexes = new
current_indexes_list = collection.indexes()
current_indexes: dict = {}
for idx in current_indexes_list:
if idx["type"] in ["primary", "edge"]:
continue # skip primary indices
idx.pop("selectivity", None)
index = IndexDefinition(**idx)
index_key = f"{index.type}_{'_'.join(sorted(index.fields))}"
current_indexes[index_key] = index
remove_old_indexes(collection, current_indexes, desired_indexes)
# Add missing desired indexes
for key in desired_indexes:
if key not in current_indexes:
add_index(collection, desired_indexes[key])
# Index mgmt ##################################################################################
def get_resources_handles(client, username=None, password=None):
"""Get BEL Resources arangodb handle"""
(username, password) = get_user_credentials(username, password)
sys_db = client.db("_system", username=username, password=password)
# Create a new database named "bel_resources"
if sys_db.has_database(resources_db_name):
if username and password:
resources_db = client.db(resources_db_name, username=username, password=password)
else:
resources_db = client.db(resources_db_name)
else:
if username and password:
resources_db = sys_db.create_database(
name=resources_db_name,
users=[{"username": username, "password": password, "active": True}],
)
else:
resources_db = sys_db.create_database(name=resources_db_name)
if resources_db.has_collection(resources_metadata_name):
resources_metadata_coll = resources_db.collection(resources_metadata_name)
else:
resources_metadata_coll = resources_db.create_collection(resources_metadata_name)
if resources_db.has_collection(equiv_nodes_name):
equiv_nodes_coll = resources_db.collection(equiv_nodes_name)
else:
equiv_nodes_coll = resources_db.create_collection(equiv_nodes_name)
if resources_db.has_collection(equiv_edges_name):
equiv_edges_coll = resources_db.collection(equiv_edges_name)
else:
equiv_edges_coll = resources_db.create_collection(equiv_edges_name, edge=True)
if resources_db.has_collection(ortholog_nodes_name):
ortholog_nodes_coll = resources_db.collection(ortholog_nodes_name)
else:
ortholog_nodes_coll = resources_db.create_collection(ortholog_nodes_name)
if resources_db.has_collection(ortholog_edges_name):
ortholog_edges_coll = resources_db.collection(ortholog_edges_name)
else:
ortholog_edges_coll = resources_db.create_collection(ortholog_edges_name, edge=True)
if resources_db.has_collection(terms_coll_name):
terms_coll = resources_db.collection(terms_coll_name)
else:
terms_coll = resources_db.create_collection(terms_coll_name)
# Update indexes
update_index_state(
terms_coll,
[
IndexDefinition(type="persistent", fields=["key"], unique=True),
IndexDefinition(type="persistent", fields=["namespace"], unique=False),
IndexDefinition(type="persistent", fields=["alt_keys[*]"], unique=False, sparse=True),
IndexDefinition(
type="persistent", fields=["equivalence_keys[*]"], unique=False, sparse=True
),
IndexDefinition(
type="persistent", fields=["obsolete_keys[*]"], unique=False, sparse=True
),
IndexDefinition(type="persistent", fields=["synonyms[*]"], unique=False, sparse=True),
],
)
update_index_state(
equiv_nodes_coll,
[
IndexDefinition(type="persistent", fields=["key"], unique=True),
IndexDefinition(type="persistent", fields=["source"], unique=False),
],
)
update_index_state(
equiv_edges_coll, [IndexDefinition(type="persistent", fields=["source"], unique=False)]
)
update_index_state(
ortholog_nodes_coll, [IndexDefinition(type="persistent", fields=["key"], unique=True)]
)
return {
"resources_db": resources_db,
"resources_metadata_coll": resources_metadata_coll,
"equiv_nodes_coll": equiv_nodes_coll,
"equiv_edges_coll": equiv_edges_coll,
"ortholog_nodes_coll": ortholog_nodes_coll,
"ortholog_edges_coll": ortholog_edges_coll,
"terms_coll": terms_coll,
}
def get_bel_handles(client, username=None, password=None):
"""Get BEL API arango db handle"""
(username, password) = get_user_credentials(username, password)
sys_db = client.db("_system", username=username, password=password)
# Create a new database named "bel"
if sys_db.has_database(bel_db_name):
if username and password:
bel_db = client.db(bel_db_name, username=username, password=password)
else:
bel_db = client.db(bel_db_name)
else:
if username and password:
sys_db.create_database(
name=bel_db_name,
users=[{"username": username, "password": password, "active": True}],
)
else:
sys_db.create_database(name=bel_db_name)
bel_db = client.db(bel_db_name)
if bel_db.has_collection(bel_config_name):
bel_config_coll = bel_db.collection(bel_config_name)
else:
bel_config_coll = bel_db.create_collection(bel_config_name, index_bucket_count=64)
if bel_db.has_collection(bel_validations_name):
bel_validations_coll = bel_db.collection(bel_validations_name)
else:
bel_validations_coll = bel_db.create_collection(bel_validations_name, index_bucket_count=64)
return {
"bel_db": bel_db,
"bel_config_coll": bel_config_coll,
"bel_validations_coll": bel_validations_coll,
}
# #############################################################################
# Initialize arango_client !!!!!!!!!!!!!!!!!!!
# and provide db and collection handles
# #############################################################################
client = get_client()
# Resources db
resources_handles = get_resources_handles(client)
resources_db = resources_handles["resources_db"]
resources_metadata_coll = resources_handles["resources_metadata_coll"]
equiv_nodes_coll = resources_handles["equiv_nodes_coll"]
equiv_edges_coll = resources_handles["equiv_edges_coll"]
ortholog_nodes_coll = resources_handles["ortholog_nodes_coll"]
ortholog_edges_coll = resources_handles["ortholog_edges_coll"]
terms_coll = resources_handles["terms_coll"]
# BEL db
bel_handles = get_bel_handles(client)
bel_db = bel_handles["bel_db"]
bel_config_coll = bel_handles["bel_config_coll"]
bel_validations_coll = bel_handles["bel_validations_coll"]
def delete_database(client, db_name, username=None, password=None):
"""Delete Arangodb database"""
(username, password) = get_user_credentials(username, password)
sys_db = client.db("_system", username=username, password=password)
try:
return sys_db.delete_database(db_name)
except Exception:
logger.warning(f"No arango database {db_name} to delete, does not exist")
def batch_load_docs(db, doc_iterator, on_duplicate: str = "replace"):
"""Batch load documents
Args:
db: ArangoDB client database handle
doc_iterator: function that yields (collection_name, doc_key, doc)
on_duplicate: defaults to replace, but can be error, update, replace or ignore
https://python-driver-for-arangodb.readthedocs.io/en/master/specs.html?highlight=import_bulk#arango.collection.StandardCollection.import_bulk
"""
batch_size = 500
counter = 0
collections = {}
docs = {}
if on_duplicate not in ["error", "update", "replace", "ignore"]:
logger.error(f"Bad parameter for on_duplicate: {on_duplicate}")
return
for (collection_name, doc) in doc_iterator:
if collection_name not in collections:
collections[collection_name] = db.collection(collection_name)
docs[collection_name] = []
counter += 1
docs[collection_name].append(doc)
if counter % batch_size == 0:
# logger.debug(f"Bulk import arangodb: {counter}")
for collection_name in docs:
collections[collection_name].import_bulk(
docs[collection_name], on_duplicate=on_duplicate, halt_on_error=False
)
docs[collection_name] = []
# logger.debug(f"Bulk import arangodb: {counter}")
for collection_name in docs:
try:
collections[collection_name].import_bulk(
docs[collection_name], details=True, on_duplicate=on_duplicate, halt_on_error=False
)
docs[collection_name] = []
except arango.exceptions.DocumentInsertError as e:
logger.error(f"Problem with arango bulk import: {str(e)}")
def arango_id_to_key(_id):
"""Remove illegal chars from potential arangodb _key (id) or return hashed key if > 60 chars
Arango _key cannot be longer than 254 chars but we convert to hash if over 60 chars
Args:
_id (str): id to be used as arangodb _key
Returns:
(str): _key value with illegal chars removed
"""
if len(_id) > 60:
key = _create_hash(_id)
elif len(_id) < 1:
logger.error(f"Arango _key cannot be an empty string: Len={len(_id)} Key: {_id}")
else:
key = re.sub(r"[^a-zA-Z0-9\_\-\:\.\@\(\)\+\,\=\;\$\!\*\%]+", r"_", _id)
return key
def aql_query(db, query, batch_size=20, ttl=300):
"""Run AQL query
Default batch_size = 20
Default time to live for the query is 300
Returns:
result_cursor
"""
result = db.aql.execute(query, batch_size=batch_size, ttl=ttl)
return result