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

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

The newest version!
package com.backendless.hive;

import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.CompletableFuture;


public final class Hive
{
  private final static WeakHashMap hives = new WeakHashMap<>();
  private final static WeakHashMap> listHives = new WeakHashMap<>();
  private final static WeakHashMap> setHives = new WeakHashMap<>();
  private final static WeakHashMap> sortedSetHives = new WeakHashMap<>();
  private final static WeakHashMap> mapHives = new WeakHashMap<>();
  private static final HiveManagement hiveManagement = HiveManagement.getInstance();

  private final String hiveName;
  private final HiveKeyValue hiveKeyValue;

  private final HiveGeneralWithoutStoreKey generalKeyValueOps;
  private final HiveGeneralWithoutStoreKey generalListOps;
  private final HiveGeneralWithoutStoreKeyForSet generalSetOps;
  private final HiveGeneralWithoutStoreKeyForSortedSet generalSortedSetOps;
  private final HiveGeneralWithoutStoreKey generalMapOps;

  private Hive( String name )
  {
    this.hiveName = name;
    this.generalKeyValueOps = new HiveGeneralWithoutStoreKey( hiveName, StoreType.KeyValue, hiveManagement );
    this.hiveKeyValue = new HiveKeyValue( hiveName, generalKeyValueOps );
    this.generalListOps = new HiveGeneralWithoutStoreKey( hiveName, StoreType.List, hiveManagement );
    this.generalSetOps = new HiveGeneralWithoutStoreKeyForSet( hiveName, StoreType.Set, hiveManagement );
    this.generalSortedSetOps = new HiveGeneralWithoutStoreKeyForSortedSet( hiveName, StoreType.SortedSet, hiveManagement );
    this.generalMapOps = new HiveGeneralWithoutStoreKey( hiveName, StoreType.Map, hiveManagement );
  }

  public static Hive getOrCreate( String name )
  {
    Hive hive = hives.get( name );
    if( hive == null )
    {
      hive = new Hive( name );
      hives.put( name, hive );
    }
    return hive;
  }

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

  public static CompletableFuture> getNames()
  {
    return hiveManagement.getNames();
  }

  public static CompletableFuture create( String name )
  {
    return hiveManagement.create( name );
  }

  public static CompletableFuture rename( String name, String newName )
  {
    return hiveManagement.rename( name, newName );
  }

  public static CompletableFuture delete( String name )
  {
    return hiveManagement.delete( name );
  }

  public static CompletableFuture deleteAll()
  {
    return hiveManagement.deleteAll();
  }

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

  public HiveKeyValue KeyValueStore()
  {
    return hiveKeyValue;
  }

  public HiveGeneralWithoutStoreKey ListStore()
  {
    return this.generalListOps;
  }

  public HiveList ListStore( String storeKey )
  {
    return ListStore( storeKey, Object.class );
  }

  public  HiveList ListStore( String storeKey, Class tClass )
  {
    final String hiveStoreKey = getComplexKey( storeKey );
    HiveList hiveList = listHives.get( hiveStoreKey );
    if( hiveList == null )
    {
      hiveList = new HiveList<>( hiveName, storeKey );
      listHives.put( hiveStoreKey, hiveList );
    }
    return (HiveList) hiveList;
  }

  public HiveGeneralWithoutStoreKeyForSet SetStore()
  {
    return this.generalSetOps;
  }

  public HiveSet SetStore( String storeKey )
  {
    return SetStore( storeKey, Object.class );
  }

  public  HiveSet SetStore( String storeKey, Class tClass )
  {
    final String hiveStoreKey = getComplexKey( storeKey );
    HiveSet hiveSet = setHives.get( hiveStoreKey );
    if( hiveSet == null )
    {
      hiveSet = new HiveSet<>( hiveName, storeKey );
      setHives.put( hiveStoreKey, hiveSet );
    }
    return (HiveSet) hiveSet;
  }

  public HiveGeneralWithoutStoreKeyForSortedSet SortedSetStore()
  {
    return this.generalSortedSetOps;
  }

  public HiveSortedSet SortedSetStore( String storeKey )
  {
    return SortedSetStore( storeKey, Object.class );
  }

  public  HiveSortedSet SortedSetStore( String storeKey, Class tClass )
  {
    final String hiveStoreKey = getComplexKey( storeKey );
    HiveSortedSet hiveSortedSet = sortedSetHives.get( hiveStoreKey );
    if( hiveSortedSet == null )
    {
      hiveSortedSet = new HiveSortedSet<>( hiveName, storeKey );
      sortedSetHives.put( hiveStoreKey, hiveSortedSet );
    }
    return (HiveSortedSet) hiveSortedSet;
  }

  public HiveGeneralWithoutStoreKey MapStore()
  {
    return this.generalMapOps;
  }

  public HiveMap MapStore( String storeKey )
  {
    return MapStore( storeKey, Object.class );
  }

  public  HiveMap MapStore( String storeKey, Class tClass )
  {
    final String hiveStoreKey = getComplexKey( storeKey );
    HiveMap hiveMap = mapHives.get( hiveStoreKey );
    if( hiveMap == null )
    {
      hiveMap = new HiveMap<>( hiveName, storeKey );
      mapHives.put( hiveStoreKey, hiveMap );
    }
    return (HiveMap) hiveMap;
  }

  private String getComplexKey( String storeKey )
  {
    return hiveName + "-" + storeKey;
  }
}