|
| 1 | +package org.indunet.fastproto.ros2.bag; |
| 2 | + |
| 3 | +import org.yaml.snakeyaml.Yaml; |
| 4 | + |
| 5 | +import java.io.Closeable; |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.InputStream; |
| 8 | +import java.nio.file.DirectoryStream; |
| 9 | +import java.nio.file.Files; |
| 10 | +import java.nio.file.Path; |
| 11 | +import java.sql.Connection; |
| 12 | +import java.sql.DriverManager; |
| 13 | +import java.sql.PreparedStatement; |
| 14 | +import java.sql.ResultSet; |
| 15 | +import java.sql.SQLException; |
| 16 | +import java.sql.Statement; |
| 17 | +import java.util.ArrayList; |
| 18 | +import java.util.Collections; |
| 19 | +import java.util.LinkedHashMap; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.function.Consumer; |
| 23 | + |
| 24 | +/** |
| 25 | + * Reader for rosbag2 SQLite3 bags. |
| 26 | + */ |
| 27 | +public final class Ros2BagReader implements Closeable { |
| 28 | + private final Connection connection; |
| 29 | + private final Map<Integer, Ros2BagTopic> topicsById; |
| 30 | + |
| 31 | + private Ros2BagReader(Connection connection) throws SQLException { |
| 32 | + this.connection = connection; |
| 33 | + this.topicsById = loadTopics(connection); |
| 34 | + } |
| 35 | + |
| 36 | + public static Ros2BagReader open(Path path) throws IOException { |
| 37 | + Path database = resolveSqliteDatabase(path); |
| 38 | + |
| 39 | + try { |
| 40 | + return new Ros2BagReader(DriverManager.getConnection("jdbc:sqlite:" + database.toAbsolutePath())); |
| 41 | + } catch (SQLException e) { |
| 42 | + throw new IOException("Failed to open rosbag2 SQLite database: " + database, e); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + public List<Ros2BagTopic> topics() { |
| 47 | + return Collections.unmodifiableList(new ArrayList<Ros2BagTopic>(topicsById.values())); |
| 48 | + } |
| 49 | + |
| 50 | + public List<Ros2BagMessage> readMessages() throws IOException { |
| 51 | + return readMessages(null); |
| 52 | + } |
| 53 | + |
| 54 | + public List<Ros2BagMessage> readMessages(String topic) throws IOException { |
| 55 | + final List<Ros2BagMessage> messages = new ArrayList<Ros2BagMessage>(); |
| 56 | + forEachMessage(topic, new Consumer<Ros2BagMessage>() { |
| 57 | + @Override |
| 58 | + public void accept(Ros2BagMessage message) { |
| 59 | + messages.add(message); |
| 60 | + } |
| 61 | + }); |
| 62 | + return messages; |
| 63 | + } |
| 64 | + |
| 65 | + public Map<String, List<Ros2BagMessage>> readMessagesByTopic() throws IOException { |
| 66 | + final Map<String, List<Ros2BagMessage>> messagesByTopic = new LinkedHashMap<String, List<Ros2BagMessage>>(); |
| 67 | + for (Ros2BagTopic topic : topicsById.values()) { |
| 68 | + messagesByTopic.put(topic.getName(), new ArrayList<Ros2BagMessage>()); |
| 69 | + } |
| 70 | + |
| 71 | + forEachMessage(new Consumer<Ros2BagMessage>() { |
| 72 | + @Override |
| 73 | + public void accept(Ros2BagMessage message) { |
| 74 | + List<Ros2BagMessage> messages = messagesByTopic.get(message.getTopic()); |
| 75 | + if (messages == null) { |
| 76 | + messages = new ArrayList<Ros2BagMessage>(); |
| 77 | + messagesByTopic.put(message.getTopic(), messages); |
| 78 | + } |
| 79 | + messages.add(message); |
| 80 | + } |
| 81 | + }); |
| 82 | + |
| 83 | + return messagesByTopic; |
| 84 | + } |
| 85 | + |
| 86 | + public void forEachMessage(Consumer<Ros2BagMessage> consumer) throws IOException { |
| 87 | + forEachMessage(null, consumer); |
| 88 | + } |
| 89 | + |
| 90 | + public void forEachMessage(String topic, Consumer<Ros2BagMessage> consumer) throws IOException { |
| 91 | + String sql = "select topic_id, timestamp, data from messages"; |
| 92 | + if (topic != null) { |
| 93 | + sql += " where topic_id = ?"; |
| 94 | + } |
| 95 | + sql += " order by timestamp, id"; |
| 96 | + |
| 97 | + try (PreparedStatement statement = connection.prepareStatement(sql)) { |
| 98 | + if (topic != null) { |
| 99 | + statement.setInt(1, findTopicId(topic)); |
| 100 | + } |
| 101 | + |
| 102 | + try (ResultSet resultSet = statement.executeQuery()) { |
| 103 | + while (resultSet.next()) { |
| 104 | + int topicId = resultSet.getInt("topic_id"); |
| 105 | + Ros2BagTopic bagTopic = topicsById.get(topicId); |
| 106 | + if (bagTopic == null) { |
| 107 | + continue; |
| 108 | + } |
| 109 | + |
| 110 | + byte[] payload = resultSet.getBytes("data"); |
| 111 | + Object decoded = Ros2MessageDecoder.decode(bagTopic.getType(), payload); |
| 112 | + consumer.accept(new Ros2BagMessage( |
| 113 | + bagTopic.getName(), |
| 114 | + bagTopic.getType(), |
| 115 | + resultSet.getLong("timestamp"), |
| 116 | + payload, |
| 117 | + decoded |
| 118 | + )); |
| 119 | + } |
| 120 | + } |
| 121 | + } catch (SQLException e) { |
| 122 | + throw new IOException("Failed to read rosbag2 messages.", e); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public void close() throws IOException { |
| 128 | + try { |
| 129 | + connection.close(); |
| 130 | + } catch (SQLException e) { |
| 131 | + throw new IOException("Failed to close rosbag2 SQLite database.", e); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + private int findTopicId(String topic) throws IOException { |
| 136 | + for (Ros2BagTopic bagTopic : topicsById.values()) { |
| 137 | + if (bagTopic.getName().equals(topic)) { |
| 138 | + return bagTopic.getId(); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + throw new IOException("Unknown rosbag2 topic: " + topic); |
| 143 | + } |
| 144 | + |
| 145 | + private static Map<Integer, Ros2BagTopic> loadTopics(Connection connection) throws SQLException { |
| 146 | + Map<Integer, Ros2BagTopic> topics = new LinkedHashMap<Integer, Ros2BagTopic>(); |
| 147 | + try (Statement statement = connection.createStatement(); |
| 148 | + ResultSet resultSet = statement.executeQuery( |
| 149 | + "select id, name, type, serialization_format, offered_qos_profiles from topics order by id")) { |
| 150 | + while (resultSet.next()) { |
| 151 | + Ros2BagTopic topic = new Ros2BagTopic( |
| 152 | + resultSet.getInt("id"), |
| 153 | + resultSet.getString("name"), |
| 154 | + resultSet.getString("type"), |
| 155 | + resultSet.getString("serialization_format"), |
| 156 | + resultSet.getString("offered_qos_profiles") |
| 157 | + ); |
| 158 | + topics.put(topic.getId(), topic); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + return topics; |
| 163 | + } |
| 164 | + |
| 165 | + private static Path resolveSqliteDatabase(Path path) throws IOException { |
| 166 | + if (Files.isRegularFile(path)) { |
| 167 | + return path; |
| 168 | + } |
| 169 | + |
| 170 | + if (!Files.isDirectory(path)) { |
| 171 | + throw new IOException("rosbag2 path does not exist: " + path); |
| 172 | + } |
| 173 | + |
| 174 | + Path metadata = path.resolve("metadata.yaml"); |
| 175 | + if (Files.exists(metadata)) { |
| 176 | + Path fromMetadata = sqliteDatabaseFromMetadata(path, metadata); |
| 177 | + if (fromMetadata != null) { |
| 178 | + if (!Files.isRegularFile(fromMetadata)) { |
| 179 | + throw new IOException("rosbag2 metadata references missing SQLite file: " + fromMetadata); |
| 180 | + } |
| 181 | + return fromMetadata; |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + try (DirectoryStream<Path> stream = Files.newDirectoryStream(path, "*.db3")) { |
| 186 | + for (Path candidate : stream) { |
| 187 | + return candidate; |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + throw new IOException("No rosbag2 SQLite .db3 file found under: " + path); |
| 192 | + } |
| 193 | + |
| 194 | + @SuppressWarnings("unchecked") |
| 195 | + private static Path sqliteDatabaseFromMetadata(Path bagDirectory, Path metadata) throws IOException { |
| 196 | + Yaml yaml = new Yaml(); |
| 197 | + Object loaded; |
| 198 | + try (InputStream inputStream = Files.newInputStream(metadata)) { |
| 199 | + loaded = yaml.load(inputStream); |
| 200 | + } |
| 201 | + |
| 202 | + if (!(loaded instanceof Map)) { |
| 203 | + return null; |
| 204 | + } |
| 205 | + |
| 206 | + Object rosbag2BagfileInformation = ((Map<String, Object>) loaded).get("rosbag2_bagfile_information"); |
| 207 | + if (!(rosbag2BagfileInformation instanceof Map)) { |
| 208 | + return null; |
| 209 | + } |
| 210 | + |
| 211 | + Map<String, Object> information = (Map<String, Object>) rosbag2BagfileInformation; |
| 212 | + Object storageIdentifier = information.get("storage_identifier"); |
| 213 | + if (storageIdentifier != null && !"sqlite3".equals(storageIdentifier.toString())) { |
| 214 | + throw new IOException("Unsupported rosbag2 storage identifier: " + storageIdentifier); |
| 215 | + } |
| 216 | + |
| 217 | + Object relativeFilePaths = information.get("relative_file_paths"); |
| 218 | + if (!(relativeFilePaths instanceof List)) { |
| 219 | + return null; |
| 220 | + } |
| 221 | + |
| 222 | + for (Object relativeFilePath : (List<Object>) relativeFilePaths) { |
| 223 | + if (relativeFilePath != null && relativeFilePath.toString().endsWith(".db3")) { |
| 224 | + return bagDirectory.resolve(relativeFilePath.toString()); |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + return null; |
| 229 | + } |
| 230 | +} |
0 commit comments