sequence
This article focuses on Claudb’s DatabaseCleaner
DatabaseCleaner
Claudb 1.7.1 / SRC/main/Java/com/lot/tonivade/claudb/data/DatabaseCleaner Java
public class DatabaseCleaner { private static final Logger LOGGER = LoggerFactory.getLogger(DatabaseCleaner.class); private final DBServerContext server; private final DBConfig config; private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); public DatabaseCleaner(DBServerContext server, DBConfig config) { this.server = server; this.config = config; } public void start() { executor.scheduleWithFixedDelay(this::clean, config.getCleanPeriod(), config.getCleanPeriod(), TimeUnit.SECONDS); } public void stop() { executor.shutdown(); } private void clean() { LOGGER.debug("cleaning database: running"); server.clean(Instant.now()); LOGGER.debug("cleaning database: done"); }}Copy the code
- DatabaseCleaner start method every config.getCleanPeriod()(
The default 30
) second scheduling executes clean method; Clear method server.clean(instant.now ())
clean
Claudb 1.7.1 / SRC/main/Java/com/lot/tonivade/claudb/claudb Java
public class ClauDB extends RespServerContext implements DBServerContext { //...... public void clean(Instant now) { executeOn(Observable.create(observable -> { getState().evictExpired(now); observable.onComplete(); })).blockingSubscribe(); } / /... }Copy the code
- ClauDB’s clean method executes getState().evictexpired (now)
evictExpired
Claudb 1.7.1 / SRC/main/Java/com/lot/tonivade/claudb/DBServerState Java
public class DBServerState { //...... public void evictExpired(Instant now) { for (Database database : databases) { database.evictableKeys(now).forEach(database::remove); }} / /... }Copy the code
- The evictExpired method executes database.evicTableKeys (now).foreach (database::remove)
evictableKeys
Claudb 1.7.1 / SRC/main/Java/com/lot/tonivade/claudb/data/Database Java
public interface Database { //...... default ImmutableSet<DatabaseKey> evictableKeys(Instant now) { return entrySet() .filter(entry -> entry.get2().isExpired(now)) .map(Tuple2::get1); } / /... }Copy the code
- The evictableKeys method iterates through entrySet to find the ImmutableSet whose isExpired is true for DatabaseValue
summary
DatabaseCleaner’s start method schedules the clean method every config.getCleanPeriod()(default: 30) seconds; Clear method server.clean(instant.now ())
doc
- DatabaseCleaner