adopted kelondroBytesIntMap to kelondroIntBytesMap

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@3734 6c8d7289-2bf4-0310-a012-ef5d649a1542
pull/1/head
orbiter 18 years ago
parent 5551ff5306
commit 3c5ff7f735

@ -67,13 +67,11 @@ public class kelondroBytesIntMap {
//assert consistencyAnalysis0() : "consistency problem: " + consistencyAnalysis(); //assert consistencyAnalysis0() : "consistency problem: " + consistencyAnalysis();
if (indexentry != null) return (int) indexentry.getColLong(1); if (indexentry != null) return (int) indexentry.getColLong(1);
} }
if (index1 != null) { assert (index1 != null);
kelondroRow.Entry indexentry = index1.get(key); kelondroRow.Entry indexentry = index1.get(key);
//assert consistencyAnalysis0() : "consistency problem: " + consistencyAnalysis(); //assert consistencyAnalysis0() : "consistency problem: " + consistencyAnalysis();
if (indexentry != null) return (int) indexentry.getColLong(1); if (indexentry == null) return -1;
} return (int) indexentry.getColLong(1);
//assert consistencyAnalysis0() : "consistency problem: " + consistencyAnalysis();
return -1;
} }
public synchronized int puti(byte[] key, int i) throws IOException { public synchronized int puti(byte[] key, int i) throws IOException {
@ -213,19 +211,24 @@ public class kelondroBytesIntMap {
//assert consistencyAnalysis0() : "consistency problem: " + consistencyAnalysis(); //assert consistencyAnalysis0() : "consistency problem: " + consistencyAnalysis();
return index0.rows(up, firstKey); return index0.rows(up, firstKey);
} }
if ((index0 == null) && (index1 != null)) { assert (index1 != null);
if (index0 == null) {
//assert consistencyAnalysis0() : "consistency problem: " + consistencyAnalysis(); //assert consistencyAnalysis0() : "consistency problem: " + consistencyAnalysis();
return index1.rows(up, firstKey); return index1.rows(up, firstKey);
} }
assert ((index0 != null) && (index1 != null));
//assert consistencyAnalysis0() : "consistency problem: " + consistencyAnalysis(); //assert consistencyAnalysis0() : "consistency problem: " + consistencyAnalysis();
return new kelondroMergeIterator(index0.rows(up, firstKey), index1.rows(up, firstKey), rowdef.objectOrder, kelondroMergeIterator.simpleMerge, true); return new kelondroMergeIterator(
index0.rows(up, firstKey),
index1.rows(up, firstKey),
rowdef.objectOrder,
kelondroMergeIterator.simpleMerge,
true);
} }
public kelondroProfile profile() { public kelondroProfile profile() {
if (index0 != null) return index0.profile(); if (index0 == null) return index1.profile();
if (index1 != null) return index1.profile(); if (index1 == null) return index0.profile();
return null; return kelondroProfile.consolidate(index0.profile(), index1.profile());
} }
public synchronized void close() { public synchronized void close() {

@ -24,6 +24,7 @@
package de.anomic.kelondro; package de.anomic.kelondro;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
@ -38,81 +39,158 @@ public class kelondroIntBytesMap {
// to index0 with addb, and a runtime-phase where data can only be written // to index0 with addb, and a runtime-phase where data can only be written
// to index1 with putb. // to index1 with putb.
private kelondroRowSet index0, index1;
private kelondroRow rowdef; private kelondroRow rowdef;
private boolean initPhase; private kelondroRowSet index0, index1;
public kelondroIntBytesMap(int payloadSize, int initSize) { public kelondroIntBytesMap(int payloadSize, int initSize) {
rowdef = new kelondroRow("Cardinal key-4 {b256}, byte[] payload-" + payloadSize, kelondroNaturalOrder.naturalOrder, 0); rowdef = new kelondroRow("Cardinal key-4 {b256}, byte[] payload-" + payloadSize, kelondroNaturalOrder.naturalOrder, 0);
index0 = new kelondroRowSet(rowdef, initSize); index0 = new kelondroRowSet(rowdef, initSize);
index1 = new kelondroRowSet(rowdef, 0); index1 = null;
initPhase = true;
}
public int size() {
return index0.size() + index1.size();
} }
public long memoryNeededForGrow() { public long memoryNeededForGrow() {
if (index1 == null)
return index0.memoryNeededForGrow();
else
return index1.memoryNeededForGrow(); return index1.memoryNeededForGrow();
} }
public kelondroRow row() throws IOException {
return index0.row();
}
public byte[] getb(int ii) { public byte[] getb(int ii) {
assert ii >= 0 : "i = " + ii;
byte[] key = kelondroNaturalOrder.encodeLong((long) ii, 4); byte[] key = kelondroNaturalOrder.encodeLong((long) ii, 4);
if (index0 != null) {
if (index1 == null) {
// finish initialization phase
index0.sort();
index0.uniq(10000);
index1 = new kelondroRowSet(rowdef, 0);
}
kelondroRow.Entry indexentry = index0.get(key); kelondroRow.Entry indexentry = index0.get(key);
if (indexentry == null) indexentry = index1.get(key); if (indexentry != null) return indexentry.getColBytes(1);
}
assert (index1 != null);
kelondroRow.Entry indexentry = index1.get(key);
if (indexentry == null) return null; if (indexentry == null) return null;
return indexentry.getColBytes(1); return indexentry.getColBytes(1);
} }
public void addb(int ii, byte[] value) { public byte[] putb(int ii, byte[] value) {
assert initPhase; assert ii >= 0 : "i = " + ii;
kelondroRow.Entry newentry = index0.row().newEntry(); assert value != null;
newentry.setCol(0, (long) ii); byte[] key = kelondroNaturalOrder.encodeLong((long) ii, 4);
newentry.setCol(1, value); if (index0 != null) {
index0.addUnique(newentry); if (index1 == null) {
// finish initialization phase
index0.sort();
index0.uniq(10000);
index1 = new kelondroRowSet(rowdef, 0);
} }
kelondroRow.Entry indexentry = index0.get(key);
if (indexentry != null) {
byte[] oldv = indexentry.getColBytes(1);
indexentry.setCol(0, key);
indexentry.setCol(1, value);
index0.put(indexentry);
return oldv;
}
// else place it in the index1
}
// at this point index1 cannot be null
assert (index1 != null);
public byte[] putb(int ii, byte[] value) {
initPhase = false;
kelondroRow.Entry newentry = rowdef.newEntry(); kelondroRow.Entry newentry = rowdef.newEntry();
newentry.setCol(0, (long) ii); newentry.setCol(0, (long) ii);
newentry.setCol(1, value); newentry.setCol(1, value);
kelondroRow.Entry indexentry = index0.get(kelondroNaturalOrder.encodeLong((long) ii, 4));
if (indexentry != null) {
index0.put(newentry);
return indexentry.getColBytes(1);
}
kelondroRow.Entry oldentry = index1.put(newentry); kelondroRow.Entry oldentry = index1.put(newentry);
if (oldentry == null) return null; if (oldentry == null) return null;
return oldentry.getColBytes(1); return oldentry.getColBytes(1);
} }
public void addb(int ii, byte[] value) {
assert index1 == null; // valid only in init-phase
assert ii >= 0 : "i = " + ii;
assert value != null;
kelondroRow.Entry newentry = index0.row().newEntry();
newentry.setCol(0, (long) ii);
newentry.setCol(1, value);
index0.addUnique(newentry);
}
public byte[] removeb(int ii) { public byte[] removeb(int ii) {
if ((index0.size() == 0) && (index1.size() == 0)) return null; assert ii >= 0 : "i = " + ii;
byte[] key = kelondroNaturalOrder.encodeLong((long) ii, 4); byte[] key = kelondroNaturalOrder.encodeLong((long) ii, 4);
if (index0 != null) {
if (index1 == null) {
// finish initialization phase
index0.sort();
index0.uniq(10000);
index1 = new kelondroRowSet(rowdef, 0);
}
kelondroRow.Entry indexentry = index0.remove(key); kelondroRow.Entry indexentry = index0.remove(key);
if (indexentry != null) { if (indexentry != null) {
return indexentry.getColBytes(1); return indexentry.getColBytes(1);
} }
indexentry = index1.remove(key); // else remove it from the index1
}
// at this point index1 cannot be null
assert (index1 != null);
if (index1.size() == 0) return null;
kelondroRow.Entry indexentry = index1.remove(key);
if (indexentry == null) return null; if (indexentry == null) return null;
return indexentry.getColBytes(1); return indexentry.getColBytes(1);
} }
public byte[] removeoneb() { public byte[] removeoneb() {
if ((index0.size() == 0) && (index1.size() == 0)) return null; if ((index1 != null) && (index1.size() != 0)) {
if (index1.size() == 0) { kelondroRow.Entry indexentry = index1.removeOne();
kelondroRow.Entry indexentry = index0.removeOne(); assert (indexentry != null);
if (indexentry == null) return null; if (indexentry == null) return null;
//assert consistencyAnalysis0() : "consistency problem: " + consistencyAnalysis();
return indexentry.getColBytes(1); return indexentry.getColBytes(1);
} }
kelondroRow.Entry indexentry = index1.removeOne(); if ((index0 != null) && (index0.size() != 0)) {
kelondroRow.Entry indexentry = index0.removeOne();
assert (indexentry != null);
if (indexentry == null) return null; if (indexentry == null) return null;
//assert consistencyAnalysis0() : "consistency problem: " + consistencyAnalysis();
return indexentry.getColBytes(1); return indexentry.getColBytes(1);
} }
return null;
}
public int size() {
if ((index0 != null) && (index1 == null)) {
//assert consistencyAnalysis0() : "consistency problem: " + consistencyAnalysis();
return index0.size();
}
if ((index0 == null) && (index1 != null)) {
//assert consistencyAnalysis0() : "consistency problem: " + consistencyAnalysis();
return index1.size();
}
assert ((index0 != null) && (index1 != null));
//assert consistencyAnalysis0() : "consistency problem: " + consistencyAnalysis();
return index0.size() + index1.size();
}
public Iterator rows() { public Iterator rows() {
if (index0 != null) {
if (index1 == null) {
// finish initialization phase
index0.sort();
index0.uniq(10000);
index1 = new kelondroRowSet(rowdef, 0);
}
return index0.rows(true, null);
}
assert (index1 != null);
if (index0 == null) {
return index1.rows(true, null);
}
return new kelondroMergeIterator( return new kelondroMergeIterator(
index0.rows(true, null), index0.rows(true, null),
index1.rows(true, null), index1.rows(true, null),
@ -122,13 +200,19 @@ public class kelondroIntBytesMap {
} }
public void flush() { public void flush() {
if (index0 != null) {
index0.sort(); index0.sort();
index0.trim(true); index0.trim(true);
}
if (index1 != null) {
index1.sort(); index1.sort();
index1.trim(true); index1.trim(true);
} }
}
public kelondroProfile profile() { public kelondroProfile profile() {
if (index0 == null) return index1.profile();
if (index1 == null) return index0.profile();
return kelondroProfile.consolidate(index0.profile(), index1.profile()); return kelondroProfile.consolidate(index0.profile(), index1.profile());
} }

Loading…
Cancel
Save