Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sql schema and entity class for ClaimsList
- Loading branch information
Showing
3 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
core/src/main/java/google/registry/schema/tmch/ClaimsList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Copyright 2019 The Nomulus Authors. All Rights Reserved. | ||
// | ||
// 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 google.registry.schema.tmch; | ||
|
||
import static com.google.common.base.Preconditions.checkState; | ||
|
||
import java.time.ZonedDateTime; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import javax.persistence.CollectionTable; | ||
import javax.persistence.Column; | ||
import javax.persistence.ElementCollection; | ||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.GenerationType; | ||
import javax.persistence.Id; | ||
import javax.persistence.JoinColumn; | ||
import javax.persistence.MapKeyColumn; | ||
import javax.persistence.Table; | ||
|
||
/** | ||
* A list of TMCH claims labels and their associated claims keys. | ||
* | ||
* <p>Note that the primary key of this entity is {@link #revisionId}, which is auto-generated by | ||
* the database. So, if a retry of insertion happens after the previous attempt unexpectedly | ||
* succeeds, we will end up with having two exact same claims list with only different {@link | ||
* #revisionId}. However, this is not an actual problem because we only use the claims list with | ||
* highest {@link #revisionId}. | ||
*/ | ||
@Entity | ||
@Table(name = "ClaimsList") | ||
public class ClaimsList { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "revision_id") | ||
private Long revisionId; | ||
|
||
@Column(name = "creation_timestamp", nullable = false) | ||
private ZonedDateTime creationTimestamp; | ||
|
||
@ElementCollection | ||
@CollectionTable( | ||
name = "ClaimsEntry", | ||
joinColumns = @JoinColumn(name = "revision_id", referencedColumnName = "revision_id")) | ||
@MapKeyColumn(name = "domain_label", nullable = false) | ||
@Column(name = "claim_key", nullable = false) | ||
private Map<String, String> labelsToKeys; | ||
|
||
private ClaimsList(ZonedDateTime creationTimestamp, Map<String, String> labelsToKeys) { | ||
this.creationTimestamp = creationTimestamp; | ||
this.labelsToKeys = labelsToKeys; | ||
} | ||
|
||
// Hibernate requires this default constructor. | ||
private ClaimsList() {} | ||
|
||
/** Constructs a {@link ClaimsList} object. */ | ||
public static ClaimsList create( | ||
ZonedDateTime creationTimestamp, Map<String, String> labelsToKeys) { | ||
return new ClaimsList(creationTimestamp, labelsToKeys); | ||
} | ||
|
||
/** Returns the revision id of this claims list, or throws exception if it is null. */ | ||
public Long getRevisionId() { | ||
checkState( | ||
revisionId != null, "revisionId is null because it is not persisted in the database"); | ||
return revisionId; | ||
} | ||
|
||
/** Returns the creation time of this claims list. */ | ||
public ZonedDateTime getCreationTimestamp() { | ||
return creationTimestamp; | ||
} | ||
|
||
/** Returns an {@link Map} mapping domain label to its lookup key. */ | ||
public Map<String, String> getLabelsToKeys() { | ||
return labelsToKeys; | ||
} | ||
|
||
/** Returns the claim key for a given domain if there is one, empty otherwise. */ | ||
public Optional<String> getClaimKey(String label) { | ||
return Optional.ofNullable(labelsToKeys.get(label)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
-- Copyright 2019 The Nomulus Authors. All Rights Reserved. | ||
-- | ||
-- 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. | ||
|
||
CREATE TABLE `ClaimsList` ( | ||
revision_id BIGSERIAL NOT NULL, | ||
creation_timestamp TIMESTAMPTZ NOT NULL, | ||
PRIMARY KEY (revision_id) | ||
); | ||
|
||
CREATE TABLE `ClaimsEntry` ( | ||
revision_id BIGSERIAL NOT NULL, | ||
claim_key TEXT NOT NULL, | ||
domain_label TEXT NOT NULL, | ||
PRIMARY KEY (revision_id, domain_label), | ||
FOREIGN KEY (revision_id) REFERENCES `ClaimsList`(revision_id) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters