PageRenderTime 57ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/im-crawler/src/main/java/com/wipro/ats/bdre/imcrawler/frontier/WorkQueues.java

https://gitlab.com/kingofhappy/openbdre
Java | 262 lines | 185 code | 12 blank | 65 comment | 33 complexity | ed108656b26cba31e668f8d1d0ad8cdc MD5 | raw file
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package com.wipro.ats.bdre.imcrawler.frontier;
  18. import com.wipro.ats.bdre.md.api.base.MetadataAPIBase;
  19. import com.wipro.ats.bdre.md.dao.jpa.Pendingurlsdb;
  20. import com.wipro.ats.bdre.md.dao.jpa.Weburlsdb;
  21. import com.wipro.ats.bdre.imcrawler.model.PendingUrlsDBDao;
  22. import com.wipro.ats.bdre.imcrawler.model.WebUrlsDBDao;
  23. import com.wipro.ats.bdre.imcrawler.url.WebURL;
  24. import org.springframework.beans.factory.annotation.Autowired;
  25. import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
  26. import java.util.ArrayList;
  27. import java.util.List;
  28. /**
  29. * @author Yasser Ganjisaffar modified by AS294216
  30. */
  31. public class WorkQueues extends MetadataAPIBase{
  32. private String dbName;
  33. //PersistenceManager manager = PMF.getInstance().getPersistenceManager();
  34. private final boolean resumable;
  35. protected final Object mutex = new Object();
  36. public WorkQueues(String dbName, boolean resumable) {
  37. this.dbName = dbName;
  38. this.resumable = resumable;
  39. /*Hibernate autowire*/
  40. AutowireCapableBeanFactory acbFactory = getAutowireCapableBeanFactory();
  41. acbFactory.autowireBean(this);
  42. }
  43. public Object execute(String[] params){
  44. return null;
  45. }
  46. @Autowired
  47. WebUrlsDBDao webUrlsDBDao;
  48. @Autowired
  49. PendingUrlsDBDao pendingUrlsDBDao;
  50. public List<WebURL> get(int max, String database, int pid) {
  51. synchronized (mutex) {
  52. List<WebURL> results = new ArrayList<>(max);
  53. //get all rows limit max
  54. if (database.equals("InProcessPagesDB")) {
  55. //Query query = manager.newQuery(WebURLsDB.class);
  56. //ForwardQueryResult data = (ForwardQueryResult) query.execute();
  57. Long totalSize = webUrlsDBDao.totalRecordCount();
  58. if (max <= totalSize) {
  59. // for (int i = 1; i <= max; i++) {
  60. List<Weburlsdb> weburlsdbList = webUrlsDBDao.list(0,max);
  61. // Weburlsdb info = webUrlsDBDao.get(i);
  62. for(Weburlsdb info:weburlsdbList) {
  63. if (info.getPid() == pid) {
  64. WebURL webURL = new WebURL();
  65. webURL.setURL(info.getUrl());
  66. webURL.setAnchor(info.getAnchor());
  67. webURL.setDepth(info.getDepth());
  68. webURL.setDocid(info.getDocid());
  69. webURL.setDomain(info.getDomain());
  70. webURL.setParentDocid(info.getParentdocid());
  71. webURL.setParentUrl(info.getParenturl());
  72. webURL.setPath(info.getPath());
  73. webURL.setPriority(info.getPriority().byteValue());
  74. webURL.setSubDomain(info.getSubdomain());
  75. webURL.setTag(info.getTag());
  76. results.add(webURL);
  77. }
  78. }
  79. // }
  80. } else {
  81. // for (int i = 1; i <= totalSize; i++) {
  82. List<Weburlsdb> weburlsdbList = webUrlsDBDao.list(0,totalSize.intValue());
  83. // Weburlsdb info = webUrlsDBDao.get(i);
  84. for(Weburlsdb info:weburlsdbList) {
  85. if (info.getPid() == pid) {
  86. WebURL webURL = new WebURL();
  87. webURL.setURL(info.getUrl());
  88. webURL.setAnchor(info.getAnchor());
  89. webURL.setDepth(info.getDepth());
  90. webURL.setDocid(info.getDocid());
  91. webURL.setDomain(info.getDomain());
  92. webURL.setParentDocid(info.getParentdocid());
  93. webURL.setParentUrl(info.getParenturl());
  94. webURL.setPath(info.getPath());
  95. webURL.setPriority(info.getPriority().byteValue());
  96. webURL.setSubDomain(info.getSubdomain());
  97. webURL.setTag(info.getTag());
  98. results.add(webURL);
  99. }
  100. }
  101. // }
  102. }
  103. } else if (database.equals("PendingURLsDB")) {
  104. //Query query = manager.newQuery(PendingURLsDB.class);
  105. //ForwardQueryResult data = (ForwardQueryResult) query.execute();
  106. Long totalSize = pendingUrlsDBDao.totalRecordCount();
  107. if (max <= totalSize) {
  108. // for (int i = 1; i <= max; i++) {
  109. List<Pendingurlsdb> pendingurlsdbList = pendingUrlsDBDao.list(0,max);
  110. // Pendingurlsdb info = pendingUrlsDBDao.get(i);
  111. for (Pendingurlsdb info:pendingurlsdbList) {
  112. if (info.getPid() == pid) {
  113. WebURL webURL = new WebURL();
  114. webURL.setURL(info.getUrl());
  115. webURL.setAnchor(info.getAnchor());
  116. webURL.setDepth(info.getDepth());
  117. webURL.setDocid(info.getDocid());
  118. webURL.setDomain(info.getDomain());
  119. webURL.setParentDocid(info.getParentdocid());
  120. webURL.setParentUrl(info.getParenturl());
  121. webURL.setPath(info.getPath());
  122. webURL.setPriority(info.getPriority().byteValue());
  123. webURL.setSubDomain(info.getSubdomain());
  124. webURL.setTag(info.getTag());
  125. results.add(webURL);
  126. }
  127. }
  128. // }
  129. } else {
  130. // for (int i = 1; i <= totalSize; i++) {
  131. List<Pendingurlsdb> pendingurlsdbList = pendingUrlsDBDao.list(0,totalSize.intValue());
  132. // Pendingurlsdb info = pendingUrlsDBDao.get(i);
  133. for (Pendingurlsdb info:pendingurlsdbList) {
  134. if (info.getPid() == pid) {
  135. WebURL webURL = new WebURL();
  136. webURL.setURL(info.getUrl());
  137. webURL.setAnchor(info.getAnchor());
  138. webURL.setDepth(info.getDepth());
  139. webURL.setDocid(info.getDocid());
  140. webURL.setDomain(info.getDomain());
  141. webURL.setParentDocid(info.getParentdocid());
  142. webURL.setParentUrl(info.getParenturl());
  143. webURL.setPath(info.getPath());
  144. webURL.setPriority(info.getPriority().byteValue());
  145. webURL.setSubDomain(info.getSubdomain());
  146. webURL.setTag(info.getTag());
  147. results.add(webURL);
  148. }
  149. }
  150. // }
  151. }
  152. }
  153. return results;
  154. }
  155. }
  156. public void delete(int count, String database) {
  157. synchronized (mutex) {
  158. //delete rows from starting in DB until count
  159. /* Transaction tx = manager.currentTransaction();
  160. tx.begin(); */
  161. if (database.equals("InProcessPagesDB")) {
  162. /* Query query = manager.newQuery(WebURLsDB.class);
  163. ForwardQueryResult data = (ForwardQueryResult) query.execute(); */
  164. List<Weburlsdb> weburlsdbList = webUrlsDBDao.list(0,count);
  165. for (Weburlsdb weburlsdb:weburlsdbList) {
  166. // for (int i = 1; i <= count; i++) {
  167. /* WebURLsDB info = (WebURLsDB) data.get(i);
  168. manager.deletePersistent(info); */
  169. webUrlsDBDao.delete(weburlsdb.getUniqid().intValue());
  170. }
  171. // }
  172. } else if (database.equals("PendingURLsDB")) {
  173. /* Query query = manager.newQuery(PendingURLsDB.class);
  174. ForwardQueryResult data = (ForwardQueryResult) query.execute(); */
  175. List<Pendingurlsdb> pendingurlsdbList = pendingUrlsDBDao.list(0,count);
  176. for (Pendingurlsdb pendingurlsdb:pendingurlsdbList) {
  177. // for (int i = 1; i <= count; i++) {
  178. /* PendingURLsDB info = (PendingURLsDB) data.get(i);
  179. manager.deletePersistent(info); */
  180. pendingUrlsDBDao.delete(pendingurlsdb.getUniqid().intValue());
  181. }
  182. // }
  183. }
  184. //tx.commit();
  185. }
  186. }
  187. public void put(WebURL url, String database, int pid, long instanceExecid) {
  188. /* Transaction tx = manager.currentTransaction();
  189. tx.begin(); */
  190. if (database.equals("InProcessPagesDB")) {
  191. if (url.getURL().length() < 2000) {
  192. Weburlsdb webURLsDB = new Weburlsdb();
  193. long lpid = (long) pid;
  194. webURLsDB.setPid(lpid);
  195. webURLsDB.setInstanceexecid(instanceExecid);
  196. webURLsDB.setUrl(url.getURL());
  197. webURLsDB.setAnchor(url.getAnchor());
  198. webURLsDB.setDepth(url.getDepth());
  199. webURLsDB.setDocid(url.getDocid());
  200. webURLsDB.setDomain(url.getDomain());
  201. webURLsDB.setParentdocid(url.getParentDocid());
  202. webURLsDB.setParenturl(url.getParentUrl());
  203. webURLsDB.setPath(url.getPath());
  204. webURLsDB.setPriority(new Integer(url.getPriority()));
  205. webURLsDB.setSubdomain(url.getSubDomain());
  206. webURLsDB.setTag(url.getTag());
  207. webUrlsDBDao.insert(webURLsDB);
  208. //manager.makePersistent(webURLsDB);
  209. }
  210. } else if (database.equals("PendingURLsDB")) {
  211. if (url.getURL().length() < 2000) {
  212. Pendingurlsdb pendingurlsdb = new Pendingurlsdb();
  213. long lpid = (long) pid;
  214. pendingurlsdb.setPid(lpid);
  215. pendingurlsdb.setInstanceexecid(instanceExecid);
  216. pendingurlsdb.setUrl(url.getURL());
  217. pendingurlsdb.setAnchor(url.getAnchor());
  218. pendingurlsdb.setDepth(url.getDepth());
  219. pendingurlsdb.setDocid(url.getDocid());
  220. pendingurlsdb.setDomain(url.getDomain());
  221. pendingurlsdb.setParentdocid(url.getParentDocid());
  222. pendingurlsdb.setParenturl(url.getParentUrl());
  223. pendingurlsdb.setPath(url.getPath());
  224. pendingurlsdb.setPriority(new Integer(url.getPriority()));
  225. pendingurlsdb.setSubdomain(url.getSubDomain());
  226. pendingurlsdb.setTag(url.getTag());
  227. pendingUrlsDBDao.insert(pendingurlsdb);
  228. //manager.makePersistent(pendingURLsDB);
  229. }
  230. }
  231. //tx.commit();
  232. }
  233. public long getLength(String database) {
  234. if (database.equals("InProcessPagesDB")) {
  235. /* Query query = manager.newQuery(WebURLsDB.class);
  236. ForwardQueryResult data = (ForwardQueryResult) query.execute(); */
  237. return webUrlsDBDao.totalRecordCount();
  238. } else if (database.equals("PendingURLsDB")) {
  239. /* Query query = manager.newQuery(PendingURLsDB.class);
  240. ForwardQueryResult data = (ForwardQueryResult) query.execute(); */
  241. return pendingUrlsDBDao.totalRecordCount();
  242. } else {
  243. return 0;
  244. }
  245. }
  246. public void close() {
  247. // urlsDB.close();
  248. // manager.close();
  249. }
  250. }