示例#1
0
        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());
        }
示例#2
0
        private InsertResult InsertAndSplit(
            CustomerRecord newCustomerRecord)
        {
            InsertWithoutSplit(
                newCustomerRecord,
                AdditionalSpaceAction.UseAddtionalSpace);

            return(CreateSplitResult(_customerRecords));
        }
示例#3
0
        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));
        }
示例#4
0
        private DataPage CreateLeftDataPage(
            CustomerRecord[] customerRecords,
            int splitCount)
        {
            var leftCustomers = new CustomerRecord[PageSizePlusAdditionalSpace];

            Array.Copy(customerRecords, 0, leftCustomers, 0, splitCount);
            return(new DataPage(PageSize, leftCustomers));
        }
示例#5
0
        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");
        }
示例#6
0
        public InsertResult Insert(
            CustomerRecord newCustomerRecord)
        {
            if (IsDataPageFull())
            {
                return(InsertAndSplit(newCustomerRecord));
            }

            return(InsertWithoutSplit(
                       newCustomerRecord,
                       AdditionalSpaceAction.DoNoUseAddtionalSpace));
        }
示例#7
0
        private CustomerRecord[] ShiftAndInsert(
            CustomerRecord[] customerRecords,
            CustomerRecord newCustomerRecord,
            int insertIndex,
            int pageSize)
        {
            ShiftCustomers(customerRecords, insertIndex, pageSize);

            customerRecords[insertIndex] = newCustomerRecord;

            return(customerRecords);
        }
示例#8
0
        public void Insert(CustomerRecord customerRecord)
        {
            var insertResult = _dataPage.Insert(customerRecord);

            if (insertResult.WasSplitCaused)
            {
                _dataPage = new IndexPage(
                    _dataPage.PageSize,
                    insertResult.SplitValue,
                    insertResult.LeftDataPage,
                    insertResult.RightDataPage);
            }
        }
示例#9
0
        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());
        }