-
Notifications
You must be signed in to change notification settings - Fork 1
/
LPUDF.cs
206 lines (171 loc) · 10.5 KB
/
LPUDF.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
using Aerospike.Client;
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using System.Xml.Linq;
namespace Aerospike.Database.LINQPadDriver
{
public sealed partial class LPUDF
{
#if NET7_0_OR_GREATER
[GeneratedRegex(@"\s*(?<local>local\s+)?function\s+(?<name>[^ (]+)\s*\((?<params>[^\)]+)\)",
RegexOptions.Compiled | RegexOptions.IgnoreCase)]
static private partial Regex CodeRegEx();
#else
private static readonly Regex codeRegEx = new Regex(@"\s*(?<local>local\s+)?function\s+(?<name>[^ (]+)\s*\((?<params>[^\)]+)\)",
RegexOptions.Compiled | RegexOptions.IgnoreCase);
static private Regex CodeRegEx() => codeRegEx;
#endif
public LPUDF(LPModule module, string code)
{
this.Code = code;
this.Module = module;
if (Client.Log.DebugEnabled())
{
if (code is null)
Client.Log.Debug($"Module {module.Name} Code is null");
else if (code == string.Empty)
Client.Log.Debug($"Module {module.Name} Code is Empty");
else
{
Client.Log.Debug($"Module {module.Name}: {code}");
}
}
var partsMatches = CodeRegEx().Match(code);
this.Name = partsMatches.Groups["name"].Value.Trim();
this.SafeName = Helpers.CheckName(this.Name, "UDF");
this.Params = partsMatches.Groups["params"].Value.Trim();
this.IsLocal = partsMatches.Groups["local"].Success;
}
public LPUDF(LPModule module)
{
this.Module = module;
this.Name = "<Not Found>";
this.SafeName = null;
this.Params = string.Empty;
this.IsNotFound = true;
}
[JsonConstructor]
public LPUDF(string code,
LPModule module,
string name,
string safename,
string Params,
bool islocal)
{
this.Code = code;
this.Module = module;
this.Name = name;
this.SafeName = safename;
this.Params = Params;
this.IsLocal = islocal;
}
/// <summary>
/// It is a local UDF, cannot be accessed by the client
/// </summary>
public bool IsLocal { get; }
/// <summary>
/// Associated module
/// </summary>
public LPModule Module { get; }
/// <summary>
/// The name of the UDF
/// </summary>
public string Name { get; }
/// <summary>
/// The UDF safe name that can be used in C# as a class name or property
/// </summary>
public string SafeName { get; }
/// <summary>
/// The UDF parameters
/// </summary>
public string Params { get; }
/// <summary>
/// The UDF code
/// </summary>
public string Code { get; }
public bool IsNotFound { get; }
#if NET7_0_OR_GREATER
[GeneratedRegex(@"^\s*(local\s+)?function\s",
RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Multiline)]
static private partial Regex FunctionRegEx();
[GeneratedRegex(@"^\s*end\s*$",
RegexOptions.RightToLeft | RegexOptions.Multiline | RegexOptions.Compiled | RegexOptions.IgnoreCase)]
static private partial Regex FunctionEndRegEx();
#else
private static readonly Regex functionRegEx = new Regex(@"^\s*(local\s+)?function\s",
RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Multiline);
static private Regex FunctionRegEx() => functionRegEx;
public static readonly Regex functionEndRegEx = new Regex(@"^\s*end\s*$",
RegexOptions.RightToLeft | RegexOptions.Multiline | RegexOptions.Compiled | RegexOptions.IgnoreCase);
static private Regex FunctionEndRegEx() => functionEndRegEx;
#endif
public static LPUDF[] Create(Connection connection, LPModule module)
{
//type=LUA;content=bG9jYWwgZnVuY3Rpb24gcHV0QmluKHIsbmFtZ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udfget == "error=invalid_filename") return Array.Empty<LPUDF>();
var contentStr = udfget.Substring(udfget.IndexOf("content=") + 8);
contentStr = contentStr.Substring(0, contentStr.Length - 1);
var codeBlock = Encoding.Default.GetString(Convert.FromBase64String(contentStr));
/*
local function putBin(r,name,value)
if not aerospike:exists(r) then aerospike:create(r) end
r[name] = value
aerospike:update(r)
end
-- Set a particular bin
function writeBin(r,name,value)
putBin(r,name,value)
end
*/
static Tuple<string, int> GetFunctionBeginEnd(string codeBlock)
{
var beginMatch = FunctionRegEx().Match(codeBlock);
if (!beginMatch.Success)
{
return null;
}
var searchFuncBlock = codeBlock.Substring(beginMatch.Index + beginMatch.Length);
var nextFuncMatch = FunctionRegEx().Match(searchFuncBlock);
if (!nextFuncMatch.Success)
{
return new Tuple<string, int>(codeBlock, -1);
}
var searchEndCodeBlock = searchFuncBlock.Substring(0, nextFuncMatch.Index);
var fndEndMatch = FunctionEndRegEx().Match(searchEndCodeBlock);
if (!fndEndMatch.Success)
{
return new Tuple<string, int>(codeBlock, -1);
}
//var endBlockReminding = searchEndCodeBlock.Substring(fndEndMatch.Index); //Debugging
var FuncCodeBlock = codeBlock.Substring(0, beginMatch.Index + beginMatch.Length + fndEndMatch.Index + 3);
return new Tuple<string, int>(FuncCodeBlock, FuncCodeBlock.Length);
}
string currentCodeBlock = string.Empty;
char[] trimLineChars = new char[] { ' ', '\t', '\r', '\n' };
var remindingCodeBlock = codeBlock.Trim(trimLineChars);
var udfList = new List<LPUDF>();
while (currentCodeBlock != null)
{
var currentFunc = GetFunctionBeginEnd(remindingCodeBlock);
currentCodeBlock = currentFunc?.Item1.TrimEnd(trimLineChars);
if (currentCodeBlock != null)
{
udfList.Add(new LPUDF(module, currentCodeBlock));
if (currentFunc.Item2 >= 0)
{
remindingCodeBlock = remindingCodeBlock.Substring(currentFunc.Item2).TrimStart(trimLineChars);
}
else
{
currentCodeBlock = null;
}
}
}
return udfList.ToArray();
}
}
}