Opera 12.15 Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ExponentialGrowthFile.h 3.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* -*- Mode: c++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*-
  2. **
  3. ** Copyright (C) 2011 Opera Software ASA. All rights reserved.
  4. **
  5. ** This file is part of the Opera web browser.
  6. ** It may not be distributed under any circumstances.
  7. */
  8. #ifndef EXPONENTIALGROWTHFILE_H
  9. #define EXPONENTIALGROWTHFILE_H
  10. #include "modules/search_engine/FileWrapper.h"
  11. #ifdef SEARCH_ENGINE_EXPONENTIAL_GROWTH_FILE
  12. /** Wrapper class for OpLowLevelFile's that ensure that the file grows in
  13. * exponentially increasing increments.
  14. *
  15. * This class can be wrapped around any OpLowLevelFile to ensure exponential
  16. * growth, limiting file fragmentation to O(log(n)).
  17. */
  18. class ExponentialGrowthFile
  19. : public FileWrapper
  20. {
  21. public:
  22. /** Create an ExponentialGrowthFile object.
  23. *
  24. * This does not create or open the file (but the supplied wrapped_file
  25. * may be already created or open).
  26. *
  27. * @param wrapped_file The normal OpLowLevelFile that will handle actual I/O.
  28. * @param status set to OK, or error code if the return value is NULL.
  29. * @param smallest_size The smallest size of the file. It should be a power of 2
  30. * @param transfer_ownership If TRUE, on success of this call the ownership of wrapped_file
  31. * is transferred to the new ExponentialGrowthFile, which becomes responsible for
  32. * deallocating it in the destructor. In this case the wrapped_file object must have
  33. * been allocated using OP_NEW.
  34. * @return The new ExponentialGrowthFile, or NULL on error
  35. */
  36. static ExponentialGrowthFile* Create(OpLowLevelFile* wrapped_file,
  37. OP_STATUS& status,
  38. OpFileLength smallest_size = EXPONENTIAL_GROWTH_FILE_SMALLEST_SIZE,
  39. BOOL transfer_ownership = TRUE);
  40. /** Extract the wrapped file and delete the ExponentialGrowthFile object.
  41. * If the file was converted to an ExponentialGrowthFile, it will be converted back, but
  42. * this should not be the case, so the code asserts that it is not converted.
  43. * @param file The ExponentialGrowthFile to be "unwrapped". The pointer will be set to NULL.
  44. * @return the wrapped file
  45. */
  46. static OpLowLevelFile* MakeNormalFile(ExponentialGrowthFile*& file);
  47. OP_STATUS SafeClose();
  48. OP_STATUS Close();
  49. /** OPPS! If mode includes OPFILE_COMMIT, this operation *will* modify the file position,
  50. * contrary to what is common in OpLowLevelFile implementations
  51. */
  52. OP_STATUS Flush();
  53. /** OPPS! Currently not posix compatible. Returns Eof()==TRUE when positioned at end of file,
  54. * not after trying to read past the end. */
  55. BOOL Eof() const;
  56. OP_STATUS Write(const void* data, OpFileLength len);
  57. OP_STATUS Read(void* data, OpFileLength len, OpFileLength* bytes_read);
  58. OP_STATUS GetFileLength(OpFileLength* len) const;
  59. /** OPPS! Currently not posix compatible. Does not allow setting a position beyond the end of the file */
  60. OP_STATUS SetFilePos(OpFileLength pos, OpSeekMode mode = SEEK_FROM_START);
  61. OP_STATUS SetFileLength(OpFileLength len) { return SetFileLength(len, FALSE); }
  62. static OpFileLength nextExpSize(OpFileLength size);
  63. private:
  64. ExponentialGrowthFile();
  65. ExponentialGrowthFile(OpLowLevelFile*, OpFileLength, BOOL);
  66. ~ExponentialGrowthFile();
  67. const OpFileLength m_smallest_size;
  68. mutable OpFileLength m_virtual_file_length;
  69. mutable BOOL m_metadata_needs_write;
  70. OpFileLength GetNewPhysicalFileLength(OpFileLength virtual_file_length) const;
  71. CHECK_RESULT(OP_STATUS SetFileLength(OpFileLength len, BOOL return_to_current_file_pos));
  72. CHECK_RESULT(OP_STATUS EnsureValidVirtualFileLength() const);
  73. CHECK_RESULT(OP_STATUS WriteMetadata());
  74. CHECK_RESULT(OP_STATUS ConvertBackToNormalFile());
  75. };
  76. #endif // SEARCH_ENGINE_EXPONENTIAL_GROWTH_FILE
  77. #endif // !EXPONENTIALGROWTH_H