|
| 1 | +using BotSharp.Abstraction.Graph; |
| 2 | +using BotSharp.Abstraction.Graph.Options; |
| 3 | +using BotSharp.Abstraction.Infrastructures; |
| 4 | +using Microsoft.Extensions.Logging; |
| 5 | +using System.Text.RegularExpressions; |
| 6 | + |
| 7 | +namespace BotSharp.Plugin.FuzzySharp.Services.DataLoaders; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Loads NER vocabulary / synonyms from a graph database (e.g. Membase) per-call. |
| 11 | +/// Required context parameter: |
| 12 | +/// - "graphId": target graph identifier (non-empty string). |
| 13 | +/// Vocabulary schemas are configured per-tenant under |
| 14 | +/// <c>FuzzySharp:Membase:Tenants:<alias>:Schema</c> (committed in appsettings), |
| 15 | +/// with the tenant's environment-specific <c>GraphId</c> supplied via user-secrets |
| 16 | +/// or appsettings.{Environment}.json. The loader resolves the incoming graphId to a |
| 17 | +/// tenant via the merged configuration. Each label yields |
| 18 | +/// <c>MATCH (n:Label) RETURN n.Property AS text</c>. Synonyms still read the flat |
| 19 | +/// (:Synonym {table, column, term, canonical_form}) schema. |
| 20 | +/// Exposes InvalidateCacheAsync(graphId) so write paths can force a refresh. |
| 21 | +/// </summary> |
| 22 | +public class MembaseNERDataLoader : IEntityDataLoader |
| 23 | +{ |
| 24 | + private const string GraphIdKey = "graphId"; |
| 25 | + private const string CacheKeyPrefix = "fuzzysharp:ner"; |
| 26 | + private const int CacheMinutes = 60; |
| 27 | + private const string GraphDbProvider = "membase"; |
| 28 | + |
| 29 | + private const string SynonymCypher = |
| 30 | + "MATCH (n:Synonym) RETURN n.table AS table, n.column AS column, n.term AS term, n.canonical_form AS canonical_form"; |
| 31 | + |
| 32 | + private static readonly Regex IdentifierRegex = new("^[A-Za-z_][A-Za-z0-9_]*$", RegexOptions.Compiled); |
| 33 | + |
| 34 | + private readonly ILogger<MembaseNERDataLoader> _logger; |
| 35 | + private readonly IEnumerable<IGraphDb> _graphDbs; |
| 36 | + private readonly ICacheService _cache; |
| 37 | + private readonly FuzzySharpSettings _settings; |
| 38 | + |
| 39 | + public MembaseNERDataLoader( |
| 40 | + ILogger<MembaseNERDataLoader> logger, |
| 41 | + IEnumerable<IGraphDb> graphDbs, |
| 42 | + ICacheService cache, |
| 43 | + FuzzySharpSettings settings) |
| 44 | + { |
| 45 | + _logger = logger; |
| 46 | + _graphDbs = graphDbs; |
| 47 | + _cache = cache; |
| 48 | + _settings = settings; |
| 49 | + } |
| 50 | + |
| 51 | + public string Provider => "fuzzy-sharp-membase"; |
| 52 | + |
| 53 | + private static string VocabKey(string graphId) => $"{CacheKeyPrefix}:vocab:{graphId}"; |
| 54 | + private static string SynonymKey(string graphId) => $"{CacheKeyPrefix}:synonym:{graphId}"; |
| 55 | + |
| 56 | + // The parameterless overloads don't make sense for a graph-backed loader. |
| 57 | + // Caller must supply a graphId via EntityDataLoadContext. |
| 58 | + public Task<Dictionary<string, HashSet<string>>> LoadVocabularyAsync() |
| 59 | + => Task.FromResult(new Dictionary<string, HashSet<string>>()); |
| 60 | + |
| 61 | + public Task<Dictionary<string, (string DataSource, string CanonicalForm)>> LoadSynonymMappingAsync() |
| 62 | + => Task.FromResult(new Dictionary<string, (string DataSource, string CanonicalForm)>()); |
| 63 | + |
| 64 | + public Task<Dictionary<string, HashSet<string>>> LoadVocabularyAsync(EntityDataLoadContext ctx) |
| 65 | + { |
| 66 | + if (!TryGetGraphId(ctx, out var graphId)) |
| 67 | + { |
| 68 | + return Task.FromResult(new Dictionary<string, HashSet<string>>()); |
| 69 | + } |
| 70 | + return LoadVocabularyByGraphIdAsync(graphId); |
| 71 | + } |
| 72 | + |
| 73 | + public Task<Dictionary<string, (string DataSource, string CanonicalForm)>> LoadSynonymMappingAsync(EntityDataLoadContext ctx) |
| 74 | + { |
| 75 | + if (!TryGetGraphId(ctx, out var graphId)) |
| 76 | + { |
| 77 | + return Task.FromResult(new Dictionary<string, (string DataSource, string CanonicalForm)>()); |
| 78 | + } |
| 79 | + return LoadSynonymMappingByGraphIdAsync(graphId); |
| 80 | + } |
| 81 | + |
| 82 | + private async Task<Dictionary<string, HashSet<string>>> LoadVocabularyByGraphIdAsync(string graphId) |
| 83 | + { |
| 84 | + var key = VocabKey(graphId); |
| 85 | + var cached = await _cache.GetAsync<Dictionary<string, HashSet<string>>>(key); |
| 86 | + if (cached != null) return cached; |
| 87 | + |
| 88 | + var result = new Dictionary<string, HashSet<string>>(); |
| 89 | + |
| 90 | + var sources = _settings.Membase?.VocabularySources; |
| 91 | + if (sources == null || !sources.TryGetValue(graphId, out var labelMap) || labelMap == null || labelMap.Count == 0) |
| 92 | + { |
| 93 | + _logger.LogWarning($"Skip {Provider}: no vocabulary sources configured for graphId='{graphId}' under FuzzySharp:Membase:VocabularySources."); |
| 94 | + return result; |
| 95 | + } |
| 96 | + |
| 97 | + var graphDb = ResolveGraphDb(); |
| 98 | + if (graphDb == null) return result; |
| 99 | + |
| 100 | + foreach (var (label, fields) in labelMap) |
| 101 | + { |
| 102 | + if (fields == null || fields.Length == 0) continue; |
| 103 | + |
| 104 | + if (!IdentifierRegex.IsMatch(label)) |
| 105 | + { |
| 106 | + _logger.LogWarning($"Skip vocabulary label '{label}' in {Provider}: invalid identifier."); |
| 107 | + continue; |
| 108 | + } |
| 109 | + |
| 110 | + // Build aliased projections: n.prop0 AS f0, n.prop1 AS f1, ... |
| 111 | + // Carry SqlSource alongside so we can key the result dict by SQL "table.column". |
| 112 | + var validFields = new List<(string Alias, string GraphProperty, string SqlSource)>(fields.Length); |
| 113 | + for (var i = 0; i < fields.Length; i++) |
| 114 | + { |
| 115 | + var graphProperty = fields[i].GraphProperty; |
| 116 | + var sqlSource = fields[i].SqlSource; |
| 117 | + if (string.IsNullOrWhiteSpace(graphProperty) || !IdentifierRegex.IsMatch(graphProperty)) |
| 118 | + { |
| 119 | + _logger.LogWarning($"Skip vocabulary field '{label}.{graphProperty}' in {Provider}: invalid identifier."); |
| 120 | + continue; |
| 121 | + } |
| 122 | + if (string.IsNullOrWhiteSpace(sqlSource)) |
| 123 | + { |
| 124 | + _logger.LogWarning($"Skip vocabulary field '{label}.{graphProperty}' in {Provider}: empty SqlSource."); |
| 125 | + continue; |
| 126 | + } |
| 127 | + validFields.Add(($"f{i}", graphProperty, sqlSource)); |
| 128 | + } |
| 129 | + if (validFields.Count == 0) continue; |
| 130 | + |
| 131 | + var projection = string.Join(", ", validFields.Select(f => $"n.{f.GraphProperty} AS {f.Alias}")); |
| 132 | + var cypher = $"MATCH (n:{label}) RETURN {projection}"; |
| 133 | + |
| 134 | + try |
| 135 | + { |
| 136 | + var queryResult = await graphDb.ExecuteQueryAsync(cypher, new GraphQueryExecuteOptions |
| 137 | + { |
| 138 | + GraphId = graphId |
| 139 | + }); |
| 140 | + |
| 141 | + foreach (var (alias, _, sqlSource) in validFields) |
| 142 | + { |
| 143 | + if (!result.TryGetValue(sqlSource, out var set)) |
| 144 | + { |
| 145 | + set = new HashSet<string>(StringComparer.OrdinalIgnoreCase); |
| 146 | + result[sqlSource] = set; |
| 147 | + } |
| 148 | + |
| 149 | + foreach (var row in queryResult?.Values ?? []) |
| 150 | + { |
| 151 | + var text = row.TryGetValue(alias, out var tx) ? tx?.ToString() : null; |
| 152 | + if (string.IsNullOrWhiteSpace(text)) continue; |
| 153 | + set.Add(text!.Trim()); |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + catch (Exception ex) |
| 158 | + { |
| 159 | + _logger.LogError(ex, $"Error loading vocabulary label '{label}' from {Provider} graphId={graphId}"); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + _logger.LogInformation($"Loaded vocabulary from {Provider} graphId={graphId}: {result.Sum(x => x.Value.Count)} terms across {result.Count} sources"); |
| 164 | + await _cache.SetAsync(key, result, TimeSpan.FromMinutes(CacheMinutes)); |
| 165 | + |
| 166 | + return result; |
| 167 | + } |
| 168 | + |
| 169 | + private async Task<Dictionary<string, (string DataSource, string CanonicalForm)>> LoadSynonymMappingByGraphIdAsync(string graphId) |
| 170 | + { |
| 171 | + var key = SynonymKey(graphId); |
| 172 | + var cached = await _cache.GetAsync<Dictionary<string, (string DataSource, string CanonicalForm)>>(key); |
| 173 | + if (cached != null) return cached; |
| 174 | + |
| 175 | + var result = new Dictionary<string, (string DataSource, string CanonicalForm)>(); |
| 176 | + var graphDb = ResolveGraphDb(); |
| 177 | + if (graphDb == null) return result; |
| 178 | + |
| 179 | + try |
| 180 | + { |
| 181 | + var queryResult = await graphDb.ExecuteQueryAsync(SynonymCypher, new GraphQueryExecuteOptions |
| 182 | + { |
| 183 | + GraphId = graphId |
| 184 | + }); |
| 185 | + |
| 186 | + foreach (var row in queryResult?.Values ?? []) |
| 187 | + { |
| 188 | + var term = row.TryGetValue("term", out var t) ? t?.ToString() : null; |
| 189 | + var table = row.TryGetValue("table", out var tb) ? tb?.ToString() : null; |
| 190 | + var column = row.TryGetValue("column", out var co) ? co?.ToString() : null; |
| 191 | + var canonical = row.TryGetValue("canonical_form", out var c) ? c?.ToString() : null; |
| 192 | + if (string.IsNullOrWhiteSpace(term) || string.IsNullOrWhiteSpace(table) || string.IsNullOrWhiteSpace(column) || string.IsNullOrWhiteSpace(canonical)) continue; |
| 193 | + |
| 194 | + var dbPath = $"{table!.Trim()}.{column!.Trim()}"; |
| 195 | + result[term!.Trim().ToLowerInvariant()] = (dbPath, canonical!); |
| 196 | + } |
| 197 | + |
| 198 | + _logger.LogInformation($"Loaded synonym mapping from {Provider} graphId={graphId}: {result.Count} terms"); |
| 199 | + await _cache.SetAsync(key, result, TimeSpan.FromMinutes(CacheMinutes)); |
| 200 | + } |
| 201 | + catch (Exception ex) |
| 202 | + { |
| 203 | + _logger.LogError(ex, $"Error loading synonym mapping from {Provider} graphId={graphId}"); |
| 204 | + } |
| 205 | + |
| 206 | + return result; |
| 207 | + } |
| 208 | + |
| 209 | + public async Task InvalidateCacheAsync(string graphId) |
| 210 | + { |
| 211 | + if (string.IsNullOrWhiteSpace(graphId)) return; |
| 212 | + await _cache.RemoveAsync(VocabKey(graphId)); |
| 213 | + await _cache.RemoveAsync(SynonymKey(graphId)); |
| 214 | + } |
| 215 | + |
| 216 | + private IGraphDb? ResolveGraphDb() |
| 217 | + { |
| 218 | + var graphDb = _graphDbs.FirstOrDefault(x => string.Equals(x.Provider, GraphDbProvider, StringComparison.OrdinalIgnoreCase)); |
| 219 | + if (graphDb == null) |
| 220 | + { |
| 221 | + _logger.LogWarning($"No IGraphDb registered with provider '{GraphDbProvider}'. Skip {Provider}."); |
| 222 | + } |
| 223 | + return graphDb; |
| 224 | + } |
| 225 | + |
| 226 | + private bool TryGetGraphId(EntityDataLoadContext ctx, out string graphId) |
| 227 | + { |
| 228 | + if (ctx.Parameters.TryGetValue(GraphIdKey, out var value) && !string.IsNullOrWhiteSpace(value)) |
| 229 | + { |
| 230 | + graphId = value; |
| 231 | + return true; |
| 232 | + } |
| 233 | + graphId = string.Empty; |
| 234 | + _logger.LogWarning($"Skip {Provider}: '{GraphIdKey}' not provided in EntityDataLoadContext."); |
| 235 | + return false; |
| 236 | + } |
| 237 | +} |
0 commit comments