/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights reserved. <a>http://www.crunii.com</a>
 */
package com.crunii.micro.common.job.common;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import lombok.extern.slf4j.Slf4j;

/**
 * <pre>快速扫描的表队列的加锁控制，及时返回加锁结果</pre>
 * @author 田平 create 2020年3月25日上午10:41:14
 *
 */
@Slf4j
public class JobLock {

	private ConcurrentMap<String, String> lockMap = new ConcurrentHashMap<String, String>(128);

	public JobLock() {
		lockMap = new ConcurrentHashMap<String, String>(512);
	}

	public JobLock(int initialSize) {
		lockMap = new ConcurrentHashMap<String, String>(initialSize);
	}

	public boolean lock(String lockId) {
		String old = lockMap.putIfAbsent(lockId, lockId);
		if (old == null) {
			return true;
		} else {
			return false;
		}
	}

	public void unlock(String lockId) {
		String old = lockMap.remove(lockId);
		if (old == null) {
			log.warn("lockId:{} not exists lock!", lockId);
		}
	}

	public int lockSize() {
		return this.lockMap.size();
	}

}
