def expand_superstructure_activities(self) -> None:
     """Store the missing activities found by `find_missing_activities`."""
     new_activities = structure_activities(self.missing_activities, self.name)
     with sqlite3_lci_db.transaction() as txn:
         for i, act in enumerate(new_activities):
             act.save()
             if i % 1000 == 0:
                 txn.commit()
 def expand_superstructure_exchanges(self) -> None:
     """Given that we have an initialized Builder, prepare and store
     the new exchanges.
     """
     deltas_set = set(self.selected_deltas)
     # Prepare new exchanges
     new_excs = structure_exchanges(self.missing_exchanges, self.name, deltas_set)
     # Save all of the new exchanges to the superstructure database.
     with sqlite3_lci_db.transaction() as txn:
         for i, exc in enumerate(new_excs):
             exc.save()
             if i % 1000 == 0:
                 txn.commit()
예제 #3
0
def relink_exchanges_existing_db(db: bw.Database, old: str,
                                 other: bw.Database) -> None:
    """Relink exchanges after the database has been created/written.

    This means possibly doing a lot of sqlite update calls.
    """
    if old == other.name:
        print("No point relinking to same database.")
        return
    assert db.backend == "sqlite", "Relinking only allowed for SQLITE backends"
    assert other.backend == "sqlite", "Relinking only allowed for SQLITE backends"

    duplicates, candidates = {}, {}
    altered = 0

    for ds in other:
        key = activity_hash(ds, DEFAULT_FIELDS)
        if key in candidates:
            duplicates.setdefault(key, []).append(ds)
        else:
            candidates[key] = ds.key

    with sqlite3_lci_db.transaction() as transaction:
        try:
            # Only do relinking on external biosphere/technosphere exchanges.
            for i, exc in enumerate(
                    exc for act in db for exc in act.exchanges()
                    if exc.get("type") in {"biosphere", "technosphere"}
                    and exc.input[0] == old):
                # Use the input activity to generate the hash.
                key = activity_hash(exc.input, DEFAULT_FIELDS)
                if key in duplicates:
                    raise StrategyError(
                        format_nonunique_key_error(exc.input, DEFAULT_FIELDS,
                                                   duplicates[key]))
                elif key in candidates:
                    exc["input"] = candidates[key]
                    altered += 1
                exc.save()
                if i % 10000 == 0:
                    # Commit changes every 10k exchanges.
                    transaction.commit()
        except (StrategyError, ValidityError) as e:
            print(e)
            transaction.rollback()
    # Process the database after the transaction is complete.
    #  this updates the 'depends' in metadata
    db.process()
    print(
        "Relinked database '{}', {} exchange inputs changed from '{}' to '{}'."
        .format(db.name, altered, old, other.name))
예제 #4
0
 def expand_superstructure(self) -> None:
     """Given that we have an initialized Builder, prepare and store
     the new exchanges.
     """
     deltas_set = set(self.selected_deltas)
     nulled = nullify_exchanges([x.data for x in self.missing_exchanges])
     # Prepare new exchanges by swapping the
     new_excs = [
         swap_exchange_activities(row, self.name, deltas_set)
         for row in nulled
     ]
     # Save all of the new exchanges to the superstructure database.
     with sqlite3_lci_db.transaction() as txn:
         for i, exc in enumerate(new_excs):
             exc.save()
             if i % 1000 == 0:
                 txn.commit()