Skip to content

Commit

Permalink
Remove all locked collections. Use Concurrent now. New string extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
grandsilence committed May 11, 2018
1 parent 4a5b020 commit 3a20b20
Show file tree
Hide file tree
Showing 40 changed files with 1,091 additions and 953 deletions.
35 changes: 0 additions & 35 deletions Collections/Generic/CollectionRandomizer.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
using System.IO;
using System.Threading.Tasks;
using System.Collections.Concurrent;
using Leaf.Core.Runtime.Serialization;
using Leaf.Core.Threading;

namespace Leaf.Core.Collections.Generic
{
/// <summary>
/// Фабрика потокобезопасных коллекций.
/// Используйте её когда требуется инициализировать потокобезопасную коллекцию из файла.
/// </summary>
public static class LockedFactory
public static class ConcurrentFactory
{
#region # Public Synchronous Methods

Expand All @@ -22,11 +24,11 @@ public static class LockedFactory
/// <param name="includeComments">Если true, то строки с коментариями тоже будут включены в выборку для десериалиализации.</param>
/// <param name="trim">Очищать начало и конец строк от отступов и пробелов.</param>
/// <returns>Возвращает новый потокобезопасный список объектов</returns>
public static LockedList<T> ListFromFile<T>(string filePath, ThreadSafeUI ui = null,
public static ConcurrentBag<T> BagFromFile<T>(string filePath, ThreadSafeUI ui = null,
bool includeComments = false, bool trim = true)
where T : IStringSerializeable, new()
{
var result = new LockedList<T>();
var result = new ConcurrentBag<T>();
ReadAndDeserialize(result, filePath, ui, includeComments, trim);
return result;
}
Expand All @@ -39,11 +41,11 @@ public static LockedList<T> ListFromFile<T>(string filePath, ThreadSafeUI ui = n
/// <param name="includeComments">Если true, то строки с коментариями тоже будут включены в выборку для десериалиализации.</param>
/// <param name="trim">Очищать начало и конец строк от отступов и пробелов.</param>
/// <returns>Возвращает новую потокобезопасную очередь объектов</returns>
public static LockedQueue<T> QueueFromFile<T>(string filePath, ThreadSafeUI ui = null,
public static ConcurrentQueue<T> QueueFromFile<T>(string filePath, ThreadSafeUI ui = null,
bool includeComments = false, bool trim = true)
where T : IStringSerializeable, new()
{
var result = new LockedQueue<T>();
var result = new ConcurrentQueue<T>();
ReadAndDeserialize(result, filePath, ui, includeComments, trim);
return result;
}
Expand All @@ -52,14 +54,14 @@ public static LockedQueue<T> QueueFromFile<T>(string filePath, ThreadSafeUI ui =

#region ## String

/// <inheritdoc cref="ListFromFile{T}"/>
/// <inheritdoc cref="BagFromFile{T}"/>
/// <summary>
/// Создаёт потокобезопасный список строк из текстового файла. Десериализация проводится построчно.
/// </summary>
/// <returns>Возвращает новый потокобезопасный список строк</returns>
public static LockedList ListFromFile(string filePath, bool includeComments = false, bool trim = true)
public static ConcurrentBag<string> BagFromFile(string filePath, bool includeComments = false, bool trim = true)
{
var result = new LockedList();
var result = new ConcurrentBag<string>();
ReadAndAppend(result, filePath, includeComments, trim);
return result;
}
Expand All @@ -69,9 +71,9 @@ public static LockedList ListFromFile(string filePath, bool includeComments = fa
/// Создаёт потокобезопасную очередь строк из текстового файла. Десериализация проводится построчно.
/// </summary>
/// <returns>Возвращает новую потокобезопасную очередь строк</returns>
public static LockedQueue QueueFromFile(string filePath, bool includeComments = false, bool trim = true)
public static ConcurrentQueue<string> QueueFromFile(string filePath, bool includeComments = false, bool trim = true)
{
var result = new LockedQueue();
var result = new ConcurrentQueue<string>();
ReadAndAppend(result, filePath, includeComments, trim);
return result;
}
Expand All @@ -84,22 +86,22 @@ public static LockedQueue QueueFromFile(string filePath, bool includeComments =

#region ## Generic

/// <inheritdoc cref="ListFromFile{T}"/>
/// <inheritdoc cref="BagFromFile{T}"/>
/// <summary>
/// Асинхронно создаёт потокобезопасный список объектов из текстового файла. Десериализация проводится построчно.
/// </summary>
public static async Task<LockedList<T>> ListFromFileAsync<T>(string filePath, ThreadSafeUI ui = null,
public static async Task<ConcurrentBag<T>> BagFromFileAsync<T>(string filePath, ThreadSafeUI ui = null,
bool includeComments = false, bool trim = true)
where T : IStringSerializeable, new()
{
return await Task.Run(() => ListFromFile<T>(filePath, ui, includeComments, trim));
return await Task.Run(() => BagFromFile<T>(filePath, ui, includeComments, trim));
}

/// <inheritdoc cref="QueueFromFile{T}"/>
/// <summary>
/// Асинхронно создаёт потокобезопасную очередь объектов из текстового файла. Десериализация проводится построчно.
/// </summary>
public static async Task<LockedQueue<T>> QueueFromFileAsync<T>(string filePath, ThreadSafeUI ui = null,
public static async Task<ConcurrentQueue<T>> QueueFromFileAsync<T>(string filePath, ThreadSafeUI ui = null,
bool includeComments = false, bool trim = true)
where T : IStringSerializeable, new()
{
Expand All @@ -110,21 +112,21 @@ public static async Task<LockedQueue<T>> QueueFromFileAsync<T>(string filePath,

#region ## String

/// <inheritdoc cref="ListFromFile"/>
/// <inheritdoc cref="BagFromFile"/>
/// <summary>
/// Асинхронно создаёт потокобезопасный список строк из текстового файла. Десериализация проводится построчно.
/// </summary>
public static async Task<LockedList> ListFromFileAsync(string filePath,
public static async Task<ConcurrentBag<string>> BagFromFileAsync(string filePath,
bool includeComments = false, bool trim = true)
{
return await Task.Run(() => ListFromFile(filePath, includeComments, trim));
return await Task.Run(() => BagFromFile(filePath, includeComments, trim));
}

/// <inheritdoc cref="QueueFromFile"/>
/// <summary>
/// Асинхронно создаёт потокобезопасную очередь строк из текстового файла. Десериализация проводится построчно.
/// </summary>
public static async Task<LockedQueue> QueueFromFileAsync(string filePath,
public static async Task<ConcurrentQueue<string>> QueueFromFileAsync(string filePath,
bool includeComments = false, bool trim = true)
{
return await Task.Run(() => QueueFromFile(filePath, includeComments, trim));
Expand Down Expand Up @@ -167,25 +169,30 @@ private static void ReadFileLineByLine(string filePath, bool includeComments, bo
}
}

private static void ReadAndDeserialize<T>(LockedCollection<T> collection, string filePath, ThreadSafeUI ui,
private static void ReadAndDeserialize<T>(IProducerConsumerCollection<T> collection, string filePath, ThreadSafeUI ui,
bool includeComments, bool trim)
where T : IStringSerializeable, new()
{
ReadFileLineByLine(filePath, includeComments, trim, (lineNumber, line) => {
// Десериализуем объект из строки
var item = new T();
if (item.DeserializeFromString(line))
collection.AppendItem(item);
if (item.DeserializeFromString(line)) {

if (!collection.TryAdd(item))
throw new IOException("Unable to ReadAndAppend to ConcurrentCollection.");
}
else
ui?.Log($"Пропускаю, неверная запись объекта {typeof(T).Name}. Строка #{lineNumber}: {line}");
});
}

private static void ReadAndAppend(LockedCollection<string> collection, string filePath,
private static void ReadAndAppend(IProducerConsumerCollection<string> collection, string filePath,
bool includeComments, bool trim)
{
ReadFileLineByLine(filePath, includeComments, trim, (lineNumber, line) => {
collection.AppendItem(line);

if (!collection.TryAdd(line))
throw new IOException("Unable to ReadAndAppend to ConcurrentCollection.");
});
}
#endregion
Expand Down
31 changes: 0 additions & 31 deletions Collections/Generic/IStorage.cs

This file was deleted.

50 changes: 0 additions & 50 deletions Collections/Generic/ListStorage.cs

This file was deleted.

70 changes: 0 additions & 70 deletions Collections/Generic/LockedCollection.cs

This file was deleted.

Loading

0 comments on commit 3a20b20

Please sign in to comment.