    001/*
    002 * Copyright (c) 2007-2017 Xplenty, Inc. All Rights Reserved.
    003 *
    004 * Project and contact information: http://www.cascading.org/
    005 *
    006 * This file is part of the Cascading project.
    007 *
    008 * Licensed under the Apache License, Version 2.0 (the "License");
    009 * you may not use this file except in compliance with the License.
    010 * You may obtain a copy of the License at
    011 *
    012 *     http://www.apache.org/licenses/LICENSE-2.0
    013 *
    014 * Unless required by applicable law or agreed to in writing, software
    015 * distributed under the License is distributed on an "AS IS" BASIS,
    016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    017 * See the License for the specific language governing permissions and
    018 * limitations under the License.
    019 */
    020
    021package cascading.tap.partition;
    022
    023import java.util.regex.Pattern;
    024
    025import cascading.tuple.Fields;
    026import cascading.tuple.TupleEntry;
    027import cascading.util.Util;
    028
    029/**
    030 * DelimitedPartition is an implementation of the {@link Partition} interface that allows for simple
    031 * text delimited paths as partitions.
    032 * <p/>
    033 * For example, given the delimiter {@code -} (dash), a partition path will have dashes.
    034 * <p/>
    035 * Note the delimiter must not be naturally present in any of the values making up the partition.
    036 * <p>
    037 * The postfix value will be appended to any partition when created, and removed when the partition is parsed. Use
    038 * this value to add static filenames to the output path. It is safe to include the delimiter in the postfix value
    039 * (e.g '/somepath/filename.csv' where the delimiter is the default '/').
    040 */
    041public class DelimitedPartition implements Partition
    042  {
    043  public static final String PATH_DELIM = "/";
    044
    045  final Fields partitionFields;
    046  final String delimiter;
    047  final String postfix;
    048
    049  int numSplits;
    050
    051  transient Pattern pattern;
    052
    053  public DelimitedPartition( Fields partitionFields )
    054    {
    055    this( partitionFields, null, null );
    056    }
    057
    058  public DelimitedPartition( Fields partitionFields, String delimiter )
    059    {
    060    this( partitionFields, delimiter, null );
    061    }
    062
    063  public DelimitedPartition( Fields partitionFields, String delimiter, String postfix )
    064    {
    065    if( partitionFields == null )
    066      throw new IllegalArgumentException( "partitionFields must not be null" );
    067
    068    if( !partitionFields.isDefined() )
    069      throw new IllegalArgumentException( "partitionFields must be defined, got: " + partitionFields.printVerbose() );
    070
    071    this.partitionFields = partitionFields;
    072    this.delimiter = delimiter == null ? PATH_DELIM : delimiter;
    073
    074    postfix = Util.isEmpty( postfix ) ? null : postfix.startsWith( this.delimiter ) ? postfix.substring( this.delimiter.length() ) : postfix;
    075
    076    this.numSplits = partitionFields.size() + ( postfix != null ? postfix.split( this.delimiter ).length : 0 );
    077    this.postfix = postfix == null ? null : delimiter + postfix; // prefix the postfix w/ the delimiter
    078    }
    079
    080  @Override
    081  public int getPathDepth()
    082    {
    083    return numSplits;
    084    }
    085
    086  @Override
    087  public Fields getPartitionFields()
    088    {
    089    return partitionFields;
    090    }
    091
    092  protected Pattern getPattern()
    093    {
    094    if( pattern == null )
    095      pattern = Pattern.compile( delimiter );
    096
    097    return pattern;
    098    }
    099
    100  public String getDelimiter()
    101    {
    102    return delimiter;
    103    }
    104
    105  public String getPostfix()
    106    {
    107    return postfix;
    108    }
    109
    110  @Override
    111  public void toTuple( String partition, TupleEntry tupleEntry )
    112    {
    113    if( partition.startsWith( delimiter ) )
    114      partition = partition.substring( 1 );
    115
    116    String[] split = getPattern().split( partition, numSplits );
    117
    118    tupleEntry.setCanonicalValues( split, 0, partitionFields.size() );
    119    }
    120
    121  @Override
    122  public String toPartition( TupleEntry tupleEntry )
    123    {
    124    String partition = Util.join( tupleEntry.asIterableOf( String.class ), delimiter, true );
    125
    126    if( postfix != null )
    127      partition = partition + postfix; // delimiter prefixed in ctor
    128
    129    return partition;
    130    }
    131  }