private InsertResult InsertWithoutSplit( CustomerRecord newCustomerRecord, AdditionalSpaceAction additionalSpaceAction) { var pageSize = GetCorrectPageSize(additionalSpaceAction); for (int recordIndex = 0; recordIndex < pageSize; recordIndex++) { if (_customerRecords[recordIndex] == null) { _customerRecords[recordIndex] = newCustomerRecord; break; } if (_customerRecords[recordIndex].CustomerId > newCustomerRecord.CustomerId) { _customerRecords = ShiftAndInsert( _customerRecords, newCustomerRecord, recordIndex, pageSize); break; } } return(InsertResult.CreateWithoutSplit()); }
private InsertResult InsertAndSplit( CustomerRecord newCustomerRecord) { InsertWithoutSplit( newCustomerRecord, AdditionalSpaceAction.UseAddtionalSpace); return(CreateSplitResult(_customerRecords)); }
private DataPage CreateRightDataPage( CustomerRecord[] customerRecords, int splitCount) { var rightCustomers = new CustomerRecord[PageSizePlusAdditionalSpace]; Array.Copy(customerRecords, splitCount, rightCustomers, 0, PageSize - splitCount + 1); return(new DataPage(PageSize, rightCustomers)); }
private DataPage CreateLeftDataPage( CustomerRecord[] customerRecords, int splitCount) { var leftCustomers = new CustomerRecord[PageSizePlusAdditionalSpace]; Array.Copy(customerRecords, 0, leftCustomers, 0, splitCount); return(new DataPage(PageSize, leftCustomers)); }
private int FindIndexToInsert(CustomerRecord cust) { for (var i = 0; i < PageSize + 1; i++) { if (_indexes[i] <= 0 || cust.CustomerId < _indexes[i]) { return(i); } } throw new Exception("Something is wrong"); }
public InsertResult Insert( CustomerRecord newCustomerRecord) { if (IsDataPageFull()) { return(InsertAndSplit(newCustomerRecord)); } return(InsertWithoutSplit( newCustomerRecord, AdditionalSpaceAction.DoNoUseAddtionalSpace)); }
private CustomerRecord[] ShiftAndInsert( CustomerRecord[] customerRecords, CustomerRecord newCustomerRecord, int insertIndex, int pageSize) { ShiftCustomers(customerRecords, insertIndex, pageSize); customerRecords[insertIndex] = newCustomerRecord; return(customerRecords); }
public void Insert(CustomerRecord customerRecord) { var insertResult = _dataPage.Insert(customerRecord); if (insertResult.WasSplitCaused) { _dataPage = new IndexPage( _dataPage.PageSize, insertResult.SplitValue, insertResult.LeftDataPage, insertResult.RightDataPage); } }
public InsertResult Insert(CustomerRecord newCustomerRecord) { var indexToInsert = FindIndexToInsert(newCustomerRecord); var insertResult = _dataPages[indexToInsert].Insert(newCustomerRecord); if (insertResult.WasSplitCaused) { if (_indexes[indexToInsert] > 0) { ShiftIndexes(indexToInsert); ShiftPages(indexToInsert); } UpdateIndexsAndPages(indexToInsert, insertResult); } if (PageIsFull()) { return(CreateSplit()); } return(InsertResult.CreateWithoutSplit()); }