import java.util.ArrayList;
import java.util.List;
+import com.hughes.android.dictionary.DictionaryInfo;
import com.hughes.util.CachingList;
import com.hughes.util.raf.RAFList;
import com.hughes.util.raf.RAFListSerializer;
static final int CACHE_SIZE = 5000;
+ static final int CURRENT_DICT_VERSION = 2;
static final String END_OF_DICTIONARY = "END OF DICTIONARY";
// persisted
public final List<EntrySource> sources;
public final List<Index> indices;
+ /**
+ * dictFileVersion 1 adds:
+ * <li> links to sources?
+ *
+ * dictFileVersion 2 adds:
+ * <li> counts of tokens in indices.
+ */
+
public Dictionary(final String dictInfo) {
- this.dictFileVersion = 0;
+ this.dictFileVersion = CURRENT_DICT_VERSION;
this.creationMillis = System.currentTimeMillis();
this.dictInfo = dictInfo;
pairEntries = new ArrayList<PairEntry>();
public Dictionary(final RandomAccessFile raf) throws IOException {
dictFileVersion = raf.readInt();
- if (dictFileVersion != 0) {
+ if (dictFileVersion < 0 || dictFileVersion > CURRENT_DICT_VERSION) {
throw new IOException("Invalid dictionary version: " + dictFileVersion);
}
creationMillis = raf.readLong();
dictInfo = raf.readUTF();
- sources = CachingList.createFullyCached(RAFList.create(raf, EntrySource.SERIALIZER, raf.getFilePointer()));
- pairEntries = CachingList.create(RAFList.create(raf, PairEntry.SERIALIZER, raf.getFilePointer()), CACHE_SIZE);
- textEntries = CachingList.create(RAFList.create(raf, TextEntry.SERIALIZER, raf.getFilePointer()), CACHE_SIZE);
+
+ // Load the sources, then seek past them, because reading them later disrupts the offset.
+ final RAFList<EntrySource> rafSources = RAFList.create(raf, EntrySource.SERIALIZER, raf.getFilePointer());
+ sources = new ArrayList<EntrySource>(rafSources);
+ raf.seek(rafSources.getEndOffset());
+
+ pairEntries = CachingList.create(RAFList.create(raf, new PairEntry.Serializer(this), raf.getFilePointer()), CACHE_SIZE);
+ textEntries = CachingList.create(RAFList.create(raf, new TextEntry.Serializer(this), raf.getFilePointer()), CACHE_SIZE);
indices = CachingList.createFullyCached(RAFList.create(raf, indexSerializer, raf.getFilePointer()));
final String end = raf.readUTF();
if (!end.equals(END_OF_DICTIONARY)) {
raf.writeLong(creationMillis);
raf.writeUTF(dictInfo);
RAFList.write(raf, sources, EntrySource.SERIALIZER);
- RAFList.write(raf, pairEntries, PairEntry.SERIALIZER);
- RAFList.write(raf, textEntries, TextEntry.SERIALIZER);
+ RAFList.write(raf, pairEntries, new PairEntry.Serializer(this));
+ RAFList.write(raf, textEntries, new TextEntry.Serializer(this));
RAFList.write(raf, indices, indexSerializer);
raf.writeUTF(END_OF_DICTIONARY);
}
}
}
+ public DictionaryInfo getDictionaryInfo() {
+ final DictionaryInfo result = new DictionaryInfo();
+ result.creationMillis = this.creationMillis;
+ result.dictInfo = this.dictInfo;
+ for (final Index index : indices) {
+ result.indexInfos.add(index.getIndexInfo());
+ }
+ return result;
+ }
+
}
\ No newline at end of file