-
Notifications
You must be signed in to change notification settings - Fork 68
/
CGCachingNameRemapper.java
90 lines (78 loc) · 3.3 KB
/
CGCachingNameRemapper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
* Copyright (c) 2022-2023 nosqlbench
*
* 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 io.nosqlbench.cqlgen.transformers;
import io.nosqlbench.nb.api.labels.NBLabeledElement;
import io.nosqlbench.nb.api.labels.NBLabels;
import io.nosqlbench.virtdata.library.basics.shared.from_long.to_string.Combinations;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.LongFunction;
public class CGCachingNameRemapper {
private LongFunction<String> namefunc;
private final Map<String,String> remapped = new HashMap<>();
private final Map<String,String> prefixmap = new HashMap<>();
private final Map<String,Long> indexmap = new HashMap<>();
public CGCachingNameRemapper() {
this.namefunc = new Combinations("a-z;a-z;a-z;a-z;a-z;a-z;");
}
public CGCachingNameRemapper(LongFunction<String> function) {
this.namefunc = function;
}
// public synchronized String nameForType(String type, String originalName, String prefix) {
// String canonical = type+"_"+originalName;
// return getOrCreateName(canonical, prefix);
// }
// public synchronized String nameFor(NBNamedElement element) {
// String prefix = prefixmap.get(element.getClass().getSimpleName());
// String canonical = element.getClass().getSimpleName()+"-"+element.getName();
// return getOrCreateName(canonical, prefix);
// }
private long indexforType(String type) {
long newvalue = indexmap.computeIfAbsent(type, t -> 0L)+1;
indexmap.put(type,newvalue);
return newvalue;
}
public synchronized String nameFor(Map<String,String> labels) {
String type = labels.get("type");
Objects.requireNonNull(type);
String name = labels.get("name");
Objects.requireNonNull(name);
String canonical = type+ '-' +name;
String prefix = prefixmap.getOrDefault(type,"");
if (!remapped.containsKey(canonical)) {
long indexForType= indexforType(type);
String newname = (null != prefix ?prefix:"")+ namefunc.apply(indexForType);
remapped.put(canonical,newname);
}
return remapped.get(canonical);
}
public synchronized String nameFor(NBLabeledElement element) {
NBLabels labels = element.getLabels();
return nameFor(labels.asMap());
}
// public Function<String, String> mapperForType(Labeled cqlTable, String prefix) {
// return in -> this.nameForType(cqlTable.getClass().getSimpleName(),in, prefix);
// }
//
public void setNamingFunction(LongFunction<String> namerFunc) {
this.namefunc = namerFunc;
}
public void setTypePrefixes(Map<String,String> prefixesByLabeledType) {
this.prefixmap.clear();
this.prefixmap.putAll(prefixesByLabeledType);
}
}