def _sort(self, objects, depsfn=None): g = {} for obj in objects: this_item = g[obj.name] = {'item': obj, 'merge': [], 'deps': []} if depsfn is not None: deps = depsfn(obj) for dep in deps: this_item['deps'].append(dep.name) g[dep.name] = {'item': dep, 'merge': [], 'deps': []} if obj.bases: g[obj.name]['deps'].extend(b.name for b in obj.bases) for base in obj.bases: if base.name.module != obj.name.module: g[base.name] = {'item': base, 'merge': [], 'deps': []} if not g: return ordered.OrderedSet() item = next(iter(g.values()))['item'] modname = item.name.module objs = topological.sort(g) return ordered.OrderedSet( filter(lambda obj: obj.name.module == modname, objs))
def sort_objects(schema, objects): from . import inheriting g = {} for obj in sorted(objects, key=lambda o: o.get_name(schema)): g[obj.get_name(schema)] = {'item': obj, 'merge': [], 'deps': []} if isinstance(obj, inheriting.InheritingObject): obj_bases = obj.get_bases(schema) else: obj_bases = None if obj_bases: g[obj.get_name(schema)]['deps'].extend( b.get_name(schema) for b in obj_bases.objects(schema)) for base in obj_bases.objects(schema): base_name = base.get_name(schema) if base_name.module != obj.get_name(schema).module: g[base_name] = {'item': base, 'merge': [], 'deps': []} if not g: return ordered.OrderedSet() item = next(iter(g.values()))['item'] modname = item.get_name(schema).module objs = topological.sort(g) return ordered.OrderedSet( filter( lambda obj: getattr(obj.get_name(schema), 'module', None) == modname, objs))
async def order_link_properties(self, schema): g = {} for prop in schema.get_objects(type=s_props.Property): g[prop.get_name(schema)] = {"item": prop, "merge": [], "deps": []} prop_bases = prop.get_bases(schema).objects(schema) if prop_bases: g[prop.get_name(schema)]['merge'].extend( b.get_name(schema) for b in prop_bases) props = topological.sort(g) for prop in props: schema = prop.finalize(schema) return schema
async def order_links(self, schema): g = {} for link in schema.get_objects(type=s_links.Link): g[link.get_name(schema)] = {"item": link, "merge": [], "deps": []} link_bases = link.get_bases(schema).objects(schema) if link_bases: g[link.get_name(schema)]['merge'].extend( b.get_name(schema) for b in link_bases) links = topological.sort(g) for link in links: schema = link.finalize(schema) return schema
def _sort_set(cls, items): if items: probe = next(iter(items)) has_bases = hasattr(probe, 'bases') if has_bases: items_idx = {p.name: p for p in items} g = {} for x in items: deps = {b for b in x._get_deps() if b in items_idx} g[x.name] = {'item': x, 'deps': deps} items = topological.sort(g) return items
async def order_objtypes(self, schema): g = {} for objtype in schema.get_objects(type=s_objtypes.BaseObjectType): g[objtype.get_name(schema)] = { "item": objtype, "merge": [], "deps": [] } objtype_bases = objtype.get_bases(schema).objects(schema) if objtype_bases: g[objtype.get_name(schema)]["merge"].extend( b.get_name(schema) for b in objtype_bases) objtypes = topological.sort(g) for objtype in objtypes: schema = objtype.finalize(schema) return schema
def _delta_sets(cls, old, new, context=None, *, old_schema=None, new_schema=None): from edb.lang.schema import named as s_named from edb.lang.schema import database as s_db adds_mods = s_db.AlterDatabase() dels = s_db.AlterDatabase() if old is None: for n in new: adds_mods.add(n.delta(None, context=context)) adds_mods.sort_subcommands_by_type() return adds_mods, dels elif new is None: for o in old: dels.add(o.delta(None, reverse=True, context=context)) return adds_mods, dels old = list(old) new = list(new) oldkeys = {o.persistent_hash() for o in old} newkeys = {o.persistent_hash() for o in new} unchanged = oldkeys & newkeys old = OrderedSet(o for o in old if o.persistent_hash() not in unchanged) new = OrderedSet(o for o in new if o.persistent_hash() not in unchanged) comparison = ((x.compare(y, context), x, y) for x, y in itertools.product(new, old)) used_x = set() used_y = set() altered = OrderedSet() comparison = sorted(comparison, key=lambda item: item[0], reverse=True) """LOG [edb.delta.comp] Index comparison from edb.lang.common import markup markup.dump(comparison) """ for s, x, y in comparison: if x not in used_x and y not in used_y: if s != 1.0: if s > 0.6: altered.add(x.delta(y, context=context)) used_x.add(x) used_y.add(y) else: used_x.add(x) used_y.add(y) deleted = old - used_y created = new - used_x if created: created = cls._sort_set(created) for x in created: adds_mods.add(x.delta(None, context=context)) if old_schema is not None and new_schema is not None: if old: probe = next(iter(old)) elif new: probe = next(iter(new)) else: probe = None if probe is not None: has_bases = hasattr(probe, 'bases') else: has_bases = False if has_bases: g = {} altered_idx = {p.classname: p for p in altered} for p in altered: for op in p.get_subcommands( type=s_named.RenameNamedObject): altered_idx[op.new_name] = p for p in altered: old_class = old_schema.get(p.classname) for op in p.get_subcommands( type=s_named.RenameNamedObject): new_name = op.new_name break else: new_name = p.classname new_class = new_schema.get(new_name) bases = {getattr(b, 'classname', getattr(b, 'name', None)) for b in old_class.bases} | \ {getattr(b, 'classname', getattr(b, 'name', None)) for b in new_class.bases} deps = {b for b in bases if b in altered_idx} g[p.classname] = {'item': p, 'deps': deps} if new_name != p.classname: g[new_name] = {'item': p, 'deps': deps} altered = topological.sort(g) for p in altered: adds_mods.add(p) if deleted: deleted = cls._sort_set(deleted) for y in reversed(list(deleted)): dels.add(y.delta(None, reverse=True, context=context)) adds_mods.sort_subcommands_by_type() return adds_mods, dels