public FactMemento Load(string domain, FactID factId)
        {
            IdentifiedFactMemento identifiedMemento = null;

            using (var session = new Session(_connectionString))
            {
                // Get the fact.
                session.Command.CommandText = HEAD_SELECT +
                                              "WHERE f.FactID = @FactID " +
                                              TAIL_JOIN +
                                              "ORDER BY p.PredecessorID";
                AddParameter(session.Command, "@FactID", factId.key);
                using (IDataReader reader = session.Command.ExecuteReader())
                {
                    session.Command.Parameters.Clear();
                    identifiedMemento = LoadMementosFromReader(reader).FirstOrDefault();
                    if (identifiedMemento == null)
                    {
                        throw new CorrespondenceException(string.Format("Unable to find fact {0}", factId.key));
                    }
                }
            }

            return(identifiedMemento.Memento);
        }
Exemple #2
0
        public void CanGetPublishedFact()
        {
            long domainId = AddDomain();
            long roomId   = AddRoom(domainId);

            FactTreeMemento tree = new FactTreeMemento(0);

            tree.Add(new IdentifiedFactMemento(
                         new FactID {
                key = 4124
            },
                         new FactMemento(TypeDomain)));
            Dictionary <long, long> pivotIds = new Dictionary <long, long>();

            pivotIds.Add(4124, 0);

            var result = _service.GetManyAsync(ClientGuid1, "domain", tree, pivotIds, 0).Result.Tree;

            Assert.AreEqual(2, result.Facts.Count());
            IdentifiedFactMemento resultDomain = (IdentifiedFactMemento)result.Facts.ElementAt(0);
            IdentifiedFactMemento resultRoom   = (IdentifiedFactMemento)result.Facts.ElementAt(1);

            Assert.AreEqual(TypeDomain, resultDomain.Memento.FactType);
            Assert.IsFalse(resultDomain.Memento.Predecessors.Any());
            Assert.AreEqual(TypeRoom, resultRoom.Memento.FactType);
            Assert.AreEqual(resultDomain.Id, resultRoom.Memento.Predecessors.ElementAt(0).ID);
            Assert.AreEqual(roomId, pivotIds[4124]);
        }
        private IEnumerable <IdentifiedFactMemento> LoadMementosFromReader(IDataReader factReader)
        {
            IdentifiedFactMemento current = null;

            while (factReader.Read())
            {
                // FactID, Data, TypeName, Version, DeclaringTypeName, DeclaringTypeVersion, RoleName, PredecessorFactID, IsPivot
                long factId = factReader.GetInt64(0);

                // Load the header.
                if (current == null || factId != current.Id.key)
                {
                    if (current != null)
                    {
                        yield return(current);
                    }

                    string typeName    = factReader.GetString(2);
                    int    typeVersion = factReader.GetInt32(3);

                    // Create the memento.
                    current = new IdentifiedFactMemento(
                        new FactID()
                    {
                        key = factId
                    },
                        new FactMemento(new CorrespondenceFactType(typeName, typeVersion)));
                    ReadBinary(factReader, current.Memento, 1);
                }

                // Load a predecessor.
                if (!factReader.IsDBNull(4))
                {
                    string declaringTypeName    = factReader.GetString(4);
                    int    declaringTypeVersion = factReader.GetInt32(5);
                    string roleName             = factReader.GetString(6);
                    long   predecessorFactId    = factReader.GetInt64(7);
                    bool   isPivot = factReader.GetBoolean(8);

                    current.Memento.AddPredecessor(
                        new RoleMemento(
                            new CorrespondenceFactType(
                                declaringTypeName,
                                declaringTypeVersion),
                            roleName,
                            null,
                            false),
                        new FactID()
                    {
                        key = predecessorFactId
                    },
                        isPivot);
                }
            }

            if (current != null)
            {
                yield return(current);
            }
        }
        public void BinaryHttp_Serialization_GetManyRequest_SingleFact()
        {
            Client.GetManyRequest clientRequest = new Client.GetManyRequest
            {
                ClientGuid     = "{96E5C2E2-1345-482E-9668-6009DCDC0E70}",
                Domain         = "TestDomain",
                TimeoutSeconds = 30,
                PivotTree      = CreateTreeWithSingleFact(),
                PivotIds       = new List <Client.FactTimestamp>
                {
                    new Client.FactTimestamp {
                        FactId = 42, TimestampId = 28768
                    }
                }
            };

            var serverRequest = WriteAndRead(
                w => clientRequest.Write(w),
                r => Server.BinaryRequest.Read(r) as Server.GetManyRequest);

            Assert.IsNotNull(serverRequest);
            Assert.AreEqual(1, serverRequest.PivotTree.Facts.Count());
            Assert.AreEqual(42, serverRequest.PivotTree.Facts.ElementAt(0).Id.key);
            IdentifiedFactMemento fact = serverRequest.PivotTree.Facts.ElementAt(0) as IdentifiedFactMemento;

            Assert.IsNotNull(fact);
            Assert.AreEqual("TestModel.Domain", fact.Memento.FactType.TypeName);
            Assert.AreEqual(1, serverRequest.PivotIds.Count());
            Assert.AreEqual(42, serverRequest.PivotIds.ElementAt(0).FactId);
            Assert.AreEqual(28768, serverRequest.PivotIds.ElementAt(0).TimestampId);
        }
        public void BinaryHttp_Serialization_GetManyResponse_SingleFact()
        {
            var serverResponse = new Server.GetManyResponse
            {
                FactTree = CreateTreeWithSingleFact(),
                PivotIds = new List <FactTimestamp>
                {
                    new FactTimestamp {
                        FactId = 42, TimestampId = 67676
                    }
                }
            };

            var clientResponse = WriteAndRead(
                w => serverResponse.Write(w),
                r => Client.BinaryResponse.Read(r) as Client.GetManyResponse);

            Assert.IsNotNull(clientResponse);
            Assert.AreEqual(1, clientResponse.FactTree.Facts.Count());
            Assert.AreEqual(42, clientResponse.FactTree.Facts.ElementAt(0).Id.key);
            IdentifiedFactMemento fact = clientResponse.FactTree.Facts.ElementAt(0) as IdentifiedFactMemento;

            Assert.IsNotNull(fact);
            Assert.AreEqual("TestModel.Domain", fact.Memento.FactType.TypeName);
            Assert.AreEqual(1, clientResponse.PivotIds.Count());
            Assert.AreEqual(42, clientResponse.PivotIds.ElementAt(0).FactId);
            Assert.AreEqual(67676, clientResponse.PivotIds.ElementAt(0).TimestampId);
        }
        public IEnumerable<IdentifiedFactMemento> LoadMementos()
        {
            IdentifiedFactMemento current = null;

            while (_reader.Read())
            {
                // FactID, Data, TypeName, Version, DeclaringTypeName, DeclaringTypeVersion, RoleName, PredecessorFactID, IsPivot
                long factId = _reader.GetInt64(0);

                // Load the header.
                if (current == null || factId != current.Id.key)
                {
                    if (current != null)
                        yield return current;

                    string typeName = _reader.GetString(2);
                    int typeVersion = _reader.GetInt32(3);

                    // Create the memento.
                    current = new IdentifiedFactMemento(
                        new FactID() { key = factId },
                        new FactMemento(new CorrespondenceFactType(typeName, typeVersion)));
                    ReadBinary(current.Memento, 1);
                }

                // Load a predecessor.
                if (!_reader.IsDBNull(4))
                {
                    string declaringTypeName = _reader.GetString(4);
                    int declaringTypeVersion = _reader.GetInt32(5);
                    string roleName = _reader.GetString(6);
                    long predecessorFactId = _reader.GetInt64(7);
                    bool isPivot = _reader.GetBoolean(8);

                    current.Memento.AddPredecessor(
                        new RoleMemento(
                            new CorrespondenceFactType(
                                declaringTypeName,
                                declaringTypeVersion),
                            roleName,
                            null,
                            false),
                        new FactID() { key = predecessorFactId },
                        isPivot);
                }
            }

            if (current != null)
                yield return current;
        }
        public FactMemento Load(string domain, FactID factId)
        {
            IdentifiedFactMemento identifiedMemento = null;

            using (var procedures = new Procedures(new Session(_connectionString)))
            {
                identifiedMemento = procedures.GetIdentifiedMemento(factId);
            }
            if (identifiedMemento == null)
            {
                throw new CorrespondenceException(string.Format("Unable to find fact {0}", factId.key));
            }

            return(identifiedMemento.Memento);
        }
        public void BinaryHttp_Serialization_GetManyRequest_MultipleFacts()
        {
            Client.GetManyRequest clientRequest = new Client.GetManyRequest
            {
                ClientGuid     = "{96E5C2E2-1345-482E-9668-6009DCDC0E70}",
                Domain         = "TestDomain",
                TimeoutSeconds = 30,
                PivotTree      = CreateTreeWithMultipleFacts(),
                PivotIds       = new List <Client.FactTimestamp>
                {
                    new Client.FactTimestamp {
                        FactId = 42, TimestampId = 28768
                    }
                }
            };

            var serverRequest = WriteAndRead(
                w => clientRequest.Write(w),
                r => Server.BinaryRequest.Read(r) as Server.GetManyRequest);

            Assert.IsNotNull(serverRequest);
            Assert.AreEqual(2, serverRequest.PivotTree.Facts.Count());
            Assert.AreEqual(42, serverRequest.PivotTree.Facts.ElementAt(0).Id.key);
            Assert.AreEqual(73, serverRequest.PivotTree.Facts.ElementAt(1).Id.key);
            IdentifiedFactMemento game = serverRequest.PivotTree.Facts.ElementAt(1) as IdentifiedFactMemento;

            Assert.IsNotNull(game);
            Assert.AreEqual("TestModel.Game", game.Memento.FactType.TypeName);
            Assert.AreEqual(1, game.Memento.Predecessors.Count());
            Assert.AreEqual(42, game.Memento.Predecessors.ElementAt(0).ID.key);
            Assert.AreEqual("TestModel.Game", game.Memento.Predecessors.ElementAt(0).Role.DeclaringType.TypeName);
            Assert.AreEqual("domain", game.Memento.Predecessors.ElementAt(0).Role.RoleName);
            Assert.IsTrue(game.Memento.Predecessors.ElementAt(0).IsPivot);
            Assert.AreEqual(4, game.Memento.Data.Length);
            Assert.AreEqual(1, game.Memento.Data[0]);
            Assert.AreEqual(2, game.Memento.Data[1]);
            Assert.AreEqual(3, game.Memento.Data[2]);
            Assert.AreEqual(4, game.Memento.Data[3]);
        }
Exemple #9
0
        private void SerializeIdentifiedFactMemento(IdentifiedFactMemento factMemento, BinaryWriter factWriter)
        {
            BinaryHelper.WriteLong(factMemento.Id.key, factWriter);
            BinaryHelper.WriteBoolean(true, factWriter);
            BinaryHelper.WriteShort(GetFactTypeId(factMemento.Memento.FactType), factWriter);
            short dataSize = factMemento.Memento.Data == null ? (short)0 : (short)(factMemento.Memento.Data.Length);

            BinaryHelper.WriteShort(dataSize, factWriter);
            if (dataSize != 0)
            {
                factWriter.Write(factMemento.Memento.Data);
            }

            short predecessorsCount = (short)factMemento.Memento.Predecessors.Count();

            BinaryHelper.WriteShort(predecessorsCount, factWriter);
            foreach (var predecessor in factMemento.Memento.Predecessors)
            {
                BinaryHelper.WriteShort(GetRoleId(predecessor.Role), factWriter);
                BinaryHelper.WriteBoolean(predecessor.IsPivot, factWriter);
                BinaryHelper.WriteLong(predecessor.ID.key, factWriter);
            }
        }
Exemple #10
0
        public void CanPublishFacts()
        {
            FactTreeMemento postTree = new FactTreeMemento(0);

            postTree.Add(new IdentifiedFactMemento(
                             new FactID {
                key = 3961
            },
                             CreateDomain()));
            postTree.Add(new IdentifiedFactMemento(
                             new FactID {
                key = 4979
            },
                             CreateRoom(3961)));
            _service.Post(ClientGuid1, "domain", postTree, new List <UnpublishMemento>());

            FactTreeMemento getTree = new FactTreeMemento(0);

            getTree.Add(new IdentifiedFactMemento(
                            new FactID {
                key = 9898
            },
                            CreateDomain()));
            Dictionary <long, long> pivotIds = new Dictionary <long, long>();

            pivotIds[9898] = 0;
            var result = _service.GetManyAsync(ClientGuid2, "domain", getTree, pivotIds, 0).Result.Tree;

            Assert.AreEqual(2, result.Facts.Count());
            IdentifiedFactMemento resultDomain = (IdentifiedFactMemento)result.Facts.ElementAt(0);
            IdentifiedFactMemento resultRoom   = (IdentifiedFactMemento)result.Facts.ElementAt(1);

            Assert.AreEqual(TypeDomain, resultDomain.Memento.FactType);
            Assert.IsFalse(resultDomain.Memento.Predecessors.Any());
            Assert.AreEqual(TypeRoom, resultRoom.Memento.FactType);
            Assert.AreEqual(resultDomain.Id, resultRoom.Memento.Predecessors.ElementAt(0).ID);
        }
        public void BinaryHttp_Serialization_GetManyResponse_MultipleFacts()
        {
            var serverResponse = new Server.GetManyResponse
            {
                FactTree = CreateTreeWithMultipleFacts(),
                PivotIds = new List <FactTimestamp>
                {
                    new FactTimestamp {
                        FactId = 42, TimestampId = 67676
                    }
                }
            };

            var clientResponse = WriteAndRead(
                w => serverResponse.Write(w),
                r => Client.BinaryResponse.Read(r) as Client.GetManyResponse);

            Assert.IsNotNull(clientResponse);
            Assert.AreEqual(2, clientResponse.FactTree.Facts.Count());
            Assert.AreEqual(42, clientResponse.FactTree.Facts.ElementAt(0).Id.key);
            Assert.AreEqual(73, clientResponse.FactTree.Facts.ElementAt(1).Id.key);
            IdentifiedFactMemento game = clientResponse.FactTree.Facts.ElementAt(1) as IdentifiedFactMemento;

            Assert.IsNotNull(game);
            Assert.AreEqual("TestModel.Game", game.Memento.FactType.TypeName);
            Assert.AreEqual(1, game.Memento.Predecessors.Count());
            Assert.AreEqual(42, game.Memento.Predecessors.ElementAt(0).ID.key);
            Assert.AreEqual("TestModel.Game", game.Memento.Predecessors.ElementAt(0).Role.DeclaringType.TypeName);
            Assert.AreEqual("domain", game.Memento.Predecessors.ElementAt(0).Role.RoleName);
            Assert.IsTrue(game.Memento.Predecessors.ElementAt(0).IsPivot);
            Assert.AreEqual(4, game.Memento.Data.Length);
            Assert.AreEqual(1, game.Memento.Data[0]);
            Assert.AreEqual(2, game.Memento.Data[1]);
            Assert.AreEqual(3, game.Memento.Data[2]);
            Assert.AreEqual(4, game.Memento.Data[3]);
        }
        private static FactMemento TranslateMemento(Dictionary <FactID, FactID> localIdByRemoteId, IdentifiedFactMemento identifiedFact)
        {
            FactMemento translatedMemento = new FactMemento(identifiedFact.Memento.FactType);

            translatedMemento.Data = identifiedFact.Memento.Data;
            foreach (PredecessorMemento remote in identifiedFact.Memento.Predecessors)
            {
                FactID localPredecessorId;
                if (!localIdByRemoteId.TryGetValue(remote.ID, out localPredecessorId))
                {
                    return(null);
                }

                translatedMemento.AddPredecessor(remote.Role, localPredecessorId, remote.IsPivot);
            }
            return(translatedMemento);
        }
        private static FactMemento TranslateMemento(Dictionary<FactID, FactID> localIdByRemoteId, IdentifiedFactMemento identifiedFact)
        {
            FactMemento translatedMemento = new FactMemento(identifiedFact.Memento.FactType);
            translatedMemento.Data = identifiedFact.Memento.Data;
            foreach (PredecessorMemento remote in identifiedFact.Memento.Predecessors)
            {
                FactID localPredecessorId;
                if (!localIdByRemoteId.TryGetValue(remote.ID, out localPredecessorId))
                    return null;

                translatedMemento.AddPredecessor(remote.Role, localPredecessorId, remote.IsPivot);
            }
            return translatedMemento;
        }
        private void SerializeIdentifiedFactMemento(IdentifiedFactMemento factMemento, BinaryWriter factWriter)
        {
            BinaryHelper.WriteLong(factMemento.Id.key, factWriter);
            BinaryHelper.WriteBoolean(true, factWriter);
            BinaryHelper.WriteShort(GetFactTypeId(factMemento.Memento.FactType), factWriter);
            short dataSize = factMemento.Memento.Data == null ? (short)0 : (short)(factMemento.Memento.Data.Length);
            BinaryHelper.WriteShort(dataSize, factWriter);
            if (dataSize != 0)
                factWriter.Write(factMemento.Memento.Data);

            short predecessorsCount = (short)factMemento.Memento.Predecessors.Count();
            BinaryHelper.WriteShort(predecessorsCount, factWriter);
            foreach (var predecessor in factMemento.Memento.Predecessors)
            {
                BinaryHelper.WriteShort(GetRoleId(predecessor.Role), factWriter);
                BinaryHelper.WriteBoolean(predecessor.IsPivot, factWriter);
                BinaryHelper.WriteLong(predecessor.ID.key, factWriter);
            }
        }