]> git.proxmox.com Git - ceph.git/blobdiff - ceph/src/rocksdb/java/src/main/java/org/rocksdb/MutableColumnFamilyOptions.java
import 14.2.4 nautilus point release
[ceph.git] / ceph / src / rocksdb / java / src / main / java / org / rocksdb / MutableColumnFamilyOptions.java
index 3585318dbda7d7e7bbcfcb6aae529bfdc706188c..1d9ca08174cc312aed0708881b33549e38b55e3f 100644 (file)
@@ -7,27 +7,20 @@ package org.rocksdb;
 
 import java.util.*;
 
-public class MutableColumnFamilyOptions {
-  private final static String KEY_VALUE_PAIR_SEPARATOR = ";";
-  private final static char KEY_VALUE_SEPARATOR = '=';
-  private final static String INT_ARRAY_INT_SEPARATOR = ",";
-
-  private final String[] keys;
-  private final String[] values;
-
-  // user must use builder pattern, or parser
-  private MutableColumnFamilyOptions(final String keys[],
-      final String values[]) {
-    this.keys = keys;
-    this.values = values;
-  }
-
-  String[] getKeys() {
-    return keys;
-  }
+public class MutableColumnFamilyOptions
+    extends AbstractMutableOptions {
 
-  String[] getValues() {
-    return values;
+  /**
+   * User must use builder pattern, or parser.
+   *
+   * @param keys the keys
+   * @param values the values
+   *
+   * See {@link #builder()} and {@link #parse(String)}.
+   */
+  private MutableColumnFamilyOptions(final String[] keys,
+      final String[] values) {
+    super(keys, values);
   }
 
   /**
@@ -60,7 +53,7 @@ public class MutableColumnFamilyOptions {
     final MutableColumnFamilyOptionsBuilder builder =
         new MutableColumnFamilyOptionsBuilder();
 
-    final String options[] = str.trim().split(KEY_VALUE_PAIR_SEPARATOR);
+    final String[] options = str.trim().split(KEY_VALUE_PAIR_SEPARATOR);
     for(final String option : options) {
       final int equalsOffset = option.indexOf(KEY_VALUE_SEPARATOR);
       if(equalsOffset <= 0) {
@@ -69,12 +62,12 @@ public class MutableColumnFamilyOptions {
       }
 
       final String key = option.substring(0, equalsOffset);
-      if(key == null || key.isEmpty()) {
+      if(key.isEmpty()) {
         throw new IllegalArgumentException("options string is invalid");
       }
 
       final String value = option.substring(equalsOffset + 1);
-      if(value == null || value.isEmpty()) {
+      if(value.isEmpty()) {
         throw new IllegalArgumentException("options string is invalid");
       }
 
@@ -84,37 +77,7 @@ public class MutableColumnFamilyOptions {
     return builder;
   }
 
-  /**
-   * Returns a string representation
-   * of MutableColumnFamilyOptions which is
-   * suitable for consumption by {@link #parse(String)}
-   *
-   * @return String representation of MutableColumnFamilyOptions
-   */
-  @Override
-  public String toString() {
-    final StringBuilder buffer = new StringBuilder();
-    for(int i = 0; i < keys.length; i++) {
-      buffer
-          .append(keys[i])
-          .append(KEY_VALUE_SEPARATOR)
-          .append(values[i]);
-
-      if(i + 1 < keys.length) {
-        buffer.append(KEY_VALUE_PAIR_SEPARATOR);
-      }
-    }
-    return buffer.toString();
-  }
-
-  public enum ValueType {
-    DOUBLE,
-    LONG,
-    INT,
-    BOOLEAN,
-    INT_ARRAY,
-    ENUM
-  }
+  private interface MutableColumnFamilyOptionKey extends MutableOptionKey {}
 
   public enum MemtableOption implements MutableColumnFamilyOptionKey {
     write_buffer_size(ValueType.LONG),
@@ -153,7 +116,8 @@ public class MutableColumnFamilyOptions {
     target_file_size_multiplier(ValueType.INT),
     max_bytes_for_level_base(ValueType.LONG),
     max_bytes_for_level_multiplier(ValueType.INT),
-    max_bytes_for_level_multiplier_additional(ValueType.INT_ARRAY);
+    max_bytes_for_level_multiplier_additional(ValueType.INT_ARRAY),
+    ttl(ValueType.LONG);
 
     private final ValueType valueType;
     CompactionOption(final ValueType valueType) {
@@ -183,356 +147,9 @@ public class MutableColumnFamilyOptions {
     }
   }
 
-  private interface MutableColumnFamilyOptionKey {
-    String name();
-    ValueType getValueType();
-  }
-
-  private static abstract class MutableColumnFamilyOptionValue<T> {
-    protected final T value;
-
-    MutableColumnFamilyOptionValue(final T value) {
-      this.value = value;
-    }
-
-    abstract double asDouble() throws NumberFormatException;
-    abstract long asLong() throws NumberFormatException;
-    abstract int asInt() throws NumberFormatException;
-    abstract boolean asBoolean() throws IllegalStateException;
-    abstract int[] asIntArray() throws IllegalStateException;
-    abstract String asString();
-    abstract T asObject();
-  }
-
-  private static class MutableColumnFamilyOptionStringValue
-      extends MutableColumnFamilyOptionValue<String> {
-    MutableColumnFamilyOptionStringValue(final String value) {
-      super(value);
-    }
-
-    @Override
-    double asDouble() throws NumberFormatException {
-      return Double.parseDouble(value);
-    }
-
-    @Override
-    long asLong() throws NumberFormatException {
-      return Long.parseLong(value);
-    }
-
-    @Override
-    int asInt() throws NumberFormatException {
-      return Integer.parseInt(value);
-    }
-
-    @Override
-    boolean asBoolean() throws IllegalStateException {
-      return Boolean.parseBoolean(value);
-    }
-
-    @Override
-    int[] asIntArray() throws IllegalStateException {
-      throw new IllegalStateException("String is not applicable as int[]");
-    }
-
-    @Override
-    String asString() {
-      return value;
-    }
-
-    @Override
-    String asObject() {
-      return value;
-    }
-  }
-
-  private static class MutableColumnFamilyOptionDoubleValue
-      extends MutableColumnFamilyOptionValue<Double> {
-    MutableColumnFamilyOptionDoubleValue(final double value) {
-      super(value);
-    }
-
-    @Override
-    double asDouble() {
-      return value;
-    }
-
-    @Override
-    long asLong() throws NumberFormatException {
-      return value.longValue();
-    }
-
-    @Override
-    int asInt() throws NumberFormatException {
-      if(value > Integer.MAX_VALUE || value < Integer.MIN_VALUE) {
-        throw new NumberFormatException(
-            "double value lies outside the bounds of int");
-      }
-      return value.intValue();
-    }
-
-    @Override
-    boolean asBoolean() throws IllegalStateException {
-      throw new IllegalStateException(
-          "double is not applicable as boolean");
-    }
-
-    @Override
-    int[] asIntArray() throws IllegalStateException {
-      if(value > Integer.MAX_VALUE || value < Integer.MIN_VALUE) {
-        throw new NumberFormatException(
-            "double value lies outside the bounds of int");
-      }
-      return new int[] { value.intValue() };
-    }
-
-    @Override
-    String asString() {
-      return Double.toString(value);
-    }
-
-    @Override
-    Double asObject() {
-      return value;
-    }
-  }
-
-  private static class MutableColumnFamilyOptionLongValue
-      extends MutableColumnFamilyOptionValue<Long> {
-    MutableColumnFamilyOptionLongValue(final long value) {
-      super(value);
-    }
-
-    @Override
-    double asDouble() {
-      if(value > Double.MAX_VALUE || value < Double.MIN_VALUE) {
-        throw new NumberFormatException(
-            "long value lies outside the bounds of int");
-      }
-      return value.doubleValue();
-    }
-
-    @Override
-    long asLong() throws NumberFormatException {
-      return value;
-    }
-
-    @Override
-    int asInt() throws NumberFormatException {
-      if(value > Integer.MAX_VALUE || value < Integer.MIN_VALUE) {
-        throw new NumberFormatException(
-            "long value lies outside the bounds of int");
-      }
-      return value.intValue();
-    }
-
-    @Override
-    boolean asBoolean() throws IllegalStateException {
-      throw new IllegalStateException(
-          "long is not applicable as boolean");
-    }
-
-    @Override
-    int[] asIntArray() throws IllegalStateException {
-      if(value > Integer.MAX_VALUE || value < Integer.MIN_VALUE) {
-        throw new NumberFormatException(
-            "long value lies outside the bounds of int");
-      }
-      return new int[] { value.intValue() };
-    }
-
-    @Override
-    String asString() {
-      return Long.toString(value);
-    }
-
-    @Override
-    Long asObject() {
-      return value;
-    }
-  }
-
-  private static class MutableColumnFamilyOptionIntValue
-      extends MutableColumnFamilyOptionValue<Integer> {
-    MutableColumnFamilyOptionIntValue(final int value) {
-      super(value);
-    }
-
-    @Override
-    double asDouble() {
-      if(value > Double.MAX_VALUE || value < Double.MIN_VALUE) {
-        throw new NumberFormatException("int value lies outside the bounds of int");
-      }
-      return value.doubleValue();
-    }
-
-    @Override
-    long asLong() throws NumberFormatException {
-      return value;
-    }
-
-    @Override
-    int asInt() throws NumberFormatException {
-      return value;
-    }
-
-    @Override
-    boolean asBoolean() throws IllegalStateException {
-      throw new IllegalStateException("int is not applicable as boolean");
-    }
-
-    @Override
-    int[] asIntArray() throws IllegalStateException {
-      return new int[] { value };
-    }
-
-    @Override
-    String asString() {
-      return Integer.toString(value);
-    }
-
-    @Override
-    Integer asObject() {
-      return value;
-    }
-  }
-
-  private static class MutableColumnFamilyOptionBooleanValue
-      extends MutableColumnFamilyOptionValue<Boolean> {
-    MutableColumnFamilyOptionBooleanValue(final boolean value) {
-      super(value);
-    }
-
-    @Override
-    double asDouble() {
-      throw new NumberFormatException("boolean is not applicable as double");
-    }
-
-    @Override
-    long asLong() throws NumberFormatException {
-      throw new NumberFormatException("boolean is not applicable as Long");
-    }
-
-    @Override
-    int asInt() throws NumberFormatException {
-      throw new NumberFormatException("boolean is not applicable as int");
-    }
-
-    @Override
-    boolean asBoolean() {
-      return value;
-    }
-
-    @Override
-    int[] asIntArray() throws IllegalStateException {
-      throw new IllegalStateException("boolean is not applicable as int[]");
-    }
-
-    @Override
-    String asString() {
-      return Boolean.toString(value);
-    }
-
-    @Override
-    Boolean asObject() {
-      return value;
-    }
-  }
-
-  private static class MutableColumnFamilyOptionIntArrayValue
-      extends MutableColumnFamilyOptionValue<int[]> {
-    MutableColumnFamilyOptionIntArrayValue(final int[] value) {
-      super(value);
-    }
-
-    @Override
-    double asDouble() {
-      throw new NumberFormatException("int[] is not applicable as double");
-    }
-
-    @Override
-    long asLong() throws NumberFormatException {
-      throw new NumberFormatException("int[] is not applicable as Long");
-    }
-
-    @Override
-    int asInt() throws NumberFormatException {
-      throw new NumberFormatException("int[] is not applicable as int");
-    }
-
-    @Override
-    boolean asBoolean() {
-      throw new NumberFormatException("int[] is not applicable as boolean");
-    }
-
-    @Override
-    int[] asIntArray() throws IllegalStateException {
-      return value;
-    }
-
-    @Override
-    String asString() {
-      final StringBuilder builder = new StringBuilder();
-      for(int i = 0; i < value.length; i++) {
-        builder.append(Integer.toString(i));
-        if(i + 1 < value.length) {
-          builder.append(INT_ARRAY_INT_SEPARATOR);
-        }
-      }
-      return builder.toString();
-    }
-
-    @Override
-    int[] asObject() {
-      return value;
-    }
-  }
-
-  private static class MutableColumnFamilyOptionEnumValue<T extends Enum<T>>
-      extends MutableColumnFamilyOptionValue<T> {
-
-    MutableColumnFamilyOptionEnumValue(final T value) {
-      super(value);
-    }
-
-    @Override
-    double asDouble() throws NumberFormatException {
-      throw new NumberFormatException("Enum is not applicable as double");
-    }
-
-    @Override
-    long asLong() throws NumberFormatException {
-      throw new NumberFormatException("Enum is not applicable as long");
-    }
-
-    @Override
-    int asInt() throws NumberFormatException {
-      throw new NumberFormatException("Enum is not applicable as int");
-    }
-
-    @Override
-    boolean asBoolean() throws IllegalStateException {
-      throw new NumberFormatException("Enum is not applicable as boolean");
-    }
-
-    @Override
-    int[] asIntArray() throws IllegalStateException {
-      throw new NumberFormatException("Enum is not applicable as int[]");
-    }
-
-    @Override
-    String asString() {
-      return value.name();
-    }
-
-    @Override
-    T asObject() {
-      return value;
-    }
-  }
-
   public static class MutableColumnFamilyOptionsBuilder
-      implements MutableColumnFamilyOptionsInterface {
+      extends AbstractMutableOptionsBuilder<MutableColumnFamilyOptions, MutableColumnFamilyOptionsBuilder, MutableColumnFamilyOptionKey>
+      implements MutableColumnFamilyOptionsInterface<MutableColumnFamilyOptionsBuilder> {
 
     private final static Map<String, MutableColumnFamilyOptionKey> ALL_KEYS_LOOKUP = new HashMap<>();
     static {
@@ -549,179 +166,24 @@ public class MutableColumnFamilyOptions {
       }
     }
 
-    private final Map<MutableColumnFamilyOptionKey, MutableColumnFamilyOptionValue<?>> options = new LinkedHashMap<>();
-
-    public MutableColumnFamilyOptions build() {
-      final String keys[] = new String[options.size()];
-      final String values[] = new String[options.size()];
-
-      int i = 0;
-      for(final Map.Entry<MutableColumnFamilyOptionKey, MutableColumnFamilyOptionValue<?>> option : options.entrySet()) {
-        keys[i] = option.getKey().name();
-        values[i] = option.getValue().asString();
-        i++;
-      }
-
-      return new MutableColumnFamilyOptions(keys, values);
-    }
-
-    private MutableColumnFamilyOptionsBuilder setDouble(
-        final MutableColumnFamilyOptionKey key, final double value) {
-      if(key.getValueType() != ValueType.DOUBLE) {
-        throw new IllegalArgumentException(
-            key + " does not accept a double value");
-      }
-      options.put(key, new MutableColumnFamilyOptionDoubleValue(value));
-      return this;
-    }
-
-    private double getDouble(final MutableColumnFamilyOptionKey key)
-        throws NoSuchElementException, NumberFormatException {
-      final MutableColumnFamilyOptionValue<?> value = options.get(key);
-      if(value == null) {
-        throw new NoSuchElementException(key.name() + " has not been set");
-      }
-      return value.asDouble();
-    }
-
-    private MutableColumnFamilyOptionsBuilder setLong(
-        final MutableColumnFamilyOptionKey key, final long value) {
-      if(key.getValueType() != ValueType.LONG) {
-        throw new IllegalArgumentException(
-            key + " does not accept a long value");
-      }
-      options.put(key, new MutableColumnFamilyOptionLongValue(value));
-      return this;
-    }
-
-    private long getLong(final MutableColumnFamilyOptionKey key)
-        throws NoSuchElementException, NumberFormatException {
-      final MutableColumnFamilyOptionValue<?> value = options.get(key);
-      if(value == null) {
-        throw new NoSuchElementException(key.name() + " has not been set");
-      }
-      return value.asLong();
-    }
-
-    private MutableColumnFamilyOptionsBuilder setInt(
-        final MutableColumnFamilyOptionKey key, final int value) {
-      if(key.getValueType() != ValueType.INT) {
-        throw new IllegalArgumentException(
-            key + " does not accept an integer value");
-      }
-      options.put(key, new MutableColumnFamilyOptionIntValue(value));
-      return this;
-    }
-
-    private int getInt(final MutableColumnFamilyOptionKey key)
-        throws NoSuchElementException, NumberFormatException {
-      final MutableColumnFamilyOptionValue<?> value = options.get(key);
-      if(value == null) {
-        throw new NoSuchElementException(key.name() + " has not been set");
-      }
-      return value.asInt();
-    }
-
-    private MutableColumnFamilyOptionsBuilder setBoolean(
-        final MutableColumnFamilyOptionKey key, final boolean value) {
-      if(key.getValueType() != ValueType.BOOLEAN) {
-        throw new IllegalArgumentException(
-            key + " does not accept a boolean value");
-      }
-      options.put(key, new MutableColumnFamilyOptionBooleanValue(value));
-      return this;
-    }
-
-    private boolean getBoolean(final MutableColumnFamilyOptionKey key)
-        throws NoSuchElementException, NumberFormatException {
-      final MutableColumnFamilyOptionValue<?> value = options.get(key);
-      if(value == null) {
-        throw new NoSuchElementException(key.name() + " has not been set");
-      }
-      return value.asBoolean();
+    private MutableColumnFamilyOptionsBuilder() {
+      super();
     }
 
-    private MutableColumnFamilyOptionsBuilder setIntArray(
-        final MutableColumnFamilyOptionKey key, final int[] value) {
-      if(key.getValueType() != ValueType.INT_ARRAY) {
-        throw new IllegalArgumentException(
-            key + " does not accept an int array value");
-      }
-      options.put(key, new MutableColumnFamilyOptionIntArrayValue(value));
-      return this;
-    }
-
-    private int[] getIntArray(final MutableColumnFamilyOptionKey key)
-        throws NoSuchElementException, NumberFormatException {
-      final MutableColumnFamilyOptionValue<?> value = options.get(key);
-      if(value == null) {
-        throw new NoSuchElementException(key.name() + " has not been set");
-      }
-      return value.asIntArray();
-    }
-
-    private <T extends Enum<T>> MutableColumnFamilyOptionsBuilder setEnum(
-        final MutableColumnFamilyOptionKey key, final T value) {
-      if(key.getValueType() != ValueType.ENUM) {
-        throw new IllegalArgumentException(
-            key + " does not accept a Enum value");
-      }
-      options.put(key, new MutableColumnFamilyOptionEnumValue<T>(value));
+    @Override
+    protected MutableColumnFamilyOptionsBuilder self() {
       return this;
-
     }
 
-    private <T extends Enum<T>> T getEnum(final MutableColumnFamilyOptionKey key)
-        throws NoSuchElementException, NumberFormatException {
-      final MutableColumnFamilyOptionValue<?> value = options.get(key);
-      if(value == null) {
-        throw new NoSuchElementException(key.name() + " has not been set");
-      }
-
-      if(!(value instanceof MutableColumnFamilyOptionEnumValue)) {
-        throw new NoSuchElementException(key.name() + " is not of Enum type");
-      }
-
-      return ((MutableColumnFamilyOptionEnumValue<T>)value).asObject();
+    @Override
+    protected Map<String, MutableColumnFamilyOptionKey> allKeys() {
+      return ALL_KEYS_LOOKUP;
     }
 
-    public MutableColumnFamilyOptionsBuilder fromString(final String keyStr,
-        final String valueStr) throws IllegalArgumentException {
-      Objects.requireNonNull(keyStr);
-      Objects.requireNonNull(valueStr);
-
-      final MutableColumnFamilyOptionKey key = ALL_KEYS_LOOKUP.get(keyStr);
-      switch(key.getValueType()) {
-        case DOUBLE:
-          return setDouble(key, Double.parseDouble(valueStr));
-
-        case LONG:
-          return setLong(key, Long.parseLong(valueStr));
-
-        case INT:
-          return setInt(key, Integer.parseInt(valueStr));
-
-        case BOOLEAN:
-          return setBoolean(key, Boolean.parseBoolean(valueStr));
-
-        case INT_ARRAY:
-          final String[] strInts = valueStr
-              .trim().split(INT_ARRAY_INT_SEPARATOR);
-          if(strInts == null || strInts.length == 0) {
-            throw new IllegalArgumentException(
-                "int array value is not correctly formatted");
-          }
-
-          final int value[] = new int[strInts.length];
-          int i = 0;
-          for(final String strInt : strInts) {
-            value[i++] = Integer.parseInt(strInt);
-          }
-          return setIntArray(key, value);
-      }
-
-      throw new IllegalStateException(
-          key + " has unknown value type: " + key.getValueType());
+    @Override
+    protected MutableColumnFamilyOptions build(final String[] keys,
+        final String[] values) {
+      return new MutableColumnFamilyOptions(keys, values);
     }
 
     @Override
@@ -993,5 +455,15 @@ public class MutableColumnFamilyOptions {
     public boolean reportBgIoStats() {
       return getBoolean(MiscOption.report_bg_io_stats);
     }
+
+    @Override
+    public MutableColumnFamilyOptionsBuilder setTtl(final long ttl) {
+      return setLong(CompactionOption.ttl, ttl);
+    }
+
+    @Override
+    public long ttl() {
+      return getLong(CompactionOption.ttl);
+    }
   }
 }