public Guid[] Get(ReservedIdsSource input)
 => _db.RetryOnTransientError(
     db => db.WithSql(
         q => q.From <ReservedIdRow>()
         .Where(d => d.Id == ReservedIdRow.GetId(input))
         .Select(d => d.Data)
         ).GetValue()
     ?.Split(',').Select(Guid.Parse).ToArray()
     ?? Array.Empty <Guid>());
Example #2
0
        public ReserveIdStorageTests()
        {
            _host = Substitute.For <IConfigureHost>();

            _host.WithReserveIdStorage(Arg.Do <IStoreReservedMessagesIds>(v => _sut = v));
            _cfg = new StoragesConfiguration(_host, Setup.GetConnection());
            _cfg.EnableReserveIdStorage(ifExists: TableExistsAction.DropIt);

            _src = new ReservedIdsSource()
            {
                Count       = 2,
                HandlerType = GetType(),
                MessageId   = Guid.Empty
            };
        }
 public void Add(ReservedIdsSource id, Guid[] ids)
 {
     _db.RetryOnTransientError(db =>
     {
         try
         {
             db.Connection.Insert(new ReservedIdRow()
             {
                 Id   = ReservedIdRow.GetId(id),
                 Data = ids.Select(d => d.ToString()).StringJoin()
             });
         }
         catch (DbException ex) when(db.Connection.IsUniqueViolation(ex))
         {
             //ignore duplicates
         }
     });
 }
Example #4
0
        /// <exception cref="InvalidReservationCountException"></exception>
        public Guid[] ReserveIdsFor <T>(T handlerType, Guid msgId, int howMany)
        {
            this.LogDebug("Getting reserved ids for event {0} handled by {1}", msgId, handlerType.GetType().Name);
            var input = new ReservedIdsSource()
            {
                Count = howMany, HandlerType = typeof(T), MessageId = msgId
            };
            var guids = _reservedIds.Get(input);

            if (guids.IsNullOrEmpty())
            {
                this.LogDebug("No ids are reserved for event {0} handled by {1}. Generating {2} ids", msgId, handlerType.GetType().Name, howMany);
                guids = Enumerable.Range(1, howMany).Select(d => Guid.NewGuid()).ToArray();
                _reservedIds.Add(input, guids);
            }
            else
            {
                if (guids.Length != howMany)
                {
                    throw new InvalidReservationCountException(msgId, typeof(T), howMany, guids.Length);
                }
            }
            return(guids);
        }
Example #5
0
 public void Add(ReservedIdsSource id, Guid[] ids)
 {
 }
Example #6
0
 public Guid[] Get(ReservedIdsSource input)
 => Array.Empty <Guid>();
Example #7
0
 public static string GetId(ReservedIdsSource src) => src.MessageId + (src.HandlerType != null ? src.HandlerType.FullName : "");