forked from infinispan/infinispan
-
Notifications
You must be signed in to change notification settings - Fork 1
/
FileCacheKey.java
125 lines (106 loc) · 3.26 KB
/
FileCacheKey.java
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
package org.infinispan.lucene;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Set;
import org.infinispan.commons.io.UnsignedNumeric;
import org.infinispan.commons.marshall.AbstractExternalizer;
import org.infinispan.commons.util.Util;
/**
* Used as a key for file headers in a cache
*
* @since 4.0
* @author Lukasz Moren
* @author Sanne Grinovero
*/
public final class FileCacheKey implements IndexScopedKey {
private final String indexName;
private final String fileName;
private final int affinitySegmentId;
private final int hashCode;
public FileCacheKey(final String indexName, final String fileName, final int affinitySegmentId) {
if (fileName == null)
throw new IllegalArgumentException("filename must not be null");
this.indexName = indexName;
this.fileName = fileName;
this.affinitySegmentId = affinitySegmentId;
this.hashCode = generatedHashCode();
}
/**
* Get the indexName.
*
* @return the indexName.
*/
public String getIndexName() {
return indexName;
}
@Override
public int getAffinitySegmentId() {
return affinitySegmentId;
}
@Override
public Object accept(KeyVisitor visitor) throws Exception {
return visitor.visit(this);
}
/**
* Get the fileName.
*
* @return the fileName.
*/
public String getFileName() {
return fileName;
}
@Override
public int hashCode() {
return hashCode;
}
private int generatedHashCode() {
final int prime = 31;
int result = prime + fileName.hashCode();
return prime * result + indexName.hashCode();
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (FileCacheKey.class != obj.getClass())
return false;
FileCacheKey other = (FileCacheKey) obj;
if (!fileName.equals(other.fileName))
return false;
return indexName.equals(other.indexName);
}
/**
* Changing the encoding could break backwards compatibility
* @see LuceneKey2StringMapper#getKeyMapping(String)
*/
@Override
public String toString() {
return "M|" + fileName + "|"+ indexName + "|" + affinitySegmentId;
}
public static final class Externalizer extends AbstractExternalizer<FileCacheKey> {
@Override
public void writeObject(final ObjectOutput output, final FileCacheKey key) throws IOException {
output.writeUTF(key.indexName);
output.writeUTF(key.fileName);
UnsignedNumeric.writeUnsignedInt(output, key.affinitySegmentId);
}
@Override
public FileCacheKey readObject(final ObjectInput input) throws IOException {
String indexName = input.readUTF();
String fileName = input.readUTF();
int affinitySegmentId = UnsignedNumeric.readUnsignedInt(input);
return new FileCacheKey(indexName, fileName, affinitySegmentId);
}
@Override
public Integer getId() {
return ExternalizerIds.FILE_CACHE_KEY;
}
@Override
public Set<Class<? extends FileCacheKey>> getTypeClasses() {
return Util.<Class<? extends FileCacheKey>>asSet(FileCacheKey.class);
}
}
}