|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright 2018-2020 The Feast Authors |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * https://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 | +package feast.core.model; |
| 18 | + |
| 19 | +import com.google.common.base.Objects; |
| 20 | +import com.google.protobuf.InvalidProtocolBufferException; |
| 21 | +import feast.proto.core.StoreProto; |
| 22 | +import java.io.ByteArrayOutputStream; |
| 23 | +import java.io.IOException; |
| 24 | +import java.io.Serializable; |
| 25 | +import javax.persistence.*; |
| 26 | +import javax.persistence.Entity; |
| 27 | +import lombok.AllArgsConstructor; |
| 28 | +import lombok.EqualsAndHashCode; |
| 29 | +import lombok.Getter; |
| 30 | +import lombok.Setter; |
| 31 | + |
| 32 | +/** |
| 33 | + * Represents {@link Store}s attached to one {@link Job}. Keeps copy of Store's proto to detect |
| 34 | + * changes in original Store. |
| 35 | + */ |
| 36 | +@Entity |
| 37 | +@Table( |
| 38 | + name = "jobs_stores", |
| 39 | + indexes = { |
| 40 | + @Index(name = "idx_jobs_stores_job_id", columnList = "job_id"), |
| 41 | + @Index(name = "idx_jobs_stores_store_name", columnList = "store_name") |
| 42 | + }) |
| 43 | +@Getter |
| 44 | +@Setter |
| 45 | +public class JobStore { |
| 46 | + @Embeddable |
| 47 | + @EqualsAndHashCode |
| 48 | + @AllArgsConstructor |
| 49 | + public static class JobStoreKey implements Serializable { |
| 50 | + public JobStoreKey() {} |
| 51 | + |
| 52 | + @Column(name = "job_id") |
| 53 | + String jobId; |
| 54 | + |
| 55 | + @Column(name = "store_name") |
| 56 | + String storeName; |
| 57 | + } |
| 58 | + |
| 59 | + @EmbeddedId private JobStoreKey id = new JobStoreKey(); |
| 60 | + |
| 61 | + @ManyToOne |
| 62 | + @MapsId("jobId") |
| 63 | + @JoinColumn(name = "job_id") |
| 64 | + private Job job; |
| 65 | + |
| 66 | + @Column(name = "store_proto", nullable = false) |
| 67 | + @Lob |
| 68 | + private byte[] storeProto; |
| 69 | + |
| 70 | + public JobStore() {} |
| 71 | + |
| 72 | + public JobStore(Job job, Store store) { |
| 73 | + this.job = job; |
| 74 | + this.id.storeName = store.getName(); |
| 75 | + try { |
| 76 | + setStoreProto(store.toProto()); |
| 77 | + } catch (InvalidProtocolBufferException e) { |
| 78 | + throw new RuntimeException("Couldn't convert Store to proto. Reason: %s", e.getCause()); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public StoreProto.Store getStoreProto() { |
| 83 | + try { |
| 84 | + return StoreProto.Store.parseFrom(this.storeProto); |
| 85 | + } catch (InvalidProtocolBufferException e) { |
| 86 | + return StoreProto.Store.newBuilder().build(); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + public void setStoreProto(StoreProto.Store storeProto) { |
| 91 | + ByteArrayOutputStream output = new ByteArrayOutputStream(); |
| 92 | + try { |
| 93 | + storeProto.writeTo(output); |
| 94 | + } catch (IOException e) { |
| 95 | + throw new RuntimeException( |
| 96 | + String.format("Couldn't write StoreProto to byteArray: %s", e.getCause())); |
| 97 | + } |
| 98 | + |
| 99 | + this.storeProto = output.toByteArray(); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public boolean equals(Object o) { |
| 104 | + if (this == o) return true; |
| 105 | + if (o == null || getClass() != o.getClass()) return false; |
| 106 | + JobStore jobStore = (JobStore) o; |
| 107 | + return Objects.equal(this.id, jobStore.id) |
| 108 | + && Objects.equal(this.storeProto, jobStore.storeProto); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public int hashCode() { |
| 113 | + return Objects.hashCode(this.id, this.storeProto); |
| 114 | + } |
| 115 | +} |
0 commit comments