2022-03-15 18:00:53 +00:00
|
|
|
|
// (c) Copyright Ascensio System SIA 2010-2022
|
|
|
|
|
//
|
|
|
|
|
// This program is a free software product.
|
|
|
|
|
// You can redistribute it and/or modify it under the terms
|
|
|
|
|
// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software
|
|
|
|
|
// Foundation. In accordance with Section 7(a) of the GNU AGPL its Section 15 shall be amended
|
|
|
|
|
// to the effect that Ascensio System SIA expressly excludes the warranty of non-infringement of
|
|
|
|
|
// any third-party rights.
|
|
|
|
|
//
|
|
|
|
|
// This program is distributed WITHOUT ANY WARRANTY, without even the implied warranty
|
|
|
|
|
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For details, see
|
|
|
|
|
// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
|
|
|
|
|
//
|
|
|
|
|
// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021.
|
|
|
|
|
//
|
|
|
|
|
// The interactive user interfaces in modified source and object code versions of the Program must
|
|
|
|
|
// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
|
|
|
|
|
//
|
|
|
|
|
// Pursuant to Section 7(b) of the License you must retain the original Product logo when
|
|
|
|
|
// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under
|
|
|
|
|
// trademark law for use of our trademarks.
|
|
|
|
|
//
|
|
|
|
|
// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing
|
|
|
|
|
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
|
|
|
|
|
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
|
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
namespace ASC.ElasticSearch;
|
|
|
|
|
|
|
|
|
|
public enum UpdateAction
|
2020-10-19 15:53:15 +00:00
|
|
|
|
{
|
2022-02-10 13:45:41 +00:00
|
|
|
|
Add,
|
|
|
|
|
Replace,
|
|
|
|
|
Remove
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Singletone]
|
|
|
|
|
public class BaseIndexerHelper
|
2020-10-19 15:53:15 +00:00
|
|
|
|
{
|
2022-02-10 13:45:41 +00:00
|
|
|
|
public ConcurrentDictionary<string, bool> IsExist { get; set; }
|
2020-01-24 13:07:51 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
private readonly ICacheNotify<ClearIndexAction> _notify;
|
|
|
|
|
|
|
|
|
|
public BaseIndexerHelper(ICacheNotify<ClearIndexAction> cacheNotify)
|
|
|
|
|
{
|
|
|
|
|
IsExist = new ConcurrentDictionary<string, bool>();
|
|
|
|
|
_notify = cacheNotify;
|
|
|
|
|
_notify.Subscribe((a) =>
|
2020-05-14 14:03:24 +00:00
|
|
|
|
{
|
2022-02-14 16:03:56 +00:00
|
|
|
|
IsExist.AddOrUpdate(a.Id, false, (string q, bool w) => false);
|
2022-02-10 13:45:41 +00:00
|
|
|
|
}, CacheNotifyAction.Any);
|
2022-02-10 13:14:03 +00:00
|
|
|
|
}
|
2022-02-14 16:03:56 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
public void Clear<T>(T t) where T : class, ISearchItem
|
|
|
|
|
{
|
|
|
|
|
_notify.Publish(new ClearIndexAction() { Id = t.IndexName }, CacheNotifyAction.Any);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Scope]
|
|
|
|
|
public class BaseIndexer<T> where T : class, ISearchItem
|
|
|
|
|
{
|
|
|
|
|
public const int QueryLimit = 10000;
|
2020-01-24 13:07:51 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
protected internal T Wrapper => _serviceProvider.GetService<T>();
|
|
|
|
|
internal string IndexName => Wrapper.IndexName;
|
|
|
|
|
private WebstudioDbContext WebstudioDbContext => _lazyWebstudioDbContext.Value;
|
|
|
|
|
|
|
|
|
|
private bool _isExist;
|
|
|
|
|
private readonly Client _client;
|
|
|
|
|
private readonly ILog _logger;
|
|
|
|
|
private readonly TenantManager _tenantManager;
|
|
|
|
|
private readonly BaseIndexerHelper _baseIndexerHelper;
|
|
|
|
|
private readonly Settings _settings;
|
|
|
|
|
private readonly IServiceProvider _serviceProvider;
|
|
|
|
|
private readonly Lazy<WebstudioDbContext> _lazyWebstudioDbContext;
|
|
|
|
|
private static readonly object _locker = new object();
|
|
|
|
|
|
|
|
|
|
public BaseIndexer(
|
|
|
|
|
Client client,
|
|
|
|
|
IOptionsMonitor<ILog> log,
|
|
|
|
|
DbContextManager<WebstudioDbContext> dbContextManager,
|
|
|
|
|
TenantManager tenantManager,
|
|
|
|
|
BaseIndexerHelper baseIndexerHelper,
|
|
|
|
|
Settings settings,
|
|
|
|
|
IServiceProvider serviceProvider)
|
|
|
|
|
{
|
|
|
|
|
_client = client;
|
|
|
|
|
_logger = log.CurrentValue;
|
|
|
|
|
_tenantManager = tenantManager;
|
|
|
|
|
_baseIndexerHelper = baseIndexerHelper;
|
|
|
|
|
_settings = settings;
|
|
|
|
|
_serviceProvider = serviceProvider;
|
|
|
|
|
_lazyWebstudioDbContext = new Lazy<WebstudioDbContext>(() => dbContextManager.Value);
|
|
|
|
|
}
|
2022-02-14 16:03:56 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
public IEnumerable<List<T>> IndexAll(
|
|
|
|
|
Func<DateTime, (int, int, int)> getCount,
|
|
|
|
|
Func<DateTime, List<int>> getIds,
|
|
|
|
|
Func<long, long, DateTime, List<T>> getData)
|
|
|
|
|
{
|
|
|
|
|
var now = DateTime.UtcNow;
|
|
|
|
|
var lastIndexed = WebstudioDbContext.WebstudioIndex
|
|
|
|
|
.Where(r => r.IndexName == Wrapper.IndexName)
|
|
|
|
|
.Select(r => r.LastModified)
|
|
|
|
|
.FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
if (lastIndexed.Equals(DateTime.MinValue))
|
|
|
|
|
{
|
|
|
|
|
CreateIfNotExist(_serviceProvider.GetService<T>());
|
2021-05-23 16:11:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
var (count, max, min) = getCount(lastIndexed);
|
|
|
|
|
_logger.Debug($"Index: {IndexName}, Count {count}, Max: {max}, Min: {min}");
|
|
|
|
|
|
|
|
|
|
var ids = new List<int>() { min };
|
|
|
|
|
ids.AddRange(getIds(lastIndexed));
|
|
|
|
|
ids.Add(max);
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < ids.Count - 1; i++)
|
2021-05-23 16:11:25 +00:00
|
|
|
|
{
|
2022-02-10 13:45:41 +00:00
|
|
|
|
yield return getData(ids[i], ids[i + 1], lastIndexed);
|
|
|
|
|
}
|
2021-05-23 16:11:25 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
WebstudioDbContext.AddOrUpdate(r => r.WebstudioIndex, new DbWebstudioIndex()
|
|
|
|
|
{
|
|
|
|
|
IndexName = Wrapper.IndexName,
|
|
|
|
|
LastModified = now
|
|
|
|
|
});
|
2021-05-23 16:11:25 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
WebstudioDbContext.SaveChanges();
|
|
|
|
|
|
|
|
|
|
_logger.Debug($"index completed {Wrapper.IndexName}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task ReIndex()
|
|
|
|
|
{
|
|
|
|
|
Clear();
|
2021-05-23 16:11:25 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
//((IIndexer) this).IndexAll();
|
|
|
|
|
}
|
2021-05-23 16:11:25 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
public void CreateIfNotExist(T data)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (CheckExist(data))
|
2022-02-10 13:14:03 +00:00
|
|
|
|
{
|
2022-02-10 13:45:41 +00:00
|
|
|
|
return;
|
2022-02-10 13:14:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
lock (_locker)
|
2022-02-10 13:14:03 +00:00
|
|
|
|
{
|
2022-02-10 13:45:41 +00:00
|
|
|
|
IPromise<IAnalyzers> analyzers(AnalyzersDescriptor b)
|
|
|
|
|
{
|
|
|
|
|
foreach (var c in Enum.GetNames(typeof(Analyzer)))
|
2021-05-23 16:11:25 +00:00
|
|
|
|
{
|
2022-02-10 13:45:41 +00:00
|
|
|
|
var c1 = c;
|
2022-02-14 16:03:56 +00:00
|
|
|
|
b.Custom(c1 + "custom", ca => ca.Tokenizer(c1).Filters(nameof(Filter.lowercase)).CharFilters(nameof(CharFilter.io)));
|
2021-05-23 16:11:25 +00:00
|
|
|
|
}
|
2022-02-10 13:14:03 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
foreach (var c in Enum.GetNames(typeof(CharFilter)))
|
2021-05-23 16:11:25 +00:00
|
|
|
|
{
|
2022-02-14 16:03:56 +00:00
|
|
|
|
if (c == nameof(CharFilter.io))
|
2021-05-23 16:11:25 +00:00
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-14 16:03:56 +00:00
|
|
|
|
var charFilters = new List<string>() { nameof(CharFilter.io), c };
|
2022-02-10 13:45:41 +00:00
|
|
|
|
var c1 = c;
|
2022-02-14 16:03:56 +00:00
|
|
|
|
b.Custom(c1 + "custom", ca => ca.Tokenizer(nameof(Analyzer.whitespace)).Filters(nameof(Filter.lowercase)).CharFilters(charFilters));
|
2021-05-23 16:11:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
if (data is ISearchItemDocument)
|
2021-05-23 16:11:25 +00:00
|
|
|
|
{
|
2022-02-14 16:03:56 +00:00
|
|
|
|
b.Custom("document", ca => ca.Tokenizer(Analyzer.whitespace.ToString()).Filters(nameof(Filter.lowercase)).CharFilters(nameof(CharFilter.io)));
|
2021-05-23 16:11:25 +00:00
|
|
|
|
}
|
2022-02-10 13:45:41 +00:00
|
|
|
|
|
|
|
|
|
return b;
|
2021-05-23 16:11:25 +00:00
|
|
|
|
}
|
2022-02-10 13:45:41 +00:00
|
|
|
|
|
2022-02-14 16:03:56 +00:00
|
|
|
|
_client.Instance.Indices.Create(data.IndexName,
|
2022-02-10 13:45:41 +00:00
|
|
|
|
c =>
|
|
|
|
|
c.Map<T>(m => m.AutoMap())
|
|
|
|
|
.Settings(r => r.Analysis(a =>
|
|
|
|
|
a.Analyzers(analyzers)
|
|
|
|
|
.CharFilters(d => d.HtmlStrip(CharFilter.html.ToString())
|
|
|
|
|
.Mapping(CharFilter.io.ToString(), m => m.Mappings("ё => е", "Ё => Е"))))));
|
|
|
|
|
|
|
|
|
|
_isExist = true;
|
2021-05-23 16:11:25 +00:00
|
|
|
|
}
|
2022-02-10 13:14:03 +00:00
|
|
|
|
}
|
2022-02-10 13:45:41 +00:00
|
|
|
|
catch (Exception e)
|
2020-05-14 14:03:24 +00:00
|
|
|
|
{
|
2022-02-10 13:45:41 +00:00
|
|
|
|
_logger.Error("CreateIfNotExist", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-10 13:14:03 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
public void Flush()
|
|
|
|
|
{
|
|
|
|
|
_client.Instance.Indices.Flush(new FlushRequest(IndexName));
|
|
|
|
|
}
|
2022-02-10 13:24:25 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
public void Refresh()
|
|
|
|
|
{
|
|
|
|
|
_client.Instance.Indices.Refresh(new RefreshRequest(IndexName));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void Index(T data, bool immediately = true)
|
|
|
|
|
{
|
|
|
|
|
CreateIfNotExist(data);
|
|
|
|
|
_client.Instance.Index(data, idx => GetMeta(idx, data, immediately));
|
|
|
|
|
}
|
2022-02-14 16:03:56 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
internal void Index(List<T> data, bool immediately = true)
|
|
|
|
|
{
|
2022-02-14 16:03:56 +00:00
|
|
|
|
if (data.Count == 0)
|
2020-05-14 14:03:24 +00:00
|
|
|
|
{
|
2022-02-10 13:45:41 +00:00
|
|
|
|
return;
|
2022-02-10 13:14:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
CreateIfNotExist(data[0]);
|
2020-01-24 13:07:51 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
if (data[0] is ISearchItemDocument)
|
2020-05-14 14:03:24 +00:00
|
|
|
|
{
|
2022-02-10 13:45:41 +00:00
|
|
|
|
var currentLength = 0L;
|
|
|
|
|
var portion = new List<T>();
|
|
|
|
|
var portionStart = 0;
|
2020-01-24 13:07:51 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
for (var i = 0; i < data.Count; i++)
|
2022-02-10 13:14:03 +00:00
|
|
|
|
{
|
2022-02-10 13:45:41 +00:00
|
|
|
|
var t = data[i];
|
|
|
|
|
var runBulk = i == data.Count - 1;
|
2020-02-17 08:58:14 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
if (!(t is ISearchItemDocument wwd) || wwd.Document == null || string.IsNullOrEmpty(wwd.Document.Data))
|
2022-02-10 13:24:25 +00:00
|
|
|
|
{
|
2022-02-10 13:45:41 +00:00
|
|
|
|
portion.Add(t);
|
2022-02-10 13:24:25 +00:00
|
|
|
|
}
|
2022-02-10 13:45:41 +00:00
|
|
|
|
else
|
2022-02-10 13:14:03 +00:00
|
|
|
|
{
|
2022-02-10 13:45:41 +00:00
|
|
|
|
var dLength = wwd.Document.Data.Length;
|
|
|
|
|
if (dLength >= _settings.MaxContentLength)
|
2022-02-10 13:14:03 +00:00
|
|
|
|
{
|
2022-02-10 13:45:41 +00:00
|
|
|
|
try
|
2022-02-10 13:14:03 +00:00
|
|
|
|
{
|
2022-02-10 13:45:41 +00:00
|
|
|
|
Index(t, immediately);
|
2022-02-10 13:14:03 +00:00
|
|
|
|
}
|
2022-02-10 13:45:41 +00:00
|
|
|
|
catch (ElasticsearchClientException e)
|
2022-02-10 13:14:03 +00:00
|
|
|
|
{
|
2022-02-10 13:45:41 +00:00
|
|
|
|
if (e.Response.HttpStatusCode == 429)
|
2022-02-10 13:24:25 +00:00
|
|
|
|
{
|
2022-02-10 13:45:41 +00:00
|
|
|
|
throw;
|
2022-02-10 13:24:25 +00:00
|
|
|
|
}
|
2022-02-10 13:45:41 +00:00
|
|
|
|
_logger.Error(e);
|
2022-02-10 13:14:03 +00:00
|
|
|
|
}
|
2022-02-10 13:45:41 +00:00
|
|
|
|
catch (Exception e)
|
2022-02-10 13:14:03 +00:00
|
|
|
|
{
|
2022-02-10 13:45:41 +00:00
|
|
|
|
_logger.Error(e);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
wwd.Document.Data = null;
|
|
|
|
|
wwd.Document = null;
|
|
|
|
|
wwd = null;
|
|
|
|
|
GC.Collect();
|
2022-02-10 13:14:03 +00:00
|
|
|
|
}
|
2020-02-17 08:58:14 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
continue;
|
2022-02-10 13:14:03 +00:00
|
|
|
|
}
|
2020-01-24 13:07:51 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
if (currentLength + dLength < _settings.MaxContentLength)
|
|
|
|
|
{
|
|
|
|
|
portion.Add(t);
|
|
|
|
|
currentLength += dLength;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
runBulk = true;
|
|
|
|
|
i--;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-24 13:07:51 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
if (runBulk)
|
|
|
|
|
{
|
|
|
|
|
var portion1 = portion.ToList();
|
|
|
|
|
_client.Instance.Bulk(r => r.IndexMany(portion1, GetMeta).SourceExcludes("attachments"));
|
|
|
|
|
for (var j = portionStart; j < i; j++)
|
|
|
|
|
{
|
|
|
|
|
if (data[j] is ISearchItemDocument doc && doc.Document != null)
|
|
|
|
|
{
|
|
|
|
|
doc.Document.Data = null;
|
|
|
|
|
doc.Document = null;
|
|
|
|
|
}
|
|
|
|
|
doc = null;
|
|
|
|
|
}
|
2020-01-24 13:07:51 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
portionStart = i;
|
|
|
|
|
portion = new List<T>();
|
|
|
|
|
currentLength = 0L;
|
|
|
|
|
GC.Collect();
|
2022-02-10 13:14:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-10 13:45:41 +00:00
|
|
|
|
else
|
2020-05-14 14:03:24 +00:00
|
|
|
|
{
|
2022-02-10 13:45:41 +00:00
|
|
|
|
_client.Instance.Bulk(r => r.IndexMany(data, GetMeta));
|
2020-04-27 11:27:58 +00:00
|
|
|
|
}
|
2022-02-10 13:45:41 +00:00
|
|
|
|
}
|
2021-05-23 16:11:25 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
internal async Task IndexAsync(List<T> data, bool immediately = true)
|
|
|
|
|
{
|
|
|
|
|
CreateIfNotExist(data[0]);
|
2020-04-27 11:27:58 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
if (data is ISearchItemDocument)
|
2020-04-27 11:27:58 +00:00
|
|
|
|
{
|
2022-02-10 13:45:41 +00:00
|
|
|
|
var currentLength = 0L;
|
|
|
|
|
var portion = new List<T>();
|
|
|
|
|
var portionStart = 0;
|
2021-05-23 16:11:25 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
for (var i = 0; i < data.Count; i++)
|
2020-04-27 11:27:58 +00:00
|
|
|
|
{
|
2022-02-10 13:45:41 +00:00
|
|
|
|
var t = data[i];
|
|
|
|
|
var runBulk = i == data.Count - 1;
|
2021-05-23 16:11:25 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
var wwd = t as ISearchItemDocument;
|
2020-04-27 11:27:58 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
if (wwd == null || wwd.Document == null || string.IsNullOrEmpty(wwd.Document.Data))
|
|
|
|
|
{
|
|
|
|
|
portion.Add(t);
|
|
|
|
|
}
|
|
|
|
|
else
|
2020-04-27 11:27:58 +00:00
|
|
|
|
{
|
2022-02-10 13:45:41 +00:00
|
|
|
|
var dLength = wwd.Document.Data.Length;
|
|
|
|
|
if (dLength >= _settings.MaxContentLength)
|
2020-04-27 11:27:58 +00:00
|
|
|
|
{
|
2022-02-10 13:45:41 +00:00
|
|
|
|
try
|
2020-04-27 11:27:58 +00:00
|
|
|
|
{
|
2022-02-10 13:45:41 +00:00
|
|
|
|
Index(t, immediately);
|
2020-04-27 11:27:58 +00:00
|
|
|
|
}
|
2022-02-10 13:45:41 +00:00
|
|
|
|
catch (ElasticsearchClientException e)
|
2020-04-27 11:27:58 +00:00
|
|
|
|
{
|
2022-02-10 13:45:41 +00:00
|
|
|
|
if (e.Response.HttpStatusCode == 429)
|
2021-05-23 16:11:25 +00:00
|
|
|
|
{
|
2022-02-10 13:45:41 +00:00
|
|
|
|
throw;
|
2021-05-23 16:11:25 +00:00
|
|
|
|
}
|
2022-02-10 13:45:41 +00:00
|
|
|
|
_logger.Error(e);
|
2020-04-27 11:27:58 +00:00
|
|
|
|
}
|
2022-02-10 13:45:41 +00:00
|
|
|
|
catch (Exception e)
|
2021-05-23 16:11:25 +00:00
|
|
|
|
{
|
2022-02-10 13:45:41 +00:00
|
|
|
|
_logger.Error(e);
|
2021-05-23 16:11:25 +00:00
|
|
|
|
}
|
2022-02-10 13:45:41 +00:00
|
|
|
|
finally
|
2020-04-27 11:27:58 +00:00
|
|
|
|
{
|
2022-02-10 13:45:41 +00:00
|
|
|
|
wwd.Document.Data = null;
|
|
|
|
|
wwd.Document = null;
|
|
|
|
|
GC.Collect();
|
2020-04-27 11:27:58 +00:00
|
|
|
|
}
|
2022-02-10 13:45:41 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2020-04-27 11:27:58 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
if (currentLength + dLength < _settings.MaxContentLength)
|
|
|
|
|
{
|
|
|
|
|
portion.Add(t);
|
|
|
|
|
currentLength += dLength;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
runBulk = true;
|
|
|
|
|
i--;
|
2020-04-29 08:04:19 +00:00
|
|
|
|
}
|
2022-02-10 13:45:41 +00:00
|
|
|
|
}
|
2020-04-27 11:27:58 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
if (runBulk)
|
|
|
|
|
{
|
|
|
|
|
var portion1 = portion.ToList();
|
|
|
|
|
await _client.Instance.BulkAsync(r => r.IndexMany(portion1, GetMeta).SourceExcludes("attachments"));
|
|
|
|
|
for (var j = portionStart; j < i; j++)
|
2021-05-23 16:11:25 +00:00
|
|
|
|
{
|
2022-02-10 13:45:41 +00:00
|
|
|
|
var doc = data[j] as ISearchItemDocument;
|
|
|
|
|
if (doc != null && doc.Document != null)
|
2021-05-23 16:11:25 +00:00
|
|
|
|
{
|
2022-02-10 13:45:41 +00:00
|
|
|
|
doc.Document.Data = null;
|
|
|
|
|
doc.Document = null;
|
2021-05-23 16:11:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-27 11:27:58 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
portionStart = i;
|
|
|
|
|
portion = new List<T>();
|
|
|
|
|
currentLength = 0L;
|
|
|
|
|
GC.Collect();
|
2020-04-27 11:27:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-10 13:45:41 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
await _client.Instance.BulkAsync(r => r.IndexMany(data, GetMeta));
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-14 16:03:56 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
internal void Update(T data, bool immediately = true, params Expression<Func<T, object>>[] fields)
|
|
|
|
|
{
|
|
|
|
|
CreateIfNotExist(data);
|
|
|
|
|
_client.Instance.Update(DocumentPath<T>.Id(data), r => GetMetaForUpdate(r, data, immediately, fields));
|
|
|
|
|
}
|
2022-02-14 16:03:56 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
internal void Update(T data, UpdateAction action, Expression<Func<T, IList>> fields, bool immediately = true)
|
|
|
|
|
{
|
|
|
|
|
CreateIfNotExist(data);
|
|
|
|
|
_client.Instance.Update(DocumentPath<T>.Id(data), r => GetMetaForUpdate(r, data, action, fields, immediately));
|
|
|
|
|
}
|
2022-02-14 16:03:56 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
internal void Update(T data, Expression<Func<Selector<T>, Selector<T>>> expression, int tenantId, bool immediately = true, params Expression<Func<T, object>>[] fields)
|
|
|
|
|
{
|
|
|
|
|
CreateIfNotExist(data);
|
|
|
|
|
_client.Instance.UpdateByQuery(GetDescriptorForUpdate(data, expression, tenantId, immediately, fields));
|
|
|
|
|
}
|
2022-02-14 16:03:56 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
internal void Update(T data, Expression<Func<Selector<T>, Selector<T>>> expression, int tenantId, UpdateAction action, Expression<Func<T, IList>> fields, bool immediately = true)
|
|
|
|
|
{
|
|
|
|
|
CreateIfNotExist(data);
|
|
|
|
|
_client.Instance.UpdateByQuery(GetDescriptorForUpdate(data, expression, tenantId, action, fields, immediately));
|
|
|
|
|
}
|
2022-02-14 16:03:56 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
internal void Delete(T data, bool immediately = true)
|
|
|
|
|
{
|
|
|
|
|
_client.Instance.Delete<T>(data, r => GetMetaForDelete(r, immediately));
|
|
|
|
|
}
|
2022-02-14 16:03:56 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
internal void Delete(Expression<Func<Selector<T>, Selector<T>>> expression, int tenantId, bool immediately = true)
|
|
|
|
|
{
|
|
|
|
|
_client.Instance.DeleteByQuery(GetDescriptorForDelete(expression, tenantId, immediately));
|
|
|
|
|
}
|
2022-02-14 16:03:56 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
internal bool CheckExist(T data)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var isExist = _baseIndexerHelper.IsExist.GetOrAdd(data.IndexName, (k) => _client.Instance.Indices.Exists(k).Exists);
|
|
|
|
|
if (isExist)
|
2021-05-23 16:11:25 +00:00
|
|
|
|
{
|
2022-02-10 13:45:41 +00:00
|
|
|
|
return true;
|
2021-05-23 16:11:25 +00:00
|
|
|
|
}
|
2022-02-14 16:03:56 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
lock (_locker)
|
2020-04-27 11:27:58 +00:00
|
|
|
|
{
|
2022-02-10 13:45:41 +00:00
|
|
|
|
isExist = _client.Instance.Indices.Exists(data.IndexName).Exists;
|
2022-02-14 16:03:56 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
_baseIndexerHelper.IsExist.TryUpdate(data.IndexName, _isExist, false);
|
2022-02-14 16:03:56 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
if (isExist)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-04-27 11:27:58 +00:00
|
|
|
|
}
|
2022-02-14 16:03:56 +00:00
|
|
|
|
}
|
2022-02-10 13:45:41 +00:00
|
|
|
|
catch (Exception e)
|
2020-05-14 14:03:24 +00:00
|
|
|
|
{
|
2022-02-10 13:45:41 +00:00
|
|
|
|
_logger.Error("CheckExist " + data.IndexName, e);
|
2020-04-27 11:27:58 +00:00
|
|
|
|
}
|
2020-01-24 13:07:51 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-01-24 13:07:51 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
internal IReadOnlyCollection<T> Select(Expression<Func<Selector<T>, Selector<T>>> expression, bool onlyId = false)
|
|
|
|
|
{
|
|
|
|
|
var func = expression.Compile();
|
|
|
|
|
var selector = new Selector<T>(_serviceProvider);
|
2022-02-21 09:51:45 +00:00
|
|
|
|
var descriptor = func(selector).Where(r => r.TenantId, _tenantManager.GetCurrentTenant().Id);
|
2022-02-10 13:45:41 +00:00
|
|
|
|
|
|
|
|
|
return _client.Instance.Search(descriptor.GetDescriptor(this, onlyId)).Documents;
|
|
|
|
|
}
|
2022-02-14 16:03:56 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
internal IReadOnlyCollection<T> Select(Expression<Func<Selector<T>, Selector<T>>> expression, bool onlyId, out long total)
|
|
|
|
|
{
|
|
|
|
|
var func = expression.Compile();
|
|
|
|
|
var selector = new Selector<T>(_serviceProvider);
|
2022-02-21 09:51:45 +00:00
|
|
|
|
var descriptor = func(selector).Where(r => r.TenantId, _tenantManager.GetCurrentTenant().Id);
|
2022-02-10 13:45:41 +00:00
|
|
|
|
var result = _client.Instance.Search(descriptor.GetDescriptor(this, onlyId));
|
|
|
|
|
total = result.Total;
|
|
|
|
|
|
|
|
|
|
return result.Documents;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Clear()
|
|
|
|
|
{
|
|
|
|
|
var index = WebstudioDbContext.WebstudioIndex.Where(r => r.IndexName == Wrapper.IndexName).FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
if (index != null)
|
2020-05-14 14:03:24 +00:00
|
|
|
|
{
|
2022-02-10 13:45:41 +00:00
|
|
|
|
WebstudioDbContext.WebstudioIndex.Remove(index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WebstudioDbContext.SaveChanges();
|
|
|
|
|
|
|
|
|
|
_logger.DebugFormat("Delete {0}", Wrapper.IndexName);
|
|
|
|
|
_client.Instance.Indices.Delete(Wrapper.IndexName);
|
|
|
|
|
_baseIndexerHelper.Clear(Wrapper);
|
|
|
|
|
CreateIfNotExist(Wrapper);
|
|
|
|
|
}
|
2020-01-24 13:07:51 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
private IIndexRequest<T> GetMeta(IndexDescriptor<T> request, T data, bool immediately = true)
|
|
|
|
|
{
|
|
|
|
|
var result = request.Index(data.IndexName).Id(data.Id);
|
2020-01-24 13:07:51 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
if (immediately)
|
2020-05-14 14:03:24 +00:00
|
|
|
|
{
|
2022-02-10 13:45:41 +00:00
|
|
|
|
result.Refresh(Elasticsearch.Net.Refresh.True);
|
2022-02-10 13:14:03 +00:00
|
|
|
|
}
|
2020-01-24 13:07:51 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
if (data is ISearchItemDocument)
|
|
|
|
|
{
|
|
|
|
|
result.Pipeline("attachments");
|
|
|
|
|
}
|
2020-01-24 13:07:51 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
private IBulkIndexOperation<T> GetMeta(BulkIndexDescriptor<T> desc, T data)
|
|
|
|
|
{
|
|
|
|
|
var result = desc.Index(IndexName).Id(data.Id);
|
2020-01-24 13:07:51 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
if (data is ISearchItemDocument)
|
|
|
|
|
{
|
|
|
|
|
result.Pipeline("attachments");
|
|
|
|
|
}
|
2020-01-24 13:07:51 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
2020-01-24 13:07:51 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
private IUpdateRequest<T, T> GetMetaForUpdate(UpdateDescriptor<T, T> request, T data, bool immediately = true, params Expression<Func<T, object>>[] fields)
|
|
|
|
|
{
|
|
|
|
|
var result = request.Index(IndexName);
|
2020-01-24 13:07:51 +00:00
|
|
|
|
|
2022-02-14 16:03:56 +00:00
|
|
|
|
if (fields.Length > 0)
|
2022-02-10 13:45:41 +00:00
|
|
|
|
{
|
|
|
|
|
result.Script(GetScriptUpdateByQuery(data, fields));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
result.Doc(data);
|
|
|
|
|
}
|
2020-01-24 13:07:51 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
if (immediately)
|
|
|
|
|
{
|
|
|
|
|
result.Refresh(Elasticsearch.Net.Refresh.True);
|
|
|
|
|
}
|
2020-01-24 13:07:51 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
2020-01-24 13:07:51 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
private Func<ScriptDescriptor, IScript> GetScriptUpdateByQuery(T data, params Expression<Func<T, object>>[] fields)
|
|
|
|
|
{
|
|
|
|
|
var source = new StringBuilder();
|
|
|
|
|
var parameters = new Dictionary<string, object>();
|
2020-01-24 13:07:51 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
for (var i = 0; i < fields.Length; i++)
|
|
|
|
|
{
|
2022-02-14 16:03:56 +00:00
|
|
|
|
var field = fields[i];
|
|
|
|
|
var func = field.Compile();
|
2022-02-10 13:45:41 +00:00
|
|
|
|
var newValue = func(data);
|
|
|
|
|
string name;
|
2020-01-24 13:07:51 +00:00
|
|
|
|
|
2022-02-14 16:03:56 +00:00
|
|
|
|
var expression = field.Body;
|
2022-02-10 13:45:41 +00:00
|
|
|
|
var isList = expression.Type.IsGenericType && expression.Type.GetGenericTypeDefinition() == typeof(List<>);
|
2020-01-24 13:07:51 +00:00
|
|
|
|
|
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
var sourceExprText = "";
|
2022-01-21 11:02:17 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
while (!string.IsNullOrEmpty(name = TryGetName(expression, out var member)))
|
|
|
|
|
{
|
2022-02-07 14:53:58 +00:00
|
|
|
|
sourceExprText = "." + name + sourceExprText;
|
2022-02-10 13:45:41 +00:00
|
|
|
|
}
|
2022-02-14 16:03:56 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
if (isList)
|
|
|
|
|
{
|
|
|
|
|
UpdateByAction(UpdateAction.Add, (IList)newValue, sourceExprText, parameters, source);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (newValue == default(T))
|
2022-02-10 13:24:25 +00:00
|
|
|
|
{
|
2022-02-14 16:03:56 +00:00
|
|
|
|
source.Append($"ctx._source.remove('{sourceExprText.Substring(1)}');");
|
2022-02-10 13:24:25 +00:00
|
|
|
|
}
|
2022-02-10 13:45:41 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var pkey = "p" + sourceExprText.Replace(".", "");
|
2022-02-14 16:03:56 +00:00
|
|
|
|
source.Append($"ctx._source{sourceExprText} = params.{pkey};");
|
2022-02-10 13:45:41 +00:00
|
|
|
|
parameters.Add(pkey, newValue);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-24 13:07:51 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
var sourceData = source.ToString();
|
2020-01-24 13:07:51 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
return r => r.Source(sourceData).Params(parameters);
|
|
|
|
|
}
|
2020-01-24 13:07:51 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
private IUpdateRequest<T, T> GetMetaForUpdate(UpdateDescriptor<T, T> request, T data, UpdateAction action, Expression<Func<T, IList>> fields, bool immediately = true)
|
|
|
|
|
{
|
|
|
|
|
var result = request.Index(IndexName).Script(GetScriptForUpdate(data, action, fields));
|
2020-01-24 13:07:51 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
if (immediately)
|
|
|
|
|
{
|
|
|
|
|
result.Refresh(Elasticsearch.Net.Refresh.True);
|
2020-04-27 11:27:58 +00:00
|
|
|
|
}
|
2020-01-24 13:07:51 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
2020-01-24 13:07:51 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
private Func<ScriptDescriptor, IScript> GetScriptForUpdate(T data, UpdateAction action, Expression<Func<T, IList>> fields)
|
|
|
|
|
{
|
|
|
|
|
var source = new StringBuilder();
|
2020-01-24 13:07:51 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
var func = fields.Compile();
|
|
|
|
|
var newValue = func(data);
|
|
|
|
|
string name;
|
2020-01-24 13:07:51 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
var expression = fields.Body;
|
2020-01-24 13:07:51 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
var sourceExprText = "";
|
2020-04-23 09:38:50 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
while (!string.IsNullOrEmpty(name = TryGetName(expression, out var member)))
|
2020-04-27 11:27:58 +00:00
|
|
|
|
{
|
2022-02-10 13:45:41 +00:00
|
|
|
|
sourceExprText = "." + name + sourceExprText;
|
|
|
|
|
expression = member.Expression;
|
2020-04-27 11:27:58 +00:00
|
|
|
|
}
|
2022-02-14 16:03:56 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
var parameters = new Dictionary<string, object>();
|
2022-02-14 16:03:56 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
UpdateByAction(action, newValue, sourceExprText, parameters, source);
|
2022-02-14 16:03:56 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
return r => r.Source(source.ToString()).Params(parameters);
|
|
|
|
|
}
|
2022-02-14 16:03:56 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
private void UpdateByAction(UpdateAction action, IList newValue, string key, Dictionary<string, object> parameters, StringBuilder source)
|
|
|
|
|
{
|
|
|
|
|
var paramKey = "p" + key.Replace(".", "");
|
|
|
|
|
switch (action)
|
|
|
|
|
{
|
|
|
|
|
case UpdateAction.Add:
|
|
|
|
|
for (var i = 0; i < newValue.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
parameters.Add(paramKey + i, newValue[i]);
|
2022-02-14 16:03:56 +00:00
|
|
|
|
source.Append($"if (!ctx._source{key}.contains(params.{paramKey + i})){{ctx._source{key}.add(params.{paramKey + i})}}");
|
2022-02-10 13:45:41 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case UpdateAction.Replace:
|
|
|
|
|
parameters.Add(paramKey, newValue);
|
2022-02-17 17:35:08 +00:00
|
|
|
|
source.Append($"ctx._source{key} = params.{paramKey};");
|
2022-02-10 13:45:41 +00:00
|
|
|
|
break;
|
|
|
|
|
case UpdateAction.Remove:
|
|
|
|
|
for (var i = 0; i < newValue.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
parameters.Add(paramKey + i, newValue[i]);
|
2022-02-17 17:35:08 +00:00
|
|
|
|
source.Append($"ctx._source{key}.removeIf(item -> item.id == params.{paramKey + i}.id)");
|
2022-02-10 13:45:41 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw new ArgumentOutOfRangeException("action", action, null);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-14 16:03:56 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
private string TryGetName(Expression expr, out MemberExpression member)
|
|
|
|
|
{
|
|
|
|
|
member = expr as MemberExpression;
|
|
|
|
|
if (member == null)
|
|
|
|
|
{
|
|
|
|
|
if (expr is UnaryExpression unary)
|
2021-05-23 16:11:25 +00:00
|
|
|
|
{
|
2022-02-10 13:45:41 +00:00
|
|
|
|
member = unary.Operand as MemberExpression;
|
2020-04-27 16:59:52 +00:00
|
|
|
|
}
|
2020-04-23 09:38:50 +00:00
|
|
|
|
}
|
2022-02-14 16:03:56 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
return member == null ? "" : member.Member.Name.ToLowerCamelCase();
|
|
|
|
|
}
|
2022-02-14 16:03:56 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
private IDeleteRequest GetMetaForDelete(DeleteDescriptor<T> request, bool immediately = true)
|
|
|
|
|
{
|
|
|
|
|
var result = request.Index(IndexName);
|
|
|
|
|
if (immediately)
|
|
|
|
|
{
|
|
|
|
|
result.Refresh(Elasticsearch.Net.Refresh.True);
|
|
|
|
|
}
|
2020-04-27 16:59:52 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
2022-02-14 16:03:56 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
private Func<DeleteByQueryDescriptor<T>, IDeleteByQueryRequest> GetDescriptorForDelete(Expression<Func<Selector<T>, Selector<T>>> expression, int tenantId, bool immediately = true)
|
|
|
|
|
{
|
|
|
|
|
var func = expression.Compile();
|
|
|
|
|
var selector = new Selector<T>(_serviceProvider);
|
|
|
|
|
var descriptor = func(selector).Where(r => r.TenantId, tenantId);
|
2020-04-27 16:59:52 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
return descriptor.GetDescriptorForDelete(this, immediately);
|
|
|
|
|
}
|
2022-02-14 16:03:56 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
private Func<UpdateByQueryDescriptor<T>, IUpdateByQueryRequest> GetDescriptorForUpdate(T data, Expression<Func<Selector<T>, Selector<T>>> expression, int tenantId, bool immediately = true, params Expression<Func<T, object>>[] fields)
|
|
|
|
|
{
|
|
|
|
|
var func = expression.Compile();
|
|
|
|
|
var selector = new Selector<T>(_serviceProvider);
|
|
|
|
|
var descriptor = func(selector).Where(r => r.TenantId, tenantId);
|
2020-04-27 16:59:52 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
return descriptor.GetDescriptorForUpdate(this, GetScriptUpdateByQuery(data, fields), immediately);
|
|
|
|
|
}
|
2022-02-14 16:03:56 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
private Func<UpdateByQueryDescriptor<T>, IUpdateByQueryRequest> GetDescriptorForUpdate(T data, Expression<Func<Selector<T>, Selector<T>>> expression, int tenantId, UpdateAction action, Expression<Func<T, IList>> fields, bool immediately = true)
|
|
|
|
|
{
|
|
|
|
|
var func = expression.Compile();
|
|
|
|
|
var selector = new Selector<T>(_serviceProvider);
|
|
|
|
|
var descriptor = func(selector).Where(r => r.TenantId, tenantId);
|
2020-04-27 11:27:58 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
return descriptor.GetDescriptorForUpdate(this, GetScriptForUpdate(data, action, fields), immediately);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-24 13:07:51 +00:00
|
|
|
|
|
2022-02-10 13:45:41 +00:00
|
|
|
|
static class CamelCaseExtension
|
|
|
|
|
{
|
|
|
|
|
internal static string ToLowerCamelCase(this string str)
|
|
|
|
|
{
|
|
|
|
|
return str.ToLowerInvariant()[0] + str.Substring(1);
|
|
|
|
|
}
|
|
|
|
|
}
|