PageRenderTime 65ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/test_apps/osm-import/client/osmimport/VoltDBOsmSink.java

https://github.com/VoltDB/voltdb
Java | 350 lines | 242 code | 66 blank | 42 comment | 18 complexity | a082f408dd2226e4d9f2c4afc07030b7 MD5 | raw file
  1. /* This file is part of VoltDB.
  2. * Copyright (C) 2008-2022 Volt Active Data Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  19. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. * OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. package osmimport;
  24. import java.io.IOException;
  25. import java.net.UnknownHostException;
  26. import java.sql.SQLException;
  27. import java.util.ArrayList;
  28. import java.util.Collection;
  29. import java.util.List;
  30. import java.util.Map;
  31. import org.openstreetmap.osmosis.core.container.v0_6.BoundContainer;
  32. import org.openstreetmap.osmosis.core.container.v0_6.EntityContainer;
  33. import org.openstreetmap.osmosis.core.container.v0_6.EntityProcessor;
  34. import org.openstreetmap.osmosis.core.container.v0_6.NodeContainer;
  35. import org.openstreetmap.osmosis.core.container.v0_6.RelationContainer;
  36. import org.openstreetmap.osmosis.core.container.v0_6.WayContainer;
  37. import org.openstreetmap.osmosis.core.domain.v0_6.Node;
  38. import org.openstreetmap.osmosis.core.domain.v0_6.Relation;
  39. import org.openstreetmap.osmosis.core.domain.v0_6.RelationMember;
  40. import org.openstreetmap.osmosis.core.domain.v0_6.Tag;
  41. import org.openstreetmap.osmosis.core.domain.v0_6.Way;
  42. import org.openstreetmap.osmosis.core.domain.v0_6.WayNode;
  43. import org.openstreetmap.osmosis.core.task.v0_6.Sink;
  44. import org.openstreetmap.osmosis.pgsimple.common.NodeLocationStoreType;
  45. import org.postgis.LineString;
  46. import org.postgis.Polygon;
  47. import org.voltdb.VoltProcedure;
  48. import org.voltdb.client.Client;
  49. import org.voltdb.client.ClientConfig;
  50. import org.voltdb.client.ClientFactory;
  51. import org.voltdb.client.ClientResponse;
  52. import org.voltdb.client.ClientStatsContext;
  53. import org.voltdb.client.ClientStatusListenerExt;
  54. import org.voltdb.client.NoConnectionsException;
  55. import org.voltdb.client.ProcedureCallback;
  56. import org.voltdb.types.TimestampType;
  57. //import SyncBenchmark.StatusListener;
  58. public class VoltDBOsmSink implements Sink, EntityProcessor {
  59. public static final String INS_NODE_PROC = "NODES.insert";
  60. public static final String INS_NODE_TAG_PROC = "NODE_TAGS.insert";
  61. public static final String INS_RELATIONS_PROC = "RELATIONS.insert";
  62. public static final String INS_RELATIONS_MEMBER_PROC = "RELATION_MEMBERS.insert";
  63. public static final String INS_RELATION_TAGS_PROC = "RELATION_TAGS.insert";
  64. public static final String INS_USERS_PROC = "USERS.insert";
  65. public static final String INS_WAYS_PROC = "WAYS.insert";
  66. public static final String INS_WAYS_NODES_PROC = "WAY_NODES.insert";
  67. public static final String INS_WAY_TAGS_PROC = "WAY_TAGS.insert";
  68. private boolean enableLinestringBuilder = false;
  69. private boolean enableBboxBuilder = true;
  70. private boolean keepInvalidWays = false;
  71. private final WayPolygonGeometryBuilder wayGeometryBuilder = new WayPolygonGeometryBuilder(NodeLocationStoreType.TempFile);
  72. // Reference to the database connection we will use
  73. private String server;
  74. private ClientStatsContext periodicStatsContext;
  75. private ClientStatsContext fullStatsContext;
  76. private Client client;
  77. public VoltDBOsmSink(String server) {
  78. this.server = server;
  79. }
  80. @Override
  81. public void initialize(Map<String, Object> arg0) {
  82. try {
  83. connect(server);
  84. } catch (Exception e) {
  85. e.printStackTrace();
  86. System.exit(1);
  87. }
  88. }
  89. private void connect(String servers) throws InterruptedException, ClassNotFoundException, SQLException {
  90. System.out.println("Connecting to VoltDB...");
  91. ClientConfig clientConfig = new ClientConfig("", "", new VoltDBOsmSink.StatusListener());
  92. clientConfig.setMaxTransactionsPerSecond(10000);
  93. client = ClientFactory.createClient(clientConfig);
  94. try {
  95. // if we have more then one server, we would connect to each one
  96. // individually inside a loop.
  97. client.createConnection(servers);
  98. } catch (UnknownHostException e) {
  99. e.printStackTrace();
  100. } catch (IOException e) {
  101. e.printStackTrace();
  102. }
  103. periodicStatsContext = client.createStatsContext();
  104. fullStatsContext = client.createStatsContext();
  105. }
  106. @Override
  107. public void complete() {
  108. }
  109. @Override
  110. public void release() {
  111. try {
  112. client.drain();
  113. } catch (NoConnectionsException | InterruptedException e) {
  114. e.printStackTrace();
  115. }
  116. try {
  117. client.close();
  118. } catch (InterruptedException e) {
  119. e.printStackTrace();
  120. }
  121. }
  122. @Override
  123. public void process(EntityContainer entity) {
  124. entity.process(this);
  125. }
  126. public void process(BoundContainer boundContainer) {
  127. // Do nothing.
  128. }
  129. public void process(NodeContainer nodeContainer) {
  130. Node node;
  131. node = nodeContainer.getEntity();
  132. double lat = node.getLatitude();
  133. double lng = node.getLongitude();
  134. String pointText = "POINT(" + lng + " " + lat + ")";
  135. // keep track of the nodes so we can build polygons later
  136. if (enableBboxBuilder || enableLinestringBuilder) {
  137. wayGeometryBuilder.addNodeLocation(node);
  138. }
  139. // client.callProcedure(callback, procName, parameters)
  140. try {
  141. client.callProcedure(new InsertCallback(), INS_NODE_PROC, node.getId(), node.getVersion(),
  142. node.getUser().getId(), new TimestampType(node.getTimestamp().getTime()), node.getChangesetId(),
  143. pointText);
  144. } catch (NoConnectionsException e) {
  145. e.printStackTrace();
  146. } catch (IOException e) {
  147. e.printStackTrace();
  148. }
  149. Collection<Tag> tags = node.getTags();
  150. for (Tag tag : tags) {
  151. // System.out.println(INS_NODE_TAG_PROC+","+node.getId()+","+tag.getKey()+","+tag.getValue());
  152. try {
  153. client.callProcedure(new InsertCallback(), INS_NODE_TAG_PROC, node.getId(), tag.getKey(),
  154. tag.getValue());
  155. } catch (NoConnectionsException e) {
  156. e.printStackTrace();
  157. } catch (IOException e) {
  158. e.printStackTrace();
  159. }
  160. }
  161. }
  162. public void process(WayContainer wayContainer) {
  163. Way way;
  164. List<Long> nodeIds;
  165. way = wayContainer.getEntity();
  166. nodeIds = new ArrayList<Long>(way.getWayNodes().size());
  167. for (WayNode wayNode : way.getWayNodes()) {
  168. nodeIds.add(wayNode.getNodeId());
  169. }
  170. // Keep invalid ways out of the database if desired by the user
  171. if (way.getWayNodes().size() > 1 || keepInvalidWays) {
  172. for (Tag tag : way.getTags()) {
  173. try {
  174. client.callProcedure(new InsertCallback(), INS_WAY_TAGS_PROC, way.getId(), tag.getKey(),
  175. tag.getValue());
  176. } catch (NoConnectionsException e) {
  177. e.printStackTrace();
  178. } catch (IOException e) {
  179. e.printStackTrace();
  180. }
  181. }
  182. // Add these to the ways_nodes_table;
  183. int sequence = 0;
  184. for (Long nodeId : nodeIds) {
  185. try {
  186. client.callProcedure(new InsertCallback(), INS_WAYS_NODES_PROC, way.getId(), nodeId, sequence);
  187. } catch (NoConnectionsException e) {
  188. e.printStackTrace();
  189. } catch (IOException e) {
  190. e.printStackTrace();
  191. }
  192. sequence++;
  193. }
  194. StringBuffer sb = new StringBuffer();
  195. // if the first node id == the last nodeId, we know that this is a
  196. // closed loop.
  197. long n0 = nodeIds.get(0);
  198. long nn = nodeIds.get(nodeIds.size() - 1);
  199. if (n0 == nn) {
  200. if (enableBboxBuilder) {
  201. Polygon pg = wayGeometryBuilder.createPolygon(way);
  202. pg.outerWKT(sb);
  203. }
  204. } else {
  205. // it's a lineString, but we don't support it yet.
  206. if (enableLinestringBuilder) {
  207. LineString lineString = wayGeometryBuilder.createWayLinestring(way);
  208. lineString.outerWKT(sb);
  209. } else {
  210. return;
  211. }
  212. }
  213. String bbox = sb.toString();
  214. try {
  215. client.callProcedure(new InsertCallback(), INS_WAYS_PROC, way.getId(), way.getVersion(),
  216. way.getUser().getId(), way.getTimestamp(), way.getChangesetId(), bbox);
  217. } catch (NoConnectionsException e) {
  218. e.printStackTrace();
  219. } catch (IOException e) {
  220. e.printStackTrace();
  221. }
  222. }
  223. }
  224. /**
  225. * {@inheritDoc}
  226. */
  227. public void process(RelationContainer relationContainer) {
  228. Relation relation;
  229. int memberSequenceId;
  230. relation = relationContainer.getEntity();
  231. try {
  232. client.callProcedure(new InsertCallback(), INS_RELATIONS_PROC, relation.getId(), relation.getVersion(),
  233. relation.getUser().getId(), relation.getTimestamp(), relation.getChangesetId());
  234. } catch (NoConnectionsException e) {
  235. e.printStackTrace();
  236. } catch (IOException e) {
  237. e.printStackTrace();
  238. }
  239. memberSequenceId = 0;
  240. for (RelationMember member : relation.getMembers()) {
  241. try {
  242. client.callProcedure(new InsertCallback(), INS_RELATIONS_MEMBER_PROC, relation.getId(),
  243. member.getMemberId(), member.getMemberType().ordinal(), member.getMemberRole(),
  244. memberSequenceId);
  245. } catch (NoConnectionsException e) {
  246. e.printStackTrace();
  247. } catch (IOException e) {
  248. e.printStackTrace();
  249. }
  250. memberSequenceId++;
  251. }
  252. for (Tag tag : relation.getTags()) {
  253. try {
  254. client.callProcedure(new InsertCallback(), INS_RELATION_TAGS_PROC, relation.getId(), tag.getKey(),
  255. tag.getValue());
  256. } catch (NoConnectionsException e) {
  257. e.printStackTrace();
  258. } catch (IOException e) {
  259. e.printStackTrace();
  260. }
  261. }
  262. }
  263. /**
  264. * Provides a callback to be notified on node failure. This example only
  265. * logs the event.
  266. */
  267. public static class StatusListener extends ClientStatusListenerExt {
  268. @Override
  269. public void connectionLost(String hostname, int port, int connectionsLeft, DisconnectCause cause) {
  270. System.err.printf("Connection to %s:%d was lost.\n", hostname, port);
  271. }
  272. public void backpressure(boolean status) {
  273. }
  274. }
  275. public static class InsertCallback implements ProcedureCallback {
  276. @Override
  277. public void clientCallback(ClientResponse response) throws Exception {
  278. if (response.getStatus() != ClientResponse.SUCCESS) {
  279. System.err.println(response.getStatusString());
  280. return;
  281. }
  282. }
  283. }
  284. }