r60235 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r60234‎ | r60235 | r60236 >
Date:22:50, 19 December 2009
Author:daniel
Status:deferred
Tags:
Comment:
proximity retrieval - work in progress
Modified paths:
  • /trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/schema/ProximityStoreSchema.java (modified) (history)
  • /trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/store/DatabaseProximityStore.java (added) (history)
  • /trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/store/LocalConceptStore.java (modified) (history)
  • /trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/store/ProximityStore.java (added) (history)
  • /trunk/WikiWord/WikiWordBuilder/src/main/java/de/brightbyte/wikiword/store/builder/DatabaseGlobalConceptStoreBuilder.java (modified) (history)
  • /trunk/WikiWord/WikiWordBuilder/src/main/java/de/brightbyte/wikiword/store/builder/DatabaseLocalConceptStoreBuilder.java (modified) (history)
  • /trunk/WikiWord/WikiWordBuilder/src/main/java/de/brightbyte/wikiword/store/builder/DatabaseProximityStoreBuilder.java (modified) (history)

Diff [purge]

Index: trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/store/DatabaseProximityStore.java
@@ -0,0 +1,100 @@
 2+package de.brightbyte.wikiword.store;
 3+
 4+import java.sql.ResultSet;
 5+import java.sql.SQLException;
 6+
 7+import de.brightbyte.data.LabeledVector;
 8+import de.brightbyte.data.MapLabeledVector;
 9+import de.brightbyte.data.cursor.DataSet;
 10+import de.brightbyte.db.EntityTable;
 11+import de.brightbyte.db.QueryDataSet;
 12+import de.brightbyte.db.RelationTable;
 13+import de.brightbyte.util.PersistenceException;
 14+import de.brightbyte.wikiword.TweakSet;
 15+import de.brightbyte.wikiword.model.LocalConcept;
 16+import de.brightbyte.wikiword.model.WikiWordConcept;
 17+import de.brightbyte.wikiword.model.WikiWordConceptReference;
 18+import de.brightbyte.wikiword.schema.ProximityStoreSchema;
 19+import de.brightbyte.wikiword.store.DatabaseWikiWordConceptStore.DatabaseConceptInfoStore.ConceptFactory;
 20+
 21+public class DatabaseProximityStore<T extends WikiWordConcept, R extends WikiWordConceptReference<T>>
 22+ extends DatabaseWikiWordStore
 23+ implements ProximityStore<T, R> {
 24+
 25+ private DatabaseWikiWordConceptStore conceptStore;
 26+ private RelationTable featureTable;
 27+ private RelationTable proximityTable;
 28+ private EntityTable conceptTable;
 29+
 30+ protected DatabaseProximityStore(DatabaseWikiWordConceptStore conceptStore, ProximityStoreSchema database, TweakSet tweaks) throws SQLException {
 31+ super(database, tweaks);
 32+
 33+ this.conceptStore = conceptStore;
 34+
 35+ this.conceptTable = (EntityTable)conceptStore.getDatabaseAccess().getTable("concept");
 36+ this.featureTable = (RelationTable)database.getTable("feature");
 37+ this.proximityTable = (RelationTable)database.getTable("proximity");
 38+ }
 39+
 40+ public DataSet<? extends R> getEnvironment(int concept, double minProximity) throws PersistenceException {
 41+ String sql = "SELECT C.*, proximity FROM " + conceptTable.getSQLName() + " as C ";
 42+ sql += " JOIN "+proximityTable.getSQLName()+" as P ON P.concept2 = C.id ";
 43+ sql += " WHERE concept1 = "+concept;
 44+
 45+ return new QueryDataSet<R>(database, referenceFactory, "getEnvironment", sql, false);
 46+ }
 47+
 48+ public LabeledVector<Integer> getEnvironmentVector(int concept, double minProximity) throws PersistenceException {
 49+ String sql = "SELECT concept2, proximity FROM " +proximityTable.getSQLName()+" as P ";
 50+ sql += " WHERE concept1 = "+concept;
 51+
 52+ return readVector("getEnvironmentVector", sql, "concept2", "proximity");
 53+ }
 54+
 55+ public LabeledVector<Integer> getFeatureVector(int concept) throws PersistenceException {
 56+ String sql = "SELECT feature, normal_value FROM " +featureTable.getSQLName()+" as F ";
 57+ sql += " WHERE concept = "+concept;
 58+
 59+ return readVector("getFeatireVector", sql, "feature", "normal_value");
 60+ }
 61+
 62+ protected <K> LabeledVector<K> readVector(String name, String sql, String keyField, String valueField) throws PersistenceException {
 63+ try {
 64+ ResultSet rs = database.executeQuery(name, sql);
 65+ try {
 66+ return readVector(rs, keyField, valueField, new MapLabeledVector<K>());
 67+ } finally {
 68+ rs.close();
 69+ }
 70+ } catch (SQLException e) {
 71+ throw new PersistenceException(e);
 72+ }
 73+ }
 74+
 75+ protected <K> LabeledVector<K> readVector(ResultSet rs, String keyField, String valueField, LabeledVector<K> v) throws SQLException {
 76+ if (v==null) v = new MapLabeledVector<K>();
 77+
 78+ while (rs.next()) {
 79+ K k = (K)rs.getObject(keyField);
 80+ Number n = (Number)rs.getObject(valueField);
 81+
 82+ v.set(k, n.doubleValue());
 83+ }
 84+
 85+ return v;
 86+ }
 87+
 88+ public double getProximity(int concept1, int concept2) throws PersistenceException {
 89+ String sql = "SELECT proximity FROM " +proximityTable.getSQLName()+" as P ";
 90+ sql += " WHERE concept1 = "+concept1;
 91+ sql += " AND concept2 = "+concept2;
 92+
 93+ try {
 94+ return ((Number)database.executeSingleValueQuery("getProximity", sql)).doubleValue();
 95+ } catch (SQLException e) {
 96+ throw new PersistenceException(e);
 97+ }
 98+ }
 99+
 100+
 101+}
\ No newline at end of file
Index: trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/store/ProximityStore.java
@@ -0,0 +1,19 @@
 2+package de.brightbyte.wikiword.store;
 3+
 4+import de.brightbyte.data.LabeledVector;
 5+import de.brightbyte.data.cursor.DataSet;
 6+import de.brightbyte.util.PersistenceException;
 7+import de.brightbyte.wikiword.model.WikiWordConcept;
 8+import de.brightbyte.wikiword.model.WikiWordConceptReference;
 9+
 10+public interface ProximityStore<T extends WikiWordConcept, R extends WikiWordConceptReference<T>> {
 11+
 12+ public LabeledVector<Integer> getFeatureVector(int concept) throws PersistenceException;
 13+
 14+ public double getProximity(int concept1, int concept2) throws PersistenceException;
 15+
 16+ public DataSet<? extends R> getEnvironment(int concept, double minProximity) throws PersistenceException;
 17+
 18+ public LabeledVector<Integer> getEnvironmentVector(int concept, double minProximity) throws PersistenceException;
 19+
 20+}
Index: trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/store/LocalConceptStore.java
@@ -2,7 +2,6 @@
33
44 import de.brightbyte.data.cursor.DataSet;
55 import de.brightbyte.util.PersistenceException;
6 -import de.brightbyte.wikiword.Corpus;
76 import de.brightbyte.wikiword.model.LocalConcept;
87 import de.brightbyte.wikiword.model.LocalConceptReference;
98 import de.brightbyte.wikiword.model.TermReference;
Index: trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/schema/ProximityStoreSchema.java
@@ -11,6 +11,7 @@
1212 import de.brightbyte.db.KeyType;
1313 import de.brightbyte.db.ReferenceField;
1414 import de.brightbyte.db.RelationTable;
 15+import de.brightbyte.util.PersistenceException;
1516 import de.brightbyte.wikiword.DatasetIdentifier;
1617 import de.brightbyte.wikiword.TweakSet;
1718
@@ -97,21 +98,23 @@
9899 protected EntityTable featureMagnitudeTable;
99100 protected RelationTable featureProductTable;
100101 protected RelationTable proximityTable;
 102+ protected String suffix;
 103+ private TweakSet tweaks;
 104+ private boolean isGlobal;
 105+ private boolean useFlushQueue;
101106
102 -
103 - public ProximityStoreSchema(DatasetIdentifier dataset, Connection connection, boolean global, TweakSet tweaks, boolean useFlushQueue) throws SQLException {
 107+ public ProximityStoreSchema(DatasetIdentifier dataset, Connection connection, String suffix, boolean global, TweakSet tweaks, boolean useFlushQueue) throws SQLException {
104108 super(dataset, connection, tweaks, useFlushQueue );
105 - init(global, tweaks);
 109+ init(global, tweaks, suffix, useFlushQueue);
106110 }
107111
108 - public ProximityStoreSchema(DatasetIdentifier dataset, DataSource connectionInfo, boolean global, TweakSet tweaks, boolean useFlushQueue) throws SQLException {
 112+ public ProximityStoreSchema(DatasetIdentifier dataset, DataSource connectionInfo, String suffix, boolean global, TweakSet tweaks, boolean useFlushQueue) throws SQLException {
109113 super(dataset, connectionInfo, tweaks, useFlushQueue);
110 - init(global, tweaks);
 114+ init(global, tweaks, suffix, useFlushQueue);
111115 }
112116
113 - public RelationTable makeFeatureTable(String suffix) {
 117+ public RelationTable makeFeatureTable() {
114118 String name = "feature";
115 - if (suffix!=null) name = name+suffix;
116119
117120 RelationTable featureTable = new RelationTable(this, name, getDefaultTableAttributes());
118121 featureTable.addField( new ReferenceField(this, "concept", "INT", null, true, null, "concept", "id", null ));
@@ -124,9 +127,8 @@
125128 return featureTable;
126129 }
127130
128 - public RelationTable makeProximityTable(String suffix) {
 131+ public RelationTable makeProximityTable() {
129132 String name = "proximity";
130 - if (suffix!=null) name = name+suffix;
131133
132134 RelationTable proximityTable = new RelationTable(this, name, getDefaultTableAttributes());
133135 proximityTable.addField( new ReferenceField(this, "concept1", "INT", null, true, null, "concept", "id", null ));
@@ -140,7 +142,7 @@
141143 return proximityTable;
142144 }
143145
144 - public EntityTable makeFeatureMagnitudeTable(String suffix) {
 146+ public EntityTable makeFeatureMagnitudeTable() {
145147 String name = "feature_magnitude";
146148 if (suffix!=null) name = name+suffix;
147149
@@ -152,8 +154,13 @@
153155 return featureMagnitudeTable;
154156 }
155157
156 - private void init(boolean global, TweakSet tweaks) {
157 - featureTable = makeFeatureTable(null);
 158+ private void init(boolean global, TweakSet tweaks, String suffix, boolean useFlushQueue) {
 159+ this.suffix = suffix;
 160+ this.tweaks = tweaks;
 161+ this.isGlobal = global;
 162+ this.useFlushQueue = useFlushQueue;
 163+
 164+ featureTable = makeFeatureTable();
158165 addTable(featureTable);
159166
160167 /*
@@ -169,12 +176,20 @@
170177 featureProductTable.setAutomaticField(null);
171178 addTable(featureProductTable);*/
172179
173 - featureMagnitudeTable = makeFeatureMagnitudeTable(null);
 180+ featureMagnitudeTable = makeFeatureMagnitudeTable();
174181 addTable(featureMagnitudeTable);
175182
176 - proximityTable = makeProximityTable(null);
 183+ proximityTable = makeProximityTable();
177184 addTable(proximityTable);
178185 }
 186+
 187+ public ProximityStoreSchema derive(String suffix) throws PersistenceException {
 188+ try {
 189+ return new ProximityStoreSchema(getDataset(), getConnection(), suffix, isGlobal, tweaks, useFlushQueue);
 190+ } catch (SQLException e) {
 191+ throw new PersistenceException(e);
 192+ }
 193+ }
179194
180195 @Override
181196 public void checkConsistency() throws SQLException {
Index: trunk/WikiWord/WikiWordBuilder/src/main/java/de/brightbyte/wikiword/store/builder/DatabaseProximityStoreBuilder.java
@@ -14,14 +14,11 @@
1515 import de.brightbyte.wikiword.TweakSet;
1616 import de.brightbyte.wikiword.processor.ImportProgressTracker;
1717 import de.brightbyte.wikiword.schema.ProximityStoreSchema;
18 -import de.brightbyte.wikiword.schema.WikiWordConceptStoreSchema;
1918
2019 public class DatabaseProximityStoreBuilder
2120 extends DatabaseWikiWordStoreBuilder
2221 implements ProximityStoreBuilder {
2322
24 - protected WikiWordConceptStoreSchema conceptDatabase;
25 -
2623 private DatabaseWikiWordConceptStoreBuilder conceptStore;
2724 private int proximityExpansionPasses;
2825
@@ -90,12 +87,16 @@
9188 protected ProximityStoreBuilder.ProximityParameters proximityParameters;
9289
9390 protected FeatureBuilder( ProximityStoreSchema database, String suffix, ProximityStoreBuilder.ProximityParameters proximityParameters, boolean temp) throws PersistenceException {
94 - this( database.makeFeatureTable(suffix),
95 - database.makeFeatureMagnitudeTable(suffix),
96 - database.makeProximityTable(suffix),
 91+ this(database.derive(suffix), proximityParameters, temp) ;
 92+ }
 93+
 94+ protected FeatureBuilder( ProximityStoreSchema database, ProximityStoreBuilder.ProximityParameters proximityParameters, boolean temp) throws PersistenceException {
 95+ this( database.makeFeatureTable(),
 96+ database.makeFeatureMagnitudeTable(),
 97+ database.makeProximityTable(),
9798 proximityParameters );
9899
99 - if (suffix!=null) {
 100+ if (database.getTableSuffix()!=null && database.getTableSuffix().length()>0) {
100101 try {
101102 database.createTable(proximityTable, !temp, temp);
102103 database.createTable(featureMagnitudeTable, !temp, temp);
@@ -509,11 +510,13 @@
510511
511512 }
512513
513 - protected FeatureBuilder makeBuilder(ProximityParameters params, String suffix) {
514 - RelationTable targetFeatureTable = ((ProximityStoreSchema)database).makeFeatureTable(suffix);
515 - EntityTable targetFeatureMagnitudeTable = ((ProximityStoreSchema)database).makeFeatureMagnitudeTable(suffix);
516 - RelationTable targetProximityTable = ((ProximityStoreSchema)database).makeProximityTable(suffix);
 514+ protected FeatureBuilder makeBuilder(ProximityParameters params, String suffix) throws PersistenceException {
 515+ ProximityStoreSchema db = ((ProximityStoreSchema)database).derive(suffix);
517516
 517+ RelationTable targetFeatureTable = db.makeFeatureTable();
 518+ EntityTable targetFeatureMagnitudeTable = db.makeFeatureMagnitudeTable();
 519+ RelationTable targetProximityTable = db.makeProximityTable();
 520+
518521 return new FeatureBuilder(targetFeatureTable, targetFeatureMagnitudeTable, targetProximityTable, params);
519522 }
520523
Index: trunk/WikiWord/WikiWordBuilder/src/main/java/de/brightbyte/wikiword/store/builder/DatabaseLocalConceptStoreBuilder.java
@@ -1443,7 +1443,7 @@
14441444 }
14451445
14461446 protected DatabaseProximityStoreBuilder newProximityStoreBuilder() throws SQLException {
1447 - ProximityStoreSchema schema = new ProximityStoreSchema(getDatasetIdentifier(), getDatabaseAccess().getConnection(), false, tweaks, false);
 1447+ ProximityStoreSchema schema = new ProximityStoreSchema(getDatasetIdentifier(), getDatabaseAccess().getConnection(), null, false, tweaks, false);
14481448 schema.setBackgroundErrorHandler(database.getBackgroundErrorHandler());
14491449 return new DatabaseProximityStoreBuilder(this, schema, tweaks, getAgenda());
14501450 }
Index: trunk/WikiWord/WikiWordBuilder/src/main/java/de/brightbyte/wikiword/store/builder/DatabaseGlobalConceptStoreBuilder.java
@@ -958,7 +958,7 @@
959959 }
960960
961961 protected DatabaseProximityStoreBuilder newProximityStoreBuilder() throws SQLException {
962 - ProximityStoreSchema schema = new ProximityStoreSchema(getDatasetIdentifier(), getDatabaseAccess().getConnection(), true, tweaks, false);
 962+ ProximityStoreSchema schema = new ProximityStoreSchema(getDatasetIdentifier(), getDatabaseAccess().getConnection(), null, true, tweaks, false);
963963 schema.setBackgroundErrorHandler(database.getBackgroundErrorHandler());
964964 return new DatabaseProximityStoreBuilder(this, schema, tweaks, getAgenda());
965965 }

Status & tagging log