private static Dictionary <FactID, FactID> ForEachFact(FactTreeMemento tree, Func <FactMemento, FactID?> processFact) { Dictionary <FactID, FactID> localIdByRemoteId = new Dictionary <FactID, FactID>(); foreach (IdentifiedFactBase identifiedFact in tree.Facts) { if (identifiedFact is IdentifiedFactMemento) { FactMemento translatedMemento = TranslateMemento(localIdByRemoteId, (IdentifiedFactMemento)identifiedFact); if (translatedMemento != null) { FactID?localId = processFact(translatedMemento); if (localId != null) { localIdByRemoteId.Add(identifiedFact.Id, localId.Value); } } } else if (identifiedFact is IdentifiedFactRemote) { localIdByRemoteId.Add(identifiedFact.Id, ((IdentifiedFactRemote)identifiedFact).RemoteId); } } return(localIdByRemoteId); }
protected static FactMemento CreateRoom(long domainId) { FactMemento room = new FactMemento(TypeRoom); room.Data = new byte[] { 1, 2, 3, 4 }; room.AddPredecessor(RoleRoomDomain, new FactID { key = domainId }, true); return room; }
public CorrespondenceFact CreateFact(FactMemento memento) { Like newFact = new Like(memento); return(newFact); }
private static FactMemento NewGame(Guid gameGuid) { FactMemento saveGame = new FactMemento(GameType); saveGame.Data = gameGuid.ToByteArray(); return(saveGame); }
private List <IdentifiedFactMemento> FindExistingFacts(FactMemento memento, Procedures procedures, bool readCommitted) { int typeId = SaveType(procedures, memento.FactType); // Load all candidates that have the same hash code. return(procedures.GetEqualFactsByHashCode(memento, readCommitted, typeId)); }
public CorrespondenceFact CreateFact(FactMemento memento) { Domain newFact = new Domain(memento); return(newFact); }
// Hydration constructor private CloudCentralThought(FactMemento memento) { InitializeResults(); _cloud = new PredecessorObj <Cloud>(this, RoleCloud, memento); _prior = new PredecessorList <CloudCentralThought>(this, RolePrior, memento); _value = new PredecessorObj <Thought>(this, RoleValue, memento); }
public CorrespondenceFact CreateFact(FactMemento memento) { ShareDelete newFact = new ShareDelete(memento); return(newFact); }
public CorrespondenceFact CreateFact(FactMemento memento) { DisableToastNotification newFact = new DisableToastNotification(memento); return(newFact); }
private static void ReadBinary(IDataReader reader, FactMemento memento, int columnIndex) { byte[] buffer = new byte[1024]; int length = (int)reader.GetBytes(columnIndex, 0, buffer, 0, buffer.Length); memento.Data = new byte[length]; Array.Copy(buffer, memento.Data, length); }
private static FactMemento NewMove(FactID gameId, byte moveIndex) { var move = new FactMemento(MoveType); move.Data = new byte[] { moveIndex }; move.AddPredecessor(MoveGame, gameId, true); return(move); }
protected static FactMemento CreateRoom(long domainId) { FactMemento room = new FactMemento(TypeRoom); room.Data = new byte[] { 1, 2, 3, 4 }; room.AddPredecessor(RoleRoomDomain, new FactID { key = domainId }, true); return(room); }
public FactID? FindExistingFact(string domain, FactMemento fact) { using (var procedures = new Procedures(new Session(_connectionString))) { FactID id; if (FindExistingFact(fact, out id, procedures, readCommitted: true)) return id; return null; } }
private void AddToFactTree(string domain, FactTreeMemento messageBody, FactID factId, Dictionary <FactID, FactID> localIdByRemoteId) { if (!messageBody.Contains(factId)) { FactMemento fact = _repository.Load(domain, factId); foreach (PredecessorMemento predecessor in fact.Predecessors) { AddToFactTree(domain, messageBody, predecessor.ID, localIdByRemoteId); } messageBody.Add(new IdentifiedFactMemento(factId, fact)); } }
public FactID?FindExistingFact(string domain, FactMemento fact) { using (var session = new Session(_connectionString)) { FactID id; if (FindExistingFact(fact, out id, session, readCommitted: true)) { return(id); } return(null); } }
public FactID? FindExistingFact(string domain, FactMemento memento) { lock (this) { // See if the fact already exists. FactRecord fact = _factTable.FirstOrDefault(o => o.IdentifiedFactMemento.Memento.Equals(memento)); if (fact == null) return null; else return fact.IdentifiedFactMemento.Id; } }
public CorrespondenceFact CreateFact(FactMemento memento) { EnableToastNotification newFact = new EnableToastNotification(memento); // Create a memory stream from the memento data. using (MemoryStream data = new MemoryStream(memento.Data)) { using (BinaryReader output = new BinaryReader(data)) { } } return(newFact); }
public CorrespondenceFact CreateFact(FactMemento memento) { Link newFact = new Link(memento); // Create a memory stream from the memento data. using (MemoryStream data = new MemoryStream(memento.Data)) { using (BinaryReader output = new BinaryReader(data)) { } } return(newFact); }
public CorrespondenceFact CreateFact(FactMemento memento) { DisableToastNotification newFact = new DisableToastNotification(memento); // Create a memory stream from the memento data. using (MemoryStream data = new MemoryStream(memento.Data)) { using (BinaryReader output = new BinaryReader(data)) { newFact._unique = (Guid)_fieldSerializerByType[typeof(Guid)].ReadData(output); } } return(newFact); }
public CorrespondenceFact CreateFact(FactMemento memento) { Tune newFact = new Tune(memento); // Create a memory stream from the memento data. using (MemoryStream data = new MemoryStream(memento.Data)) { using (BinaryReader output = new BinaryReader(data)) { newFact._name = (string)_fieldSerializerByType[typeof(string)].ReadData(output); } } return(newFact); }
public CorrespondenceFact CreateFact(FactMemento memento) { Identity newFact = new Identity(memento); // Create a memory stream from the memento data. using (MemoryStream data = new MemoryStream(memento.Data)) { using (BinaryReader output = new BinaryReader(data)) { newFact._anonymousId = (string)_fieldSerializerByType[typeof(string)].ReadData(output); } } return newFact; }
private IdentifiedFactMemento DeserlializeIdentifiedFactMemento(BinaryReader factReader, long factId) { short dataSize; byte[] data; short predecessorCount; CorrespondenceFactType factType = GetFactType(BinaryHelper.ReadShort(factReader)); dataSize = BinaryHelper.ReadShort(factReader); if (dataSize > MaxDataLength) { throw new CorrespondenceException("Maximum data length exceeded."); } data = dataSize > 0 ? factReader.ReadBytes(dataSize) : new byte[0]; predecessorCount = BinaryHelper.ReadShort(factReader); if (predecessorCount > MaxPredecessorCount) { throw new CorrespondenceException("Maximum predecessor count exceeded."); } FactMemento factMemento = new FactMemento(factType); factMemento.Data = data; for (short i = 0; i < predecessorCount; i++) { bool isPivot; long predecessorFactId; RoleMemento role = GetRole(BinaryHelper.ReadShort(factReader)); isPivot = BinaryHelper.ReadBoolean(factReader); predecessorFactId = BinaryHelper.ReadLong(factReader); factMemento.AddPredecessor( role, new FactID() { key = predecessorFactId }, isPivot ); } return(new IdentifiedFactMemento(new FactID { key = factId }, factMemento)); }
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); }
public FactID?FindExistingFact(string domain, FactMemento memento) { lock (this) { // See if the fact already exists. FactRecord fact = _factTable.FirstOrDefault(o => o.IdentifiedFactMemento.Memento.Equals(memento)); if (fact == null) { return(null); } else { return(fact.IdentifiedFactMemento.Id); } } }
public FactID InsertFact(FactMemento fact, int typeId) { _session.Command.CommandText = "INSERT Fact (FKTypeID, Data, Hashcode) VALUES (@TypeID, @Data, @Hashcode)"; AddParameter("@TypeID", typeId); AddParameter("@Data", fact.Data); AddParameter("@Hashcode", fact.GetHashCode()); _session.Command.ExecuteNonQuery(); _session.Command.Parameters.Clear(); _session.Command.CommandText = "SELECT @@IDENTITY"; decimal result = (decimal)_session.Command.ExecuteScalar(); _session.Command.Parameters.Clear(); return(new FactID { key = (Int64)result }); }
public List <IdentifiedFactMemento> GetEqualFactsByHashCode(FactMemento memento, bool readCommitted, int typeId) { _session.Command.CommandText = HEAD_SELECT + (readCommitted ? "" : "WITH (NOLOCK) ") + "WHERE f.FKTypeID = @TypeID AND f.Hashcode = @Hashcode " + TAIL_JOIN + "ORDER BY ff.FactID, p.PredecessorID"; AddParameter("@TypeID", typeId); AddParameter("@Hashcode", memento.GetHashCode()); using (var loader = new Loader(_session.Command.ExecuteReader())) { _session.Command.Parameters.Clear(); return(loader.LoadMementos() .Where(im => im.Memento.Equals(memento)) .ToList()); } }
private bool FindExistingFact(FactMemento memento, out FactID id, Session session, bool readCommitted) { var existingFacts = FindExistingFacts(memento, session, readCommitted); if (existingFacts.Count > 1) { throw new CorrespondenceException(string.Format("More than one fact matched the given {0}.", memento.FactType)); } if (existingFacts.Count == 1) { id = existingFacts[0].Id; return(true); } else { id = new FactID(); return(false); } }
private List <IdentifiedFactMemento> FindExistingFacts(FactMemento memento, Session session, bool readCommitted) { int typeId = SaveType(session, memento.FactType); // Load all candidates that have the same hash code. session.Command.CommandText = HEAD_SELECT + (readCommitted ? "" : "WITH (NOLOCK) ") + "WHERE f.FKTypeID = @TypeID AND f.Hashcode = @Hashcode " + TAIL_JOIN + "ORDER BY ff.FactID, p.PredecessorID"; AddParameter(session.Command, "@TypeID", typeId); AddParameter(session.Command, "@Hashcode", memento.GetHashCode()); using (IDataReader factReader = session.Command.ExecuteReader()) { session.Command.Parameters.Clear(); return(LoadMementosFromReader(factReader).Where(im => im.Memento.Equals(memento)).ToList()); } }
private static FactTreeMemento CreateTreeWithMultipleFacts() { FactTreeMemento tree = CreateTreeWithSingleFact(); var game = new FactMemento( new CorrespondenceFactType("TestModel.Game", 1)) .AddPredecessor( CreateRoleDomain(), new FactID { key = 42 }, true); game.Data = new byte[] { 1, 2, 3, 4 }; tree.Add(new IdentifiedFactMemento( new FactID { key = 73 }, game)); return(tree); }
// Hydration constructor private Message(FactMemento memento) { InitializeResults(); _messageBoard = new PredecessorObj<MessageBoard>(this, RoleMessageBoard, memento); _domain = new PredecessorObj<Domain>(this, RoleDomain, memento); }
// Hydration constructor private Individual(FactMemento memento) { InitializeResults(); }
// Hydration constructor private Company(FactMemento memento) { InitializeResults(); }
// Hydration constructor private Individual__name(FactMemento memento) { InitializeResults(); _individual = new PredecessorObj <Individual>(this, GetRoleIndividual(), memento, Individual.GetUnloadedInstance, Individual.GetNullInstance); _prior = new PredecessorList <Individual__name>(this, GetRolePrior(), memento, Individual__name.GetUnloadedInstance, Individual__name.GetNullInstance); }
// Hydration constructor private DeprovisionFrontOffice(FactMemento memento) { InitializeResults(); _provision = new PredecessorObj<ProvisionFrontOffice>(this, RoleProvision, memento); }
public FactID Save(string domain, FactMemento fact, Guid clientGuid) { // Retry on concurrency failure. while (true) { using (var session = new Session(_connectionString)) { session.BeginTransaction(); // First see if the fact is already in storage. FactID id; if (FindExistingFact(fact, out id, session, readCommitted: true)) { return(id); } // It isn't there, so store it. int typeId = SaveType(session, fact.FactType); session.Command.CommandText = "INSERT Fact (FKTypeID, Data, Hashcode) VALUES (@TypeID, @Data, @Hashcode)"; AddParameter(session.Command, "@TypeID", typeId); AddParameter(session.Command, "@Data", fact.Data); AddParameter(session.Command, "@Hashcode", fact.GetHashCode()); session.Command.ExecuteNonQuery(); session.Command.Parameters.Clear(); session.Command.CommandText = "SELECT @@IDENTITY"; decimal result = (decimal)session.Command.ExecuteScalar(); session.Command.Parameters.Clear(); id.key = (Int64)result; // Store the predecessors. foreach (PredecessorMemento predecessor in fact.Predecessors) { int roleId = SaveRole(session, predecessor.Role); session.Command.CommandText = "INSERT Predecessor (FKFactID, FKRoleID, FKPredecessorFactID, IsPivot) VALUES (@FactID, @RoleID, @PredecessorFactID, @IsPivot)"; AddParameter(session.Command, "@FactID", id.key); AddParameter(session.Command, "@RoleID", roleId); AddParameter(session.Command, "@PredecessorFactID", predecessor.ID.key); AddParameter(session.Command, "@IsPivot", predecessor.IsPivot); session.Command.ExecuteNonQuery(); session.Command.Parameters.Clear(); } // Store a message for each pivot. FactID newFactId = id; List <MessageMemento> pivotMessages = fact.Predecessors .Where(predecessor => predecessor.IsPivot) .Select(predecessor => new MessageMemento(predecessor.ID, newFactId)) .ToList(); // Store messages for each non-pivot. This fact belongs to all predecessors' pivots. string[] nonPivots = fact.Predecessors .Where(predecessor => !predecessor.IsPivot) .Select(predecessor => predecessor.ID.key.ToString()) .ToArray(); List <MessageMemento> nonPivotMessages; if (nonPivots.Length > 0) { string nonPivotGroup = string.Join(",", nonPivots); session.Command.CommandText = string.Format( "SELECT DISTINCT PivotId FROM Message WHERE FactId IN ({0})", nonPivotGroup); List <FactID> predecessorsPivots; using (IDataReader predecessorPivotReader = session.Command.ExecuteReader()) { session.Command.Parameters.Clear(); predecessorsPivots = LoadIDsFromReader(predecessorPivotReader).ToList(); } nonPivotMessages = predecessorsPivots .Select(predecessorPivot => new MessageMemento(predecessorPivot, newFactId)) .ToList(); } else { nonPivotMessages = new List <MessageMemento>(); } int clientId = SaveClient(session, clientGuid); var messages = pivotMessages.Union(nonPivotMessages).Distinct().ToList(); SaveMessages(session, messages, clientId); // Optimistic concurrency check. // Make sure we don't find more than one. var existingFacts = FindExistingFacts(fact, session, readCommitted: false); if (existingFacts.Count == 1) { session.Commit(); if (messages.Any() && PivotAffected != null) { foreach (var message in messages) { PivotAffected(domain, message.PivotId); } } return(id); } else { _retried = true; } } } }
// Hydration constructor private Share(FactMemento memento) { InitializeResults(); _individual = new PredecessorObj<Individual>(this, RoleIndividual, memento); _messageBoard = new PredecessorObj<MessageBoard>(this, RoleMessageBoard, memento); }
public CorrespondenceFact CreateFact(FactMemento memento) { Message newFact = new Message(memento); // Create a memory stream from the memento data. using (MemoryStream data = new MemoryStream(memento.Data)) { using (BinaryReader output = new BinaryReader(data)) { newFact._unique = (Guid)_fieldSerializerByType[typeof(Guid)].ReadData(output); newFact._text = (string)_fieldSerializerByType[typeof(string)].ReadData(output); } } return newFact; }
// Hydration constructor private ContactInformation(FactMemento memento) { InitializeResults(); _order = new PredecessorObj<Order>(this, RoleOrder, memento); _prior = new PredecessorList<ContactInformation>(this, RolePrior, memento); }
// Hydration constructor private Tune(FactMemento memento) { InitializeResults(); }
// Hydration constructor private Like(FactMemento memento) { InitializeResults(); _individual = new PredecessorObj <Individual>(this, GetRoleIndividual(), memento, Individual.GetUnloadedInstance, Individual.GetNullInstance); _tune = new PredecessorObj <Tune>(this, GetRoleTune(), memento, Tune.GetUnloadedInstance, Tune.GetNullInstance); }
// Hydration constructor private MessageBoard(FactMemento memento) { InitializeResults(); }
// Hydration constructor private CakeDetail(FactMemento memento) { InitializeResults(); _order = new PredecessorObj<Order>(this, RoleOrder, memento); _size = new PredecessorObj<CakeSize>(this, RoleSize, memento); _cakeFlavor = new PredecessorObj<CakeFlavor>(this, RoleCakeFlavor, memento); _frostingFlavor = new PredecessorObj<FrostingFlavor>(this, RoleFrostingFlavor, memento); _mainColor = new PredecessorObj<FrostingColor>(this, RoleMainColor, memento); _decorationColor = new PredecessorObj<FrostingColor>(this, RoleDecorationColor, memento); _prior = new PredecessorList<CakeDetail>(this, RolePrior, memento); }
// Hydration constructor private ProvisionFrontOffice(FactMemento memento) { InitializeResults(); _machine = new PredecessorObj<Machine>(this, RoleMachine, memento); _company = new PredecessorObj<Company>(this, RoleCompany, memento); }
// Hydration constructor private Pull(FactMemento memento) { InitializeResults(); _order = new PredecessorObj<Order>(this, RoleOrder, memento); _kitchen = new PredecessorObj<Kitchen>(this, RoleKitchen, memento); }
// Hydration constructor private DeliveryDetail(FactMemento memento) { InitializeResults(); _order = new PredecessorObj<Order>(this, RoleOrder, memento); _city = new PredecessorObj<City>(this, RoleCity, memento); _prior = new PredecessorList<DeliveryDetail>(this, RolePrior, memento); }
// Hydration constructor private Closed(FactMemento memento) { InitializeResults(); _delivered = new PredecessorObj<Delivered>(this, RoleDelivered, memento); }
// Hydration constructor private DeprovisionKitchen(FactMemento memento) { InitializeResults(); _provision = new PredecessorObj<ProvisionKitchen>(this, RoleProvision, memento); }
// Hydration constructor private Machine(FactMemento memento) { InitializeResults(); }
// Hydration constructor private DisableToastNotification(FactMemento memento) { InitializeResults(); _enable = new PredecessorList<EnableToastNotification>(this, RoleEnable, memento); }
// Hydration constructor private ProvisionKitchen(FactMemento memento) { InitializeResults(); _machine = new PredecessorObj<Machine>(this, RoleMachine, memento); _kitchen = new PredecessorObj<Kitchen>(this, RoleKitchen, memento); }
// Hydration constructor private Domain(FactMemento memento) { InitializeResults(); }
// Hydration constructor private City(FactMemento memento) { InitializeResults(); _company = new PredecessorObj<Company>(this, RoleCompany, memento); }
public CorrespondenceFact CreateFact(FactMemento memento) { Domain newFact = new Domain(memento); // Create a memory stream from the memento data. using (MemoryStream data = new MemoryStream(memento.Data)) { using (BinaryReader output = new BinaryReader(data)) { } } return newFact; }
// Hydration constructor private Commitment(FactMemento memento) { InitializeResults(); _pull = new PredecessorObj<Pull>(this, RolePull, memento); _cakeDetail = new PredecessorList<CakeDetail>(this, RoleCakeDetail, memento); _prior = new PredecessorList<Commitment>(this, RolePrior, memento); }
// Hydration constructor private EnableToastNotification(FactMemento memento) { InitializeResults(); _individual = new PredecessorObj<Individual>(this, RoleIndividual, memento); }
// Hydration constructor private Completed(FactMemento memento) { InitializeResults(); _commitment = new PredecessorObj<Commitment>(this, RoleCommitment, memento); }
public CorrespondenceFact CreateFact(FactMemento memento) { EnableToastNotification newFact = new EnableToastNotification(memento); // Create a memory stream from the memento data. using (MemoryStream data = new MemoryStream(memento.Data)) { using (BinaryReader output = new BinaryReader(data)) { newFact._unique = (Guid)_fieldSerializerByType[typeof(Guid)].ReadData(output); } } return newFact; }
// Hydration constructor private Delivered(FactMemento memento) { InitializeResults(); _completed = new PredecessorObj<Completed>(this, RoleCompleted, memento); _deliveryDetail = new PredecessorObj<DeliveryDetail>(this, RoleDeliveryDetail, memento); }
public FactID Save(string domain, FactMemento fact, Guid clientGuid) { FactID factId; List<FactID> affectedPivots; lock (this) { // See if the fact already exists. FactRecord existingFact = _factTable.FirstOrDefault(o => o.IdentifiedFactMemento.Memento.Equals(fact)); if (existingFact == null) { // It doesn't, so create it. FactID newFactID = new FactID() { key = _factTable.Count + 1 }; factId = newFactID; existingFact = new FactRecord() { IdentifiedFactMemento = new IdentifiedFactMemento(factId, fact) }; _factTable.Add(existingFact); // Store a message for each pivot. var pivots = fact.Predecessors .Where(predecessor => predecessor.IsPivot); _messageTable.AddRange(pivots .Select(predecessor => new MessageRecord( new MessageMemento(predecessor.ID, newFactID), newFactID, predecessor.Role, clientGuid))); // Store messages for each non-pivot. This fact belongs to all predecessors' pivots. List<FactID> nonPivots = fact.Predecessors .Where(predecessor => !predecessor.IsPivot) .Select(predecessor => predecessor.ID) .ToList(); List<MessageRecord> predecessorsPivots = _messageTable .Where(message => nonPivots.Contains(message.Message.FactId)) .Distinct() .ToList(); _messageTable.AddRange(predecessorsPivots .Select(predecessorPivot => new MessageRecord( new MessageMemento(predecessorPivot.Message.PivotId, newFactID), predecessorPivot.AncestorFact, predecessorPivot.AncestorRole, clientGuid))); affectedPivots = pivots .Select(pivot => pivot.ID) .Union(predecessorsPivots .Select(predecessorPivot => predecessorPivot.Message.PivotId)) .ToList(); } else { factId = existingFact.IdentifiedFactMemento.Id; affectedPivots = null; } } if (affectedPivots != null && affectedPivots.Any() && PivotAffected != null) foreach (var pivotId in affectedPivots) PivotAffected(domain, pivotId, factId, clientGuid); return factId; }