Hide implementation details of SystemDictionary into .cc file

This CL does nothing except hiding implementation details of SystemDictionary into .cc file.  No user-visible behavior change is intended.

BUG=none
TEST=unittest

git-svn-id: https://mozc.googlecode.com/svn/trunk@546 a6090854-d499-a067-5803-1114d4e51264
diff --git a/src/dictionary/system/system_dictionary.cc b/src/dictionary/system/system_dictionary.cc
index ef36f9c..c6301e1 100644
--- a/src/dictionary/system/system_dictionary.cc
+++ b/src/dictionary/system/system_dictionary.cc
@@ -76,28 +76,9 @@
 
 namespace {
 
-// rbx_array default setting
-const int kMinRbxBlobSize = 4;
+const int kMinTokenArrayBlobSize = 4;
 const char *kReverseLookupCache = "reverse_lookup_cache";
 
-class ReverseLookupCache : public NodeAllocatorData::Data {
- public:
-  multimap<int, SystemDictionary::ReverseLookupResult> results;
-};
-
-bool IsCacheAvailable(
-    const set<int> &id_set,
-    const multimap<int, SystemDictionary::ReverseLookupResult> &results) {
-  for (set<int>::const_iterator itr = id_set.begin();
-       itr != id_set.end();
-       ++itr) {
-    if (results.find(*itr) == results.end()) {
-      return false;
-    }
-  }
-  return true;
-}
-
 // Expansion table format:
 // "<Character to expand>[<Expanded character 1><Expanded character 2>...]"
 //
@@ -394,8 +375,8 @@
                                             &result_.value_id, &read_bytes));
     if (is_last_token) {
       int tokens_size = offset_ + read_bytes - tokens_offset_;
-      if (tokens_size < kMinRbxBlobSize) {
-        tokens_size = kMinRbxBlobSize;
+      if (tokens_size < kMinTokenArrayBlobSize) {
+        tokens_size = kMinTokenArrayBlobSize;
       }
       tokens_offset_ += tokens_size;
       ++index_;
@@ -418,9 +399,39 @@
   DISALLOW_COPY_AND_ASSIGN(TokenScanIterator);
 };
 
+struct ReverseLookupResult {
+  ReverseLookupResult() : tokens_offset(-1), id_in_key_trie(-1) {}
+  // Offset from the tokens section beginning.
+  // (token_array_.Get(id_in_key_trie) == token_array_.Get(0) + tokens_offset)
+  int tokens_offset;
+  // Id in key trie
+  int id_in_key_trie;
+};
+
 }  // namespace
 
-class ReverseLookupIndex {
+class SystemDictionary::ReverseLookupCache : public NodeAllocatorData::Data {
+ public:
+  ReverseLookupCache() {}
+
+  bool IsAvailable(const set<int> &id_set) const {
+    for (set<int>::const_iterator itr = id_set.begin();
+         itr != id_set.end();
+         ++itr) {
+      if (results.find(*itr) == results.end()) {
+        return false;
+      }
+    }
+    return true;
+  }
+
+  multimap<int, ReverseLookupResult> results;
+
+ private:
+  DISALLOW_COPY_AND_ASSIGN(ReverseLookupCache);
+};
+
+class SystemDictionary::ReverseLookupIndex {
  public:
   ReverseLookupIndex(
       const SystemDictionaryCodecInterface *codec,
@@ -448,8 +459,7 @@
     }
 
     for (size_t i = 0; i < index_size_; ++i) {
-      index_[i].results.reset(
-          new SystemDictionary::ReverseLookupResult[index_[i].size]);
+      index_[i].results.reset(new ReverseLookupResult[index_[i].size]);
     }
 
     // Builds index.
@@ -468,7 +478,7 @@
       for (result_index = 0;
            result_index < result_array->size;
            ++result_index) {
-        const SystemDictionary::ReverseLookupResult &lookup_result =
+        const ReverseLookupResult &lookup_result =
             result_array->results[result_index];
         if (lookup_result.tokens_offset == -1 &&
             lookup_result.id_in_key_trie == -1) {
@@ -485,9 +495,8 @@
 
   ~ReverseLookupIndex() {}
 
-  void FillResultMap(
-      const set<int> &id_set,
-      multimap<int, SystemDictionary::ReverseLookupResult> *result_map) {
+  void FillResultMap(const set<int> &id_set,
+                     multimap<int, ReverseLookupResult> *result_map) {
     for (set<int>::const_iterator id_itr  = id_set.begin();
          id_itr != id_set.end(); ++id_itr) {
       const ReverseLookupResultArray &result_array = index_[*id_itr];
@@ -503,7 +512,7 @@
     // Use scoped_ptr for reducing memory consumption as possible.
     // Using vector requires 90 MB even when we call resize explicitly.
     // On the other hand, scoped_ptr requires 57 MB.
-    scoped_ptr<SystemDictionary::ReverseLookupResult[]> results;
+    scoped_ptr<ReverseLookupResult[]> results;
     size_t size;
   };
 
@@ -514,51 +523,85 @@
   DISALLOW_COPY_AND_ASSIGN(ReverseLookupIndex);
 };
 
+struct SystemDictionary::PredictiveLookupSearchState {
+  PredictiveLookupSearchState() : key_pos(0), is_expanded(false) {}
+  PredictiveLookupSearchState(const storage::louds::LoudsTrie::Node &n,
+                              size_t pos, bool expanded)
+      : node(n), key_pos(pos), is_expanded(expanded) {}
+
+  storage::louds::LoudsTrie::Node node;
+  size_t key_pos;
+  bool is_expanded;
+};
+
+struct SystemDictionary::Builder::Specification {
+  enum InputType {
+    FILENAME,
+    IMAGE,
+  };
+
+  Specification(InputType t, const string &fn, const char *p, int l, Options o,
+                const SystemDictionaryCodecInterface *codec)
+      : type(t), filename(fn), ptr(p), len(l), options(o), codec(codec) {}
+
+  InputType type;
+
+  // For InputType::FILENAME
+  const string filename;
+
+  // For InputType::IMAGE
+  const char *ptr;
+  const int len;
+
+  Options options;
+  const SystemDictionaryCodecInterface *codec;
+};
+
 SystemDictionary::Builder::Builder(const string &filename)
-    : type_(FILENAME), filename_(filename),
-      ptr_(NULL), len_(-1), options_(NONE), codec_(NULL)  {}
+    : spec_(new Specification(Specification::FILENAME,
+                              filename, NULL, -1, NONE, NULL)) {}
 
 SystemDictionary::Builder::Builder(const char *ptr, int len)
-    : type_(IMAGE), filename_(""),
-      ptr_(ptr), len_(len), options_(NONE), codec_(NULL) {}
+    : spec_(new Specification(Specification::IMAGE,
+                              "", ptr, len, NONE, NULL)) {}
 
 SystemDictionary::Builder::~Builder() {}
 
 SystemDictionary::Builder & SystemDictionary::Builder::SetOptions(
     Options options) {
-  options_ = options;
+  spec_->options = options;
   return *this;
 }
 
 SystemDictionary::Builder &SystemDictionary::Builder::SetCodec(
     const SystemDictionaryCodecInterface *codec) {
-  codec_ = codec;
+  spec_->codec = codec;
   return *this;
 }
 
 SystemDictionary *SystemDictionary::Builder::Build() {
-  if (codec_ == NULL) {
-    codec_ = SystemDictionaryCodecFactory::GetCodec();
+  if (spec_->codec == NULL) {
+    spec_->codec = SystemDictionaryCodecFactory::GetCodec();
   }
 
-  scoped_ptr<SystemDictionary> instance(new SystemDictionary(codec_));
+  scoped_ptr<SystemDictionary> instance(new SystemDictionary(spec_->codec));
 
-  switch (type_) {
-    case FILENAME:
-      if (!instance->dictionary_file_->OpenFromFile(filename_)) {
+  switch (spec_->type) {
+    case Specification::FILENAME:
+      if (!instance->dictionary_file_->OpenFromFile(spec_->filename)) {
         LOG(ERROR) << "Failed to open system dictionary file";
         return NULL;
       }
       break;
-    case IMAGE:
+    case Specification::IMAGE:
       // Make the dictionary not to be paged out.
       // We don't check the return value because the process doesn't necessarily
       // has the priviledge to mlock.
       // Note that we don't munlock the space because it's always better to keep
       // the singleton system dictionary paged in as long as the process runs.
-      SystemUtil::MaybeMLock(ptr_, len_);
-      if (!instance->dictionary_file_->OpenFromImage(ptr_, len_)) {
-        LOG(ERROR) << "Failed to open system dictionary file";
+      SystemUtil::MaybeMLock(spec_->ptr, spec_->len);
+      if (!instance->dictionary_file_->OpenFromImage(spec_->ptr, spec_->len)) {
+        LOG(ERROR) << "Failed to open system dictionary image";
         return NULL;
       }
       break;
@@ -568,7 +611,7 @@
   }
 
   if (!instance->OpenDictionaryFile(
-          (options_ & ENABLE_REVERSE_LOOKUP_INDEX) != 0)) {
+          (spec_->options & ENABLE_REVERSE_LOOKUP_INDEX) != 0)) {
     LOG(ERROR) << "Failed to create system dictionary";
     return NULL;
   }
@@ -1167,7 +1210,7 @@
     pos += Util::OneCharLen(suffix.data());
   }
   // Collect tokens for all IDs.
-  ScanTokens(id_set, &cache->results);
+  ScanTokens(id_set, cache);
 }
 
 void SystemDictionary::ClearReverseLookupCache(
@@ -1229,16 +1272,17 @@
   const bool has_cache = (allocator != NULL &&
                           allocator->data().has(kReverseLookupCache));
   ReverseLookupCache *cache =
-      (has_cache ? allocator->mutable_data()->get<ReverseLookupCache>(
-          kReverseLookupCache) : NULL);
+      has_cache
+      ? allocator->mutable_data()->get<ReverseLookupCache>(kReverseLookupCache)
+      : NULL;
 
-  multimap<int, ReverseLookupResult> *results = NULL;
-  multimap<int, ReverseLookupResult> non_cached_results;
+  ReverseLookupCache *results = NULL;
+  ReverseLookupCache non_cached_results;
   if (reverse_lookup_index_ != NULL) {
-    reverse_lookup_index_->FillResultMap(id_set, &non_cached_results);
+    reverse_lookup_index_->FillResultMap(id_set, &non_cached_results.results);
     results = &non_cached_results;
-  } else if (cache != NULL && IsCacheAvailable(id_set, cache->results)) {
-    results = &(cache->results);
+  } else if (cache != NULL && cache->IsAvailable(id_set)) {
+    results = cache;
   } else {
     // Cache is not available. Get token for each ID.
     ScanTokens(id_set, &non_cached_results);
@@ -1250,8 +1294,7 @@
 }
 
 void SystemDictionary::ScanTokens(
-    const set<int> &id_set,
-    multimap<int, ReverseLookupResult> *reverse_results) const {
+    const set<int> &id_set, ReverseLookupCache *cache) const {
   for (TokenScanIterator iter(codec_, token_array_);
        !iter.Done(); iter.Next()) {
     const TokenScanIterator::Result &result = iter.Get();
@@ -1260,14 +1303,14 @@
       ReverseLookupResult lookup_result;
       lookup_result.tokens_offset = result.tokens_offset;
       lookup_result.id_in_key_trie = result.index;
-      reverse_results->insert(make_pair(result.value_id, lookup_result));
+      cache->results.insert(make_pair(result.value_id, lookup_result));
     }
   }
 }
 
 void SystemDictionary::RegisterReverseLookupResults(
     const set<int> &id_set,
-    const multimap<int, ReverseLookupResult> &reverse_results,
+    const ReverseLookupCache &cache,
     Callback *callback) const {
   const uint8 *encoded_tokens_ptr = GetTokenArrayPtr(token_array_, 0);
   char buffer[LoudsTrie::kMaxDepth + 1];
@@ -1276,7 +1319,7 @@
        ++set_itr) {
     const int value_id = *set_itr;
     typedef multimap<int, ReverseLookupResult>::const_iterator ResultItr;
-    pair<ResultItr, ResultItr> range = reverse_results.equal_range(*set_itr);
+    pair<ResultItr, ResultItr> range = cache.results.equal_range(*set_itr);
     for (ResultItr result_itr = range.first;
          result_itr != range.second;
          ++result_itr) {
diff --git a/src/dictionary/system/system_dictionary.h b/src/dictionary/system/system_dictionary.h
index fc3948b..6f661bc 100644
--- a/src/dictionary/system/system_dictionary.h
+++ b/src/dictionary/system/system_dictionary.h
@@ -27,12 +27,9 @@
 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
-// Mozc system dictionary
-
 #ifndef MOZC_DICTIONARY_SYSTEM_SYSTEM_DICTIONARY_H_
 #define MOZC_DICTIONARY_SYSTEM_SYSTEM_DICTIONARY_H_
 
-#include <map>
 #include <set>
 #include <string>
 #include <vector>
@@ -54,7 +51,6 @@
 namespace dictionary {
 
 class SystemDictionaryCodecInterface;
-class ReverseLookupIndex;
 
 class SystemDictionary : public DictionaryInterface {
  public:
@@ -72,7 +68,9 @@
   //   SystemDictionary::Builder builder(filename);
   //   builder.SetOptions(SystemDictionary::NONE);
   //   builder.SetCodec(NULL);
-  //   SystemDictionary *dictionry = builder.Build();
+  //   SystemDictionary *dictionary = builder.Build();
+  //   ...
+  //   delete dictionary;
   class Builder {
    public:
     // Creates Builder from filename
@@ -93,23 +91,8 @@
     SystemDictionary *Build();
 
    private:
-    enum InputType {
-      FILENAME,
-      IMAGE,
-    };
-
-    InputType type_;
-
-    // For InputType::FILENAME
-    const string filename_;
-
-    // For InputTYpe::IMAGE
-    const char *ptr_;
-    const int len_;
-
-    Options options_;
-    const SystemDictionaryCodecInterface *codec_;
-
+    struct Specification;
+    scoped_ptr<Specification> spec_;
     DISALLOW_COPY_AND_ASSIGN(Builder);
   };
 
@@ -132,18 +115,11 @@
   virtual void ClearReverseLookupCache(
       NodeAllocatorInterface *allocator) const;
 
-  // TODO(noriyukit): This structure is implementation detail so should be
-  // hidden.
-  struct ReverseLookupResult {
-    ReverseLookupResult() : tokens_offset(-1), id_in_key_trie(-1) {}
-    // Offset from the tokens section beginning.
-    // (token_array_.Get(id_in_key_trie) == token_array_.Get(0) + tokens_offset)
-    int tokens_offset;
-    // Id in key trie
-    int id_in_key_trie;
-  };
-
  private:
+  class ReverseLookupCache;
+  class ReverseLookupIndex;
+  struct PredictiveLookupSearchState;
+
   explicit SystemDictionary(const SystemDictionaryCodecInterface *codec);
   bool OpenDictionaryFile(bool enable_reverse_lookup_index);
 
@@ -152,12 +128,10 @@
   void RegisterReverseLookupTokensForValue(StringPiece value,
                                            NodeAllocatorInterface *allocator,
                                            Callback *callback) const;
-  void ScanTokens(const set<int> &id_set,
-                  multimap<int, ReverseLookupResult> *reverse_results) const;
-  void RegisterReverseLookupResults(
-      const set<int> &id_set,
-      const multimap<int, ReverseLookupResult> &reverse_results,
-      Callback *callback) const;
+  void ScanTokens(const set<int> &id_set, ReverseLookupCache *cache) const;
+  void RegisterReverseLookupResults(const set<int> &id_set,
+                                    const ReverseLookupCache &cache,
+                                    Callback *callback) const;
   void InitReverseLookupIndex();
 
   Callback::ResultType LookupPrefixWithKeyExpansionImpl(
@@ -171,17 +145,6 @@
       char *actual_key_buffer,
       string *actual_prefix) const;
 
-  struct PredictiveLookupSearchState {
-    PredictiveLookupSearchState() : key_pos(0), is_expanded(false) {}
-    PredictiveLookupSearchState(const storage::louds::LoudsTrie::Node &n,
-                                size_t pos, bool expanded)
-        : node(n), key_pos(pos), is_expanded(expanded) {}
-
-    storage::louds::LoudsTrie::Node node;
-    size_t key_pos;
-    bool is_expanded;
-  };
-
   void CollectPredictiveNodesInBfsOrder(
       StringPiece encoded_key,
       const KeyExpansionTable &table,
diff --git a/src/mozc_version_template.txt b/src/mozc_version_template.txt
index 1768dcd..5d80494 100644
--- a/src/mozc_version_template.txt
+++ b/src/mozc_version_template.txt
@@ -1,6 +1,6 @@
 MAJOR=2
 MINOR=16
-BUILD=2062
+BUILD=2063
 REVISION=102
 # NACL_DICTIONARY_VERSION is the target version of the system dictionary to be
 # downloaded by NaCl Mozc.