Changeset 848:d8b3aadce143


Ignore:
Timestamp:
02/04/12 16:55:30 (4 months ago)
Author:
xeraph
Branch:
default
Message:
  • removed main bottleneck of manifest-read (slow down caused by many array manipulation)
  • added manual manifestId parsing from change log binary for speed-up
  • removed writer.sync() per config add
  • getManifest() fast-path for ensureCollection() speed-up (skip config fetch)
  • manual manifestId parsing from changeset log for ensureCollection() speed-up
Location:
kraken-confdb/src/main/java/org/krakenapps/confdb
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • kraken-confdb/src/main/java/org/krakenapps/confdb/ConfigEntry.java

    r433 r848  
    3636        } 
    3737 
    38         @Override 
    39         public int hashCode() { 
    40                 final int prime = 31; 
    41                 int result = 1; 
    42                 result = prime * result + colId; 
    43                 result = prime * result + docId; 
    44                 return result; 
    45         } 
    46  
    47         /** 
    48          * the key is composition of collection id and doc id 
    49          */ 
    50         @Override 
    51         public boolean equals(Object obj) { 
    52                 if (this == obj) 
    53                         return true; 
    54                 if (obj == null) 
    55                         return false; 
    56                 if (getClass() != obj.getClass()) 
    57                         return false; 
    58                 ConfigEntry other = (ConfigEntry) obj; 
    59                 if (colId != other.colId) 
    60                         return false; 
    61                 if (docId != other.docId) 
    62                         return false; 
    63                 return true; 
    64         } 
    65  
    6638        public int getColId() { 
    6739                return colId; 
     
    9264                return "col=" + colId + ", doc=" + docId + ", rev=" + rev; 
    9365        } 
    94  
    9566} 
  • kraken-confdb/src/main/java/org/krakenapps/confdb/file/ChangeLog.java

    r829 r848  
    147147                } 
    148148                return m; 
     149        } 
     150 
     151        /** 
     152         * get manifest id from change log binary. manual parsing for speed-up 
     153         */ 
     154        public static int getManifest(byte[] b) { 
     155                ByteBuffer bb = ByteBuffer.wrap(b); 
     156                bb.get(); // type (9) 
     157                EncodingRule.decodeRawNumber(bb); // skip map length part 
     158 
     159                // enumerate keys of map 
     160                while (true) { 
     161                        // parse map key 
     162                        String s = EncodingRule.decodeString(bb); 
     163                        if (s.equals("manifest_id")) { 
     164                                return EncodingRule.decodeInt(bb); 
     165                        } else { 
     166                                // parse map value 
     167                                bb.get(); 
     168                                long l = EncodingRule.decodeRawNumber(bb); 
     169                                bb.position((int) (bb.position() + l)); 
     170                        } 
     171                } 
    149172        } 
    150173 
  • kraken-confdb/src/main/java/org/krakenapps/confdb/file/FileConfigCollection.java

    r845 r848  
    136136                Manifest manifest = db.getManifest(changeset); 
    137137                List<RevLog> snapshot = new ArrayList<RevLog>(); 
    138  
    139138                long count = reader.count(); 
    140139                for (long index = 0; index < count; index++) { 
     
    145144                                snapshot.add(log); 
    146145                } 
    147  
    148146                return snapshot; 
    149147        } 
     
    177175                        // write collection log 
    178176                        int docId = writer.write(revlog); 
    179                         writer.sync(); 
    180177 
    181178                        // write db changelog 
  • kraken-confdb/src/main/java/org/krakenapps/confdb/file/FileConfigDatabase.java

    r834 r848  
    225225        public ConfigCollection getCollection(String name) { 
    226226                try { 
    227                         Manifest manifest = getManifest(changeset); 
     227                        Manifest manifest = getManifest(changeset, true); 
    228228                        CollectionEntry col = manifest.getCollectionEntry(name); 
    229229                        if (col == null) 
     
    260260                try { 
    261261                        Manifest manifest = null; 
    262                         if (xact == null) 
    263                                 manifest = getManifest(changeset); 
    264                         else 
     262                        if (xact == null) { 
     263                                manifest = getManifest(changeset, true); 
     264                        } else 
    265265                                manifest = xact.getManifest(); 
    266  
    267266                        CollectionEntry col = manifest.getCollectionEntry(name); 
    268267 
     
    331330        @Override 
    332331        public Manifest getManifest(Integer rev) { 
     332                return getManifest(rev, false); 
     333        } 
     334 
     335        public Manifest getManifest(Integer rev, boolean noConfigs) { 
    333336                // read last changelog and get manifest doc id 
    334337                int manifestId = 0; 
     
    345348                                revlog = reader.findDoc(rev); 
    346349                        } 
    347  
    348350                        byte[] doc = reader.readDoc(revlog.getDocOffset(), revlog.getDocLength()); 
    349                         ChangeLog change = ChangeLog.deserialize(doc); 
    350                         manifestId = change.getManifestId(); 
     351                        manifestId = ChangeLog.getManifest(doc); 
    351352                } catch (FileNotFoundException e) { 
    352353                        // changeset can be empty 
     
    366367                        RevLog revlog = reader.findDoc(manifestId); 
    367368                        byte[] doc = reader.readDoc(revlog.getDocOffset(), revlog.getDocLength()); 
    368  
    369369                        // manifest id should be set here (id = revlog id) 
    370                         FileManifest manifest = FileManifest.deserialize(doc); 
     370                        FileManifest manifest = FileManifest.deserialize(doc, noConfigs); 
    371371                        manifest.setId(manifestId); 
    372  
    373372                        return manifest; 
    374373                } catch (FileNotFoundException e) { 
  • kraken-confdb/src/main/java/org/krakenapps/confdb/file/FileConfigIterator.java

    r840 r848  
    118118                byte[] b = reader.readDoc(log.getDocOffset(), log.getDocLength()); 
    119119                Object doc = EncodingRule.decode(ByteBuffer.wrap(b)); 
    120  
    121120                return new FileConfig(db, col, log.getDocId(), log.getRev(), log.getPrevRev(), doc, parser); 
    122121        } 
  • kraken-confdb/src/main/java/org/krakenapps/confdb/file/FileManifest.java

    r843 r848  
    2323import java.util.Map; 
    2424import java.util.Set; 
     25import java.util.TreeMap; 
    2526import java.util.TreeSet; 
    2627 
    27 import org.krakenapps.api.CollectionTypeHint; 
    2828import org.krakenapps.api.FieldOption; 
    2929import org.krakenapps.codec.EncodingRule; 
     
    3535        private int id; 
    3636 
    37         @CollectionTypeHint(CollectionEntry.class) 
     37        @FieldOption(skip = true) 
    3838        private List<CollectionEntry> cols = new ArrayList<CollectionEntry>(); 
    3939 
    40         @CollectionTypeHint(ConfigEntry.class) 
    41         private List<ConfigEntry> configs = new ArrayList<ConfigEntry>(); 
    42  
    43         /** 
    44          * just for existence test. we cannot change configs type (need ordering) 
    45          */ 
    4640        @FieldOption(skip = true) 
    47         private Map<ConfigMatchKey, Long> tester = new HashMap<ConfigMatchKey, Long>(); 
     41        private Map<ConfigMatchKey, ConfigEntry> configs = new TreeMap<ConfigMatchKey, ConfigEntry>(); 
    4842 
    4943        public FileManifest() { 
     
    7367        @Override 
    7468        public void add(ConfigEntry e) { 
    75                 configs.remove(e); // remove duplicates 
    76                 configs.add(e); 
    77                 tester.put(new ConfigMatchKey(e), e.getRev()); 
     69                configs.put(new ConfigMatchKey(e), e); 
    7870        } 
    7971 
    8072        @Override 
    8173        public void remove(ConfigEntry e) { 
    82                 configs.remove(e); 
    83                 tester.remove(new ConfigMatchKey(e)); 
     74                configs.remove(new ConfigMatchKey(e)); 
    8475        } 
    8576 
     
    10798 
    10899                List<ConfigEntry> entries = new LinkedList<ConfigEntry>(); 
    109                 for (ConfigEntry e : configs) 
     100                for (ConfigEntry e : configs.values()) 
    110101                        if (e.getColId() == colId) 
    111102                                entries.add(e); 
     
    125116                CollectionEntry col = getCollectionEntry(colName); 
    126117                ConfigMatchKey key = new ConfigMatchKey(col.getId(), docId); 
    127                 Long r = tester.get(key); 
    128                 return r != null && r == rev; 
     118                ConfigEntry e = configs.get(key); 
     119                return e != null && e.getRev() == rev; 
    129120        } 
    130121 
     
    149140        private List<Object> serializeConfigs() { 
    150141                List<Object> l = new ArrayList<Object>(configs.size()); 
    151                 for (ConfigEntry e : configs) 
     142                for (ConfigEntry e : configs.values()) 
    152143                        l.add(new Object[] { e.getColId(), e.getDocId(), e.getRev() }); 
    153144                return l; 
     
    155146 
    156147        public static FileManifest deserialize(byte[] b) { 
     148                return deserialize(b, false); 
     149        } 
     150 
     151        public static FileManifest deserialize(byte[] b, boolean noConfigs) { 
    157152                ByteBuffer bb = ByteBuffer.wrap(b); 
    158153                Map<String, Object> m = EncodingRule.decodeMap(bb); 
     
    172167                } 
    173168 
     169                // for ensureCollection() acceleration 
     170                if (noConfigs) 
     171                        return manifest; 
     172 
    174173                for (Object o : (Object[]) m.get("configs")) { 
    175174                        if (o instanceof Map) { 
     
    195194        } 
    196195 
    197         private static class ConfigMatchKey { 
     196        private static class ConfigMatchKey implements Comparable<ConfigMatchKey> { 
    198197                private int colId; 
    199198                private int docId; 
     
    206205                        this.colId = colId; 
    207206                        this.docId = docId; 
     207                } 
     208 
     209                @Override 
     210                public int compareTo(ConfigMatchKey o) { 
     211                        if (o == null) 
     212                                return -1; 
     213 
     214                        int value = colId - o.colId; 
     215                        if (value != 0) 
     216                                return value; 
     217 
     218                        value = docId - o.docId; 
     219                        if (value != 0) 
     220                                return value; 
     221 
     222                        return 0; 
    208223                } 
    209224 
Note: See TracChangeset for help on using the changeset viewer.