Skip to content

Commit

Permalink
feat(cli): support key suffix (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
catcherwong authored Jul 25, 2023
1 parent 235947f commit 1d3105c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 14 deletions.
2 changes: 1 addition & 1 deletion build/version.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>

<RDBParserVersion>0.6.0</RDBParserVersion>
<RDBCliVersion>0.7.5</RDBCliVersion>
<RDBCliVersion>0.8.0</RDBCliVersion>

</PropertyGroup>
</Project>
17 changes: 16 additions & 1 deletion src/RDBCli/Commands/MemoryCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ internal class MemoryCommand : Command
private static Option<int> _sepPrefixCountOption = CommonCLIOptions.SepPrefixCountOption();
private static Option<bool?> _isPermanentOption = CommonCLIOptions.IsPermanentOption();
private static Option<bool?> _isIgnoreFieldOfLargestElemOption = CommonCLIOptions.IsIgnoreFieldOfLargestElemOption();
private static Option<bool?> _keySuffixEnableOption = CommonCLIOptions.KeySuffixEnableOption();
private static Argument<string> _fileArg = CommonCLIArguments.FileArgument();

public MemoryCommand()
Expand All @@ -38,6 +39,7 @@ public MemoryCommand()
this.AddOption(_sepPrefixCountOption);
this.AddOption(_isPermanentOption);
this.AddOption(_isIgnoreFieldOfLargestElemOption);
this.AddOption(_keySuffixEnableOption);
this.AddArgument(_fileArg);

this.SetHandler((InvocationContext context) =>
Expand All @@ -54,7 +56,7 @@ private void Do(InvocationContext context, CommandOptions options)
var cb = new CliCB.MemoryCallback(options.IsIgnoreFole ?? false);
var rdbDataInfo = cb.GetRdbDataInfo();

var counter = new RdbDataCounter(rdbDataInfo.Records, options.Separators, options.SepPrefixCount);
var counter = new RdbDataCounter(rdbDataInfo.Records, options.Separators, options.SepPrefixCount, options.keySuffixEnable ?? false);
var task = counter.Count();

console.WriteLine($"");
Expand Down Expand Up @@ -159,6 +161,7 @@ private class CommandOptions
public string Separators { get; set; }
public int SepPrefixCount { get; set; }
public bool? IsIgnoreFole { get; set; }
public bool? keySuffixEnable { get; set; }


public static CommandOptions FromContext(InvocationContext context)
Expand All @@ -175,6 +178,7 @@ public static CommandOptions FromContext(InvocationContext context)
var sepPrefixCount = context.ParseResult.GetValueForOption<int>(_sepPrefixCountOption);
var isPermanent = context.ParseResult.GetValueForOption<bool?>(_isPermanentOption);
var isIgnoreFole = context.ParseResult.GetValueForOption<bool?>(_isIgnoreFieldOfLargestElemOption);
var keySuffixEnable = context.ParseResult.GetValueForOption<bool?>(_keySuffixEnableOption);

var parseFilter = new RDBParser.ParserFilter()
{
Expand All @@ -195,6 +199,7 @@ public static CommandOptions FromContext(InvocationContext context)
Separators = sep,
SepPrefixCount = sepPrefixCount,
IsIgnoreFole = isIgnoreFole,
keySuffixEnable = keySuffixEnable,
};
}
}
Expand Down Expand Up @@ -338,6 +343,16 @@ public static Option<List<string>> KeyPrefixesOption()
return option;
}

public static Option<bool?> KeySuffixEnableOption()
{
Option<bool?> option =
new Option<bool?>(
aliases: new string[] { "--key-suffix-enable" },
description: "Use the key suffix as the key prefix.");

return option;
}

public static Option<string> SeparatorsOption()
{
Option<string> option =
Expand Down
50 changes: 38 additions & 12 deletions src/RDBCli/Stats/RdbDataCounter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ internal class RdbDataCounter

private readonly BlockingCollection<AnalysisRecord> _records;
private readonly int _sepCount;
private readonly bool _keySuffixEnable;

public RdbDataCounter(BlockingCollection<AnalysisRecord> records, string separators = "", int sepCount = -1)
public RdbDataCounter(BlockingCollection<AnalysisRecord> records, string separators = "", int sepCount = -1, bool keySuffixEnable = false)
{
this._records = records;
this._largestRecords = new PriorityQueue<Record, ulong>();
Expand All @@ -36,11 +37,12 @@ public RdbDataCounter(BlockingCollection<AnalysisRecord> records, string separat
}

_sepCount = sepCount > 0 ? sepCount : 1;
_keySuffixEnable = keySuffixEnable;
}

public Task Count()
{
System.Threading.CancellationTokenSource cts = new System.Threading.CancellationTokenSource();
System.Threading.CancellationTokenSource cts = new();
var task = Task.Factory.StartNew(() =>
{
while (!_records.IsCompleted)
Expand Down Expand Up @@ -192,23 +194,47 @@ private List<string> GetPrefixes(string s)

var span = s.AsSpan();

var sepIdx = span.IndexOfAny(_separators);
if (!_keySuffixEnable)
{
var sepIdx = span.IndexOfAny(_separators);

if (sepIdx < 0) res.Add(s);

while (sepIdx > -1)
{
var str = new string(span[..(sepIdx + 1)]);

if (sepIdx < 0) res.Add(s);
if (res.Any())
{
str = string.Concat(res[^1], str);
}

while (sepIdx > -1)
res.Add(str);

span = span[(sepIdx + 1)..];
sepIdx = span.IndexOfAny(_separators);
}
}
else
{
var str = new string(span[..(sepIdx + 1)]);
var sepIdx = span.LastIndexOfAny(_separators);

if (res.Any())
if (sepIdx < 0) res.Add(s);

while (sepIdx > -1)
{
str = string.Concat(res[^1], str);
}
var str = new string(span[(sepIdx + 1)..]) + span[sepIdx];

res.Add(str);
if (res.Any())
{
str = string.Concat(res[^1], str);
}

span = span[(sepIdx + 1)..];
sepIdx = span.IndexOfAny(_separators);
res.Add(str);

span = span[..sepIdx];
sepIdx = span.LastIndexOfAny(_separators);
}
}

for (int i = 0; i < res.Count; i++)
Expand Down

0 comments on commit 1d3105c

Please sign in to comment.