public void saveRecord(string filename, int position, MyRecord c) { bw = new BinaryWriter(File.Open(filename, FileMode.OpenOrCreate, FileAccess.Write)); int startingPosition = position * (sizeof(int) * rozmiar_ciagu + sizeof(bool)); bw.BaseStream.Position = startingPosition; int[] values = c.getValues(); bw.Write(c.getKey()); bool status = c.isDeleted(); int pointer = c.getOverflowPointer(); bw.Write(status); bw.Write(pointer); int counter = 0; for (int i = startingPosition + (2 * sizeof(int) + 1); i < startingPosition + (sizeof(int) * rozmiar_ciagu + 1); i += sizeof(int)) { bw.Write(values[counter]); counter++; } bw.Close(); }
public void Edit(MyRecord c, string filename) { int savesCounter = 0; int loadsCounter = 0; int position = findElementsPosition(c.getKey(), ref savesCounter, ref loadsCounter); myFileManager.saveRecord(filename, position, c); savesCounter++; Results editResult = new Results("editRecord", loadsCounter, savesCounter); editResult.Show(); }
public void addRecord(MyRecord c, ref int sav, ref int load) { int whatPage = index.whatPage(c.getKey()); if (MainArea[0].getUsedSize() == 0) { addRecord(c, whatPage, ref sav, ref load); } else { if (findElementsPosition(c.getKey(), ref sav, ref load) == Int32.MaxValue) { addRecord(c, whatPage, ref sav, ref load); } else { Console.WriteLine("Podany element: " + c.getKey() + " juz istnieje!"); } } Results addResult = new Results("addRecord", load, sav); addResult.Show(); }
private void addRecord(MyRecord c, int whatPage, ref int saves, ref int loads) { int positionInFile = 0; int PageSize = MainArea[0].getPageSize(); //wszystkie strony maja taka sama wartosc positionInFile = (whatPage) * PageSize; //pierwszy rekord na nowej stronie if (index.getKey(whatPage) == 0) { MyRecord[] page = new MyRecord[PageSize]; for (int i = 0; i < PageSize; i++) { page[i] = new MyRecord(); } page[0] = c; myFileManager.savePage(FileName, page, positionInFile); saves++; index.addIndex(c.getKey(), whatPage); MainArea[whatPage].incrementUsedSize(); saveIndex(); } else { int PageUsedSize = MainArea[whatPage].getUsedSize(); MyRecord[] page = myFileManager.load(FileName, positionInFile, PageSize); loads++; //niepelna strona if (!MainArea[whatPage].isFull()) { int positionInPage = PageUsedSize; for (int i = 0; i < PageUsedSize; i++) { if (c.getKey() < page[i].getKey()) { positionInPage = i; break; } } //zapis strony MyRecord[] newPage = new MyRecord[PageSize]; for (int i = 0; i < positionInPage; i++) { newPage[i] = page[i]; } newPage[positionInPage] = c; for (int i = positionInPage + 1; i < PageSize; i++) { newPage[i] = page[i - 1]; } myFileManager.savePage(FileName, newPage, positionInFile); saves++; MainArea[whatPage].incrementUsedSize(); if (c.getKey() < index.getKey(whatPage)) { index.addIndex(c.getKey(), whatPage); saveIndex(); } } //pelna strona else { //jesli rekord jest mniejszy niz najmniejszy na stronie if (c.getKey() < page[0].getKey()) { //na indexie if (c.getKey() < index.getKey(whatPage)) { index.addIndex(c.getKey(), whatPage); saveIndex(); } //na stronie MyRecord temp = page[0]; page[0] = c; c = temp; page[0].setOverflowPointer(OverflowPointer + OverflowSize); myFileManager.saveRecord(FileName, positionInFile, page[0]); saves++; //na overflow myFileManager.saveRecord(FileName, OverflowPointer + OverflowSize, c); saves++; OverflowSize++; } else { //znajdywanie odpowiedniego miejsca int positionInPage = PageUsedSize - 1; for (int i = 1; i < PageUsedSize; i++) { if (c.getKey() < page[i].getKey()) { positionInPage = i - 1; break; } } //jesli odpowiedni rekord nie ma overflow if (page[positionInPage].getOverflowPointer() == Int32.MaxValue) { page[positionInPage].setOverflowPointer(OverflowPointer + OverflowSize); myFileManager.saveRecord(FileName, positionInFile + positionInPage, page[positionInPage]); myFileManager.saveRecord(FileName, OverflowPointer + OverflowSize, c); OverflowSize++; saves += 2; } //jesli ma else { int positionOfPreviousElement = page[positionInPage].getOverflowPointer(); MyRecord[] previousElement = myFileManager.load(FileName, positionOfPreviousElement, 1); loads++; if (c.getKey() < previousElement[0].getKey()) { c.setOverflowPointer(page[positionInPage].getOverflowPointer()); page[positionInPage].setOverflowPointer(OverflowPointer + OverflowSize); myFileManager.saveRecord(FileName, positionInFile + positionInPage, page[positionInPage]); myFileManager.saveRecord(FileName, OverflowPointer + OverflowSize, c); OverflowSize++; saves += 2; } else { bool status = true; if (previousElement[0].getOverflowPointer() != Int32.MaxValue) { MyRecord[] nextElement = myFileManager.load(FileName, previousElement[0].getOverflowPointer(), 1); loads++; //sprwdzaj do ostatniego istniejacego w lancuchu elementu while (nextElement[0].getTotal() != Int32.MinValue) { if (c.getKey() < nextElement[0].getKey()) { c.setOverflowPointer(previousElement[0].getOverflowPointer()); previousElement[0].setOverflowPointer(OverflowPointer + OverflowSize); myFileManager.saveRecord(FileName, positionOfPreviousElement, previousElement[0]); myFileManager.saveRecord(FileName, OverflowPointer + OverflowSize, c); OverflowSize++; saves += 2; status = false; break; } positionOfPreviousElement = previousElement[0].getOverflowPointer(); previousElement = nextElement; nextElement = myFileManager.load(FileName, previousElement[0].getOverflowPointer(), 1); loads++; } } //ostatni element w lancuchu if (status) { previousElement[0].setOverflowPointer(OverflowPointer + OverflowSize); myFileManager.saveRecord(FileName, positionOfPreviousElement, previousElement[0]); myFileManager.saveRecord(FileName, OverflowPointer + OverflowSize, c); OverflowSize++; saves += 2; } } } } if (OverflowSize > REORGANIZE_VALUE) { Reorganize(); } } } }