/// <summary>
        /// Gets the next available Database Triple ID if the Triple is not in the Database of the existing Database Triple ID
        /// </summary>
        /// <param name="t">Triple to get an ID for</param>
        /// <param name="createRequired">Whether the Manager needs to create a Triple Record in the Database</param>
        /// <returns></returns>
        protected virtual int GetNextTripleID(Triple t, out bool createRequired)
        {
            int id = -1;
            createRequired = false;

            try
            {
                Monitor.Enter(this._tripleIDs);
                if (this._nextTripleID == -1)
                {
                    this.LoadTripleIDMap();
                }

                //Use the Triple Collection to record Triples we've requested IDs for
                //This allows us to take advantage of the Triple Collections ability to detect
                //when Triple Hash Codes collide and then we can use this in the next step to
                //do a Database lookup if necessary
                if (!this._tripleCollection.Contains(t))
                {
                    this._tripleCollection.Add(t);
                }

                if (this._tripleIDs.ContainsKey(t.GetHashCode()))
                {
                    id = this._tripleIDs[t.GetHashCode()];

                    if (id == -1 || t.Collides)
                    {
                        //There are multiple Triples with this Hash Code
                        //Lookup from Database
                        String s, p, o;
                        s = this.SaveNode(t.Subject);
                        p = this.SaveNode(t.Predicate);
                        o = this.SaveNode(t.Object);
                        String getTripleID = "SELECT tripleID FROM TRIPLES WHERE tripleSubject=" + s + " AND triplePredicate=" + p + " AND tripleObject=" + o;
                        Object tripleID = this.ExecuteScalar(getTripleID);
                        if (tripleID != null)
                        {
                            return Int32.Parse(tripleID.ToString());
                        }
                        else
                        {
                            id = ++this._nextTripleID;
                            createRequired = true;
                        }
                    }
                }
                else
                {
                    id = ++this._nextTripleID;
                    this._tripleIDs.Add(t.GetHashCode(), id);
                    createRequired = true;
                }
            }
            finally
            {
                Monitor.Exit(this._tripleIDs);
            }
            if (id != -1)
            {
                return id;
            }
            else
            {
                throw new RdfStorageException("Error obtaining a Triple ID");
            }
        }
Esempio n. 2
0
        public static void Main(string[] args)
        {
            StreamWriter output = new StreamWriter("HashCodeTests.txt");
            Console.SetOut(output);

            try
            {
                Console.WriteLine("## Hash Code Tests");
                Console.WriteLine("Tests that Literal and URI Nodes produce different Hashes");
                Console.WriteLine();

                //Create the Nodes
                Graph g = new Graph();
                IUriNode u = g.CreateUriNode(new Uri("http://www.google.com"));
                ILiteralNode l = g.CreateLiteralNode("http://www.google.com/");

                Console.WriteLine("Created a URI and Literal Node both referring to 'http://www.google.com'");
                Console.WriteLine("String form of URI Node is:");
                Console.WriteLine(u.ToString());
                Console.WriteLine("String form of Literal Node is:");
                Console.WriteLine(l.ToString());
                Console.WriteLine("Hash Code of URI Node is " + u.GetHashCode());
                Console.WriteLine("Hash Code of Literal Node is " + l.GetHashCode());
                Console.WriteLine("Hash Codes are Equal? " + u.GetHashCode().Equals(l.GetHashCode()));
                Console.WriteLine("Nodes are equal? " + u.Equals(l));

                //Create Triples
                IBlankNode b = g.CreateBlankNode();
                IUriNode type = g.CreateUriNode("rdf:type");
                Triple t1, t2;
                t1 = new Triple(b, type, u);
                t2 = new Triple(b, type, l);

                Console.WriteLine();
                Console.WriteLine("Created two Triples stating a Blank Node has rdf:type of the Nodes created earlier");
                Console.WriteLine("String form of Triple 1 (using URI Node) is:");
                Console.WriteLine(t1.ToString());
                Console.WriteLine("String form of Triple 2 (using Literal Node) is:");
                Console.WriteLine(t2.ToString());
                Console.WriteLine("Hash Code of Triple 1 is " + t1.GetHashCode());
                Console.WriteLine("Hash Code of Triple 2 is " + t2.GetHashCode());
                Console.WriteLine("Hash Codes are Equal? " + t1.GetHashCode().Equals(t2.GetHashCode()));
                Console.WriteLine("Triples are Equal? " + t1.Equals(t2));

                //Now going to look at the Hash Code collisions from the dotNetRDF Store
                Console.WriteLine();
                Console.WriteLine("Examing the Hash Code Collisions from one of our SQL Store test data sets");

//                MicrosoftSqlStoreManager manager = new MicrosoftSqlStoreManager("localhost", "bbcone", "example", "password");
//                DataTable collisions = manager.ExecuteQuery(@"SELECT * FROM TRIPLES WHERE tripleHash IN 
//	(
//	SELECT tripleHash FROM TRIPLES GROUP BY tripleHash HAVING COUNT(tripleID)>1
//	) ORDER BY tripleHash");

//                foreach (DataRow r in collisions.Rows)
//                {
//                    String s, p, o;
//                    int hash;
//                    s = r["tripleSubject"].ToString();
//                    p = r["triplePredicate"].ToString();
//                    o = r["tripleObject"].ToString();
//                    hash = Int32.Parse(r["tripleHash"].ToString());

//                    INode subj = manager.LoadNode(g, s);
//                    INode pred = manager.LoadNode(g, p);
//                    INode obj = manager.LoadNode(g, o);

//                    Triple t = new Triple(subj, pred, obj);

//                    Console.WriteLine("Subject (ID " + s + "): " + subj.ToString() + " (Hash " + subj.GetHashCode() + ")");
//                    Console.WriteLine("Predicate (ID " + p + "): " + pred.ToString() + " (Hash " + pred.GetHashCode() + ")");
//                    Console.WriteLine("Object (ID " + o + "): " + obj.ToString() + " (Hash " + obj.GetHashCode() + ")");
//                    Console.WriteLine(t.ToString());
//                    Console.WriteLine("Triple Hash " + t.GetHashCode());
//                    Console.WriteLine("Triple Hash Code Construct " + subj.GetHashCode().ToString() + pred.GetHashCode().ToString() + obj.GetHashCode().ToString());
//                    Console.WriteLine("Triple Hash in Store " + hash);
//                    Console.WriteLine();
//                }

//                manager.Dispose();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
            finally
            {
                output.Close();
            }
        }
 protected override string GetIndexNameForTriple(Triple t)
 {
     return @"index\spo\" + this.GetHash(t.GetHashCode().ToString());
 }
Esempio n. 4
0
        public void NodesHashCodes()
        {
            Console.WriteLine("Tests that Literal and URI Nodes produce different Hashes");
            Console.WriteLine();

            //Create the Nodes
            Graph g = new Graph();
            IUriNode u = g.CreateUriNode(new Uri("http://www.google.com"));
            ILiteralNode l = g.CreateLiteralNode("http://www.google.com/");

            Console.WriteLine("Created a URI and Literal Node both referring to 'http://www.google.com'");
            Console.WriteLine("String form of URI Node is:");
            Console.WriteLine(u.ToString());
            Console.WriteLine("String form of Literal Node is:");
            Console.WriteLine(l.ToString());
            Console.WriteLine("Hash Code of URI Node is " + u.GetHashCode());
            Console.WriteLine("Hash Code of Literal Node is " + l.GetHashCode());
            Console.WriteLine("Hash Codes are Equal? " + u.GetHashCode().Equals(l.GetHashCode()));
            Console.WriteLine("Nodes are equal? " + u.Equals(l));

            Assert.AreNotEqual(u.GetHashCode(), l.GetHashCode());
            Assert.AreNotEqual(u, l);

            //Create some plain and typed literals which may have colliding Hash Codes
            ILiteralNode plain = g.CreateLiteralNode("test^^http://example.org/type");
            ILiteralNode typed = g.CreateLiteralNode("test", new Uri("http://example.org/type"));

            Console.WriteLine();
            Console.WriteLine("Created a Plain and Typed Literal where the String representations are identical");
            Console.WriteLine("Plain Literal String form is:");
            Console.WriteLine(plain.ToString());
            Console.WriteLine("Typed Literal String from is:");
            Console.WriteLine(typed.ToString());
            Console.WriteLine("Hash Code of Plain Literal is " + plain.GetHashCode());
            Console.WriteLine("Hash Code of Typed Literal is " + typed.GetHashCode());
            Console.WriteLine("Hash Codes are Equal? " + plain.GetHashCode().Equals(typed.GetHashCode()));
            Console.WriteLine("Nodes are equal? " + plain.Equals(typed));

            Assert.AreNotEqual(plain.GetHashCode(), typed.GetHashCode());
            Assert.AreNotEqual(plain, typed);

            //Create Triples
            IBlankNode b = g.CreateBlankNode();
            IUriNode type = g.CreateUriNode("rdf:type");
            Triple t1, t2;
            t1 = new Triple(b, type, u);
            t2 = new Triple(b, type, l);

            Console.WriteLine();
            Console.WriteLine("Created two Triples stating a Blank Node has rdf:type of the URI Nodes created earlier");
            Console.WriteLine("String form of Triple 1 (using URI Node) is:");
            Console.WriteLine(t1.ToString());
            Console.WriteLine("String form of Triple 2 (using Literal Node) is:");
            Console.WriteLine(t2.ToString());
            Console.WriteLine("Hash Code of Triple 1 is " + t1.GetHashCode());
            Console.WriteLine("Hash Code of Triple 2 is " + t2.GetHashCode());
            Console.WriteLine("Hash Codes are Equal? " + t1.GetHashCode().Equals(t2.GetHashCode()));
            Console.WriteLine("Triples are Equal? " + t1.Equals(t2));

            Assert.AreNotEqual(t1.GetHashCode(), t2.GetHashCode());
            Assert.AreNotEqual(t1, t2);

            //Create Triples from the earlier Literal Nodes
            t1 = new Triple(b, type, plain);
            t2 = new Triple(b, type, typed);

            Console.WriteLine();
            Console.WriteLine("Created two Triples stating a Blank Node has rdf:type of the Literal Nodes created earlier");
            Console.WriteLine("String form of Triple 1 (using Plain Literal) is:");
            Console.WriteLine(t1.ToString());
            Console.WriteLine("String form of Triple 2 (using Typed Literal) is:");
            Console.WriteLine(t2.ToString());
            Console.WriteLine("Hash Code of Triple 1 is " + t1.GetHashCode());
            Console.WriteLine("Hash Code of Triple 2 is " + t2.GetHashCode());
            Console.WriteLine("Hash Codes are Equal? " + t1.GetHashCode().Equals(t2.GetHashCode()));
            Console.WriteLine("Triples are Equal? " + t1.Equals(t2));

            Assert.AreNotEqual(t1.GetHashCode(), t2.GetHashCode());
            Assert.AreNotEqual(t1, t2);

        }
        /// <summary>
        /// Removes a Triple from a Graphs Database record
        /// </summary>
        /// <param name="t">Triple to remove</param>
        /// <param name="graphID">Database Graph ID</param>
        public override void RemoveTriple(Triple t, string graphID)
        {
            //If the Triple being removed is in the Buffer to be added still remove it
            lock (this._writerBuffer)
            {
                this._writerBuffer.Remove(new BatchTriple(t, graphID));
            }

            //Does the Triple have a known Database ID
            if (this._tripleIDs.ContainsKey(t.GetHashCode()))
            {
                //Can use the existing ID
                int id = this._tripleIDs[t.GetHashCode()];
                String removeTriple = "DELETE FROM GRAPH_TRIPLES WHERE graphID=" + graphID + " AND tripleID=" + id;
                try
                {
                    this.Open(false);
                    this.ExecuteNonQuery(removeTriple);
                    this.Close(false);
                }
                catch (SqlException sqlEx)
                {
                    this.Close(true, true);
                    throw new RdfStorageException("Unable to unlink a Triple record to a Graph in the Database", sqlEx);
                }

            }
            else
            {
                //Get the IDs for the Nodes
                String s, p, o;
                s = this.SaveNode(t.Subject);
                p = this.SaveNode(t.Predicate);
                o = this.SaveNode(t.Object);

                //Does the Triple exist in the Database
                String getTriple = "SELECT tripleID FROM TRIPLES WHERE tripleHash=" + t.GetHashCode();
                Object id = this.ExecuteScalar(getTriple);
                if (id == null)
                {
                    //Nothing to do since the Triple isn't in the Database and thus can't be linked to the Graph for unlinking to be needed
                }
                else
                {
                    //Remove the link to the Triple
                    String removeTriple = "DELETE FROM GRAPH_TRIPLES WHERE graphID=" + graphID + " AND tripleID=" + id.ToString();
                    try
                    {
                        this.Open(false);
                        this.ExecuteNonQuery(removeTriple);
                        this.Close(false);
                    }
                    catch (SqlException sqlEx)
                    {
                        this.Close(true, true);
                        throw new RdfStorageException("Unable to unlink a Triple record to a Graph in the Database", sqlEx);
                    }
                }
            }
        }
        /// <summary>
        /// Loads Triples for the Graph with the given ID into the given Graph object
        /// </summary>
        /// <param name="g">Graph to load into</param>
        /// <param name="graphID">Database Graph ID</param>
        public override void LoadTriples(IGraph g, string graphID)
        {
            //Build the SQL for getting the Triples
            String getTriples = "SELECT * FROM GRAPH_TRIPLES G INNER JOIN TRIPLES T ON G.tripleID=T.tripleID WHERE G.graphID=" + graphID;

            //Get the Data
            this.Open(true);
            DataTable data = this.ExecuteQuery(getTriples);

            //Load the Triples
            String s, p, o;
            INode subj, pred, obj;
            foreach (DataRow r in data.Rows)
            {
                //Get the IDs
                s = r["tripleSubject"].ToString();
                p = r["triplePredicate"].ToString();
                o = r["tripleObject"].ToString();

                //Get the Nodes from these IDs
                subj = this.LoadNode(g, s);
                pred = this.LoadNode(g, p);
                obj = this.LoadNode(g, o);

                Triple t = new Triple(subj, pred, obj);
                g.Assert(t);

                //Store ID for later
                try
                {
                    Monitor.Enter(this._tripleIDs);
                    if (!this._tripleIDs.ContainsKey(t.GetHashCode()))
                    {
                        int id = Int32.Parse(r["tripleID"].ToString());
                        this._tripleIDs.Add(t.GetHashCode(), id);
                    }
                }
                finally
                {
                    Monitor.Exit(this._tripleIDs);
                }
            }

            //Close the Database Connection
            this.Close(true);
        }
Esempio n. 7
0
        public void NongenericComparable()
        {
            Triple <int, OddEvenComparable, string> triple1, triple2;

            triple1 = new Triple <int, OddEvenComparable, string>(4, new OddEvenComparable(7), "B");
            triple2 = new Triple <int, OddEvenComparable, string>(7, new OddEvenComparable(3), "A");
            Assert.IsTrue(triple1.CompareTo(triple2) < 0);
            Assert.IsFalse(triple1.Equals(triple2));
            Assert.IsFalse(triple1.GetHashCode() == triple2.GetHashCode());

            triple1 = new Triple <int, OddEvenComparable, string>(4, new OddEvenComparable(7), "B");
            triple2 = new Triple <int, OddEvenComparable, string>(4, new OddEvenComparable(2), "A");
            Assert.IsTrue(triple1.CompareTo(triple2) < 0);
            Assert.IsFalse(triple1.Equals(triple2));
            Assert.IsFalse(triple1.GetHashCode() == triple2.GetHashCode());

            triple1 = new Triple <int, OddEvenComparable, string>(4, new OddEvenComparable(7), "A");
            triple2 = new Triple <int, OddEvenComparable, string>(4, new OddEvenComparable(7), "A");
            Assert.IsTrue(triple1.CompareTo(triple2) == 0);
            Assert.IsTrue(triple1.Equals(triple2));
            Assert.IsTrue(triple1.GetHashCode() == triple2.GetHashCode());

            triple1 = new Triple <int, OddEvenComparable, string>(7, new OddEvenComparable(7), "B");
            triple2 = new Triple <int, OddEvenComparable, string>(4, new OddEvenComparable(2), "C");
            Assert.IsTrue(triple1.CompareTo(triple2) > 0);
            Assert.IsFalse(triple1.Equals(triple2));
            Assert.IsFalse(triple1.GetHashCode() == triple2.GetHashCode());

            triple1 = new Triple <int, OddEvenComparable, string>(0, new OddEvenComparable(8), "A");
            triple2 = new Triple <int, OddEvenComparable, string>(0, new OddEvenComparable(2), "A");
            Assert.IsTrue(triple1.CompareTo(triple2) > 0);
            Assert.IsFalse(triple1.Equals(triple2));
            Assert.IsFalse(triple1.GetHashCode() == triple2.GetHashCode());

            Triple <OddEvenComparable, int, string> triple3, triple4;

            triple3 = new Triple <OddEvenComparable, int, string>(new OddEvenComparable(7), 4, "A");
            triple4 = new Triple <OddEvenComparable, int, string>(new OddEvenComparable(3), 7, "B");
            Assert.IsTrue(triple3.CompareTo(triple4) > 0);
            Assert.IsFalse(triple3.Equals(triple4));
            Assert.IsFalse(triple3.GetHashCode() == triple4.GetHashCode());

            triple3 = new Triple <OddEvenComparable, int, string>(new OddEvenComparable(7), 4, "B");
            triple4 = new Triple <OddEvenComparable, int, string>(new OddEvenComparable(2), 4, "A");
            Assert.IsTrue(triple3.CompareTo(triple4) < 0);
            Assert.IsFalse(triple3.Equals(triple4));
            Assert.IsFalse(triple3.GetHashCode() == triple4.GetHashCode());

            triple3 = new Triple <OddEvenComparable, int, string>(new OddEvenComparable(7), 4, "C");
            triple4 = new Triple <OddEvenComparable, int, string>(new OddEvenComparable(7), 4, "C");
            Assert.IsTrue(triple3.CompareTo(triple4) == 0);
            Assert.IsTrue(triple3.Equals(triple4));
            Assert.IsTrue(triple3.GetHashCode() == triple4.GetHashCode());

            triple3 = new Triple <OddEvenComparable, int, string>(new OddEvenComparable(2), 7, "A");
            triple4 = new Triple <OddEvenComparable, int, string>(new OddEvenComparable(7), 4, "B");
            Assert.IsTrue(triple3.CompareTo(triple4) > 0);
            Assert.IsFalse(triple3.Equals(triple4));
            Assert.IsFalse(triple3.GetHashCode() == triple4.GetHashCode());

            triple3 = new Triple <OddEvenComparable, int, string>(new OddEvenComparable(8), 0, "A");
            triple4 = new Triple <OddEvenComparable, int, string>(new OddEvenComparable(2), 0, "A");
            Assert.IsTrue(triple3.CompareTo(triple4) > 0);
            Assert.IsFalse(triple3.Equals(triple4));
            Assert.IsFalse(triple3.GetHashCode() == triple4.GetHashCode());

            triple3 = new Triple <OddEvenComparable, int, string>(new OddEvenComparable(2), 4, "A");
            triple4 = new Triple <OddEvenComparable, int, string>(new OddEvenComparable(2), 3, "B");
            Assert.IsTrue(triple3.CompareTo(triple4) > 0);
            Assert.IsFalse(triple3.Equals(triple4));
            Assert.IsFalse(triple3.GetHashCode() == triple4.GetHashCode());

            Triple <int, string, OddEvenComparable> triple5, triple6;

            triple5 = new Triple <int, string, OddEvenComparable>(4, "B", new OddEvenComparable(7));
            triple6 = new Triple <int, string, OddEvenComparable>(7, "A", new OddEvenComparable(3));
            Assert.IsTrue(triple5.CompareTo(triple6) < 0);
            Assert.IsFalse(triple5.Equals(triple6));
            Assert.IsFalse(triple5.GetHashCode() == triple6.GetHashCode());

            triple5 = new Triple <int, string, OddEvenComparable>(4, "A", new OddEvenComparable(7));
            triple6 = new Triple <int, string, OddEvenComparable>(4, "A", new OddEvenComparable(2));
            Assert.IsTrue(triple5.CompareTo(triple6) < 0);
            Assert.IsFalse(triple5.Equals(triple6));
            Assert.IsFalse(triple5.GetHashCode() == triple6.GetHashCode());

            triple5 = new Triple <int, string, OddEvenComparable>(4, "A", new OddEvenComparable(7));
            triple6 = new Triple <int, string, OddEvenComparable>(4, "A", new OddEvenComparable(7));
            Assert.IsTrue(triple5.CompareTo(triple6) == 0);
            Assert.IsTrue(triple5.Equals(triple6));
            Assert.IsTrue(triple5.GetHashCode() == triple6.GetHashCode());

            triple5 = new Triple <int, string, OddEvenComparable>(7, "B", new OddEvenComparable(7));
            triple6 = new Triple <int, string, OddEvenComparable>(4, "C", new OddEvenComparable(2));
            Assert.IsTrue(triple5.CompareTo(triple6) > 0);
            Assert.IsFalse(triple5.Equals(triple6));
            Assert.IsFalse(triple5.GetHashCode() == triple6.GetHashCode());

            triple5 = new Triple <int, string, OddEvenComparable>(0, "A", new OddEvenComparable(8));
            triple6 = new Triple <int, string, OddEvenComparable>(0, "A", new OddEvenComparable(2));
            Assert.IsTrue(triple5.CompareTo(triple6) > 0);
            Assert.IsFalse(triple5.Equals(triple6));
            Assert.IsFalse(triple5.GetHashCode() == triple6.GetHashCode());
        }
Esempio n. 8
0
 /// <summary>
 /// Returns a hash code for this instance.
 /// </summary>
 /// <returns>
 /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
 /// </returns>
 public override int GetHashCode()
 {
     return(m_left.GetHashCode() ^ m_right.GetHashCode() ^ m_sharedItem.GetHashCode() ^ m_posLeft.GetHashCode() ^ m_posRight.GetHashCode());
 }
Esempio n. 9
0
 /// <summary>
 /// Hash Code for Batch Triples
 /// </summary>
 /// <returns></returns>
 public override int GetHashCode()
 {
     return((_graphID + _t.GetHashCode()).GetHashCode());
 }