]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/engine/Dictionary.java
Run automated code cleanup.
[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.DataInputStream;
26 import java.io.DataOutput;
27 import java.io.File;
28 import java.io.IOException;
29 import java.io.PrintStream;
30 import java.io.RandomAccessFile;
31 import java.nio.channels.Channels;
32 import java.nio.channels.FileChannel;
33 import java.util.ArrayList;
34 import java.util.Collections;
35 import java.util.List;
36
37 public class Dictionary implements RAFSerializable<Dictionary> {
38
39     static final int CACHE_SIZE = 5000;
40
41     static final int CURRENT_DICT_VERSION = 7;
42     static final String END_OF_DICTIONARY = "END OF DICTIONARY";
43
44     // persisted
45     final int dictFileVersion;
46     final long creationMillis;
47     public final String dictInfo;
48     public final List<PairEntry> pairEntries;
49     public final List<TextEntry> textEntries;
50     public final List<HtmlEntry> htmlEntries;
51     public final List<byte[]> htmlData;
52     public final List<EntrySource> sources;
53     public final List<Index> indices;
54
55     /**
56      * dictFileVersion 1 adds: <li>links to sources? dictFileVersion 2 adds: <li>
57      * counts of tokens in indices.
58      */
59
60     public Dictionary(final String dictInfo) {
61         this.dictFileVersion = CURRENT_DICT_VERSION;
62         this.creationMillis = System.currentTimeMillis();
63         this.dictInfo = dictInfo;
64         pairEntries = new ArrayList<PairEntry>();
65         textEntries = new ArrayList<TextEntry>();
66         htmlEntries = new ArrayList<HtmlEntry>();
67         htmlData = null;
68         sources = new ArrayList<EntrySource>();
69         indices = new ArrayList<Index>();
70     }
71
72     public Dictionary(final FileChannel ch) throws IOException {
73         DataInput raf = new DataInputStream(Channels.newInputStream(ch));
74         dictFileVersion = raf.readInt();
75         if (dictFileVersion < 0 || dictFileVersion > CURRENT_DICT_VERSION) {
76             throw new IOException("Invalid dictionary version: " + dictFileVersion);
77         }
78         creationMillis = raf.readLong();
79         dictInfo = raf.readUTF();
80
81         // Load the sources, then seek past them, because reading them later
82         // disrupts the offset.
83         try {
84             final RAFList<EntrySource> rafSources = RAFList.create(ch, new EntrySource.Serializer(
85                     this), ch.position(), dictFileVersion, dictInfo + " sources: ");
86             sources = new ArrayList<EntrySource>(rafSources);
87             ch.position(rafSources.getEndOffset());
88
89             pairEntries = CachingList.create(
90                               RAFList.create(ch, new PairEntry.Serializer(this), ch.position(), dictFileVersion, dictInfo + " pairs: "),
91                               CACHE_SIZE, false);
92             textEntries = CachingList.create(
93                               RAFList.create(ch, new TextEntry.Serializer(this), ch.position(), dictFileVersion, dictInfo + " text: "),
94                               CACHE_SIZE, true);
95             if (dictFileVersion >= 5) {
96                 htmlEntries = CachingList.create(
97                                   RAFList.create(ch, new HtmlEntry.Serializer(this, ch), ch.position(), dictFileVersion, dictInfo + " html: "),
98                                   CACHE_SIZE, false);
99             } else {
100                 htmlEntries = Collections.emptyList();
101             }
102             if (dictFileVersion >= 7) {
103                 htmlData = RAFList.create(ch, new HtmlEntry.DataDeserializer(), ch.position(), dictFileVersion, dictInfo + " html: ");
104             } else {
105                 htmlData = null;
106             }
107             indices = CachingList.createFullyCached(RAFList.create(ch, new IndexSerializer(ch),
108                                                     ch.position(), dictFileVersion, dictInfo + " index: "));
109         } catch (RuntimeException e) {
110             final IOException ioe = new IOException("RuntimeException loading dictionary", e);
111             throw ioe;
112         }
113         final String end = raf.readUTF();
114         if (!end.equals(END_OF_DICTIONARY)) {
115             throw new IOException("Dictionary seems corrupt: " + end);
116         }
117     }
118
119     @Override
120     public void write(DataOutput out) throws IOException {
121         RandomAccessFile raf = (RandomAccessFile)out;
122         raf.writeInt(dictFileVersion);
123         raf.writeLong(creationMillis);
124         raf.writeUTF(dictInfo);
125         System.out.println("sources start: " + raf.getFilePointer());
126         RAFList.write(raf, sources, new EntrySource.Serializer(this));
127         System.out.println("pair start: " + raf.getFilePointer());
128         RAFList.write(raf, pairEntries, new PairEntry.Serializer(this), 64, true);
129         System.out.println("text start: " + raf.getFilePointer());
130         RAFList.write(raf, textEntries, new TextEntry.Serializer(this));
131         System.out.println("html index start: " + raf.getFilePointer());
132         RAFList.write(raf, htmlEntries, new HtmlEntry.Serializer(this, null), 64, true);
133         System.out.println("html data start: " + raf.getFilePointer());
134         assert htmlData == null;
135         RAFList.write(raf, htmlEntries, new HtmlEntry.DataSerializer(), 128, true);
136         System.out.println("indices start: " + raf.getFilePointer());
137         RAFList.write(raf, indices, new IndexSerializer(null));
138         System.out.println("end: " + raf.getFilePointer());
139         raf.writeUTF(END_OF_DICTIONARY);
140     }
141
142     private final class IndexSerializer implements RAFListSerializer<Index> {
143         private final FileChannel ch;
144
145         public IndexSerializer(FileChannel ch) {
146             this.ch = ch;
147         }
148
149         @Override
150         public Index read(DataInput raf, final int readIndex) throws IOException {
151             return new Index(Dictionary.this, ch, raf);
152         }
153
154         @Override
155         public void write(DataOutput raf, Index t) throws IOException {
156             t.write(raf);
157         }
158     }
159
160     final RAFListSerializer<HtmlEntry> htmlEntryIndexSerializer = new RAFListSerializer<HtmlEntry>() {
161         @Override
162         public void write(DataOutput raf, HtmlEntry t) {
163             assert false;
164         }
165
166         @Override
167         public HtmlEntry read(DataInput raf, int readIndex) throws IOException {
168             return htmlEntries.get(raf.readInt());
169         }
170     };
171
172     public void print(final PrintStream out) {
173         out.println("dictInfo=" + dictInfo);
174         for (final EntrySource entrySource : sources) {
175             out.printf("EntrySource: %s %d\n", entrySource.name, entrySource.numEntries);
176         }
177         out.println();
178         for (final Index index : indices) {
179             out.printf("Index: %s %s\n", index.shortName, index.longName);
180             index.print(out);
181             out.println();
182         }
183     }
184
185     public DictionaryInfo getDictionaryInfo() {
186         final DictionaryInfo result = new DictionaryInfo();
187         result.creationMillis = this.creationMillis;
188         result.dictInfo = this.dictInfo;
189         for (final Index index : indices) {
190             result.indexInfos.add(index.getIndexInfo());
191         }
192         return result;
193     }
194
195     public static DictionaryInfo getDictionaryInfo(final File file) {
196         RandomAccessFile raf = null;
197         try {
198             raf = new RandomAccessFile(file, "r");
199             final Dictionary dict = new Dictionary(raf.getChannel());
200             final DictionaryInfo dictionaryInfo = dict.getDictionaryInfo();
201             dictionaryInfo.uncompressedFilename = file.getName();
202             dictionaryInfo.uncompressedBytes = file.length();
203             raf.close();
204             return dictionaryInfo;
205         } catch (IOException e) {
206             final DictionaryInfo dictionaryInfo = new DictionaryInfo();
207             dictionaryInfo.uncompressedFilename = file.getName();
208             dictionaryInfo.uncompressedBytes = file.length();
209             return dictionaryInfo;
210         } finally {
211             if (raf != null) {
212                 try {
213                     raf.close();
214                 } catch (IOException e) {
215                     e.printStackTrace();
216                 }
217             }
218         }
219     }
220
221 }