using System;
using System.Collections.Generic;
using System.Collections;
using System.Globalization;
using System.Text;
using BrainTechLLC.ThreadSafeObjects;
namespace BrainTechLLC.DAL
{
public partial class DBLayer<T> where T : DBLayer<T>, IDBLayer<T>, new()
{
/// <summary>
/// Stores generated (integer) indices on the fly. Each dictionary entry stores a property value name. Associated
/// with the property value name: another dictionary which maps Integer values to "rows" where property value = Integer value
/// </summary>
internal static Dictionary<string, Dictionary<long, ResultSet<T>>> _integerIndices = new Dictionary<string, Dictionary<long, ResultSet<T>>>(32, StringComparer.OrdinalIgnoreCase);
/// <summary>
/// Stores generated (string) indices on the fly. (See above)
/// </summary>
internal static Dictionary<string, Dictionary<string, ResultSet<T>>> _stringIndices = new Dictionary<string, Dictionary<string, ResultSet<T>>>(32, StringComparer.OrdinalIgnoreCase);
internal static void CheckDynamicIntIndices(PropertyNameAndValueCollection pnv, int propNameValueIndex, long numericValue, out int itemCount, out bool createdIndex, ref ResultSet<T> results)
{
createdIndex = false;
Dictionary<long, ResultSet<T>> dynamicIndex;
if (_integerIndices.TryGetValue(pnv._propertyNameAndValuePairs[propNameValueIndex]._propertyName, out dynamicIndex) == true)
{
ResultSet<T> indexedResults;
if (WhereCache.CheckCacheDirty())
{
results.InvalidateCachedProperties();
_integerIndices.Clear();
_stringIndices.Clear();
itemCount = results.RowCount;
}
else
{
// For the value we are intered in (numericValue), see if we have already stored the
// results for a search on this value. If so, start with this shortened result set
// (fewer entries / rows to search)
if (dynamicIndex.TryGetValue(numericValue, out indexedResults) == true)
{
itemCount = indexedResults.RowCount;
results = indexedResults;
}
else
{
createdIndex = true;
itemCount = results.RowCount;
}
}
}
else // no dynamic index found, but one can be made
{
createdIndex = true;
itemCount = results.RowCount;
}
}
internal static void CheckDynamicStringIndices(PropertyNameAndValueCollection pnv, int propNameValueIndex, string stringValue, out int itemCount, out bool createdIndex, ref ResultSet<T> results)
{
createdIndex = false;
Dictionary<string, ResultSet<T>> dynamicStringIndex;
// Handle/look for string indices
if (_stringIndices.TryGetValue(pnv._propertyNameAndValuePairs[propNameValueIndex]._propertyName, out dynamicStringIndex) == true)
{
ResultSet<T> indexedResults;
if (WhereCache.CheckCacheDirty())
{
results.InvalidateCachedProperties(); _integerIndices.Clear(); _stringIndices.Clear();
itemCount = results.RowCount;
}
else
{
if (dynamicStringIndex.TryGetValue(stringValue, out indexedResults) == true)
{
itemCount = indexedResults.RowCount;
results = indexedResults;
}
else
{
createdIndex = true;
itemCount = results.RowCount;
}
}
}
else // no dynamic index found, but one can be made
{
createdIndex = true;
itemCount = results.RowCount;
}
}
}
}