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);
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);
}
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
t.write(raf);
}};
+ final RAFListSerializer<HtmlEntry> htmlEntryIndexSerializer = new RAFListSerializer<HtmlEntry>() {
+ @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) {
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<HtmlEntry>, Comparable<HtmlEntry> {
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();
}
}
@Override
- public int addToDictionary(Dictionary dictionary) {
+ public void addToDictionary(Dictionary dictionary) {
+ assert index == -1;
dictionary.htmlEntries.add(this);
- return dictionary.htmlEntries.size() - 1;
+ index = dictionary.htmlEntries.size() - 1;
}
@Override
- public RowBase CreateRow(int entryIndex, int rowIndex, Index dictionaryIndex) {
- return new Row(entryIndex, rowIndex, dictionaryIndex);
+ public RowBase CreateRow(int rowIndex, Index dictionaryIndex) {
+ return new Row(this.index, rowIndex, dictionaryIndex);
}
- static final class Serializer implements RAFSerializer<HtmlEntry> {
+ static final class Serializer implements RAFListSerializer<HtmlEntry> {
final Dictionary dictionary;
}
@Override
- public HtmlEntry read(RandomAccessFile raf) throws IOException {
- return new HtmlEntry(dictionary, raf);
+ public HtmlEntry read(RandomAccessFile raf, final int index) throws IOException {
+ return new HtmlEntry(dictionary, raf, index);
}
@Override
*/
package com.hughes.android.dictionary.engine;
+import com.hughes.android.dictionary.DictionaryInfo;
+import com.hughes.android.dictionary.DictionaryInfo.IndexInfo;
+import com.hughes.android.dictionary.engine.RowBase.RowKey;
+import com.hughes.util.CachingList;
+import com.hughes.util.TransformingList;
+import com.hughes.util.raf.RAFList;
+import com.hughes.util.raf.RAFSerializable;
+import com.hughes.util.raf.RAFSerializer;
+import com.hughes.util.raf.SerializableSerializer;
+import com.hughes.util.raf.UniformRAFList;
+import com.ibm.icu.text.Collator;
+import com.ibm.icu.text.Transliterator;
+
import java.io.IOException;
import java.io.PrintStream;
import java.io.RandomAccessFile;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Pattern;
-import com.hughes.android.dictionary.DictionaryInfo;
-import com.hughes.android.dictionary.DictionaryInfo.IndexInfo;
-import com.hughes.android.dictionary.engine.RowBase.RowKey;
-import com.hughes.util.CachingList;
-import com.hughes.util.TransformingList;
-import com.hughes.util.raf.RAFList;
-import com.hughes.util.raf.RAFSerializable;
-import com.hughes.util.raf.RAFSerializer;
-import com.hughes.util.raf.SerializableSerializer;
-import com.hughes.util.raf.UniformRAFList;
-import com.ibm.icu.text.Collator;
-import com.ibm.icu.text.Transliterator;
-
public final class Index implements RAFSerializable<Index> {
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;
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<Set<String>>().read(raf);
} else {
if (dict.dictFileVersion >= 2) {
raf.writeInt(mainTokenCount);
}
- RAFList.write(raf, sortedIndexEntries, IndexEntry.SERIALIZER);
+ RAFList.write(raf, sortedIndexEntries, indexEntrySerializer);
new SerializableSerializer<Set<String>>().write(raf, stoplist);
UniformRAFList.write(raf, (Collection<RowBase>) rows, new RowBase.Serializer(this), 5 /* bytes per entry */);
}
}
}
- public static final class IndexEntry implements RAFSerializable<Index.IndexEntry> {
- 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<IndexEntry> SERIALIZER = new RAFSerializer<IndexEntry> () {
+ private final RAFSerializer<IndexEntry> indexEntrySerializer = new RAFSerializer<IndexEntry> () {
@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<Index.IndexEntry> {
+ 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<HtmlEntry> 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<HtmlEntry>();
}
- 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 {
if (hasNormalizedForm) {
raf.writeUTF(normalizedToken);
}
+ RAFList.write(raf, htmlEntries, index.dict.htmlEntryIndexSerializer);
}
public String toString() {
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;
}
}
result.addAll(ordered);
}
- System.out.println("searchDuration: " + (System.currentTimeMillis() - startMills));
+ //System.out.println("searchDuration: " + (System.currentTimeMillis() - startMills));
return result;
}
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.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<PairEntry>, Comparable<PairEntry> {
public final List<Pair> pairs;
this.pairs.add(new Pair(lang1, lang2));
}
- public PairEntry(final Dictionary dictionary, final RandomAccessFile raf) throws IOException {
- super(dictionary, raf);
+ public PairEntry(final Dictionary dictionary, final RandomAccessFile raf, final int index) throws IOException {
+ super(dictionary, raf, index);
final int size = raf.readInt();
pairs = new ArrayList<PairEntry.Pair>(size);
for (int i = 0; i < size; ++i) {
}
}
- static final class Serializer implements RAFSerializer<PairEntry> {
+ static final class Serializer implements RAFListSerializer<PairEntry> {
final Dictionary dictionary;
}
@Override
- public PairEntry read(RandomAccessFile raf) throws IOException {
- return new PairEntry(dictionary, raf);
+ public PairEntry read(RandomAccessFile raf, int index) throws IOException {
+ return new PairEntry(dictionary, raf, index);
}
@Override
};
@Override
- public int addToDictionary(final Dictionary dictionary) {
+ public void addToDictionary(final Dictionary dictionary) {
+ assert index == -1;
dictionary.pairEntries.add(this);
- return dictionary.pairEntries.size() - 1;
+ index = dictionary.pairEntries.size() - 1;
}
@Override
- public RowBase CreateRow(int entryIndex, int rowIndex, Index dictionaryIndex) {
- return new Row(entryIndex, rowIndex, dictionaryIndex);
+ public RowBase CreateRow(int rowIndex, Index dictionaryIndex) {
+ return new Row(this.index, rowIndex, dictionaryIndex);
}
if (rowType == 0) {
return new PairEntry.Row(raf, listIndex, index);
} else if (rowType == 1 || rowType == 3) {
- return new TokenRow(raf, listIndex, index, rowType == 1);
+ return new TokenRow(raf, listIndex, index, /* hasMainEntry */ rowType == 1);
} else if (rowType == 2) {
return new TextEntry.Row(raf, listIndex, index);
} else if (rowType == 4) {
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.HtmlEntry.Row;
-import com.hughes.util.raf.RAFSerializable;
-import com.hughes.util.raf.RAFSerializer;
-import com.ibm.icu.text.Transliterator;
-
public class TextEntry extends AbstractEntry implements RAFSerializable<TextEntry> {
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();
}
raf.writeUTF(text);
}
- static final class Serializer implements RAFSerializer<TextEntry> {
+ static final class Serializer implements RAFListSerializer<TextEntry> {
final Dictionary dictionary;
}
@Override
- public TextEntry read(RandomAccessFile raf) throws IOException {
- return new TextEntry(dictionary, raf);
+ public TextEntry read(RandomAccessFile raf, final int index) throws IOException {
+ return new TextEntry(dictionary, raf, index);
}
@Override
@Override
- public int addToDictionary(final Dictionary dictionary) {
+ public void addToDictionary(final Dictionary dictionary) {
+ assert index == -1;
dictionary.textEntries.add(this);
- return dictionary.textEntries.size() - 1;
+ index = dictionary.textEntries.size() - 1;
}
@Override
- public RowBase CreateRow(int entryIndex, int rowIndex, Index dictionaryIndex) {
+ public RowBase CreateRow(int rowIndex, Index dictionaryIndex) {
throw new UnsupportedOperationException("TextEntry's don't really exist.");
}
public void print(final PrintStream out) {
final String surrounder = hasMainEntry ? "***" : "===";
out.println(surrounder + getToken() + surrounder);
+ for (final HtmlEntry htmlEntry : index.sortedIndexEntries.get(referenceIndex).htmlEntries) {
+ out.println("HtmlEntry: " + htmlEntry.title + " <<<" + htmlEntry.html + ">>>");
+ }
}
@Override