package journal.reader; import java.text.ParseException; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.ArrayList; import java.util.Map; import journal.schema.Attribute; import journal.schema.Index; import journal.schema.Schema; import journal.schema.Table; import journal.schema.TableVersion; // Suppress unchecked because I cannot figure out a way to make Comparable<> work // the way I want it to work. @SuppressWarnings("unchecked") public class DataJournalEntry extends JournalEntry implements Comparable<DataJournalEntry> { private int version; private String tableName; private TableVersion tableVersion; public class Argument { Token token; Attribute attribute; Comparable<Object> value; Argument(final Token token) { this.token = token; attribute = tableVersion.getAttribute(arguments.size()); value = (Comparable<Object>) attribute.getDomain().parse(token); } public boolean equals(Object to) { Argument toArg = (Argument) to; return (toArg.attribute.equals(attribute) && toArg.value.equals(value)); } public String toString() { return attribute.getDomain().toJournalFormat(value); } public Attribute getAttribute() { return attribute; } public Comparable<Object> getValue() { return value; } } private List<Argument> arguments = new ArrayList<Argument>(); private Map<String, Argument> argumentsByName = new HashMap<String, Argument>(); public DataJournalEntry(Token startToken, Token versionToken, Token tableToken) throws ParseException { super(startToken); version = Integer.parseInt(versionToken.getValue()); tableName = tableToken.getValue(); try { tableVersion = Schema.GetTableVersion(tableName, version); if (tableVersion == null) { throw new ParseException("Unknown table " + tableName + "[" + version + "]", tableToken.getLine()); } } catch (IndexOutOfBoundsException e) { throw new ParseException("Unknown version " + tableName + "[" + version + "]", tableToken.getLine()); } } public void addArgument(Token token) { Argument argument = new Argument(token); arguments.add(argument); argumentsByName.put(argument.attribute.getName(), argument); } public Object getValue(String attributeName) { Argument argument = argumentsByName.get(attributeName); if (argument != null) { return argument.getValue(); } return null; } public List<Object> getValues() { List<Object> result = new ArrayList<Object>(); for (Argument arg : arguments) { result.add(arg.getValue()); } return result; } public List<Argument> getArguments() { return arguments; } public boolean equals(Object to) { if (! to.getClass().equals(getClass())) { return false; } DataJournalEntry toEntry = (DataJournalEntry) to; if (toEntry.tableVersion == tableVersion) { Iterator<Argument> toArgIter = toEntry.arguments.iterator(); Iterator<Argument> argIter = arguments.iterator(); while (toArgIter.hasNext() && argIter.hasNext()) { if (! toArgIter.next().equals(argIter.next())) { return false; } } // both toArgIter.hasNext() and argIter.hasNext() should return false. If one does not // then that iterator is not finished and we have a mismatch return (toArgIter.hasNext() == argIter.hasNext()); } return false; } public String toString() { StringBuffer buf = new StringBuffer(actionType.toString()); buf.append(" "); buf.append(tableName); buf.append("["); buf.append(version); buf.append("] = "); for (Argument arg : arguments) { buf.append(arg.attribute); buf.append(":"); buf.append(arg.value); buf.append(" "); } return buf.toString(); } public int compareTo(DataJournalEntry o) { int tableDiff = Schema.GetTableIndex(tableName) - Schema.GetTableIndex(o.tableName); if (tableDiff == 0) { Table table = tableVersion.getTable(); List<Index> indeces = table.getIndeces(); for (Index index : indeces) { Argument myArg = argumentsByName.get(index.getName()); Argument otherArg = o.argumentsByName.get(index.getName()); int result = myArg.value.compareTo(otherArg.value); if (result != 0) { if (index.isAscending()) return result; else return -result; } } return 0; } else { return tableDiff; } } @Override public String toJournalString() { StringBuffer buf = new StringBuffer(super.toJournalString()); buf.append(" "); buf.append(tableVersion.toJournalString()); for (Argument a : arguments) { buf.append(" "); buf.append(a.toString()); } buf.append(" "); // that is the Perforce way - Chris knows why return buf.toString(); } public int getVersion() { return version; } public String getTableName() { return tableName; } public TableVersion getTableVersion() { return tableVersion; } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#3 | 7529 | Sven Erik Knop |
Deleted the whole JournalReader. It has actually moved to //guest/sven_erik_knop/java/JournalReader/. |
||
#2 | 7375 | Sven Erik Knop |
Major update of the JournalReader. Complete rewrite of the command line parsing Change in the options parsing within the journal reader New SQLLoader action. Currently only against MySQL (needs MySQL JDBC driver) with fixed database and user name. This will be replaced by a config file at some stage. |
||
#1 | 7374 | Sven Erik Knop | Rename/move file(s) - correct location for Eclipse project | ||
//guest/sven_erik_knop/JournalReader/journal/reader/DataJournalEntry.java | |||||
#1 | 6467 | Sven Erik Knop |
Added JournalReader, a Java library of useful tools to read and process checkpoints and journals. Added are a readme.txt to explain some details, and a jar file that contains the compiled class files. The programs will need Java 1.6 to run. |