Remove Namespace.populate()
[controller.git] / third-party / atomix / storage / src / main / java / io / atomix / utils / serializer / Namespace.java
index ff2afd09618a11b95c6dabcd9a30e89ab4954eef..dcad23d9f0f211332f58f7a32cb6452e8ae6bb8f 100644 (file)
@@ -69,14 +69,7 @@ public final class Namespace implements KryoFactory, KryoPool {
 
   private static final Logger LOGGER = LoggerFactory.getLogger(Namespace.class);
 
-  /**
-   * Default Kryo namespace.
-   */
-  public static final Namespace DEFAULT = builder().build();
-
-  private final KryoPool kryoPool = new KryoPool.Builder(this)
-      .softReferences()
-      .build();
+  private final KryoPool kryoPool = new KryoPool.Builder(this).softReferences().build();
 
   private final KryoOutputPool kryoOutputPool = new KryoOutputPool();
   private final KryoInputPool kryoInputPool = new KryoInputPool();
@@ -84,7 +77,6 @@ public final class Namespace implements KryoFactory, KryoPool {
   private final ImmutableList<RegistrationBlock> registeredBlocks;
 
   private final ClassLoader classLoader;
-  private final boolean registrationRequired;
   private final String friendlyName;
 
   /**
@@ -96,7 +88,6 @@ public final class Namespace implements KryoFactory, KryoPool {
     private List<Entry<Class<?>[], Serializer<?>>> types = new ArrayList<>();
     private List<RegistrationBlock> blocks = new ArrayList<>();
     private ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
-    private boolean registrationRequired = true;
 
     /**
      * Builds a {@link Namespace} instance.
@@ -117,7 +108,7 @@ public final class Namespace implements KryoFactory, KryoPool {
       if (!types.isEmpty()) {
         blocks.add(new RegistrationBlock(this.blockHeadId, types));
       }
-      return new Namespace(blocks, classLoader, registrationRequired, friendlyName).populate(1);
+      return new Namespace(blocks, classLoader, friendlyName);
     }
 
     /**
@@ -145,18 +136,6 @@ public final class Namespace implements KryoFactory, KryoPool {
       this.classLoader = classLoader;
       return this;
     }
-
-    /**
-     * Sets the registrationRequired flag.
-     *
-     * @param registrationRequired Kryo's registrationRequired flag
-     * @return this
-     * @see Kryo#setRegistrationRequired(boolean)
-     */
-    public Builder setRegistrationRequired(boolean registrationRequired) {
-      this.registrationRequired = registrationRequired;
-      return this;
-    }
   }
 
   /**
@@ -178,26 +157,13 @@ public final class Namespace implements KryoFactory, KryoPool {
   private Namespace(
       final List<RegistrationBlock> registeredTypes,
       ClassLoader classLoader,
-      boolean registrationRequired,
       String friendlyName) {
     this.registeredBlocks = ImmutableList.copyOf(registeredTypes);
-    this.registrationRequired = registrationRequired;
     this.classLoader = classLoader;
     this.friendlyName = requireNonNull(friendlyName);
-  }
 
-  /**
-   * Populates the Kryo pool.
-   *
-   * @param instances to add to the pool
-   * @return this
-   */
-  public Namespace populate(int instances) {
-
-    for (int i = 0; i < instances; ++i) {
-      release(create());
-    }
-    return this;
+    // Pre-populate with a single instance
+    release(create());
   }
 
   /**
@@ -342,21 +308,6 @@ public final class Namespace implements KryoFactory, KryoPool {
     }
   }
 
-  private String friendlyName() {
-    return friendlyName;
-  }
-
-  /**
-   * Gets the number of classes registered in this Kryo namespace.
-   *
-   * @return size of namespace
-   */
-  public int size() {
-    return (int) registeredBlocks.stream()
-        .flatMap(block -> block.types().stream())
-        .count();
-  }
-
   /**
    * Creates a Kryo instance.
    *
@@ -367,7 +318,7 @@ public final class Namespace implements KryoFactory, KryoPool {
     LOGGER.trace("Creating Kryo instance for {}", this);
     Kryo kryo = new Kryo();
     kryo.setClassLoader(classLoader);
-    kryo.setRegistrationRequired(registrationRequired);
+    kryo.setRegistrationRequired(true);
 
     // TODO rethink whether we want to use StdInstantiatorStrategy
     kryo.setInstantiatorStrategy(
@@ -406,7 +357,7 @@ public final class Namespace implements KryoFactory, KryoPool {
 
       if (!matches) {
         LOGGER.error("{}: Failed to register {} as {}, {} was already registered.",
-            friendlyName(), types, id, existing.getType());
+            friendlyName, types, id, existing.getType());
 
         throw new IllegalStateException(String.format(
             "Failed to register %s as %s, %s was already registered.",
@@ -429,7 +380,7 @@ public final class Namespace implements KryoFactory, KryoPool {
       if (r != null) {
         if (r.getId() != id) {
           LOGGER.debug("{}: {} already registered as {}. Skipping {}.",
-              friendlyName(), r.getType(), r.getId(), id);
+              friendlyName, r.getType(), r.getId(), id);
         }
         LOGGER.trace("{} registered as {}", r.getType(), r.getId());
       }