All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.structr.cloud.sync.Synchronize Maven / Gradle / Ivy

Go to download

Structr is an open source framework based on the popular Neo4j graph database.

The newest version!
/**
 * Copyright (C) 2010-2016 Structr GmbH
 *
 * This file is part of Structr .
 *
 * Structr is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * Structr 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Structr.  If not, see .
 */
package org.structr.cloud.sync;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import org.structr.api.DatabaseService;
import org.structr.api.graph.Node;
import org.structr.api.graph.Relationship;
import org.structr.cloud.CloudConnection;
import org.structr.cloud.message.Finish;
import org.structr.cloud.message.Message;
import org.structr.common.error.FrameworkException;
import org.structr.core.GraphObject;
import org.structr.core.app.StructrApp;

/**
 * Synchronization message that causes the slave to examine its database
 * contents, reporting everything to the master.
 *
 *
 */
public class Synchronize extends Message {

	@Override
	public void onRequest(final CloudConnection serverConnection) throws IOException, FrameworkException {

		final DatabaseService graphDb        = StructrApp.getInstance().getDatabaseService();
		final String uuidPropertyName        = GraphObject.id.dbName();
		final Set visitedObjectIDs     = new HashSet<>();

		for (final Node node : graphDb.getAllNodes()) {

			if (!visitedObjectIDs.contains(node.getId())) {

				final String hash = contentHashCode(node, visitedObjectIDs);
				final Object uuid = node.getProperty(uuidPropertyName, null);

				if (uuid != null && uuid instanceof String) {

					serverConnection.send(new Diff(uuid.toString(), hash));
				}
			}
		}

		// clear set of visited objects because node and relationship IDs are offsets and can overlap.
		visitedObjectIDs.clear();

		for (final Relationship relationship : graphDb.getAllRelationships()) {

			if (!visitedObjectIDs.contains(relationship.getId())) {

				final String hash = contentHashCode(relationship, visitedObjectIDs);
				final Object uuid = relationship.getProperty(uuidPropertyName, null);

				if (uuid != null && uuid instanceof String) {

					serverConnection.send(new Diff(uuid.toString(), hash));
				}
			}
		}

		serverConnection.send(new Finish());
	}

	@Override
	public void onResponse(final CloudConnection clientConnection) throws IOException, FrameworkException {
	}

	@Override
	public void afterSend(final CloudConnection connection) {
	}

	@Override
	protected void deserializeFrom(final DataInputStream inputStream) throws IOException {
	}

	@Override
	protected void serializeTo(final DataOutputStream outputStream) throws IOException {
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy