Exemple #1
0
        /// <exception cref="InvalidOperationException">Missed secondary key for some criterion.</exception>
        public virtual IEnumerable <DatabaseEntry> GetByJoin(IBDbEntityInfo info, ICollection <IDataCriterion> criteria)
        {
            var        cursorsLength    = 0;
            var        secondaryCursors = new SecondaryCursor[criteria.Count];
            JoinCursor joinCursor       = null;

            try {
                var primaryDb = _schema.GetPrimaryDb(info.EntityName);

                foreach (var criterion in criteria)
                {
                    SecondaryKeyAttribute secondaryKey;
                    if (!info.SecondaryKeys.TryGetValue(criterion.Name, out secondaryKey))
                    {
                        var msg = $"Missed secondaty key '{criterion.Name}' for entity {info.EntityName} ({info.EntityType}).";
                        Log.TraceEvent(TraceEventType.Critical, msg);
                        throw new InvalidOperationException(msg);
                    }

                    var secondaryDb = _schema.GetSecondaryDb(primaryDb, secondaryKey);

                    var cursor = secondaryDb.SecondaryCursor();
                    secondaryCursors[cursorsLength++] = cursor;

                    var key = new DatabaseEntry(criterion.Value.ToBinnary());
                    if (!cursor.Move(key, exact:true))
                    {
                        yield break;
                    }
                }

                joinCursor = primaryDb.Join(secondaryCursors, true);
                while (joinCursor.MoveNext())
                {
                    yield return(joinCursor.Current.Value);
                }
            }
            finally {
                joinCursor?.Close();

                for (var i = 0; i < cursorsLength; i++)
                {
                    secondaryCursors[i].Close();
                }
            }
        }
Exemple #2
0
        /// <exception cref="NotSupportedException">Can't provide id for <paramref name="entity"/>.</exception>
        protected virtual DatabaseEntry KeyOf(object entity, IBDbEntityInfo info)
        {
            var bytesIdentified = entity as IIdentified <byte[]>;

            if (bytesIdentified != null)
            {
                return(new DatabaseEntry(bytesIdentified.Id));
            }

            var guidIdentified = entity as IIdentified <Guid>;

            if (guidIdentified != null)
            {
                return(new DatabaseEntry(guidIdentified.Id.ToByteArray()));
            }

            var longIdentified = entity as IIdentified <long>;

            if (longIdentified != null)
            {
                return(new DatabaseEntry(BitConverter.GetBytes(longIdentified.Id)));
            }

            var intIdentified = entity as IIdentified <int>;

            if (intIdentified != null)
            {
                return(new DatabaseEntry(BitConverter.GetBytes(intIdentified.Id)));
            }

            throw new NotSupportedException("By default, supported only identifying of entities that implements at least one of the following interfaces:\n" +
                                            $"{nameof(IIdentified<byte[]>)},\n" +
                                            $"{nameof(IIdentified<Guid>)},\n" +
                                            $"{nameof(IIdentified<long>)},\n" +
                                            $"{nameof(IIdentified<int>)}.\n" +
                                            $"Please, implement at least one of these interfaces in entity '{info.EntityType.FullName}', or manually override that logic.");
        }
Exemple #3
0
 public BDbDataGetter(IReadingRepository readingRepository, IBDbEntityInfo info) : base(readingRepository, info)
 {
 }
Exemple #4
0
 protected bool Equals(IBDbEntityInfo other)
 {
     return(string.Equals(EntityName, other.EntityName) && Equals(EntityType, other.EntityType) && Equals(Repository, other.Repository));
 }