private DequeueResults Retrieve(string spName, IEnumerable <DeviceIdWithOpHint> deviceIds) { if (!deviceIds.Any()) { return new DequeueResults { Messages = new List <DequeueResult>(), UnknownEntries = new List <DeviceEntry>() } } ; var dataTable = new DataTable("DeviceIdWithIndexTable"); dataTable.Columns.Add("DeviceId", typeof(long)); dataTable.Columns.Add("Index", typeof(int)); foreach (var item in deviceIds) { var dataRow = dataTable.NewRow(); dataRow[0] = item.DeviceId; dataRow[1] = item.Index ?? -1; // just to avoid NULL check problems in SQL dataTable.Rows.Add(dataRow); } using (var sqlConnection = new SqlConnection(_connectionString)) { sqlConnection.Open(); using (var sqlCommand = new SqlCommand(spName, sqlConnection)) { sqlCommand.CommandType = CommandType.StoredProcedure; sqlCommand.Parameters.Add(new SqlParameter("@DequeueItems", dataTable)); var dequeueResults = new DequeueResults(); var messages = new List <DequeueResult>(); var unknownEntries = new List <DeviceEntry>(); using (var reader = sqlCommand.ExecuteReader()) { var colIsMessage = reader.GetOrdinal("IsMessage"); 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"); var colPayload = reader.GetOrdinal("Payload"); var colTimestamp = reader.GetOrdinal("Timestamp"); var colSenderDeviceId = reader.GetOrdinal("SenderUid"); while (reader.Read()) { if ((int)reader[colIsMessage] == 1) { var dequeueResult = new DequeueResult { Id = (long)reader[colDeviceId], DequeueIndex = (int)reader[colDequeueIndex], EnqueueIndex = (int)reader[colEnqueueIndex], Peek = (bool)reader[colPeek], Version = (int)reader[colVersion], MessageId = (int)reader[colMessageId] }; if (!reader.IsDBNull(colPayload)) { dequeueResult.Payload = (byte[])reader[colPayload]; dequeueResult.Timestamp = (DateTime)reader[colTimestamp]; dequeueResult.SenderDeviceId = (string)reader[colSenderDeviceId]; } messages.Add(dequeueResult); } else { var deviceEntry = new DeviceEntry { Id = (long)reader[colDeviceId], DequeueIndex = (int)reader[colDequeueIndex], EnqueueIndex = (int)reader[colEnqueueIndex], Peek = (bool)reader[colPeek], Version = (int)reader[colVersion] }; unknownEntries.Add(deviceEntry); } } } dequeueResults.Messages = messages; dequeueResults.UnknownEntries = unknownEntries; return(dequeueResults); } } } }
private DequeueResults Retrieve(string spName, IEnumerable <DeviceIdWithOpHint> deviceIds) { if (!deviceIds.Any()) { return new DequeueResults { Messages = new List <DequeueResult>(), UnknownEntries = new List <DeviceEntry>() } } ; using (var connection = new NpgsqlConnection(_connectionString)) { connection.Open(); using (var command = new NpgsqlCommand(spName, connection)) { command.CommandType = CommandType.StoredProcedure; var dequeueItemsJsonSource = deviceIds.Select(item => new RetrieveData { deviceid = item.DeviceId, index = item.Index ?? -1 }).ToList(); var dequeueItems = new NpgsqlParameter("dequeueitemsjson", NpgsqlDbType.Json); dequeueItems.Value = JsonConvert.SerializeObject(dequeueItemsJsonSource); command.Parameters.Add(dequeueItems); var dequeueResults = new DequeueResults(); var messages = new List <DequeueResult>(); var unknownEntries = new List <DeviceEntry>(); using (var reader = command.ExecuteReader()) { var colIsMessage = reader.GetOrdinal("ismessage"); 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"); var colPayload = reader.GetOrdinal("payload"); var colTimestamp = reader.GetOrdinal("timestamp"); var colSenderUid = reader.GetOrdinal("senderuid"); while (reader.Read()) { if ((bool)reader[colIsMessage]) { var dequeueResult = new DequeueResult { Id = (long)reader[colDeviceId], DequeueIndex = (int)reader[colDequeueIndex], EnqueueIndex = (int)reader[colEnqueueIndex], Peek = (bool)reader[colPeek], Version = (int)reader[colVersion], MessageId = (int)reader[colMessageId] }; if (!reader.IsDBNull(colPayload)) { dequeueResult.Payload = Convert.FromBase64String(Encoding.UTF8.GetString((byte[])reader[colPayload])); dequeueResult.Timestamp = (DateTime)reader[colTimestamp]; dequeueResult.SenderDeviceId = (string)reader[colSenderUid]; } messages.Add(dequeueResult); } else { var deviceEntry = new DeviceEntry { Id = (long)reader[colDeviceId], DequeueIndex = (int)reader[colDequeueIndex], EnqueueIndex = (int)reader[colEnqueueIndex], Peek = (bool)reader[colPeek], Version = (int)reader[colVersion] }; unknownEntries.Add(deviceEntry); } } } dequeueResults.Messages = messages; dequeueResults.UnknownEntries = unknownEntries; return(dequeueResults); } } }
public DequeueResults Dequeue(IEnumerable <DeviceIdWithOpHint> deviceIds) { var dequeueResults = new DequeueResults(); var messages = new List <DequeueResult>(); var unknownEntries = new List <DequeueResult>(); lock (_lock) { foreach (var deviceId in deviceIds) { var meta = _messagingMetas[deviceId.DeviceId]; if (meta.DequeueIndex < meta.EnqueueIndex) { int dequeueIdx = meta.DequeueIndex % meta.QueueSize; var queueEntry = _queueEntries[deviceId.DeviceId][dequeueIdx]; var dequeueItem = new DequeueResult { Id = deviceId.DeviceId, MessageId = meta.DequeueIndex, Peek = false, EnqueueIndex = meta.EnqueueIndex }; meta.DequeueIndex++; meta.Version++; dequeueItem.DequeueIndex = meta.DequeueIndex; dequeueItem.Version = meta.Version; if (dequeueItem.MessageId != deviceId.Index) { dequeueItem.Payload = queueEntry.Payload; dequeueItem.Timestamp = queueEntry.Timestamp; dequeueItem.SenderDeviceId = queueEntry.SenderDeviceId; } messages.Add(dequeueItem); } else { if (meta.DequeueIndex == meta.EnqueueIndex) { unknownEntries.Add(new DequeueResult { Id = meta.Id, DequeueIndex = meta.DequeueIndex, EnqueueIndex = meta.EnqueueIndex, Peek = meta.Peek, Version = meta.Version, Payload = null }); } } } dequeueResults.Messages = messages; dequeueResults.UnknownEntries = unknownEntries; return(dequeueResults); } }