Sfoglia il codice sorgente

索引实时更新部分增加对语句级触发器中批量更新的处理

sunyj 9 anni fa
parent
commit
78e156d110

+ 10 - 3
search-console/src/main/java/com/uas/search/console/jms/AQListener.java

@@ -155,17 +155,24 @@ public class AQListener {
 			return;
 		}
 
+		Object[] objects = parsedQueueMessage.getObjects();
 		// 新增索引
 		if (parsedQueueMessage.isInsert()) {
-			indexService.save(parsedQueueMessage.getObject());
+			for (Object object : objects) {
+				indexService.save(object);
+			}
 		}
 		// 更新索引
 		else if (parsedQueueMessage.isUpdate()) {
-			indexService.update(parsedQueueMessage.getObject());
+			for (Object object : objects) {
+				indexService.update(object);
+			}
 		}
 		// 删除索引
 		else if (parsedQueueMessage.isDelete()) {
-			indexService.delete(parsedQueueMessage.getObject());
+			for (Object object : objects) {
+				indexService.delete(object);
+			}
 		} else {
 			logger.error("message parsing failed!");
 		}

+ 61 - 45
search-console/src/main/java/com/uas/search/console/jms/QueueMessageParser.java

@@ -65,19 +65,19 @@ public class QueueMessageParser {
 		}
 
 		// 解析哪个表有更改
-		Object object = null;
+		Object[] objects = null;
 		String table = jsonObject.getString("table");
 		if (table.equals(SearchConstants.KIND_TABLE_NAME)) {
-			object = parseKind(jsonObject);
+			objects = parseKind(jsonObject);
 		} else if (table.equals(SearchConstants.BRAND_TABLE_NAME)) {
-			object = parseBrand(jsonObject);
+			objects = parseBrand(jsonObject);
 		} else if (table.equals(SearchConstants.COMPONENT_TABLE_NAME)) {
-			object = parseComponent(jsonObject);
+			objects = parseComponent(jsonObject);
 		} else {
 			return null;
 		}
 
-		parsedQueueMessage.setObject(object);
+		parsedQueueMessage.setObjects(objects);
 		return parsedQueueMessage;
 	}
 
@@ -85,70 +85,86 @@ public class QueueMessageParser {
 	 * 对kind类目进行解析
 	 * 
 	 * @param jsonObject
-	 * @return kind类目对象
+	 * @return kind类目对象数组
 	 * @throws JSONException
 	 */
-	// {"method":"value1","table":"product$kind","ki_id":5}
-	private KindSimpleInfo parseKind(JSONObject jsonObject) throws JSONException {
-		Long kindid = jsonObject.getLong("ki_id");
-		KindSimpleInfo kind = kindDao.findById(kindid);
-		// 对删除操作的事务回退进行处理
-		// (新增操作回退的话,返回的是null,并不影响索引;
-		// 更新回退的话,数据与先前一样,这时更新索引,索引数据并不变)
-		if (jsonObject.getString("method").equals("delete")) {
-			// 数据库中该类目仍存在,认为事务回退,不删除索引
-			if (kind != null) {
-				return null;
-			} else {
-				kind = new KindSimpleInfo();
-				kind.setId(kindid);
+	// {"method":"insert","table":"product$kind","ids":[2400,1299]}
+	private KindSimpleInfo[] parseKind(JSONObject jsonObject) throws JSONException {
+		// 获取本次更改的所有类目id
+		Object[] ids = jsonObject.getJSONArray("ids").toArray();
+		KindSimpleInfo[] kinds = new KindSimpleInfo[ids.length];
+		for (int i = 0; i < ids.length; i++) {
+			Long kindid = Long.parseLong(ids[i].toString());
+			KindSimpleInfo kind = kindDao.findById(kindid);
+			// 对删除操作的事务回退进行处理
+			// (新增操作回退的话,返回的是null,并不影响索引;
+			// 更新回退的话,数据与先前一样,这时更新索引,索引数据并不变)
+			if (jsonObject.getString("method").equals("delete")) {
+				// 数据库中该类目仍存在,认为事务回退,不删除索引
+				if (kind != null) {
+					kind = null;
+				} else {
+					kind = new KindSimpleInfo();
+					kind.setId(kindid);
+				}
 			}
+			kinds[i] = kind;
 		}
-		return kind;
+		return kinds;
 	}
 
 	/**
 	 * 对brand品牌进行解析
 	 * 
 	 * @param jsonObject
-	 * @return brand品牌对象
+	 * @return brand品牌对象数组
 	 * @throws JSONException
 	 */
-	// {"method":"value1","table":"product$brand","br_id":60}
-	private BrandSimpleInfo parseBrand(JSONObject jsonObject) throws JSONException {
-		Long brandid = jsonObject.getLong("br_id");
-		BrandSimpleInfo brand = brandDao.findById(brandid);
-		if (jsonObject.getString("method").equals("delete")) {
-			if (brand != null) {
-				return null;
-			} else {
-				brand = new BrandSimpleInfo();
-				brand.setId(brandid);
+	// {"method":"insert","table":"product$brand","ids":[124]}
+	private BrandSimpleInfo[] parseBrand(JSONObject jsonObject) throws JSONException {
+		Object[] ids = jsonObject.getJSONArray("ids").toArray();
+		BrandSimpleInfo[] brands = new BrandSimpleInfo[ids.length];
+		for (int i = 0; i < ids.length; i++) {
+			Long brandid = Long.parseLong(ids[i].toString());
+			BrandSimpleInfo brand = brandDao.findById(brandid);
+			if (jsonObject.getString("method").equals("delete")) {
+				if (brand != null) {
+					brand = null;
+				} else {
+					brand = new BrandSimpleInfo();
+					brand.setId(brandid);
+				}
 			}
+			brands[i] = brand;
 		}
-		return brand;
+		return brands;
 	}
 
 	/**
 	 * 对component器件进行解析
 	 * 
 	 * @param jsonObject
-	 * @return component器件对象
+	 * @return component器件对象数组
 	 * @throws JSONException
 	 */
-	// {"method":"value1","table":"product$component","cmp_id":2029}
-	private ComponentSimpleInfo parseComponent(JSONObject jsonObject) throws JSONException {
-		Long componentid = jsonObject.getLong("cmp_id");
-		ComponentSimpleInfo component = componentDao.findById(componentid);
-		if (jsonObject.getString("method").equals("delete")) {
-			if (component != null) {
-				return null;
-			} else {
-				component = new ComponentSimpleInfo();
-				component.setId(componentid);
+	// {"method":"value1","table":"product$component","ids":[1024]}
+	private ComponentSimpleInfo[] parseComponent(JSONObject jsonObject) throws JSONException {
+		Object[] ids = jsonObject.getJSONArray("ids").toArray();
+		ComponentSimpleInfo[] components = new ComponentSimpleInfo[ids.length];
+		for (int i = 0; i < ids.length; i++) {
+			Long componentid = Long.parseLong(ids[i].toString());
+			ComponentSimpleInfo component = componentDao.findById(componentid);
+			if (jsonObject.getString("method").equals("delete")) {
+				if (component != null) {
+					component = null;
+				} else {
+					component = new ComponentSimpleInfo();
+					component.setId(componentid);
+				}
 			}
+			components[i] = component;
 		}
-		return component;
+		return components;
 	}
 
 }

+ 8 - 6
search-console/src/main/java/com/uas/search/console/model/ParsedQueueMessage.java

@@ -1,5 +1,7 @@
 package com.uas.search.console.model;
 
+import java.util.Arrays;
+
 /**
  * 对数据库队列里的消息进行解析后所得到的数据
  * 
@@ -31,7 +33,7 @@ public class ParsedQueueMessage {
 	/**
 	 * 存放解析出来的对象:kind、brand或component对象
 	 */
-	private Object object;
+	private Object[] objects;
 
 	/**
 	 * 是否为insert类型
@@ -68,17 +70,17 @@ public class ParsedQueueMessage {
 		this.methodType = methodType;
 	}
 
-	public Object getObject() {
-		return object;
+	public Object[] getObjects() {
+		return objects;
 	}
 
-	public void setObject(Object object) {
-		this.object = object;
+	public void setObjects(Object[] objects) {
+		this.objects = objects;
 	}
 
 	@Override
 	public String toString() {
-		return "ParsedQueueMessage [methodType=" + methodType + ", object=" + object + "]";
+		return "ParsedQueueMessage [methodType=" + methodType + ", object=" + Arrays.toString(objects) + "]";
 	}
 
 }