-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathRow.java
More file actions
161 lines (141 loc) · 5.07 KB
/
Row.java
File metadata and controls
161 lines (141 loc) · 5.07 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright 2018-2019 The Feast Authors
*
* 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
*
* https://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 dev.feast;
import com.google.protobuf.ByteString;
import com.google.protobuf.Timestamp;
import com.google.protobuf.util.Timestamps;
import feast.proto.serving.ServingAPIProto.FieldStatus;
import feast.proto.types.ValueProto.Value;
import feast.proto.types.ValueProto.Value.ValCase;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@SuppressWarnings("UnusedReturnValue")
public class Row {
private Timestamp entity_timestamp;
private HashMap<String, Value> fields;
private HashMap<String, FieldStatus> fieldStatuses;
public static Row create() {
Row row = new Row();
row.entity_timestamp = Timestamps.fromMillis(System.currentTimeMillis());
row.fields = new HashMap<>();
row.fieldStatuses = new HashMap<>();
return row;
}
public Row setEntityTimestamp(Instant timestamp) {
entity_timestamp = Timestamps.fromMillis(timestamp.toEpochMilli());
return this;
}
public Timestamp getEntityTimestamp() {
return entity_timestamp;
}
public Row setEntityTimestamp(String dateTime) {
entity_timestamp = Timestamps.fromMillis(Instant.parse(dateTime).toEpochMilli());
return this;
}
public Row set(String fieldName, Object value) {
return this.set(fieldName, value, FieldStatus.PRESENT);
}
public Row set(String fieldName, Object value, FieldStatus status) {
String valueType = value.getClass().getCanonicalName();
switch (valueType) {
case "java.lang.Integer":
fields.put(fieldName, Value.newBuilder().setInt32Val((int) value).build());
break;
case "java.lang.Long":
fields.put(fieldName, Value.newBuilder().setInt64Val((long) value).build());
break;
case "java.lang.Float":
fields.put(fieldName, Value.newBuilder().setFloatVal((float) value).build());
break;
case "java.lang.Double":
fields.put(fieldName, Value.newBuilder().setDoubleVal((double) value).build());
break;
case "java.lang.String":
fields.put(fieldName, Value.newBuilder().setStringVal((String) value).build());
break;
case "byte[]":
fields.put(
fieldName, Value.newBuilder().setBytesVal(ByteString.copyFrom((byte[]) value)).build());
break;
case "feast.proto.types.ValueProto.Value":
fields.put(fieldName, (Value) value);
break;
default:
throw new IllegalArgumentException(
String.format(
"Type '%s' is unsupported in Feast. Please use one of these value types: Integer, Long, Float, Double, String, byte[].",
valueType));
}
fieldStatuses.put(fieldName, status);
return this;
}
public Map<String, Value> getFields() {
return fields;
}
public Integer getInt(String fieldName) {
return getValue(fieldName).map(Value::getInt32Val).orElse(null);
}
public Long getLong(String fieldName) {
return getValue(fieldName).map(Value::getInt64Val).orElse(null);
}
public Float getFloat(String fieldName) {
return getValue(fieldName).map(Value::getFloatVal).orElse(null);
}
public Double getDouble(String fieldName) {
return getValue(fieldName).map(Value::getDoubleVal).orElse(null);
}
public String getString(String fieldName) {
return getValue(fieldName).map(Value::getStringVal).orElse(null);
}
public byte[] getByte(String fieldName) {
return getValue(fieldName).map(Value::getBytesVal).map(ByteString::toByteArray).orElse(null);
}
public Map<String, FieldStatus> getStatuses() {
return fieldStatuses;
}
public FieldStatus getStatus(String fieldName) {
return fieldStatuses.get(fieldName);
}
@Override
public String toString() {
List<String> parts = new ArrayList<>();
fields.forEach(
(key, value) ->
parts.add(
key
+ ":"
+ (value.getValCase().equals(ValCase.VAL_NOT_SET)
? "NULL"
: value.toString().trim())));
return String.join(", ", parts);
}
private Optional<Value> getValue(String fieldName) {
if (!fields.containsKey(fieldName)) {
throw new IllegalArgumentException(
String.format("Row does not contain field '%s'", fieldName));
}
Value value = fields.get(fieldName);
if (value.getValCase().equals(ValCase.VAL_NOT_SET)) {
return Optional.empty();
}
return Optional.of(value);
}
}