mirror of
https://github.com/hanzoai/jube-fork.git
synced 2026-10-30 18:33:33 +00:00
* Pushed Case Workflow Display HTML rendering logic down into a controller for improved security and consistent tokenisation of the Jube token format against context. Included a migration to update the default demo entry to use proper tokenisation. Updated the repository to support fetching CaseWorkflowDisplay with appropriate security. * Implemented improved tokenisation of the EntityAnalysisModelInstanceEntryPayload model via a common extension method on EntityAnalysisModelInstanceEntryPayload, used consistently across the system for serialisation and storage. * Included a migration to update the tokenisation format for full CaseWorkflowDisplay support. * Fixed an issue where CaseWorkflowFilter was incorrectly filtering on CaseWorkflowStatus.Guid instead of CaseWorkflowStatus.Name, causing the GUID to be returned rather than the display name on the template/demo model. (The remainder was fixed in GitHub issue #6.) Included a migration with a corrective SQL statement. * Fixed an issue where tokens were not being stored alongside CaseWorkflowFilter definition configuration. It now behaves identically to CaseSearch. Note: migrating missing tokens is not feasible as it requires JS-side parsing via JQueryBuilder. * Reduced the size of the CaseWorkflowForm invocation to include only the required fields given the new POST body context being rendered server side. Improved API serialisation by using a dictionary for form payload keys and values. * Consolidated JSON serialisation settings into DI and updated usages throughout. * Updated EntityAnalysisModelInstanceEntryPayload write serialisers to handle deserialisation, which is required to support tokenisation of new Case Automation POST bodies. * Created an extension method on the EntityAnalysisModelInstanceEntryPayload to centralise tokenisation of strings across Activation and Case Automation on a consistent basis. Simplified Case Notification and Case HTTP dispatch by removing tokenisation logic from those classes. * Removed a redundant field from the CaseWorkflowFormEntry POCO (database left unchanged for now). * Fixed a bug in Case session SQL rebuilding where the rebuild date was not being correctly checked for null or default value, which caused a failure in the last migration. * Made minor modifications to the Create Case process and method signature to skip serialisation when the payload object required for tokenisation is already available on the thread (e.g. during synchronous reprocessing). * Updated Case Workflow Macro processing to fetch the archive payload for the case from the database, improving security of that invocation endpoint. * Removed what appeared to be a duplicate EntityAnalysisModelInstanceEntryPayload model introduced during refactoring. * Fixed a but in Case Worfkflow Display Role assigment owing to volatile Guid in its update.
381 lines
10 KiB
C#
381 lines
10 KiB
C#
/* Copyright (C) 2022-present Jube Holdings Limited.
|
|
*
|
|
* This file is part of Jube™ software.
|
|
*
|
|
* Jube™ is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License
|
|
* as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
|
* Jube™ is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
|
|
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU Affero General Public License along with Jube™. If not,
|
|
* see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
namespace Jube.Dictionary
|
|
{
|
|
using System.Buffers;
|
|
using System.Collections;
|
|
|
|
[Serializable]
|
|
public class PooledDictionary<TKey, TValue> : IDictionary<TKey, TValue>, IDisposable
|
|
{
|
|
private const float LoadFactor = 0.75f;
|
|
private readonly bool fixedInitialSize;
|
|
private readonly ArrayPool<int> intPool = ArrayPool<int>.Shared;
|
|
private readonly ArrayPool<TKey> keyPool = ArrayPool<TKey>.Shared;
|
|
private readonly ArrayPool<TValue> valuePool = ArrayPool<TValue>.Shared;
|
|
|
|
private int capacity;
|
|
private int[] hashes;
|
|
private TKey[] keys;
|
|
private TValue[] values;
|
|
|
|
public PooledDictionary() : this(null)
|
|
{
|
|
}
|
|
|
|
public PooledDictionary(int? initialCapacity = null)
|
|
{
|
|
if (initialCapacity.HasValue)
|
|
{
|
|
capacity = initialCapacity.Value;
|
|
fixedInitialSize = true;
|
|
}
|
|
else
|
|
{
|
|
capacity = 16;
|
|
fixedInitialSize = false;
|
|
}
|
|
|
|
hashes = intPool.Rent(capacity);
|
|
keys = keyPool.Rent(capacity);
|
|
values = valuePool.Rent(capacity);
|
|
}
|
|
|
|
public int Count { get; private set; }
|
|
public bool IsReadOnly
|
|
{
|
|
get
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public ICollection<TKey> Keys
|
|
{
|
|
get
|
|
{
|
|
return keys.Where((_, i) => hashes[i] != 0).ToArray();
|
|
}
|
|
}
|
|
public ICollection<TValue> Values
|
|
{
|
|
get
|
|
{
|
|
return values.Where((_, i) => hashes[i] != 0).ToArray();
|
|
}
|
|
}
|
|
|
|
public TValue this[TKey key]
|
|
{
|
|
get
|
|
{
|
|
return TryGetValue(key, out var value) ? value : default(TValue)!;
|
|
}
|
|
set
|
|
{
|
|
var index = FindIndex(key);
|
|
if (index >= 0)
|
|
{
|
|
values[index] = value;
|
|
}
|
|
else
|
|
{
|
|
Add(key, value);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Add(TKey key, TValue value)
|
|
{
|
|
if (key == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(key));
|
|
}
|
|
|
|
EnsureCapacity();
|
|
|
|
var hash = key.GetHashCode() & 0x7FFFFFFF;
|
|
var index = hash % capacity;
|
|
|
|
for (var i = 0; i < capacity; i++)
|
|
{
|
|
var probeIndex = (index + i) % capacity;
|
|
|
|
if (hashes[probeIndex] == 0)
|
|
{
|
|
hashes[probeIndex] = hash;
|
|
keys[probeIndex] = key;
|
|
values[probeIndex] = value;
|
|
Count++;
|
|
return;
|
|
}
|
|
|
|
if (hashes[probeIndex] == hash && Equals(keys[probeIndex], key))
|
|
{
|
|
throw new ArgumentException("Key already exists");
|
|
}
|
|
}
|
|
|
|
throw new InvalidOperationException("Dictionary is full after resize attempt.");
|
|
}
|
|
|
|
public bool ContainsKey(TKey key)
|
|
{
|
|
return FindIndex(key) >= 0;
|
|
}
|
|
|
|
public bool TryGetValue(TKey key, out TValue value)
|
|
{
|
|
var index = FindIndex(key);
|
|
if (index >= 0)
|
|
{
|
|
value = values[index];
|
|
return true;
|
|
}
|
|
|
|
value = default(TValue)!;
|
|
return false;
|
|
}
|
|
|
|
public bool Remove(TKey key)
|
|
{
|
|
var index = FindIndex(key);
|
|
if (index < 0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
hashes[index] = 0;
|
|
keys[index] = default(TKey)!;
|
|
values[index] = default(TValue)!;
|
|
Count--;
|
|
return true;
|
|
}
|
|
|
|
public void Clear()
|
|
{
|
|
for (var i = 0; i < capacity; i++)
|
|
{
|
|
hashes[i] = 0;
|
|
keys[i] = default(TKey)!;
|
|
values[i] = default(TValue)!;
|
|
}
|
|
Count = 0;
|
|
}
|
|
|
|
void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item)
|
|
{
|
|
Add(item.Key, item.Value);
|
|
}
|
|
|
|
bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item)
|
|
{
|
|
var index = FindIndex(item.Key);
|
|
return index >= 0 && Equals(values[index], item.Value);
|
|
}
|
|
|
|
bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item)
|
|
{
|
|
var index = FindIndex(item.Key);
|
|
if (index < 0 || !Equals(values[index], item.Value))
|
|
{
|
|
return false;
|
|
}
|
|
hashes[index] = 0;
|
|
keys[index] = default(TKey)!;
|
|
values[index] = default(TValue)!;
|
|
Count--;
|
|
return true;
|
|
}
|
|
|
|
void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
|
|
{
|
|
foreach (var kv in this)
|
|
{
|
|
array[arrayIndex++] = kv;
|
|
}
|
|
}
|
|
|
|
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
|
|
{
|
|
for (var i = 0; i < capacity; i++)
|
|
{
|
|
if (hashes[i] != 0)
|
|
{
|
|
yield return new KeyValuePair<TKey, TValue>(keys[i], values[i]);
|
|
}
|
|
}
|
|
}
|
|
|
|
IEnumerator IEnumerable.GetEnumerator()
|
|
{
|
|
return GetEnumerator();
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
Dispose(true);
|
|
GC.SuppressFinalize(this);
|
|
}
|
|
|
|
public bool TryAdd(TKey key, TValue value)
|
|
{
|
|
if (key == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(key));
|
|
}
|
|
|
|
EnsureCapacity();
|
|
|
|
var hash = key.GetHashCode() & 0x7FFFFFFF;
|
|
var index = hash % capacity;
|
|
|
|
for (var i = 0; i < capacity; i++)
|
|
{
|
|
var probeIndex = (index + i) % capacity;
|
|
|
|
if (hashes[probeIndex] == 0)
|
|
{
|
|
hashes[probeIndex] = hash;
|
|
keys[probeIndex] = key;
|
|
values[probeIndex] = value;
|
|
Count++;
|
|
return true;
|
|
}
|
|
|
|
if (hashes[probeIndex] == hash && Equals(keys[probeIndex], key))
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
throw new InvalidOperationException("Dictionary is full after resize attempt.");
|
|
}
|
|
|
|
private int FindIndex(TKey key)
|
|
{
|
|
if (key == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(key));
|
|
}
|
|
|
|
var hash = key.GetHashCode() & 0x7FFFFFFF;
|
|
var index = hash % capacity;
|
|
|
|
for (var i = 0; i < capacity; i++)
|
|
{
|
|
var probeIndex = (index + i) % capacity;
|
|
if (hashes[probeIndex] == 0)
|
|
{
|
|
return -1;
|
|
}
|
|
if (hashes[probeIndex] == hash && Equals(keys[probeIndex], key))
|
|
{
|
|
return probeIndex;
|
|
}
|
|
}
|
|
|
|
return -1;
|
|
}
|
|
|
|
private void EnsureCapacity()
|
|
{
|
|
if (Count + 1 <= capacity * LoadFactor)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (fixedInitialSize)
|
|
{
|
|
Resize(capacity < 8 ? capacity * 2 : capacity + 1);
|
|
}
|
|
else
|
|
{
|
|
Resize(capacity * 2);
|
|
}
|
|
}
|
|
|
|
private void Resize(int newCapacity)
|
|
{
|
|
int[] newHashes = null!;
|
|
TKey[] newKeys = null!;
|
|
TValue[] newValues = null!;
|
|
|
|
try
|
|
{
|
|
newHashes = intPool.Rent(newCapacity);
|
|
newKeys = keyPool.Rent(newCapacity);
|
|
newValues = valuePool.Rent(newCapacity);
|
|
|
|
Array.Clear(newHashes, 0, newCapacity);
|
|
Array.Clear(newKeys, 0, newCapacity);
|
|
Array.Clear(newValues, 0, newCapacity);
|
|
|
|
for (var i = 0; i < capacity; i++)
|
|
{
|
|
if (hashes[i] == 0)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var hash = hashes[i];
|
|
var index = hash % newCapacity;
|
|
|
|
while (newHashes[index] != 0)
|
|
{
|
|
index = (index + 1) % newCapacity;
|
|
}
|
|
|
|
newHashes[index] = hash;
|
|
newKeys[index] = keys[i];
|
|
newValues[index] = values[i];
|
|
}
|
|
|
|
intPool.Return(hashes, true);
|
|
keyPool.Return(keys, true);
|
|
valuePool.Return(values, true);
|
|
|
|
hashes = newHashes;
|
|
keys = newKeys;
|
|
values = newValues;
|
|
capacity = newCapacity;
|
|
}
|
|
catch
|
|
{
|
|
intPool.Return(newHashes, true);
|
|
keyPool.Return(newKeys, true);
|
|
valuePool.Return(newValues, true);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
~PooledDictionary()
|
|
{
|
|
Dispose(false);
|
|
}
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
{
|
|
if (!disposing)
|
|
{
|
|
return;
|
|
}
|
|
|
|
intPool.Return(hashes, true);
|
|
keyPool.Return(keys, true);
|
|
valuePool.Return(values, true);
|
|
}
|
|
}
|
|
}
|