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