diff --git a/bookMarkService/business/bookmark/src/main/java/com/fanxb/bookmark/business/bookmark/entity/BookmarkEs.java b/bookMarkService/business/bookmark/src/main/java/com/fanxb/bookmark/business/bookmark/entity/BookmarkEs.java new file mode 100644 index 0000000..b9a1ea3 --- /dev/null +++ b/bookMarkService/business/bookmark/src/main/java/com/fanxb/bookmark/business/bookmark/entity/BookmarkEs.java @@ -0,0 +1,33 @@ +package com.fanxb.bookmark.business.bookmark.entity; + +import com.fanxb.bookmark.common.entity.Bookmark; +import com.fanxb.bookmark.common.entity.EsInsertEntity; +import lombok.Data; + +/** + * 类功能简述: 书签在es中的存储结构 + * 类功能详述: + * + * @author fanxb + * @date 2019/7/24 15:07 + */ +@Data +public class BookmarkEs extends EsInsertEntity { + private Integer bookmarkId; + private Integer userId; + private String name; + private String url; + + public BookmarkEs() { + super(); + } + + public BookmarkEs(Bookmark bookmark) { + super(); + this.setData(this); + this.bookmarkId = bookmark.getBookmarkId(); + this.userId = bookmark.getUserId(); + this.name = bookmark.getName(); + this.url = bookmark.getUrl(); + } +} diff --git a/bookMarkService/business/bookmark/src/main/java/com/fanxb/bookmark/business/bookmark/service/BookmarkService.java b/bookMarkService/business/bookmark/src/main/java/com/fanxb/bookmark/business/bookmark/service/BookmarkService.java index a2f6b60..6b34a08 100644 --- a/bookMarkService/business/bookmark/src/main/java/com/fanxb/bookmark/business/bookmark/service/BookmarkService.java +++ b/bookMarkService/business/bookmark/src/main/java/com/fanxb/bookmark/business/bookmark/service/BookmarkService.java @@ -1,9 +1,12 @@ package com.fanxb.bookmark.business.bookmark.service; import com.fanxb.bookmark.business.bookmark.dao.BookmarkDao; +import com.fanxb.bookmark.business.bookmark.entity.BookmarkEs; import com.fanxb.bookmark.business.bookmark.entity.MoveNodeBody; +import com.fanxb.bookmark.common.constant.EsConstant; import com.fanxb.bookmark.common.entity.Bookmark; import com.fanxb.bookmark.common.exception.CustomException; +import com.fanxb.bookmark.common.util.EsUtil; import com.fanxb.bookmark.common.util.UserContextHolder; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; @@ -15,6 +18,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.io.InputStream; +import java.util.ArrayList; import java.util.List; /** @@ -35,6 +39,9 @@ public class BookmarkService { @Autowired private BookmarkDao bookmarkDao; + @Autowired + private EsUtil esUtil; + /** * Description: 解析书签文件 * @@ -53,19 +60,81 @@ public class BookmarkService { sortBase = 0; } int count = 0; + // 將要插入es的书签数据放到list中,最后一次插入,尽量避免mysql回滚了,但是es插入了 + List insertEsList = new ArrayList<>(); for (int i = 0, length = elements.size(); i < length; i++) { if (i == 0) { Elements firstChildren = elements.get(0).child(1).children(); count = firstChildren.size(); for (int j = 0; j < count; j++) { - dealBookmark(userId, firstChildren.get(j), path, sortBase + j); + dealBookmark(userId, firstChildren.get(j), path, sortBase + j, insertEsList); } } else { - dealBookmark(userId, elements.get(i), path, sortBase + count + i - 1); + dealBookmark(userId, elements.get(i), path, sortBase + count + i - 1, insertEsList); } } } + /** + * Description: 处理html节点,解析出文件夹和书签 + * + * @param ele 待处理节点 + * @param path 节点路径,不包含自身 + * @param sort 当前层级中的排序序号 + * @author fanxb + * @date 2019/7/8 14:49 + */ + private void dealBookmark(int userId, Element ele, String path, int sort, List insertList) { + if (!DT.equalsIgnoreCase(ele.tagName())) { + return; + } + Element first = ele.child(0); + if (A.equalsIgnoreCase(first.tagName())) { + //说明为链接 + Bookmark node = new Bookmark(userId, path, first.ownText(), first.attr("href"), first.attr("icon") + , Long.valueOf(first.attr("add_date")) * 1000, sort); + //存入数据库 + insertOne(node); + insertList.add(new BookmarkEs(node)); + } else { + //说明为文件夹 + Bookmark node = new Bookmark(userId, path, first.ownText(), Long.valueOf(first.attr("add_date")) * 1000, sort); + Integer sortBase = 0; + if (insertOne(node)) { + sortBase = bookmarkDao.selectMaxSort(node.getUserId(), path); + if (sortBase == null) { + sortBase = 0; + } + } + String childPath = path + "." + node.getBookmarkId(); + Elements children = ele.child(1).children(); + for (int i = 0, size = children.size(); i < size; i++) { + dealBookmark(userId, children.get(i), childPath, sortBase + i + 1, insertList); + } + esUtil.insertBatch(EsConstant.BOOKMARK_INDEX, insertList); + } + } + + /** + * Description: 插入一条书签,如果已经存在同名书签将跳过 + * + * @param node node + * @return boolean 如果已经存在返回true,否则false + * @author fanxb + * @date 2019/7/8 17:25 + */ + private boolean insertOne(Bookmark node) { + //先根据name,userId,parentId获取此节点id + Integer id = bookmarkDao.selectIdByUserIdAndNameAndPath(node.getUserId(), node.getName(), node.getPath()); + if (id == null) { + bookmarkDao.insertOne(node); + return false; + } else { + node.setBookmarkId(id); + return true; + } + } + /** * Description: 根据userId和path获取书签列表 @@ -109,6 +178,7 @@ public class BookmarkService { * @author fanxb * @date 2019/7/12 17:18 */ + @Transactional(rollbackFor = Exception.class) public Bookmark addOne(Bookmark bookmark) { int userId = UserContextHolder.get().getUserId(); Integer sort = bookmarkDao.selectMaxSort(userId, bookmark.getPath()); @@ -121,6 +191,10 @@ public class BookmarkService { } catch (DuplicateKeyException e) { throw new CustomException("同级目录下不能存在相同名称的数据"); } + //如果是书签,插入到es中 + if (bookmark.getType() == 0) { + esUtil.insertOne(EsConstant.BOOKMARK_INDEX, new BookmarkEs(bookmark)); + } return bookmark; } @@ -132,69 +206,15 @@ public class BookmarkService { * @author fanxb * @date 2019/7/17 14:42 */ + @Transactional(rollbackFor = Exception.class) public void updateOne(int userId, Bookmark bookmark) { bookmark.setUserId(userId); bookmarkDao.editBookmark(bookmark); - } - - - /** - * Description: 处理html节点,解析出文件夹和书签 - * - * @param ele 待处理节点 - * @param path 节点路径,不包含自身 - * @param sort 当前层级中的排序序号 - * @author fanxb - * @date 2019/7/8 14:49 - */ - private void dealBookmark(int userId, Element ele, String path, int sort) { - if (!DT.equalsIgnoreCase(ele.tagName())) { - return; - } - Element first = ele.child(0); - if (A.equalsIgnoreCase(first.tagName())) { - //说明为链接 - Bookmark node = new Bookmark(userId, path, first.ownText(), first.attr("href"), first.attr("icon") - , Long.valueOf(first.attr("add_date")) * 1000, sort); - //存入数据库 - insertOne(node); - } else { - //说明为文件夹 - Bookmark node = new Bookmark(userId, path, first.ownText(), Long.valueOf(first.attr("add_date")) * 1000, sort); - Integer sortBase = 0; - if (insertOne(node)) { - sortBase = bookmarkDao.selectMaxSort(node.getUserId(), path); - if (sortBase == null) { - sortBase = 0; - } - } - String childPath = path + "." + node.getBookmarkId(); - Elements children = ele.child(1).children(); - for (int i = 0, size = children.size(); i < size; i++) { - dealBookmark(userId, children.get(i), childPath, sortBase + i + 1); - } + if (bookmark.getType() == 0) { + esUtil.insertOne(EsConstant.BOOKMARK_INDEX, new BookmarkEs(bookmark)); } } - /** - * Description: 插入一条书签,如果已经存在同名书签将跳过 - * - * @param node node - * @return boolean 如果已经存在返回true,否则false - * @author fanxb - * @date 2019/7/8 17:25 - */ - private boolean insertOne(Bookmark node) { - //先根据name,userId,parentId获取此节点id - Integer id = bookmarkDao.selectIdByUserIdAndNameAndPath(node.getUserId(), node.getName(), node.getPath()); - if (id == null) { - bookmarkDao.insertOne(node); - return false; - } else { - node.setBookmarkId(id); - return true; - } - } @Transactional(rollbackFor = Exception.class) public void moveNode(int userId, MoveNodeBody body) { diff --git a/bookMarkService/business/user/src/main/java/com/fanxb/bookmark/business/user/service/UserService.java b/bookMarkService/business/user/src/main/java/com/fanxb/bookmark/business/user/service/UserService.java index cb5b85e..fbebc4d 100644 --- a/bookMarkService/business/user/src/main/java/com/fanxb/bookmark/business/user/service/UserService.java +++ b/bookMarkService/business/user/src/main/java/com/fanxb/bookmark/business/user/service/UserService.java @@ -4,7 +4,7 @@ import com.fanxb.bookmark.business.user.dao.UserDao; import com.fanxb.bookmark.business.user.entity.LoginBody; import com.fanxb.bookmark.business.user.entity.LoginRes; import com.fanxb.bookmark.business.user.entity.RegisterBody; -import com.fanxb.bookmark.common.Constant; +import com.fanxb.bookmark.common.constant.Constant; import com.fanxb.bookmark.common.entity.MailInfo; import com.fanxb.bookmark.common.entity.User; import com.fanxb.bookmark.common.exception.CustomException; diff --git a/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/constant/EsConstant.java b/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/constant/EsConstant.java index 048e37f..5c353ea 100644 --- a/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/constant/EsConstant.java +++ b/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/constant/EsConstant.java @@ -8,4 +8,31 @@ package com.fanxb.bookmark.common.constant; * @date 2019/7/23 14:34 */ public class EsConstant { + + /** + * 书签 index + */ + public static final String BOOKMARK_INDEX = "bookmark"; + + /** + * 创建bookmark index语句 + */ + public static final String CREATE_BOOKMARK_INDEX = "{" + + "\"properties\": {\n" + + " \"userId\":{\n" + + " \"type\":\"integer\",\n" + + " \"store\": true,\n" + + " \"index\":false\n" + + " },\n" + + " \"name\":{\n" + + " \"type\":\"text\",\n" + + " \"analyzer\": \"ik_max_word\",\n" + + " \"search_analyzer\": \"ik_smart\"\n" + + " },\n" + + " \"url\":{\n" + + " \"type\":\"text\",\n" + + " \"term_vector\": \"with_positions_offsets\"\n" + + " }\n" + + " }" + + "}"; } diff --git a/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/entity/EsInsertEntity.java b/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/entity/EsInsertEntity.java index 71f3bc8..08cd22c 100644 --- a/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/entity/EsInsertEntity.java +++ b/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/entity/EsInsertEntity.java @@ -1,5 +1,7 @@ package com.fanxb.bookmark.common.entity; +import lombok.Data; + /** * 类功能简述: * 类功能详述: @@ -7,5 +9,17 @@ package com.fanxb.bookmark.common.entity; * @author fanxb * @date 2019/7/24 17:37 */ -public class EsInsertEntity { +@Data +public class EsInsertEntity { + + private String id; + private T data; + + public EsInsertEntity() { + } + + public EsInsertEntity(String id, T data) { + this.data = data; + this.id = id; + } } diff --git a/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/filter/LoginFilter.java b/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/filter/LoginFilter.java index c953e9a..5b1c70c 100644 --- a/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/filter/LoginFilter.java +++ b/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/filter/LoginFilter.java @@ -2,7 +2,7 @@ package com.fanxb.bookmark.common.filter; import com.alibaba.fastjson.JSON; import com.auth0.jwt.interfaces.Claim; -import com.fanxb.bookmark.common.Constant; +import com.fanxb.bookmark.common.constant.Constant; import com.fanxb.bookmark.common.dao.UrlDao; import com.fanxb.bookmark.common.entity.Result; import com.fanxb.bookmark.common.entity.Url;