Changeset 848:d8b3aadce143
- Timestamp:
- 02/04/12 16:55:30 (4 months ago)
- Branch:
- default
- Location:
- kraken-confdb/src/main/java/org/krakenapps/confdb
- Files:
-
- 6 edited
-
ConfigEntry.java (modified) (2 diffs)
-
file/ChangeLog.java (modified) (1 diff)
-
file/FileConfigCollection.java (modified) (3 diffs)
-
file/FileConfigDatabase.java (modified) (5 diffs)
-
file/FileConfigIterator.java (modified) (1 diff)
-
file/FileManifest.java (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
kraken-confdb/src/main/java/org/krakenapps/confdb/ConfigEntry.java
r433 r848 36 36 } 37 37 38 @Override39 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 id49 */50 @Override51 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 66 38 public int getColId() { 67 39 return colId; … … 92 64 return "col=" + colId + ", doc=" + docId + ", rev=" + rev; 93 65 } 94 95 66 } -
kraken-confdb/src/main/java/org/krakenapps/confdb/file/ChangeLog.java
r829 r848 147 147 } 148 148 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 } 149 172 } 150 173 -
kraken-confdb/src/main/java/org/krakenapps/confdb/file/FileConfigCollection.java
r845 r848 136 136 Manifest manifest = db.getManifest(changeset); 137 137 List<RevLog> snapshot = new ArrayList<RevLog>(); 138 139 138 long count = reader.count(); 140 139 for (long index = 0; index < count; index++) { … … 145 144 snapshot.add(log); 146 145 } 147 148 146 return snapshot; 149 147 } … … 177 175 // write collection log 178 176 int docId = writer.write(revlog); 179 writer.sync();180 177 181 178 // write db changelog -
kraken-confdb/src/main/java/org/krakenapps/confdb/file/FileConfigDatabase.java
r834 r848 225 225 public ConfigCollection getCollection(String name) { 226 226 try { 227 Manifest manifest = getManifest(changeset );227 Manifest manifest = getManifest(changeset, true); 228 228 CollectionEntry col = manifest.getCollectionEntry(name); 229 229 if (col == null) … … 260 260 try { 261 261 Manifest manifest = null; 262 if (xact == null) 263 manifest = getManifest(changeset );264 else262 if (xact == null) { 263 manifest = getManifest(changeset, true); 264 } else 265 265 manifest = xact.getManifest(); 266 267 266 CollectionEntry col = manifest.getCollectionEntry(name); 268 267 … … 331 330 @Override 332 331 public Manifest getManifest(Integer rev) { 332 return getManifest(rev, false); 333 } 334 335 public Manifest getManifest(Integer rev, boolean noConfigs) { 333 336 // read last changelog and get manifest doc id 334 337 int manifestId = 0; … … 345 348 revlog = reader.findDoc(rev); 346 349 } 347 348 350 byte[] doc = reader.readDoc(revlog.getDocOffset(), revlog.getDocLength()); 349 ChangeLog change = ChangeLog.deserialize(doc); 350 manifestId = change.getManifestId(); 351 manifestId = ChangeLog.getManifest(doc); 351 352 } catch (FileNotFoundException e) { 352 353 // changeset can be empty … … 366 367 RevLog revlog = reader.findDoc(manifestId); 367 368 byte[] doc = reader.readDoc(revlog.getDocOffset(), revlog.getDocLength()); 368 369 369 // manifest id should be set here (id = revlog id) 370 FileManifest manifest = FileManifest.deserialize(doc );370 FileManifest manifest = FileManifest.deserialize(doc, noConfigs); 371 371 manifest.setId(manifestId); 372 373 372 return manifest; 374 373 } catch (FileNotFoundException e) { -
kraken-confdb/src/main/java/org/krakenapps/confdb/file/FileConfigIterator.java
r840 r848 118 118 byte[] b = reader.readDoc(log.getDocOffset(), log.getDocLength()); 119 119 Object doc = EncodingRule.decode(ByteBuffer.wrap(b)); 120 121 120 return new FileConfig(db, col, log.getDocId(), log.getRev(), log.getPrevRev(), doc, parser); 122 121 } -
kraken-confdb/src/main/java/org/krakenapps/confdb/file/FileManifest.java
r843 r848 23 23 import java.util.Map; 24 24 import java.util.Set; 25 import java.util.TreeMap; 25 26 import java.util.TreeSet; 26 27 27 import org.krakenapps.api.CollectionTypeHint;28 28 import org.krakenapps.api.FieldOption; 29 29 import org.krakenapps.codec.EncodingRule; … … 35 35 private int id; 36 36 37 @ CollectionTypeHint(CollectionEntry.class)37 @FieldOption(skip = true) 38 38 private List<CollectionEntry> cols = new ArrayList<CollectionEntry>(); 39 39 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 */46 40 @FieldOption(skip = true) 47 private Map<ConfigMatchKey, Long> tester = new HashMap<ConfigMatchKey, Long>();41 private Map<ConfigMatchKey, ConfigEntry> configs = new TreeMap<ConfigMatchKey, ConfigEntry>(); 48 42 49 43 public FileManifest() { … … 73 67 @Override 74 68 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); 78 70 } 79 71 80 72 @Override 81 73 public void remove(ConfigEntry e) { 82 configs.remove(e); 83 tester.remove(new ConfigMatchKey(e)); 74 configs.remove(new ConfigMatchKey(e)); 84 75 } 85 76 … … 107 98 108 99 List<ConfigEntry> entries = new LinkedList<ConfigEntry>(); 109 for (ConfigEntry e : configs )100 for (ConfigEntry e : configs.values()) 110 101 if (e.getColId() == colId) 111 102 entries.add(e); … … 125 116 CollectionEntry col = getCollectionEntry(colName); 126 117 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; 129 120 } 130 121 … … 149 140 private List<Object> serializeConfigs() { 150 141 List<Object> l = new ArrayList<Object>(configs.size()); 151 for (ConfigEntry e : configs )142 for (ConfigEntry e : configs.values()) 152 143 l.add(new Object[] { e.getColId(), e.getDocId(), e.getRev() }); 153 144 return l; … … 155 146 156 147 public static FileManifest deserialize(byte[] b) { 148 return deserialize(b, false); 149 } 150 151 public static FileManifest deserialize(byte[] b, boolean noConfigs) { 157 152 ByteBuffer bb = ByteBuffer.wrap(b); 158 153 Map<String, Object> m = EncodingRule.decodeMap(bb); … … 172 167 } 173 168 169 // for ensureCollection() acceleration 170 if (noConfigs) 171 return manifest; 172 174 173 for (Object o : (Object[]) m.get("configs")) { 175 174 if (o instanceof Map) { … … 195 194 } 196 195 197 private static class ConfigMatchKey {196 private static class ConfigMatchKey implements Comparable<ConfigMatchKey> { 198 197 private int colId; 199 198 private int docId; … … 206 205 this.colId = colId; 207 206 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; 208 223 } 209 224
Note: See TracChangeset
for help on using the changeset viewer.
