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

cz.o2.proxima.direct.bulk.NamingConvention Maven / Gradle / Ivy

/*
 * Copyright 2017-2023 O2 Czech Republic, a.s.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cz.o2.proxima.direct.bulk;

import com.google.common.base.Preconditions;
import cz.o2.proxima.annotations.Internal;
import cz.o2.proxima.util.Pair;
import java.io.Serializable;
import java.time.Duration;
import java.util.Collection;
import java.util.stream.Collectors;

/** Interface wrapping generic convention for naming files. */
@Internal
public interface NamingConvention extends Serializable {

  /**
   * Return default {@link NamingConvention} that is used with {@link BinaryBlobFormat} {@link
   * FileFormat}.
   *
   * @param rollTimePeriod time rolling interval in milliseconds.
   * @param prefix prefix of all names generated
   * @param suffix suffix of filenames
   * @return default naming convention with given settings
   */
  static NamingConvention defaultConvention(Duration rollTimePeriod, String prefix, String suffix) {
    return new DefaultNamingConvention(rollTimePeriod, prefix, suffix);
  }

  /**
   * Create a {@link NamingConvention} that adds prefix to each generated name of parent convention.
   *
   * @param prefix prefix to be added to generated names
   * @param parent the parent convention that generates names
   * @return prefixed {@link NamingConvention}
   */
  static NamingConvention prefixed(String prefix, NamingConvention parent) {
    return new NamingConvention() {

      private static final long serialVersionUID = 1L;

      @Override
      public String nameOf(long ts) {
        return prefix + parent.nameOf(ts);
      }

      @Override
      public Collection prefixesOf(long minTs, long maxTs) {
        return parent
            .prefixesOf(minTs, maxTs)
            .stream()
            .map(p -> prefix + p)
            .collect(Collectors.toList());
      }

      @Override
      public boolean isInRange(String name, long minTs, long maxTs) {
        Preconditions.checkArgument(name.startsWith(prefix));
        return parent.isInRange(name.substring(prefix.length()), minTs, maxTs);
      }

      @Override
      public Pair parseMinMaxTimestamp(String name) {
        Preconditions.checkArgument(name.startsWith(prefix));
        return parent.parseMinMaxTimestamp(name.substring(prefix.length()));
      }
    };
  }

  /**
   * Convert given timestamp to string representing atomic time range of this naming convention.
   *
   * @param ts timestamp to create name for
   * @return String representation of (prefixable) name representing time range
   */
  String nameOf(long ts);

  /**
   * Convert given time range to prefixes, at least one of which must all {@link Path Paths} in
   * given time range have.
   *
   * @param minTs minimal timestamp (inclusive)
   * @param maxTs maximal timestamp (exclusive)
   * @return String representation of name prefix
   */
  Collection prefixesOf(long minTs, long maxTs);

  /**
   * Validate that the given name belongs to given time range.
   *
   * @param name name of the {@link Path}
   * @param minTs minimal timestamp (inclusive)
   * @param maxTs maximal timestamp (exclusive)
   * @return {@code true} if the name belongs to given time range {@code false} otherwise
   */
  boolean isInRange(String name, long minTs, long maxTs);

  /**
   * Parse min and max timestamp from given string.
   *
   * @param name name generated by this convention
   * @return {@link Pair} of min and max timestamps
   */
  Pair parseMinMaxTimestamp(String name);
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy