Home | History | Annotate | Download | only in content
      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 #include "suggest/policyimpl/dictionary/structure/v4/content/terminal_position_lookup_table.h"
     18 
     19 #include "suggest/policyimpl/dictionary/structure/v4/ver4_patricia_trie_reading_utils.h"
     20 #include "suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.h"
     21 
     22 namespace latinime {
     23 
     24 int TerminalPositionLookupTable::getTerminalPtNodePosition(const int terminalId) const {
     25     if (terminalId < 0 || terminalId >= mSize) {
     26         return NOT_A_DICT_POS;
     27     }
     28     const int terminalPos = getBuffer()->readUint(
     29             Ver4DictConstants::TERMINAL_ADDRESS_TABLE_ADDRESS_SIZE, getEntryPos(terminalId));
     30     return (terminalPos == Ver4DictConstants::NOT_A_TERMINAL_ADDRESS) ?
     31             NOT_A_DICT_POS : terminalPos;
     32 }
     33 
     34 bool TerminalPositionLookupTable::setTerminalPtNodePosition(
     35         const int terminalId, const int terminalPtNodePos) {
     36     if (terminalId < 0) {
     37         return NOT_A_DICT_POS;
     38     }
     39     while (terminalId >= mSize) {
     40         // Write new entry.
     41         if (!getWritableBuffer()->writeUint(Ver4DictConstants::NOT_A_TERMINAL_ADDRESS,
     42                 Ver4DictConstants::TERMINAL_ADDRESS_TABLE_ADDRESS_SIZE, getEntryPos(mSize))) {
     43             return false;
     44         }
     45         mSize++;
     46     }
     47     const int terminalPos = (terminalPtNodePos != NOT_A_DICT_POS) ?
     48             terminalPtNodePos : Ver4DictConstants::NOT_A_TERMINAL_ADDRESS;
     49     return getWritableBuffer()->writeUint(terminalPos,
     50             Ver4DictConstants::TERMINAL_ADDRESS_TABLE_ADDRESS_SIZE, getEntryPos(terminalId));
     51 }
     52 
     53 bool TerminalPositionLookupTable::flushToFile(FILE *const file) const {
     54     // If the used buffer size is smaller than the actual buffer size, regenerate the lookup
     55     // table and write the new table to the file.
     56     if (getEntryPos(mSize) < getBuffer()->getTailPosition()) {
     57         TerminalPositionLookupTable lookupTableToWrite;
     58         for (int i = 0; i < mSize; ++i) {
     59             const int terminalPtNodePosition = getTerminalPtNodePosition(i);
     60             if (!lookupTableToWrite.setTerminalPtNodePosition(i, terminalPtNodePosition)) {
     61                 AKLOGE("Cannot set terminal position to lookupTableToWrite."
     62                         " terminalId: %d, position: %d", i, terminalPtNodePosition);
     63                 return false;
     64             }
     65         }
     66         return lookupTableToWrite.flush(file);
     67     } else {
     68         // We can simply use this lookup table because the buffer size has not been
     69         // changed.
     70         return flush(file);
     71     }
     72 }
     73 
     74 bool TerminalPositionLookupTable::runGCTerminalIds(TerminalIdMap *const terminalIdMap) {
     75     int removedEntryCount = 0;
     76     int nextNewTerminalId = 0;
     77     for (int i = 0; i < mSize; ++i) {
     78         const int terminalPos = getBuffer()->readUint(
     79                 Ver4DictConstants::TERMINAL_ADDRESS_TABLE_ADDRESS_SIZE, getEntryPos(i));
     80         if (terminalPos == Ver4DictConstants::NOT_A_TERMINAL_ADDRESS) {
     81             // This entry is a garbage.
     82             removedEntryCount++;
     83         } else {
     84             // Give a new terminal id to the entry.
     85             if (!getWritableBuffer()->writeUint(terminalPos,
     86                     Ver4DictConstants::TERMINAL_ADDRESS_TABLE_ADDRESS_SIZE,
     87                     getEntryPos(nextNewTerminalId))) {
     88                 return false;
     89             }
     90             // Memorize the mapping to the old terminal id to the new terminal id.
     91             terminalIdMap->insert(TerminalIdMap::value_type(i, nextNewTerminalId));
     92             nextNewTerminalId++;
     93         }
     94     }
     95     mSize = nextNewTerminalId;
     96     return true;
     97 }
     98 
     99 } // namespace latinime
    100