// TablesColumnRAMIndex.java // (C) 2012 by Stefan Foerster, sof@gmx.de, Norderstedt, Germany // first published 2012 on http://yacy.net // // This is a part of YaCy, a peer-to-peer based web search engine // // LICENSE // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 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 General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA package net.yacy.kelondro.blob; import java.util.Collection; import java.util.Comparator; import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.TreeSet; import java.util.concurrent.ConcurrentHashMap; import net.yacy.cora.order.NaturalOrder; public class TablesColumnRAMIndex extends TablesColumnIndex{ // Map>> private final Map>> index; private final static Comparator NATURALORDER = new NaturalOrder(true); public TablesColumnRAMIndex() { super(TablesColumnIndex.INDEXTYPE.RAM); this.index = new ConcurrentHashMap>>(); } public void deleteIndex(final String columnName) { this.index.remove(columnName); } protected void insertPK(final String columnName, final String columnValue, final byte[] pk) { Map> valueIdxMap; TreeSet PKset; if(this.index.containsKey(columnName)) { valueIdxMap = this.index.get(columnName); } else { valueIdxMap = new ConcurrentHashMap>(); this.index.put(columnName, valueIdxMap); } if(valueIdxMap.containsKey(columnValue)) { PKset = valueIdxMap.get(columnValue); } else { PKset = new TreeSet(NATURALORDER); valueIdxMap.put(columnValue, PKset); } PKset.add(pk); } protected synchronized void removePK(final byte[] pk) { for(Map.Entry>> columnName : this.index.entrySet()) { final Iterator>> viter = columnName.getValue().entrySet().iterator(); while(viter.hasNext()) { final Map.Entry> columnValue = viter.next(); columnValue.getValue().remove(pk); if(columnValue.getValue().isEmpty()) viter.remove(); } } } public void clear() { this.index.clear(); } public Collection columns() { return this.index.keySet(); } public Set keySet(final String columnName) { // a TreeSet is used to get sorted set of keys (e.g. folders) if(this.index.containsKey(columnName)) { return new TreeSet(this.index.get(columnName).keySet()); } return new TreeSet(); } public boolean containsKey(final String columnName, final String columnValue) { if(this.index.containsKey(columnName)) { return this.index.get(columnName).containsKey(columnValue); } return false; } public boolean hasIndex(final String columnName) { return this.index.containsKey(columnName); } public Collection get(final String columnName, final String key) { return this.index.get(columnName).get(key); } public int size(final String columnName) { if(this.index.containsKey(columnName)) { return this.index.get(columnName).size(); } return -1; } public int size() { return this.index.size(); } }