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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document;
import com.yahoo.compress.CompressionType;
import com.yahoo.compress.Compressor;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* Date: Apr 15, 2008
*
* @author humbe
*/
public abstract class BaseStructDataType extends StructuredDataType {
protected Map<Integer, Field> fieldIds = new LinkedHashMap<>();
protected Map<String, Field> fields = new LinkedHashMap<>();
protected Compressor compressor = new Compressor(CompressionType.NONE);
BaseStructDataType(String name) {
super(name);
}
BaseStructDataType(int id, String name) {
super(id, name);
}
protected void assign(BaseStructDataType type) {
BaseStructDataType stype = type.clone();
fieldIds = stype.fieldIds;
fields = stype.fields;
}
@Override
public BaseStructDataType clone() {
BaseStructDataType type = (BaseStructDataType) super.clone();
type.fieldIds = new LinkedHashMap<>();
type.fields = new LinkedHashMap<>();
for (Field field : fieldIds.values()) {
type.fields.put(field.getName(), field);
type.fieldIds.put(field.getId(), field);
}
return type;
}
public void addField(Field field) {
if (fields.containsKey(field.getName())) {
throw new IllegalArgumentException("Struct " + getName() + " already contains field with name " + field.getName());
}
if (fieldIds.containsKey(field.getId())) {
throw new IllegalArgumentException("Struct " + getName() + " already contains field with id " + field.getId());
}
fields.put(field.getName(), field);
fieldIds.put(field.getId(), field);
}
public Field removeField(String fieldName) {
Field old = fields.remove(fieldName);
if (old != null) {
fieldIds.remove(old.getId());
}
return old;
}
public void clearFields() {
fieldIds.clear();
fields.clear();
}
@Override
public Field getField(String fieldName) {
return fields.get(fieldName);
}
@Override
public Field getField(int id) {
return fieldIds.get(id);
}
public boolean hasField(Field field) {
Field f = getField(field.getId());
return f != null && f.equals(field);
}
public boolean hasField(String name) {
return fields.containsKey(name);
}
@Override
public Collection<Field> getFields() {
return fields.values();
}
public int getFieldCount() {
return fields.size();
}
/** Returns the compressor to use to compress data of this type */
public Compressor getCompressor() { return compressor; }
/** Returns a view of the configuration of the compressor used to compress this type */
public CompressionConfig getCompressionConfig() {
// CompressionConfig accepts a percentage (but exposes a factor) ...
float compressionThresholdPercentage = (float)compressor.compressionThresholdFactor() * 100;
return new CompressionConfig(compressor.type(),
compressor.level(),
compressionThresholdPercentage,
compressor.compressMinSizeBytes());
}
/** Set the config to the compressor used to compress data of this type */
public void setCompressionConfig(CompressionConfig config) {
CompressionType type = config.type;
compressor = new Compressor(type,
config.compressionLevel,
config.thresholdFactor(),
(int)config.minsize);
}
}
|