You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
yacy_search_server/source/net/yacy/kelondro/blob/TablesColumnBLOBIndex.java

218 lines
6.5 KiB

// TablesColumnBLOBIndex.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.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
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.util.ByteBuffer;
import net.yacy.cora.util.ConcurrentLog;
import net.yacy.cora.util.SpaceExceededException;
12 years ago
import net.yacy.data.ymark.YMarkUtil;
public class TablesColumnBLOBIndex extends TablesColumnIndex{
// Map<ColumnName, Map<ColumnValue, T<PrimaryKey>>>
12 years ago
private final BEncodedHeap index;
private final static byte SEPERATOR = (byte) ',';
12 years ago
public TablesColumnBLOBIndex(final BEncodedHeap bheap) {
super(TablesColumnIndex.INDEXTYPE.BLOB);
12 years ago
this.index = bheap;
}
12 years ago
public static Collection<byte[]> byteToCollection(final byte[] b) {
final Collection<byte[]> PKset = ByteBuffer.split(b, SEPERATOR);
return PKset;
}
12 years ago
public static byte[] CollectionToByte(final Collection<byte[]> bc) {
final ByteBuffer buf = new ByteBuffer(15 * bc.size());
final Iterator<byte[]> iter = bc.iterator();
while(iter.hasNext()) {
buf.append(iter.next());
buf.append(SEPERATOR);
}
12 years ago
byte[] b = buf.getBytes();
try {buf.close(); } catch (final IOException e) {}
12 years ago
return b;
}
12 years ago
@Override
public void deleteIndex(final String columnName) {
final byte[] column = YMarkUtil.getKeyId(columnName);
try {
this.index.remove(column);
} catch (final IOException e) {
ConcurrentLog.logException(e);
} catch (final SpaceExceededException e) {
ConcurrentLog.logException(e);
}
}
12 years ago
@Override
protected void insertPK(final String columnName, final String columnValue, final byte[] pk) {
Map<String, byte[]> valueIdxMap;
12 years ago
Collection<byte[]> PKset;
final byte[] column = YMarkUtil.getKeyId(columnName);
try {
valueIdxMap = this.index.get(column);
12 years ago
if(valueIdxMap != null) {
if(valueIdxMap.containsKey(columnValue)) {
12 years ago
PKset = byteToCollection(valueIdxMap.get(columnValue));
if(!ByteBuffer.contains(PKset, pk)) {
PKset.add(pk);
}
} else {
PKset = new ArrayList<byte[]>(1);
PKset.add(pk);
valueIdxMap.put(columnValue, CollectionToByte(PKset));
}
} else {
PKset = new ArrayList<byte[]>(1);
PKset.add(pk);
valueIdxMap = new ConcurrentHashMap<String, byte[]>();
}
12 years ago
valueIdxMap.put(columnValue, CollectionToByte(PKset));
this.index.insert(column, valueIdxMap);
return;
} catch (final IOException e) {
ConcurrentLog.logException(e);
} catch (final SpaceExceededException e) {
ConcurrentLog.logException(e);
12 years ago
}
}
12 years ago
@Override
protected void removePK(final byte[] pk) {
final Iterator<Map.Entry<byte[], Map<String, byte[]>>> niter = this.index.iterator();
while (niter.hasNext()) {
final Map.Entry<byte[], Map<String,byte[]>> entry = niter.next();
final Iterator<Map.Entry<String, byte[]>> viter = entry.getValue().entrySet().iterator();
while(viter.hasNext()) {
final Map.Entry<String, byte[]> columnValue = viter.next();
final Collection<byte[]> PKset = byteToCollection(columnValue.getValue());
12 years ago
ByteBuffer.remove(PKset, pk);
if(PKset.isEmpty()) {
12 years ago
viter.remove();
} else {
12 years ago
columnValue.setValue(CollectionToByte(PKset));
}
}
try {
this.index.insert(entry.getKey(), entry.getValue());
} catch (final SpaceExceededException e) {
ConcurrentLog.logException(e);
} catch (final IOException e) {
ConcurrentLog.logException(e);
}
}
}
12 years ago
@Override
public void clear() {
this.index.clear();
}
12 years ago
@Override
public Collection<String> columns() {
return this.index.columns();
}
12 years ago
@Override
public Set<String> keySet(final String columnName) {
final byte[] column = YMarkUtil.getKeyId(columnName);
// a TreeSet is used to get sorted set of keys (e.g. folders)
if(this.index.containsKey(column)) {
try {
return new TreeSet<String>(this.index.get(column).keySet());
} catch (final IOException e) {
ConcurrentLog.logException(e);
} catch (final SpaceExceededException e) {
ConcurrentLog.logException(e);
}
12 years ago
}
return new TreeSet<String>();
}
12 years ago
@Override
public boolean containsKey(final String columnName, final String key) {
final byte[] column = YMarkUtil.getKeyId(columnName);
if(this.index.containsKey(column)) {
try {
return this.index.get(column).containsKey(key);
} catch (final IOException e) {
ConcurrentLog.logException(e);
} catch (final SpaceExceededException e) {
ConcurrentLog.logException(e);
}
}
return false;
}
12 years ago
@Override
public boolean hasIndex(final String columnName) {
final byte[] column = YMarkUtil.getKeyId(columnName);
return this.index.containsKey(column);
}
12 years ago
@Override
public Collection<byte[]> get(final String columnName, final String key) {
final byte[] column = YMarkUtil.getKeyId(columnName);
// deserialize
try {
return byteToCollection(this.index.get(column).get(key));
} catch (final IOException e) {
ConcurrentLog.logException(e);
} catch (final SpaceExceededException e) {
ConcurrentLog.logException(e);
}
return new ArrayList<byte[]>();
}
12 years ago
@Override
public int size(final String columnName) {
final byte[] column = YMarkUtil.getKeyId(columnName);
if(this.index.containsKey(column)) {
try {
return this.index.get(column).size();
} catch (final IOException e) {
ConcurrentLog.logException(e);
} catch (final SpaceExceededException e) {
ConcurrentLog.logException(e);
}
}
return -1;
12 years ago
}
@Override
public int size() {
return this.index.size();
}
}