Ejemplo n.º 1
0
        public object Add(PatriciaTrieKey key, object obj)
#endif
        {
            Modify();
            count += 1;

            if (firstBit(key.mask, key.length) == 1)
            {
                if (rootOne != null)
                {
                    return(rootOne.add(key.mask, key.length, obj));
                }
                else
                {
                    rootOne = new PTrieNode(key.mask, key.length, obj);
                    return(null);
                }
            }
            else
            {
                if (rootZero != null)
                {
                    return(rootZero.add(key.mask, key.length, obj));
                }
                else
                {
                    rootZero = new PTrieNode(key.mask, key.length, obj);
                    return(null);
                }
            }
        }
    static void loadIpCountryTable(Root root, string fileName)
    {
        FileStream   fs = new FileStream(fileName, FileMode.Open);
        StreamReader sr = new StreamReader(fs);
        string       line;

        while ((line = sr.ReadLine()) != null)
        {
            int sep1 = line.IndexOf('|');
            if (sep1 >= 0)
            {
                int sep2 = line.IndexOf('|', sep1 + 1);
                int sep3 = line.IndexOf('|', sep2 + 1);
                int sep4 = line.IndexOf('|', sep3 + 1);
                if (sep2 > sep1 && sep4 > sep3)
                {
                    String iso = line.Substring(sep1 + 1, sep2 - sep1 - 1).ToUpper();
                    String ip  = line.Substring(sep3 + 1, sep4 - sep3 - 1);
                    if (ip.IndexOf('.') > 0 && iso.Length == 2)
                    {
                        Country c = (Country)root.countries[iso];
                        if (c == null)
                        {
                            Console.WriteLine("Unknown country code: " + iso);
                        }
                        else
                        {
                            root.trie.Add(PatriciaTrieKey.FromIpAddress(ip), c);
                        }
                    }
                }
            }
        }
    }
Ejemplo n.º 3
0
        public IPersistent Add(PatriciaTrieKey key, IPersistent obj)
        {
            Modify();
            count += 1;

            if (firstDigit(key.mask, key.length) == 1)
            {
                if (rootOne != null)
                {
                    return(rootOne.add(key.mask, key.length, obj));
                }
                else
                {
                    rootOne = new PTrieNode(key.mask, key.length, obj);
                    return(null);
                }
            }
            else
            {
                if (rootZero != null)
                {
                    return(rootZero.add(key.mask, key.length, obj));
                }
                else
                {
                    rootZero = new PTrieNode(key.mask, key.length, obj);
                    return(null);
                }
            }
        }
Ejemplo n.º 4
0
    public static void Main(string[] args)
    {
        IDatabase db = DatabaseFactory.CreateDatabase();

        db.Open("ipcountry.dbs", PagePoolSize);
        Root root = (Root)db.Root;

        if (root == null)
        {
            root           = new Root();
            root.countries = db.CreateIndex <string, Country>(IndexType.Unique);
            root.trie      = db.CreatePatriciaTrie <Country>();
            loadCountries(root.countries);
            db.Root = root;
        }
        for (int i = 0; i < args.Length; i++)
        {
            loadIpCountryTable(root, args[i]);
        }

        string ip;

        while ((ip = Console.ReadLine()) != null)
        {
            Country country = root.trie.FindBestMatch(PatriciaTrieKey.FromIpAddress(ip));
            if (country != null)
            {
                Console.WriteLine(ip + "->" + country.name);
            }
        }
        db.Close();
    }
Ejemplo n.º 5
0
 public IPersistent FindExactMatch(PatriciaTrieKey key)
 {
     if (firstDigit(key.mask, key.length) == 1)
     {
         if (rootOne != null)
         {
             return(rootOne.findExactMatch(key.mask, key.length));
         }
     }
     else
     {
         if (rootZero != null)
         {
             return(rootZero.findExactMatch(key.mask, key.length));
         }
     }
     return(null);
 }
Ejemplo n.º 6
0
        public object FindExactMatch(PatriciaTrieKey key)
#endif
        {
            if (firstBit(key.mask, key.length) == 1)
            {
                if (rootOne != null)
                {
                    return(rootOne.findExactMatch(key.mask, key.length));
                }
            }
            else
            {
                if (rootZero != null)
                {
                    return(rootZero.findExactMatch(key.mask, key.length));
                }
            }
            return(null);
        }
Ejemplo n.º 7
0
        public object Remove(PatriciaTrieKey key)
        {
            object obj;
#endif
            if (firstBit(key.mask, key.length) == 1)
            {
                if (rootOne != null)
                {
                    obj = rootOne.remove(key.mask, key.length);
                    if (obj != null)
                    {
                        Modify();
                        count -= 1;
                        if (rootOne.isNotUsed())
                        {
                            rootOne.Deallocate();
                            rootOne = null;
                        }
                        return(obj);
                    }
                }
            }
            else
            {
                if (rootZero != null)
                {
                    obj = rootZero.remove(key.mask, key.length);
                    if (obj != null)
                    {
                        Modify();
                        count -= 1;
                        if (rootZero.isNotUsed())
                        {
                            rootZero.Deallocate();
                            rootZero = null;
                        }
                        return(obj);
                    }
                }
            }
            return(null);
        }
    public static void Main(string[] args)
    {
        Storage db = StorageFactory.Instance.CreateStorage();

        db.Open("ipcountry.dbs", PagePoolSize);
        Root root = (Root)db.Root;

        if (root == null)
        {
            root = new Root();
#if USE_GENERICS
            root.countries = db.CreateIndex <string, Country>(true);
            root.trie      = db.CreatePatriciaTrie <Country>();
#else
            root.countries = db.CreateIndex(typeof(string), true);
            root.trie      = db.CreatePatriciaTrie();
#endif
            loadCountries(root.countries);
            db.Root = root;
        }
        for (int i = 0; i < args.Length; i++)
        {
            loadIpCountryTable(root, args[i]);
        }

        string ip;
        while ((ip = Console.ReadLine()) != null)
        {
#if USE_GENERICS
            Country country = root.trie.FindBestMatch(PatriciaTrieKey.FromIpAddress(ip));
#else
            Country country = (Country)root.trie.FindBestMatch(PatriciaTrieKey.FromIpAddress(ip));
#endif
            if (country != null)
            {
                Console.WriteLine(ip + "->" + country.name);
            }
        }
        db.Close();
    }
Ejemplo n.º 9
0
 public T Add(PatriciaTrieKey key, T obj)
Ejemplo n.º 10
0
 public T Remove(PatriciaTrieKey key)
 {
     T obj;
Ejemplo n.º 11
0
 public T FindExactMatch(PatriciaTrieKey key)
Ejemplo n.º 12
0
 public T FindBestMatch(PatriciaTrieKey key)