]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/engine/Dictionary.java
Improve v7 format compression.
[Dictionary.git] / src / com / hughes / android / dictionary / engine / Dictionary.java
1 // Copyright 2011 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package com.hughes.android.dictionary.engine;
16
17 import com.hughes.android.dictionary.DictionaryInfo;
18 import com.hughes.util.CachingList;
19 import com.hughes.util.StringUtil;
20 import com.hughes.util.raf.RAFList;
21 import com.hughes.util.raf.RAFListSerializer;
22 import com.hughes.util.raf.RAFSerializable;
23
24 import java.io.DataInput;
25 import java.io.DataOutput;
26 import java.io.File;
27 import java.io.IOException;
28 import java.io.PrintStream;
29 import java.io.RandomAccessFile;
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.List;
33
34 public class Dictionary implements RAFSerializable<Dictionary> {
35
36     static final int CACHE_SIZE = 5000;
37
38     static final int CURRENT_DICT_VERSION = 7;
39     static final String END_OF_DICTIONARY = "END OF DICTIONARY";
40
41     // persisted
42     final int dictFileVersion;
43     final long creationMillis;
44     public final String dictInfo;
45     public final List<PairEntry> pairEntries;
46     public final List<TextEntry> textEntries;
47     public final List<HtmlEntry> htmlEntries;
48     public final List<byte[]> htmlData;
49     public final List<EntrySource> sources;
50     public final List<Index> indices;
51
52     /**
53      * dictFileVersion 1 adds: <li>links to sources? dictFileVersion 2 adds: <li>
54      * counts of tokens in indices.
55      */
56
57     public Dictionary(final String dictInfo) {
58         this.dictFileVersion = CURRENT_DICT_VERSION;
59         this.creationMillis = System.currentTimeMillis();
60         this.dictInfo = dictInfo;
61         pairEntries = new ArrayList<PairEntry>();
62         textEntries = new ArrayList<TextEntry>();
63         htmlEntries = new ArrayList<HtmlEntry>();
64         htmlData = null;
65         sources = new ArrayList<EntrySource>();
66         indices = new ArrayList<Index>();
67     }
68
69     public Dictionary(final RandomAccessFile raf) throws IOException {
70         dictFileVersion = raf.readInt();
71         if (dictFileVersion < 0 || dictFileVersion > CURRENT_DICT_VERSION) {
72             throw new IOException("Invalid dictionary version: " + dictFileVersion);
73         }
74         creationMillis = raf.readLong();
75         dictInfo = raf.readUTF();
76
77         // Load the sources, then seek past them, because reading them later
78         // disrupts the offset.
79         try {
80             final RAFList<EntrySource> rafSources = RAFList.create(raf, new EntrySource.Serializer(
81                     this), raf.getFilePointer(), dictFileVersion);
82             sources = new ArrayList<EntrySource>(rafSources);
83             raf.seek(rafSources.getEndOffset());
84
85             pairEntries = CachingList.create(
86                     RAFList.create(raf, new PairEntry.Serializer(this), raf.getFilePointer(), dictFileVersion),
87                     CACHE_SIZE);
88             textEntries = CachingList.create(
89                     RAFList.create(raf, new TextEntry.Serializer(this), raf.getFilePointer(), dictFileVersion),
90                     CACHE_SIZE);
91             if (dictFileVersion >= 5) {
92                 htmlEntries = CachingList.create(
93                         RAFList.create(raf, new HtmlEntry.Serializer(this), raf.getFilePointer(), dictFileVersion),
94                         CACHE_SIZE);
95             } else {
96                 htmlEntries = Collections.emptyList();
97             }
98             if (dictFileVersion >= 7) {
99                 htmlData = RAFList.create(raf, new HtmlEntry.DataDeserializer(), raf.getFilePointer(), dictFileVersion);
100             } else {
101                 htmlData = null;
102             }
103             indices = CachingList.createFullyCached(RAFList.create(raf, indexSerializer,
104                     raf.getFilePointer(), dictFileVersion));
105         } catch (RuntimeException e) {
106             final IOException ioe = new IOException("RuntimeException loading dictionary");
107             ioe.initCause(e);
108             throw ioe;
109         }
110         final String end = raf.readUTF();
111         if (!end.equals(END_OF_DICTIONARY)) {
112             throw new IOException("Dictionary seems corrupt: " + end);
113         }
114     }
115
116     @Override
117     public void write(DataOutput out) throws IOException {
118         RandomAccessFile raf = (RandomAccessFile)out;
119         raf.writeInt(dictFileVersion);
120         raf.writeLong(creationMillis);
121         raf.writeUTF(dictInfo);
122         System.out.println("sources start: " + raf.getFilePointer());
123         RAFList.write(raf, sources, new EntrySource.Serializer(this));
124         System.out.println("pair start: " + raf.getFilePointer());
125         RAFList.write(raf, pairEntries, new PairEntry.Serializer(this), 64, true);
126         System.out.println("text start: " + raf.getFilePointer());
127         RAFList.write(raf, textEntries, new TextEntry.Serializer(this));
128         System.out.println("html start: " + raf.getFilePointer());
129         RAFList.write(raf, htmlEntries, new HtmlEntry.Serializer(this), 64, true);
130         assert htmlData == null;
131         RAFList.write(raf, htmlEntries, new HtmlEntry.DataSerializer(), 128, true);
132         System.out.println("indices start: " + raf.getFilePointer());
133         RAFList.write(raf, indices, indexSerializer);
134         System.out.println("end: " + raf.getFilePointer());
135         raf.writeUTF(END_OF_DICTIONARY);
136     }
137
138     private final RAFListSerializer<Index> indexSerializer = new RAFListSerializer<Index>() {
139         @Override
140         public Index read(DataInput raf, final int readIndex) throws IOException {
141             return new Index(Dictionary.this, raf);
142         }
143
144         @Override
145         public void write(DataOutput raf, Index t) throws IOException {
146             t.write(raf);
147         }
148     };
149
150     final RAFListSerializer<HtmlEntry> htmlEntryIndexSerializer = new RAFListSerializer<HtmlEntry>() {
151         @Override
152         public void write(DataOutput raf, HtmlEntry t) throws IOException {
153             if (t.index() == -1)
154                 throw new IndexOutOfBoundsException();
155             raf.writeInt(t.index());
156         }
157
158         @Override
159         public HtmlEntry read(DataInput raf, int readIndex) throws IOException {
160             return htmlEntries.get(raf.readInt());
161         }
162     };
163
164     public void print(final PrintStream out) {
165         out.println("dictInfo=" + dictInfo);
166         for (final EntrySource entrySource : sources) {
167             out.printf("EntrySource: %s %d\n", entrySource.name, entrySource.numEntries);
168         }
169         out.println();
170         for (final Index index : indices) {
171             out.printf("Index: %s %s\n", index.shortName, index.longName);
172             index.print(out);
173             out.println();
174         }
175     }
176
177     public DictionaryInfo getDictionaryInfo() {
178         final DictionaryInfo result = new DictionaryInfo();
179         result.creationMillis = this.creationMillis;
180         result.dictInfo = this.dictInfo;
181         for (final Index index : indices) {
182             result.indexInfos.add(index.getIndexInfo());
183         }
184         return result;
185     }
186
187     public static DictionaryInfo getDictionaryInfo(final File file) {
188         RandomAccessFile raf = null;
189         try {
190             raf = new RandomAccessFile(file, "r");
191             final Dictionary dict = new Dictionary(raf);
192             final DictionaryInfo dictionaryInfo = dict.getDictionaryInfo();
193             dictionaryInfo.uncompressedFilename = file.getName();
194             dictionaryInfo.uncompressedBytes = file.length();
195             raf.close();
196             return dictionaryInfo;
197         } catch (IOException e) {
198             return null;
199         } finally {
200             if (raf != null) {
201                 try {
202                     raf.close();
203                 } catch (IOException e) {
204                     e.printStackTrace();
205                 }
206             }
207         }
208     }
209
210 }