+ final int dictFileVersion;
+ public final long creationMillis;
+ public final String dictInfo;
+ public final List<PairEntry> pairEntries;
+ public final List<TextEntry> textEntries;
+ public final List<HtmlEntry> htmlEntries;
+ public final List<byte[]> htmlData;
+ 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 = CURRENT_DICT_VERSION;
+ this.creationMillis = System.currentTimeMillis();
+ this.dictInfo = dictInfo;
+ pairEntries = new ArrayList<>();
+ textEntries = new ArrayList<>();
+ htmlEntries = new ArrayList<>();
+ htmlData = null;
+ sources = new ArrayList<>();
+ indices = new ArrayList<>();
+ }
+
+ public Dictionary(final FileChannel ch) throws IOException {
+ DataInput raf = new DataInputStream(Channels.newInputStream(ch));
+ dictFileVersion = raf.readInt();
+ if (dictFileVersion < 0 || dictFileVersion > CURRENT_DICT_VERSION) {
+ throw new IOException("Invalid dictionary version: " + dictFileVersion);
+ }
+ creationMillis = raf.readLong();
+ dictInfo = raf.readUTF();
+
+ // Load the sources, then seek past them, because reading them later
+ // disrupts the offset.
+ try {
+ final RAFList<EntrySource> rafSources = RAFList.create(ch, new EntrySource.Serializer(
+ this), ch.position(), dictFileVersion, dictInfo + " sources: ");
+ sources = new ArrayList<>(rafSources);
+ ch.position(rafSources.getEndOffset());
+
+ pairEntries = CachingList.create(
+ RAFList.create(ch, new PairEntry.Serializer(this), ch.position(), dictFileVersion, dictInfo + " pairs: "),
+ CACHE_SIZE, false);
+ textEntries = CachingList.create(
+ RAFList.create(ch, new TextEntry.Serializer(this), ch.position(), dictFileVersion, dictInfo + " text: "),
+ CACHE_SIZE, true);
+ if (dictFileVersion >= 5) {
+ htmlEntries = CachingList.create(
+ RAFList.create(ch, new HtmlEntry.Serializer(this, ch), ch.position(), dictFileVersion, dictInfo + " html: "),
+ CACHE_SIZE, false);
+ } else {
+ htmlEntries = Collections.emptyList();
+ }
+ if (dictFileVersion >= 7) {
+ htmlData = RAFList.create(ch, new HtmlEntry.DataDeserializer(), ch.position(), dictFileVersion, dictInfo + " html: ");
+ } else {
+ htmlData = null;
+ }
+ indices = CachingList.createFullyCached(RAFList.create(ch, new IndexSerializer(ch),
+ ch.position(), dictFileVersion, dictInfo + " index: "));
+ } catch (RuntimeException e) {
+ throw new IOException("RuntimeException loading dictionary", e);
+ }
+ final String end = raf.readUTF();
+ if (!end.equals(END_OF_DICTIONARY)) {
+ throw new IOException("Dictionary seems corrupt: " + end);
+ }
+ }
+
+ @Override
+ public void write(DataOutput out) throws IOException {
+ RandomAccessFile raf = (RandomAccessFile)out;
+ if (dictFileVersion < 7) throw new RuntimeException("write function cannot write formats older than v7!");
+ raf.writeInt(dictFileVersion);
+ raf.writeLong(creationMillis);
+ raf.writeUTF(dictInfo);
+ System.out.println("sources start: " + raf.getFilePointer());
+ RAFList.write(raf, sources, new EntrySource.Serializer(this));
+ System.out.println("pair start: " + raf.getFilePointer());
+ RAFList.write(raf, pairEntries, new PairEntry.Serializer(this), 64, true);
+ System.out.println("text start: " + raf.getFilePointer());
+ RAFList.write(raf, textEntries, new TextEntry.Serializer(this));
+ System.out.println("html index start: " + raf.getFilePointer());
+ RAFList.write(raf, htmlEntries, new HtmlEntry.Serializer(this, null), 64, true);
+ System.out.println("html data start: " + raf.getFilePointer());
+ assert htmlData == null;
+ RAFList.write(raf, htmlEntries, new HtmlEntry.DataSerializer(), 128, true);
+ System.out.println("indices start: " + raf.getFilePointer());
+ RAFList.write(raf, indices, new IndexSerializer(null));
+ System.out.println("end: " + raf.getFilePointer());
+ raf.writeUTF(END_OF_DICTIONARY);