/// <summary> /// Asynchronously deletes all logs in a particular batch /// </summary> /// <param name="channelName">The name of the channel associated with the batch</param> /// <param name="batchId">The batch identifier</param> /// <exception cref="StorageException"/> public Task DeleteLogs(string channelName, string batchId) { return(AddTaskToQueue(() => { try { AppCenterLog.Debug(AppCenterLog.LogTag, $"Deleting logs from storage for channel '{channelName}' with batch id '{batchId}'"); var identifiers = _pendingDbIdentifierGroups[GetFullIdentifier(channelName, batchId)]; _pendingDbIdentifierGroups.Remove(GetFullIdentifier(channelName, batchId)); var deletedIdsMessage = "The IDs for deleting log(s) is/are:"; foreach (var identifier in identifiers) { deletedIdsMessage += "\n\t" + identifier; _pendingDbIdentifiers.Remove(identifier); } AppCenterLog.Debug(AppCenterLog.LogTag, deletedIdsMessage); _storageAdapter.Delete(TableName, ColumnIdName, identifiers.Cast <object>().ToArray()); } catch (KeyNotFoundException e) { throw new StorageException(e); } })); }
/// <summary> /// Asynchronously adds a log to storage /// </summary> /// <param name="channelName">The name of the channel associated with the log</param> /// <param name="log">The log to add</param> /// <exception cref="StorageException"/> public Task PutLog(string channelName, Log log) { return(AddTaskToQueue(() => { var logJsonString = LogSerializer.Serialize(log); var maxSize = _storageAdapter.GetMaxStorageSize(); var logSize = Encoding.UTF8.GetBytes(logJsonString).Length; if (maxSize < 0) { throw new StorageException("Failed to store a log to the database."); } if (maxSize <= logSize) { throw new StorageException($"Log is too large ({logSize} bytes) to store in database. " + $"Current maximum database size is {maxSize} bytes."); } while (true) { try { _storageAdapter.Insert(TableName, new[] { ColumnChannelName, ColumnLogName }, new List <object[]> { new object[] { channelName, logJsonString } }); return; } catch (StorageFullException) { var oldestLog = _storageAdapter.Select(TableName, ColumnChannelName, channelName, string.Empty, null, 1, new string[] { ColumnIdName }); if (oldestLog != null && oldestLog.Count > 0 && oldestLog[0].Length > 0) { _storageAdapter.Delete(TableName, ColumnIdName, oldestLog[0][0]); } else { throw new StorageException("Failed to add a new log. Storage is full and old logs cannot be purged."); } } } })); }
public void Delete(Guid id, Type type) { _storageAdapter.Delete(id, type, _connection, _transaction); }