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

com.orientechnologies.orient.server.OPushManager Maven / Gradle / Ivy

There is a newer version: 3.2.33
Show newest version
package com.orientechnologies.orient.server;

import com.orientechnologies.orient.client.remote.message.*;
import com.orientechnologies.orient.core.Orient;
import com.orientechnologies.orient.core.config.OStorageConfiguration;
import com.orientechnologies.orient.core.db.ODatabaseDocumentInternal;
import com.orientechnologies.orient.core.db.OMetadataUpdateListener;
import com.orientechnologies.orient.core.index.OIndexManager;
import com.orientechnologies.orient.core.index.OIndexManagerShared;
import com.orientechnologies.orient.core.metadata.schema.OSchemaShared;
import com.orientechnologies.orient.server.network.protocol.binary.ONetworkProtocolBinary;

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.*;

public class OPushManager implements OMetadataUpdateListener {

  protected final Set>              distributedConfigPush = new HashSet<>();
  protected final Map>> storageConfigurations = new HashMap<>();
  protected final Map>> schema                = new HashMap<>();
  protected final Map>> indexManager          = new HashMap<>();
  protected final Map>> functions             = new HashMap<>();
  protected final Map>> sequences             = new HashMap<>();
  private         Set                                             registerDatabase      = new HashSet<>();

  public synchronized void pushDistributedConfig(String database, List hosts) {
    Iterator> iter = distributedConfigPush.iterator();
    while (iter.hasNext()) {
      WeakReference ref = iter.next();
      ONetworkProtocolBinary protocolBinary = ref.get();
      if (protocolBinary != null) {
        //TODO Filter by database, push just list of active server for a specific database
        OPushDistributedConfigurationRequest request = new OPushDistributedConfigurationRequest(hosts);
        try {
          OBinaryPushResponse response = protocolBinary.push(request);
        } catch (IOException e) {
          iter.remove();
        }
      } else {
        iter.remove();
      }
    }
  }

  public synchronized void subscribeDistributeConfig(ONetworkProtocolBinary channel) {
    distributedConfigPush.add(new WeakReference(channel));
  }

  public synchronized void cleanPushSockets() {
    Iterator> iter = distributedConfigPush.iterator();
    while (iter.hasNext()) {
      if (iter.next().get() == null) {
        iter.remove();
      }
    }
    cleanListeners(storageConfigurations);
    cleanListeners(schema);
    cleanListeners(indexManager);
    cleanListeners(functions);
    cleanListeners(sequences);
  }

  private void cleanListeners(Map>> toClean) {
    for (Set> value : toClean.values()) {
      Iterator> iter = value.iterator();
      while (iter.hasNext()) {
        if (iter.next().get() == null) {
          iter.remove();
        }
      }
    }
  }

  public void shutdown() {

  }

  private void genericSubscribe(Map>> context, ODatabaseDocumentInternal database,
      ONetworkProtocolBinary protocol) {
    if (!registerDatabase.contains(database.getName())) {
      database.getSharedContext().registerListener(this);
      registerDatabase.add(database.getName());
    }
    Set> pushSockets = context.get(database.getName());
    if (pushSockets == null) {
      pushSockets = new HashSet<>();
      context.put(database.getName(), pushSockets);
    }
    pushSockets.add(new WeakReference<>(protocol));
  }

  public synchronized void subscribeStorageConfiguration(ODatabaseDocumentInternal database, ONetworkProtocolBinary protocol) {
    genericSubscribe(storageConfigurations, database, protocol);
  }

  public synchronized void subscribeSchema(ODatabaseDocumentInternal database, ONetworkProtocolBinary protocol) {
    genericSubscribe(schema, database, protocol);
  }

  public synchronized void subscribeIndexManager(ODatabaseDocumentInternal database, ONetworkProtocolBinary protocol) {
    genericSubscribe(indexManager, database, protocol);
  }

  public synchronized void subscribeFunctions(ODatabaseDocumentInternal database, ONetworkProtocolBinary protocol) {
    genericSubscribe(functions, database, protocol);
  }

  public synchronized void subscribeSequences(ODatabaseDocumentInternal database, ONetworkProtocolBinary protocol) {
    genericSubscribe(sequences, database, protocol);
  }

  @Override
  public void onSchemaUpdate(String database, OSchemaShared schema) {
    OPushSchemaRequest request = new OPushSchemaRequest(schema.toStream());
    genericNotify(this.schema, database, request);
  }

  @Override
  public void onIndexManagerUpdate(String database, OIndexManager indexManager) {
    OPushIndexManagerRequest request = new OPushIndexManagerRequest(((OIndexManagerShared) indexManager).toStream());
    genericNotify(this.indexManager, database, request);
  }

  @Override
  public void onFunctionLibraryUpdate(String database) {
    OPushFunctionsRequest request = new OPushFunctionsRequest();
    genericNotify(this.functions, database, request);
  }

  @Override
  public void onSequenceLibraryUpdate(String database) {
    OPushSequencesRequest request = new OPushSequencesRequest();
    genericNotify(this.functions, database, request);
  }

  @Override
  public void onStorageConfigurationUpdate(String database, OStorageConfiguration update) {
    OPushStorageConfigurationRequest request = new OPushStorageConfigurationRequest(update);
    genericNotify(storageConfigurations, database, request);
  }

  private void genericNotify(Map>> context, String database,
      OBinaryPushRequest request) {
    Orient.instance().submit(() -> {
      synchronized (OPushManager.this) {
        Set> clients = context.get(database);
        if (clients != null) {
          Iterator> iter = clients.iterator();
          while (iter.hasNext()) {
            WeakReference ref = iter.next();
            ONetworkProtocolBinary protocolBinary = ref.get();
            if (protocolBinary != null) {
              try {
                OBinaryPushResponse response = protocolBinary.push(request);
              } catch (IOException e) {
                iter.remove();
              }
            } else {
              iter.remove();
            }
          }
        }
      }
    });
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy