View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shardingsphere.data.pipeline.core.ingest.position.type.pk;
19  
20  import com.google.common.base.Preconditions;
21  import com.google.common.base.Splitter;
22  import lombok.AccessLevel;
23  import lombok.NoArgsConstructor;
24  import org.apache.shardingsphere.data.pipeline.core.ingest.position.IngestPosition;
25  import org.apache.shardingsphere.data.pipeline.core.ingest.position.type.pk.type.IntegerPrimaryKeyIngestPosition;
26  import org.apache.shardingsphere.data.pipeline.core.ingest.position.type.pk.type.StringPrimaryKeyIngestPosition;
27  import org.apache.shardingsphere.data.pipeline.core.ingest.position.type.pk.type.UnsupportedKeyIngestPosition;
28  
29  import java.util.List;
30  
31  /**
32   * Primary key ingest position factory.
33   */
34  @NoArgsConstructor(access = AccessLevel.PRIVATE)
35  public final class PrimaryKeyIngestPositionFactory {
36      
37      /**
38       * Create new instance by string data.
39       *
40       * @param data string data
41       * @return primary key position
42       * @throws IllegalArgumentException illegal argument exception
43       */
44      public static IngestPosition newInstance(final String data) {
45          List<String> parts = Splitter.on(',').splitToList(data);
46          Preconditions.checkArgument(3 == parts.size(), "Unknown primary key position: " + data);
47          Preconditions.checkArgument(1 == parts.get(0).length(), "Invalid primary key position type: " + parts.get(0));
48          char type = parts.get(0).charAt(0);
49          String beginValue = parts.get(1);
50          String endValue = parts.get(2);
51          switch (type) {
52              case 'i':
53                  return new IntegerPrimaryKeyIngestPosition(Long.parseLong(beginValue), Long.parseLong(endValue));
54              case 's':
55                  return new StringPrimaryKeyIngestPosition(beginValue, endValue);
56              case 'u':
57                  return new UnsupportedKeyIngestPosition();
58              default:
59                  throw new IllegalArgumentException("Unknown primary key position type: " + type);
60          }
61      }
62      
63      /**
64       * New instance by begin value and end value.
65       *
66       * @param beginValue begin value
67       * @param endValue end value
68       * @return ingest position
69       */
70      public static IngestPosition newInstance(final Object beginValue, final Object endValue) {
71          if (beginValue instanceof Number) {
72              return new IntegerPrimaryKeyIngestPosition(((Number) beginValue).longValue(), null != endValue ? ((Number) endValue).longValue() : Long.MAX_VALUE);
73          }
74          if (beginValue instanceof CharSequence) {
75              return new StringPrimaryKeyIngestPosition(beginValue.toString(), null != endValue ? endValue.toString() : null);
76          }
77          // TODO support more types, e.g. byte[] (MySQL varbinary)
78          return new UnsupportedKeyIngestPosition();
79      }
80  }