public override void Insert(Record record)
 {
     Cache.WritePageData(record);
     Buffer.InsertEntry(new LeafEntry(record.BoundingBox, record.Address));
     if (buffer.NeedFlush)
         FlushBuffer();
 }
 public override void Insert(Record record)
 {
     Cache.WritePageData(record);
     OverflowMarkers = new List<Int32>();
     LeafEntry newEntry = new LeafEntry(record.BoundingBox, record.Address);
     Insert(newEntry, TreeHeight);
 }
 public virtual void Delete(Record record)
 {
     Leaf leafWithRecord = FindLeaf(record, Cache.LookupNode(Root));
     if (leafWithRecord == null)
         return;
     LeafEntry entryToRemove = null;
     foreach (LeafEntry entry in leafWithRecord.NodeEntries)
         if (entry.Child.Equals(record.Address))
             entryToRemove = entry;
     leafWithRecord.RemoveNodeEntry(entryToRemove);
     if (leafWithRecord.NodeEntries.Count >= Constants.MINIMUM_ENTRIES_PER_NODE)
         Cache.WritePageData(leafWithRecord);
     CondenseTree(leafWithRecord);
     Node rootNode = Cache.LookupNode(Root);
     if (rootNode.NodeEntries.Count == 1)
     {
         Node newRoot = Cache.LookupNode(rootNode.NodeEntries[0].Child);
         newRoot.Parent = Address.Empty;
         Root = newRoot.Address;
         Cache.DeletePageData(rootNode);
         Cache.WritePageData(newRoot);
     }
     Cache.DeletePageData(record);
 }
 protected virtual PageData LookupPageData(Address address)
 {
     PageData data;
     Page page = LookupPage(address);
     Byte type = page.Data[0], childType = page.Data[1];
     if (type == (Byte)PageDataType.Node || type == (Byte)PageDataType.Leaf)
         if (childType == (Byte)NodeChildType.Leaf)
             data = new Node(address, typeof(Leaf), page.Data);
         else if (childType == (Byte)NodeChildType.Node)
             data = new Node(address, typeof(Node), page.Data);
         else if (childType == (Byte)NodeChildType.Record)
             data = new Leaf(address, page.Data);
         else
             throw new Exception("Not a node");
     else if (type == (Byte)PageDataType.Record)
         data = new Record(address, page.Data);
     else if (type == (Byte)PageDataType.IndexUnitSector)
         data = new Sector(address, page.Data);
     else
         throw new Exception("Not valid Page Data");
     CacheOverflowCheck();
     return data;
 }
 protected override Leaf ChooseLeaf(Record record)
 {
     LeafEntry entry = new LeafEntry(record.BoundingBox, record.Address);
     return ChooseNode(entry, TreeHeight) as Leaf;
 }
 public override void Delete(Record record)
 {
     buffer.DeleteEntry(new LeafEntry(record.BoundingBox, record.Address));
     if (buffer.NeedFlush)
         FlushBuffer();
 }
 public virtual void Insert(Record record)
 {
     Cache.WritePageData(record);
     Leaf leafToInsertInto = ChooseLeaf(record);
     Insert(record, leafToInsertInto);
     if (leafToInsertInto.NodeEntries.Count > Constants.MAXIMUM_ENTRIES_PER_NODE)
     {
         List<Node> splitNodes = Split(leafToInsertInto);
         RemoveFromParent(leafToInsertInto);
         AdjustTree(splitNodes[0] as Leaf, splitNodes[1] as Leaf);
     }
     else
         AdjustTree(leafToInsertInto);
 }
 protected virtual Single GetFutureSize(Record record, MinimumBoundingBox area)
 {
     return GetFutureSize(record.BoundingBox, area);
 }
 protected virtual void Insert(Record record, Leaf leaf)
 {
     leaf.AddNodeEntry(new LeafEntry(record.BoundingBox, record.Address));
 }
 protected virtual Leaf FindLeaf(Record record, Node node)
 {
     if (node is Leaf)
     {
         foreach (LeafEntry entry in node.NodeEntries)
             if (entry.Child.Equals(record.Address))
                 return node as Leaf;
     }
     else
         foreach (NodeEntry entry in node.NodeEntries)
             if (Overlaps(entry.MinimumBoundingBox, record.BoundingBox))
             {
                 Leaf leaf = FindLeaf(record, Cache.LookupNode(entry.Child));
                 if (leaf != null)
                     return leaf;
             }
     return null;
 }
 protected virtual Leaf ChooseLeaf(Record record)
 {
     Node node = Cache.LookupNode(Root);
     while (!(node is Leaf))
     {
         NodeEntry minEnlargment = node.NodeEntries[0];
         Single minEnlargedArea = GetFutureSize(record, minEnlargment.MinimumBoundingBox) - minEnlargment.MinimumBoundingBox.GetArea();
         foreach (NodeEntry nodeEntry in node.NodeEntries)
         {
             Single enlargment = GetFutureSize(record, nodeEntry.MinimumBoundingBox) - nodeEntry.MinimumBoundingBox.GetArea();
             if ((enlargment == minEnlargedArea && nodeEntry.MinimumBoundingBox.GetArea() < minEnlargment.MinimumBoundingBox.GetArea()) ||
                 enlargment < minEnlargedArea)
             {
                 minEnlargedArea = enlargment;
                 minEnlargment = nodeEntry;
             }
         }
         node = Cache.LookupNode(minEnlargment.Child);
     }
     return node as Leaf;
 }
 public virtual void Update(Record originalRecord, Record newRecord)
 {
     Delete(originalRecord);
     Insert(newRecord);
 }
        public virtual void BuildIndex()
        {
            SelectTreeType();

            StreamReader reader = new StreamReader(DataSetFileLocation);
            while (!reader.EndOfStream)
            {
                String[] values = reader.ReadLine().Split(new char[] { '\t', ' ' }, StringSplitOptions.RemoveEmptyEntries);
                Int32 recordId = Int32.Parse(values[0]);
                if (recordId % 2500 == 0)
                    Console.WriteLine(recordId.ToString());
                Single x = Single.Parse(values[1]), y = Single.Parse(values[2]);
                Record record = new Record(recordId, new MinimumBoundingBox(x, y, x, y));
                if (record.Address.ToString().Equals("18336"))
                {
                    Console.WriteLine("Here it is");
                    Console.ReadLine();
                }
                TreeIndex.Insert(record);
            }
            reader.Close();
            TreeIndex.SaveIndex(IndexSaveFileLocation, CacheSaveFileLocation, MemorySaveFileLocation);
        }
        public void ExecuteQueryPlan()
        {
            List<Record> pooledResults = new List<Record>(1000);
            Int32 headIndex = 0;
            StreamReader reader = new StreamReader(QueryPlanFileLocation);
            StreamWriter qwriter = new StreamWriter(ResultSaveFileLocation + ".q");
            StreamWriter iwriter = new StreamWriter(ResultSaveFileLocation + ".i");
            StreamWriter dwriter = new StreamWriter(ResultSaveFileLocation + ".d");
            StreamWriter uwriter = new StreamWriter(ResultSaveFileLocation + ".u");
            Console.WriteLine("Loading Complete");
            Cache.FlushCache();
            while (!reader.EndOfStream)
            {
                PerformanceAnalyzer.ClearStatistics();
                String[] values = reader.ReadLine().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                Int32 queryNumber = Int32.Parse(values[0]);
                if (queryNumber % 500 == 0)
                    Console.WriteLine(queryNumber.ToString());
                if (queryNumber == 5000)
                    break;
                Char queryType = Char.Parse(values[1]);
                if (queryType == 'Q')
                {
                    //writer.WriteLine();
                    Query query;
                    Char searchType = Char.Parse(values[2]);
                    switch (searchType)
                    {
                        case 'R':
                            {
                                Single x = Single.Parse(values[3]), y = Single.Parse(values[4]), r = Single.Parse(values[5]);
                                query = new RangeQuery(x, y, r*2);
                                //writer.WriteLine("Traditional Range Search");
                                //writer.WriteLine("Point: ({0:F2},{1:F2})", x, y);
                                //writer.WriteLine("Range: {0:F2}", r);
                                break;
                            }
                        case 'W':
                            {
                                Single x = Single.Parse(values[3]), y = Single.Parse(values[4]), r = Single.Parse(values[5]);
                                query = new WindowQuery(x - r, y - r, x+r, y+r);
                                //writer.WriteLine("Traditional Window Search");
                                //writer.WriteLine("Lower Point: ({0:F2},{1:F2})", x - r, y - r);
                                //writer.WriteLine("Upper Point: ({0:F2},{1:F2})", x, y);
                                break;
                            }
                        case 'K':
                            {
                                Single x = Single.Parse(values[3]), y = Single.Parse(values[4]);
                                Int32 k = Int32.Parse(values[5]);
                                query = new KNearestNeighborQuery(k, x, y);
                                //writer.WriteLine("Traditional Nearest Neighbor Search");
                                //writer.WriteLine("Point: ({0:F2},{1:F2})", x, y);
                                //writer.WriteLine("Items: {0}", k);
                                break;
                            }
                        default: continue;
                    }
                    PerformanceAnalyzer.OperationBegan();
                    List<Record> results = TreeIndex.Search(query);
                    Cache.FlushCache();
                    while (results.Contains(null))
                        results.Remove(null);
                    PerformanceAnalyzer.OperationEnded();
                    //writer.WriteLine("Result Objects:");
                    foreach (Record result in results)
                    {
                        if (pooledResults.Count >= 1000)
                            pooledResults.RemoveAt(0);
                        pooledResults.Add(result);
                        //writer.WriteLine("{0} {1:F2} {2:F2}", result.RecordID + 1000000 - 1, result.BoundingBox.MinX, result.BoundingBox.MinY);
                    }
                    //writer.WriteLine("Complementary Objects:");
                    //writer.WriteLine("Statistics");
                    //writer.WriteLine("Page Loads:\t{0}\tPage Writes:\t{1}\tExecution Time:\t{2}\tCPU Time:\t{3}", PerformanceAnalyzer.PageFaults, PerformanceAnalyzer.PageWrites, PerformanceAnalyzer.ExecutionTime.Ticks, PerformanceAnalyzer.CPUTime.Ticks);
                    qwriter.WriteLine("{0}\t{1}\t{2}\t{3}", PerformanceAnalyzer.PageFaults, PerformanceAnalyzer.PageWrites, PerformanceAnalyzer.ExecutionTime.Ticks, PerformanceAnalyzer.CPUTime.Ticks);
                }
                else if (queryType == 'I')
                {
                    Int32 recordID = Int32.Parse(values[2]);
                    Single x = Single.Parse(values[3]), y = Single.Parse(values[4]);
                    Record recordToInsert = new Record(recordID, new MinimumBoundingBox(x, y, x, y));
                    PerformanceAnalyzer.OperationBegan();
                    TreeIndex.Insert(recordToInsert);
                    Cache.FlushCache();
                    PerformanceAnalyzer.OperationEnded();
                    //writer.WriteLine();
                    //writer.WriteLine("Inserting new Record");
                    //writer.WriteLine("{0}, {1:F2} {2:F2}", recordID + 1000000 - 1, x, y);
                    //writer.WriteLine("Statistics");
                    //writer.WriteLine("Page Loads:\t{0}\tPage Writes:\t{1}\tExecution Time:\t{2}\tCPU Time:\t{3}", PerformanceAnalyzer.PageFaults, PerformanceAnalyzer.PageWrites, PerformanceAnalyzer.ExecutionTime.Ticks, PerformanceAnalyzer.CPUTime.Ticks);
                    iwriter.WriteLine("{0}\t{1}\t{2}\t{3}", PerformanceAnalyzer.PageFaults, PerformanceAnalyzer.PageWrites, PerformanceAnalyzer.ExecutionTime.Ticks, PerformanceAnalyzer.CPUTime.Ticks);

                }
                else if (queryType == 'U')
                {
                    /*Int32 recordIndex = Int32.Parse(values[2]);
                    if (pooledResults.Count == 0)
                        continue;
                    while (recordIndex >= pooledResults.Count)
                        recordIndex -= pooledResults.Count;
                    Record recordToUpdate = pooledResults[recordIndex];
                    while (pooledResults.Count > 0 && recordIndex-- > 0)
                        pooledResults.RemoveAt(0);
                    for(int i = 0; i < pooledResults.Count; i++)
                        if(pooledResults[i].RecordID == recordToUpdate.RecordID)
                            pooledResults.RemoveAt(i--);
                    Single x = Single.Parse(values[3]), y = Single.Parse(values[4]);
                    Record updatedRecord = new Record(recordToUpdate.RecordID, new MinimumBoundingBox(x, y, x, y));
                    if (recordToUpdate.Address.ToString().Equals("18336"))
                    {
                        Console.WriteLine("Updated");
                        Console.ReadLine();
                    }
                    PerformanceAnalyzer.OperationBegan();

                    TreeIndex.Update(recordToUpdate, updatedRecord);
                    Cache.FlushCache();
                    PerformanceAnalyzer.OperationEnded();
                    //writer.WriteLine();
                    //writer.WriteLine("Updating Record");
                    //writer.WriteLine("From: {0}, {1:F2} {2:F2}", recordToUpdate.RecordID + 1000000 - 1, recordToUpdate.BoundingBox.MinX, recordToUpdate.BoundingBox.MinY);
                    //writer.WriteLine("To: {0}, {1:F2} {2:F2}", updatedRecord.RecordID + 1000000 - 1, x, y);
                    //writer.WriteLine("Statistics");
                    //writer.WriteLine("Page Loads:\t{0}\tPage Writes:\t{1}\tExecution Time:\t{2}\tCPU Time:\t{3}", PerformanceAnalyzer.PageFaults, PerformanceAnalyzer.PageWrites, PerformanceAnalyzer.ExecutionTime.Ticks, PerformanceAnalyzer.CPUTime.Ticks);
                    uwriter.WriteLine("{0}\t{1}\t{2}\t{3}", PerformanceAnalyzer.PageFaults, PerformanceAnalyzer.PageWrites, PerformanceAnalyzer.ExecutionTime.Ticks, PerformanceAnalyzer.CPUTime.Ticks);
               */ }
                else if (queryType == 'D')
                {
                   /* Int32 recordIndex = Int32.Parse(values[2]);
                    if (pooledResults.Count == 0)
                        continue;
                    while (recordIndex >= pooledResults.Count)
                        recordIndex -= pooledResults.Count;
                    Record recordToDelete = pooledResults[recordIndex];
                    while (pooledResults.Count > 0 && recordIndex-- > 0)
                        pooledResults.RemoveAt(0);
                    for (int i = 0; i < pooledResults.Count; i++)
                        if (pooledResults[i].RecordID == recordToDelete.RecordID)
                            pooledResults.RemoveAt(i--);
                    if (recordToDelete.Address.ToString().Equals("18336"))
                    {
                        Console.WriteLine("Deleted");
                        Console.ReadLine();
                    }
                    PerformanceAnalyzer.OperationBegan();
                    TreeIndex.Delete(recordToDelete);
                    Cache.FlushCache();
                    PerformanceAnalyzer.OperationEnded();
                    //writer.WriteLine();
                    //writer.WriteLine("Deleted Record");
                    //writer.WriteLine("{0}, {1:F2} {2:F2}", recordToDelete.RecordID + 1000000 - 1, recordToDelete.BoundingBox.MinX, recordToDelete.BoundingBox.MinY);
                    //writer.WriteLine("Statistics");
                    //writer.WriteLine("Page Loads:\t{0}\tPage Writes:\t{1}\tExecution Time:\t{2}\tCPU Time:\t{3}", PerformanceAnalyzer.PageFaults, PerformanceAnalyzer.PageWrites, PerformanceAnalyzer.ExecutionTime.Ticks, PerformanceAnalyzer.CPUTime.Ticks);
                    dwriter.WriteLine("{0}\t{1}\t{2}\t{3}", PerformanceAnalyzer.PageFaults, PerformanceAnalyzer.PageWrites, PerformanceAnalyzer.ExecutionTime.Ticks, PerformanceAnalyzer.CPUTime.Ticks);
               */ }
            }
            reader.Close();
            qwriter.Close();
            iwriter.Close();
            dwriter.Close();
            uwriter.Close();
        }