All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.datarouter.job.storage.triggerlock.DatarouterTriggerLockDao Maven / Gradle / Ivy

The newest version!
/*
 * Copyright © 2009 HotPads ([email protected])
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.datarouter.job.storage.triggerlock;

import java.time.Duration;
import java.util.List;

import io.datarouter.job.storage.triggerlock.TriggerLock.TriggerLockFielder;
import io.datarouter.scanner.Scanner;
import io.datarouter.storage.Datarouter;
import io.datarouter.storage.client.ClientId;
import io.datarouter.storage.config.Config;
import io.datarouter.storage.config.PutMethod;
import io.datarouter.storage.dao.BaseDao;
import io.datarouter.storage.node.factory.NodeFactory;
import io.datarouter.storage.node.op.combo.SortedMapStorage.SortedMapStorageNode;
import io.datarouter.storage.tag.Tag;
import io.datarouter.storage.vacuum.PrimaryKeyVacuum;
import io.datarouter.storage.vacuum.PrimaryKeyVacuum.PrimaryKeyVacuumBuilder;
import io.datarouter.types.MilliTime;
import io.datarouter.virtualnode.redundant.RedundantSortedMapStorageNode;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;

@Singleton
public class DatarouterTriggerLockDao extends BaseDao{

	public record DatarouterTriggerLockDaoParams(List clientIds){
	}

	private final SortedMapStorageNode node;

	@Inject
	public DatarouterTriggerLockDao(
			Datarouter datarouter,
			NodeFactory nodeFactory,
			DatarouterTriggerLockDaoParams params){
		super(datarouter);

		node = Scanner.of(params.clientIds)
				.map(clientId -> {
					SortedMapStorageNode node =
							nodeFactory.create(clientId, TriggerLock::new, TriggerLockFielder::new)
							.withTag(Tag.DATAROUTER)
							.build();
					return node;
				})
				.listTo(RedundantSortedMapStorageNode::makeIfMulti);
		datarouter.register(node);
	}

	public TriggerLock get(TriggerLockKey key){
		return node.get(key);
	}

	public void putAndAcquire(TriggerLock databean){
		var config = new Config()
				.setPutMethod(PutMethod.INSERT_OR_BUST)
				.setIgnoreException(true);
		node.put(databean, config);
	}

	public void delete(TriggerLockKey key){
		node.delete(key);
	}

	public PrimaryKeyVacuum makeVacuum(){
		Duration retentionPeriod = Duration.ofDays(7);
		MilliTime deleteBeforeTime = MilliTime.now().minus(retentionPeriod);
		return new PrimaryKeyVacuumBuilder<>(
				node.scanKeys(),
				key -> key.getTriggerTime().isBefore(deleteBeforeTime),
				node::deleteMulti)
				.build();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy