Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2015 GraphAware
   *
   * This file is part of GraphAware.
   *
   * GraphAware is free software: you can redistribute it and/or modify it under the terms of
   * the GNU General Public License as published by the Free Software Foundation, either
   * version 3 of the License, or (at your option) any later version.
   *
  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  * See the GNU General Public License for more details. You should have received a copy of
  * the GNU General Public License along with this program.  If not, see
  * <http://www.gnu.org/licenses/>.
  */
 
 package com.graphaware.example.module;
 
 
 
 import static com.graphaware.example.module.Labels.FriendshipCounter;
 import static com.graphaware.example.module.Relationships.FRIEND_OF;
 import static org.neo4j.tooling.GlobalGraphOperations.at;

com.graphaware.runtime.module.TxDrivenModule that counts the total friendship strength in the database and keeps it up to date.
 
 public class FriendshipStrengthModule extends BaseTxDrivenModule<Void> {
 
     private final TxDrivenModuleConfiguration configuration;
     private final FriendshipStrengthCounter counter;
 
     public FriendshipStrengthModule(String moduleIdGraphDatabaseService database) {
         super(moduleId);
         this. = new FriendshipStrengthCounter(database);
 
         //only take into account relationships with FRIEND_OF type:
          = FluentTxDrivenModuleConfiguration
                 .defaultConfiguration()
                 .with(
                         new RelationshipInclusionPolicy.Adapter() {
                             @Override
                             public boolean include(Relationship relationship) {
                                 return relationship.isType();
                             }
                         }
                 );
     }

    
 
     @Override
         return ;
     }

    
 
     @Override
     public void initialize(GraphDatabaseService database) {
         try (Transaction tx = database.beginTx()) {
             for (Node counter : at(database).getAllNodesWithLabel()) {
                 counter.delete();
             }
             tx.success();
         }
 
         new IterableInputBatchTransactionExecutor<>(
                 database, 10000,
                 new TransactionalInput<>(database, 10000, new TransactionCallback<Iterable<Relationship>>() {
                     @Override
                     public Iterable<RelationshipdoInTransaction(GraphDatabaseService databasethrows Exception {
                         return at(database).getAllRelationships();
                     }
                 }),
                 new UnitOfWork<Relationship>() {
                     @Override
                     public void execute(GraphDatabaseService databaseRelationship relationshipint batchNumberint stepNumber) {
                         .handleCreatedFriendships(Collections.singleton(relationship));
                     }
                 }
         ).execute();
    }

    
    @Override
    public Void beforeCommit(ImprovedTransactionData transactionData) {
        if (transactionData.mutationsOccurred()) {
            .handleCreatedFriendships(transactionData.getAllCreatedRelationships());
            .handleChangedFriendships(transactionData.getAllChangedRelationships());
            .handleDeletedFriendships(transactionData.getAllDeletedRelationships());
        }
        return null;
    }
New to GrepCode? Check out our FAQ X