BombayTable.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. Copyright (c) 2009-2010 Christopher A. Taylor. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. * Redistributions of source code must retain the above copyright notice,
  6. this list of conditions and the following disclaimer.
  7. * Redistributions in binary form must reproduce the above copyright notice,
  8. this list of conditions and the following disclaimer in the documentation
  9. and/or other materials provided with the distribution.
  10. * Neither the name of LibCat nor the names of its contributors may be used
  11. to endorse or promote products derived from this software without
  12. specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  14. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  17. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  23. POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef CAT_BOMBAY_TABLE_HPP
  26. #define CAT_BOMBAY_TABLE_HPP
  27. #include <cat/threads/RWLock.hpp>
  28. #include <cat/io/ThreadPoolFiles.hpp>
  29. #include <cat/db/BombayTableIndex.hpp>
  30. namespace cat {
  31. namespace bombay {
  32. static u64 INVALID_RECORD_OFFSET = ~(u64)0;
  33. struct CacheNode
  34. {
  35. CacheNode *parent, *lower, *higher;
  36. u64 offset;
  37. };
  38. class TableIndex;
  39. class IHash;
  40. // Query() AsyncBuffer tag must derive from AsyncQueryRead
  41. struct AsyncQueryRead
  42. {
  43. ThreadRefObject *_reference;
  44. AsyncCallback _callback;
  45. CAT_INLINE void SetCallback(AsyncCallback callback = 0, ThreadRefObject *reference = 0)
  46. {
  47. if (reference)
  48. reference->AddRef();
  49. _callback = callback;
  50. _reference = reference;
  51. }
  52. };
  53. ///// Table
  54. class Table : public AsyncFile
  55. {
  56. ShutdownObserver *_shutdown_observer;
  57. u32 _record_bytes; // Bytes per record (without CacheNode overhead)
  58. u64 _next_record; // Next record offset
  59. protected:
  60. RWLock _lock;
  61. u64 _index_database_size, _index_read_offset, _index_read_completed;
  62. u32 _index_read_size;
  63. static const u32 MAX_INDEX_READ_SIZE = 32768;
  64. static const int NUM_PARALLEL_INDEX_READS = 3;
  65. // Cache hash table of binary trees
  66. static const u32 TARGET_TREE_SIZE = 16;
  67. static const u32 MIN_TABLE_SIZE = 2048;
  68. u32 _hash_table_size;
  69. CacheNode **_cache_hash_table;
  70. u8 *_cache; // Cache memory
  71. u32 _cache_bytes; // Cache bytes
  72. u32 _next_cache_slot; // Offset in cache memory to next free slot
  73. bool _cache_full; // Cache full flag for optimization
  74. TableIndex *_head_index, *_head_index_unique;
  75. TableIndex *_head_index_waiting, *_head_index_update;
  76. bool AllocateCache();
  77. void FreeCache();
  78. // Node versions
  79. CacheNode *FindNode(u64 offset);
  80. void UnlinkNode(CacheNode *node);
  81. void InsertNode(u64 offset, u32 key, CacheNode *hint, CacheNode *node);
  82. // Always returns with a cache node; may re-use an old cache node
  83. u8 *SetOffset(u64 offset);
  84. u8 *InsertOffset(u64 offset);
  85. u8 *PeekOffset(u64 offset);
  86. bool RemoveOffset(u64 offset);
  87. public:
  88. Table(const char *file_path, u32 record_bytes, u32 cache_bytes, ShutdownObserver *shutdown_observer);
  89. virtual ~Table();
  90. private:
  91. TableIndex *MakeIndex(const char *index_file_path, IHash *hash_function, bool unique);
  92. u64 UniqueIndexLookup(const void *data);
  93. public:
  94. // To initialize, run MakeIndex() for all of the desired indexing routines,
  95. // and then run Initialize(), which will initialize index objects.
  96. template<class THashFunc> CAT_INLINE TableIndex *MakeIndex(const char *index_file_path, bool unique)
  97. {
  98. return MakeIndex(index_file_path, new THashFunc, unique);
  99. }
  100. bool Initialize();
  101. public:
  102. CAT_INLINE u32 GetCacheBytes() { return _cache_bytes; }
  103. CAT_INLINE u32 GetRecordBytes() { return _record_bytes; }
  104. protected:
  105. virtual bool OnRemoveRead(ThreadPoolLocalStorage *tls, int error, AsyncBuffer *buffer, u32 bytes);
  106. virtual bool OnQueryRead(ThreadPoolLocalStorage *tls, int error, AsyncBuffer *buffer, u32 bytes);
  107. protected:
  108. bool StartIndexing();
  109. bool StartIndexingRead();
  110. void OnIndexingDone();
  111. virtual bool OnIndexingRead(ThreadPoolLocalStorage *tls, int error, AsyncBuffer *buffer, u32 bytes);
  112. public:
  113. bool RequestIndexRebuild(TableIndex *index);
  114. public:
  115. // Insert an AsyncBuffer data buffer
  116. u64 Insert(void *data);
  117. // Update with an AsyncBuffer data buffer
  118. bool Update(void *data, u64 offset);
  119. // Query with an AsyncBuffer
  120. // NOTE: Query() AsyncBuffer tag must derive from AsyncQueryRead
  121. bool Query(u64 offset, AsyncBuffer *buffer);
  122. // Remove based on offset
  123. bool Remove(u64 offset);
  124. };
  125. } // namespace bombay
  126. } // namespace cat
  127. #endif // CAT_BOMBAY_TABLE_HPP
粤ICP备19079148号