2019-03-29 19:18:22 +01:00
using System.Configuration ;
using CSharpTest.Net.Collections ;
2016-05-27 14:26:28 +02:00
using CSharpTest.Net.Serialization ;
2021-02-18 11:06:02 +01:00
using Umbraco.Cms.Core.Configuration.Models ;
2016-05-27 14:26:28 +02:00
2021-02-18 11:06:02 +01:00
namespace Umbraco.Cms.Infrastructure.PublishedCache.DataSource
2016-05-27 14:26:28 +02:00
{
2017-07-12 14:09:31 +02:00
internal class BTree
2016-05-27 14:26:28 +02:00
{
2021-06-24 09:43:57 -06:00
public static BPlusTree < int , ContentNodeKit > GetTree ( string filepath , bool exists , NuCacheSettings settings , ContentDataSerializer contentDataSerializer = null )
2016-05-27 14:26:28 +02:00
{
var keySerializer = new PrimitiveSerializer ( ) ;
2021-06-24 09:43:57 -06:00
var valueSerializer = new ContentNodeKitSerializer ( contentDataSerializer ) ;
2016-05-27 14:26:28 +02:00
var options = new BPlusTree < int , ContentNodeKit > . OptionsV2 ( keySerializer , valueSerializer )
{
CreateFile = exists ? CreatePolicy . IfNeeded : CreatePolicy . Always ,
FileName = filepath ,
2019-04-02 09:40:14 +02:00
// read or write but do *not* keep in memory
2019-03-29 19:18:22 +01:00
CachePolicy = CachePolicy . None ,
// default is 4096, min 2^9 = 512, max 2^16 = 64K
2020-03-03 11:18:54 +00:00
FileBlockSize = GetBlockSize ( settings ) ,
2019-03-29 19:18:22 +01:00
2020-04-28 12:31:15 +02:00
//HACK: Forces FileOptions to be WriteThrough here: https://github.com/mamift/CSharpTest.Net.Collections/blob/9f93733b3af7ee0e2de353e822ff54d908209b0b/src/CSharpTest.Net.Collections/IO/TransactedCompoundFile.cs#L316-L327,
// as the reflection uses otherwise will failed in .NET Core as the "_handle" field in FileStream is renamed to "_fileHandle".
StoragePerformance = StoragePerformance . CommitToDisk ,
2016-05-27 14:26:28 +02:00
// other options?
} ;
var tree = new BPlusTree < int , ContentNodeKit > ( options ) ;
// anything?
//btree.
return tree ;
2021-06-24 09:43:57 -06:00
2016-05-27 14:26:28 +02:00
}
2020-08-21 14:52:47 +01:00
private static int GetBlockSize ( NuCacheSettings settings )
2019-03-29 19:18:22 +01:00
{
var blockSize = 4096 ;
2020-03-03 11:18:54 +00:00
var appSetting = settings . BTreeBlockSize ;
2020-09-18 12:54:17 +02:00
if ( ! appSetting . HasValue )
2019-03-29 19:18:22 +01:00
return blockSize ;
2020-09-18 12:54:17 +02:00
blockSize = appSetting . Value ;
2019-03-29 19:18:22 +01:00
var bit = 0 ;
for ( var i = blockSize ; i ! = 1 ; i > > = 1 )
bit + + ;
if ( 1 < < bit ! = blockSize )
throw new ConfigurationErrorsException ( $"Invalid block size value \" { blockSize } \ ": must be a power of two." ) ;
if ( blockSize < 512 | | blockSize > 65536 )
throw new ConfigurationErrorsException ( $"Invalid block size value \" { blockSize } \ ": must be >= 512 and <= 65536." ) ;
return blockSize ;
}
2016-05-27 14:26:28 +02:00
/ *
class ListOfIntSerializer : ISerializer < List < int > >
{
public List < int > ReadFrom ( Stream stream )
{
var list = new List < int > ( ) ;
var count = PrimitiveSerializer . Int32 . ReadFrom ( stream ) ;
for ( var i = 0 ; i < count ; i + + )
list . Add ( PrimitiveSerializer . Int32 . ReadFrom ( stream ) ) ;
return list ;
}
public void WriteTo ( List < int > value , Stream stream )
{
PrimitiveSerializer . Int32 . WriteTo ( value . Count , stream ) ;
foreach ( var item in value )
PrimitiveSerializer . Int32 . WriteTo ( item , stream ) ;
}
}
* /
}
}