// We no longer create new TCs, we simply use the template and modify to another data tree public static TableContext CreateNewTableContext(PSTFile file, List <TableColumnDescriptor> columns) { HeapOnNode heap = HeapOnNode.CreateNewHeap(file); TableContextInfo tcInfo = new TableContextInfo(); tcInfo.rgTCOLDESC = columns; tcInfo.UpdateDataLayout(); HeapID newUserRoot = heap.AddItemToHeap(tcInfo.GetBytes()); // The heap header may have just been updated HeapOnNodeHeader header = heap.HeapHeader; header.bClientSig = OnHeapTypeName.bTypeTC; header.hidUserRoot = newUserRoot; heap.UpdateHeapHeader(header); BTreeOnHeapHeader bTreeHeader = new BTreeOnHeapHeader(); bTreeHeader.cbKey = TableContextRowID.RecordKeyLength; bTreeHeader.cbEnt = TableContextRowID.RecordDataLength; tcInfo.hidRowIndex = heap.AddItemToHeap(bTreeHeader.GetBytes()); // this will replace the item in place (as they have the same size since number of columns was not modified) heap.ReplaceHeapItem(header.hidUserRoot, tcInfo.GetBytes()); return(new TableContext(heap, null)); }
/// <summary> /// Add column to an empty table context. /// If this is a Contents Table, the caller should call UpdateMessage() afterwards. /// Similarly, for attachment table, the caller should call UpdateAttachment(). /// </summary> public void AddPropertyColumn(PropertyID propertyID, PropertyTypeName propertyType) { TableColumnDescriptor newColumnDescriptor = new TableColumnDescriptor(); newColumnDescriptor.PropertyID = propertyID; newColumnDescriptor.PropertyType = propertyType; newColumnDescriptor.iBit = (byte)m_tcInfo.ColumnCount; newColumnDescriptor.cbData = (byte)GetPropertyDataLength(propertyType); // Set the ibData: // http://social.msdn.microsoft.com/Forums/en-US/os_binaryfile/thread/a5f9c653-40f5-4638-85d3-00c54607d984/ // PidTagLtpRowId and PidTagLtpRowVer must not be relocated if (newColumnDescriptor.DataLengthGroup == TableContextInfo.TCI_4b) { newColumnDescriptor.ibData = m_tcInfo.rgib[TableContextInfo.TCI_4b]; } else if (newColumnDescriptor.DataLengthGroup == TableContextInfo.TCI_2b) { newColumnDescriptor.ibData = m_tcInfo.rgib[TableContextInfo.TCI_2b]; } else { newColumnDescriptor.ibData = m_tcInfo.rgib[TableContextInfo.TCI_1b]; } // We call GetRedistributedRows() before adding the new column: List <byte[]> rows = GetRedistributedRows(newColumnDescriptor.ibData, newColumnDescriptor.cbData); // add the new column m_tcInfo.rgTCOLDESC.Add(newColumnDescriptor); // redistribute column descriptions ushort offset = (ushort)(newColumnDescriptor.ibData + newColumnDescriptor.cbData); for (int groupIndex = newColumnDescriptor.DataLengthGroup + 1; groupIndex < 3; groupIndex++) { for (int index = 0; index < m_tcInfo.rgTCOLDESC.Count; index++) { TableColumnDescriptor descriptor = m_tcInfo.rgTCOLDESC[index]; if (groupIndex == descriptor.DataLengthGroup) { // changes to descriptor will be saved when calling UpdateTableContextInfo() descriptor.ibData = offset; offset += descriptor.cbData; } } } // update the group ending offset m_tcInfo.UpdateDataLayout(); m_rowsPerBlock = (int)Math.Floor((double)DataBlock.MaximumDataLength / RowLength); // Update the rows data if (!m_tcInfo.hnidRows.IsEmpty) { if (m_tcInfo.hnidRows.IsHeapID) { m_heap.RemoveItemFromHeap(m_tcInfo.hnidRows.HeapID); CreateSubnodeForRows(); } else { if (m_subnodeRows == null) { m_subnodeRows = m_subnodeBTree.GetSubnode(m_tcInfo.hnidRows.NodeID); } m_subnodeRows.Delete(); // this will set the subnode data-tree to null // New data tree will be created when the first row will be added m_subnodeBTree.UpdateSubnodeEntry(m_tcInfo.hnidRows.NodeID, null, null); } for (int index = 0; index < rows.Count; index++) { AddRowToSubnode(index, rows[index]); } } UpdateTableContextInfo(); }