Cache parsed database

cusax-fix
Ingo Bauersachs 13 years ago
parent 5192902101
commit 471d31bb3e

@ -50,77 +50,87 @@ public ThunderbirdContactQuery(ThunderbirdContactSourceService owner,
@Override
protected void run()
{
String file = super.getContactSource().getFilename();
String filename = super.getContactSource().getFilename();
File file = new File(filename);
try
{
// parse the Thunderbird Mork database
InputStreamReader sr =
new InputStreamReader(new FileInputStream(file));
MorkDocument md = new MorkDocument(sr);
sr.close();
// We now have rows in their tables and additional rows at
// transaction level. Put the to a better format:
// DB -> Tables -> Rows
Map<String, Map<String, Row>> db =
new HashMap<String, Map<String, Row>>();
for (Table t : md.getTables())
if (file.lastModified() > getContactSource().lastDatabaseFileChange)
{
String tableId = t.getTableId() + "/" + t.getScopeName();
Map<String, Row> table = db.get(tableId);
if (table == null)
// parse the Thunderbird Mork database
InputStreamReader sr =
new InputStreamReader(new FileInputStream(filename));
MorkDocument md = new MorkDocument(sr);
sr.close();
// We now have rows in their tables and additional rows at
// transaction level. Put the to a better format:
// DB -> Tables -> Rows
Map<String, Map<String, Row>> db =
new HashMap<String, Map<String, Row>>();
for (Table t : md.getTables())
{
table = new HashMap<String, Row>();
db.put(tableId, table);
String tableId = t.getTableId() + "/" + t.getScopeName();
Map<String, Row> table = db.get(tableId);
if (table == null)
{
table = new HashMap<String, Row>();
db.put(tableId, table);
}
for (Row r : t.getRows())
{
String scope = r.getScopeName();
if (scope == null)
{
scope = t.getScopeName();
}
table.put(r.getRowId() + "/" + scope, r);
}
}
for (Row r : t.getRows())
// The additional rows at the root-level update/replace the ones
// in the tables. There's usually neither a table nor a scope
// defined, so lets just use the default.
String defaultScope = md.getDicts().get(0).dereference("^80");
for (Row r : md.getRows())
{
String scope = r.getScopeName();
if (scope == null)
{
scope = t.getScopeName();
scope = defaultScope;
}
table.put(r.getRowId() + "/" + scope, r);
}
}
// The additional rows at the root-level update/replace the ones in
// the tables. There's usually neither a table nor a scope defined,
// so lets just use the default.
String defaultScope = md.getDicts().get(0).dereference("^80");
for (Row r : md.getRows())
{
String scope = r.getScopeName();
if (scope == null)
{
scope = defaultScope;
}
String tableId = "1/" + scope;
Map<String, Row> table = db.get(tableId);
if (table == null)
{
table = new HashMap<String, Row>();
db.put(tableId, table);
}
String rowId = r.getRowId() + "/" + scope;
if (rowId.startsWith("-"))
{
rowId = rowId.substring(1);
String tableId = "1/" + scope;
Map<String, Row> table = db.get(tableId);
if (table == null)
{
table = new HashMap<String, Row>();
db.put(tableId, table);
}
String rowId = r.getRowId() + "/" + scope;
if (rowId.startsWith("-"))
{
rowId = rowId.substring(1);
}
table.put(rowId, r);
}
table.put(rowId, r);
super.getContactSource().database = db;
super.getContactSource().defaultScope = defaultScope;
super.getContactSource().lastDatabaseFileChange =
file.lastModified();
}
// okay, "transactions" are applied, now perform the search
for (Entry<String, Map<String, Row>> table : db.entrySet())
for (Entry<String, Map<String, Row>> table
: super.getContactSource().database.entrySet())
{
for (Map.Entry<String, Row> e : table.getValue().entrySet())
{
if (e.getKey().endsWith(defaultScope))
if (e.getKey().endsWith(getContactSource().defaultScope))
{
readEntry(e.getValue());
}

@ -6,10 +6,12 @@
*/
package net.java.sip.communicator.plugin.thunderbird;
import java.util.Map;
import java.util.regex.*;
import org.jitsi.service.configuration.*;
import mork.Row;
import net.java.sip.communicator.service.contactsource.*;
/**
@ -72,6 +74,19 @@ public class ThunderbirdContactSourceService
/** Value of property {@link #PNAME_PREFIX} */
private String prefix;
/** Date/time when the Thunderbird database file was last changed. */
long lastDatabaseFileChange = 0;
/**
* The parsed Thunderbird database. This field is used as a cache and set by
* the query. It is re-set when the file date is newer than
* {@link #lastDatabaseFileChange}
*/
Map<String, Map<String, Row>> database;
/** Name of the default namespace in the Thunderbird database. */
String defaultScope;
/**
* Creates a new instance of this class.
*

Loading…
Cancel
Save