Exemple #1
0
 private int createNewPropertyKeyId(String stringKey) {
   PropertyKeyTokenStore idxStore = getPropertyKeyTokenStore();
   int keyId = (int) idxStore.nextId();
   PropertyKeyTokenRecord record = new PropertyKeyTokenRecord(keyId);
   record.setInUse(true);
   record.setCreated();
   Collection<DynamicRecord> keyRecords = idxStore.allocateNameRecords(encodeString(stringKey));
   record.setNameId((int) first(keyRecords).getId());
   record.addNameRecords(keyRecords);
   idxStore.updateRecord(record);
   propertyKeyTokens.addToken(stringKey, keyId);
   return keyId;
 }
  @Test
  public void shouldDeduplicateUniquePropertyIndexKeys() throws Exception {
    // GIVEN
    // a store that contains two nodes with property "name" of which there are two key tokens
    Unzip.unzip(Legacy19Store.class, "propkeydupdb.zip", storeDir);

    // WHEN
    // upgrading that store, the two key tokens for "name" should be merged
    upgrader(new StoreMigrator(monitor, fs)).migrateIfNeeded(storeDir);

    // THEN
    // verify that the "name" property for both the involved nodes
    GraphDatabaseService db =
        new GraphDatabaseFactory().newEmbeddedDatabase(storeDir.getAbsolutePath());
    try {
      Node nodeA = getNodeWithName(db, "A");
      assertThat(nodeA, inTx(db, hasProperty("name").withValue("A")));

      Node nodeB = getNodeWithName(db, "B");
      assertThat(nodeB, inTx(db, hasProperty("name").withValue("B")));

      Node nodeC = getNodeWithName(db, "C");
      assertThat(nodeC, inTx(db, hasProperty("name").withValue("C")));
      assertThat(nodeC, inTx(db, hasProperty("other").withValue("a value")));
      assertThat(nodeC, inTx(db, hasProperty("third").withValue("something")));
    } finally {
      db.shutdown();
    }

    // THEN
    // verify that there are no duplicate keys in the store
    PropertyKeyTokenStore tokenStore = cleanup.add(storeFactory.newPropertyKeyTokenStore());
    Token[] tokens = tokenStore.getTokens(MAX_VALUE);
    tokenStore.close();
    assertNoDuplicates(tokens);

    assertConsistentStore(storeDir);
  }