private void addNewRecord() { int[] values = new int[record_size]; labelStatus.Content = ""; try { int key = Int32.Parse(textBoxKey.Text); values[0] = Int32.Parse(textBoxFirst.Text); values[1] = Int32.Parse(textBoxSecond.Text); values[2] = Int32.Parse(textBoxThird.Text); values[3] = Int32.Parse(textBoxFourth.Text); values[4] = Int32.Parse(textBoxFifth.Text); values[5] = Int32.Parse(textBoxSixth.Text); values[6] = Int32.Parse(textBoxSeventh.Text); values[7] = Int32.Parse(textBoxEighth.Text); values[8] = Int32.Parse(textBoxNineth.Text); values[9] = Int32.Parse(textBoxTenth.Text); if (action == 'N') { savesCounter = 0; loadsCounter = 0; if (key > 0) { c = new MyRecord(key, values); logic.addRecord(c, ref savesCounter, ref loadsCounter); } } else if (action == 'E') { c = new MyRecord(key, values, c.getOverflowPointer(), c.isDeleted()); logic.Edit(c, FileName); } } catch (Exception ex) { labelStatus.Content = "Błąd!"; } }
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(); }
//position - pozycja, licząc od zerowego elementu, zwieksza sie co 1 //pozycję liczę W REKORDACH public MyRecord[] load(string fileName, int position, int buffer_size) { MyRecord[] buffer = new MyRecord[buffer_size]; int[] values = new int[rozmiar_ciagu]; int counter = 0; int record_counter = 0; if (File.Exists(fileName)) { try { br = new BinaryReader(File.Open(fileName, FileMode.Open, FileAccess.Read)); int startingPosition = position * (sizeof(int) * rozmiar_ciagu + sizeof(bool)); br.BaseStream.Position = startingPosition; for (int j = 0; j < buffer_size; j++) { int key = br.ReadInt32(); bool status = br.ReadBoolean(); int pointer = br.ReadInt32(); counter = 0; for (int i = startingPosition + (2 * sizeof(int) + 1); i < startingPosition + (sizeof(int) * rozmiar_ciagu + 1); i += sizeof(int)) { values[counter] = br.ReadInt32(); counter++; } buffer[record_counter] = new MyRecord(key, values, pointer, status); record_counter++; //zwiększenie pozycji startowej wczytywania rekordu startingPosition += sizeof(int) * rozmiar_ciagu; } br.Close(); return(buffer); } catch (IOException ex) { //jeśli nastąpi end of file, nadaj wartość minusInfinity sumie ciągu buffer[record_counter] = new MyRecord(0, Int32.MaxValue, true, minusInf, 0, 0, 0, 0, 0, 0, 0, 0, 0); br.Close(); return(buffer); } catch (Exception ex) { buffer[record_counter] = new MyRecord(0, Int32.MaxValue, true, minusInf, 0, 0, 0, 0, 0, 0, 0, 0, 0); br.Close(); return(buffer); } } buffer = new MyRecord[1]; buffer[record_counter] = new MyRecord(0, Int32.MaxValue, true, minusInf, 0, 0, 0, 0, 0, 0, 0, 0, 0); return(buffer); }
public void addRecordManually() { int savesCounter = 0; int loadsCounter = 0; MyRecord c = new MyRecord(); NewRecordWindow nrw = new NewRecordWindow(this, ref savesCounter, ref loadsCounter); nrw.Show(); }
private void checkIfReadyToSave(ref MyRecord[] c, int pageSize, ref int recordsCounter, ref int saves) { if (c[pageSize - 1] != null) { myFileManager.save(FileName2, c, 'S'); saves++; c = new MyRecord[PageSize]; recordsCounter = 0; } }
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(); }
private void addRecordButton_Click(object sender, RoutedEventArgs e) { Random r = new Random(); int loa = 0; int sav = 0; int i = r.Next(1, 100); int[] values = newTable(i); MyRecord c = new MyRecord(i, values); logic.addRecord(c, ref sav, ref loa); }
public void editRecord(int key, char action) { MyRecord[] ciag = new MyRecord[1]; this.action = action; label.Content = ""; int position = logic.findElementsPosition(key, ref savesCounter, ref loadsCounter); ciag = myFileManager.load(FileName, position, 1); loadsCounter++; if (ciag[0].getKey() != 0) { Show(); } //logic.loadsCounter++; c = ciag[0]; textBoxKey.Text = c.Key.ToString(); textBoxFirst.Text = c.First.ToString(); textBoxSecond.Text = c.Second.ToString(); textBoxThird.Text = c.Third.ToString(); textBoxFourth.Text = c.Fourth.ToString(); textBoxFifth.Text = c.Fifth.ToString(); textBoxSixth.Text = c.Sixth.ToString(); textBoxSeventh.Text = c.Seventh.ToString(); textBoxEighth.Text = c.Eighth.ToString(); textBoxNineth.Text = c.Nineth.ToString(); textBoxTenth.Text = c.Tenth.ToString(); if (action == 'E') { buttonAdd.Content = "Zapisz"; } else if (action == 'S') { textBoxFirst.IsReadOnly = true; textBoxSecond.IsReadOnly = true; textBoxThird.IsReadOnly = true; textBoxFourth.IsReadOnly = true; textBoxFifth.IsReadOnly = true; textBoxSixth.IsReadOnly = true; textBoxSeventh.IsReadOnly = true; textBoxEighth.IsReadOnly = true; textBoxNineth.IsReadOnly = true; textBoxTenth.IsReadOnly = true; textBoxFileName.IsReadOnly = true; buttonAdd.Visibility = Visibility.Hidden; buttonReset.Visibility = Visibility.Hidden; } textBoxKey.IsReadOnly = true; }
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(); } } } }
public void Reorganize() { int reorgSaves = 0; int reorgLoads = 0; int positionInOrganisedFile = 0; int position = 0; for (int i = 0; i < PagesInMainAreaCount; i++) { MyRecord[] page = myFileManager.load(FileName, positionInOrganisedFile, PageSize); reorgLoads++; MyRecord[] newPage = new MyRecord[PageSize]; int recordsInNewPageIndex = 0; for (int j = 0; j < PageSize; j++) { if (page[j].getKey() == 0) { break; } position = page[j].getOverflowPointer(); if (!page[j].isDeleted()) { newPage[recordsInNewPageIndex] = page[j]; newPage[recordsInNewPageIndex].setOverflowPointer(Int32.MaxValue); recordsInNewPageIndex++; checkIfReadyToSave(ref newPage, PageSize, ref recordsInNewPageIndex, ref reorgSaves); } if (position != Int32.MaxValue) { do { MyRecord[] overflowRecord = myFileManager.load(FileName, position, 1); reorgLoads++; position = overflowRecord[0].getOverflowPointer(); if (!overflowRecord[0].isDeleted()) { newPage[recordsInNewPageIndex] = overflowRecord[0]; newPage[recordsInNewPageIndex].setOverflowPointer(Int32.MaxValue); recordsInNewPageIndex++; checkIfReadyToSave(ref newPage, PageSize, ref recordsInNewPageIndex, ref reorgSaves); } } while (position != Int32.MaxValue); } } if (recordsInNewPageIndex != 0) { myFileManager.save(FileName2, newPage, 'S'); recordsInNewPageIndex = 0; reorgSaves++; } positionInOrganisedFile += PageSize; } File.Delete(FileName); AddDataFromFile(ref reorgSaves, ref reorgLoads); File.Delete(FileName2); OverflowPointer = PagesInMainAreaCount * PageSize; OverflowSize = 0; Results reorganizeResult = new Results("reorganizeRecord", reorgLoads, reorgSaves); reorganizeResult.Show(); }