]> gitweb.fperrin.net Git - Dictionary.git/commitdiff
First decent implementation of HtmlEntry attached to TokenRow.
authorthadh <thadh@localhost>
Mon, 10 Sep 2012 03:40:06 +0000 (20:40 -0700)
committerthadh <thadh@localhost>
Mon, 10 Sep 2012 03:40:06 +0000 (20:40 -0700)
src/com/hughes/android/dictionary/engine/AbstractEntry.java
src/com/hughes/android/dictionary/engine/Dictionary.java
src/com/hughes/android/dictionary/engine/HtmlEntry.java
src/com/hughes/android/dictionary/engine/Index.java
src/com/hughes/android/dictionary/engine/PairEntry.java
src/com/hughes/android/dictionary/engine/RowBase.java
src/com/hughes/android/dictionary/engine/TextEntry.java
src/com/hughes/android/dictionary/engine/TokenRow.java

index 7bae09b342f5a572a85096f77463e03df307f533..de221eafc0eae60fa64990ddeabdfc6b0314eb45 100644 (file)
 
 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);
 
 }
index c8e63d855e93a9f399ccef2c3cdfb42fb5e1dca6..9224f51deb87cf52bd7063100ac80dd7860e3446 100644 (file)
@@ -33,7 +33,7 @@ public class Dictionary implements RAFSerializable<Dictionary> {
   
   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<Dictionary> {
       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) {
index fe3fd1d34bcfb21564215abcc3e073556a7a596c..404bc6a28f21713dbd8a2ddf7031d3ff28805fca 100644 (file)
@@ -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<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();
   }
@@ -36,18 +33,19 @@ public class HtmlEntry extends AbstractEntry implements RAFSerializable<HtmlEntr
   }
 
   @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;
     
@@ -56,8 +54,8 @@ public class HtmlEntry extends AbstractEntry implements RAFSerializable<HtmlEntr
     }
 
     @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
index db0544081cdd67f288b5394bfa3597d1ae0e54b6..4f7073541055a6a4138aa98e878d95510f4a5c07 100644 (file)
  */
 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;
@@ -33,24 +46,11 @@ import java.util.Set;
 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;
@@ -124,7 +124,7 @@ public final class Index implements RAFSerializable<Index> {
     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 {
@@ -143,7 +143,7 @@ public final class Index implements RAFSerializable<Index> {
     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 */);
   }
@@ -154,38 +154,49 @@ public final class Index implements RAFSerializable<Index> {
     }
   }
   
-  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 {
@@ -197,6 +208,7 @@ public final class Index implements RAFSerializable<Index> {
       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<Index> {
         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<Index> {
       result.addAll(ordered);
     }
     
-    System.out.println("searchDuration: " + (System.currentTimeMillis() - startMills));
+    //System.out.println("searchDuration: " + (System.currentTimeMillis() - startMills));
     return result;
   }
   
index c48298af2283d3ad8230fc9727c4e13af774bbf3..3453dc357f12e329f5536d023d860612c90fc31f 100644 (file)
 
 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<PairEntry>, Comparable<PairEntry> {
   
   public final List<Pair> pairs;
@@ -40,8 +39,8 @@ public class PairEntry extends AbstractEntry implements RAFSerializable<PairEntr
     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) {
@@ -60,7 +59,7 @@ public class PairEntry extends AbstractEntry implements RAFSerializable<PairEntr
     }
   }
   
-  static final class Serializer implements RAFSerializer<PairEntry> {
+  static final class Serializer implements RAFListSerializer<PairEntry> {
     
     final Dictionary dictionary;
     
@@ -69,8 +68,8 @@ public class PairEntry extends AbstractEntry implements RAFSerializable<PairEntr
     }
 
     @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
@@ -80,14 +79,15 @@ public class PairEntry extends AbstractEntry implements RAFSerializable<PairEntr
   };
   
   @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);
   }
 
 
index 907bb3a83bd62ea7a65dcc63f54f8f06857db63f..c83c7cdbd5022d7c59895d98b85164258a67a1bc 100644 (file)
@@ -148,7 +148,7 @@ public abstract class RowBase extends IndexedObject {
       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) {
index d08b450ed92a100ec518f11b0c0a6c3e0a328aea..a003ca91c8c143ffcfb90d876ff2a0c5b8c288c1 100644 (file)
 
 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();
   }
@@ -40,7 +39,7 @@ public class TextEntry extends AbstractEntry implements RAFSerializable<TextEntr
     raf.writeUTF(text);
   }
   
-  static final class Serializer implements RAFSerializer<TextEntry> {
+  static final class Serializer implements RAFListSerializer<TextEntry> {
     
     final Dictionary dictionary;
     
@@ -49,8 +48,8 @@ public class TextEntry extends AbstractEntry implements RAFSerializable<TextEntr
     }
 
     @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
@@ -61,13 +60,14 @@ public class TextEntry extends AbstractEntry implements RAFSerializable<TextEntr
 
   
   @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.");
   }
 
index 29a23bcacfc6b51ef9fa689e0dbf4671b1babe79..956044afe3a715f1dd6b6e1e80417399d6749c7e 100644 (file)
@@ -58,6 +58,9 @@ public class TokenRow extends RowBase {
   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