/// <summary> /// Write the news rows to the database as specified in <param name="rows">. /// </summary> /// <param name="rows"> /// The new rows to write to the database. /// </param> public void WriteNewRows(List <Row> rows) { // A dictionary for the new values that need to be written to the index file. Dictionary <int, int> toAddToIndex = new Dictionary <int, int> (); DataBaseStream.Position = (RowCount * ROW_BYTE_SIZE) + HEADER_BYTE_SIZE; foreach (Row r in rows) { // Write the row with the next primary key. WriteRow(r, NextPrimaryKey, WriteType.NewRowWrite); // Bump up the row count. RowCount++; // Add the new index file data to dictionary. toAddToIndex.Add(NextPrimaryKey, RowCount); // Add the new index data to the dictionary that references the index file. Index.Add(NextPrimaryKey, RowCount); // Bump up the primary key. NextPrimaryKey++; } // Set the database stream position to the header and update the header with the next primary key and the new row count. DataBaseStream.Position = HEADER_INFO_NEXT_PRIMARY_KEY; DataBaseWriter.Write(NextPrimaryKey); DataBaseWriter.Write(RowCount); //Set the index stream to the end of the file and write the new index values to tthe index file. IndexStream.Position = (RowCount - toAddToIndex.Count) * INDEX_ROW_BYTE_SIZE; foreach (KeyValuePair <int, int> primaryKeyRowLocation in toAddToIndex) { WriteIndexRow(primaryKeyRowLocation.Key, primaryKeyRowLocation.Value, WriteType.IndexNewRowWrite); } }
/// <summary> /// Write the header details for the database file if it has just been created. /// </summary> private void WriteNewFileHeader() { DataBaseStream.Position = 0; DataBaseWriter.Write("rubbishtable________".ToCharArray()); DataBaseWriter.Write(0); DataBaseWriter.Write(0); DataBaseWriter.Write(0); }
/// <summary> /// Write the rows as specified in <param name="rows"> as deleted. /// </summary> /// <param name="rows"> /// The rows that are to be marked as deleted. /// </param> public void MarkRowsDeleted(List <Row> rows) { foreach (Row row in rows) { // Write the row's status to false (deleted) and bump up the number of rows deleted. WriteRow(row, row.PrimaryKey, WriteType.RowDelete); DeletedRowCount++; } // Update the header information with the new number of rows deleted. DataBaseStream.Position = HEADER_INFO_NUM_ROWS_DELETED; DataBaseWriter.Write(DeletedRowCount); }
public void UploadData(string name, string author, string creationYear, string style, string epoch, string medium, int width, int height, string plot, string cost) { Picture addedPic = new Picture() { Name = name, CreationYear = int.Parse(creationYear), Style = style, Epoch = epoch, Medium = medium, Width = width, Height = height, Plot = plot, Cost = long.Parse(cost) }; DataBaseWriter.Write(addedPic); }
/// <summary> /// Reorder the database and index rows with the dictionary of row - location in <param name="rowsToMove">. /// </summary> /// <param name="rowsToMove"> /// Dictionary of row - location. /// </param> private void RearrangeRowsAndIndex(Dictionary <Row, int> rowsToMove) { // Set the new sizes of both the index and database file. DataBaseStream.SetLength(((RowCount - DeletedRowCount) * ROW_BYTE_SIZE) + HEADER_BYTE_SIZE); IndexStream.SetLength((RowCount - DeletedRowCount) * INDEX_ROW_BYTE_SIZE); // Write each row to it's new location. Write each index keyvalue pair of primary key location to the index file. // Update the object index. foreach (KeyValuePair <Row, int> data in rowsToMove) { Index [data.Key.PrimaryKey] = data.Value; WriteRow(data.Key, data.Key.PrimaryKey, WriteType.RowMove); WriteIndexRow(data.Key.PrimaryKey, data.Value, WriteType.IndexRowMove); } // Update the row count accordingly and reassign deleted rows the value of 0. We've just compacted! RowCount -= DeletedRowCount; DeletedRowCount = 0; DataBaseStream.Position = HEADER_INFO_NUM_ROWS; // Write that to the database header. DataBaseWriter.Write(RowCount); DataBaseWriter.Write(DeletedRowCount); }
/// <summary> /// Write the row <param name="row"> with primary key <param name="primaryKey"> using the WriteType <param name="type">. /// </summary> /// <param name="row"> /// Row to write. /// </param> /// <param name="primaryKey"> /// Primary key of row to write. /// </param> /// <param name="type"> /// The kind of row write to perform. /// </param> private void WriteRow(Row row, int primaryKey, WriteType type) { // If we're not udating an existing row. I.e. moving a row, writing a new row or deleting a row. if (type != WriteType.RowUpdate) { if (type != WriteType.NewRowWrite) { // Set the database stream to the location of the row DataBaseStream.Position = (Index [row.PrimaryKey] - 1) * ROW_BYTE_SIZE + HEADER_BYTE_SIZE; } // If the row is deleted, only write to it's status and leave the method. if (type == WriteType.RowDelete) { DataBaseWriter.Write(row.Status); return; } // Write the row's status and primary key. DataBaseWriter.Write(row.Status); DataBaseWriter.Write(primaryKey); // If it's a new row, write the new row's data and return from the method. if (type == WriteType.NewRowWrite) { DataBaseWriter.Write(row.NewTextData.PadRight(20, (char)0).ToCharArray()); DataBaseWriter.Write(row.NewNumberData); return; } } // If the row is being updated, set the database stream to the rows data locaton if (type == WriteType.RowUpdate) { DataBaseStream.Position = (Index [row.PrimaryKey] - 1) * ROW_BYTE_SIZE + HEADER_BYTE_SIZE + ROW_DATA_LOCATION; } // Write the row data. This will happen for rows being moved or updated. DataBaseWriter.Write(row.TextData.PadRight(20, (char)0).ToCharArray()); DataBaseWriter.Write(row.NumberData); }