Esempio n. 1
0
        public BPlusTree(Configuration <T> cfg)
        {
            Order               = cfg.BPTree_Order;
            Alignment           = cfg.Alignment;
            Cluster_Data_Length = cfg.Clustering_Data_Length;

            _key_Serializer = cfg.Key_Serializer;

            _node_Factory = new Node_Factory <T>(_key_Serializer, Order, Alignment, Cluster_Data_Length);
            _block_Size   = _node_Factory.Size_In_Bytes(Order);

            _data_Serializer = new Data_Serializer <T>(_key_Serializer, Alignment, 2, 1024);
            _pending_Changes = new Pending_Changes <T>(_block_Size, _index_Pointer, _data_Pointer, _node_Factory, _data_Serializer);

            _streamFactory = cfg.Stream_Factory;

            Index_Stream    = _streamFactory.Create_ReadWrite_Index_Stream();
            Data_Stream     = _streamFactory.Create_ReadWrite_Data_Stream();
            Metadata_Stream = _streamFactory.Create_ReadWrite_Metadata_Stream();

            Cached_Nodes = new Dictionary <long, Node <T> >();
            _cache       = new Cache_LRU <long, Node <T> >(() => this.Current_Time);
            _metadata    = new Metadata {
                Order = Order
            };

            Init();
        }
Esempio n. 2
0
        public BPlusTree(Stream metadataStream, Stream indexStream, Stream dataStream, int order, int alignment, int cluster_data_length, IKey_Serializer <T> serializer)
        {
            Order               = order;
            Alignment           = alignment;
            Cluster_Data_Length = cluster_data_length;

            _key_Serializer = serializer;

            _node_Factory = new Node_Factory <T>(_key_Serializer, Order, Alignment, Cluster_Data_Length);
            _block_Size   = _node_Factory.Size_In_Bytes(Order);

            _data_Serializer = new Data_Serializer <T>(_key_Serializer, Alignment, 2, 1024);
            _pending_Changes = new Pending_Changes <T>(_block_Size, _index_Pointer, _data_Pointer, _node_Factory, _data_Serializer);


            Index_Stream    = indexStream;
            Data_Stream     = dataStream;
            Metadata_Stream = metadataStream;

            Cached_Nodes = new Dictionary <long, Node <T> >();
            _cache       = new Cache_LRU <long, Node <T> >(() => this.Current_Time);
            _metadata    = new Metadata {
                Order = Order
            };

            Init();
        }
Esempio n. 3
0
        private void Init()
        {
            System.Threading.Tasks.Task.Factory.StartNew(() =>
            {
                while (true)
                {
                    lock (this)
                    {
                        Current_Time = DateTime.Now;
                        System.Threading.Monitor.Wait(this, 100);
                    }
                }
            });

            try
            {
                var buffer = new byte[512];
                Metadata_Stream.Seek(0, SeekOrigin.Begin);
                Metadata_Stream.Read(buffer, 0, buffer.Length);

                var metadata = Metadata.From_Bytes(buffer);

                Root = Read_Root(metadata.Root_Address);
                if (Root.IsValid)
                {
                    Order               = metadata.Order;
                    _data_Pointer       = metadata.DataStream_Length;
                    _index_Pointer      = metadata.IndexStream_Length;
                    Cluster_Data_Length = metadata.Clustered_Data_Length;
                    Alignment           = metadata.Alignment;

                    _node_Factory = new Node_Factory <T>(_key_Serializer, Order, Alignment, Cluster_Data_Length);
                    _block_Size   = _node_Factory.Size_In_Bytes(Order);

                    _data_Serializer = new Data_Serializer <T>(_key_Serializer, Alignment, 2, 1024);
                    _pending_Changes = new Pending_Changes <T>(_block_Size, _index_Pointer, _data_Pointer, _node_Factory, _data_Serializer);
                    _metadata        = metadata;
                    return;
                }
            }
            catch (Exception) { }


            var root = _node_Factory.Create_New(true);

            Write_Node(root);
            _pending_Changes.Append_New_Root(root);
        }
Esempio n. 4
0
        public Node(Node_Factory <T> factory, int order, bool isLeaf, bool isClustered, int clustered_Data_Length)
        {
            _Factory    = factory;
            IsLeaf      = isLeaf;
            IsClustered = isClustered;
            Pointers    = new long[order + 1];
            Keys        = new T[order];
            Versions    = new int[order];
            Children    = new Node <T> [order + 1];
            Is_Volatile = true;

            if (isClustered)
            {
                Data = new byte[order + 1, clustered_Data_Length + 12];
                _clustered_Data_Length = clustered_Data_Length;
            }
            else
            {
                Data = new byte[0, 0];
            }
        }
Esempio n. 5
0
        public Node_Split_Result <T> Split(Node_Factory <T> node_Factory)
        {
            int size = Keys.Length;

            var node_Left  = this;
            var node_Right = node_Factory.Create_New(node_Left.IsLeaf);

            node_Right.Parent = node_Left.Parent;
            var mid_Key = node_Left.Keys[size / 2];

            node_Right.Key_Num = size - size / 2 - 1;
            for (int i = 0; i < node_Right.Key_Num; i++)
            {
                node_Right.Keys[i]     = node_Left.Keys[i + (size / 2 + 1)];
                node_Right.Pointers[i] = node_Left.Pointers[i + (size / 2 + 1)];
                if (!IsLeaf)
                {
                    node_Right.Children[i] = node_Left.Children[i + (size / 2 + 1)];
                }
                else
                {
                    for (int j = 0; j < _clustered_Data_Length; j++)
                    {
                        node_Right.Data[i, j] = node_Left.Data[i + (size / 2 + 1), j];
                    }
                }
                if (node_Right.Children[i] != null)
                {
                    node_Right.Children[i].Parent = node_Right;
                }
            }

            node_Right.Pointers[node_Right.Key_Num] = node_Left.Pointers[size];
            node_Right.Children[node_Right.Key_Num] = node_Left.Children[size];


            for (int j = 0; j < _clustered_Data_Length; j++)
            {
                node_Right.Data[node_Right.Key_Num, j] = node_Left.Data[size, j];
            }

            if (node_Right.Children[node_Right.Key_Num] != null)
            {
                node_Right.Children[node_Right.Key_Num].Parent = node_Right;
            }
            node_Left.Key_Num = size / 2;

            if (node_Left.IsLeaf)
            {
                node_Left.Key_Num++;
                node_Right.Pointers[0] = node_Left.Pointers[0];
                node_Right.Children[0] = node_Left.Children[0];

                node_Left.Pointers[0] = node_Right.Address;  //TODO double linked list
                mid_Key = node_Left.Keys[size / 2 + 1];
            }

            return(new Node_Split_Result <T> {
                Node_Left = node_Left, Node_Right = node_Right, Mid_Key = mid_Key
            });
        }