Home | History | Annotate | Download | only in v4
      1 /*
      2  * Copyright (C) 2013, The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef LATINIME_VER4_PATRICIA_TRIE_WRITING_HELPER_H
     18 #define LATINIME_VER4_PATRICIA_TRIE_WRITING_HELPER_H
     19 
     20 #include "defines.h"
     21 #include "dictionary/structure/pt_common/dynamic_pt_gc_event_listeners.h"
     22 #include "dictionary/structure/v4/content/terminal_position_lookup_table.h"
     23 #include "dictionary/utils/entry_counters.h"
     24 
     25 namespace latinime {
     26 
     27 class HeaderPolicy;
     28 class Ver4DictBuffers;
     29 class Ver4PatriciaTrieNodeReader;
     30 class Ver4PatriciaTrieNodeWriter;
     31 
     32 class Ver4PatriciaTrieWritingHelper {
     33  public:
     34     Ver4PatriciaTrieWritingHelper(Ver4DictBuffers *const buffers)
     35             : mBuffers(buffers) {}
     36 
     37     bool writeToDictFile(const char *const dictDirPath, const EntryCounts &entryCounts) const;
     38 
     39     // This method cannot be const because the original dictionary buffer will be updated to detect
     40     // useless PtNodes during GC.
     41     bool writeToDictFileWithGC(const int rootPtNodeArrayPos, const char *const dictDirPath);
     42 
     43  private:
     44     DISALLOW_IMPLICIT_CONSTRUCTORS(Ver4PatriciaTrieWritingHelper);
     45 
     46     class TraversePolicyToUpdateAllPtNodeFlagsAndTerminalIds
     47             : public DynamicPtReadingHelper::TraversingEventListener {
     48      public:
     49         TraversePolicyToUpdateAllPtNodeFlagsAndTerminalIds(
     50                 Ver4PatriciaTrieNodeWriter *const ptNodeWriter,
     51                 const TerminalPositionLookupTable::TerminalIdMap *const terminalIdMap)
     52                 : mPtNodeWriter(ptNodeWriter), mTerminalIdMap(terminalIdMap) {}
     53 
     54         bool onAscend() { return true; }
     55 
     56         bool onDescend(const int ptNodeArrayPos) { return true; }
     57 
     58         bool onReadingPtNodeArrayTail() { return true; }
     59 
     60         bool onVisitingPtNode(const PtNodeParams *const ptNodeParams);
     61 
     62      private:
     63         DISALLOW_IMPLICIT_CONSTRUCTORS(TraversePolicyToUpdateAllPtNodeFlagsAndTerminalIds);
     64 
     65         Ver4PatriciaTrieNodeWriter *const mPtNodeWriter;
     66         const TerminalPositionLookupTable::TerminalIdMap *const mTerminalIdMap;
     67     };
     68 
     69     bool runGC(const int rootPtNodeArrayPos, const HeaderPolicy *const headerPolicy,
     70             Ver4DictBuffers *const buffersToWrite, MutableEntryCounters *const outEntryCounters);
     71 
     72     Ver4DictBuffers *const mBuffers;
     73 };
     74 } // namespace latinime
     75 
     76 #endif /* LATINIME_VER4_PATRICIA_TRIE_WRITING_HELPER_H */
     77