Page Speed Optimization Libraries  1.4.26.1
net/instaweb/util/public/shared_dynamic_string_map.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2011 Google Inc.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http:///www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
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 
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/scoped_ptr.h"
00028 #include "net/instaweb/util/public/string.h"
00029 #include "net/instaweb/util/public/string_util.h"
00030 
00031 namespace net_instaweb {
00032 
00033 class MessageHandler;
00034 class Writer;
00035 
00036 struct Entry {
00037   int value;
00038   size_t string_offset;
00039 };
00040 
00046 class SharedDynamicStringMap {
00047  public:
00052   SharedDynamicStringMap(size_t number_of_strings,
00053                          size_t average_string_length,
00054                          AbstractSharedMem* shm_runtime,
00055                          const GoogleString& filename_prefix,
00056                          const GoogleString& filename_suffix);
00057 
00062   bool InitSegment(bool parent, MessageHandler* message_handler);
00063 
00069   int IncrementElement(const StringPiece& string);
00070 
00073   int LookupElement(const StringPiece& string) const;
00074 
00076   void GetKeys(StringSet* strings);
00077 
00079   int GetNumberInserted() const;
00080 
00082   void GlobalCleanup(MessageHandler* message_handler);
00083 
00088   void Dump(Writer* writer, MessageHandler* message_handler);
00089 
00090  private:
00091   void ClearSegment(MessageHandler* message_handler);
00092 
00114   int FindEntry(const StringPiece& string,
00115                 bool lock,
00116                 Entry** entry_pointer) const;
00117   Entry* GetEntry(size_t n) const;
00118   Entry* GetFirstEntry() const;
00119 
00121   AbstractMutex* GetMutex(size_t n) const;
00127   int InsertString(const StringPiece& string, Entry* entry_pointer);
00128   char* GetStringAtOffset(size_t offset) const;
00129 
00132   static size_t NextPowerOfTwo(size_t n);
00133 
00203 
00204   size_t number_of_strings_;
00205   size_t average_string_length_;
00207   size_t mutex_size_;
00208   size_t table_size_;
00218   size_t mutex_offset_;
00219   size_t strings_offset_;
00220   size_t string_offset_offset_;
00221   size_t number_inserted_offset_;
00222   size_t table_offset_;
00224   size_t total_size_;
00225 
00228   scoped_ptr<AbstractMutex> insert_string_mutex_;
00229 
00230   const GoogleString segment_name_;
00231   AbstractSharedMem* shm_runtime_;
00232   scoped_ptr<AbstractSharedMemSegment> segment_;
00233 
00234   DISALLOW_COPY_AND_ASSIGN(SharedDynamicStringMap);
00235 };
00236 
00237 }  
00238 
00239 #endif  ///< NET_INSTAWEB_UTIL_PUBLIC_SHARED_DYNAMIC_STRING_MAP_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines