/// <summary>
        /// Deletes the Lightning key-value pairs identified by the given set of keys, from the specified database.
        /// </summary>
        /// <param name="database">The database.</param>
        /// <param name="keys">The keys that uniquely identify the key-value pairs to be deleted.</param>
        /// <returns></returns>
        public async Task<int> DeleteAsync(string database, IEnumerable<byte[]> keys)
        {
            if (_cancellationToken.IsCancellationRequested)
                return 0;

            var operation = new WriteOperation
            {
                Type = WriteOperationType.Delete,
                Database = database,
                KeyValuePairs = keys.Select(k => new LightningKeyValuePair { Key = k }).ToList() ,
                TaskCompletionSource = new TaskCompletionSource<int>()
            };

            if (!_writeOperationsQueue.IsCompleted)
                _writeOperationsQueue.Add(operation);
            else
                operation.TaskCompletionSource.TrySetCanceled();

            var result = await operation.TaskCompletionSource.Task.ConfigureAwait(false);
            return result;
        }
        /// <summary>
        /// Drops the specified Lightning database.
        /// </summary>
        /// <param name="database">The Lightning database to drop.</param>
        /// <returns></returns>
        public async Task<int> DropAsync(string database)
        {
            if (_cancellationToken.IsCancellationRequested)
                return 0;

            var operation = new WriteOperation
            {
                Type = WriteOperationType.DropDatabase,
                Database = database,               
                TaskCompletionSource = new TaskCompletionSource<int>()
            };

            if (!_writeOperationsQueue.IsCompleted)
                _writeOperationsQueue.Add(operation);
            else
                operation.TaskCompletionSource.TrySetCanceled();
            
            var result = await operation.TaskCompletionSource.Task.ConfigureAwait(false);

            var tryCount = 0;
            while (tryCount < 3)
            {
                tryCount += 1;
                
                LightningDatabase db = null;
                _openDatabases.TryRemove(database, out db);

                await Task.Delay(TimeSpan.FromSeconds(0.5)).ConfigureAwait(false);

                if (!_openDatabases.ContainsKey(database))
                    break; // We are successful
            }

            if (_openDatabases.ContainsKey(database))
                throw new Exception($"Unable to remove the LightningDB database: {database}");

            return result;
        }
        /// <summary>
        /// Updates specified Lightning key-value pairs, in the specified database.
        /// </summary>
        /// <param name="database">The database.</param>
        /// <param name="kvItems">The key-value pairs.</param>
        /// <returns></returns>
        public async Task<int> UpdateAsync(string database, IEnumerable<LightningKeyValuePair> kvItems)
        {
            if (_cancellationToken.IsCancellationRequested)
                return 0;

            var operation = new WriteOperation
            {
                Type = WriteOperationType.Update,
                Database = database,
                KeyValuePairs = kvItems,
                TaskCompletionSource = new TaskCompletionSource<int>()
            };

            if (!_writeOperationsQueue.IsCompleted)
                _writeOperationsQueue.Add(operation);
            else
                operation.TaskCompletionSource.TrySetCanceled();

            var result = await operation.TaskCompletionSource.Task.ConfigureAwait(false);
            return result;
        }