80 lines
2.3 KiB
C#
80 lines
2.3 KiB
C#
// /**
|
|
// * File: StorageAreaContainer.cs
|
|
// * Author: haraldwolff
|
|
// *
|
|
// * This file and it's content is copyrighted by the Author and / or copyright holder.
|
|
// * Any use wihtout proper permission is illegal and may lead to legal actions.
|
|
// *
|
|
// *
|
|
// **/
|
|
using System;
|
|
using ln.types.btree;
|
|
using ln.logging;
|
|
namespace ln.types.odb.ng.storage
|
|
{
|
|
public class StorageAreaContainer
|
|
{
|
|
public bool DEBUG = false;
|
|
|
|
public int SplitLimit { get; set; } = 32;
|
|
|
|
MappingBTree<int, StorageArea> storageAreas = new MappingBTree<int, StorageArea>((value)=>value.Offset);
|
|
|
|
public StorageAreaContainer()
|
|
{
|
|
}
|
|
|
|
public StorageArea Push(StorageArea storageArea)
|
|
{
|
|
if (DEBUG)
|
|
Logging.Log(LogLevel.DEBUG, "StorageAreaContainer: Push: {0}", storageArea);
|
|
|
|
storageAreas.Add(storageArea);
|
|
|
|
try
|
|
{
|
|
StorageArea previousStorageArea = storageAreas.Previous(storageArea);
|
|
if ((previousStorageArea != null) && (previousStorageArea.NextOffset == storageArea.Offset))
|
|
{
|
|
previousStorageArea.Size += storageArea.Size;
|
|
storageAreas.Remove(storageArea);
|
|
storageArea = previousStorageArea;
|
|
}
|
|
} catch
|
|
{
|
|
}
|
|
|
|
try
|
|
{
|
|
StorageArea nextStorageArea = storageAreas.Next(storageArea);
|
|
if ((nextStorageArea != null) && (storageArea.NextOffset == nextStorageArea.Offset))
|
|
{
|
|
storageArea.Size += nextStorageArea.Size;
|
|
storageAreas.Remove(nextStorageArea);
|
|
}
|
|
} catch
|
|
{ }
|
|
|
|
|
|
return storageArea;
|
|
}
|
|
|
|
public StorageArea Pop(int minSize)
|
|
{
|
|
foreach (StorageArea storageArea in storageAreas)
|
|
{
|
|
if (storageArea.Size >= minSize)
|
|
{
|
|
if (DEBUG)
|
|
Logging.Log(LogLevel.DEBUG, "StorageAreaContainer: Pop: {0}", storageArea);
|
|
|
|
storageAreas.RemoveKey(storageArea.Offset);
|
|
return storageArea;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
}
|
|
}
|