00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00018
00019 #ifndef NET_INSTAWEB_UTIL_PUBLIC_SHARED_DYNAMIC_STRING_MAP_H_
00020 #define NET_INSTAWEB_UTIL_PUBLIC_SHARED_DYNAMIC_STRING_MAP_H_
00021
00022 #include <cstddef>
00023 #include "base/scoped_ptr.h"
00024 #include "net/instaweb/util/public/abstract_mutex.h"
00025 #include "net/instaweb/util/public/abstract_shared_mem.h"
00026 #include "net/instaweb/util/public/basictypes.h"
00027 #include "net/instaweb/util/public/string.h"
00028 #include "net/instaweb/util/public/string_util.h"
00029
00030 namespace net_instaweb {
00031
00032 class MessageHandler;
00033 class Writer;
00034
00035 struct Entry {
00036 int value;
00037 size_t string_offset;
00038 };
00039
00045 class SharedDynamicStringMap {
00046 public:
00051 SharedDynamicStringMap(size_t number_of_strings,
00052 size_t average_string_length,
00053 AbstractSharedMem* shm_runtime,
00054 const GoogleString& filename_prefix,
00055 const GoogleString& filename_suffix);
00056
00061 bool InitSegment(bool parent, MessageHandler* message_handler);
00062
00068 int IncrementElement(const StringPiece& string);
00069
00072 int LookupElement(const StringPiece& string) const;
00073
00075 void GetKeys(StringSet* strings);
00076
00078 int GetNumberInserted() const;
00079
00081 void GlobalCleanup(MessageHandler* message_handler);
00082
00087 void Dump(Writer* writer, MessageHandler* message_handler);
00088
00089 private:
00090 void ClearSegment(MessageHandler* message_handler);
00091
00113 int FindEntry(const StringPiece& string,
00114 bool lock,
00115 Entry** entry_pointer) const;
00116 Entry* GetEntry(size_t n) const;
00117 Entry* GetFirstEntry() const;
00118
00120 AbstractMutex* GetMutex(size_t n) const;
00126 int InsertString(const StringPiece& string, Entry* entry_pointer);
00127 char* GetStringAtOffset(size_t offset) const;
00128
00131 static size_t NextPowerOfTwo(size_t n);
00132
00202
00203 size_t number_of_strings_;
00204 size_t average_string_length_;
00206 size_t mutex_size_;
00207 size_t table_size_;
00217 size_t mutex_offset_;
00218 size_t strings_offset_;
00219 size_t string_offset_offset_;
00220 size_t number_inserted_offset_;
00221 size_t table_offset_;
00223 size_t total_size_;
00224
00227 scoped_ptr<AbstractMutex> insert_string_mutex_;
00228
00229 const GoogleString segment_name_;
00230 AbstractSharedMem* shm_runtime_;
00231 scoped_ptr<AbstractSharedMemSegment> segment_;
00232
00233 DISALLOW_COPY_AND_ASSIGN(SharedDynamicStringMap);
00234 };
00235
00236 }
00237
00238 #endif ///< NET_INSTAWEB_UTIL_PUBLIC_SHARED_DYNAMIC_STRING_MAP_H_