From b084948c7f75bffbbcb52ed550345c4c8e99a534 Mon Sep 17 00:00:00 2001 From: thadh Date: Sun, 9 Sep 2012 20:40:06 -0700 Subject: [PATCH] First decent implementation of HtmlEntry attached to TokenRow. --- .../dictionary/engine/AbstractEntry.java | 15 ++-- .../android/dictionary/engine/Dictionary.java | 13 +++- .../android/dictionary/engine/HtmlEntry.java | 30 ++++---- .../android/dictionary/engine/Index.java | 72 +++++++++++-------- .../android/dictionary/engine/PairEntry.java | 28 ++++---- .../android/dictionary/engine/RowBase.java | 2 +- .../android/dictionary/engine/TextEntry.java | 26 +++---- .../android/dictionary/engine/TokenRow.java | 3 + 8 files changed, 107 insertions(+), 82 deletions(-) diff --git a/src/com/hughes/android/dictionary/engine/AbstractEntry.java b/src/com/hughes/android/dictionary/engine/AbstractEntry.java index 7bae09b..de221ea 100644 --- a/src/com/hughes/android/dictionary/engine/AbstractEntry.java +++ b/src/com/hughes/android/dictionary/engine/AbstractEntry.java @@ -14,19 +14,23 @@ package com.hughes.android.dictionary.engine; +import com.hughes.util.IndexedObject; + import java.io.IOException; import java.io.RandomAccessFile; -public abstract class AbstractEntry { +public abstract class AbstractEntry extends IndexedObject { final EntrySource entrySource; protected AbstractEntry(EntrySource entrySource) { + super(-1); this.entrySource = entrySource; } - public AbstractEntry(Dictionary dictionary, RandomAccessFile raf) throws IOException { + public AbstractEntry(Dictionary dictionary, RandomAccessFile raf, final int index) throws IOException { + super(index); if (dictionary.dictFileVersion >= 1) { final int entrySouceIdx = raf.readShort(); this.entrySource = dictionary.sources.get(entrySouceIdx); @@ -39,11 +43,8 @@ public abstract class AbstractEntry { raf.writeShort(entrySource.index()); } - /** - * @return this entry's position within the list just added to. - */ - public abstract int addToDictionary(final Dictionary dictionary); + public abstract void addToDictionary(final Dictionary dictionary); - public abstract RowBase CreateRow(int entryIndex, int rowIndex, Index dictionaryIndex); + public abstract RowBase CreateRow(int rowIndex, Index dictionaryIndex); } diff --git a/src/com/hughes/android/dictionary/engine/Dictionary.java b/src/com/hughes/android/dictionary/engine/Dictionary.java index c8e63d8..9224f51 100644 --- a/src/com/hughes/android/dictionary/engine/Dictionary.java +++ b/src/com/hughes/android/dictionary/engine/Dictionary.java @@ -33,7 +33,7 @@ public class Dictionary implements RAFSerializable { static final int CACHE_SIZE = 5000; - static final int CURRENT_DICT_VERSION = 5; + static final int CURRENT_DICT_VERSION = 6; static final String END_OF_DICTIONARY = "END OF DICTIONARY"; // persisted @@ -121,6 +121,17 @@ public class Dictionary implements RAFSerializable { t.write(raf); }}; + final RAFListSerializer htmlEntryIndexSerializer = new RAFListSerializer() { + @Override + public void write(RandomAccessFile raf, HtmlEntry t) throws IOException { + if (t.index() == -1) throw new IndexOutOfBoundsException(); + raf.writeInt(t.index()); + } + @Override + public HtmlEntry read(RandomAccessFile raf, int readIndex) throws IOException { + return htmlEntries.get(raf.readInt()); + }}; + public void print(final PrintStream out) { out.println("dictInfo=" + dictInfo); for (final EntrySource entrySource : sources) { diff --git a/src/com/hughes/android/dictionary/engine/HtmlEntry.java b/src/com/hughes/android/dictionary/engine/HtmlEntry.java index fe3fd1d..404bc6a 100644 --- a/src/com/hughes/android/dictionary/engine/HtmlEntry.java +++ b/src/com/hughes/android/dictionary/engine/HtmlEntry.java @@ -1,30 +1,27 @@ package com.hughes.android.dictionary.engine; +import com.hughes.util.raf.RAFListSerializer; +import com.hughes.util.raf.RAFSerializable; +import com.ibm.icu.text.Transliterator; + import java.io.IOException; import java.io.PrintStream; import java.io.RandomAccessFile; import java.util.List; import java.util.regex.Pattern; -import com.hughes.android.dictionary.engine.PairEntry.Pair; -import com.hughes.util.raf.RAFSerializable; -import com.hughes.util.raf.RAFSerializer; -import com.ibm.icu.text.Transliterator; - public class HtmlEntry extends AbstractEntry implements RAFSerializable, Comparable { public final String title; public String html; - - public HtmlEntry(final EntrySource entrySource, String title) { super(entrySource); this.title = title; } - public HtmlEntry(Dictionary dictionary, RandomAccessFile raf) throws IOException { - super(dictionary, raf); + public HtmlEntry(Dictionary dictionary, RandomAccessFile raf, final int index) throws IOException { + super(dictionary, raf, index); title = raf.readUTF(); html = raf.readUTF(); } @@ -36,18 +33,19 @@ public class HtmlEntry extends AbstractEntry implements RAFSerializable { + static final class Serializer implements RAFListSerializer { final Dictionary dictionary; @@ -56,8 +54,8 @@ public class HtmlEntry extends AbstractEntry implements RAFSerializable { static final int CACHE_SIZE = 5000; - final Dictionary dict; + public final Dictionary dict; public final String shortName; // Typically the ISO code for the language. public final String longName; @@ -124,7 +124,7 @@ public final class Index implements RAFSerializable { if (dict.dictFileVersion >= 2) { mainTokenCount = raf.readInt(); } - sortedIndexEntries = CachingList.create(RAFList.create(raf, IndexEntry.SERIALIZER, raf.getFilePointer()), CACHE_SIZE); + sortedIndexEntries = CachingList.create(RAFList.create(raf, indexEntrySerializer, raf.getFilePointer()), CACHE_SIZE); if (dict.dictFileVersion >= 4) { stoplist = new SerializableSerializer>().read(raf); } else { @@ -143,7 +143,7 @@ public final class Index implements RAFSerializable { if (dict.dictFileVersion >= 2) { raf.writeInt(mainTokenCount); } - RAFList.write(raf, sortedIndexEntries, IndexEntry.SERIALIZER); + RAFList.write(raf, sortedIndexEntries, indexEntrySerializer); new SerializableSerializer>().write(raf, stoplist); UniformRAFList.write(raf, (Collection) rows, new RowBase.Serializer(this), 5 /* bytes per entry */); } @@ -154,38 +154,49 @@ public final class Index implements RAFSerializable { } } - public static final class IndexEntry implements RAFSerializable { - public final String token; - private final String normalizedToken; - public final int startRow; - public final int numRows; // doesn't count the token row! - - - static final RAFSerializer SERIALIZER = new RAFSerializer () { + private final RAFSerializer indexEntrySerializer = new RAFSerializer () { @Override public IndexEntry read(RandomAccessFile raf) throws IOException { - return new IndexEntry(raf); + return new IndexEntry(Index.this, raf); } @Override public void write(RandomAccessFile raf, IndexEntry t) throws IOException { t.write(raf); }}; - public IndexEntry(final String token, final String normalizedToken, final int startRow, final int numRows) { + + public static final class IndexEntry implements RAFSerializable { + private final Index index; + public final String token; + private final String normalizedToken; + public final int startRow; + public final int numRows; // doesn't count the token row! + public final List htmlEntries; + + + public IndexEntry(final Index index, final String token, final String normalizedToken, final int startRow, final int numRows) { + this.index = index; assert token.equals(token.trim()); assert token.length() > 0; this.token = token; this.normalizedToken = normalizedToken; this.startRow = startRow; this.numRows = numRows; + this.htmlEntries = new ArrayList(); } - public IndexEntry(final RandomAccessFile raf) throws IOException { + public IndexEntry(final Index index, final RandomAccessFile raf) throws IOException { + this.index = index; token = raf.readUTF(); startRow = raf.readInt(); numRows = raf.readInt(); final boolean hasNormalizedForm = raf.readBoolean(); normalizedToken = hasNormalizedForm ? raf.readUTF() : token; + if (index.dict.dictFileVersion >= 6) { + this.htmlEntries = CachingList.create(RAFList.create(raf, index.dict.htmlEntryIndexSerializer, raf.getFilePointer()), 1); + } else { + this.htmlEntries = Collections.emptyList(); + } } public void write(RandomAccessFile raf) throws IOException { @@ -197,6 +208,7 @@ public final class Index implements RAFSerializable { if (hasNormalizedForm) { raf.writeUTF(normalizedToken); } + RAFList.write(raf, htmlEntries, index.dict.htmlEntryIndexSerializer); } public String toString() { @@ -247,10 +259,10 @@ public final class Index implements RAFSerializable { final int result = windBackCase(token, mid, interrupted); return result; } else if (comp < 0) { - //System.out.println("Upper bound: " + midEntry + ", norm=" + midEntry.normalizedToken() + ", mid=" + mid); + System.out.println("Upper bound: " + midEntry + ", norm=" + midEntry.normalizedToken() + ", mid=" + mid); end = mid; } else { - //System.out.println("Lower bound: " + midEntry + ", norm=" + midEntry.normalizedToken() + ", mid=" + mid); + System.out.println("Lower bound: " + midEntry + ", norm=" + midEntry.normalizedToken() + ", mid=" + mid); start = mid + 1; } } @@ -397,7 +409,7 @@ public final class Index implements RAFSerializable { result.addAll(ordered); } - System.out.println("searchDuration: " + (System.currentTimeMillis() - startMills)); + //System.out.println("searchDuration: " + (System.currentTimeMillis() - startMills)); return result; } diff --git a/src/com/hughes/android/dictionary/engine/PairEntry.java b/src/com/hughes/android/dictionary/engine/PairEntry.java index c48298a..3453dc3 100644 --- a/src/com/hughes/android/dictionary/engine/PairEntry.java +++ b/src/com/hughes/android/dictionary/engine/PairEntry.java @@ -14,6 +14,10 @@ package com.hughes.android.dictionary.engine; +import com.hughes.util.raf.RAFListSerializer; +import com.hughes.util.raf.RAFSerializable; +import com.ibm.icu.text.Transliterator; + import java.io.IOException; import java.io.PrintStream; import java.io.RandomAccessFile; @@ -21,11 +25,6 @@ import java.util.ArrayList; import java.util.List; import java.util.regex.Pattern; -import com.hughes.android.dictionary.engine.HtmlEntry.Row; -import com.hughes.util.raf.RAFSerializable; -import com.hughes.util.raf.RAFSerializer; -import com.ibm.icu.text.Transliterator; - public class PairEntry extends AbstractEntry implements RAFSerializable, Comparable { public final List pairs; @@ -40,8 +39,8 @@ public class PairEntry extends AbstractEntry implements RAFSerializable(size); for (int i = 0; i < size; ++i) { @@ -60,7 +59,7 @@ public class PairEntry extends AbstractEntry implements RAFSerializable { + static final class Serializer implements RAFListSerializer { final Dictionary dictionary; @@ -69,8 +68,8 @@ public class PairEntry extends AbstractEntry implements RAFSerializable { final String text; - public TextEntry(final Dictionary dictionary, final RandomAccessFile raf) throws IOException { - super(dictionary, raf); + public TextEntry(final Dictionary dictionary, final RandomAccessFile raf, final int index) throws IOException { + super(dictionary, raf, index); text = raf.readUTF(); throw new RuntimeException(); } @@ -40,7 +39,7 @@ public class TextEntry extends AbstractEntry implements RAFSerializable { + static final class Serializer implements RAFListSerializer { final Dictionary dictionary; @@ -49,8 +48,8 @@ public class TextEntry extends AbstractEntry implements RAFSerializable>>"); + } } @Override -- 2.43.0