-
Notifications
You must be signed in to change notification settings - Fork 242
/
LuceneFieldContext.java
128 lines (110 loc) · 3.97 KB
/
LuceneFieldContext.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
126
127
128
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* JBoss, Home of Professional Open Source
* Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors
* as indicated by the @authors tag. All rights reserved.
* See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License, v. 2.1.
* This program is distributed in the hope that it will be useful, but WITHOUT A
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License,
* v.2.1 along with this distribution; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
package org.hibernate.search.indexes.serialization.codex.spi;
import java.io.Reader;
import java.io.Serializable;
import org.apache.lucene.document.Field;
import org.hibernate.annotations.common.AssertionFailure;
import org.hibernate.search.SearchException;
import org.hibernate.search.indexes.serialization.codex.impl.CopyTokenStream;
import org.hibernate.search.indexes.serialization.codex.impl.SerializationHelper;
/**
* @author Emmanuel Bernard <emmanuel@hibernate.org>
*/
public class LuceneFieldContext {
private Field field;
public LuceneFieldContext(Field field) {
this.field = field;
}
public String getName() {
return field.name();
}
public SerializableStore getStore() {
return field.isStored() ? SerializableStore.YES : SerializableStore.NO;
}
public SerializableIndex getIndex() {
Field.Index index = Field.Index.toIndex( field.isIndexed(), field.isTokenized(), field.getOmitNorms() );
switch ( index ) {
case ANALYZED:
return SerializableIndex.ANALYZED;
case ANALYZED_NO_NORMS:
return SerializableIndex.ANALYZED_NO_NORMS;
case NO:
return SerializableIndex.NO;
case NOT_ANALYZED:
return SerializableIndex.NOT_ANALYZED;
case NOT_ANALYZED_NO_NORMS:
return SerializableIndex.NOT_ANALYZED_NO_NORMS;
default:
throw new SearchException( "Unable to convert Field.Index value into serializable Index: " + index);
}
}
public SerializableTermVector getTermVector() {
Field.TermVector vector = Field.TermVector.toTermVector( field.isTermVectorStored(), field.isStoreOffsetWithTermVector(), field.isStorePositionWithTermVector() );
switch ( vector ) {
case NO:
return SerializableTermVector.NO;
case WITH_OFFSETS:
return SerializableTermVector.WITH_OFFSETS;
case WITH_POSITIONS:
return SerializableTermVector.WITH_POSITIONS;
case WITH_POSITIONS_OFFSETS:
return SerializableTermVector.WITH_POSITIONS_OFFSETS;
case YES:
return SerializableTermVector.YES;
default:
throw new SearchException( "Unable to convert Field.TermVector value into serializable TermVector: " + vector);
}
}
public float getBoost() {
return field.getBoost();
}
public boolean isOmitNorms() {
return field.getOmitNorms();
}
public boolean isOmitTermFreqAndPositions() {
return field.getOmitTermFreqAndPositions();
}
public String getStringValue() {
return field.stringValue();
}
public byte[] getReaderValue() {
Reader reader = field.readerValue();
if (reader instanceof Serializable) {
return SerializationHelper.toByteArray( (Serializable) reader );
}
else {
throw new AssertionFailure( "Should not call getReaderValue for a non Serializable Reader" );
}
}
public SerializableTokenStream getTokenStream() {
return CopyTokenStream.buildSerializabletokenStream( field.tokenStreamValue() );
}
public byte[] getBinaryValue() {
return field.getBinaryValue();
}
public int getBinaryOffset() {
return field.getBinaryOffset();
}
public int getBinaryLength() {
return field.getBinaryLength();
}
}