public Structure(string name, IStructureId id, IStructureIndex[] indexes, string data = null)
        {
            Ensure.That(name, "name").IsNotNullOrWhiteSpace();
            Ensure.That(id, "id").IsNotNull();

            Name    = name;
            Id      = id;
            Data    = data;
            Indexes = Lists.New(indexes);
            Uniques = Lists.New(indexes.Where(i => i.IsUnique).ToArray());

            if (Uniques.Count > 0)
            {
                var firstUniqueNotBeingUnique =
                    Uniques.FirstOrDefault(u => Indexes.Count(i => i.Path.Equals(u.Path)) > 1);
                if (firstUniqueNotBeingUnique != null)
                {
                    var idValue     = Sys.StringConverter.AsString(firstUniqueNotBeingUnique.StructureId.Value);
                    var uniqueValue = Sys.StringConverter.AsString(firstUniqueNotBeingUnique.Value);
                    throw new SisoDbException(
                              ExceptionMessages.Structure_DuplicateUniques.Inject(
                                  Name,
                                  idValue,
                                  firstUniqueNotBeingUnique.Path,
                                  uniqueValue));
                }
            }
        }
 protected virtual void EnsureThatNewIdEqualsOldId(IStructureId oldId, IStructureId newId)
 {
     if (!newId.Value.Equals(oldId.Value))
     {
         throw new SisoDbException(ExceptionMessages.StructureSetMigrator_NewIdDoesNotMatchOldId.Inject(newId.Value, oldId.Value));
     }
 }
示例#3
0
 public virtual void NotifyDeleted(ISession session, IStructureSchema schema, IStructureId id)
 {
     foreach (var handler in OnDeletedHandlers)
     {
         handler.Invoke(session, schema, id);
     }
 }
示例#4
0
        public virtual void DeleteById(IStructureId structureId, IStructureSchema structureSchema)
        {
            Ensure.That(structureSchema, "structureSchema").IsNotNull();

            var sql = SqlStatements.GetSql("DeleteById").Inject(structureSchema.GetStructureTableName());

            ExecuteNonQuery(sql, new DacParameter("id", structureId.Value));
        }
示例#5
0
        public virtual string GetJsonByIdWithLock(IStructureId structureId, IStructureSchema structureSchema)
        {
            Ensure.That(structureSchema, "structureSchema").IsNotNull();

            var sql = SqlStatements.GetSql("GetJsonByIdWithLock").Inject(structureSchema.GetStructureTableName());

            return(ExecuteScalar <string>(sql, new DacParameter("id", structureId.Value)));
        }
示例#6
0
        protected virtual void RemoveQueriesHaving(IStructureId structureId)
        {
            var keys = InternalQueryCache.Where(kv => ((ISet <IStructureId>)kv.Value).Contains(structureId)).Select(kv => kv.Key).ToArray();

            foreach (var key in keys)
            {
                InternalQueryCache.Remove(key);
            }
        }
示例#7
0
        public virtual T Put <T>(IStructureId id, T structure) where T : class
        {
            if (structure != null)
            {
                InternalStructureCache.Set(GenerateCacheKey(id), structure, CreateCacheItemPolicy());
            }

            return(structure);
        }
        public IStructureId[] Generate(IStructureSchema structureSchema, int numOfIds)
        {
            var structureIds = new IStructureId[numOfIds];

            for (var c = 0; c < structureIds.Length; c++)
                structureIds[c] = StructureId.Create(Guid.NewGuid());

            return structureIds;
        }
示例#9
0
        public virtual bool Exists(IStructureSchema structureSchema, IStructureId structureId)
        {
            Ensure.That(structureSchema, "structureSchema").IsNotNull();
            Ensure.That(structureId, "structureId").IsNotNull();

            var sql = SqlStatements.GetSql("ExistsById").Inject(structureSchema.GetStructureTableName());

            return(ExecuteScalar <int>(sql, new DacParameter("id", structureId.Value)) > 0);
        }
        public virtual IStructureId[] Generate(IStructureSchema structureSchema, int numOfIds)
        {
            var structureIds = new IStructureId[numOfIds];

            for (var c = 0; c < structureIds.Length; c++)
                structureIds[c] = StructureId.Create(SequentialGuid.New());

            return structureIds;
        }
        private IStructureId[] GenerateBigIdentityStructureId(IStructureSchema structureSchema, int numOfIds)
        {
            var structureIds = new IStructureId[numOfIds];
			var startId = DbClient.CheckOutAndGetNextIdentity(structureSchema.Name, numOfIds);

            for (var c = 0; c < numOfIds; c++)
                structureIds[c] = StructureId.Create(startId++);

            return structureIds;
        }
示例#12
0
        public virtual string GetJsonById(IStructureId structureId, IStructureSchema structureSchema)
        {
            Ensure.That(structureSchema, "structureSchema").IsNotNull();

            var sql = StringExtensions.Inject(SqlStatements.GetSql("GetJsonById"), structureSchema.GetStructureTableName());

            return(HasPipe
                ? Pipe.Reading(structureSchema, ExecuteScalar <string>(sql, new DacParameter("id", structureId.Value)))
                : ExecuteScalar <string>(sql, new DacParameter("id", structureId.Value)));
        }
        public override void DeleteById(IStructureId structureId, IStructureSchema structureSchema)
        {
            Ensure.That(structureSchema, "structureSchema").IsNotNull();

            var sql = SqlStatements.GetSql("DeleteById").Inject(structureSchema.GetStructureTableName());

            using (var cmd = CreateCommand(sql, new DacParameter("id", structureId.Value)))
            {
                cmd.ExecuteNonQuery();
            }
        }
示例#14
0
        public virtual IStructureId[] Generate(IStructureSchema structureSchema, int numOfIds)
        {
            var structureIds = new IStructureId[numOfIds];

            for (var c = 0; c < structureIds.Length; c++)
            {
                structureIds[c] = StructureId.Create(GenerateComb());
            }

            return(structureIds);
        }
        protected virtual IStructureId[] GenerateBigIdentityStructureId(IStructureSchema structureSchema, int numOfIds)
        {
            var structureIds = new IStructureId[numOfIds];
            var startId      = DbClient.CheckOutAndGetNextIdentity(structureSchema.Name, numOfIds);

            for (var c = 0; c < numOfIds; c++)
            {
                structureIds[c] = StructureId.Create(startId++);
            }

            return(structureIds);
        }
        public int CompareTo(IStructureId other)
        {
            if (other.IdType != IdType)
            {
                throw new SisoDbException(ExceptionMessages.StructureId_CompareTo_DifferentIdTypes);
            }

            if (Equals(other))
            {
                return(0);
            }

            if (IdType == StructureIdTypes.Identity)
            {
                var x = (int?)Value;
                var y = (int?)other.Value;

                if (x.HasValue && y.HasValue)
                {
                    return(x.Value.CompareTo(y.Value));
                }

                return(x.HasValue ? -1 : 1);
            }

            if (IdType == StructureIdTypes.BigIdentity)
            {
                var x = (long?)Value;
                var y = (long?)other.Value;

                if (x.HasValue && y.HasValue)
                {
                    return(x.Value.CompareTo(y.Value));
                }

                return(x.HasValue ? -1 : 1);
            }

            if (IdType.IsGuid())
            {
                var x = (Guid?)Value;
                var y = (Guid?)other.Value;

                if (x.HasValue && y.HasValue)
                {
                    return(x.Value.CompareTo(y.Value));
                }

                return(x.HasValue ? -1 : 1);
            }

            return(Sys.StringComparer.Compare(Value.ToString(), other.Value.ToString()));
        }
示例#17
0
        public virtual void DeleteIndexesAndUniquesById(IStructureId structureId, IStructureSchema structureSchema)
        {
            Ensure.That(structureSchema, "structureSchema").IsNotNull();

            var indexesTableNames = structureSchema.GetIndexesTableNames();
            var uniquesTableName  = structureSchema.GetUniquesTableName();

            var sql = StringExtensions.Inject(SqlStatements.GetSql("DeleteIndexesAndUniquesById"), uniquesTableName,
                                              indexesTableNames.BooleansTableName,
                                              indexesTableNames.DatesTableName,
                                              indexesTableNames.FractalsTableName,
                                              indexesTableNames.GuidsTableName,
                                              indexesTableNames.IntegersTableName,
                                              indexesTableNames.StringsTableName,
                                              indexesTableNames.TextsTableName);

            ExecuteNonQuery(sql, new DacParameter("id", structureId.Value));
        }
        public bool Equals(IStructureId other)
        {
            if (ReferenceEquals(null, other))
            {
                return(false);
            }
            if (ReferenceEquals(this, other))
            {
                return(true);
            }

            if (IdType == StructureIdTypes.String)
            {
                return(string.Equals(Value as string, other.Value as string, Sys.StringComparision));
            }

            return(Equals(other.Value, Value));
        }
        public StructureIndex(IStructureId structureId, string path, object value, Type dataType, DataTypeCode dataTypeCode, StructureIndexType indexType = StructureIndexType.Normal)
        {
            var valueIsOkType = value is string || value is ValueType;

            if (value != null && !valueIsOkType)
                throw new ArgumentException(ExceptionMessages.StructureIndex_ValueArgument_IncorrectType);

            Ensure.That(structureId, "structureId").IsNotNull();
            Ensure.That(path, "path").IsNotNullOrWhiteSpace();
			Ensure.That(dataType, "dataType").IsNotNull();

            StructureId = structureId;
            Path = path;
            Value = value;
        	DataType = dataType;
			DataTypeCode = dataTypeCode;
            IndexType = indexType;
            IsUnique = indexType.IsUnique();
        }
        public StructureIndex(IStructureId structureId, string path, object value, Type dataType, DataTypeCode dataTypeCode, StructureIndexType indexType = StructureIndexType.Normal)
        {
            var valueIsOkType = value is string || value is ValueType;

            if (value != null && !valueIsOkType)
            {
                throw new ArgumentException(ExceptionMessages.StructureIndex_ValueArgument_IncorrectType);
            }

            Ensure.That(structureId, "structureId").IsNotNull();
            Ensure.That(path, "path").IsNotNullOrWhiteSpace();
            Ensure.That(dataType, "dataType").IsNotNull();

            StructureId  = structureId;
            Path         = path;
            Value        = value;
            DataType     = dataType;
            DataTypeCode = dataTypeCode;
            IndexType    = indexType;
            IsUnique     = indexType.IsUnique();
        }
示例#21
0
 internal IndexRow(IStructureId structureId, IStructureIndex[] indexes)
 {
     StructureId = structureId;
     Indexes = indexes;
 }
示例#22
0
        public bool Equals(IStructureId other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;

            if (IdType == StructureIdTypes.String)
                return string.Equals(Value as string, other.Value as string, Sys.StringComparision);

            return Equals(other.Value, Value);
        }
示例#23
0
 public virtual void Remove(IStructureId id)
 {
     RemoveQueriesHaving(id);
     InternalStructureCache.Remove(GenerateCacheKey(id));
 }
示例#24
0
 public static IStructureId GetSmallest(IStructureId x, IStructureId y)
 {
     return x.CompareTo(y) == -1
         ? x
         : y;
 }
示例#25
0
        public int CompareTo(IStructureId other)
        {
            if (other.IdType != IdType)
                throw new SisoDbException(ExceptionMessages.StructureId_CompareTo_DifferentIdTypes);

            if (Equals(other))
                return 0;

            if (IdType == StructureIdTypes.Identity)
            {
                var x = (int?)Value;
                var y = (int?)other.Value;

                if (x.HasValue && y.HasValue)
                    return x.Value.CompareTo(y.Value);

                return x.HasValue ? -1 : 1;
            }

            if (IdType == StructureIdTypes.BigIdentity)
            {
                var x = (long?)Value;
                var y = (long?)other.Value;

                if (x.HasValue && y.HasValue)
                    return x.Value.CompareTo(y.Value);

                return x.HasValue ? -1 : 1;
            }

            if (IdType.IsGuid())
            {
                var x = (Guid?)Value;
                var y = (Guid?)other.Value;

                if (x.HasValue && y.HasValue)
                    return x.Value.CompareTo(y.Value);

                return x.HasValue ? -1 : 1;
            }

            return Sys.StringComparer.Compare(Value.ToString(), other.Value.ToString());
        }
 public virtual void NotifyDeleted(ISession session, IStructureSchema schema, IStructureId id)
 {
     foreach (var handler in OnDeletedHandlers)
         handler.Invoke(session, schema, id);
 }
 public virtual void NotifyDeleted(ISession session, IStructureSchema schema, IStructureId[] ids)
 {
     foreach (var id in ids)
         NotifyDeleted(session, schema, id);
 }
示例#28
0
 public virtual T GetById <T>(IStructureId id) where T : class
 {
     return(InternalStructureCache.Get(GenerateCacheKey(id)) as T);
 }
        public static bool Exists(this ICacheProvider cacheProvider, IStructureSchema structureSchema, IStructureId structureId, Func <IStructureId, bool> nonCacheQuery)
        {
            if (!cacheProvider.IsEnabledFor(structureSchema))
            {
                return(nonCacheQuery.Invoke(structureId));
            }

            return(cacheProvider[structureSchema.Type.Type].Exists(structureId) || nonCacheQuery.Invoke(structureId));
        }
 public void Set(IStructureId id)
 {
     From       = From ?? id;
     To         = id;
     IsComplete = From != null && To != null;
 }
示例#31
0
        public IStructureIndex[] CreateIndexes <T>(IStructureSchema structureSchema, T item, IStructureId structureId) where T : class
        {
            var indexes = new IEnumerable <IStructureIndex> [structureSchema.IndexAccessors.Count];

            Parallel.For(0, indexes.Length, c =>
            {
                var indexAccessor        = structureSchema.IndexAccessors[c];
                var values               = indexAccessor.GetValues(item);
                var valuesExists         = values != null && values.Count > 0;
                var isCollectionOfValues = indexAccessor.IsEnumerable || indexAccessor.IsElement || (values != null && values.Count > 1);

                if (!valuesExists)
                {
                    if (!isCollectionOfValues && indexAccessor.IsUnique)
                    {
                        throw new SisoDbException(ExceptionMessages.StructureIndexesFactory_UniqueIndex_IsNull.Inject(structureSchema.Name, indexAccessor.Path));
                    }

                    return;
                }

                if (!isCollectionOfValues)
                {
                    indexes[c] = new[] { new StructureIndex(structureId, indexAccessor.Path, values[0], indexAccessor.DataType, indexAccessor.DataTypeCode, indexAccessor.UniqueMode.ToStructureIndexType()) }
                }
                ;
                else
                {
                    var subIndexes = new IStructureIndex[values.Count];
                    Parallel.For(0, subIndexes.Length, subC =>
                    {
                        if (values[subC] != null)
                        {
                            subIndexes[subC] = new StructureIndex(structureId, indexAccessor.Path, values[subC],
                                                                  indexAccessor.DataType, indexAccessor.DataTypeCode,
                                                                  indexAccessor.UniqueMode.ToStructureIndexType());
                        }
                    });
                    indexes[c] = subIndexes;
                }
            });

            return(indexes.Where(i => i != null).SelectMany(i => i).Where(i => i != null).ToArray());
        }
示例#32
0
 protected virtual string GenerateCacheKey(IStructureId id)
 {
     return(id.Value.ToString());
 }
 public void Clear()
 {
     From = null;
     To = null;
     IsComplete = false;
 }
		public void Set(IStructureId id)
		{
			From = From ?? id;
			To = id;
			IsComplete = From != null && To != null;
		}
示例#35
0
 public void SetValue <T>(T item, IStructureId value)
     where T : class
 {
     _setter.SetIdValue(item, value, Property);
 }
        public static T Consume <T>(this ICacheProvider cacheProvider, IStructureSchema structureSchema, IStructureId structureId, Func <IStructureId, T> nonCacheQuery, CacheConsumeModes consumeMode) where T : class
        {
            if (!cacheProvider.IsEnabledFor(structureSchema))
            {
                return(nonCacheQuery.Invoke(structureId));
            }

            var cache     = cacheProvider[structureSchema.Type.Type];
            var structure = cache.GetById <T>(structureId);

            if (structure != null)
            {
                return(structure);
            }

            structure = nonCacheQuery.Invoke(structureId);
            if (structure == null || consumeMode == CacheConsumeModes.DoNotUpdateCacheWithDbResult)
            {
                return(structure);
            }

            return(cache.Put(structureId, structure));
        }
        public static void Remove(this ICacheProvider cacheProvider, IStructureSchema structureSchema, IStructureId structureId)
        {
            if (!cacheProvider.IsEnabledFor(structureSchema))
            {
                return;
            }

            cacheProvider[structureSchema.Type.Type].Remove(structureId);
        }
示例#38
0
 public virtual bool Exists(IStructureId id)
 {
     return(InternalStructureCache.Contains(GenerateCacheKey(id)));
 }
        public override void DeleteWhereIdIsBetween(IStructureId structureIdFrom, IStructureId structureIdTo, IStructureSchema structureSchema)
        {
            Ensure.That(structureSchema, "structureSchema").IsNotNull();

            var sql = SqlStatements.GetSql("DeleteWhereIdIsBetween").Inject(structureSchema.GetStructureTableName());

            using (var cmd = CreateCommand(sql, new DacParameter("idFrom", structureIdFrom.Value), new DacParameter("idTo", structureIdTo.Value)))
            {
                cmd.ExecuteNonQuery();
            }
        }
示例#40
0
        protected virtual void OnEnsureConcurrencyTokenIsValid(IStructureSchema structureSchema, IStructureId structureId, object newItem, Type typeForDeserialization)
        {
            var existingJson = DbClient.GetJsonById(structureId, structureSchema);

            if (string.IsNullOrWhiteSpace(existingJson))
            {
                throw new SisoDbException(ExceptionMessages.WriteSession_NoItemExistsForUpdate.Inject(structureSchema.Name, structureId.Value));
            }

            var existingItem  = Db.Serializer.Deserialize(existingJson, typeForDeserialization);
            var existingToken = structureSchema.ConcurrencyTokenAccessor.GetValue(existingItem);
            var updatingToken = structureSchema.ConcurrencyTokenAccessor.GetValue(newItem);

            if (!Equals(updatingToken, existingToken))
            {
                throw new SisoDbConcurrencyException(structureId.Value, structureSchema.Name, ExceptionMessages.ConcurrencyException);
            }

            if (existingToken is Guid)
            {
                structureSchema.ConcurrencyTokenAccessor.SetValue(newItem, Guid.NewGuid());
                return;
            }

            if (existingToken is int)
            {
                var existingNumericToken = (int)existingToken;
                structureSchema.ConcurrencyTokenAccessor.SetValue(newItem, existingNumericToken + 1);
                return;
            }

            if (existingToken is long)
            {
                var existingNumericToken = (long)existingToken;
                structureSchema.ConcurrencyTokenAccessor.SetValue(newItem, existingNumericToken + 1);
                return;
            }

            throw new SisoDbException(ExceptionMessages.ConcurrencyTokenIsOfWrongType);
        }
示例#41
0
 public void SetIdValue <T>(T item, IStructureId id, IStructureProperty property) where T : class
 {
     property.SetValue(item, (long?)id.Value);
 }
 public static IStructureId GetSmallest(IStructureId x, IStructureId y)
 {
     return(x.CompareTo(y) == -1
         ? x
         : y);
 }