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/dictionary_structure_with_buffer_policy_factory.h" 18 19 #include <stdint.h> 20 21 #include "defines.h" 22 #include "suggest/policyimpl/dictionary/dynamic_patricia_trie_policy.h" 23 #include "suggest/policyimpl/dictionary/patricia_trie_policy.h" 24 #include "suggest/policyimpl/dictionary/utils/format_utils.h" 25 #include "suggest/policyimpl/dictionary/utils/mmapped_buffer.h" 26 27 namespace latinime { 28 29 /* static */ DictionaryStructureWithBufferPolicy *DictionaryStructureWithBufferPolicyFactory 30 ::newDictionaryStructureWithBufferPolicy(const char *const path, const int bufOffset, 31 const int size, const bool isUpdatable) { 32 // Allocated buffer in MmapedBuffer::openBuffer() will be freed in the destructor of 33 // impl classes of DictionaryStructureWithBufferPolicy. 34 const MmappedBuffer *const mmapedBuffer = MmappedBuffer::openBuffer(path, bufOffset, size, 35 isUpdatable); 36 if (!mmapedBuffer) { 37 return 0; 38 } 39 switch (FormatUtils::detectFormatVersion(mmapedBuffer->getBuffer(), 40 mmapedBuffer->getBufferSize())) { 41 case FormatUtils::VERSION_2: 42 return new PatriciaTriePolicy(mmapedBuffer); 43 case FormatUtils::VERSION_3: 44 return new DynamicPatriciaTriePolicy(mmapedBuffer); 45 default: 46 AKLOGE("DICT: dictionary format is unknown, bad magic number"); 47 delete mmapedBuffer; 48 ASSERT(false); 49 return 0; 50 } 51 } 52 53 } // namespace latinime 54