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); }
public void Commit() { if (!_pending_Changes.Has_Pending_Changes()) { return; } foreach (var address in _pending_Changes.Get_Freed_Empty_Slots()) { _cache.Invalidate(address); } var newRoot = _pending_Changes.Commit(Index_Stream, Data_Stream); writes++; Metadata_Stream.Seek(0, SeekOrigin.Begin); var buffer = new byte[512]; _metadata.DataStream_Length = _pending_Changes.Get_Current_Data_Pointer(); _metadata.IndexStream_Length = _pending_Changes.Get_Index_Pointer(); _metadata.Root_Address = newRoot.Address; _metadata.Clustered_Data_Length = Cluster_Data_Length; _metadata.Alignment = Alignment; _metadata.Number_Of_Keys += key_added; _metadata.Number_Of_Leaves += leaves_added; _metadata.Number_Of_Nodes += nodes_added; _metadata.Height = 0; _metadata.To_Bytes_In_Buffer(buffer, 0); Metadata_Stream.Write(buffer, 0, buffer.Length); Metadata_Stream.Flush(); Root = newRoot; _pending_Changes.Clean_Root(); key_added = leaves_added = nodes_added = 0; // TODO persist empty pages on metadata ? //Cached_Nodes.Clear(); //foreach (var node in Pending_Changes.Last_Cached_Nodes()) // Cached_Nodes[node.Address] = node; _index_Pointer = _pending_Changes.Get_Index_Pointer(); Index_Stream.Flush(); }
public void Dispose() { Index_Stream.Dispose(); Metadata_Stream.Dispose(); Data_Stream.Dispose(); }