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.

BSCache.h 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /* -*- Mode: c++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*-
  2. **
  3. ** Copyright (C) 1995-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 BLOCKSTORAGECACHE_H
  9. #define BLOCKSTORAGECACHE_H
  10. #include "modules/search_engine/BlockStorage.h"
  11. #define NUR_MASK 0x7FFF
  12. #define NUR_MAX 0x8000
  13. /**
  14. * @brief Cache over BlockStorage
  15. * @author Pavel Studeny <pavels@opera.com>
  16. *
  17. * If the access to the file isn't completely random, like for ACT or BTree,
  18. * it makes sense to have some cache. This is an NUR cache, also suitable
  19. * for prejournalling the data before commit.
  20. */
  21. class BSCache : public NonCopyable, public SearchGroupable
  22. {
  23. public:
  24. /**
  25. * specifies which Items to release from memory on Flush
  26. */
  27. enum ReleaseSeverity
  28. {
  29. ReleaseNo = 0, /**< just flush */
  30. ReleaseAll = 1, /**< release all unreferenced */
  31. ReleaseNUR = 2, /**< release not used recently */
  32. JournalOnly = 3, /**< just journal the modified Items */
  33. JournalAll = 4 /**< JournalOnly + reserve all the unsaved items (with disk_id < 0) */
  34. };
  35. /**
  36. * cacheable item, define its payload and virtual methods in descendants
  37. * as well as NewMemoryItem and NewDiskItem
  38. */
  39. struct Item : public NonCopyable
  40. {
  41. typedef INT32 DiskId;
  42. DiskId disk_id;
  43. Item *previous; // Possible improvement: Use other structure than linked list
  44. BOOL modified;
  45. BOOL in_list;
  46. BOOL journalled;
  47. DiskId deleted_id;
  48. int reference_count;
  49. unsigned short NUR_mark;
  50. BOOL id_reserved;
  51. Item(void)
  52. {
  53. disk_id = 0;
  54. previous = NULL;
  55. modified = FALSE;
  56. in_list = FALSE;
  57. journalled = FALSE;
  58. deleted_id = 0;
  59. reference_count = 0;
  60. id_reserved = FALSE;
  61. NUR_mark = 0;
  62. }
  63. virtual ~Item(void) {}
  64. /**
  65. * read data from position specified by disk_id
  66. */
  67. CHECK_RESULT(virtual OP_STATUS Read(BlockStorage *storage)) = 0;
  68. /**
  69. * write data to the storage:
  70. * update if disk_id > 0 and create a new record if disk_id < 0;
  71. * in this case also update disk_id to the number of the new block;
  72. * if disk_id == 0, delete deleted_id from the file
  73. */
  74. CHECK_RESULT(virtual OP_STATUS Flush(BlockStorage *storage)) = 0;
  75. /**
  76. * called whenever disk_id changes (could be in Flush, for example),
  77. * the value of Item::disk_id is undefined during this call
  78. */
  79. virtual void OnIdChange(DiskId new_id, DiskId old_id) {}
  80. /**
  81. * @return an estimate of the memory used by this data structure
  82. */
  83. #ifdef ESTIMATE_MEMORY_USED_AVAILABLE
  84. virtual size_t EstimateMemoryUsed() const;
  85. #endif
  86. };
  87. /**
  88. * @param max_cache maximum number of non-referenced Items in memory
  89. */
  90. BSCache(int max_cache);
  91. virtual ~BSCache(void);
  92. void SetNURFlush(BOOL nur)
  93. {
  94. m_flush_mode = nur ? ReleaseNUR : JournalOnly;
  95. }
  96. /**
  97. * write all unsaved data to disk
  98. * @return OpBoolean::IS_TRUE if the operation completed, OpBoolean::IS_FALSE if time limit was reached
  99. */
  100. CHECK_RESULT(OP_BOOLEAN Flush(ReleaseSeverity severity = ReleaseNo, int max_ms = 0));
  101. /**
  102. * @return number of cached Items
  103. */
  104. int GetItemCount(void) {return m_branch_count;}
  105. /**
  106. * @return direct pointer to the cached file
  107. */
  108. BlockStorage *GetStorage(void) {return &m_storage;}
  109. /**
  110. * Get SearchGroupable group member
  111. */
  112. virtual BlockStorage &GetGroupMember() { return m_storage; }
  113. /**
  114. * @return an estimate of the memory used by this data structure
  115. */
  116. #ifdef ESTIMATE_MEMORY_USED_AVAILABLE
  117. virtual size_t EstimateMemoryUsed() const;
  118. #endif
  119. protected:
  120. /**
  121. * create a new Item
  122. * @param id set disk_id to -id
  123. * @param rbranch Item referencing the new Item (if any)
  124. * @param rnode element of the referencing Item referencing the new Item (if Item is a kind of array)
  125. * @param nur current NUR stamp
  126. * @return NULL on error
  127. */
  128. virtual Item *NewMemoryItem(int id, Item *rbranch, int rnode, unsigned short nur) = 0;
  129. /**
  130. * read an item from disk
  131. * @param id number of the block to read
  132. * @param nur current NUR stamp
  133. * @return NULL on error
  134. */
  135. virtual Item *NewDiskItem(OpFileLength id, unsigned short nur) = 0;
  136. /**
  137. * forcibly remove everything from memory without saving
  138. */
  139. void ClearCache(void);
  140. /**
  141. * create a new Item and add it to the list
  142. */
  143. CHECK_RESULT(OP_STATUS Create(Item **t, Item *reference, int ref_node));
  144. /**
  145. * delete the Item from disk
  146. */
  147. void Unlink(Item *t);
  148. /**
  149. * go to Item with given id (both positive or negative)
  150. */
  151. CHECK_RESULT(OP_STATUS Load(Item **t, OpFileLength id));
  152. /**
  153. * just increase the reference counter of b
  154. */
  155. void Load(Item **t, Item *b);
  156. /**
  157. * decrease the reference counter
  158. */
  159. void Release(Item *t);
  160. Item *m_head;
  161. int m_branch_count; // total number if Items in m_head list
  162. int m_cache_count; // number of not referenced Items
  163. int m_journal_count; // number of Items processed by the PreJournal/Reserve conditions
  164. int m_max_cache; // maximum number of not referenced items to store
  165. BlockStorage m_storage;
  166. protected:
  167. OpFileLength m_memory_id;
  168. unsigned short m_NUR_mark;
  169. ReleaseSeverity m_flush_mode;
  170. BOOL m_journal_flushed;
  171. #ifdef _DEBUG
  172. // statistics
  173. public:
  174. int branches_read;
  175. int branches_created;
  176. int branches_cached;
  177. int branches_written;
  178. int flush_count;
  179. #endif
  180. #if defined(_DEBUG) || defined(SELFTEST)
  181. public:
  182. void CheckCache(void)
  183. {
  184. Item *tmp;
  185. for (tmp = m_head; tmp != NULL; tmp = tmp->previous)
  186. {
  187. OP_ASSERT(tmp->reference_count == 0);
  188. }
  189. }
  190. #endif
  191. };
  192. #endif // BLOCKSTORAGECACHE_H