Skip to content

Commit

Permalink
Restructured and combined query options/attributes.
Browse files Browse the repository at this point in the history
  • Loading branch information
NTDLS committed Oct 9, 2024
1 parent 1c27a46 commit 2b1d2a5
Show file tree
Hide file tree
Showing 29 changed files with 155 additions and 213 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using NTDLS.Katzebase.Api.Exceptions;
using Newtonsoft.Json;
using NTDLS.Katzebase.Api.Exceptions;
using NTDLS.Katzebase.Engine.Atomicity;
using NTDLS.Katzebase.Engine.Interactions.APIHandlers;
using NTDLS.Katzebase.Engine.Interactions.QueryHandlers;
using NTDLS.Katzebase.Shared;
using System.Text.Json;
using static NTDLS.Katzebase.Parsers.Query.SupportingTypes.PreparedQuery;
using NTDLS.Katzebase.Parsers.Query.SupportingTypes;

namespace NTDLS.Katzebase.Engine.Interactions.Management
{
Expand Down Expand Up @@ -34,74 +33,36 @@ internal EnvironmentManager(EngineCore core)
}
}

static void UpdateSettingProperty<T>(T obj, string propertyName, object newValue)
{
Type type = typeof(T);
var property = type.GetProperty(propertyName);
if (property != null && property.CanWrite)
{
property.SetValue(obj, Convert.ChangeType(newValue, property.PropertyType));
}
}

internal void Alter(Transaction transaction, IReadOnlyDictionary<QueryAttribute, object> attributes)
internal void Alter(Transaction transaction, IReadOnlyDictionary<string, QueryAttribute> configuration)
{
try
{
var appSettingsPath = Path.Combine(AppContext.BaseDirectory, "appsettings.json");

if (!File.Exists(appSettingsPath))
{
throw new KbEngineException($"Could not locate configuration file: [{appSettingsPath}[.");
}

string json = File.ReadAllText(appSettingsPath);
var settingsType = _core.Settings.GetType();

// Parse the JSON into a JsonDocument
using JsonDocument document = JsonDocument.Parse(json);
var root = document.RootElement;
//var settingsElement = root.GetProperty("Settings");
var settings = JsonSerializer.Deserialize<KatzebaseSettings>(root.ToString());

foreach (var settingElement in root.EnumerateObject())
foreach (var item in configuration)
{
if (Enum.TryParse(settingElement.Name, true, out QueryAttribute optionType))
//Update the running engine setting.
var property = settingsType.GetProperty(item.Key);
if (property != null && property.CanWrite)
{
if (attributes.TryGetValue(optionType, out var value))
{
UpdateSettingProperty(settings, settingElement.Name, value); //Save the value in the JSON settings file.
UpdateSettingProperty(_core.Settings, settingElement.Name, value); //Save the setting in the live core.
}
property.SetValue(_core.Settings, Convert.ChangeType(item.Value.Value, property.PropertyType));
}
}

string updatedSettingsJson = JsonSerializer.Serialize(settings);
using var file = File.Create(appSettingsPath);
using (var writer = new Utf8JsonWriter(file, new JsonWriterOptions { Indented = true }))
{
writer.WriteStartObject();
foreach (var property in root.EnumerateObject())
{
if (property.Name == "Settings")
{
writer.WritePropertyName(property.Name);
writer.WriteRawValue(updatedSettingsJson);
}
else
{
property.WriteTo(writer);
}
}
writer.WriteEndObject();
}
file.Close();
//Save the new settings to file.
File.WriteAllText(appSettingsPath, JsonConvert.SerializeObject(_core.Settings, Formatting.Indented));
}
catch (Exception ex)
{
LogManager.Error($"Failed to alter environment manager for process id {transaction.ProcessId}.", ex);
throw;
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ internal KbQueryDocumentListResult ExecuteSelectInto(SessionState session, Prepa
try
{
using var transactionReference = _core.Transactions.APIAcquire(session);
var targetSchema = preparedQuery.GetAttribute<string>(PreparedQuery.QueryAttribute.TargetSchemaName);
var targetSchema = preparedQuery.GetAttribute<string>(PreparedQuery.Attribute.TargetSchemaName);

var physicalTargetSchema = _core.Schemas.AcquireVirtual(transactionReference.Transaction, targetSchema.EnsureNotNull(), LockOperation.Write, LockOperation.Read);
if (physicalTargetSchema.Exists == false)
Expand Down Expand Up @@ -194,7 +194,7 @@ internal KbActionResponse ExecuteUpdate(SessionState session, PreparedQuery prep
{
using var transactionReference = _core.Transactions.APIAcquire(session);

var targetSchemaAlias = preparedQuery.GetAttribute<string>(PreparedQuery.QueryAttribute.TargetSchemaAlias);
var targetSchemaAlias = preparedQuery.GetAttribute<string>(PreparedQuery.Attribute.TargetSchemaAlias);
var targetSchema = preparedQuery.Schemas.Where(o => o.Alias.Is(targetSchemaAlias)).Single();

var physicalSchema = _core.Schemas.Acquire(transactionReference.Transaction, targetSchema.Name, LockOperation.Read);
Expand Down Expand Up @@ -332,7 +332,7 @@ internal KbActionResponse ExecuteDelete(SessionState session, PreparedQuery prep
{
using var transactionReference = _core.Transactions.APIAcquire(session);

var targetSchemaAlias = preparedQuery.GetAttribute<string>(PreparedQuery.QueryAttribute.TargetSchemaAlias);
var targetSchemaAlias = preparedQuery.GetAttribute<string>(PreparedQuery.Attribute.TargetSchemaAlias);
var firstSchema = preparedQuery.Schemas.Where(o => o.Alias.Is(targetSchemaAlias)).Single();

var physicalSchema = _core.Schemas.Acquire(transactionReference.Transaction, firstSchema.Name, LockOperation.Delete);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal KbActionResponse ExecuteDrop(SessionState session, PreparedQuery prepar
if (preparedQuery.SubQueryType == SubQueryType.Index || preparedQuery.SubQueryType == SubQueryType.UniqueKey)
{
_core.Indexes.DropIndex(transactionReference.Transaction, schemaName,
preparedQuery.GetAttribute<string>(PreparedQuery.QueryAttribute.IndexName));
preparedQuery.GetAttribute<string>(PreparedQuery.Attribute.IndexName));
}
else
{
Expand All @@ -61,8 +61,8 @@ internal KbQueryDocumentListResult ExecuteAnalyze(SessionState session, Prepared
using var transactionReference = _core.Transactions.APIAcquire(session);

var analysis = _core.Indexes.AnalyzeIndex(transactionReference.Transaction,
preparedQuery.GetAttribute<string>(PreparedQuery.QueryAttribute.Schema),
preparedQuery.GetAttribute<string>(PreparedQuery.QueryAttribute.IndexName));
preparedQuery.GetAttribute<string>(PreparedQuery.Attribute.Schema),
preparedQuery.GetAttribute<string>(PreparedQuery.Attribute.IndexName));

transactionReference.Transaction.AddMessage(analysis, KbMessageType.Verbose);

Expand All @@ -83,8 +83,8 @@ internal KbActionResponse ExecuteRebuild(SessionState session, PreparedQuery pre
using var transactionReference = _core.Transactions.APIAcquire(session);
string schemaName = preparedQuery.Schemas.First().Name;

var indexName = preparedQuery.GetAttribute<string>(PreparedQuery.QueryAttribute.IndexName);
var indexPartitions = preparedQuery.GetAttribute(PreparedQuery.QueryAttribute.Partitions, _core.Settings.DefaultIndexPartitions);
var indexName = preparedQuery.GetAttribute<string>(PreparedQuery.Attribute.IndexName);
var indexPartitions = preparedQuery.GetAttribute(PreparedQuery.Attribute.Partitions, _core.Settings.DefaultIndexPartitions);

_core.Indexes.RebuildIndex(transactionReference.Transaction, schemaName, indexName, indexPartitions);
return transactionReference.CommitAndApplyMetricsThenReturnResults();
Expand All @@ -105,12 +105,12 @@ internal KbActionResponse ExecuteCreate(SessionState session, PreparedQuery prep
if (preparedQuery.SubQueryType == SubQueryType.Index || preparedQuery.SubQueryType == SubQueryType.UniqueKey)
{
var indexPartitions = preparedQuery.GetAttribute(
PreparedQuery.QueryAttribute.Partitions, _core.Settings.DefaultIndexPartitions);
PreparedQuery.Attribute.Partitions, _core.Settings.DefaultIndexPartitions);

var index = new KbIndex
{
Name = preparedQuery.GetAttribute<string>(PreparedQuery.QueryAttribute.IndexName),
IsUnique = preparedQuery.GetAttribute<bool>(PreparedQuery.QueryAttribute.IsUnique),
Name = preparedQuery.GetAttribute<string>(PreparedQuery.Attribute.IndexName),
IsUnique = preparedQuery.GetAttribute<bool>(PreparedQuery.Attribute.IsUnique),
Partitions = indexPartitions
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ internal KbActionResponse ExecuteCreate(SessionState session, PreparedQuery prep

if (preparedQuery.SubQueryType == SubQueryType.Procedure)
{
var objectName = preparedQuery.GetAttribute<string>(PreparedQuery.QueryAttribute.ObjectName);
var objectSchema = preparedQuery.GetAttribute<string>(PreparedQuery.QueryAttribute.Schema);
var parameters = preparedQuery.GetAttribute<List<PhysicalProcedureParameter>>(PreparedQuery.QueryAttribute.Parameters);
var Batches = preparedQuery.GetAttribute<List<string>>(PreparedQuery.QueryAttribute.Batches);
var objectName = preparedQuery.GetAttribute<string>(PreparedQuery.Attribute.ObjectName);
var objectSchema = preparedQuery.GetAttribute<string>(PreparedQuery.Attribute.Schema);
var parameters = preparedQuery.GetAttribute<List<PhysicalProcedureParameter>>(PreparedQuery.Attribute.Parameters);
var Batches = preparedQuery.GetAttribute<List<string>>(PreparedQuery.Attribute.Batches);

_core.Procedures.CreateCustomProcedure(transactionReference.Transaction, objectSchema, objectName, parameters, Batches);
}
Expand All @@ -64,8 +64,8 @@ internal KbQueryResultCollection ExecuteExec(SessionState session, PreparedQuery
{
try
{
var schemaName = preparedQuery.GetAttribute<string>(PreparedQuery.QueryAttribute.Schema);
var objectName = preparedQuery.GetAttribute<string>(PreparedQuery.QueryAttribute.ObjectName);
var schemaName = preparedQuery.GetAttribute<string>(PreparedQuery.Attribute.Schema);
var objectName = preparedQuery.GetAttribute<string>(PreparedQuery.Attribute.ObjectName);

using var transactionReference = _core.Transactions.APIAcquire(session);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ internal KbQueryDocumentListResult ExecuteAnalyze(SessionState session, Prepared

if (preparedQuery.SubQueryType == SubQueryType.Schema)
{
var includePhysicalPages = preparedQuery.GetAttribute(PreparedQuery.QueryAttribute.IncludePhysicalPages, false);
var includePhysicalPages = preparedQuery.GetAttribute(PreparedQuery.Attribute.IncludePhysicalPages, false);
result = _core.Schemas.AnalyzePages(transactionReference.Transaction, schemaName, includePhysicalPages);
}
else
Expand Down Expand Up @@ -89,7 +89,7 @@ internal KbActionResponse ExecuteAlter(SessionState session, PreparedQuery prepa

if (preparedQuery.SubQueryType == SubQueryType.Schema)
{
var pageSize = preparedQuery.GetAttribute(PreparedQuery.QueryAttribute.PageSize, _core.Settings.DefaultDocumentPageSize);
var pageSize = preparedQuery.GetAttribute(PreparedQuery.Attribute.PageSize, _core.Settings.DefaultDocumentPageSize);
string schemaName = preparedQuery.Schemas.Single().Name;
_core.Schemas.Alter(transactionReference.Transaction, schemaName, pageSize);
}
Expand All @@ -115,7 +115,7 @@ internal KbActionResponse ExecuteCreate(SessionState session, PreparedQuery prep

if (preparedQuery.SubQueryType == SubQueryType.Schema)
{
var pageSize = preparedQuery.GetAttribute(PreparedQuery.QueryAttribute.PageSize, _core.Settings.DefaultDocumentPageSize);
var pageSize = preparedQuery.GetAttribute(PreparedQuery.Attribute.PageSize, _core.Settings.DefaultDocumentPageSize);
string schemaName = preparedQuery.Schemas.Single().Name;
_core.Schemas.CreateSingleSchema(transactionReference.Transaction, schemaName, pageSize);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ internal KbActionResponse ExecuteKillProcess(SessionState session, PreparedQuery
try
{
using var transactionReference = _core.Transactions.APIAcquire(session);
var referencedProcessId = preparedQuery.GetAttribute<ulong>(PreparedQuery.QueryAttribute.ProcessId);
var referencedProcessId = preparedQuery.GetAttribute<ulong>(PreparedQuery.Attribute.ProcessId);

_core.Sessions.CloseByProcessId(referencedProcessId);
return transactionReference.CommitAndApplyMetricsThenReturnResults();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ private void TextArea_MouseMove(object sender, System.Windows.Input.MouseEventAr
_tooltipTimer.Stop();
}
}
else
{
_toolTip.IsOpen = false; // Hide tooltip if no marker
_tooltipTimer.Stop();
}
}
catch { }
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using NTDLS.Katzebase.Parsers.Query.Specific.WithOptions;
using NTDLS.Katzebase.Parsers.Query.SupportingTypes;
using NTDLS.Katzebase.Parsers.Query.SupportingTypes;
using NTDLS.Katzebase.Parsers.Tokens;
using NTDLS.Katzebase.Shared;
using System.Reflection;
Expand All @@ -18,7 +17,7 @@ internal static PreparedQuery Parse(QueryBatch queryBatch, Tokenizer tokenizer)

tokenizer.EatIfNext("with");

var options = new ExpectedWithOptions();
var options = new ExpectedQueryAttributes();

var properties = typeof(KatzebaseSettings).GetProperties(BindingFlags.Public | BindingFlags.Instance);

Expand All @@ -27,7 +26,7 @@ internal static PreparedQuery Parse(QueryBatch queryBatch, Tokenizer tokenizer)
options.Add(property.Name, property.PropertyType);
}

query.AddAttributes(StaticParserWithOptions.Parse(tokenizer, options));
query.AddAttributes(StaticParserAttributes.Parse(tokenizer, options));

return query;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using NTDLS.Katzebase.Api.Exceptions;
using NTDLS.Katzebase.Parsers.Query.Specific.WithOptions;
using NTDLS.Katzebase.Parsers.Query.SupportingTypes;
using NTDLS.Katzebase.Parsers.Tokens;
using static NTDLS.Katzebase.Parsers.Constants;
Expand All @@ -24,11 +23,11 @@ internal static PreparedQuery Parse(QueryBatch queryBatch, Tokenizer tokenizer)

tokenizer.EatIfNext("with");
{
var options = new ExpectedWithOptions
var options = new ExpectedQueryAttributes
{
{"pagesize", typeof(uint) }
};
query.AddAttributes(StaticParserWithOptions.Parse(tokenizer, options));
query.AddAttributes(StaticParserAttributes.Parse(tokenizer, options));
}

return query;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal static PreparedQuery Parse(QueryBatch queryBatch, Tokenizer tokenizer)
{
throw new KbParserException(tokenizer.GetCurrentLineNumber(), $"Expected index name, found: [{indexName}].");
}
query.AddAttribute(PreparedQuery.QueryAttribute.IndexName, indexName);
query.AddAttribute(PreparedQuery.Attribute.IndexName, indexName);

tokenizer.EatIfNext("on");

Expand All @@ -28,7 +28,7 @@ internal static PreparedQuery Parse(QueryBatch queryBatch, Tokenizer tokenizer)
throw new KbParserException(tokenizer.GetCurrentLineNumber(), $"Expected schema name, found: [{schemaName}].");
}
query.Schemas.Add(new QuerySchema(tokenizer.GetCurrentLineNumber(), schemaName, QuerySchemaUsageType.Primary));
query.AddAttribute(PreparedQuery.QueryAttribute.Schema, schemaName);
query.AddAttribute(PreparedQuery.Attribute.Schema, schemaName);
return query;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using NTDLS.Katzebase.Api.Exceptions;
using NTDLS.Katzebase.Parsers.Query.Specific.WithOptions;
using NTDLS.Katzebase.Parsers.Query.SupportingTypes;
using NTDLS.Katzebase.Parsers.Tokens;
using static NTDLS.Katzebase.Parsers.Constants;
Expand All @@ -26,11 +25,11 @@ internal static PreparedQuery Parse(QueryBatch queryBatch, Tokenizer tokenizer)

if (tokenizer.TryEatIfNext("with"))
{
var options = new ExpectedWithOptions
var options = new ExpectedQueryAttributes
{
{"includephysicalpages", typeof(bool) }
};
query.AddAttributes(StaticParserWithOptions.Parse(tokenizer, options));
query.AddAttributes(StaticParserAttributes.Parse(tokenizer, options));
}

return query;
Expand Down
Loading

0 comments on commit 2b1d2a5

Please sign in to comment.