moved table row/column matching method from front-end to back-end

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@6770 6c8d7289-2bf4-0310-a012-ef5d649a1542
pull/1/head
orbiter 15 years ago
parent e12f1fd821
commit 0018163c07

@ -106,7 +106,8 @@ public class Table_API_p {
// insert rows
int count = 0;
try {
final Iterator<Tables.Row> mapIterator = sb.tables.orderBy(WorkTables.TABLE_API_NAME, -1, WorkTables.TABLE_API_COL_DATE).iterator();
final Iterator<Tables.Row> plainIterator = sb.tables.iterator(WorkTables.TABLE_API_NAME);
final Iterator<Tables.Row> mapIterator = sb.tables.orderBy(plainIterator, -1, WorkTables.TABLE_API_COL_DATE).iterator();
Tables.Row row;
boolean dark = true;
while (mapIterator.hasNext()) {

@ -173,26 +173,15 @@ public class Tables_p {
}
count = 0;
try {
final Iterator<Tables.Row> mapIterator = sb.tables.orderByPK(table, (matcher == null) ? maxcount : -1).iterator();
final Iterator<Tables.Row> plainIterator = sb.tables.iterator(table, matcher);
final Iterator<Tables.Row> mapIterator = sb.tables.orderByPK(plainIterator, maxcount).iterator();
Tables.Row row;
boolean dark = true;
byte[] cell;
tableloop: while (mapIterator.hasNext() && count < maxcount) {
while (mapIterator.hasNext() && count < maxcount) {
row = mapIterator.next();
if (row == null) continue;
// check matcher
boolean matched = matcher == null;
if (matcher != null) checkloop: for (int i = 0; i < columns.size(); i++) {
cell = row.from(columns.get(i));
if (cell == null) continue checkloop;
if (matcher.matcher(new String(cell)).matches()) {
matched = true;
break checkloop;
}
}
if (!matched) continue tableloop;
// write table content
prop.put("showtable_list_" + count + "_dark", ((dark) ? 1 : 0) ); dark=!dark;
prop.put("showtable_list_" + count + "_pk", new String(row.getPK()));

@ -82,7 +82,8 @@ public class table_p {
}
int count = 0;
try {
final Iterator<Tables.Row> mapIterator = sb.tables.orderByPK(table, maxCount).iterator();
final Iterator<Tables.Row> plainIterator = sb.tables.iterator(table);
final Iterator<Tables.Row> mapIterator = sb.tables.orderByPK(plainIterator, maxCount).iterator();
Tables.Row trow;
boolean dark = true;
byte[] cell;

@ -36,6 +36,7 @@ import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;
import net.yacy.kelondro.index.RowSpaceExceededException;
import net.yacy.kelondro.logging.Log;
@ -334,37 +335,35 @@ public class Tables {
return new RowIterator(table);
}
public Iterator<Row> iterator(String table, String whereKey, byte[] whereValue) throws IOException {
return new RowIterator(table, whereKey, whereValue);
public Iterator<Row> iterator(String table, String whereColumn, byte[] whereValue) throws IOException {
return new RowIterator(table, whereColumn, whereValue);
}
public Collection<Row> orderByPK(String table, int maxcount) throws IOException {
return orderByPK(table, maxcount, null, null);
public Iterator<Row> iterator(String table, String whereColumn, Pattern wherePattern) throws IOException {
return new RowIterator(table, whereColumn, wherePattern);
}
public Collection<Row> orderByPK(String table, int maxcount, String whereKey, byte[] whereValue) throws IOException {
public Iterator<Row> iterator(String table, Pattern wherePattern) throws IOException {
return new RowIterator(table, wherePattern);
}
public Collection<Row> orderByPK(Iterator<Row> rowIterator, int maxcount) throws IOException {
TreeMap<String, Row> sortTree = new TreeMap<String, Row>();
Iterator<Row> i = iterator(table, whereKey, whereValue);
Row row;
while ((maxcount < 0 || maxcount-- > 0) && i.hasNext()) {
row = i.next();
while ((maxcount < 0 || maxcount-- > 0) && rowIterator.hasNext()) {
row = rowIterator.next();
sortTree.put(new String(row.pk), row);
}
return sortTree.values();
}
public Collection<Row> orderBy(String table, int maxcount, String sortField) throws IOException {
return orderBy(table, maxcount, sortField, null, null);
}
public Collection<Row> orderBy(String table, int maxcount, String sortField, String whereKey, byte[] whereValue) throws IOException {
public Collection<Row> orderBy(Iterator<Row> rowIterator, int maxcount, String sortColumn) throws IOException {
TreeMap<String, Row> sortTree = new TreeMap<String, Row>();
Iterator<Row> i = iterator(table, whereKey, whereValue);
Row row;
byte[] r;
while ((maxcount < 0 || maxcount-- > 0) && i.hasNext()) {
row = i.next();
r = row.from(sortField);
while ((maxcount < 0 || maxcount-- > 0) && rowIterator.hasNext()) {
row = rowIterator.next();
r = row.from(sortColumn);
if (r == null) continue;
sortTree.put(new String(r) + new String(row.pk), row);
}
@ -378,20 +377,68 @@ public class Tables {
public class RowIterator extends LookAheadIterator<Row> implements Iterator<Row> {
private final String whereKey;
private final String whereColumn;
private final byte[] whereValue;
private final Pattern wherePattern;
private final Iterator<Map.Entry<byte[], Map<String, byte[]>>> i;
/**
* iterator that iterates all elements in the given table
* @param table
* @throws IOException
*/
public RowIterator(String table) throws IOException {
this.whereKey = null;
this.whereColumn = null;
this.whereValue = null;
this.wherePattern = null;
BEncodedHeap heap = getHeap(table);
i = heap.iterator();
}
public RowIterator(String table, String whereKey, byte[] whereValue) throws IOException {
this.whereKey = whereKey;
/**
* iterator that iterates all elements in the given table
* where a given column is equal to a given value
* @param table
* @param whereColumn
* @param whereValue
* @throws IOException
*/
public RowIterator(String table, String whereColumn, byte[] whereValue) throws IOException {
assert whereColumn != null || whereValue == null;
this.whereColumn = whereColumn;
this.whereValue = whereValue;
this.wherePattern = null;
BEncodedHeap heap = getHeap(table);
i = heap.iterator();
}
/**
* iterator that iterates all elements in the given table
* where a given column matches with a given value
* @param table
* @param whereColumn
* @param wherePattern
* @throws IOException
*/
public RowIterator(String table, String whereColumn, Pattern wherePattern) throws IOException {
this.whereColumn = whereColumn;
this.whereValue = null;
this.wherePattern = wherePattern;
BEncodedHeap heap = getHeap(table);
i = heap.iterator();
}
/**
* iterator that iterates all elements in the given table
* where any column matches with a given value
* @param table
* @param pattern
* @throws IOException
*/
public RowIterator(String table, Pattern pattern) throws IOException {
this.whereColumn = null;
this.whereValue = null;
this.wherePattern = pattern;
BEncodedHeap heap = getHeap(table);
i = heap.iterator();
}
@ -399,8 +446,21 @@ public class Tables {
protected Row next0() {
while (i.hasNext()) {
Row r = new Row(i.next());
if (this.whereKey == null) return r;
if (ByteBuffer.equals(r.from(this.whereKey), this.whereValue)) return r;
if (this.whereValue != null) {
if (ByteBuffer.equals(r.from(this.whereColumn), this.whereValue)) return r;
} else if (this.wherePattern != null) {
if (this.whereColumn == null) {
// shall match any column
for (byte[] b: r.values()) {
if (this.wherePattern.matcher(new String(b)).matches()) return r;
}
} else {
// must match the given column
if (this.wherePattern.matcher(new String(r.from(this.whereColumn))).matches()) return r;
}
} else {
return r;
}
}
return null;
}
@ -450,6 +510,10 @@ public class Tables {
return r;
}
protected Collection<byte[]> values() {
return this.map.values();
}
public String toString() {
StringBuilder sb = new StringBuilder(keymaxlen + 20 * map.size());
sb.append(new String(pk)).append(":").append(map.toString());

Loading…
Cancel
Save