Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
54 changes: 52 additions & 2 deletions csharp/Platform.Data.Doublets.Xml.Exporter/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
var linksFilePath = ConsoleHelpers.GetOrReadArgument(argumentIndex++, "Links file path", args);
var xmlFilePath = ConsoleHelpers.GetOrReadArgument(argumentIndex++, "XML file path", args);
var defaultDocumentName = Path.GetFileNameWithoutExtension(xmlFilePath);
var documentName = ConsoleHelpers.GetOrReadArgument(argumentIndex, $"Document name (default: {defaultDocumentName})", args);
var documentName = ConsoleHelpers.GetOrReadArgument(argumentIndex++, $"Document name (default: {defaultDocumentName})", args);
if (string.IsNullOrWhiteSpace(documentName))
{
documentName = defaultDocumentName;
}
var growSizeString = ConsoleHelpers.GetOrReadArgument(argumentIndex, "Database grow size in bytes (optional, default: auto)", args);
var growSize = ParseGrowSize(growSizeString);
if (!File.Exists(linksFilePath))
{
Console.WriteLine($"${linksFilePath} file does not exist.");
Expand All @@ -32,7 +34,7 @@
};
var xmlWriter = XmlWriter.Create(xmlFileStream, xmlWriterSettings);
var linksConstants = new LinksConstants<TLinkAddress>(enableExternalReferencesSupport: true);
using UnitedMemoryLinks<TLinkAddress> memoryAdapter = new (new FileMappedResizableDirectMemory(linksFilePath), UnitedMemoryLinks<TLinkAddress>.DefaultLinksSizeStep, linksConstants, IndexTreeType.Default);
using UnitedMemoryLinks<TLinkAddress> memoryAdapter = new (new FileMappedResizableDirectMemory(linksFilePath), growSize, linksConstants, IndexTreeType.Default);
var links = memoryAdapter.DecorateWithAutomaticUniquenessAndUsagesResolution();
var balancedVariantConverter = new BalancedVariantConverter<TLinkAddress>(links);
var storage = new DefaultXmlStorage<TLinkAddress>(links, balancedVariantConverter);
Expand All @@ -43,5 +45,53 @@
Console.WriteLine("Press CTRL+C to stop.");
exporter.Export(xmlWriter, document, cancellationToken);
}

private static long ParseGrowSize(string growSizeString)
{
if (string.IsNullOrWhiteSpace(growSizeString))
{
return UnitedMemoryLinks<TLinkAddress>.DefaultLinksSizeStep;
}

growSizeString = growSizeString.Trim().ToUpperInvariant();

// Handle byte suffixes
long multiplier = 1;
if (growSizeString.EndsWith("KB"))
{
multiplier = 1024;
growSizeString = growSizeString[..^2];
}
else if (growSizeString.EndsWith("MB"))
{
multiplier = 1024 * 1024;
growSizeString = growSizeString[..^2];
}
else if (growSizeString.EndsWith("GB"))
{
multiplier = 1024 * 1024 * 1024;
growSizeString = growSizeString[..^2];
}
else if (growSizeString.EndsWith("B"))

Check notice on line 75 in csharp/Platform.Data.Doublets.Xml.Exporter/Program.cs

View check run for this annotation

codefactor.io / CodeFactor

csharp/Platform.Data.Doublets.Xml.Exporter/Program.cs#L75

Use 'string.EndsWith(char)' instead of 'string.EndsWith(string)' when you have a string with a single char. (CA1866)
{
growSizeString = growSizeString[..^1];
}

if (!long.TryParse(growSizeString.Trim(), out long value))
{
Console.WriteLine($"Invalid grow size format: {growSizeString}. Using default value.");
return UnitedMemoryLinks<TLinkAddress>.DefaultLinksSizeStep;
}

var totalBytes = value * multiplier;

if (totalBytes <= 0)
{
Console.WriteLine($"Invalid grow size: {totalBytes}. Must be positive. Using default value.");
return UnitedMemoryLinks<TLinkAddress>.DefaultLinksSizeStep;
}

return totalBytes;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
54 changes: 52 additions & 2 deletions csharp/Platform.Data.Doublets.Xml.Importer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@
}
var linksFilePath = ConsoleHelpers.GetOrReadArgument(argumentIndex++, "Links storage file path", args);
var defaultDocumentName = Path.GetFileNameWithoutExtension(xmlFilePath);
var documentName = ConsoleHelpers.GetOrReadArgument(argumentIndex, $"Document name (default: {defaultDocumentName})", args);
var documentName = ConsoleHelpers.GetOrReadArgument(argumentIndex++, $"Document name (default: {defaultDocumentName})", args);
if (string.IsNullOrWhiteSpace(documentName))
{
documentName = defaultDocumentName;
}
var growSizeString = ConsoleHelpers.GetOrReadArgument(argumentIndex, "Database grow size in bytes (optional, default: auto)", args);
var growSize = ParseGrowSize(growSizeString);
var xmlReader = XmlReader.Create(xmlFilePath);
var linksConstants = new LinksConstants<TLinkAddress>(enableExternalReferencesSupport: true);
var fileMappedResizableDirectMemory = new FileMappedResizableDirectMemory(linksFilePath);
var unitedMemoryLinks = UnitedMemoryLinks<TLinkAddress>.DefaultLinksSizeStep;
var unitedMemoryLinks = growSize;
const IndexTreeType indexTreeType = IndexTreeType.Default;
using UnitedMemoryLinks<TLinkAddress> memoryAdapter = new(fileMappedResizableDirectMemory, unitedMemoryLinks, linksConstants, indexTreeType);
var links = memoryAdapter.DecorateWithAutomaticUniquenessAndUsagesResolution();
Expand All @@ -42,5 +44,53 @@
importer.Import(xmlReader, documentName, cancellationToken);
Console.WriteLine("Import completed successfully.");
}

private static long ParseGrowSize(string growSizeString)
{
if (string.IsNullOrWhiteSpace(growSizeString))
{
return UnitedMemoryLinks<TLinkAddress>.DefaultLinksSizeStep;
}

growSizeString = growSizeString.Trim().ToUpperInvariant();

// Handle byte suffixes
long multiplier = 1;
if (growSizeString.EndsWith("KB"))
{
multiplier = 1024;
growSizeString = growSizeString[..^2];
}
else if (growSizeString.EndsWith("MB"))
{
multiplier = 1024 * 1024;
growSizeString = growSizeString[..^2];
}
else if (growSizeString.EndsWith("GB"))
{
multiplier = 1024 * 1024 * 1024;
growSizeString = growSizeString[..^2];
}
else if (growSizeString.EndsWith("B"))

Check notice on line 74 in csharp/Platform.Data.Doublets.Xml.Importer/Program.cs

View check run for this annotation

codefactor.io / CodeFactor

csharp/Platform.Data.Doublets.Xml.Importer/Program.cs#L74

Use 'string.EndsWith(char)' instead of 'string.EndsWith(string)' when you have a string with a single char. (CA1866)
{
growSizeString = growSizeString[..^1];
}

if (!long.TryParse(growSizeString.Trim(), out long value))
{
Console.WriteLine($"Invalid grow size format: {growSizeString}. Using default value.");
return UnitedMemoryLinks<TLinkAddress>.DefaultLinksSizeStep;
}

var totalBytes = value * multiplier;

if (totalBytes <= 0)
{
Console.WriteLine($"Invalid grow size: {totalBytes}. Must be positive. Using default value.");
return UnitedMemoryLinks<TLinkAddress>.DefaultLinksSizeStep;
}

return totalBytes;
}
}
}
Binary file added experiments/test.links
Binary file not shown.
Binary file added experiments/test2.links
Binary file not shown.
Binary file added experiments/test3.links
Binary file not shown.
4 changes: 4 additions & 0 deletions experiments/test_grow_size.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<test>Hello World</test>
</root>
Binary file added experiments/test_invalid.links
Binary file not shown.
4 changes: 4 additions & 0 deletions experiments/test_output.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<test>Hello World</test>
</root>
Loading