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.event;
19  
20  import java.io.Serializable;
21  
22  public class CacheInvalidateEvent implements Serializable {
23    private final long regionId;
24    private final long storeId;
25    private final CacheType cacheType;
26    private boolean invalidateRegion;
27    private boolean invalidateStore;
28  
29    public CacheInvalidateEvent(
30        long regionId, long storeId, boolean updateRegion, boolean updateStore, CacheType type) {
31      this.regionId = regionId;
32      this.storeId = storeId;
33      this.cacheType = type;
34      if (updateRegion) {
35        invalidateRegion();
36      }
37  
38      if (updateStore) {
39        invalidateStore();
40      }
41    }
42  
43    public long getRegionId() {
44      return regionId;
45    }
46  
47    public long getStoreId() {
48      return storeId;
49    }
50  
51    @Override
52    public boolean equals(Object obj) {
53      if (obj == this) {
54        return true;
55      } else if (obj instanceof CacheInvalidateEvent) {
56        CacheInvalidateEvent event = (CacheInvalidateEvent) obj;
57        return event.getRegionId() == getRegionId()
58            && event.getStoreId() == getStoreId()
59            && event.getCacheType() == getCacheType();
60      }
61      return false;
62    }
63  
64    @Override
65    public int hashCode() {
66      int result = 1106;
67      result += result * 31 + getStoreId();
68      result += result * 31 + getRegionId();
69      result += result * 31 + getCacheType().name().hashCode();
70      return result;
71    }
72  
73    public void invalidateRegion() {
74      invalidateRegion = true;
75    }
76  
77    public void invalidateStore() {
78      invalidateStore = true;
79    }
80  
81    public boolean shouldUpdateRegion() {
82      return invalidateRegion;
83    }
84  
85    public boolean shouldUpdateStore() {
86      return invalidateStore;
87    }
88  
89    public CacheType getCacheType() {
90      return cacheType;
91    }
92  
93    @Override
94    public String toString() {
95      return String.format("RegionId=%d,StoreId=%d,Type=%s", regionId, storeId, cacheType.name());
96    }
97  
98    public enum CacheType implements Serializable {
99      REGION_STORE,
100     STORE,
101     REGION,
102     REQ_FAILED,
103     LEADER
104   }
105 }