From bcf35085ed33732335560f7d72f37e04b8af33df Mon Sep 17 00:00:00 2001 From: sk89q Date: Fri, 16 Nov 2012 13:36:26 -0800 Subject: [PATCH] Indices now have a separate reindex() method because large changes to the index have to be supported. Therefore, indices are no longer thread-safe. They don't need to be. --- .../worldguard/region/indices/FlatIndex.java | 5 +++++ .../region/indices/PriorityRTreeIndex.java | 13 ++++++++----- .../region/indices/RegionIndex.java | 19 +++++++++++++++++-- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/sk89q/worldguard/region/indices/FlatIndex.java b/src/main/java/com/sk89q/worldguard/region/indices/FlatIndex.java index d180fc39..ecd8e5a3 100644 --- a/src/main/java/com/sk89q/worldguard/region/indices/FlatIndex.java +++ b/src/main/java/com/sk89q/worldguard/region/indices/FlatIndex.java @@ -102,4 +102,9 @@ public synchronized Collection queryOverlapping( public synchronized int size() { return regions.size(); } + + @Override + public void reindex() { + // Whoo, nothing to do, because this index is so simple + } } diff --git a/src/main/java/com/sk89q/worldguard/region/indices/PriorityRTreeIndex.java b/src/main/java/com/sk89q/worldguard/region/indices/PriorityRTreeIndex.java index f46fce0c..aacde882 100644 --- a/src/main/java/com/sk89q/worldguard/region/indices/PriorityRTreeIndex.java +++ b/src/main/java/com/sk89q/worldguard/region/indices/PriorityRTreeIndex.java @@ -21,6 +21,7 @@ import com.sk89q.worldedit.Vector; import com.sk89q.worldguard.region.Region; import com.sk89q.worldguard.region.UnsupportedIntersectionException; +import com.sk89q.worldguard.region.shapes.IndexableShape; import com.sk89q.worldguard.region.shapes.ShapeMBRConverter; import org.apache.commons.lang.Validate; @@ -59,8 +60,6 @@ public synchronized void add(Region... region) { for (Region r : region) { regions.put(normalizeId(r.getId()), r); } - tree = new PRTree(converter, branchFactor); - tree.load(regions.values()); } @Override @@ -69,8 +68,6 @@ public synchronized void remove(String... id) { for (String i : id) { regions.remove(normalizeId(i)); } - tree = new PRTree(converter, branchFactor); - tree.load(regions.values()); } @Override @@ -98,7 +95,7 @@ public synchronized Collection queryContains( location.getY(), location.getZ(), location.getZ()); for (Region region : tree.find(pointMBR)) { - if (region.contains(location) && !result.contains(region)) { + if (region.getShape().contains(location) && !result.contains(region)) { result.add(region); } } @@ -129,4 +126,10 @@ public synchronized int size() { return regions.size(); } + @Override + public void reindex() { + tree = new PRTree(converter, branchFactor); + tree.load(regions.values()); + } + } \ No newline at end of file diff --git a/src/main/java/com/sk89q/worldguard/region/indices/RegionIndex.java b/src/main/java/com/sk89q/worldguard/region/indices/RegionIndex.java index bcca6b9c..f43b2b60 100644 --- a/src/main/java/com/sk89q/worldguard/region/indices/RegionIndex.java +++ b/src/main/java/com/sk89q/worldguard/region/indices/RegionIndex.java @@ -35,8 +35,8 @@ * using the method {@link Region#shouldCache()}. *

* Region IDs are case in-sensitive and implementations must be aware of this when - * querying by ID. Any casing can be used when looking up an ID. Implementations must - * be thread-safe. + * querying by ID. Any casing can be used when looking up an ID. Implementations do NOT + * have to be thread-safe. */ public interface RegionIndex { @@ -100,6 +100,9 @@ Collection queryOverlapping( * Add the given region to this index. If a region already known by this index is * attempted to be added to this index, nothing will happen. Parents of the * given region will not be added automatically. + *

+ * After making changes, {@link #reindex()} MUST be called in order to ensure + * maximum performance and index consistency. * * @param region the region to add */ @@ -109,6 +112,9 @@ Collection queryOverlapping( * Remove the region with the given ID from this index. If the region being removed * has children, they will not be removed automatically. Their parent will also * not be set to null automatically. + *

+ * After making changes, {@link #reindex()} MUST be called in order to ensure + * maximum performance and index consistency. * * @param id the ID of the region to remove */ @@ -118,6 +124,9 @@ Collection queryOverlapping( * Remove a region from this index having the exact same ID, but possibly no * other equal attribute. If the region being removed as children, they will not be * removed automatically. Their parent will also not be set to null automatically. + *

+ * After making changes, {@link #reindex()} MUST be called in order to ensure + * maximum performance and index consistency. * * @param region the region with the ID to match against */ @@ -176,4 +185,10 @@ Collection queryOverlapping( */ int size(); + /** + * After making a series of changes, this method should be called in order to + * perform any re-indexing activities. + */ + void reindex(); + }