Search in sources :

Example 6 with Neo4jSession

use of org.neo4j.ogm.session.Neo4jSession in project neo4j-ogm by neo4j.

the class BasicDriverTest method shouldSaveMultipleObjectsWithWriteProtectionFromRoot.

@Test
public void shouldSaveMultipleObjectsWithWriteProtectionFromRoot() throws Exception {
    User avon = new User("Avon Barksdale");
    session.save(avon);
    User stringer = new User("Stringer Bell");
    session.save(stringer);
    session.clear();
    try {
        // save only Avon's properties, protect neighboring nodes from writes
        ((Neo4jSession) session).addWriteProtection(WriteProtectionTarget.PROPERTIES, object -> (object instanceof User) && !avon.getId().equals(((User) object).getId()));
        stringer.setName("Marlo");
        avon.befriend(stringer);
        session.save(avon);
        session.clear();
        Collection<User> users = session.loadAll(User.class);
        assertThat(users).hasSize(2).extracting(User::getName).containsExactlyInAnyOrder("Avon Barksdale", "Stringer Bell");
    } finally {
        ((Neo4jSession) session).removeWriteProtection(WriteProtectionTarget.PROPERTIES);
    }
}
Also used : User(org.neo4j.ogm.domain.social.User) Neo4jSession(org.neo4j.ogm.session.Neo4jSession) Test(org.junit.Test)

Example 7 with Neo4jSession

use of org.neo4j.ogm.session.Neo4jSession in project neo4j-ogm by neo4j.

the class CyclicStructureTest method testCyclicStructure.

/**
 * This issue was introduced by #407
 *
 * @see org.neo4j.ogm.persistence.examples.social.SocialIntegrationTest#shouldSaveObjectsToCorrectDepth
 * @see org.neo4j.ogm.persistence.examples.social.SocialIntegrationTest#shouldSaveAllDirectedRelationships
 */
// GH-609
@Test
public void testCyclicStructure() throws Exception {
    long numberOfNodes = 10;
    long numberOfRefFields = 100;
    List<CyclicNodeType> nodes = new ArrayList<>();
    for (long i = 0; i < numberOfNodes; ++i) {
        nodes.add(new CyclicNodeType());
    }
    List<RefField> refFields = new ArrayList<>();
    for (long i = 0; i < numberOfRefFields; i++) {
        RefField field = new RefField().setNodeTypes(nodes);
        refFields.add(field);
    }
    for (CyclicNodeType nodeType : nodes) {
        nodeType.setSubordinateNodeTypes(nodes);
        nodeType.setRefFields(refFields);
    }
    Session session = sessionFactory.openSession();
    // Make sure the number of builders fits the number of expected relationships
    MultiStatementCypherCompiler compiler = (MultiStatementCypherCompiler) mapAndCompile((Neo4jSession) session, nodes.get(0), -1);
    List<RelationshipBuilder> newRelationshipBuilders = extractRelationshipBuilder(compiler);
    assertThat(newRelationshipBuilders).size().isEqualTo(numberOfNodes * numberOfNodes + (numberOfNodes * numberOfRefFields * 2));
    // Make sure the session saves all relationships accordingly
    Transaction transaction = session.beginTransaction();
    session.save(nodes.get(0), -1);
    session.clear();
    transaction.commit();
    session = sessionFactory.openSession();
    Result result = session.query("" + " MATCH (c1:CyclicNodeType) - [s:SUBORDINATE] - (c2:CyclicNodeType)," + "       (c1) - [f:HAS_FIELD] -> (r:RefField)" + " RETURN count(distinct c1) as numberOfNodes, count(distinct s) as countRelCyc, " + "        count(distinct r)  as numberOfRefFields, count(distinct f) as countRelHasField", Collections.emptyMap());
    assertThat(result).hasSize(1);
    result.queryResults().forEach(record -> {
        assertThat(record.get("numberOfNodes")).isEqualTo(numberOfNodes);
        assertThat(record.get("numberOfRefFields")).isEqualTo(numberOfRefFields);
        assertThat(record.get("countRelCyc")).isEqualTo(numberOfNodes * numberOfNodes);
        assertThat(record.get("countRelHasField")).isEqualTo(numberOfNodes * numberOfRefFields);
    });
}
Also used : ArrayList(java.util.ArrayList) RefField(org.neo4j.ogm.domain.gh609.RefField) Result(org.neo4j.ogm.model.Result) Transaction(org.neo4j.ogm.transaction.Transaction) Neo4jSession(org.neo4j.ogm.session.Neo4jSession) CyclicNodeType(org.neo4j.ogm.domain.gh609.CyclicNodeType) Session(org.neo4j.ogm.session.Session) Neo4jSession(org.neo4j.ogm.session.Neo4jSession) Test(org.junit.Test)

Example 8 with Neo4jSession

use of org.neo4j.ogm.session.Neo4jSession in project LinkAgent by shulieTech.

the class DataSourceWrapUtil method wrap.

public static void wrap(DataSourceMeta<Neo4jSession> neo4jSessionDataSourceMeta) {
    if (DataSourceWrapUtil.pressureDataSources.containsKey(neo4jSessionDataSourceMeta)) {
        return;
    }
    // 生成影子库配置
    // 初始化影子库配置
    DriverConfiguration sourceConfig = Components.driver().getConfiguration();
    Configuration shadowConfig = new Configuration();
    DriverConfiguration configuration = new DriverConfiguration(shadowConfig);
    configuration.setDriverClassName(sourceConfig.getDriverClassName());
    configuration.setConnectionPoolSize(sourceConfig.getConnectionPoolSize());
    configuration.setEncryptionLevel(sourceConfig.getEncryptionLevel());
    configuration.setTrustCertFile(sourceConfig.getTrustCertFile());
    configuration.setTrustStrategy(sourceConfig.getTrustStrategy());
    String key = DbUrlUtils.getKey(neo4jSessionDataSourceMeta.getUrl(), neo4jSessionDataSourceMeta.getUsername());
    ShadowDatabaseConfig shadowDatabaseConfig = GlobalConfig.getInstance().getShadowDatabaseConfig(key);
    if (null == shadowDatabaseConfig) {
        ErrorReporter.buildError().setErrorType(ErrorTypeEnum.DataSource).setErrorCode("datasource-0002").setMessage("没有配置对应的影子表或影子库!").setDetail("业务库配置:::url: " + neo4jSessionDataSourceMeta.getUrl()).closePradar(ConfigNames.SHADOW_DATABASE_CONFIGS).report();
        return;
    }
    configuration.setURI(shadowDatabaseConfig.getShadowUrl());
    configuration.setCredentials(shadowDatabaseConfig.getShadowUsername(), shadowDatabaseConfig.getShadowPassword());
    Driver shadowDriver = DriverService.load(configuration);
    // 生成影子库session
    Neo4jSession sourceSession = neo4jSessionDataSourceMeta.getDataSource();
    Neo4JSessionExt shadowSession = new Neo4JSessionExt(metaDataMap.get(sourceSession), shadowDriver);
    // 放入本地
    DbMediatorDataSource<Neo4jSession> driverConfigurationDbMediatorDataSource = new DriverConfig();
    driverConfigurationDbMediatorDataSource.setDataSourceBusiness(sourceSession);
    driverConfigurationDbMediatorDataSource.setDataSourcePerformanceTest(shadowSession);
    DataSourceWrapUtil.pressureDataSources.put(neo4jSessionDataSourceMeta, driverConfigurationDbMediatorDataSource);
}
Also used : Neo4JSessionExt(com.pamirs.attach.plugin.neo4j.config.Neo4JSessionExt) DriverConfiguration(org.neo4j.ogm.config.DriverConfiguration) Configuration(org.neo4j.ogm.config.Configuration) DriverConfiguration(org.neo4j.ogm.config.DriverConfiguration) Neo4jSession(org.neo4j.ogm.session.Neo4jSession) Driver(org.neo4j.ogm.driver.Driver) DriverConfig(com.pamirs.attach.plugin.neo4j.config.DriverConfig) ShadowDatabaseConfig(com.pamirs.pradar.internal.config.ShadowDatabaseConfig)

Example 9 with Neo4jSession

use of org.neo4j.ogm.session.Neo4jSession in project neo4j-ogm by neo4j.

the class UserTest method testDeserialiseUserWithArrayOfEnums.

@Test
public void testDeserialiseUserWithArrayOfEnums() {
    MetaData metadata = new MetaData("org.neo4j.ogm.domain.cineasts.annotated");
    Neo4jSession session = new Neo4jSession(metadata, true, new UsersRequest());
    User user = session.load(User.class, "luanne", 1);
    assertThat(user.getLogin()).isEqualTo("luanne");
    assertThat(user.getSecurityRoles()).isNotNull();
    assertThat(user.getSecurityRoles().length).isEqualTo(2);
}
Also used : User(org.neo4j.ogm.domain.cineasts.annotated.User) MetaData(org.neo4j.ogm.metadata.MetaData) Neo4jSession(org.neo4j.ogm.session.Neo4jSession) Test(org.junit.Test)

Example 10 with Neo4jSession

use of org.neo4j.ogm.session.Neo4jSession in project neo4j-ogm by neo4j.

the class ElectionTest method shouldAllowVoterToChangeHerMind.

@Test
public void shouldAllowVoterToChangeHerMind() {
    Candidate a = new Candidate("A");
    Candidate b = new Candidate("B");
    Voter v = new Voter("V");
    v.candidateVotedFor = b;
    session.save(a);
    session.save(v);
    MappingContext context = ((Neo4jSession) session).context();
    assertThat(context.containsRelationship(new MappedRelationship(v.getId(), "CANDIDATE_VOTED_FOR", b.getId(), null, Voter.class, Candidate.class))).isTrue();
    session.clear();
    a = session.load(Candidate.class, a.getId());
    v = session.load(Voter.class, v.getId());
    assertThat(v.candidateVotedFor.getId()).isEqualTo(b.getId());
    assertThat(context.containsRelationship(new MappedRelationship(v.getId(), "CANDIDATE_VOTED_FOR", b.getId(), null, Voter.class, Candidate.class))).isTrue();
    v.candidateVotedFor = a;
    session.save(v);
    session.clear();
    session.load(Candidate.class, b.getId());
    session.load(Voter.class, v.getId());
    assertThat(v.candidateVotedFor.getId()).isEqualTo(a.getId());
    assertThat(context.containsRelationship(new MappedRelationship(v.getId(), "CANDIDATE_VOTED_FOR", a.getId(), null, Voter.class, Candidate.class))).isTrue();
    assertThat(context.containsRelationship(new MappedRelationship(v.getId(), "CANDIDATE_VOTED_FOR", b.getId(), null, Voter.class, Candidate.class))).isFalse();
    session.clear();
}
Also used : Candidate(org.neo4j.ogm.domain.election.Candidate) MappingContext(org.neo4j.ogm.context.MappingContext) Neo4jSession(org.neo4j.ogm.session.Neo4jSession) MappedRelationship(org.neo4j.ogm.context.MappedRelationship) Voter(org.neo4j.ogm.domain.election.Voter) Test(org.junit.Test)

Aggregations

Neo4jSession (org.neo4j.ogm.session.Neo4jSession)14 Test (org.junit.Test)11 Neo4JSessionExt (com.pamirs.attach.plugin.neo4j.config.Neo4JSessionExt)3 ArrayList (java.util.ArrayList)3 DriverConfiguration (org.neo4j.ogm.config.DriverConfiguration)3 MappingContext (org.neo4j.ogm.context.MappingContext)3 Artist (org.neo4j.ogm.domain.music.Artist)3 User (org.neo4j.ogm.domain.social.User)3 PressureMeasureError (com.pamirs.pradar.exception.PressureMeasureError)2 DataSourceMeta (com.pamirs.pradar.pressurement.agent.shared.service.DataSourceMeta)2 AuthTokenCredentials (org.neo4j.ogm.authentication.AuthTokenCredentials)2 Credentials (org.neo4j.ogm.authentication.Credentials)2 UsernamePasswordCredentials (org.neo4j.ogm.authentication.UsernamePasswordCredentials)2 Album (org.neo4j.ogm.domain.music.Album)2 Driver (org.neo4j.ogm.driver.Driver)2 MetaData (org.neo4j.ogm.metadata.MetaData)2 Result (org.neo4j.ogm.model.Result)2 Session (org.neo4j.ogm.session.Session)2 DriverConfig (com.pamirs.attach.plugin.neo4j.config.DriverConfig)1 Neo4JSessionOperation (com.pamirs.attach.plugin.neo4j.config.Neo4JSessionOperation)1