-
-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Parse large JSON module dictionaries in parallel
- Loading branch information
Showing
11 changed files
with
216 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Collections.Generic; | ||
using System.Collections.Concurrent; | ||
|
||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
|
||
using CKAN.Extensions; | ||
|
||
namespace CKAN | ||
{ | ||
/// <summary> | ||
/// A converter that loads a dictionary in parallel, | ||
/// use with large collections of complex objects for a speed boost | ||
/// </summary> | ||
public class JsonParallelDictionaryConverter<V> : JsonConverter | ||
{ | ||
public override object ReadJson(JsonReader reader, | ||
Type objectType, | ||
object existingValue, | ||
JsonSerializer serializer) | ||
=> ParseWithProgress(JObject.Load(reader) | ||
.Properties() | ||
.ToArray(), | ||
serializer); | ||
|
||
private object ParseWithProgress(JProperty[] properties, | ||
JsonSerializer serializer) | ||
=> Partitioner.Create(properties, true) | ||
.AsParallel() | ||
.WithMergeOptions(ParallelMergeOptions.NotBuffered) | ||
.Select(prop => new KeyValuePair<string, V>( | ||
prop.Name, | ||
prop.Value.ToObject<V>())) | ||
.WithProgress(properties.Length, | ||
serializer.Context.Context as IProgress<int>) | ||
.ToDictionary(); | ||
|
||
public override bool CanWrite => false; | ||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
// Only convert when we're an explicit attribute | ||
public override bool CanConvert(Type object_type) => false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System; | ||
|
||
namespace CKAN | ||
{ | ||
public class ProgressImmediate<T> : IProgress<T> | ||
{ | ||
public ProgressImmediate(Action<T> action) | ||
{ | ||
this.action = action; | ||
} | ||
|
||
public void Report(T val) | ||
{ | ||
action(val); | ||
} | ||
|
||
private readonly Action<T> action; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace CKAN | ||
{ | ||
/// <summary> | ||
/// Accepts progress updates in terms of percentage of one file within a group | ||
/// and translates them into percentages across the whole operation. | ||
/// </summary> | ||
public class ProgressScalePercentsByFileSizes : IProgress<int> | ||
{ | ||
/// <summary> | ||
/// Initialize an percent-to-scaled-percent progress translator | ||
/// </summary> | ||
/// <param name="percentProgress">The upstream progress object expecting percentages</param> | ||
/// <param name="sizes">Sequence of sizes of files in our group</param> | ||
public ProgressScalePercentsByFileSizes(IProgress<int> percentProgress, | ||
IEnumerable<long> sizes) | ||
{ | ||
this.percentProgress = percentProgress; | ||
this.sizes = sizes.ToArray(); | ||
totalSize = this.sizes.Sum(); | ||
} | ||
|
||
/// <summary> | ||
/// The IProgress member called when we advance within the current file | ||
/// </summary> | ||
/// <param name="currentFilePercent">How far into the current file we are</param> | ||
public void Report(int currentFilePercent) | ||
{ | ||
if (basePercent < 100 && currentIndex < sizes.Length) | ||
{ | ||
var percent = basePercent + (int)(currentFilePercent * sizes[currentIndex] / totalSize); | ||
// Only report each percentage once, to avoid spamming UI calls | ||
if (percent > lastPercent) | ||
{ | ||
percentProgress.Report(percent); | ||
lastPercent = percent; | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Call this when you move on from one file to the next | ||
/// </summary> | ||
public void NextFile() | ||
{ | ||
doneSize += sizes[currentIndex]; | ||
basePercent = (int)(100 * doneSize / totalSize); | ||
++currentIndex; | ||
if (basePercent > lastPercent) | ||
{ | ||
percentProgress.Report(basePercent); | ||
lastPercent = basePercent; | ||
} | ||
} | ||
|
||
private IProgress<int> percentProgress; | ||
private long[] sizes; | ||
private long totalSize; | ||
private long doneSize = 0; | ||
private int currentIndex = 0; | ||
private int basePercent = 0; | ||
private int lastPercent = -1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters