From 0018163c0779f602902da74411ae019f0b04422d Mon Sep 17 00:00:00 2001 From: orbiter Date: Fri, 26 Mar 2010 10:01:27 +0000 Subject: [PATCH] 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 --- htroot/Table_API_p.java | 3 +- htroot/Tables_p.java | 17 +--- htroot/api/table_p.java | 3 +- source/net/yacy/kelondro/blob/Tables.java | 110 +++++++++++++++++----- 4 files changed, 94 insertions(+), 39 deletions(-) diff --git a/htroot/Table_API_p.java b/htroot/Table_API_p.java index 6e7b24d4c..797cd6fb3 100644 --- a/htroot/Table_API_p.java +++ b/htroot/Table_API_p.java @@ -106,7 +106,8 @@ public class Table_API_p { // insert rows int count = 0; try { - final Iterator mapIterator = sb.tables.orderBy(WorkTables.TABLE_API_NAME, -1, WorkTables.TABLE_API_COL_DATE).iterator(); + final Iterator plainIterator = sb.tables.iterator(WorkTables.TABLE_API_NAME); + final Iterator mapIterator = sb.tables.orderBy(plainIterator, -1, WorkTables.TABLE_API_COL_DATE).iterator(); Tables.Row row; boolean dark = true; while (mapIterator.hasNext()) { diff --git a/htroot/Tables_p.java b/htroot/Tables_p.java index 4054c665f..2b6e76047 100644 --- a/htroot/Tables_p.java +++ b/htroot/Tables_p.java @@ -173,26 +173,15 @@ public class Tables_p { } count = 0; try { - final Iterator mapIterator = sb.tables.orderByPK(table, (matcher == null) ? maxcount : -1).iterator(); + final Iterator plainIterator = sb.tables.iterator(table, matcher); + final Iterator 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())); diff --git a/htroot/api/table_p.java b/htroot/api/table_p.java index cbd9dcb67..3692c4fb6 100644 --- a/htroot/api/table_p.java +++ b/htroot/api/table_p.java @@ -82,7 +82,8 @@ public class table_p { } int count = 0; try { - final Iterator mapIterator = sb.tables.orderByPK(table, maxCount).iterator(); + final Iterator plainIterator = sb.tables.iterator(table); + final Iterator mapIterator = sb.tables.orderByPK(plainIterator, maxCount).iterator(); Tables.Row trow; boolean dark = true; byte[] cell; diff --git a/source/net/yacy/kelondro/blob/Tables.java b/source/net/yacy/kelondro/blob/Tables.java index f67076a11..3d621ef4c 100644 --- a/source/net/yacy/kelondro/blob/Tables.java +++ b/source/net/yacy/kelondro/blob/Tables.java @@ -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 iterator(String table, String whereKey, byte[] whereValue) throws IOException { - return new RowIterator(table, whereKey, whereValue); + public Iterator iterator(String table, String whereColumn, byte[] whereValue) throws IOException { + return new RowIterator(table, whereColumn, whereValue); } - public Collection orderByPK(String table, int maxcount) throws IOException { - return orderByPK(table, maxcount, null, null); + public Iterator iterator(String table, String whereColumn, Pattern wherePattern) throws IOException { + return new RowIterator(table, whereColumn, wherePattern); } - public Collection orderByPK(String table, int maxcount, String whereKey, byte[] whereValue) throws IOException { + public Iterator iterator(String table, Pattern wherePattern) throws IOException { + return new RowIterator(table, wherePattern); + } + + public Collection orderByPK(Iterator rowIterator, int maxcount) throws IOException { TreeMap sortTree = new TreeMap(); - Iterator 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 orderBy(String table, int maxcount, String sortField) throws IOException { - return orderBy(table, maxcount, sortField, null, null); - } - - public Collection orderBy(String table, int maxcount, String sortField, String whereKey, byte[] whereValue) throws IOException { + public Collection orderBy(Iterator rowIterator, int maxcount, String sortColumn) throws IOException { TreeMap sortTree = new TreeMap(); - Iterator 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 implements Iterator { - private final String whereKey; + private final String whereColumn; private final byte[] whereValue; + private final Pattern wherePattern; private final Iterator>> 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 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());