1 package fi.vtt.simantics.procore.internal;
\r
4 import java.io.FileInputStream;
\r
5 import java.io.FileOutputStream;
\r
6 import java.io.IOException;
\r
7 import java.io.InputStream;
\r
8 import java.io.OutputStream;
\r
9 import java.nio.ByteBuffer;
\r
11 import org.simantics.db.common.utils.Logger;
\r
12 import org.simantics.db.exception.DatabaseException;
\r
13 import org.simantics.db.exception.RuntimeDatabaseException;
\r
16 public class ReservedIds {
\r
17 private File file = null;
\r
18 private boolean loaded = false;
\r
19 private Data data = new Data();
\r
20 public static class Data {
\r
24 ReservedIds(File workDir, String id) {
\r
25 if (!workDir.isDirectory()) {
\r
26 if (!workDir.mkdir())
\r
27 throw new RuntimeDatabaseException("Work directory not correct: " + workDir.getPath());
\r
29 String name = workDir.getPath() + File.separator + "reservedIds." + id;
\r
30 file = new File(name);
\r
32 public boolean loaded() {
\r
35 public Data load() {
\r
37 if(file == null || !file.exists())
\r
41 } catch (IOException e) {
\r
42 e.printStackTrace();
\r
43 Logger.defaultLogError("Failed to load reserved ids.", e);
\r
48 public void create(int idCount, long nextId) {
\r
49 data.idCount = idCount;
\r
50 data.nextId = nextId;
\r
51 saveNoThrow(idCount, nextId);
\r
54 public void saveNoThrow(int idCount, long nextId) {
\r
56 save(idCount, nextId);
\r
57 } catch (IOException e) {
\r
58 e.printStackTrace();
\r
59 Logger.defaultLogError("Failed to load reserved ids.", e);
\r
62 public void save(int idCount, long nextId)
\r
63 throws IOException {
\r
64 data.idCount = idCount;
\r
65 data.nextId = nextId;
\r
66 OutputStream stream = new FileOutputStream(file);
\r
68 byte[] bytes = new byte[12];
\r
69 ByteBuffer bb = ByteBuffer.wrap(bytes);
\r
72 stream.write(bb.array());
\r
79 public void mergeToFile(File toFile)
\r
80 throws DatabaseException {
\r
83 long next = data.nextId + data.idCount;
\r
84 if (toFile.isDirectory()) {
\r
85 toFile = new File(toFile, file.getName());
\r
86 }else if (toFile.exists()) {
\r
88 InputStream is = null;
\r
90 is = new FileInputStream(toFile);
\r
91 byte[] bytes = new byte[8];
\r
92 int n = is.read(bytes);
\r
94 ByteBuffer bb = ByteBuffer.wrap(bytes);
\r
95 long oldNext = bb.getLong();
\r
102 } catch (IOException e) {
\r
103 String msg = "Could not open file " + toFile.getAbsolutePath() + ".";
\r
104 Logger.defaultLogError(msg, e);
\r
105 throw new DatabaseException(msg, e);
\r
109 OutputStream stream = new FileOutputStream(toFile);
\r
111 byte[] bytes = new byte[8];
\r
112 ByteBuffer bb = ByteBuffer.wrap(bytes);
\r
114 stream.write(bb.array());
\r
119 } catch (IOException e) {
\r
120 String msg = "Could not save file " + toFile.getAbsolutePath() + ".";
\r
121 Logger.defaultLogError(msg, e);
\r
122 throw new DatabaseException(msg, e);
\r
125 private void loadInternal()
\r
126 throws IOException {
\r
127 InputStream is = new FileInputStream(file);
\r
129 byte[] bytes = new byte[12];
\r
130 int n = is.read(bytes);
\r
133 ByteBuffer bb = ByteBuffer.wrap(bytes);
\r
134 data.idCount = bb.getInt();
\r
135 data.nextId = bb.getLong();
\r