Пример #1
0
        public IReadOnlyCollection <EnqueueResult> Enqueue(IEnumerable <EnqueueItem> items)
        {
            lock (_lock)
            {
                var result = new List <EnqueueResult>();
                foreach (var item in items)
                {
                    var meta       = _messagingMetas[item.DeviceId];
                    int enqueueIdx = meta.EnqueueIndex % meta.QueueSize;

                    var queueEntry = _queueEntries[item.DeviceId][enqueueIdx];
                    queueEntry.Payload        = item.Payload;
                    queueEntry.Timestamp      = item.Timestamp;
                    queueEntry.SenderDeviceId = item.SenderDeviceId;

                    var resultItem = new EnqueueResult
                    {
                        Id        = item.DeviceId,
                        MessageId = meta.EnqueueIndex,
                    };

                    meta.Version++;
                    meta.EnqueueIndex++;
                    if (meta.DequeueIndex + meta.QueueSize <= meta.EnqueueIndex)
                    {
                        meta.DequeueIndex++;
                        meta.Peek = false;
                    }

                    resultItem.DequeueIndex = meta.DequeueIndex;
                    resultItem.EnqueueIndex = meta.EnqueueIndex;
                    resultItem.Peek         = meta.Peek;
                    resultItem.Version      = meta.Version;

                    result.Add(resultItem);
                }

                return(result);
            }
        }
Пример #2
0
        public IReadOnlyCollection <EnqueueResult> Enqueue(IEnumerable <EnqueueItem> items)
        {
            if (!items.Any())
            {
                return(new List <EnqueueResult>());
            }

            var dataTable = new DataTable("EnqueueItemTable");

            dataTable.Columns.Add("DeviceId", typeof(long));
            dataTable.Columns.Add("Payload", typeof(byte[]));
            dataTable.Columns.Add("Timestamp", typeof(DateTime));
            dataTable.Columns.Add("SenderUid", typeof(string));

            foreach (var item in items)
            {
                var dataRow = dataTable.NewRow();
                dataRow[0] = item.DeviceId;
                dataRow[1] = item.Payload;
                dataRow[2] = item.Timestamp;
                dataRow[3] = item.SenderDeviceId;
                dataTable.Rows.Add(dataRow);
            }

            using (var sqlConnection = new SqlConnection(_connectionString))
            {
                sqlConnection.Open();

                using (var sqlCommand = new SqlCommand("Enqueue", sqlConnection))
                {
                    sqlCommand.CommandType = CommandType.StoredProcedure;
                    sqlCommand.Parameters.Add(new SqlParameter("@Messages", dataTable));

                    var enqueueResults = new List <EnqueueResult>();

                    using (var reader = sqlCommand.ExecuteReader())
                    {
                        var colDeviceId     = reader.GetOrdinal("DeviceId");
                        var colDequeueIndex = reader.GetOrdinal("DequeueIndex");
                        var colEnqueueIndex = reader.GetOrdinal("EnqueueIndex");
                        var colPeek         = reader.GetOrdinal("Peek");
                        var colVersion      = reader.GetOrdinal("Version");
                        var colMessageId    = reader.GetOrdinal("MessageId");

                        while (reader.Read())
                        {
                            var enqueueResult = new EnqueueResult
                            {
                                Id           = (long)reader[colDeviceId],
                                DequeueIndex = (int)reader[colDequeueIndex],
                                EnqueueIndex = (int)reader[colEnqueueIndex],
                                Peek         = (bool)reader[colPeek],
                                Version      = (int)reader[colVersion],
                                MessageId    = (int)reader[colMessageId]
                            };

                            enqueueResults.Add(enqueueResult);
                        }
                    }

                    return(enqueueResults);
                }
            }
        }