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

org.aksw.sparqlify.database.TreeIndex Maven / Gradle / Ivy

The newest version!
package org.aksw.sparqlify.database;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;










/*
class ListIndexMetaFactory
	implements MetaIndexFactory
{
	public static Set> supportedConstraintClasses = new HashSet>();
	
	static
	{
		supportedConstraintClasses.add(PrefixConstraint.class);
	}
	
	public Set> getSupportedConstraintClasses() {
		return supportedConstraintClasses;
	}
	
	
	private Transformer> prefixExtractor;
	
	public ListIndexMetaFactory(Transformer> prefixExtractor) {
		this.prefixExtractor = prefixExtractor;
	}
	
	@Override
	public MapStoreAccessor create(Table table) {
		
		int[] indexColumns = new int[];
		
		for(int i = 0; i < indexColumns.length; ++i) {
			String columnName = columnNames.get(i);
			indexColumns[i] = table.getColumns().getIndex(columnName);
		}
	
		PrefixMapStoreAccessor accessor = new PrefixMapStoreAccessor(indexColumns, prefixExtractor);
		
		return accessor;
	}
	
}
*/


/*
interface MapStoreFactory
{
	MapStoreAccessor getAccessor();
	Object createStore();
}


class PrefixMapStoreFactory
	implements MapStoreFactory
{
	private PrefixMapStoreAccessor accessor;

	public PrefixMapStoreFactory(PrefixMapStoreAccessor accessor) {
		this.accessor = accessor;
	}
		
	public Object createStore() {
		return new TreeMap();
	}
	
	public MapStoreAccessor getAccessor() {
		return accessor;
	}	
}
*/





/**
 * 
 * 
 * @author Claus Stadler 
 *
 */
interface Index2 {
	Table getTable();
	
	Index2 getParent(); // null if there is no parent index
	List getColumnNames();
	
	Set> getSupportedConstraintClasses();
	
	List lookup(List keys);
}

class IndexBase {
	protected Table table;
	protected List columnNames;
	
	public Table getTable() {
		return table;
	}
	
	public List getColumnNames() {
		return columnNames;
	}
}


class IndexNode {
	//Map ;
	
	//Map columnNameToSubIndex; // schemaLevel
		
	
}




/*
class ColumnIndexPair
{
	private List columns;
	private List factory;
}*/


/**
 * 
 * @author Claus Stadler 
 *
 */
/*
class TreeIndexBuilder {
	private List cis = new ArrayList();
	
	public IndexMetaNode add(String ... columns) {
		
	}
	
	public Index create() {
		
	}
}
*/



public class TreeIndex
	extends AbstractIndex
{
	private IndexMetaNode root;
	private Object store;


	public TreeIndex(Table table, IndexMetaNode root) 
	{
		super(table, null, null);
		this.root = root;
		this.store = root.getFactory().createStore();
	}

	public Object getStore() {
		return store;
	}
	
	
	@Override
	public void add(List row) {
		add(row, root, store);
	}
	
	private void add(List row, List nodes, List stores) {

		Iterator itNode = nodes.iterator();
		Iterator itStore = stores.iterator();

		while(itNode.hasNext()) {
			IndexMetaNode node = itNode.next();
			Object store = itStore.next();
			
			add(row, node, store);
		}
	}
	
	/**
	 * Adds a row to the index. Depending on the type of node
	 * tree, different datastructures are used.
	 * 
	 * 
	 * @param row
	 * @param node
	 * @param store
	 */
	private void add(List row, IndexMetaNode node, Object store)
	{
		List children = node.getChildren();
		MapStoreAccessor accessor = node.getFactory(); 

		Object value = accessor.get(store, row);
		
		switch(children.size()) {
		case 0:
			List> rows = (List>)value;
			if(rows == null) {
				rows = new ArrayList>();
				accessor.put(store, row, rows);
			}
			
			rows.add(row);
			break;

		case 1:
			Object subStore = value;
			IndexMetaNode subNode = node.getChildren().iterator().next();
			if(subStore == null) {
				subStore = subNode.getFactory().createStore();
				accessor.put(store, row, subStore);
			}
			
			add(row, subNode, subStore);
			break;
			
		default:
			List subStores = (List)value;
			if(subStores == null) {
				subStores = new ArrayList(children.size());
				
				for(IndexMetaNode child : children) {
					subStores.add(child.getFactory().createStore());
				}
			
				accessor.put(store, row, subStores);
			}
			
			add(row, children, subStores);
			break;
		}			
	}
	
	
	/*
	private void put() {
		NavigableMap current = map;
		
		for(int i = 0; i < indexColumns.length; ++i) {
			boolean isLast = i == indexColumns.length - 1;
			int index = indexColumns[i];
			Object value = row.get(index);
			
			Set prefixes = prefixExtractor.transform(value);
			
			for(String prefix : prefixes) {
				Object o = current.get(prefix);
				
				if(isLast) {
					if(o == null) {
						current.put(prefix, row);
					} else {
						throw new RuntimeException("Duplicate row");
					}
				} else {				
					NavigableMap next = (NavigableMap) o;
					if(next == null) {
						next = new TreeMap();
						
						current.put(prefix, next);
					}
					current = next;
				}
			}
		}

	}*/

	@Override
	public IndexMetaNode getRoot() {
		return root;
	}


	
	
	/*
	List subMetaIndexes;
	
	TreeMap map = new TreeMap();
*/
	
	public static TreeIndex attach(Table table, IndexMetaNode node) {

		TreeIndex index = new TreeIndex(table, node);
		
		table.addIndex(index);
		
		return index;
	}

	@Override
	public String toString() {
		return "TreeIndex [root=" + root + ", store=" + store + "]";
	}
}