View Javadoc
1   /*
2    * Copyright 2021 TiKV Project Authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   */
17  
18  package org.tikv.common.meta;
19  
20  import com.fasterxml.jackson.annotation.JsonCreator;
21  import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
22  import com.fasterxml.jackson.annotation.JsonProperty;
23  import java.io.Serializable;
24  import java.util.List;
25  
26  @JsonIgnoreProperties(ignoreUnknown = true)
27  public class TiDBInfo implements Serializable {
28    private final long id;
29    private final String name;
30    private final String charset;
31    private final String collate;
32    private final List<TiTableInfo> tables;
33    private final org.tikv.common.meta.SchemaState schemaState;
34  
35    @JsonCreator
36    public TiDBInfo(
37        @JsonProperty("id") long id,
38        @JsonProperty("db_name") org.tikv.common.meta.CIStr name,
39        @JsonProperty("charset") String charset,
40        @JsonProperty("collate") String collate,
41        @JsonProperty("-") List<TiTableInfo> tables,
42        @JsonProperty("state") int schemaState) {
43      this.id = id;
44      this.name = name.getL();
45      this.charset = charset;
46      this.collate = collate;
47      this.tables = tables;
48      this.schemaState = org.tikv.common.meta.SchemaState.fromValue(schemaState);
49    }
50  
51    private TiDBInfo(
52        long id,
53        String name,
54        String charset,
55        String collate,
56        List<TiTableInfo> tables,
57        org.tikv.common.meta.SchemaState schemaState) {
58      this.id = id;
59      this.name = name;
60      this.charset = charset;
61      this.collate = collate;
62      this.tables = tables;
63      this.schemaState = schemaState;
64    }
65  
66    public TiDBInfo rename(String newName) {
67      return new TiDBInfo(id, newName, charset, collate, tables, schemaState);
68    }
69  
70    public long getId() {
71      return id;
72    }
73  
74    public String getName() {
75      return name;
76    }
77  
78    public String getCharset() {
79      return charset;
80    }
81  
82    public String getCollate() {
83      return collate;
84    }
85  
86    public List<TiTableInfo> getTables() {
87      return tables;
88    }
89  
90    org.tikv.common.meta.SchemaState getSchemaState() {
91      return schemaState;
92    }
93  
94    @Override
95    public boolean equals(Object other) {
96      if (other == this) {
97        return true;
98      }
99      if (!(other instanceof TiDBInfo)) {
100       return false;
101     }
102     TiDBInfo otherDB = (TiDBInfo) other;
103     return otherDB.getId() == getId() && otherDB.getName().equals(getName());
104   }
105 
106   @Override
107   public int hashCode() {
108     final int prime = 31;
109     int result = prime + Long.hashCode(getId());
110     return result * prime + getName().hashCode();
111   }
112 }