RecordCache.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. #endregion // Fields
  36. #region Constructors
  37. internal RecordCache(DataTable table)
  38. {
  39. _table = table;
  40. }
  41. #endregion //Constructors
  42. #region Properties
  43. internal int CurrentCapacity
  44. {
  45. get {
  46. return _currentCapacity;
  47. }
  48. }
  49. #endregion // Properties
  50. #region Methods
  51. internal int NewRecord()
  52. {
  53. if (_records.Count > 0) {
  54. return (int)_records.Pop();
  55. }
  56. else {
  57. DataColumnCollection cols = _table.Columns;
  58. if (_nextFreeIndex >= _currentCapacity) {
  59. _currentCapacity *= 2;
  60. if ( _currentCapacity < MIN_CACHE_SIZE ) {
  61. _currentCapacity = MIN_CACHE_SIZE;
  62. }
  63. foreach(DataColumn col in cols) {
  64. col.DataContainer.Capacity = _currentCapacity;
  65. }
  66. }
  67. return _nextFreeIndex++;
  68. }
  69. }
  70. internal void DisposeRecord(int index)
  71. {
  72. if ( index < 0 ) {
  73. throw new ArgumentException();
  74. }
  75. _records.Push(index);
  76. }
  77. internal int CopyRecord(DataTable fromTable,int fromRecordIndex,int toRecordIndex)
  78. {
  79. int recordIndex = toRecordIndex;
  80. if (toRecordIndex == -1) {
  81. recordIndex = NewRecord();
  82. }
  83. foreach(DataColumn fromColumn in fromTable.Columns) {
  84. DataColumn column = _table.Columns[fromColumn.ColumnName];
  85. if (column != null) {
  86. column.DataContainer.CopyValue(fromColumn.DataContainer,fromRecordIndex,recordIndex);
  87. }
  88. }
  89. return recordIndex;
  90. }
  91. #endregion // Methods
  92. }
  93. }