From ed8a1c98405a3354412edeadef10243ffa45ebcc Mon Sep 17 00:00:00 2001 From: Badrish Chandramouli Date: Wed, 1 Dec 2021 14:40:32 -0800 Subject: [PATCH] update for code consistency --- cs/remote/samples/VarLenServer/Options.cs | 2 +- cs/remote/samples/VarLenServer/Program.cs | 1 + .../samples/VarLenServer/VarLenServer.csproj | 47 ++--- .../src/FASTER.server/PubSub/GlobUtils.cs | 163 ++++++++++++++++++ .../PubSub/SpanByteKeySerializer.cs | 151 +--------------- cs/src/core/Epochs/LightEpoch.cs | 10 ++ 6 files changed, 200 insertions(+), 174 deletions(-) create mode 100644 cs/remote/src/FASTER.server/PubSub/GlobUtils.cs diff --git a/cs/remote/samples/VarLenServer/Options.cs b/cs/remote/samples/VarLenServer/Options.cs index cd61b65c6..460e00dc1 100644 --- a/cs/remote/samples/VarLenServer/Options.cs +++ b/cs/remote/samples/VarLenServer/Options.cs @@ -11,7 +11,7 @@ class Options [Option("port", Required = false, Default = 3278, HelpText = "Port to run server on")] public int Port { get; set; } - [Option("bind", Required = false, Default = "127.0.0.1", HelpText = "IP address to bind server to")] + [Option("bind", Required = false, Default = null, HelpText = "IP address to bind server to (default: any)")] public string Address { get; set; } [Option('m', "memory", Required = false, Default = "16g", HelpText = "Total log memory used in bytes (rounds down to power of 2)")] diff --git a/cs/remote/samples/VarLenServer/Program.cs b/cs/remote/samples/VarLenServer/Program.cs index cc8d44632..cc376bea6 100644 --- a/cs/remote/samples/VarLenServer/Program.cs +++ b/cs/remote/samples/VarLenServer/Program.cs @@ -17,6 +17,7 @@ class Program { static void Main(string[] args) { + Environment.SetEnvironmentVariable("DOTNET_SYSTEM_NET_SOCKETS_INLINE_COMPLETIONS", "1"); Trace.Listeners.Add(new ConsoleTraceListener()); Console.WriteLine("FASTER variable-length KV server"); diff --git a/cs/remote/samples/VarLenServer/VarLenServer.csproj b/cs/remote/samples/VarLenServer/VarLenServer.csproj index 9ea260135..af9a6128c 100644 --- a/cs/remote/samples/VarLenServer/VarLenServer.csproj +++ b/cs/remote/samples/VarLenServer/VarLenServer.csproj @@ -1,31 +1,32 @@  - - Exe - net5.0 - true - AnyCPU;x64 - latest - + + Exe + net5.0 + true + AnyCPU;x64 + latest + true + - - TRACE;DEBUG - full - bin\$(Platform)\Debug\ - + + TRACE;DEBUG + full + bin\$(Platform)\Debug\ + - - TRACE - true - bin\$(Platform)\Release\ - + + TRACE + true + bin\$(Platform)\Release\ + - - - + + + - - - + + + diff --git a/cs/remote/src/FASTER.server/PubSub/GlobUtils.cs b/cs/remote/src/FASTER.server/PubSub/GlobUtils.cs new file mode 100644 index 000000000..903a85797 --- /dev/null +++ b/cs/remote/src/FASTER.server/PubSub/GlobUtils.cs @@ -0,0 +1,163 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. + +namespace FASTER.server +{ + /// + /// Glob utils + /// + public static class GlobUtils + { + /// + /// Glob-style pattern matching + /// + /// Whether match was found + public static unsafe bool Match(byte* pattern, int patternLen, byte* key, int stringLen, bool nocase = false) + { + while (patternLen > 0 && stringLen > 0) + { + switch (pattern[0]) + { + case (byte)'*': + while (patternLen > 0 && pattern[1] == '*') + { + pattern++; + patternLen--; + } + if (patternLen == 1) + return true; /* match */ + while (stringLen > 0) + { + if (Match(pattern + 1, patternLen - 1, key, stringLen, nocase)) + return true; /* match */ + key++; + stringLen--; + } + return false; /* no match */ + + case (byte)'?': + key++; + stringLen--; + break; + + case (byte)'[': + { + bool not, match; + pattern++; + patternLen--; + not = (pattern[0] == '^'); + if (not) + { + pattern++; + patternLen--; + } + match = false; + while (true) + { + if (pattern[0] == '\\' && patternLen >= 2) + { + pattern++; + patternLen--; + if (pattern[0] == key[0]) + match = true; + } + else if (pattern[0] == ']') + { + break; + } + else if (patternLen == 0) + { + pattern--; + patternLen++; + break; + } + else if (patternLen >= 3 && pattern[1] == '-') + { + int start = pattern[0]; + int end = pattern[2]; + int c = key[0]; + if (start > end) + { + int t = start; + start = end; + end = t; + } + if (nocase) + { + start = char.ToLower((char)start); + end = char.ToLower((char)end); + c = char.ToLower((char)c); + } + pattern += 2; + patternLen -= 2; + if (c >= start && c <= end) + match = true; + } + else + { + if (!nocase) + { + if (pattern[0] == key[0]) + match = true; + } + else + { + if (char.ToLower((char)pattern[0]) == char.ToLower((char)key[0])) + match = true; + } + } + pattern++; + patternLen--; + } + + if (not) + match = !match; + if (!match) + return false; /* no match */ + key++; + stringLen--; + break; + } + + case (byte)'\\': + if (patternLen >= 2) + { + pattern++; + patternLen--; + } + goto default; + + /* fall through */ + default: + if (!nocase) + { + if (pattern[0] != key[0]) + return false; /* no match */ + } + else + { + if (char.ToLower((char)pattern[0]) != char.ToLower((char)key[0])) + return false; /* no match */ + } + key++; + stringLen--; + break; + } + pattern++; + patternLen--; + if (stringLen == 0) + { + while (*pattern == '*') + { + pattern++; + patternLen--; + } + break; + } + } + if (patternLen == 0 && stringLen == 0) + return true; + return false; + } + } +} diff --git a/cs/remote/src/FASTER.server/PubSub/SpanByteKeySerializer.cs b/cs/remote/src/FASTER.server/PubSub/SpanByteKeySerializer.cs index 6dcf516cb..608f077a6 100644 --- a/cs/remote/src/FASTER.server/PubSub/SpanByteKeySerializer.cs +++ b/cs/remote/src/FASTER.server/PubSub/SpanByteKeySerializer.cs @@ -46,161 +46,12 @@ public bool Match(ref SpanByte k, bool asciiKey, ref SpanByte pattern, bool asci { if (asciiKey && asciiPattern) { - return GlobMatching(pattern.ToPointer(), pattern.LengthWithoutMetadata, k.ToPointer(), k.LengthWithoutMetadata); + return GlobUtils.Match(pattern.ToPointer(), pattern.LengthWithoutMetadata, k.ToPointer(), k.LengthWithoutMetadata); } if (pattern.LengthWithoutMetadata > k.LengthWithoutMetadata) return false; return pattern.AsReadOnlySpan().SequenceEqual(k.AsReadOnlySpan().Slice(0, pattern.LengthWithoutMetadata)); } - - /* Glob-style pattern matching. */ - private static bool GlobMatching(byte* pattern, int patternLen, byte* key, int stringLen, bool nocase = false) - { - while (patternLen > 0 && stringLen > 0) - { - switch (pattern[0]) - { - case (byte)'*': - while (patternLen > 0 && pattern[1] == '*') - { - pattern++; - patternLen--; - } - if (patternLen == 1) - return true; /* match */ - while (stringLen > 0) - { - if (GlobMatching(pattern + 1, patternLen - 1, key, stringLen, nocase)) - return true; /* match */ - key++; - stringLen--; - } - return false; /* no match */ - - case (byte)'?': - key++; - stringLen--; - break; - - case (byte)'[': - { - bool not, match; - pattern++; - patternLen--; - not = (pattern[0] == '^'); - if (not) - { - pattern++; - patternLen--; - } - match = false; - while (true) - { - if (pattern[0] == '\\' && patternLen >= 2) - { - pattern++; - patternLen--; - if (pattern[0] == key[0]) - match = true; - } - else if (pattern[0] == ']') - { - break; - } - else if (patternLen == 0) - { - pattern--; - patternLen++; - break; - } - else if (patternLen >= 3 && pattern[1] == '-') - { - int start = pattern[0]; - int end = pattern[2]; - int c = key[0]; - if (start > end) - { - int t = start; - start = end; - end = t; - } - if (nocase) - { - start = char.ToLower((char)start); - end = char.ToLower((char)end); - c = char.ToLower((char)c); - } - pattern += 2; - patternLen -= 2; - if (c >= start && c <= end) - match = true; - } - else - { - if (!nocase) - { - if (pattern[0] == key[0]) - match = true; - } - else - { - if (char.ToLower((char)pattern[0]) == char.ToLower((char)key[0])) - match = true; - } - } - pattern++; - patternLen--; - } - - if (not) - match = !match; - if (!match) - return false; /* no match */ - key++; - stringLen--; - break; - } - - case (byte)'\\': - if (patternLen >= 2) - { - pattern++; - patternLen--; - } - goto default; - - /* fall through */ - default: - if (!nocase) - { - if (pattern[0] != key[0]) - return false; /* no match */ - } - else - { - if (char.ToLower((char)pattern[0]) != char.ToLower((char)key[0])) - return false; /* no match */ - } - key++; - stringLen--; - break; - } - pattern++; - patternLen--; - if (stringLen == 0) - { - while (*pattern == '*') - { - pattern++; - patternLen--; - } - break; - } - } - if (patternLen == 0 && stringLen == 0) - return true; - return false; - } } } \ No newline at end of file diff --git a/cs/src/core/Epochs/LightEpoch.cs b/cs/src/core/Epochs/LightEpoch.cs index d62cb5be9..03314b49e 100644 --- a/cs/src/core/Epochs/LightEpoch.cs +++ b/cs/src/core/Epochs/LightEpoch.cs @@ -285,6 +285,16 @@ public void Resume() ProtectAndDrain(); } + /// + /// Thread resumes its epoch entry + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Resume(out int resumeEpoch) + { + Acquire(); + resumeEpoch = ProtectAndDrain(); + } + /// /// Increment global current epoch ///