Skip to content

Commit

Permalink
内存版rocksrtree
Browse files Browse the repository at this point in the history
  • Loading branch information
codingmiao committed Mar 28, 2023
1 parent a174da7 commit 559674f
Show file tree
Hide file tree
Showing 10 changed files with 246 additions and 113 deletions.
11 changes: 11 additions & 0 deletions giscat-vector/giscat-vector-rocksrtree/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@
<artifactId>giscat-vector-util</artifactId>
<groupId>org.wowtools</groupId>
</dependency>

<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -52,38 +52,38 @@ final class Branch extends Node {

private int size;

Branch(final TreeBuilder builder, String id) {
super(id);
public Branch(TreeBuilder builder, String id) {
super(builder, id);
this.builder = builder;
this.child = new String[builder.mMax];
}


protected static Branch fromBytes(TreeBuilder builder, String id, byte[] bytes) {
@Override
public void fill(byte[] bytes) {
RocksRtreePb.BranchPb branchPb;
try {
branchPb = RocksRtreePb.BranchPb.parseFrom(bytes);
} catch (InvalidProtocolBufferException e) {
throw new RuntimeException(e);
}
Branch branch = new Branch(builder, id);
List<String> childIdsList = branchPb.getChildIdsList();
if (childIdsList.size() > 0) {
int i = 0;
for (String l : childIdsList) {
branch.child[i] = l;
child[i] = l;
i++;
}
branch.size = childIdsList.size();
size = childIdsList.size();
}
if (branchPb.hasMbr()) {
branch.mbr = new RectNd(branchPb.getMbr());
mbr = new RectNd(branchPb.getMbr());
}
return branch;
}



@Override
public byte[] toBytes() {
protected byte[] toBytes() {
RocksRtreePb.BranchPb.Builder branchBuilder = RocksRtreePb.BranchPb.newBuilder();
if (null != child) {
List<String> list = new ArrayList<>(size);
Expand Down Expand Up @@ -152,7 +152,7 @@ Node add(final RectNd t, TreeTransaction tx) {
if (getChild(i, tx).getBound().contains(tRect)) {
child[i] = getChild(i, tx).add(t, tx).id;
mbr = mbr.getMbr(getChild(i, tx).getBound());
tx.put(id, toBytes());
tx.put(id, this);
return this;
}
}
Expand All @@ -161,14 +161,14 @@ Node add(final RectNd t, TreeTransaction tx) {
nextLeaf.add(t, tx);
final int nextChild = addChild(nextLeaf);
mbr = mbr.getMbr(getChild(nextChild, tx).getBound());
tx.put(id, toBytes());
tx.put(id, this);
return this;

} else {
final int bestLeaf = chooseLeaf(t, tRect, tx);
child[bestLeaf] = getChild(bestLeaf, tx).add(t, tx).id;
mbr = mbr.getMbr(getChild(bestLeaf, tx).getBound());
tx.put(id, toBytes());
tx.put(id, this);
return this;
}
}
Expand All @@ -192,11 +192,11 @@ Node remove(final RectNd t, TreeTransaction tx) {
}

if (size == 0) {
tx.put(id, toBytes());
tx.put(id, this);
return null;
} else if (size == 1) {
// unsplit branch
tx.put(id, toBytes());
tx.put(id, this);
Node c = getChild(0, tx);
return c;
}
Expand All @@ -205,7 +205,7 @@ Node remove(final RectNd t, TreeTransaction tx) {
for (int i = 1; i < size; i++) {
mbr = mbr.getMbr(getChild(i, tx).getBound());
}
tx.put(id, toBytes());
tx.put(id, this);
return this;
}

Expand All @@ -223,7 +223,7 @@ Node update(final RectNd told, final RectNd tnew, TreeTransaction tx) {
mbr = mbr.getMbr(getChild(i, tx).getBound());
}
}
tx.put(id, toBytes());
tx.put(id, this);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,47 +64,46 @@ final class Leaf extends Node {

protected int size;

protected Leaf(final TreeBuilder builder, String id) {
super(id);
public Leaf(final TreeBuilder builder, String id) {
super(builder, id);
this.builder = builder;
this.entryRects = new RectNd[builder.mMax];
this.entry = new RectNd[builder.mMax];
}

protected static Leaf fromBytes(TreeBuilder builder, String id, byte[] bytes) {
@Override
public void fill(byte[] bytes) {
RocksRtreePb.LeafPb leafPb;
try {
leafPb = RocksRtreePb.LeafPb.parseFrom(bytes);
} catch (InvalidProtocolBufferException e) {
throw new RuntimeException(e);
}
Leaf leaf = new Leaf(builder, id);

if (leafPb.hasMbr()) {
leaf.mbr = new RectNd(leafPb.getMbr());
mbr = new RectNd(leafPb.getMbr());
}

List<RocksRtreePb.RectNdPb> entryRectPbs = leafPb.getEntryRectsList();
if (entryRectPbs.size() > 0) {
int i = 0;
for (RocksRtreePb.RectNdPb entryRectPb : entryRectPbs) {
leaf.entryRects[i] = new RectNd(entryRectPb);
leaf.entry[i] = leaf.entryRects[i];
entryRects[i] = new RectNd(entryRectPb);
entry[i] = entryRects[i];
i++;
}
leaf.size = entryRectPbs.size();
size = entryRectPbs.size();

i = 0;
byte[] fcBytes = leafPb.getEntries().toByteArray();
FeatureCollection fc = ProtoFeatureConverter.proto2featureCollection(fcBytes, gf);
for (Feature feature : fc.getFeatures()) {
leaf.entry[i].feature = feature;
entry[i].feature = feature;
i++;
}

}

return leaf;
}

@Override
Expand Down Expand Up @@ -149,10 +148,10 @@ Node add(final RectNd t, TreeTransaction tx) {
size++;
} else {
Node sp = split(t, tx);
tx.put(id, toBytes());
tx.put(id, this);
return sp;
}
tx.put(id, toBytes());
tx.put(id, this);
return this;
}

Expand Down Expand Up @@ -348,7 +347,7 @@ public int compare(final RectNd o1, final RectNd o2) {
pNode.addChild(l1Node);
pNode.addChild(l2Node);

tx.put(pNode.id, pNode.toBytes());
tx.put(pNode.id, pNode);
return pNode;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,10 @@
/**
* Created by jcairns on 4/30/15.
*/
abstract class Node implements ProtoAble{
abstract class Node extends ProtoAble{

protected final String id;

public Node(String id) {
this.id = id;
public Node(TreeBuilder builder, String id) {
super(builder, id);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,59 @@

package org.wowtools.giscat.vector.rocksrtree;

import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* 可以转为proto对象的接口标识
*
* @author liuyu
* @date 2023/3/28
*/
interface ProtoAble {
byte[] toBytes();
abstract class ProtoAble {

protected final TreeBuilder builder;
protected final String id;

private static final Map<Class<? extends ProtoAble>, Constructor<? extends ProtoAble>> implConstructors;

static {
List<Class<? extends ProtoAble>> impls = List.of(
Branch.class,
Leaf.class
);
try {
Map<Class<? extends ProtoAble>, Constructor<? extends ProtoAble>> constructors = new HashMap<>();
for (Class<? extends ProtoAble> impl : impls) {
Constructor<? extends ProtoAble> constructor = impl.getConstructor(TreeBuilder.class, String.class);
constructors.put(impl, constructor);
}
implConstructors = Map.copyOf(constructors);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

public ProtoAble(TreeBuilder builder, String id) {
this.builder = builder;
this.id = id;
}

public abstract void fill(byte[] bytes);

protected abstract byte[] toBytes();

public static <T extends ProtoAble> T fromBytes(Class<T> t, TreeBuilder builder, String id, byte[] bytes) {
Constructor<T> constructor = (Constructor<T>) implConstructors.get(t);
T instance;
try {
instance = constructor.newInstance(builder, id);
} catch (Exception e) {
throw new RuntimeException(e);
}
instance.fill(bytes);
return instance;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@
* #L%
*/

import com.google.protobuf.InvalidProtocolBufferException;
import org.rocksdb.RocksDB;
import org.rocksdb.RocksDBException;
import org.wowtools.giscat.vector.pojo.Feature;

import java.nio.charset.StandardCharsets;
import java.util.function.Consumer;


Expand All @@ -44,14 +48,17 @@
* <p>
* Created by jcairns on 4/30/15.</p>
*/
public final class RTree {
public final class RTree{

public static final byte[] TreeDbKey = "T".getBytes(StandardCharsets.UTF_8);

private static final double EPSILON = 1e-12;

private final TreeBuilder builder;
protected String root = null;

private String root = null;
private final TreeBuilder builder;

public RTree(final TreeBuilder builder) {
protected RTree(TreeBuilder builder) {
this.builder = builder;
}

Expand Down Expand Up @@ -187,4 +194,12 @@ Node getRoot(TreeTransaction tx) {
return builder.getNode(root, tx);
}


protected byte[] toBytes() {
RocksRtreePb.RTreePb.Builder rtreeBuilder = RocksRtreePb.RTreePb.newBuilder();
rtreeBuilder.setMMax(builder.mMax);
rtreeBuilder.setMMin(builder.mMin);
rtreeBuilder.setRootId(root);
return rtreeBuilder.build().toByteArray();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@

import org.wowtools.giscat.vector.pojo.Feature;

import java.util.List;
import java.util.Objects;

/**
* An N dimensional rectangle or "hypercube" that is a representation of a data entry.
* <p>
Expand All @@ -55,14 +52,15 @@ protected RocksRtreePb.RectNdPb.Builder toBuilder() {
return builder;
}

public boolean featureEquals(RectNd other,TreeBuilder builder) {
if (feature == null) {
throw new RuntimeException("feature为空,不符合逻辑");
}
if (other.feature == null) {
throw new RuntimeException("feature为空,不符合逻辑");
}
return Objects.equals(builder.getFeatureKey(feature), builder.getFeatureKey(other.feature));
public boolean featureEquals(RectNd other, TreeBuilder builder) {
return false;
// if (feature == null) {
// throw new RuntimeException("feature为空,不符合逻辑");
// }
// if (other.feature == null) {
// throw new RuntimeException("feature为空,不符合逻辑");
// }
// return Objects.equals(builder.getFeatureKey(feature), builder.getFeatureKey(other.feature));
}

protected RectNd(PointNd min, PointNd max) {
Expand Down
Loading

0 comments on commit 559674f

Please sign in to comment.