Skip to content

Commit

Permalink
support filter by if a key is expired (#55)
Browse files Browse the repository at this point in the history
Signed-off-by: catcherwong <catcher_hwq@outlook.com>
  • Loading branch information
catcherwong authored Apr 14, 2024
1 parent df3cfc4 commit cee47c6
Show file tree
Hide file tree
Showing 9 changed files with 427 additions and 370 deletions.
4 changes: 2 additions & 2 deletions build/version.props
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project>
<PropertyGroup>

<RDBParserVersion>0.7.0</RDBParserVersion>
<RDBCliVersion>0.8.2</RDBCliVersion>
<RDBParserVersion>0.7.1</RDBParserVersion>
<RDBCliVersion>0.8.3</RDBCliVersion>

</PropertyGroup>
</Project>
15 changes: 15 additions & 0 deletions src/RDBCli/Commands/CommonCLIArguments.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.CommandLine;

namespace RDBCli.Commands
{
internal static class CommonCLIArguments
{
public static Argument<string> FileArgument()
{
Argument<string> arg =
new Argument<string>("file", "The path of rdb file.");

return arg;
}
}
}
176 changes: 176 additions & 0 deletions src/RDBCli/Commands/CommonCLIOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
using System.Collections.Generic;
using System.CommandLine;

namespace RDBCli.Commands
{
internal static class CommonCLIOptions
{
public static Option<string> OutputOption()
{
Option<string> option =
new Option<string>(
aliases: new string[] { "--output", "-o" },
description: "The output path of parsing result.")
.LegalFilePathsOnly();

return option;
}

public static Option<string> OutputTypeOption()
{
Option<string> option =
new Option<string>(
aliases: new string[] { "--output-type", "-ot" },
getDefaultValue: () => "json",
description: "The output type of parsing result.")
.FromAmong("json", "html", "csv");

return option;
}

public static Option<int> TopPrefixCountOption()
{
Option<int> option =
new Option<int>(
aliases: new string[] { "--top-prefixes", "-tp" },
getDefaultValue: () => 50,
description: "The number of top key prefixes.");

option.AddValidator(x =>
{
var c = x.GetValueOrDefault<int>();
if (c > 200) x.ErrorMessage = "The number can not greater than 200!!";
});

return option;
}

public static Option<int> TopBigKeyCountOption()
{
Option<int> option =
new Option<int>(
aliases: new string[] { "--top-bigkeys", "-tb" },
getDefaultValue: () => 50,
description: "The number of top big keys.");

option.AddValidator(x =>
{
var c = x.GetValueOrDefault<int>();
if (c > 200) x.ErrorMessage = "The number can not greater than 200!!";
});

return option;
}

public static Option<List<int>> DBsOption()
{
Option<List<int>> option =
new Option<List<int>>(
aliases: new string[] { "--db" },
description: "The filter of redis databases.");

return option;
}

public static Option<List<string>> TypesOption()
{
Option<List<string>> option =
new Option<List<string>>(
aliases: new string[] { "--type" },
description: "The filter of redis types.")
.FromAmong("string", "list", "set", "sortedset", "hash", "module", "stream");

return option;
}

public static Option<List<string>> KeyPrefixesOption()
{
Option<List<string>> option =
new Option<List<string>>(
aliases: new string[] { "--key-prefix" },
description: "The filter of redis key prefix.");

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 =
new Option<string>(
aliases: new string[] { "--separators" },
description: "The separators of redis key prefix.");

return option;
}

public static Option<int> SepPrefixCountOption()
{
Option<int> option =
new Option<int>(
aliases: new string[] { "--sep-count" },
description: "The count of separating a key to prefix.");

return option;
}

public static Option<bool?> IsPermanentOption()
{
Option<bool?> option =
new Option<bool?>(
aliases: new string[] { "--permanent" },
description: "Whether the key is permanent.");

return option;
}

public static Option<bool?> IsExpiredOption()
{
Option<bool?> option =
new Option<bool?>(
aliases: new string[] { "--expired" },
description: "Whether the key is expired.");

return option;
}

public static Option<bool?> IsIgnoreFieldOfLargestElemOption()
{
Option<bool?> option =
new Option<bool?>(
aliases: new string[] { "--ignore-fole" },
description: "Whether ignore the field of largest elem.");

return option;
}

public static Option<ulong?> MinIdleOption()
{
Option<ulong?> option =
new Option<ulong?>(
aliases: new string[] { "--min-idle" },
description: "The minimum idle time of the key(must lru policy)");

return option;
}

public static Option<int?> MinFreqOption()
{
Option<int?> option =
new Option<int?>(
aliases: new string[] { "--min-freq" },
description: "The minimum frequency of the key(must lfu policy)");

return option;
}
}
}
22 changes: 15 additions & 7 deletions src/RDBCli/Commands/CsvCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ internal class CsvCommand : Command
private static Option<List<int>> _databasesOption = CommonCLIOptions.DBsOption();
private static Option<List<string>> _typesOption = CommonCLIOptions.TypesOption();
private static Option<List<string>> _keyPrefixesOption = CommonCLIOptions.KeyPrefixesOption();
private static Option<ulong> _minIdleOption = CommonCLIOptions.MinIdleOption();
private static Option<int> _minFreqOption = CommonCLIOptions.MinFreqOption();
private static Option<ulong?> _minIdleOption = CommonCLIOptions.MinIdleOption();
private static Option<int?> _minFreqOption = CommonCLIOptions.MinFreqOption();
private static Option<bool?> _permanentOption = CommonCLIOptions.IsPermanentOption();
private static Option<bool?> _expiredOption = CommonCLIOptions.IsExpiredOption();
private static Argument<string> _fileArg = CommonCLIArguments.FileArgument();

public CsvCommand()
Expand All @@ -30,6 +32,8 @@ public CsvCommand()
this.AddOption(_keyPrefixesOption);
this.AddOption(_minIdleOption);
this.AddOption(_minFreqOption);
this.AddOption(_permanentOption);
this.AddOption(_expiredOption);
this.AddArgument(_fileArg);

this.SetHandler((InvocationContext context) =>
Expand Down Expand Up @@ -80,16 +84,20 @@ public static CommandOptions FromContext(InvocationContext context)
var databases = context.ParseResult.GetValueForOption<List<int>>(_databasesOption);
var types = context.ParseResult.GetValueForOption<List<string>>(_typesOption);
var keyPrefixes = context.ParseResult.GetValueForOption<List<string>>(_keyPrefixesOption);
var minIdle = context.ParseResult.GetValueForOption<ulong>(_minIdleOption);
var minFreq = context.ParseResult.GetValueForOption<int>(_minFreqOption);
var minIdle = context.ParseResult.GetValueForOption<ulong?>(_minIdleOption);
var minFreq = context.ParseResult.GetValueForOption<int?>(_minFreqOption);
var permanent = context.ParseResult.GetValueForOption<bool?>(_permanentOption);
var expired = context.ParseResult.GetValueForOption<bool?>(_expiredOption);

var parseFilter = new RDBParser.ParserFilter()
{
Databases = databases,
Types = types,
KeyPrefixes = keyPrefixes,
MinFreq = minFreq,
MinIdle = minIdle
MinIdle = minIdle,
IsPermanent = permanent,
IsExpired = expired
};

return new CommandOptions
Expand Down Expand Up @@ -119,12 +127,12 @@ public Task<string> Output(string output)
if(output.EndsWith(".csv", StringComparison.OrdinalIgnoreCase))
{
var dir = Path.GetDirectoryName(output);
if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
path = output;
}
else
{
if (!Directory.Exists(output)) Directory.CreateDirectory(output);
if (!Directory.Exists(output)) Directory.CreateDirectory(output);
path = Path.Combine(output, $"dump.csv");
}
}
Expand Down
Loading

0 comments on commit cee47c6

Please sign in to comment.