def set_up_collection(name): """ Set up a collection of edges for a named graph This will ensure that the fields edge, source and target exists, that edges are unique and there's an index on source """ client = mongodb.client() db = client['edges'] db.create_index('edge', unique=True) db.create_index('source') return db
import mongodb client = mongodb.client() db = client.euchr_test edge_col = db['edges'] def set_up_collection(name): """ Set up a collection of edges for a named graph This will ensure that the fields edge, source and target exists, that edges are unique and there's an index on source """ client = mongodb.client() db = client['edges'] db.create_index('edge', unique=True) db.create_index('source') return db def get_common_neighbors(x,y,G): try u = set(scoped_neighborhood(x,x,G)) v = set(scoped_neighborhood(y,x,G)) return u.intersection(v) def get_precalculated(candidate_edges, field): pre_calculated = list(edge_col.find({'edge': {'$in': candidate_edges}}, {'source': True, 'target': True, field: True})) results = [(record['source'], record['target'], {'score': record[field]}) for record in pre_calculated] candidate_edges = list(set(candidate_edges) - set([(record['source'], record['target']) for record in pre_calculated])) return results, validation_set def save_precalculated(records, field): records = [{'edge': (x,y), field: data['score']}] db.insert_many(records)