Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

GUID Tree Data Structure

, 14 Feb 2013
Data structure designed to store billions of items using GUID as a key.
GuidArrayTree.zip
GuidArrayTree
GuidArrayTree.suo
GuidArrayTree
Properties
GuidArrayTree_UnitTests
Properties
Local.testsettings
TraceAndTestImpact.testsettings
TrevyLibrary.vsmdi
// Author: Trevy
//
namespace TrevyCo.Tools
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections;
    using System.Runtime.Serialization;

    /// <summary>
    /// It uses computer memory to store data for memory usage under 1 MG
    /// Above that, it switches to using computer storage as a medium
    /// </summary>
    public class GuidCollection<T>
    {
        #region Data
        private GuidTreeBase<T> guidTree;

        public long Count
        {
            get
            {
                return count;
            }
        }
        private long count = 0; 
        #endregion

        #region Constructors
        /// <summary>
        /// Uses computer memory exclusively.
        /// </summary>
        /// <remarks>
        /// Can slow computer if too many entries added
        /// </remarks>
        public GuidCollection(bool overwriteValues = false)
        {
            guidTree = new GuidArrayTree<T>();
            guidTree.OverwriteValues = overwriteValues;
        }

        /// <summary>
        /// Uses computer memory to store data for memory usage under 1 MG
        /// Above that, it switches to using computer storage as a medium
        /// </summary>
        /// <param name="scratchArea">Area to store data.</param>
        /// <remarks>
        /// WARNING: Choose a secure are with sufficient memory to store all the data you need to add
        /// </remarks>
        public GuidCollection(string scratchArea, bool overwriteValues = false)
        {
            Attribute attribute = Attribute.GetCustomAttribute(typeof(T), typeof(System.SerializableAttribute));
            if (attribute == null)
            {
                throw new ApplicationException(typeof(T).Name + " must have the System.Serializable Attribute, since we are storing the value in the file system.");
            }

            guidTree = new GuidFileTree<T>(scratchArea);
            guidTree.OverwriteValues = overwriteValues;
        } 
        #endregion

        #region Public methods
        /// <summary>
        /// Add key, value pair to dictionary
        /// </summary>
        public void Add(Guid key, T val)
        {
            if (guidTree.Add(key, val))
            {
                count++;
            }
        }

        /// <summary>
        /// Set weather we can overwrite existing values
        /// </summary>
        public bool OverwriteValues
        {
            get
            {
                return guidTree.OverwriteValues;
            }
            set
            {
                guidTree.OverwriteValues = value;
            }
        }

        /// <summary>
        /// Index into collection
        /// </summary>
        /// <param name="key">GUID</param>
        /// <returns>the value</returns>
        public T this[Guid key]
        {
            get
            {
                T val;
                if (TryGetValue(key, out val))
                {
                    return val;
                }
                else
                {
                    throw new InvalidOperationException("Key doesn't exist");
                }
            }
            set
            {
                Add(key, value);
            }
        }

        /// <summary>
        /// Try to get the value
        /// </summary>
        /// <returns>True if vlue exists, false othervise</returns>
        public bool TryGetValue(Guid key, out T val)
        {
            return guidTree.TryGetValue(key, out val);
        }

        /// <summary>
        /// Can be time-consuming
        /// </summary>
        public void Clear()
        {
            guidTree.Clear();
            count = 0;
        }

        /// <summary>
        /// Remove item corresponding to the key
        /// </summary>
        /// <returns>true if value removed</returns>
        public bool Remove(Guid key)
        {
            bool res = guidTree.Remove(key);
            if (res)
            {
                count--;
            }

            return res;
        }

        /// <summary>
        /// Check if key exists
        /// </summary>
        /// <returns>true if key exists, false otherwise</returns>
        public bool ContainsKey(Guid key)
        {
            return guidTree.ContainsKey(key);
        }

        /// <summary>
        /// Gets the enummerator
        /// </summary>
        /// <returns>Enumerator</returns>
        public IEnumerable<T> GetEnumerator()
        {
            return guidTree.GetEnumerator();
        } 
        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

TrevyBurgess
Software Developer
United States United States
I’ve been a software developer for the last 11 years.
My language of choice is C#.
My strength lies in windows based programming as an SDET.
 
Strengths include:
• Object Oriented Programming
• Design Patterns
• Data Structures, Algorithms
• Windows Automation
 
Principle technologies include:
• Visual Studios 2010 (Ultimate, TFS, Test Manager)
• C# 4.0, XML, WPF, SQL Server
 
Education includes:
• BSEE - Concordia University, Quebec, Canada
• Programmer Analyst Certificate - John Abbott College, Qc, Canada
• Certified Scrum Master Training - Danube, Bellevue, WA
• Windows Azure Training - Wintellect, Bellevue, WA
 
Certification:
• Microsoft Certified Solution Developer for the MS.NET Platform
Follow on   Twitter

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 14 Feb 2013
Article Copyright 2013 by TrevyBurgess
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid