Home

Awesome

FriendlyCSharp.Databases

A library of cross platform C# data structures. Generic B-tree written in C#, which can be replaced with NoSQL database stored in the memory of discharge requirements in real-time (Firebase, Redis Cache, SAP HANA, Exadata, OLTP, etc.). Basic information B-tree can be found in the book N. Wirth, Algorithms + data structures = programs and on Wikipedia, namely:

"In computer science, a B-tree is a self-balancing tree data structure that keeps data sorted and allows searches, sequential access, insertions, and deletions in logarithmic time. The B-tree is a generalization of a binary search tree in that a node can have more than two children (Comer 1979, p. 123). Unlike self-balancing binary search trees, the B-tree is optimized for systems that read and write large blocks of data. B-trees are a good example of a data structure for external memory. It is commonly used in databases and filesystems. (...) Rudolf Bayer and Ed McCreight invented the B-tree while working at Boeing Research Labs in 1971 (Bayer & McCreight 1972), but they did not explain what, if anything, the B stands for." - Wikipedia.

 

B-Tree generic class

FcsBTreeN<TKey, TValue> [where TKey : struct, IComparable<TKey>]

FcsFastBTreeN<TKey, TValue> [where TKey : struct, IComparable<TKey>]

FcsDuplValueFastBTreeN<TKey, TValue> [where TKey : struct, IComparable<TKey>]

FcsKeyFastBTreeN<TKeyValue> [where TKeyValue : struct]

FcsLockBTreeN<TKey, TValue> [where TKey : struct, IComparable<TKey>]

FcsFastLockBTreeN<TKey, TValue> [where TKey : struct, IComparable<TKey>]

Samples

Performance

A B-tree of order m is a tree which satisfies the following properties:

  1. Every node has at most m children.
  2. Every non-leaf node (except root) has at least ⌈m/2⌉ children.
  3. The root has at least two children if it is not a leaf node.
  4. A non-leaf node with k children contains k−1 keys.
  5. All leaves appear in the same level.
generic classsorted by keyduplicate keysB-treelocking records
fastDB<...>YesYesYesYes
FcsBTreeN<TKey, TValue>YesYesYesNo
FcsFastBTreeN<TKey, TValue>YesYesYesNo
FcsLockBTreeN<TKey, TValue>YesYesYesNo
FcsFastLockBTreeN<TKey, TValue>YesYesYesNo
FcsKeyFastBTreeN<TKeyValue>YesYesYesNo
SortedSet<KeyValuePair<TKey, TValue>>YesNoNoNo
HashSet<KeyValuePair<TKey, TValue>>NoNoNoNo
Dictionary<TKey, TValue>NoNoNoNo

Benchmark

The benchmark was configured as follows:

Adding in a single thread:

<int, uint>sorted by keyiterationtotal (ms)one time (ns)speedRAM (MB)occupied
FcsFastBTreeN<...>Yes10,000,0006,185619100%131100%
SortedSet<...>Yes10,000,000 19,443  1,944  32%  458  358% 
HashSet<...>No10,000,0002,017202307%229179%
Dictionary<...>No10,000,0001,378138449%229179%

Foreach in a single thread:

<int, uint>sorted by keyiterationtotal (ms)one time (ns)speedIOPS
fastDB<...>Yes10,000,00010010200%100,000,000
FcsFastBTreeN<...>Yes10,000,00020020100%50,000,000
SortedSet<...>Yes10,000,000 1,230  123  16% 8,000,000
HashSet<...>No10,000,00047.34,73422%210,000,000
Dictionary<...>No10,000,00086.58,65231%115,000,000

Functions

Various methods used throughout the library.

Comparator

Some data structures require a comparator method to automatically keep their elements sorted upon insertion.

Default comparator is initialized as follows:

protected virtual int BtnCompares(TKey keyX, TKey keyY, object objCmp)
{
  return keyX.CompareTo(keyY);
}

Writing custom class with comparators is easy:

public class MyBtnKeyValue : FcsBTreeN<int, uint>
{
  protected override bool BtnUpdates(int keyAdd, uint valueAdd, ref uint valueUpdates, object objUpdates)
  {
    valueUpdates++;
    return true;
  }
  //////////////////////////
  protected override int BtnCompares(int keyX, int keyY, object objCmp)
  {
    return keyX - keyY;
  }
  //////////////////////////
  public MyBtnKeyValue() : base()
  {
  }
}

Iterator

Tree gradually passes from the lowest, from the specified keys or higher.

Typical usage:

foreach(KeyValuePair<TKey, TValue>? btnKV in MyBtnKeyValue)
{
}

Other usages:

if (MyBtnKeyValue.BtnFirst(out btnKey, out btnValue) != null)
{
  do
  {
  }
  while (MyBtnKeyValue.BtnNext(ref btnKey, out btnValue) != null)
}
if (MyBtnKeyValue.BtnFind(btnKey, out btnValue) != null)
{
  do
  {
  }
  while (MyBtnKeyValue.BtnNext(ref btnKey, out btnValue) != null)
}
if (MyBtnKeyValue.BtnSearch(ref btnKey, out btnValue) != null)
{
  do
  {
  }
  while (MyBtnKeyValue.BtnNext(ref btnKey, out btnValue) != null)
}

Reverse Iterator

The tree passes successively from the last or entered or lower than the specified key.

Typical usage of iteration in reverse:

if (MyBtnKeyValue.BtnLast(out btnKey, out btnValue) != null)
{
  do
  {
  }
  while (MyBtnKeyValue.BtnPrev(ref btnKey, out btnValue) != null)
}

Other usages:

if (MyBtnKeyValue.BtnFind(btnKey, out btnValue) != null)
{
  do
  {
  }
  while (MyBtnKeyValue.BtnPrev(ref btnKey, out btnValue) != null)
}
if (MyBtnKeyValue.BtnSearchPrev(btnKey, out btnValue) != null)
{
  do
  {
  }
  while (MyBtnKeyValue.BtnPrev(ref btnKey, out btnValue) != null)
}

Searching

Methods that seek the desired key and returns the key value pair or null.

Find

The method finds the specified key and returns the key value pair or null.

public virtual  bool? BtnFind(TKey key, out TValue value)
{
}
public virtual KeyValuePair<TKey, TValue>? BtnFind(TKey key)
{
}

First

The method finds the first key and returns the key value pair or null.

public virtual bool? BtnFirst(out TKey key, out TValue value)
{
}
public virtual KeyValuePair<TKey, TValue>? BtnFirst()
{
}

Last

The method finds the last key and returns the key value pair or null.

public virtual bool? BtnLast(out TKey key, out TValue value)
{
}
public virtual KeyValuePair<TKey, TValue>? BtnLast()
{
}

Search

The method finds the specified key or the next higher and returns the key value pair or null.

public virtual bool? BtnSearch(ref TKey key, out TValue value)
{
}
public virtual KeyValuePair<TKey, TValue>? BtnSearch(TKey key)
{
}

SearchPrev

The method finds the specified key or the previous and returns the key value pair or null.

public virtual bool? BtnSearchPrev(ref TKey key, out TValue value)
{
}
public virtual KeyValuePair<TKey, TValue>? BtnSearchPrev(TKey key)
{
}

 

MemoryStream generic class

FcsInmemStream<T> [where T : struct, ICloneable]

Samples

Benchmark

The benchmark was configured as follows:

FcsInmemStream<T>AppendReadWriteforeach
IOPS [T = 8 Byte]160,000,000800,000,000800,000,00080,000,000
IOPS [T = 16 Byte]140,000,000500,000,000400,000,00080,000,000
IOPS [T = 32 Byte]90,000,000280,000,000280,000,00070,000,000
IOPS [T = 64 Byte]45,000,000150,000,000150,000,00060,000,000
IOPS [T = 128 Byte]20,000,00075,000,00050,000,00033,000,000
IOPS [T = 256 Byte]12,000,00035,000,00033,000,00022,000,000
IOPS [T = 1024 Byte]3,000,0008,000,0008,000,0006,000,000
IOPS [T = 4096 Byte]700,0001,600,0001,200,0001,300,000

  

INSTALL

Instal Visual Studio 2017 version 15.4.0 preview 3.0 & .NET Core 2.0 & support for multiple target frameworks

Install via Nuget Package Manager version 2.0.0

PM> Install-Package FriendlyCSharp.Databases

 

LICENSE

See the LICENSE.