1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
33
34 @NoArgsConstructor(access = AccessLevel.PRIVATE)
35 public final class PrimaryKeyIngestPositionFactory {
36
37
38
39
40
41
42
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
65
66
67
68
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
78 return new UnsupportedKeyIngestPosition();
79 }
80 }