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

org.restcomm.cluster.TreeTxState Maven / Gradle / Ivy

/*
 * Copyright 2022-2023, Mobius Software LTD. and individual contributors
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This program is free software: you can redistribute it and/or modify
 * under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation; either version 3 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see 
 */

package org.restcomm.cluster;

import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;

import org.restcomm.cluster.data.TreeSegment;
import org.restcomm.cluster.data.WriteOperation;

/**
 * Holds last operation for specific tree path and its data
 * @author yulian.oifa
 *
 */
public class TreeTxState {
	private Boolean createdInThisTX;
	private Boolean isPreloaded=false;
	private WriteOperation operation;
	private Object data;
	private ConcurrentHashMap, TreeTxState> childs=null;
	
	public TreeTxState(Boolean createdInThisTX, WriteOperation operation, Object data) {
		this.createdInThisTX=createdInThisTX;
		this.operation = operation;
		this.data = data;
	}

	public WriteOperation getOperation() {
		return operation;
	}

	public Object getData() {
		return data;
	}
	
	public Boolean isPreloaded() {
		return isPreloaded;
	}
	
	public void setIsPreloaded(Boolean value) {
		this.isPreloaded=value;
	}
	
	public Boolean createdInThisTX() {
		return createdInThisTX;
	}
	
	public void updateOperation(Boolean createdInThisTX, WriteOperation operation,Object data) {
		this.createdInThisTX=createdInThisTX;
		this.operation=operation;
		this.data=data;
	}
	
	public ConcurrentHashMap, TreeTxState> getAllChilds() {
		return childs;
	}
	
	public boolean hasRealChilds() {
		if(childs==null || childs.size()==0)
			return false;
		
		ConcurrentLinkedQueue, TreeTxState>> pendingItems=new ConcurrentLinkedQueue, TreeTxState>>();
		pendingItems.addAll(childs.entrySet());
		while(pendingItems.size()>0) {			
			Entry, TreeTxState> currEntry = pendingItems.poll();
			if(currEntry!=null) {
				if(currEntry.getValue()!=null && currEntry.getValue().getOperation()!=null && currEntry.getValue().getOperation()!=WriteOperation.NOOP)
					return true;
				
				if(currEntry.getValue()!=null && currEntry.getValue().getAllChilds()!=null && currEntry.getValue().getAllChilds().size()>0)
					pendingItems.addAll(currEntry.getValue().getAllChilds().entrySet());
			}
		}
		
		return false;
	}
	
	public void clearChilds() {
		childs=null;
	}
	
	public void setChilds(ConcurrentHashMap, TreeTxState> map) {
		this.childs=map;
	}
	
	public void addChild(TreeSegment key,TreeTxState state) {
		if(childs==null)
			childs=new ConcurrentHashMap, TreeTxState>();
		
		childs.put(key, state);
	}
	
	public TreeTxState getChild(TreeSegment segment) {
		if(childs==null)
			return null;
		
		return childs.get(segment);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy