///------------------------------------------------------------------------------------------------- /// <summary> /// Gets or try to create property. /// </summary> /// <exception cref="Exception"> /// Thrown when an exception error condition occurs. /// </exception> /// <param name="propertyName"> /// Name of the property. /// </param> /// <param name="type"> /// The type. /// </param> /// <returns> /// The or try to create property. /// </returns> ///------------------------------------------------------------------------------------------------- protected ISchemaProperty GetOrTryToCreateProperty(string propertyName, Type type) { DebugContract.RequiresNotEmpty(propertyName, "protectedName"); DebugContract.Requires(type, "type"); var propertyMetadata = ((IModelElement)this).SchemaInfo.GetProperty(propertyName); if (propertyMetadata == null) { //Session.Current.AcquireLock(LockType.ExclusiveWait, String.Format("{0}.{1}", Metadata.Id, propertyName)); //propertyMetadata = Metadata.GetProperty(propertyName); //if (propertyMetadata == null) //{ // var pmetadata = Store.GetMetadata(typeName) as IMetaValue; // if (pmetadata == null) // throw new Exception("Unknow meta value type " + typeName); // propertyMetadata = new MetaProperty(Metadata, propertyName, pmetadata); // Metadata.DefineProperty(propertyMetadata); //} throw new PropertyDefinitionException(string.Format(ExceptionMessages.UnknownPropertyFormat, propertyName)); } return(propertyMetadata); }
private void ReleaseLock(LockInfo lockInfo) { DebugContract.Requires(lockInfo, "lockInfo"); if (lockInfo.Lock.IsWriteLockHeld) { lockInfo.Lock.ExitWriteLock(); } if (lockInfo.Lock.IsUpgradeableReadLockHeld) { lockInfo.Lock.ExitUpgradeableReadLock(); } _trace.WriteTrace(TraceCategory.LockManager, "Release lock for {0} in tx {1}", lockInfo.Ressource, lockInfo.SessionId); // Suppression des locks associés à cette transaction si il n'y en a pas en attente. if (lockInfo.DecRef() == 0) { Debug.Assert(lockInfo.Lock.WaitingWriteCount == 0 && lockInfo.Lock.WaitingUpgradeCount == 0); _sync.EnterWriteLock(); try { _locks.Remove(lockInfo.Ressource); } finally { _sync.ExitWriteLock(); } } }
/// <summary> /// Notification de la fin de la session /// </summary> private ISessionInformation OnSessionCompleted(SessionLocalInfo info, IEnumerable <IEventNotifier> notifiers, IExecutionResultInternal messages) { DebugContract.Requires(notifiers != null, "notifiers"); // On fait une copie de la session car les événements peuvent // être souscrits dans un autre thread var sessionContext = new SessionInformation(this, info, SessionDataContext.TrackingData, messages); try { // Notifications via RX. foreach (var notifier in notifiers) { notifier.NotifySessionCompleted(sessionContext, SessionContext); } // Déclenchement des événements // D'abord évenement hard var tmp = Completing; if (tmp != null) { tmp(this, new SessionCompletingEventArgs(sessionContext)); } } catch (Exception ex) { // Si une erreur survient dans une notification, on l'intercepte Log(new DiagnosticMessage(MessageType.Error, ex.Message, "SessionTerminated", SessionDataContext.InValidationProcess, null, ex)); } return(sessionContext); }
///------------------------------------------------------------------------------------------------- /// <summary> /// Persist element. /// </summary> /// <param name="commandFactory"> /// . /// </param> ///------------------------------------------------------------------------------------------------- protected virtual void PersistElement(Func <IDomainModel, Identity, ISchemaElement, IDomainCommand> commandFactory) { Contract.Requires(commandFactory, "commandFactory"); DebugContract.Requires(Session.Current); Session.Current.Execute(commandFactory(DomainModel, _id, ((IModelElement)this).SchemaInfo)); }
// Création d'une référence 0..1 à partir de l'élément courant ou vers l'élement courant si opposite vaut true internal void SetReference(ref Identity relationshipId, ISchemaRelationship relationshipSchema, IModelElement target, bool opposite) { DebugContract.Requires(relationshipSchema, "relationshipMetadata"); using (var session = EnsuresRunInSession()) { var commands = new List <IDomainCommand>(); IModelRelationship relationship = null; // !opposite ? this -> target : target -> this IModelElement start = !opposite ? this : null; IModelElement end = !opposite ? null : this; // Si l'identifiant est fourni c'est que la relation existe surement dèjà if (relationshipId != null) { relationship = DomainModel.GetRelationship(relationshipId); } if (relationship == null) { relationship = DomainModel.GetRelationships(relationshipSchema, start, end).FirstOrDefault(); } start = !opposite ? this : target; end = !opposite ? target : this; // Si cette relation existe dèjà mais sur un autre élement, on la supprime if (relationship != null) { // Si elle existe sur le même élement, c'est bon on la conserve if (end != null && relationship.End.Id == end.Id && start != null && relationship.Start.Id == start.Id) { relationshipId = relationship.Id; return; } // Suppression car elle pointe sur un élement diffèrent commands.Add(new RemoveRelationshipCommand(relationship)); } relationship = null; relationshipId = null; // Si elle n'a pas été mise à null if (end != null && start != null) { relationshipId = DomainModel.IdGenerator.NextValue(relationshipSchema); commands.Add(new AddRelationshipCommand(relationshipSchema, start, end, relationshipId)); } Session.Current.Execute(commands.ToArray()); if (session != null) { session.AcceptChanges(); } } }
///------------------------------------------------------------------------------------------------- /// <summary> /// Releases the locks. /// </summary> /// <param name="locks"> /// The locks. /// </param> /// <param name="sessionAborted"> /// true if session aborted. /// </param> ///------------------------------------------------------------------------------------------------- public void ReleaseLocks(ICollection <ILockInfo> locks, bool sessionAborted) { DebugContract.Requires(locks); foreach (LockInfo lockInfo in locks) { lockInfo.SessionStatus = sessionAborted ? TransactionStatus.Aborted : TransactionStatus.Committed; ReleaseLock(lockInfo); } }
///------------------------------------------------------------------------------------------------- /// <summary> /// Called when [deserializing]. /// </summary> /// <param name="schemaElement"> /// The schema element. /// </param> /// <param name="domainModel"> /// The domain model. /// </param> /// <param name="key"> /// The key. /// </param> /// <param name="start"> /// The start. /// </param> /// <param name="end"> /// The end. /// </param> ///------------------------------------------------------------------------------------------------- protected override void OnDeserializing(ISchemaElement schemaElement, IDomainModel domainModel, string key, Identity start, Identity end) { DebugContract.Requires(start); DebugContract.Requires(end); base.OnDeserializing(schemaElement, domainModel, key, start, end); _startId = start; _endId = end; }
void IPropertyChangedNotifier.NotifyPropertyChanged(string propertyName) { DebugContract.RequiresNotEmpty(propertyName); var tmp = PropertyChanged; if (tmp != null) { tmp(this, new PropertyChangedEventArgs(propertyName)); } }
internal ExtensionSchemaDefinition(ISchemaDefinition definition, ISchema extendedSchema, SchemaConstraintExtensionMode mode) : base(definition.SchemaName) { DebugContract.Requires(extendedSchema, "extendedSchema"); DebugContract.Requires(definition, "definition"); _definition = definition; _extendedSchema = extendedSchema; _mode = mode; }
///------------------------------------------------------------------------------------------------- /// <summary> /// Initializes a new instance of the <see cref="DiagnosticMessage"/> class. /// </summary> /// <param name="messageType"> /// Type of the message. /// </param> /// <param name="message"> /// The message. /// </param> /// <param name="category"> /// The category. /// </param> /// <param name="modelElement"> /// (Optional) The model element. /// </param> /// <param name="ex"> /// (Optional) The ex. /// </param> /// <param name="propertyName"> /// (Optional) The name of the property. /// </param> ///------------------------------------------------------------------------------------------------- public DiagnosticMessage(MessageType messageType, string message, string category, IModelElement modelElement = null, Exception ex = null, string propertyName = null) { DebugContract.RequiresNotEmpty(message); MessageType = messageType; Message = message; Category = category; Element = modelElement; Exception = ex; PropertyName = propertyName; }
/// <summary> /// Constructeur interne. Il est appelé par le store quand il crée la session /// </summary> /// <param name="store">The store.</param> /// <param name="cfg">The CFG.</param> internal Session(IHyperstore store, SessionConfiguration cfg) { DebugContract.Requires(store, "store"); if (SessionIndex == null) { SessionIndex = s_sessionSequences.GetFirstFreeValue(); } _trace = store.Trace; var ctx = SessionDataContext; if (ctx == null) { // _statSessionCount.Incr(); // Nouvelle session ctx = new SessionDataContext // TODO optimize memory size { TrackingData = new SessionTrackingData(this), SessionIsolationLevel = cfg.IsolationLevel, Locks = new List <ILockInfo>(), ReadOnly = cfg.Readonly, ReadOnlyStatus = cfg.Readonly, Current = this, OriginStoreId = cfg.Origin ?? store.Id, Mode = cfg.Mode, SessionId = cfg.SessionId != 0 ? cfg.SessionId : Interlocked.Increment(ref s_sessionIdSequences), Store = store, CancellationToken = cfg.CancellationToken, Enlistment = new List <ISessionEnlistmentNotification>(), SessionInfos = new Stack <SessionLocalInfo>(), TopLevelSession = this, DefaultDomainModel = cfg.DefaultDomainModel, }; SessionDataContext = ctx; _scope = Hyperstore.Modeling.Platform.PlatformServices.Current.CreateTransactionScope(this, cfg); } else if (ctx.SessionInfos.Count == 0) { throw new HyperstoreException(ExceptionMessages.CannotCreateNestedSessionInDisposingSession); } ctx.SessionInfos.Push(new SessionLocalInfo { DefaultDomainModel = cfg.DefaultDomainModel ?? ctx.DefaultDomainModel, Mode = cfg.Mode | ctx.Mode, OriginStoreId = cfg.Origin ?? ctx.OriginStoreId }); }
internal static Tuple <string, string> SplitFullName(string name) { DebugContract.RequiresNotEmpty(name); var pos = name.LastIndexOf('.'); if (pos < 0) { return(Tuple.Create(String.Empty, name)); } return(Tuple.Create(name.Substring(0, pos), name.Substring(pos + 1))); }
///------------------------------------------------------------------------------------------------- /// <summary> /// Queries if a given ensures schema exists. /// </summary> /// <param name="domainModel"> /// . /// </param> /// <param name="name"> /// The name. /// </param> /// <returns> /// An ISchemaElement. /// </returns> ///------------------------------------------------------------------------------------------------- protected virtual ISchemaElement EnsuresSchemaExists(IDomainModel domainModel, string name) { Contract.Requires(domainModel, "domainModel"); Contract.RequiresNotEmpty(name, "name"); DebugContract.Requires(Session.Current); var metadata = domainModel.Store.GetSchemaElement(name, false); if (metadata != null) { return(metadata); } Session.Current.AcquireLock(LockType.ExclusiveWait, name); metadata = domainModel.Store.GetSchemaElement(name, !(domainModel is ISchema)) ?? new GeneratedSchemaEntity(domainModel as ISchema, name, GetType()); return(metadata); }
internal IModelElement GetReference(ref Identity relationshipId, ISchemaRelationship relationshipSchema, bool isOpposite) { DebugContract.Requires(relationshipSchema, "relationshipSchema"); var propertyName = isOpposite ? relationshipSchema.EndPropertyName : relationshipSchema.StartPropertyName; SetCalculatedPropertySource(propertyName); IModelRelationship relationship = null; if (relationshipId != null) { relationship = DomainModel.GetRelationship(relationshipId); } if (relationship == null) { var start = isOpposite ? null : this; var end = isOpposite ? this : null; relationship = DomainModel.GetRelationships(relationshipSchema, start, end).FirstOrDefault(); } if (relationship != null) { relationshipId = relationship.Id; var opposite = isOpposite ? relationship.Start : relationship.End; if (opposite != null) { var mel = _store.GetElement(opposite.Id); if (mel == null) { throw new InvalidElementException(opposite.Id, ExceptionMessages.InvalidReference); } return(mel); } return(opposite); } relationshipId = null; return(null); }
internal SessionInformation(Session session, SessionLocalInfo info, ISessionTrackingData trackingData, IExecutionResultInternal messages) { DebugContract.Requires(session, "session"); DebugContract.Requires(trackingData); _context = session.SessionContext; TrackingData = trackingData; CancellationToken = session.CancellationToken; IsAborted = session.IsAborted; IsNested = session.IsNested; Store = session.Store; IsReadOnly = session.IsReadOnly; Mode = info.Mode; OriginStoreId = info.OriginStoreId; SessionId = session.SessionId; DefaultDomainModel = info.DefaultDomainModel; _contextInfos = info.Infos; Events = session.Events.ToList(); if (messages != null) { HasErrors = messages.HasErrors; HasWarnings = messages.HasWarnings; } }
internal MultiThreadedSession(ushort?sessionIndex, int threadId) { DebugContract.Requires(sessionIndex > 0); SessionIndex = sessionIndex; _threadId = threadId; }
/// <summary> /// Initializes a new instance of the <see cref="SessionCompletingEventArgs" /> class. /// </summary> /// <param name="session">The session.</param> internal SessionCompletingEventArgs(ISessionInformation session) { DebugContract.Requires(session); Session = session; }
///------------------------------------------------------------------------------------------------- /// <summary> /// Creates a schema. /// </summary> /// <param name="services"> /// The services. /// </param> /// <returns> /// The new schema. /// </returns> ///------------------------------------------------------------------------------------------------- ISchema <T> ISchemaDefinition.CreateSchema <T>(IServicesContainer services) { DebugContract.Requires(services); return(CreateSchema <T>(services)); }
///------------------------------------------------------------------------------------------------- /// <summary> /// Defines the schema. /// </summary> /// <param name="schema"> /// The schema. /// </param> ///------------------------------------------------------------------------------------------------- void ISchemaDefinition.DefineSchema(ISchema schema) { DebugContract.Requires(schema); DefineSchema(schema); }
///------------------------------------------------------------------------------------------------- /// <summary> /// Called when [schema loaded]. /// </summary> /// <param name="schema"> /// The schema. /// </param> ///------------------------------------------------------------------------------------------------- void ISchemaDefinition.OnSchemaLoaded(ISchema schema) { DebugContract.Requires(schema); OnSchemaLoaded(schema); }
internal static string ToJsonName(string name) { DebugContract.Assert(name.Length > 0); return(Char.ToLower(name[0]) + name.Substring(1)); }
public CalculatedProperty(string propertyName) { DebugContract.RequiresNotEmpty(propertyName); PropertyName = propertyName; }
internal DiagnosticMessage(MessageType messageType, string message, string category, bool isConstraintMessage, IModelElement modelElement = null, Exception ex = null, string propertyName = null) : this(messageType, message, category, modelElement, ex, propertyName) { DebugContract.RequiresNotEmpty(message); IsConstraintMessage = isConstraintMessage; }
internal DynamicModelElementMetaObject(Expression parameter, object value) : base(parameter, BindingRestrictions.Empty, value) { DebugContract.Requires(parameter, "parameter"); }
internal SessionException(IEnumerable <DiagnosticMessage> messages) { DebugContract.Requires(messages); Messages = messages.ToList(); }