public BTree(int Degree, string FilePath, IFixedLengthFactory <T> ContentFactory, IFixedLengthFactory <TKey> KeyFactory) { FileName = FilePath; TFactory = ContentFactory; TKeyFactory = KeyFactory; File = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read); RootPointer = FileManagment.ReadLine(File, 0); FirstAvailablePointer = FileManagment.ReadLine(File, 1); this.Degree = FileManagment.ReadLine(File, 2); if (FirstAvailablePointer == int.MinValue) { FirstAvailablePointer = 0; } if (this.Degree == int.MinValue) { this.Degree = Degree; } if (RootPointer == int.MinValue) { BNode <T, TKey> Root = new BNode <T, TKey>(this.Degree, FirstAvailablePointer, int.MinValue, TFactory, TKeyFactory); FirstAvailablePointer++; RootPointer = Root.Pointer; Root.DiskWrite(File, SizesNSpecialCharacters.HeaderSizeInBytes); } UpdateHeader(); }
internal BNode(int Degree, int Pointer, int Parent, IFixedLengthFactory <T> ContentFactory, IFixedLengthFactory <TKey> KeyFactory) { this.Degree = Degree; this.Pointer = Pointer; this.Parent = Parent; this.KeyFactory = KeyFactory; this.ContentFactory = ContentFactory; Clear(ContentFactory, KeyFactory); }
private void Clear(IFixedLengthFactory <T> ContentFactory, IFixedLengthFactory <TKey> KeyFactory) { Keys = new List <TKey>(); Values = new List <T>(); for (int i = 0; i < Degree - 1; i++) { Keys.Add(KeyFactory.CreateNull()); Values.Add(ContentFactory.CreateNull()); } ChildrenPointers = new List <int>(); for (int i = 0; i < Degree; i++) { ChildrenPointers.Add(SizesNSpecialCharacters.NullPointer); } }
public void Clear(FileStream File, IFixedLengthFactory <T> ContentFactory, IFixedLengthFactory <TKey> KeyFactory) { Clear(ContentFactory, KeyFactory); DiskWrite(File, SizesNSpecialCharacters.HeaderSizeInBytes); }
public static BNode <T, TKey> DiskRead(FileStream File, int Degreee, int Pointer, IFixedLengthFactory <T> ContentFactory, IFixedLengthFactory <TKey> KeyFactory) { BNode <T, TKey> NewNode = new BNode <T, TKey>(Degreee, Pointer, 0, ContentFactory, KeyFactory); byte[] BytesArray = new byte[NewNode.SizeInBytes]; int FieldsIndex = 0; File.Seek(NewNode.DiskPosition(), SeekOrigin.Begin); File.Read(BytesArray, 0, NewNode.SizeInBytes); string[] Fields = Encoding.UTF8.GetString(BytesArray).Replace(Environment.NewLine, "").Replace("".PadRight(3, SizesNSpecialCharacters.GroupSeparator[0]), SizesNSpecialCharacters.GroupSeparator.ToString()).Split(SizesNSpecialCharacters.GroupSeparator[0]); NewNode.Parent = int.Parse(Fields[FieldsIndex]); FieldsIndex++; for (int i = 0; i < NewNode.ChildrenPointers.Count; i++) { NewNode.ChildrenPointers[i] = int.Parse(Fields[FieldsIndex]); FieldsIndex++; } for (int i = 0; i < NewNode.Keys.Count; i++) { NewNode.Keys[i] = KeyFactory.Create(Fields[FieldsIndex]); FieldsIndex++; } for (int i = 0; i < NewNode.Values.Count; i++) { NewNode.Values[i] = ContentFactory.Create(Fields[FieldsIndex]); FieldsIndex++; } return(NewNode); }