|
8 | 8 | import com.orientechnologies.orient.core.db.ODatabaseSession; |
9 | 9 | import com.orientechnologies.orient.core.db.OrientDB; |
10 | 10 | import com.orientechnologies.orient.core.id.ORID; |
| 11 | +import com.orientechnologies.orient.core.metadata.schema.OClass; |
11 | 12 | import com.orientechnologies.orient.core.record.ORecord; |
12 | 13 | import java.io.ByteArrayInputStream; |
13 | 14 | import java.io.ByteArrayOutputStream; |
@@ -162,4 +163,71 @@ public void onMessage(String iText) {} |
162 | 163 | orientDB.drop(databaseName); |
163 | 164 | orientDB.close(); |
164 | 165 | } |
| 166 | + |
| 167 | + @Test |
| 168 | + public void importPreserveClusterIdWithRids() throws IOException { |
| 169 | + final String databaseName = "testClusters"; |
| 170 | + final String exportDbUrl = |
| 171 | + "embedded:target/export_" |
| 172 | + + ODatabaseImportTest.class.getSimpleName() |
| 173 | + + "_preserveRidsClusters"; |
| 174 | + final OrientDB orientDB = |
| 175 | + OCreateDatabaseUtil.createDatabase( |
| 176 | + databaseName, exportDbUrl, OCreateDatabaseUtil.TYPE_PLOCAL); |
| 177 | + ORID toCheck; |
| 178 | + final ByteArrayOutputStream output = new ByteArrayOutputStream(); |
| 179 | + try (final ODatabaseSession db = |
| 180 | + orientDB.open(databaseName, "admin", OCreateDatabaseUtil.NEW_ADMIN_PASSWORD)) { |
| 181 | + OClass cl = db.createClass("SimpleClass"); |
| 182 | + // Create gaps in the id of the clusters to make sure that it not pass just by chance |
| 183 | + int[] prev = cl.getClusterIds(); |
| 184 | + int id = db.addCluster("simpleNewCluster"); |
| 185 | + int id1 = db.addCluster("simpleNewCluster1"); |
| 186 | + cl.addClusterId(id); |
| 187 | + cl.addClusterId(id1); |
| 188 | + for (int pid : prev) { |
| 189 | + cl.removeClusterId(pid); |
| 190 | + db.dropCluster(pid); |
| 191 | + } |
| 192 | + db.save(db.newElement("SimpleClass")); |
| 193 | + ORID toDelete = db.save(db.newElement("SimpleClass")).getIdentity(); |
| 194 | + toCheck = db.save(db.newElement("SimpleClass")).getIdentity(); |
| 195 | + db.delete(toDelete); |
| 196 | + |
| 197 | + final ODatabaseExport export = |
| 198 | + new ODatabaseExport( |
| 199 | + (ODatabaseDocumentInternal) db, |
| 200 | + output, |
| 201 | + new OCommandOutputListener() { |
| 202 | + @Override |
| 203 | + public void onMessage(String iText) {} |
| 204 | + }); |
| 205 | + export.exportDatabase(); |
| 206 | + } |
| 207 | + |
| 208 | + final String importDbUrl = |
| 209 | + "embedded:target/import_" |
| 210 | + + ODatabaseImportTest.class.getSimpleName() |
| 211 | + + "_preserveRidsClusters"; |
| 212 | + OCreateDatabaseUtil.createDatabase(databaseName, importDbUrl, OCreateDatabaseUtil.TYPE_PLOCAL); |
| 213 | + |
| 214 | + try (final ODatabaseSession db = |
| 215 | + orientDB.open(databaseName, "admin", OCreateDatabaseUtil.NEW_ADMIN_PASSWORD)) { |
| 216 | + final ODatabaseImport importer = |
| 217 | + new ODatabaseImport( |
| 218 | + (ODatabaseDocumentInternal) db, |
| 219 | + new ByteArrayInputStream(output.toByteArray()), |
| 220 | + new OCommandOutputListener() { |
| 221 | + @Override |
| 222 | + public void onMessage(String iText) {} |
| 223 | + }); |
| 224 | + importer.setPreserveRids(true); |
| 225 | + importer.importDatabase(); |
| 226 | + ORecord read = db.load(toCheck); |
| 227 | + assertNotNull(read); |
| 228 | + Assert.assertEquals(read.getIdentity(), toCheck); |
| 229 | + } |
| 230 | + orientDB.drop(databaseName); |
| 231 | + orientDB.close(); |
| 232 | + } |
165 | 233 | } |
0 commit comments