|
|
@@ -7,6 +7,7 @@ import com.uas.search.console.core.util.ContextUtils;
|
|
|
import com.uas.search.console.dao.BrandSimpleInfoDao;
|
|
|
import com.uas.search.console.dao.ComponentSimpleInfoDao;
|
|
|
import com.uas.search.console.dao.KindSimpleInfoDao;
|
|
|
+import com.uas.search.console.dao.PropertyValueSimpleInfoDao;
|
|
|
import com.uas.search.console.model.BrandSimpleInfo;
|
|
|
import com.uas.search.console.model.ComponentSimpleInfo;
|
|
|
import com.uas.search.console.model.KindSimpleInfo;
|
|
|
@@ -40,51 +41,131 @@ public class DCNListener implements DatabaseChangeListener {
|
|
|
|
|
|
private ComponentSimpleInfoDao componentDao;
|
|
|
|
|
|
+ private PropertyValueSimpleInfoDao propertyValueDao;
|
|
|
+
|
|
|
private Logger logger = Logger.getLogger(DCNListener.class);
|
|
|
|
|
|
public DCNListener() {
|
|
|
indexService = ContextUtils.getBean(IndexService.class);
|
|
|
+ // 有两个SearchService bean:
|
|
|
+ // searchService用于dubbo服务,searchServiceImpl来自@Service
|
|
|
searchService = ContextUtils.getApplicationContext().getBean("searchService", SearchService.class);
|
|
|
kindDao = ContextUtils.getBean(KindSimpleInfoDao.class);
|
|
|
brandDao = ContextUtils.getBean(BrandSimpleInfoDao.class);
|
|
|
componentDao = ContextUtils.getBean(ComponentSimpleInfoDao.class);
|
|
|
+ propertyValueDao = ContextUtils.getBean(PropertyValueSimpleInfoDao.class);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void onDatabaseChangeNotification(DatabaseChangeEvent event) {
|
|
|
- logger.info(event);
|
|
|
QueryChangeDescription[] queryChangeDescriptions = event.getQueryChangeDescription();
|
|
|
for (QueryChangeDescription description : queryChangeDescriptions) {
|
|
|
TableChangeDescription[] tableChangeDescriptions = description.getTableChangeDescription();
|
|
|
for (TableChangeDescription tableChangeDescription : tableChangeDescriptions) {
|
|
|
String tableName = tableChangeDescription.getTableName();
|
|
|
- logger.info(tableName);
|
|
|
+ logger.info("tableName: " + tableName);
|
|
|
RowChangeDescription[] rowChangeDescriptions = tableChangeDescription.getRowChangeDescription();
|
|
|
for (RowChangeDescription rowChangeDescription : rowChangeDescriptions) {
|
|
|
RowOperation rowOperation = rowChangeDescription.getRowOperation();
|
|
|
logger.info("rowOperation " + rowOperation);
|
|
|
ROWID rowid = rowChangeDescription.getRowid();
|
|
|
- logger.info(rowid);
|
|
|
+ logger.info("rowid: " + rowid);
|
|
|
realTimeUpdateIndex(tableName, rowOperation, rowid.stringValue());
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 根据表的变化更新索引
|
|
|
+ *
|
|
|
+ * @param tableName
|
|
|
+ * @param rowOperation
|
|
|
+ * @param rowid
|
|
|
+ */
|
|
|
private void realTimeUpdateIndex(String tableName, RowOperation rowOperation, String rowid) {
|
|
|
- if (rowOperation.equals(RowOperation.DELETE)) {
|
|
|
- indexService.delete(getObject(tableName, rowid));
|
|
|
- } else if (rowOperation.equals(RowOperation.INSERT)) {
|
|
|
- indexService.save(getObject(tableName, rowid));
|
|
|
- } else if (rowOperation.equals(RowOperation.UPDATE)) {
|
|
|
- indexService.update(getObject(tableName, rowid));
|
|
|
+ // propertyvalue表变化,只涉及器件索引的更新,与其他表的变化不同
|
|
|
+ if (tableName.contains(SearchConstants.PROPERTYVALUE_TABLE_NAME.toUpperCase())) {
|
|
|
+ realTimeUpdateComponentIndex(rowOperation, rowid);
|
|
|
} else {
|
|
|
- logger.error("DCN error: tableName " + tableName + " " + rowOperation + " rowid " + rowid);
|
|
|
+ if (rowOperation.equals(RowOperation.DELETE)) {
|
|
|
+ indexService.delete(getObject(tableName, rowid));
|
|
|
+ } else if (rowOperation.equals(RowOperation.INSERT)) {
|
|
|
+ indexService.save(getObject(tableName, rowid));
|
|
|
+ } else if (rowOperation.equals(RowOperation.UPDATE)) {
|
|
|
+ indexService.update(getObject(tableName, rowid));
|
|
|
+ } else {
|
|
|
+ logger.error("DCN error: tableName " + tableName + " " + rowOperation + " rowid " + rowid);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据属性值表的变化更新器件索引
|
|
|
+ *
|
|
|
+ * @param rowOperation
|
|
|
+ * @param propertyValueRowid
|
|
|
+ */
|
|
|
+ private void realTimeUpdateComponentIndex(RowOperation rowOperation, String propertyValueRowid) {
|
|
|
+ // 属性值表变化,最终还是要更新其所属器件的索引
|
|
|
+ // 插入和更新可通过查询数据库获知相应器件的id
|
|
|
+ // 而删除操作需从本地索引中获取器件rowid,再根据rowid更新索引
|
|
|
+ ComponentSimpleInfo component = null;
|
|
|
+
|
|
|
+ // 属性值表变化为插入或更新操作
|
|
|
+ if (!rowOperation.equals(RowOperation.DELETE)) {
|
|
|
+ Long componentId = propertyValueDao.findComponentIdByRowid(propertyValueRowid);
|
|
|
+ if (componentId == null) {
|
|
|
+ logger.error("数据库中未找到属性值rowid: " + propertyValueRowid + " 所对应的器件");
|
|
|
+ } else {
|
|
|
+ component = componentDao.findById(componentId);
|
|
|
+ if (component == null) {
|
|
|
+ logger.error("数据库中未找到器件id: " + componentId + " 所对应的器件");
|
|
|
+ }
|
|
|
+ // 更新器件索引
|
|
|
+ else {
|
|
|
+ indexService.update(component);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 属性值表变化为删除操作
|
|
|
+ else {
|
|
|
+ // 从本地获取component的rowid
|
|
|
+ String componentRowid = searchService.getComponentRowidByPropertyValueRowid(propertyValueRowid);
|
|
|
+ if (StringUtils.isEmpty(componentRowid)) {
|
|
|
+ // 当同一个器件的属性值在一次事务中有多个变化时,
|
|
|
+ // 因为第一次变化时便已更新了索引,
|
|
|
+ // 之后的属性值DELETE操作,
|
|
|
+ // 在本地是无法根据属性值rowid找到对应的器件的,
|
|
|
+ // 于是会报下列的错,但实际索引是正确的,这种情况下不必在意
|
|
|
+ logger.error("本地索引中未找到属性值rowid: " + propertyValueRowid + " 所对应的器件");
|
|
|
+ } else {
|
|
|
+ // 根据rowid获取器件
|
|
|
+ component = componentDao.findByRowid(componentRowid);
|
|
|
+ // 存在,更新器件索引
|
|
|
+ if (component != null) {
|
|
|
+ indexService.update(component);
|
|
|
+ }
|
|
|
+ // 不存在,删除本地索引中的器件
|
|
|
+ else {
|
|
|
+ component = new ComponentSimpleInfo();
|
|
|
+ component.setRowid(propertyValueRowid);
|
|
|
+ indexService.delete(component);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 根据表的变化获得类目、品牌或器件对象
|
|
|
+ *
|
|
|
+ * @param tableName
|
|
|
+ * @param rowid
|
|
|
+ * @return
|
|
|
+ */
|
|
|
private Object getObject(String tableName, String rowid) {
|
|
|
// tableName可能为 UUPLATFORMDEMO.PRODUCT$COMPONENT
|
|
|
+ // kind表数据变化
|
|
|
if (tableName.contains(SearchConstants.KIND_TABLE_NAME.toUpperCase())) {
|
|
|
KindSimpleInfo kind = kindDao.findByRowid(rowid);
|
|
|
if (kind == null) {
|
|
|
@@ -92,29 +173,19 @@ public class DCNListener implements DatabaseChangeListener {
|
|
|
kind.setRowid(rowid);
|
|
|
}
|
|
|
return kind;
|
|
|
- } else if (tableName.contains(SearchConstants.BRAND_TABLE_NAME.toUpperCase())) {
|
|
|
+ }
|
|
|
+ // brand表变化
|
|
|
+ else if (tableName.contains(SearchConstants.BRAND_TABLE_NAME.toUpperCase())) {
|
|
|
BrandSimpleInfo brand = brandDao.findByRowid(rowid);
|
|
|
if (brand == null) {
|
|
|
brand = new BrandSimpleInfo();
|
|
|
brand.setRowid(rowid);
|
|
|
}
|
|
|
return brand;
|
|
|
- } else if (tableName.contains(SearchConstants.COMPONENT_TABLE_NAME.toUpperCase())) {
|
|
|
- ComponentSimpleInfo component = componentDao.findByRowid(rowid);
|
|
|
- if (component == null) {
|
|
|
- component = new ComponentSimpleInfo();
|
|
|
- component.setRowid(rowid);
|
|
|
- }
|
|
|
- return component;
|
|
|
}
|
|
|
- // TODO 属性值实时更新还存在问题
|
|
|
- else if (tableName.contains(SearchConstants.PROPERTYVALUE_TABLE_NAME.toUpperCase())) {
|
|
|
- // 从本地获取component的rowid
|
|
|
- String componentRowid = searchService.getComponentRowidByPropertyValueRowid(rowid);
|
|
|
- if (StringUtils.isEmpty(componentRowid)) {
|
|
|
- logger.error("未找到属性值rowid: " + rowid + " 所对应的器件id");
|
|
|
- }
|
|
|
- ComponentSimpleInfo component = componentDao.findByRowid(componentRowid);
|
|
|
+ // component表变化
|
|
|
+ else if (tableName.contains(SearchConstants.COMPONENT_TABLE_NAME.toUpperCase())) {
|
|
|
+ ComponentSimpleInfo component = componentDao.findByRowid(rowid);
|
|
|
if (component == null) {
|
|
|
component = new ComponentSimpleInfo();
|
|
|
component.setRowid(rowid);
|