|
|
|
@ -63,7 +63,7 @@ public class Tables implements Iterable<String> {
|
|
|
|
|
public final static String CIDX = "_cidx";
|
|
|
|
|
public final static int NOINDEX = 50000;
|
|
|
|
|
public final static int RAMINDEX = 100000;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final String suffix = ".bheap";
|
|
|
|
|
private static final String system_table_pkcounter = "pkcounter";
|
|
|
|
|
private static final String system_table_pkcounter_counterName = "pk";
|
|
|
|
@ -95,7 +95,7 @@ public class Tables implements Iterable<String> {
|
|
|
|
|
}
|
|
|
|
|
this.cidx = new ConcurrentHashMap<String, TablesColumnIndex>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public TablesColumnIndex getIndex(final String tableName, TablesColumnIndex.INDEXTYPE indexType) throws Exception {
|
|
|
|
|
final TablesColumnIndex index;
|
|
|
|
|
switch(indexType) {
|
|
|
|
@ -104,7 +104,7 @@ public class Tables implements Iterable<String> {
|
|
|
|
|
break;
|
|
|
|
|
case BLOB:
|
|
|
|
|
final String idx_table = tableName+CIDX;
|
|
|
|
|
BEncodedHeap bheap;
|
|
|
|
|
BEncodedHeap bheap;
|
|
|
|
|
bheap = this.getHeap(idx_table);
|
|
|
|
|
index = new TablesColumnBLOBIndex(bheap);
|
|
|
|
|
break;
|
|
|
|
@ -113,13 +113,13 @@ public class Tables implements Iterable<String> {
|
|
|
|
|
}
|
|
|
|
|
return index;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public TablesColumnIndex getIndex(final String tableName) throws Exception {
|
|
|
|
|
// return an existing index
|
|
|
|
|
if(this.cidx.containsKey(tableName)) {
|
|
|
|
|
return this.cidx.get(tableName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// create a new index
|
|
|
|
|
int size;
|
|
|
|
|
try {
|
|
|
|
@ -129,15 +129,15 @@ public class Tables implements Iterable<String> {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final TablesColumnIndex index;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(size < NOINDEX) {
|
|
|
|
|
throw new Exception("TableColumnIndex not available for tables with less than "+NOINDEX+" rows: "+tableName);
|
|
|
|
|
}
|
|
|
|
|
throw new RuntimeException("TableColumnIndex not available for tables with less than "+NOINDEX+" rows: "+tableName);
|
|
|
|
|
}
|
|
|
|
|
if(size < RAMINDEX) {
|
|
|
|
|
index = new TablesColumnRAMIndex();
|
|
|
|
|
} else {
|
|
|
|
|
final String idx_table = tableName+CIDX;
|
|
|
|
|
BEncodedHeap bheap;
|
|
|
|
|
BEncodedHeap bheap;
|
|
|
|
|
try {
|
|
|
|
|
bheap = this.getHeap(idx_table);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
@ -146,20 +146,20 @@ public class Tables implements Iterable<String> {
|
|
|
|
|
}
|
|
|
|
|
if(bheap != null) {
|
|
|
|
|
index = new TablesColumnBLOBIndex(bheap);
|
|
|
|
|
} else {
|
|
|
|
|
} else {
|
|
|
|
|
index = new TablesColumnRAMIndex();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
this.cidx.put(tableName, index);
|
|
|
|
|
this.cidx.put(tableName, index);
|
|
|
|
|
return index;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public boolean hasIndex (final String tableName) {
|
|
|
|
|
return this.cidx.contains(tableName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public boolean hasIndex (final String tableName, final String columnName) {
|
|
|
|
|
if(this.cidx.containsKey(tableName)) {
|
|
|
|
|
if(this.cidx.containsKey(tableName)) {
|
|
|
|
|
return this.cidx.get(tableName).hasIndex(columnName);
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
@ -168,22 +168,22 @@ public class Tables implements Iterable<String> {
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
Log.logException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Iterator<Row> getByIndex(final String table, final String whereColumn, final String separator, final String whereValue) {
|
|
|
|
|
public Iterator<Row> getByIndex(final String table, final String whereColumn, final String separator, final String whereValue) {
|
|
|
|
|
final HashSet<Tables.Row> rows = new HashSet<Tables.Row>();
|
|
|
|
|
final TreeSet<byte[]> set1 = new TreeSet<byte[]>(TablesColumnIndex.NATURALORDER);
|
|
|
|
|
final TreeSet<byte[]> set2 = new TreeSet<byte[]>(TablesColumnIndex.NATURALORDER);
|
|
|
|
|
final String[] values = whereValue.split(separator);
|
|
|
|
|
if(this.hasIndex(table, whereColumn)) {
|
|
|
|
|
try {
|
|
|
|
|
try {
|
|
|
|
|
final TablesColumnIndex index = this.getIndex(table);
|
|
|
|
|
for(int i=0; i<values.length; i++) {
|
|
|
|
|
if(index.containsKey(whereColumn, values[i])) {
|
|
|
|
|
final Iterator<byte[]> biter = index.get(whereColumn, values[i]).iterator();
|
|
|
|
|
while(biter.hasNext()) {
|
|
|
|
|
while(biter.hasNext()) {
|
|
|
|
|
set1.add(biter.next());
|
|
|
|
|
}
|
|
|
|
|
if(i==0) {
|
|
|
|
@ -192,12 +192,12 @@ public class Tables implements Iterable<String> {
|
|
|
|
|
set2.retainAll(set1);
|
|
|
|
|
}
|
|
|
|
|
set1.clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for(byte[] pk : set2) {
|
|
|
|
|
rows.add(this.select(table, pk));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Log.logException(e);
|
|
|
|
|
return new HashSet<Row>().iterator();
|
|
|
|
@ -214,10 +214,10 @@ public class Tables implements Iterable<String> {
|
|
|
|
|
patternBuilder.append(p3);
|
|
|
|
|
patternBuilder.append(values.length);
|
|
|
|
|
patternBuilder.append('}');
|
|
|
|
|
final Pattern p = Pattern.compile(patternBuilder.toString(), Pattern.CASE_INSENSITIVE);
|
|
|
|
|
final Pattern p = Pattern.compile(patternBuilder.toString(), Pattern.CASE_INSENSITIVE);
|
|
|
|
|
try {
|
|
|
|
|
return this.iterator(table, whereColumn, p);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
Log.logException(e);
|
|
|
|
|
return new HashSet<Row>().iterator();
|
|
|
|
|
}
|
|
|
|
|