Skip to content

Commit d2639d9

Browse files
committed
fix:corrected issue for preserving cluster ids when importing with preserve rids
1 parent ff68ae0 commit d2639d9

File tree

2 files changed

+71
-3
lines changed

2 files changed

+71
-3
lines changed

‎core/src/main/java/com/orientechnologies/orient/core/db/tool/ODatabaseImport.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,7 +1041,7 @@ private long importClusters() throws ParseException, IOException {
10411041
// CREATE IT
10421042
if (!preserveClusterIDs) createdClusterId = database.addCluster(name);
10431043
else if (getDatabase().getClusterNameById(clusterIdFromJson) == null) {
1044-
createdClusterId = database.addCluster(name, clusterIdFromJson, null);
1044+
createdClusterId = database.addCluster(name, clusterIdFromJson);
10451045
assert createdClusterId == clusterIdFromJson;
10461046
} else {
10471047
createdClusterId = database.addCluster(name);
@@ -1055,7 +1055,7 @@ else if (getDatabase().getClusterNameById(clusterIdFromJson) == null) {
10551055
if (database.countClusterElements(createdClusterId - 1) == 0) {
10561056
listener.onMessage("Found previous version: migrating old clusters...");
10571057
database.dropCluster(name);
1058-
database.addCluster("temp_" + createdClusterId, null);
1058+
database.addCluster("temp_" + createdClusterId);
10591059
createdClusterId = database.addCluster(name);
10601060
} else
10611061
throw new OConfigurationException(
@@ -1078,7 +1078,7 @@ else if (getDatabase().getClusterNameById(clusterIdFromJson) == null) {
10781078
((OClassEmbedded) clazz).removeClusterId(createdClusterId, true);
10791079

10801080
database.dropCluster(createdClusterId);
1081-
createdClusterId = database.addCluster(name, clusterIdFromJson, null);
1081+
createdClusterId = database.addCluster(name, clusterIdFromJson);
10821082
}
10831083
}
10841084
clusterToClusterMapping.put(clusterIdFromJson, createdClusterId);

‎core/src/test/java/com/orientechnologies/orient/core/db/tool/ODatabaseImportTest.java‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.orientechnologies.orient.core.db.ODatabaseSession;
99
import com.orientechnologies.orient.core.db.OrientDB;
1010
import com.orientechnologies.orient.core.id.ORID;
11+
import com.orientechnologies.orient.core.metadata.schema.OClass;
1112
import com.orientechnologies.orient.core.record.ORecord;
1213
import java.io.ByteArrayInputStream;
1314
import java.io.ByteArrayOutputStream;
@@ -162,4 +163,71 @@ public void onMessage(String iText) {}
162163
orientDB.drop(databaseName);
163164
orientDB.close();
164165
}
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+
}
165233
}

0 commit comments

Comments
 (0)