forked from apache/lucenenet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CannedBinaryTokenStream.cs
154 lines (135 loc) · 4.85 KB
/
CannedBinaryTokenStream.cs
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using Lucene.Net.Analysis.TokenAttributes;
using Lucene.Net.Util;
using System;
using System.Runtime.CompilerServices;
using Attribute = Lucene.Net.Util.Attribute;
namespace Lucene.Net.Analysis
{
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
/// <summary>
/// Represents a binary token. </summary>
public sealed class BinaryToken
{
internal BytesRef Term { get; set; }
internal int PosInc { get; set; }
internal int PosLen { get; set; }
internal int StartOffset { get; set; }
internal int EndOffset { get; set; }
public BinaryToken(BytesRef term)
{
this.Term = term;
this.PosInc = 1;
this.PosLen = 1;
}
public BinaryToken(BytesRef term, int posInc, int posLen)
{
this.Term = term;
this.PosInc = posInc;
this.PosLen = posLen;
}
}
/// <summary>
/// An attribute extending <see cref="ITermToBytesRefAttribute"/>
/// but exposing <see cref="BytesRef"/> property.
/// </summary>
public interface IBinaryTermAttribute : ITermToBytesRefAttribute
{
/// <summary>
/// Set the current binary value. </summary>
new BytesRef BytesRef { get; set; }
}
/// <summary>
/// Implementation for <see cref="IBinaryTermAttribute"/>. </summary>
public sealed class BinaryTermAttribute : Attribute, IBinaryTermAttribute
{
private readonly BytesRef bytes = new BytesRef();
public void FillBytesRef()
{
// no-op: we already filled externally during owner's incrementToken
}
public BytesRef BytesRef
{
get => bytes;
set => this.bytes.CopyBytes(value);
}
public override void Clear()
{
}
public override bool Equals(object other)
{
return other == this;
}
public override int GetHashCode()
{
return RuntimeHelpers.GetHashCode(this);
}
public override void CopyTo(IAttribute target)
{
BinaryTermAttribute other = (BinaryTermAttribute)target;
other.bytes.CopyBytes(bytes);
}
public override object Clone()
{
throw UnsupportedOperationException.Create();
}
}
/// <summary>
/// <see cref="TokenStream"/> from a canned list of binary (<see cref="BytesRef"/>-based)
/// tokens.
/// </summary>
public sealed class CannedBinaryTokenStream : TokenStream
{
// LUCENENET specific - de-nested BinaryToken
private readonly BinaryToken[] tokens;
private int upto = 0;
private readonly IBinaryTermAttribute termAtt;
private readonly IPositionIncrementAttribute posIncrAtt;
private readonly IPositionLengthAttribute posLengthAtt;
private readonly IOffsetAttribute offsetAtt;
// LUCENENET specific - de-nested IBinaryTermAttribute
// LUCENENET specific - de-nested BinaryTermAttribute
public CannedBinaryTokenStream(params BinaryToken[] tokens)
: base()
{
this.tokens = tokens;
termAtt = AddAttribute<IBinaryTermAttribute>();
posIncrAtt = AddAttribute<IPositionIncrementAttribute>();
posLengthAtt = AddAttribute<IPositionLengthAttribute>();
offsetAtt = AddAttribute<IOffsetAttribute>();
}
public override bool IncrementToken()
{
if (upto < tokens.Length)
{
BinaryToken token = tokens[upto++];
// TODO: can we just capture/restoreState so
// we get all attrs...?
ClearAttributes();
termAtt.BytesRef = token.Term;
posIncrAtt.PositionIncrement = token.PosInc;
posLengthAtt.PositionLength = token.PosLen;
offsetAtt.SetOffset(token.StartOffset, token.EndOffset);
return true;
}
else
{
return false;
}
}
}
}