package com.metamx.collections.spatial;

import com.google.common.base.Preconditions;
import com.google.common.primitives.Ints;
import com.metamx.collections.bitmap.BitmapFactory;
import com.metamx.collections.bitmap.ImmutableBitmap;
import com.metamx.collections.spatial.search.Bound;
import com.metamx.collections.spatial.search.GutmanSearchStrategy;
import com.metamx.collections.spatial.search.SearchStrategy;

import java.nio.ByteBuffer;

/**
 * An immutable representation of an {@link RTree} for spatial indexing.
 */
public class ImmutableRTree
{
  private static byte VERSION = 0x0;

  public static ImmutableRTree newImmutableFromMutable(RTree rTree)
  {
    if (rTree.getSize() == 0) {
      return new ImmutableRTree();
    }

    ByteBuffer buffer = ByteBuffer.wrap(new byte[calcNumBytes(rTree)]);

    buffer.put(VERSION);
    buffer.putInt(rTree.getNumDims());
    rTree.getRoot().storeInByteBuffer(buffer, buffer.position());
    buffer.position(0);
    return new ImmutableRTree(buffer.asReadOnlyBuffer(), rTree.getBitmapFactory());
  }

  private static int calcNumBytes(RTree tree)
  {
    int total = 1 + Ints.BYTES; // VERSION and numDims

    total += calcNodeBytes(tree.getRoot());

    return total;
  }

  private static int calcNodeBytes(Node node)
  {
    int total = 0;

    // find size of this node
    total += node.getSizeInBytes();

    // recursively find sizes of child nodes
    for (Node child : node.getChildren()) {
      if (node.isLeaf()) {
        total += child.getSizeInBytes();
      } else {
        total += calcNodeBytes(child);
      }
    }

    return total;
  }

  private final int numDims;
  private final ImmutableNode root;
  private final ByteBuffer data;

  private final SearchStrategy defaultSearchStrategy = new GutmanSearchStrategy();

  public ImmutableRTree()
  {
    this.numDims = 0;
    this.data = ByteBuffer.wrap(new byte[]{});
    this.root = null;
  }

  public ImmutableRTree(ByteBuffer data, BitmapFactory bitmapFactory)
  {
    final int initPosition = data.position();
    Preconditions.checkArgument(data.get(0) == VERSION, "Mismatching versions");
    this.numDims = data.getInt(1 + initPosition) & 0x7FFF;
    this.data = data;
    this.root = new ImmutableNode(numDims, initPosition, 1 + Ints.BYTES, data, bitmapFactory);
  }

  public int size()
  {
    return data.capacity();
  }

  public ImmutableNode getRoot()
  {
    return root;
  }

  public int getNumDims()
  {
    return numDims;
  }

  public Iterable<ImmutableBitmap> search(Bound bound)
  {
    Preconditions.checkArgument(bound.getNumDims() == numDims);

    return defaultSearchStrategy.search(root, bound);
  }

  public Iterable<ImmutableBitmap> search(SearchStrategy strategy, Bound bound)
  {
    Preconditions.checkArgument(bound.getNumDims() == numDims);

    return strategy.search(root, bound);
  }

  public byte[] toBytes()
  {
    ByteBuffer buf = ByteBuffer.allocate(data.capacity());
    buf.put(data.asReadOnlyBuffer());
    return buf.array();
  }

  public int compareTo(ImmutableRTree other)
  {
    return this.data.compareTo(other.data);
  }
}

@@ -79,16 +79,64 @@
 itions;%0A
+import com.google.common.collect.ImmutableList;%0A
 import c
@@ -2585,80 +2585,22 @@
     
-P
 re
-conditions.checkArgument(bound.getNumDims() == numDims);%0A%0A    return 
+turn search(
 defa
@@ -2612,36 +2612,24 @@
 archStrategy
-.search(root
 , bound);%0A  
@@ -2723,35 +2723,10 @@
     
-Preconditions.checkArgument
+if
 (bou
@@ -2752,19 +2752,21 @@
 numDims)
-;%0A%0A
+ %7B%0A  
     retu
@@ -2790,32 +2790,211 @@
 h(root, bound);%0A
+    %7Delse%7B%0A      // If the dimension counts don't match (for example, if this is called on a blank %60new ImmutableRTree()%60)%0A      return ImmutableList.%3CImmutableBitmap%3Eof();%0A    %7D%0A
   %7D%0A%0A  public by

Change ImmutableRTree to return an empty set of Bittmaps on dimension mismatch