RecordCache.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System;
  2. using System.Collections;
  3. namespace System.Data.Common
  4. {
  5. internal class RecordCache
  6. {
  7. #region Fields
  8. const int MIN_CACHE_SIZE = 128;
  9. Stack _records = new Stack(16);
  10. int _nextFreeIndex = 0;
  11. int _currentCapacity = 0;
  12. DataTable _table;
  13. #endregion // Fields
  14. #region Constructors
  15. internal RecordCache(DataTable table)
  16. {
  17. _table = table;
  18. }
  19. #endregion //Constructors
  20. #region Properties
  21. internal int CurrentCapacity
  22. {
  23. get {
  24. return _currentCapacity;
  25. }
  26. }
  27. #endregion // Properties
  28. #region Methods
  29. internal int NewRecord()
  30. {
  31. if (_records.Count > 0) {
  32. return (int)_records.Pop();
  33. }
  34. else {
  35. DataColumnCollection cols = _table.Columns;
  36. if (_nextFreeIndex >= _currentCapacity) {
  37. _currentCapacity *= 2;
  38. if ( _currentCapacity < MIN_CACHE_SIZE ) {
  39. _currentCapacity = MIN_CACHE_SIZE;
  40. }
  41. foreach(DataColumn col in cols) {
  42. col.DataContainer.Capacity = _currentCapacity;
  43. }
  44. }
  45. return _nextFreeIndex++;
  46. }
  47. }
  48. internal void DisposeRecord(int index)
  49. {
  50. if ( index < 0 ) {
  51. throw new ArgumentException();
  52. }
  53. _records.Push(index);
  54. }
  55. #endregion // Methods
  56. }
  57. }