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

com.backendless.hive.HiveGeneral Maven / Gradle / Ivy

The newest version!
package com.backendless.hive;

import com.backendless.Invoker;
import com.backendless.async.callback.BackendlessCallback;
import com.backendless.core.responder.AdaptingResponder;

import java.util.concurrent.CompletableFuture;


abstract class HiveGeneral
{
  public final static String HIVE_GENERAL_KEY_ALIAS = "com.backendless.services.hive.HiveGeneralKeyService";
  protected final String hiveName;
  protected final StoreType storeType;
  protected final String storeKey;

  HiveGeneral( String hiveName, StoreType storeType, String storeKey )
  {
    this.hiveName = hiveName;
    this.storeType = storeType;
    this.storeKey = storeKey;
  }

  protected CompletableFuture delete()
  {
    return makeRemoteCallForGeneral( "del", new AdaptingResponder<>( Long.class ) );
  }

  protected CompletableFuture delete( String key )
  {
    return makeRemoteCallForGeneral( "del", new AdaptingResponder<>( Long.class ), key );
  }

  protected CompletableFuture rename( String newKey, boolean overwrite )
  {
    if (overwrite)
      return makeRemoteCallForGeneral( "rename", newKey ).thenApply( voidNull -> true );
    else
      return makeRemoteCallForGeneral( "renameIfNotExists", newKey );
  }

  protected CompletableFuture rename( String key, String newKey, boolean overwrite )
  {
    if (overwrite)
      return makeRemoteCallForGeneral( "rename", key, newKey ).thenApply( voidNull -> true );
    else
      return makeRemoteCallForGeneral( "renameIfNotExists", key, newKey );
  }

  protected CompletableFuture expireAfter( Integer ttlSeconds )
  {
    return makeRemoteCallForGeneral( "expire", ttlSeconds );
  }

  protected CompletableFuture expireAfter( String key, Integer ttlSeconds )
  {
    return makeRemoteCallForGeneral( "expire", key, ttlSeconds );
  }

  protected CompletableFuture expireAt( Integer unixTimeSeconds )
  {
    return makeRemoteCallForGeneral( "expireAt", unixTimeSeconds );
  }

  protected CompletableFuture expireAt( String key, Integer unixTimeSeconds )
  {
    return makeRemoteCallForGeneral( "expireAt", key, unixTimeSeconds );
  }

  protected CompletableFuture getExpiration()
  {
    return makeRemoteCallForGeneral( "getExpirationTTL", new AdaptingResponder<>( Long.class ) );
  }

  protected CompletableFuture getExpiration( String key )
  {
    return makeRemoteCallForGeneral( "getExpirationTTL", new AdaptingResponder<>( Long.class ), key );
  }

  protected CompletableFuture clearExpiration()
  {
    return makeRemoteCallForGeneral( "clearExpiration" );
  }

  protected CompletableFuture clearExpiration( String key )
  {
    return makeRemoteCallForGeneral( "clearExpiration", key );
  }

  protected CompletableFuture secondsSinceLastOperation()
  {
    return makeRemoteCallForGeneral( "secondsSinceLastOperation", new AdaptingResponder<>( Long.class ) );
  }

  protected CompletableFuture secondsSinceLastOperation( String key )
  {
    return makeRemoteCallForGeneral( "secondsSinceLastOperation", new AdaptingResponder<>( Long.class ), key );
  }

  // ----------------------------------------

  protected  CompletableFuture makeRemoteCallForGeneral( String methodName, Object... args )
  {
    return makeRemoteCallForGeneral( methodName, null, args );
  }

  protected  CompletableFuture makeRemoteCallForGeneral( String methodName, AdaptingResponder adaptingResponder, Object... args )
  {
    final Object[] combinedArgs;
    final int dstPos;
    if( storeKey == null )
    {
      dstPos = 2;
      combinedArgs = new Object[ dstPos + args.length ];
      combinedArgs[ 0 ] = hiveName;
      combinedArgs[ 1 ] = storeType;
    }
    else
    {
      dstPos = 3;
      combinedArgs = new Object[ dstPos + args.length ];
      combinedArgs[ 0 ] = hiveName;
      combinedArgs[ 1 ] = storeType;
      combinedArgs[ 2 ] = storeKey;
    }

    System.arraycopy( args, 0, combinedArgs, dstPos, args.length );
    return makeRemoteCall( HIVE_GENERAL_KEY_ALIAS, methodName, adaptingResponder, combinedArgs );
  }

  protected  CompletableFuture makeRemoteCallWithoutStoreKey( String remoteServiceName, String methodName, Object[] args )
  {
    return makeRemoteCallWithoutStoreKey( remoteServiceName, methodName, null, args );
  }

  protected  CompletableFuture makeRemoteCallWithoutStoreKey( String remoteServiceName, String methodName, AdaptingResponder adaptingResponder, Object[] args )
  {
    final Object[] combinedArgs = new Object[ 1 + args.length ];
    combinedArgs[ 0 ] = hiveName;
    System.arraycopy( args, 0, combinedArgs, 1, args.length );
    return makeRemoteCall( remoteServiceName, methodName, adaptingResponder, combinedArgs );
  }

  protected  CompletableFuture makeRemoteCallWithStoreKey( String remoteServiceName, String methodName, Object[] args )
  {
    return makeRemoteCallWithStoreKey( remoteServiceName, methodName, null, args );
  }

  protected  CompletableFuture makeRemoteCallWithStoreKey( String remoteServiceName, String methodName, AdaptingResponder adaptingResponder, Object[] args )
  {
    final Object[] combinedArgs = new Object[ 2 + args.length ];
    combinedArgs[ 0 ] = hiveName;
    combinedArgs[ 1 ] = storeKey;
    System.arraycopy( args, 0, combinedArgs, 2, args.length );

    return makeRemoteCall( remoteServiceName, methodName, adaptingResponder, combinedArgs );
  }

  private  CompletableFuture makeRemoteCall( String remoteServiceName, String methodName, AdaptingResponder adaptingResponder, Object[] args )
  {
    CompletableFuture futureResult = new CompletableFuture<>();
    Invoker.invokeAsync( remoteServiceName, methodName, args, new BackendlessCallback()
    {
      @Override
      public void handleResponse( T response )
      {
        futureResult.complete( response );
      }
    }, adaptingResponder );
    return futureResult;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy