class TrimService {

    @Transactional
    private int doTrimDerivedTablesOnHeight(int height, boolean isSharding) {
        log.debug("TRIM: doTrimDerivedTablesOnHeight on height={}, isSharding={}", height, isSharding);
        long start = System.currentTimeMillis();

        TransactionalDataSource dataSource = dbManager.getDataSource();
        boolean inTransaction = dataSource.isInTransaction();
        log.debug("doTrimDerivedTablesOnHeight height = '{}', inTransaction = '{}'", height, inTransaction);
        if (!inTransaction) {
            dataSource.begin();
        }
        long onlyTrimTime = 0;
        int epochTime = timeService.getEpochTime();
        int pruningTime = epochTime - epochTime % DEFAULT_PRUNABLE_UPDATE_PERIOD;

        for (DerivedTableInterface table : dbTablesRegistry.getDerivedTables()) {
            log.trace("Try to acquire lock...");
            globalSync.readLock();
            log.trace("Got it.");
            try {
                long startTime = System.currentTimeMillis();
                table.prune(pruningTime);
                table.trim(height, isSharding);
                dataSource.commit(false);
                long duration = System.currentTimeMillis() - startTime;
                // do not log trim duration here, instead go to the logback config and enable trace logs for BasicDbTable class
                //                    log.trace("Trim of {} took {} ms", table.getName(), duration);
                onlyTrimTime += duration;
            } finally {
                globalSync.readUnlock();
            }
        }

        log.info("Trim time onlyTrim/full: {} / {} ms, pruning='{}' on height='{}'",
            onlyTrimTime, System.currentTimeMillis() - start, pruningTime, height);
        return pruningTime;
    }

    @Inject public  TrimService(DatabaseManager databaseManager,
                       DerivedTablesRegistry derivedDbTablesRegistry,
                       GlobalSync globalSync,
                       TimeService timeService,
                       Event<TrimData> trimEvent,
                       Event<TrimConfig> trimConfigEvent,
                       TrimDao trimDao,
                       @Property(value = "apl.maxRollback", defaultValue = "720") int maxRollback
    );

}