Merge commit 'ffdf837'
[simantics/platform.git] / bundles / org.simantics.db.procore / src / fi / vtt / simantics / procore / internal / ReservedIds.java
1 package fi.vtt.simantics.procore.internal;\r
2 \r
3 import java.io.File;\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
10 \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
14 \r
15 \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
21         int idCount = 0;\r
22         long nextId = 0;\r
23     }\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
28         }\r
29         String name = workDir.getPath() + File.separator + "reservedIds." + id;\r
30         file = new File(name);\r
31     }\r
32     public boolean loaded() {\r
33         return loaded;  \r
34     }\r
35     public Data load() {\r
36         try {\r
37                 if(file == null || !file.exists())\r
38                     return data;\r
39             loadInternal();\r
40             loaded = true;\r
41         } catch (IOException e) {\r
42             e.printStackTrace();\r
43             Logger.defaultLogError("Failed to load reserved ids.", e);\r
44             return null;\r
45         }\r
46         return data;\r
47     }\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
52         loaded = true;\r
53     }\r
54     public void saveNoThrow(int idCount, long nextId) {\r
55         try {\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
60         }\r
61     }\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
67         try  {\r
68             byte[] bytes = new byte[12];\r
69             ByteBuffer bb = ByteBuffer.wrap(bytes);\r
70             bb.putInt(idCount);\r
71             bb.putLong(nextId);\r
72             stream.write(bb.array());\r
73             stream.close();\r
74         } finally {\r
75             stream.close();\r
76         }\r
77         loaded = true;\r
78     }\r
79     public void mergeToFile(File toFile)\r
80     throws DatabaseException {\r
81         if (!loaded)\r
82             load();\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
87             try {\r
88                 InputStream is = null;\r
89                 try {\r
90                     is = new FileInputStream(toFile);\r
91                     byte[] bytes = new byte[8];\r
92                     int n = is.read(bytes);\r
93                     if (n == 8) {\r
94                         ByteBuffer bb = ByteBuffer.wrap(bytes);\r
95                         long oldNext = bb.getLong();\r
96                         if (oldNext > next)\r
97                             next = oldNext;\r
98                     }\r
99                 } finally {\r
100                     is.close();\r
101                 }\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
106             }\r
107         }\r
108         try{\r
109             OutputStream stream = new FileOutputStream(toFile);\r
110             try  {\r
111                 byte[] bytes = new byte[8];\r
112                 ByteBuffer bb = ByteBuffer.wrap(bytes);\r
113                 bb.putLong(next);\r
114                 stream.write(bb.array());\r
115                 stream.close();\r
116             } finally {\r
117                 stream.close();\r
118             }\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
123         }\r
124     }\r
125     private void loadInternal()\r
126     throws IOException {\r
127         InputStream is = new FileInputStream(file);\r
128         try {\r
129             byte[] bytes = new byte[12];\r
130             int n = is.read(bytes);\r
131             if (n != 12)\r
132                 return;\r
133             ByteBuffer bb = ByteBuffer.wrap(bytes);\r
134             data.idCount = bb.getInt();\r
135             data.nextId = bb.getLong();\r
136         } finally {\r
137             is.close();\r
138         }\r
139     }\r
140 }\r