1 package org.simantics.db.common;
3 import java.io.IOException;
4 import java.nio.charset.StandardCharsets;
5 import java.util.ArrayList;
8 import org.simantics.databoard.Bindings;
9 import org.simantics.databoard.binding.Binding;
10 import org.simantics.databoard.serialization.Serializer;
11 import org.simantics.db.Metadata;
12 import org.simantics.db.Session;
13 import org.simantics.db.WriteGraph;
14 import org.simantics.db.exception.DatabaseException;
15 import org.simantics.db.service.Bytes;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
19 import gnu.trove.list.array.TByteArrayList;
21 public final class CommandMetadata implements Metadata {
23 private static final Logger LOGGER = LoggerFactory.getLogger(CommandMetadata.class);
25 public static final boolean DEBUG = false;
26 public static final String RESET_COMMAND = "// RESET";
28 private static final Binding BINDING =
29 Bindings.getBindingUnchecked(CommandMetadata.class);
30 private static final Serializer SERIALIZER =
31 Bindings.getSerializerUnchecked(BINDING);
33 public List<Command> commands;
35 public static final class Command {
37 public String command;
42 public Command(long modelId, String command) {
44 this.modelId = modelId;
45 this.command = command;
49 public CommandMetadata() {
53 public byte[] serialise(Session session) {
55 TByteArrayList commandsSerialized = new TByteArrayList(4096);
56 byte[] bytes = new byte[8];
58 int commandsSize = commands.size();
59 Bytes.writeLE(bytes, 0, commandsSize);
60 commandsSerialized.add(bytes, 0, 4);
62 for (Command command : commands) {
63 Bytes.writeLE8(bytes, 0, command.modelId);
64 commandsSerialized.add(bytes);
65 byte[] commandBytes = command.command.getBytes(StandardCharsets.UTF_8);
66 Bytes.writeLE(bytes, 0, commandBytes.length);
67 commandsSerialized.add(bytes, 0, 4);
68 commandsSerialized.add(commandBytes);
70 return commandsSerialized.toArray();
71 } catch (Exception ee) {
72 // Ok, backwards compatibility required
73 // This is rather slow operation hence the new implementation above
75 return SERIALIZER.serialize(this);
76 } catch (IOException e) {
77 LOGGER.error("Could not serialize", e);
78 LOGGER.error("Original exception for new serialisation", ee);
79 throw new RuntimeException(e);
84 public static CommandMetadata deserialise(Session session, byte[] input) {
86 CommandMetadata metadata = new CommandMetadata();
87 metadata.commands = new ArrayList<Command>();
92 int commandsSize = Bytes.readLE4(input, byteIndex);
95 List<Command> commands = new ArrayList<>(commandsSize);
96 for (int i = 0; i < commandsSize; i++) {
97 long modelId = Bytes.readLE8(input, byteIndex);
99 int commandsLength = Bytes.readLE4(input, byteIndex);
102 String command = new String(input, byteIndex, commandsLength);
103 byteIndex += commandsLength;
104 Command comm = new Command(modelId, command);
107 CommandMetadata metadata = new CommandMetadata();
108 metadata.commands = commands;
110 } catch (Exception ee) {
111 // Ok, backwards compatibility required
112 // This is rather slow operation hence the new implementation above
114 return (CommandMetadata)SERIALIZER.deserialize(input);
115 } catch (Exception e) {
116 LOGGER.error("Could not deserialise", e);
117 LOGGER.error("Original exception for new deserialisation", ee);
123 public CommandMetadata add(Command command) {
124 commands.add(command);
128 public List<Command> getCommands() {
132 public static void add(WriteGraph graph, long modelId, String command) throws DatabaseException {
134 System.out.println("-------------------------------------------------------------");
135 System.out.println(command);
137 graph.addMetadata(graph.getMetadata(CommandMetadata.class).add(
138 new Command(modelId, command)));
141 public static void addReset(WriteGraph graph, long modelId) throws DatabaseException {
142 graph.addMetadata(graph.getMetadata(CommandMetadata.class).add(
143 new Command(modelId, RESET_COMMAND)));