use of de.lmu.ifi.dbs.elki.database.ids.DBID in project elki by elki-project.
the class BundleWriter method writeBundleStream.
/**
* Write a bundle stream to a file output channel.
*
* @param source Data source
* @param output Output channel
* @throws IOException on IO errors
*/
public void writeBundleStream(BundleStreamSource source, WritableByteChannel output) throws IOException {
ByteBuffer buffer = ByteBuffer.allocateDirect(INITIAL_BUFFER);
DBIDVar var = DBIDUtil.newVar();
ByteBufferSerializer<?>[] serializers = null;
loop: while (true) {
BundleStreamSource.Event ev = source.nextEvent();
switch(ev) {
case NEXT_OBJECT:
if (serializers == null) {
serializers = writeHeader(source, buffer, output);
}
if (serializers[0] != null) {
if (!source.assignDBID(var)) {
throw new AbortException("An object did not have an DBID assigned.");
}
DBID id = DBIDUtil.deref(var);
@SuppressWarnings("unchecked") ByteBufferSerializer<DBID> ser = (ByteBufferSerializer<DBID>) serializers[0];
int size = ser.getByteSize(id);
buffer = ensureBuffer(size, buffer, output);
ser.toByteBuffer(buffer, id);
}
for (int i = 1, j = 0; i < serializers.length; ++i, ++j) {
@SuppressWarnings("unchecked") ByteBufferSerializer<Object> ser = (ByteBufferSerializer<Object>) serializers[i];
int size = ser.getByteSize(source.data(j));
buffer = ensureBuffer(size, buffer, output);
ser.toByteBuffer(buffer, source.data(j));
}
// switch
break;
case META_CHANGED:
if (serializers != null) {
throw new AbortException("Meta changes are not supported, once the block header has been written.");
}
// switch
break;
case END_OF_STREAM:
break loop;
default:
LOG.warning("Unknown bundle stream event. API inconsistent? " + ev);
// switch
break;
}
}
if (buffer.position() > 0) {
flushBuffer(buffer, output);
}
}
use of de.lmu.ifi.dbs.elki.database.ids.DBID in project elki by elki-project.
the class TrivialDBIDFactory method generateSingleDBID.
@Override
public DBID generateSingleDBID() {
final int id = next.getAndIncrement();
if (id == Integer.MAX_VALUE) {
throw new AbortException("DBID allocation error - too many objects allocated!");
}
DBID ret = new IntegerDBID(id);
return ret;
}
use of de.lmu.ifi.dbs.elki.database.ids.DBID in project elki by elki-project.
the class MetricalIndexKNNQuery method getKNNForObject.
@Override
public KNNList getKNNForObject(O q, int k) {
if (k < 1) {
throw new IllegalArgumentException("At least one object has to be requested!");
}
index.statistics.countKNNQuery();
KNNHeap knnList = DBIDUtil.newHeap(k);
double d_k = Double.POSITIVE_INFINITY;
final ComparableMinHeap<MTreeSearchCandidate> pq = new ComparableMinHeap<>();
// Push the root node
pq.add(new MTreeSearchCandidate(0., index.getRootID(), null, 0.));
// search in tree
while (!pq.isEmpty()) {
MTreeSearchCandidate pqNode = pq.poll();
if (knnList.size() >= k && pqNode.mindist > d_k) {
break;
}
AbstractMTreeNode<?, ?, ?> node = index.getNode(pqNode.nodeID);
DBID id_p = pqNode.routingObjectID;
double d1 = pqNode.routingDistance;
// directory node
if (!node.isLeaf()) {
for (int i = 0; i < node.getNumEntries(); i++) {
MTreeEntry entry = node.getEntry(i);
DBID o_r = entry.getRoutingObjectID();
double r_or = entry.getCoveringRadius();
double d2 = id_p != null ? entry.getParentDistance() : 0.;
double diff = Math.abs(d1 - d2);
double sum = d_k + r_or;
if (diff <= sum) {
double d3 = distanceQuery.distance(o_r, q);
index.statistics.countDistanceCalculation();
double d_min = Math.max(d3 - r_or, 0.);
if (d_min <= d_k) {
pq.add(new MTreeSearchCandidate(d_min, ((DirectoryEntry) entry).getPageID(), o_r, d3));
}
}
}
} else // data node
{
for (int i = 0; i < node.getNumEntries(); i++) {
MTreeEntry entry = node.getEntry(i);
DBID o_j = entry.getRoutingObjectID();
double d2 = id_p != null ? entry.getParentDistance() : 0.;
double diff = Math.abs(d1 - d2);
if (diff <= d_k) {
double d3 = distanceQuery.distance(o_j, q);
index.statistics.countDistanceCalculation();
if (d3 <= d_k) {
knnList.insert(d3, o_j);
d_k = knnList.getKNNDistance();
}
}
}
}
}
return knnList.toKNNList();
}
use of de.lmu.ifi.dbs.elki.database.ids.DBID in project elki by elki-project.
the class MkTabTreeIndex method initialize.
@Override
public void initialize() {
super.initialize();
List<MkTabEntry> objs = new ArrayList<>(relation.size());
for (DBIDIter iter = relation.iterDBIDs(); iter.valid(); iter.advance()) {
// FIXME: expensive
DBID id = DBIDUtil.deref(iter);
final O object = relation.get(id);
objs.add(createNewLeafEntry(id, object, Double.NaN));
}
insertAll(objs);
}
use of de.lmu.ifi.dbs.elki.database.ids.DBID in project elki by elki-project.
the class MTreeIndex method insertAll.
@Override
public void insertAll(DBIDs ids) {
List<MTreeEntry> objs = new ArrayList<>(ids.size());
for (DBIDIter iter = ids.iter(); iter.valid(); iter.advance()) {
DBID id = DBIDUtil.deref(iter);
final O object = relation.get(id);
objs.add(createNewLeafEntry(id, object, Double.NaN));
}
insertAll(objs);
}
Aggregations