RecordCache.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. using System;
  24. using System.Collections;
  25. namespace System.Data.Common
  26. {
  27. internal class RecordCache
  28. {
  29. #region Fields
  30. const int MIN_CACHE_SIZE = 128;
  31. Stack _records = new Stack(16);
  32. int _nextFreeIndex = 0;
  33. int _currentCapacity = 0;
  34. DataTable _table;
  35. DataRow[] _rowsToRecords;
  36. #endregion // Fields
  37. #region Constructors
  38. internal RecordCache(DataTable table)
  39. {
  40. _table = table;
  41. _rowsToRecords = table.NewRowArray(16);
  42. }
  43. #endregion //Constructors
  44. #region Properties
  45. internal int CurrentCapacity
  46. {
  47. get {
  48. return _currentCapacity;
  49. }
  50. }
  51. internal DataRow this[int index]
  52. {
  53. get {
  54. return _rowsToRecords[index];
  55. }
  56. set {
  57. if (index >= 0) {
  58. _rowsToRecords[index] = value;
  59. }
  60. }
  61. }
  62. #endregion // Properties
  63. #region Methods
  64. internal int NewRecord()
  65. {
  66. if (_records.Count > 0) {
  67. return (int)_records.Pop();
  68. }
  69. else {
  70. DataColumnCollection cols = _table.Columns;
  71. if (_nextFreeIndex >= _currentCapacity) {
  72. _currentCapacity *= 2;
  73. if ( _currentCapacity < MIN_CACHE_SIZE ) {
  74. _currentCapacity = MIN_CACHE_SIZE;
  75. }
  76. foreach(DataColumn col in cols) {
  77. col.DataContainer.Capacity = _currentCapacity;
  78. }
  79. DataRow[] old = _rowsToRecords;
  80. _rowsToRecords = _table.NewRowArray(_currentCapacity);
  81. Array.Copy(old,0,_rowsToRecords,0,old.Length);
  82. }
  83. return _nextFreeIndex++;
  84. }
  85. }
  86. internal void DisposeRecord(int index)
  87. {
  88. if ( index < 0 ) {
  89. throw new ArgumentException();
  90. }
  91. if (!_records.Contains (index))
  92. _records.Push(index);
  93. this[index] = null;
  94. }
  95. internal int CopyRecord(DataTable fromTable,int fromRecordIndex,int toRecordIndex)
  96. {
  97. int recordIndex = toRecordIndex;
  98. if (toRecordIndex == -1) {
  99. recordIndex = NewRecord();
  100. }
  101. try
  102. {
  103. foreach(DataColumn fromColumn in fromTable.Columns)
  104. {
  105. DataColumn column = _table.Columns[fromColumn.ColumnName];
  106. if (column != null)
  107. {
  108. column.DataContainer.CopyValue(fromColumn.DataContainer,fromRecordIndex,recordIndex);
  109. }
  110. }
  111. return recordIndex;
  112. }
  113. catch
  114. {
  115. if (toRecordIndex == -1)
  116. DisposeRecord(recordIndex);
  117. throw;
  118. }
  119. }
  120. internal void ReadIDataRecord(int recordIndex, IDataRecord record, int[] mapping, int length)
  121. {
  122. if ( mapping.Length > _table.Columns.Count)
  123. throw new ArgumentException ();
  124. int i=0;
  125. for(; i < length; i++) {
  126. DataColumn column = _table.Columns[mapping[i]];
  127. column.DataContainer.SetItemFromDataRecord(recordIndex, record,i);
  128. }
  129. for (; i < mapping.Length; i++)
  130. {
  131. DataColumn column = _table.Columns[mapping[i]];
  132. if ( column.AutoIncrement )
  133. column.DataContainer[recordIndex] = column.AutoIncrementValue ();
  134. else
  135. column.DataContainer[recordIndex] = column.DefaultValue;
  136. }
  137. }
  138. #endregion // Methods
  139. }
  140. }