Guest User

Untitled

a guest
Aug 11th, 2012
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 86.98 KB | None | 0 0
  1. Index: lucene/benchmark/src/test/org/apache/lucene/benchmark/byTask/TestPerfTasksLogic.java
  2. ===================================================================
  3. --- lucene/benchmark/src/test/org/apache/lucene/benchmark/byTask/TestPerfTasksLogic.java (revision 1372046)
  4. +++ lucene/benchmark/src/test/org/apache/lucene/benchmark/byTask/TestPerfTasksLogic.java (working copy)
  5. @@ -43,7 +43,6 @@
  6. import org.apache.lucene.index.DirectoryReader;
  7. import org.apache.lucene.index.DocsEnum;
  8. import org.apache.lucene.index.Fields;
  9. -import org.apache.lucene.index.FieldsEnum;
  10. import org.apache.lucene.index.IndexReader;
  11. import org.apache.lucene.index.IndexWriter;
  12. import org.apache.lucene.index.IndexWriterConfig.OpenMode;
  13. @@ -493,13 +492,13 @@
  14.  
  15. int totalTokenCount2 = 0;
  16.  
  17. - FieldsEnum fields = MultiFields.getFields(reader).iterator();
  18. - String fieldName = null;
  19. - while((fieldName = fields.next()) != null) {
  20. + Fields fields = MultiFields.getFields(reader);
  21. +
  22. + for (String fieldName : fields) {
  23. if (fieldName.equals(DocMaker.ID_FIELD) || fieldName.equals(DocMaker.DATE_MSEC_FIELD) || fieldName.equals(DocMaker.TIME_SEC_FIELD)) {
  24. continue;
  25. }
  26. - Terms terms = fields.terms();
  27. + Terms terms = fields.terms(fieldName);
  28. if (terms == null) {
  29. continue;
  30. }
  31. Index: lucene/misc/src/java/org/apache/lucene/misc/HighFreqTerms.java
  32. ===================================================================
  33. --- lucene/misc/src/java/org/apache/lucene/misc/HighFreqTerms.java (revision 1372046)
  34. +++ lucene/misc/src/java/org/apache/lucene/misc/HighFreqTerms.java (working copy)
  35. @@ -25,7 +25,6 @@
  36. import org.apache.lucene.index.Fields;
  37. import org.apache.lucene.index.ReaderUtil;
  38. import org.apache.lucene.index.TermsEnum;
  39. -import org.apache.lucene.index.FieldsEnum;
  40. import org.apache.lucene.index.Terms;
  41. import org.apache.lucene.index.DocsEnum;
  42. import org.apache.lucene.search.DocIdSetIterator;
  43. @@ -133,16 +132,10 @@
  44. throw new RuntimeException("no fields found for this index");
  45. }
  46. tiq = new TermStatsQueue(numTerms);
  47. - FieldsEnum fieldsEnum = fields.iterator();
  48. - while (true) {
  49. - field = fieldsEnum.next();
  50. - if (field != null) {
  51. - Terms terms = fieldsEnum.terms();
  52. - if (terms != null) {
  53. - tiq.fill(field, terms.iterator(null));
  54. - }
  55. - } else {
  56. - break;
  57. + for (String fieldName : fields) {
  58. + Terms terms = fields.terms(fieldName);
  59. + if (terms != null) {
  60. + tiq.fill(fieldName, terms.iterator(null));
  61. }
  62. }
  63. }
  64. Index: lucene/test-framework/src/java/org/apache/lucene/codecs/asserting/AssertingPostingsFormat.java
  65. ===================================================================
  66. --- lucene/test-framework/src/java/org/apache/lucene/codecs/asserting/AssertingPostingsFormat.java (revision 1372046)
  67. +++ lucene/test-framework/src/java/org/apache/lucene/codecs/asserting/AssertingPostingsFormat.java (working copy)
  68. @@ -19,6 +19,7 @@
  69.  
  70. import java.io.IOException;
  71. import java.util.Comparator;
  72. +import java.util.Iterator;
  73.  
  74. import org.apache.lucene.codecs.FieldsConsumer;
  75. import org.apache.lucene.codecs.FieldsProducer;
  76. @@ -30,7 +31,6 @@
  77. import org.apache.lucene.index.AssertingAtomicReader;
  78. import org.apache.lucene.index.FieldInfo;
  79. import org.apache.lucene.index.FieldInfo.IndexOptions;
  80. -import org.apache.lucene.index.FieldsEnum;
  81. import org.apache.lucene.index.SegmentReadState;
  82. import org.apache.lucene.index.SegmentWriteState;
  83. import org.apache.lucene.index.Terms;
  84. @@ -70,10 +70,10 @@
  85. }
  86.  
  87. @Override
  88. - public FieldsEnum iterator() throws IOException {
  89. - FieldsEnum iterator = in.iterator();
  90. + public Iterator<String> iterator() {
  91. + Iterator<String> iterator = in.iterator();
  92. assert iterator != null;
  93. - return new AssertingAtomicReader.AssertingFieldsEnum(iterator);
  94. + return iterator;
  95. }
  96.  
  97. @Override
  98. Index: lucene/test-framework/src/java/org/apache/lucene/codecs/ramonly/RAMOnlyPostingsFormat.java
  99. ===================================================================
  100. --- lucene/test-framework/src/java/org/apache/lucene/codecs/ramonly/RAMOnlyPostingsFormat.java (revision 1372046)
  101. +++ lucene/test-framework/src/java/org/apache/lucene/codecs/ramonly/RAMOnlyPostingsFormat.java (working copy)
  102. @@ -39,7 +39,6 @@
  103. import org.apache.lucene.index.DocsEnum;
  104. import org.apache.lucene.index.FieldInfo.IndexOptions;
  105. import org.apache.lucene.index.FieldInfo;
  106. -import org.apache.lucene.index.FieldsEnum;
  107. import org.apache.lucene.index.IndexFileNames;
  108. import org.apache.lucene.index.SegmentReadState;
  109. import org.apache.lucene.index.SegmentWriteState;
  110. @@ -50,6 +49,7 @@
  111. import org.apache.lucene.util.Bits;
  112. import org.apache.lucene.util.BytesRef;
  113. import org.apache.lucene.util.IOUtils;
  114. +import org.apache.lucene.util.UnmodifiableIterator;
  115.  
  116. /** Stores all postings data in RAM, but writes a small
  117. * token (header + single int) to identify which "slot" the
  118. @@ -112,8 +112,8 @@
  119. }
  120.  
  121. @Override
  122. - public FieldsEnum iterator() {
  123. - return new RAMFieldsEnum(this);
  124. + public Iterator<String> iterator() {
  125. + return new UnmodifiableIterator<String>(fieldToTerms.keySet().iterator());
  126. }
  127.  
  128. @Override
  129. @@ -303,33 +303,6 @@
  130. }
  131. }
  132.  
  133. - // Classes for reading from the postings state
  134. - static class RAMFieldsEnum extends FieldsEnum {
  135. - private final RAMPostings postings;
  136. - private final Iterator<String> it;
  137. - private String current;
  138. -
  139. - public RAMFieldsEnum(RAMPostings postings) {
  140. - this.postings = postings;
  141. - this.it = postings.fieldToTerms.keySet().iterator();
  142. - }
  143. -
  144. - @Override
  145. - public String next() {
  146. - if (it.hasNext()) {
  147. - current = it.next();
  148. - } else {
  149. - current = null;
  150. - }
  151. - return current;
  152. - }
  153. -
  154. - @Override
  155. - public Terms terms() {
  156. - return postings.fieldToTerms.get(current);
  157. - }
  158. - }
  159. -
  160. static class RAMTermsEnum extends TermsEnum {
  161. Iterator<String> it;
  162. String current;
  163. Index: lucene/test-framework/src/java/org/apache/lucene/index/FieldFilterAtomicReader.java
  164. ===================================================================
  165. --- lucene/test-framework/src/java/org/apache/lucene/index/FieldFilterAtomicReader.java (revision 1372046)
  166. +++ lucene/test-framework/src/java/org/apache/lucene/index/FieldFilterAtomicReader.java (working copy)
  167. @@ -19,6 +19,8 @@
  168.  
  169. import java.io.IOException;
  170. import java.util.ArrayList;
  171. +import java.util.Iterator;
  172. +import java.util.NoSuchElementException;
  173. import java.util.Set;
  174.  
  175. public final class FieldFilterAtomicReader extends FilterAtomicReader {
  176. @@ -125,15 +127,16 @@
  177. }
  178.  
  179. private class FieldFilterFields extends FilterFields {
  180. +
  181. public FieldFilterFields(Fields in) {
  182. super(in);
  183. }
  184.  
  185. @Override
  186. - public int size() throws IOException {
  187. + public int size() {
  188. // TODO: add faster implementation!
  189. int c = 0;
  190. - final FieldsEnum it = iterator();
  191. + final Iterator<String> it = iterator();
  192. while (it.next() != null) {
  193. c++;
  194. }
  195. @@ -141,16 +144,46 @@
  196. }
  197.  
  198. @Override
  199. - public FieldsEnum iterator() throws IOException {
  200. - return new FilterFieldsEnum(super.iterator()) {
  201. + public Iterator<String> iterator() {
  202. + final Iterator<String> in = super.iterator();
  203. + return new Iterator<String>() {
  204. + String cached = null;
  205. +
  206. @Override
  207. - public String next() throws IOException {
  208. - String f;
  209. - while ((f = super.next()) != null) {
  210. - if (hasField(f)) return f;
  211. + public String next() {
  212. + if (cached != null) {
  213. + String next = cached;
  214. + cached = null;
  215. + return next;
  216. + } else {
  217. + String next = doNext();
  218. + if (next == null) {
  219. + throw new NoSuchElementException();
  220. + } else {
  221. + return next;
  222. + }
  223. }
  224. + }
  225. +
  226. + @Override
  227. + public boolean hasNext() {
  228. + return cached != null || (cached = doNext()) != null;
  229. + }
  230. +
  231. + private String doNext() {
  232. + while (in.hasNext()) {
  233. + String field = in.next();
  234. + if (hasField(field)) {
  235. + return field;
  236. + }
  237. + }
  238. return null;
  239. - }
  240. + }
  241. +
  242. + @Override
  243. + public void remove() {
  244. + throw new UnsupportedOperationException();
  245. + }
  246. };
  247. }
  248.  
  249. Index: lucene/test-framework/src/java/org/apache/lucene/index/AssertingAtomicReader.java
  250. ===================================================================
  251. --- lucene/test-framework/src/java/org/apache/lucene/index/AssertingAtomicReader.java (revision 1372046)
  252. +++ lucene/test-framework/src/java/org/apache/lucene/index/AssertingAtomicReader.java (working copy)
  253. @@ -1,6 +1,7 @@
  254. package org.apache.lucene.index;
  255.  
  256. import java.io.IOException;
  257. +import java.util.Iterator;
  258.  
  259. import org.apache.lucene.search.DocIdSetIterator;
  260. import org.apache.lucene.util.Bits;
  261. @@ -60,10 +61,10 @@
  262. }
  263.  
  264. @Override
  265. - public FieldsEnum iterator() throws IOException {
  266. - FieldsEnum fieldsEnum = super.iterator();
  267. - assert fieldsEnum != null;
  268. - return new AssertingFieldsEnum(fieldsEnum);
  269. + public Iterator<String> iterator() {
  270. + Iterator<String> iterator = super.iterator();
  271. + assert iterator != null;
  272. + return iterator;
  273. }
  274.  
  275. @Override
  276. @@ -74,21 +75,6 @@
  277. }
  278.  
  279. /**
  280. - * Wraps a FieldsEnum but with additional asserts
  281. - */
  282. - public static class AssertingFieldsEnum extends FilterFieldsEnum {
  283. - public AssertingFieldsEnum(FieldsEnum in) {
  284. - super(in);
  285. - }
  286. -
  287. - @Override
  288. - public Terms terms() throws IOException {
  289. - Terms terms = super.terms();
  290. - return terms == null ? null : new AssertingTerms(terms);
  291. - }
  292. - }
  293. -
  294. - /**
  295. * Wraps a Terms but with additional asserts
  296. */
  297. public static class AssertingTerms extends FilterTerms {
  298. Index: lucene/core/src/test/org/apache/lucene/search/TestTermVectors.java
  299. ===================================================================
  300. --- lucene/core/src/test/org/apache/lucene/search/TestTermVectors.java (revision 1372046)
  301. +++ lucene/core/src/test/org/apache/lucene/search/TestTermVectors.java (working copy)
  302. @@ -19,6 +19,7 @@
  303.  
  304. import java.io.IOException;
  305. import java.util.HashMap;
  306. +import java.util.Iterator;
  307. import java.util.Map;
  308.  
  309. import org.apache.lucene.analysis.MockAnalyzer;
  310. @@ -124,13 +125,13 @@
  311. assertEquals(4, v.size());
  312. String[] expectedFields = new String[]{"a", "b", "c", "x"};
  313. int[] expectedPositions = new int[]{1, 2, 0};
  314. - FieldsEnum fieldsEnum = v.iterator();
  315. + Iterator<String> fieldsEnum = v.iterator();
  316. for(int i=0;i<expectedFields.length;i++) {
  317. assertEquals(expectedFields[i], fieldsEnum.next());
  318. assertEquals(3, v.terms(expectedFields[i]).size());
  319.  
  320. DocsAndPositionsEnum dpEnum = null;
  321. - Terms terms = fieldsEnum.terms();
  322. + Terms terms = v.terms(expectedFields[i]);
  323. assertNotNull(terms);
  324. TermsEnum termsEnum = terms.iterator(null);
  325. assertEquals("content", termsEnum.next().utf8ToString());
  326. @@ -251,11 +252,11 @@
  327. writer.close();
  328. IndexSearcher knownSearcher = newSearcher(reader);
  329. knownSearcher.setSimilarity(new DefaultSimilarity());
  330. - FieldsEnum fields = MultiFields.getFields(knownSearcher.reader).iterator();
  331. + Fields fields = MultiFields.getFields(knownSearcher.reader);
  332.  
  333. DocsEnum docs = null;
  334. - while(fields.next() != null) {
  335. - Terms terms = fields.terms();
  336. + for (String fieldName : fields) {
  337. + Terms terms = fields.terms(fieldName);
  338. assertNotNull(terms); // NOTE: kinda sketchy assumptions, but ideally we would fix fieldsenum api...
  339. TermsEnum termsEnum = terms.iterator(null);
  340.  
  341. Index: lucene/core/src/test/org/apache/lucene/search/TestMultiThreadTermVectors.java
  342. ===================================================================
  343. --- lucene/core/src/test/org/apache/lucene/search/TestMultiThreadTermVectors.java (revision 1372046)
  344. +++ lucene/core/src/test/org/apache/lucene/search/TestMultiThreadTermVectors.java (working copy)
  345. @@ -23,7 +23,6 @@
  346. import org.apache.lucene.document.*;
  347. import org.apache.lucene.index.DirectoryReader;
  348. import org.apache.lucene.index.Fields;
  349. -import org.apache.lucene.index.FieldsEnum;
  350. import org.apache.lucene.index.IndexReader;
  351. import org.apache.lucene.index.IndexWriter;
  352. import org.apache.lucene.index.Terms;
  353. @@ -179,9 +178,8 @@
  354. }
  355.  
  356. private void verifyVectors(Fields vectors, int num) throws IOException {
  357. - FieldsEnum fieldsEnum = vectors.iterator();
  358. - while(fieldsEnum.next() != null) {
  359. - Terms terms = fieldsEnum.terms();
  360. + for (String field : vectors) {
  361. + Terms terms = vectors.terms(field);
  362. assert terms != null;
  363. verifyVector(terms.iterator(null), num);
  364. }
  365. Index: lucene/core/src/test/org/apache/lucene/index/TestPrefixCodedTerms.java
  366. ===================================================================
  367. --- lucene/core/src/test/org/apache/lucene/index/TestPrefixCodedTerms.java (revision 1372046)
  368. +++ lucene/core/src/test/org/apache/lucene/index/TestPrefixCodedTerms.java (working copy)
  369. @@ -25,6 +25,7 @@
  370. import java.util.TreeSet;
  371.  
  372. import org.apache.lucene.util.LuceneTestCase;
  373. +import org.apache.lucene.util.MergedIterator;
  374. import org.apache.lucene.util._TestUtil;
  375.  
  376. public class TestPrefixCodedTerms extends LuceneTestCase {
  377. @@ -67,17 +68,20 @@
  378. assertFalse(expected.hasNext());
  379. }
  380.  
  381. + @SuppressWarnings("unchecked")
  382. public void testMergeEmpty() {
  383. List<Iterator<Term>> subs = Collections.emptyList();
  384. - assertFalse(CoalescedDeletes.mergedIterator(subs).hasNext());
  385. + Iterator<Term> merged = new MergedIterator<Term>(subs.toArray(new Iterator[0]));
  386. + assertFalse(merged.hasNext());
  387.  
  388. subs = new ArrayList<Iterator<Term>>();
  389. subs.add(new PrefixCodedTerms.Builder().finish().iterator());
  390. subs.add(new PrefixCodedTerms.Builder().finish().iterator());
  391. - Iterator<Term> merged = CoalescedDeletes.mergedIterator(subs);
  392. + merged = new MergedIterator<Term>(subs.toArray(new Iterator[0]));
  393. assertFalse(merged.hasNext());
  394. }
  395.  
  396. + @SuppressWarnings("unchecked")
  397. public void testMergeOne() {
  398. Term t1 = new Term("foo", "a");
  399. PrefixCodedTerms.Builder b1 = new PrefixCodedTerms.Builder();
  400. @@ -93,13 +97,14 @@
  401. subs.add(pb1.iterator());
  402. subs.add(pb2.iterator());
  403.  
  404. - Iterator<Term> merged = CoalescedDeletes.mergedIterator(subs);
  405. + Iterator<Term> merged = new MergedIterator<Term>(subs.toArray(new Iterator[0]));
  406. assertTrue(merged.hasNext());
  407. assertEquals(t1, merged.next());
  408. assertTrue(merged.hasNext());
  409. assertEquals(t2, merged.next());
  410. }
  411.  
  412. + @SuppressWarnings("unchecked")
  413. public void testMergeRandom() {
  414. PrefixCodedTerms pb[] = new PrefixCodedTerms[_TestUtil.nextInt(random(), 2, 10)];
  415. Set<Term> superSet = new TreeSet<Term>();
  416. @@ -126,7 +131,7 @@
  417. }
  418.  
  419. Iterator<Term> expected = superSet.iterator();
  420. - Iterator<Term> actual = CoalescedDeletes.mergedIterator(subs);
  421. + Iterator<Term> actual = new MergedIterator<Term>(subs.toArray(new Iterator[0]));
  422. while (actual.hasNext()) {
  423. assertTrue(expected.hasNext());
  424. assertEquals(expected.next(), actual.next());
  425. Index: lucene/core/src/test/org/apache/lucene/index/TestSegmentReader.java
  426. ===================================================================
  427. --- lucene/core/src/test/org/apache/lucene/index/TestSegmentReader.java (revision 1372046)
  428. +++ lucene/core/src/test/org/apache/lucene/index/TestSegmentReader.java (working copy)
  429. @@ -114,10 +114,9 @@
  430. }
  431.  
  432. public void testTerms() throws IOException {
  433. - FieldsEnum fields = MultiFields.getFields(reader).iterator();
  434. - String field;
  435. - while((field = fields.next()) != null) {
  436. - Terms terms = fields.terms();
  437. + Fields fields = MultiFields.getFields(reader);
  438. + for (String field : fields) {
  439. + Terms terms = fields.terms(field);
  440. assertNotNull(terms);
  441. TermsEnum termsEnum = terms.iterator(null);
  442. while(termsEnum.next() != null) {
  443. Index: lucene/core/src/test/org/apache/lucene/index/TestDoc.java
  444. ===================================================================
  445. --- lucene/core/src/test/org/apache/lucene/index/TestDoc.java (revision 1372046)
  446. +++ lucene/core/src/test/org/apache/lucene/index/TestDoc.java (working copy)
  447. @@ -32,7 +32,6 @@
  448. import org.apache.lucene.analysis.MockAnalyzer;
  449. import org.apache.lucene.codecs.Codec;
  450. import org.apache.lucene.document.Document;
  451. -import org.apache.lucene.document.Field;
  452. import org.apache.lucene.document.TextField;
  453. import org.apache.lucene.index.IndexWriterConfig.OpenMode;
  454. import org.apache.lucene.search.DocIdSetIterator;
  455. @@ -236,10 +235,9 @@
  456. for (int i = 0; i < reader.numDocs(); i++)
  457. out.println(reader.document(i));
  458.  
  459. - FieldsEnum fis = reader.fields().iterator();
  460. - String field = fis.next();
  461. - while(field != null) {
  462. - Terms terms = fis.terms();
  463. + Fields fields = reader.fields();
  464. + for (String field : fields) {
  465. + Terms terms = fields.terms(field);
  466. assertNotNull(terms);
  467. TermsEnum tis = terms.iterator(null);
  468. while(tis.next() != null) {
  469. @@ -259,7 +257,6 @@
  470. out.println("");
  471. }
  472. }
  473. - field = fis.next();
  474. }
  475. reader.close();
  476. }
  477. Index: lucene/core/src/test/org/apache/lucene/index/TestStressIndexing2.java
  478. ===================================================================
  479. --- lucene/core/src/test/org/apache/lucene/index/TestStressIndexing2.java (revision 1372046)
  480. +++ lucene/core/src/test/org/apache/lucene/index/TestStressIndexing2.java (working copy)
  481. @@ -396,13 +396,11 @@
  482. Fields tv1 = r1.getTermVectors(id1);
  483. System.out.println(" d1=" + tv1);
  484. if (tv1 != null) {
  485. - FieldsEnum fieldsEnum = tv1.iterator();
  486. - String field;
  487. DocsAndPositionsEnum dpEnum = null;
  488. DocsEnum dEnum = null;
  489. - while ((field=fieldsEnum.next()) != null) {
  490. + for (String field : tv1) {
  491. System.out.println(" " + field + ":");
  492. - Terms terms3 = fieldsEnum.terms();
  493. + Terms terms3 = tv1.terms(field);
  494. assertNotNull(terms3);
  495. TermsEnum termsEnum3 = terms3.iterator(null);
  496. BytesRef term2;
  497. @@ -430,13 +428,11 @@
  498. Fields tv2 = r2.getTermVectors(id2);
  499. System.out.println(" d2=" + tv2);
  500. if (tv2 != null) {
  501. - FieldsEnum fieldsEnum = tv2.iterator();
  502. - String field;
  503. DocsAndPositionsEnum dpEnum = null;
  504. DocsEnum dEnum = null;
  505. - while ((field=fieldsEnum.next()) != null) {
  506. + for (String field : tv2) {
  507. System.out.println(" " + field + ":");
  508. - Terms terms3 = fieldsEnum.terms();
  509. + Terms terms3 = tv2.terms(field);
  510. assertNotNull(terms3);
  511. TermsEnum termsEnum3 = terms3.iterator(null);
  512. BytesRef term2;
  513. @@ -469,9 +465,12 @@
  514.  
  515. // Verify postings
  516. //System.out.println("TEST: create te1");
  517. - final FieldsEnum fields1 = MultiFields.getFields(r1).iterator();
  518. - final FieldsEnum fields2 = MultiFields.getFields(r2).iterator();
  519. + final Fields fields1 = MultiFields.getFields(r1);
  520. + final Iterator<String> fields1Enum = fields1.iterator();
  521. + final Fields fields2 = MultiFields.getFields(r2);
  522. + final Iterator<String> fields2Enum = fields2.iterator();
  523.  
  524. +
  525. String field1=null, field2=null;
  526. TermsEnum termsEnum1 = null;
  527. termsEnum2 = null;
  528. @@ -489,16 +488,15 @@
  529. for(;;) {
  530. len1=0;
  531. if (termsEnum1 == null) {
  532. - field1 = fields1.next();
  533. - if (field1 == null) {
  534. + if (!fields1Enum.hasNext()) {
  535. break;
  536. - } else {
  537. - Terms terms = fields1.terms();
  538. - if (terms == null) {
  539. - continue;
  540. - }
  541. - termsEnum1 = terms.iterator(null);
  542. }
  543. + field1 = fields1Enum.next();
  544. + Terms terms = fields1.terms(field1);
  545. + if (terms == null) {
  546. + continue;
  547. + }
  548. + termsEnum1 = terms.iterator(null);
  549. }
  550. term1 = termsEnum1.next();
  551. if (term1 == null) {
  552. @@ -523,16 +521,15 @@
  553. for(;;) {
  554. len2=0;
  555. if (termsEnum2 == null) {
  556. - field2 = fields2.next();
  557. - if (field2 == null) {
  558. + if (!fields2Enum.hasNext()) {
  559. break;
  560. - } else {
  561. - Terms terms = fields2.terms();
  562. - if (terms == null) {
  563. - continue;
  564. - }
  565. - termsEnum2 = terms.iterator(null);
  566. }
  567. + field2 = fields2Enum.next();
  568. + Terms terms = fields2.terms(field2);
  569. + if (terms == null) {
  570. + continue;
  571. + }
  572. + termsEnum2 = terms.iterator(null);
  573. }
  574. term2 = termsEnum2.next();
  575. if (term2 == null) {
  576. @@ -605,18 +602,17 @@
  577. }
  578. assertTrue(d2 != null);
  579.  
  580. - FieldsEnum fieldsEnum1 = d1.iterator();
  581. - FieldsEnum fieldsEnum2 = d2.iterator();
  582. - String field1;
  583. - while ((field1 = fieldsEnum1.next()) != null) {
  584. + Iterator<String> fieldsEnum2 = d2.iterator();
  585. +
  586. + for (String field1 : d1) {
  587. String field2 = fieldsEnum2.next();
  588. assertEquals(field1, field2);
  589.  
  590. - Terms terms1 = fieldsEnum1.terms();
  591. + Terms terms1 = d1.terms(field1);
  592. assertNotNull(terms1);
  593. TermsEnum termsEnum1 = terms1.iterator(null);
  594.  
  595. - Terms terms2 = fieldsEnum2.terms();
  596. + Terms terms2 = d2.terms(field2);
  597. assertNotNull(terms2);
  598. TermsEnum termsEnum2 = terms2.iterator(null);
  599.  
  600. @@ -689,7 +685,7 @@
  601.  
  602. assertNull(termsEnum2.next());
  603. }
  604. - assertNull(fieldsEnum2.next());
  605. + assertFalse(fieldsEnum2.hasNext());
  606. }
  607.  
  608. private class IndexingThread extends Thread {
  609. Index: lucene/core/src/test/org/apache/lucene/index/TestFilterAtomicReader.java
  610. ===================================================================
  611. --- lucene/core/src/test/org/apache/lucene/index/TestFilterAtomicReader.java (revision 1372046)
  612. +++ lucene/core/src/test/org/apache/lucene/index/TestFilterAtomicReader.java (working copy)
  613. @@ -40,11 +40,8 @@
  614. TestFields(Fields in) {
  615. super(in);
  616. }
  617. +
  618. @Override
  619. - public FieldsEnum iterator() throws IOException {
  620. - return new TestFieldsEnum(super.iterator());
  621. - }
  622. - @Override
  623. public Terms terms(String field) throws IOException {
  624. return new TestTerms(super.terms(field));
  625. }
  626. @@ -61,17 +58,6 @@
  627. }
  628. }
  629.  
  630. - private static class TestFieldsEnum extends FilterFieldsEnum {
  631. - TestFieldsEnum(FieldsEnum in) {
  632. - super(in);
  633. - }
  634. -
  635. - @Override
  636. - public Terms terms() throws IOException {
  637. - return new TestTerms(super.terms());
  638. - }
  639. - }
  640. -
  641. private static class TestTermsEnum extends FilterTermsEnum {
  642. public TestTermsEnum(TermsEnum in) {
  643. super(in);
  644. @@ -194,7 +180,6 @@
  645. checkOverrideMethods(FilterAtomicReader.class);
  646. checkOverrideMethods(FilterAtomicReader.FilterFields.class);
  647. checkOverrideMethods(FilterAtomicReader.FilterTerms.class);
  648. - checkOverrideMethods(FilterAtomicReader.FilterFieldsEnum.class);
  649. checkOverrideMethods(FilterAtomicReader.FilterTermsEnum.class);
  650. checkOverrideMethods(FilterAtomicReader.FilterDocsEnum.class);
  651. checkOverrideMethods(FilterAtomicReader.FilterDocsAndPositionsEnum.class);
  652. Index: lucene/core/src/test/org/apache/lucene/index/TestDuelingCodecs.java
  653. ===================================================================
  654. --- lucene/core/src/test/org/apache/lucene/index/TestDuelingCodecs.java (revision 1372046)
  655. +++ lucene/core/src/test/org/apache/lucene/index/TestDuelingCodecs.java (working copy)
  656. @@ -173,15 +173,15 @@
  657. }
  658. assertFieldStatistics(leftFields, rightFields);
  659.  
  660. - FieldsEnum leftEnum = leftFields.iterator();
  661. - FieldsEnum rightEnum = rightFields.iterator();
  662. + Iterator<String> leftEnum = leftFields.iterator();
  663. + Iterator<String> rightEnum = rightFields.iterator();
  664.  
  665. - String field;
  666. - while ((field = leftEnum.next()) != null) {
  667. + while (leftEnum.hasNext()) {
  668. + String field = leftEnum.next();
  669. assertEquals(info, field, rightEnum.next());
  670. - assertTerms(leftEnum.terms(), rightEnum.terms(), deep);
  671. + assertTerms(leftFields.terms(field), rightFields.terms(field), deep);
  672. }
  673. - assertNull(rightEnum.next());
  674. + assertFalse(rightEnum.hasNext());
  675. }
  676.  
  677. /**
  678. @@ -531,9 +531,7 @@
  679. return;
  680. }
  681.  
  682. - FieldsEnum fieldsEnum = leftFields.iterator();
  683. - String field;
  684. - while ((field = fieldsEnum.next()) != null) {
  685. + for (String field : leftFields) {
  686. DocValues leftNorms = MultiDocValues.getNormDocValues(leftReader, field);
  687. DocValues rightNorms = MultiDocValues.getNormDocValues(rightReader, field);
  688. if (leftNorms != null && rightNorms != null) {
  689. Index: lucene/core/src/test/org/apache/lucene/index/TestParallelTermEnum.java
  690. ===================================================================
  691. --- lucene/core/src/test/org/apache/lucene/index/TestParallelTermEnum.java (revision 1372046)
  692. +++ lucene/core/src/test/org/apache/lucene/index/TestParallelTermEnum.java (working copy)
  693. @@ -18,6 +18,7 @@
  694. */
  695.  
  696. import java.io.IOException;
  697. +import java.util.Iterator;
  698.  
  699. import org.apache.lucene.analysis.MockAnalyzer;
  700. import org.apache.lucene.document.Document;
  701. @@ -94,20 +95,21 @@
  702.  
  703. Bits liveDocs = pr.getLiveDocs();
  704.  
  705. - FieldsEnum fe = pr.fields().iterator();
  706. + Fields fields = pr.fields();
  707. + Iterator<String> fe = fields.iterator();
  708.  
  709. String f = fe.next();
  710. assertEquals("field1", f);
  711. - checkTerms(fe.terms(), liveDocs, "brown", "fox", "jumps", "quick", "the");
  712. + checkTerms(fields.terms(f), liveDocs, "brown", "fox", "jumps", "quick", "the");
  713.  
  714. f = fe.next();
  715. assertEquals("field2", f);
  716. - checkTerms(fe.terms(), liveDocs, "brown", "fox", "jumps", "quick", "the");
  717. + checkTerms(fields.terms(f), liveDocs, "brown", "fox", "jumps", "quick", "the");
  718.  
  719. f = fe.next();
  720. assertEquals("field3", f);
  721. - checkTerms(fe.terms(), liveDocs, "dog", "fox", "jumps", "lazy", "over", "the");
  722. + checkTerms(fields.terms(f), liveDocs, "dog", "fox", "jumps", "lazy", "over", "the");
  723.  
  724. - assertNull(fe.next());
  725. + assertFalse(fe.hasNext());
  726. }
  727. }
  728. Index: lucene/core/src/test/org/apache/lucene/index/TestDirectoryReader.java
  729. ===================================================================
  730. --- lucene/core/src/test/org/apache/lucene/index/TestDirectoryReader.java (revision 1372046)
  731. +++ lucene/core/src/test/org/apache/lucene/index/TestDirectoryReader.java (working copy)
  732. @@ -612,20 +612,20 @@
  733. }
  734.  
  735. // check dictionary and posting lists
  736. - FieldsEnum fenum1 = MultiFields.getFields(index1).iterator();
  737. - FieldsEnum fenum2 = MultiFields.getFields(index2).iterator();
  738. - String field1 = null;
  739. + Fields fields1 = MultiFields.getFields(index1);
  740. + Fields fields2 = MultiFields.getFields(index2);
  741. + Iterator<String> fenum2 = fields2.iterator();
  742. Bits liveDocs = MultiFields.getLiveDocs(index1);
  743. - while((field1=fenum1.next()) != null) {
  744. + for (String field1 : fields1) {
  745. assertEquals("Different fields", field1, fenum2.next());
  746. - Terms terms1 = fenum1.terms();
  747. + Terms terms1 = fields1.terms(field1);
  748. if (terms1 == null) {
  749. - assertNull(fenum2.terms());
  750. + assertNull(fields2.terms(field1));
  751. continue;
  752. }
  753. TermsEnum enum1 = terms1.iterator(null);
  754.  
  755. - Terms terms2 = fenum2.terms();
  756. + Terms terms2 = fields2.terms(field1);
  757. assertNotNull(terms2);
  758. TermsEnum enum2 = terms2.iterator(null);
  759.  
  760. @@ -644,6 +644,7 @@
  761. }
  762. }
  763. }
  764. + assertFalse(fenum2.hasNext());
  765. }
  766.  
  767. public void testGetIndexCommit() throws IOException {
  768. Index: lucene/core/src/test/org/apache/lucene/index/TestCodecs.java
  769. ===================================================================
  770. --- lucene/core/src/test/org/apache/lucene/index/TestCodecs.java (revision 1372046)
  771. +++ lucene/core/src/test/org/apache/lucene/index/TestCodecs.java (working copy)
  772. @@ -20,6 +20,7 @@
  773. import java.io.IOException;
  774. import java.util.Arrays;
  775. import java.util.HashSet;
  776. +import java.util.Iterator;
  777.  
  778. import org.apache.lucene.analysis.MockAnalyzer;
  779. import org.apache.lucene.codecs.Codec;
  780. @@ -258,9 +259,10 @@
  781.  
  782. final FieldsProducer reader = codec.postingsFormat().fieldsProducer(new SegmentReadState(dir, si, fieldInfos, newIOContext(random()), DirectoryReader.DEFAULT_TERMS_INDEX_DIVISOR));
  783.  
  784. - final FieldsEnum fieldsEnum = reader.iterator();
  785. - assertNotNull(fieldsEnum.next());
  786. - final Terms terms2 = fieldsEnum.terms();
  787. + final Iterator<String> fieldsEnum = reader.iterator();
  788. + String fieldName = fieldsEnum.next();
  789. + assertNotNull(fieldName);
  790. + final Terms terms2 = reader.terms(fieldName);
  791. assertNotNull(terms2);
  792.  
  793. final TermsEnum termsEnum = terms2.iterator(null);
  794. @@ -286,7 +288,7 @@
  795. assertEquals(termsEnum.seekCeil(new BytesRef(terms[i].text2)), TermsEnum.SeekStatus.FOUND);
  796. }
  797.  
  798. - assertNull(fieldsEnum.next());
  799. + assertFalse(fieldsEnum.hasNext());
  800. reader.close();
  801. dir.close();
  802. }
  803. Index: lucene/core/src/test/org/apache/lucene/index/TestSumDocFreq.java
  804. ===================================================================
  805. --- lucene/core/src/test/org/apache/lucene/index/TestSumDocFreq.java (revision 1372046)
  806. +++ lucene/core/src/test/org/apache/lucene/index/TestSumDocFreq.java (working copy)
  807. @@ -74,9 +74,8 @@
  808. private void assertSumDocFreq(IndexReader ir) throws Exception {
  809. // compute sumDocFreq across all fields
  810. Fields fields = MultiFields.getFields(ir);
  811. - FieldsEnum fieldEnum = fields.iterator();
  812. - String f = null;
  813. - while ((f = fieldEnum.next()) != null) {
  814. +
  815. + for (String f : fields) {
  816. Terms terms = fields.terms(f);
  817. long sumDocFreq = terms.getSumDocFreq();
  818. if (sumDocFreq == -1) {
  819. Index: lucene/core/src/test/org/apache/lucene/index/TestDocCount.java
  820. ===================================================================
  821. --- lucene/core/src/test/org/apache/lucene/index/TestDocCount.java (revision 1372046)
  822. +++ lucene/core/src/test/org/apache/lucene/index/TestDocCount.java (working copy)
  823. @@ -61,9 +61,7 @@
  824. if (fields == null) {
  825. return;
  826. }
  827. - FieldsEnum e = fields.iterator();
  828. - String field;
  829. - while ((field = e.next()) != null) {
  830. + for (String field : fields) {
  831. Terms terms = fields.terms(field);
  832. if (terms == null) {
  833. continue;
  834. Index: lucene/core/src/java/org/apache/lucene/util/UnmodifiableIterator.java
  835. ===================================================================
  836. --- lucene/core/src/java/org/apache/lucene/util/UnmodifiableIterator.java (revision 0)
  837. +++ lucene/core/src/java/org/apache/lucene/util/UnmodifiableIterator.java (working copy)
  838. @@ -0,0 +1,46 @@
  839. +package org.apache.lucene.util;
  840. +
  841. +/*
  842. + * Licensed to the Apache Software Foundation (ASF) under one or more
  843. + * contributor license agreements. See the NOTICE file distributed with
  844. + * this work for additional information regarding copyright ownership.
  845. + * The ASF licenses this file to You under the Apache License, Version 2.0
  846. + * (the "License"); you may not use this file except in compliance with
  847. + * the License. You may obtain a copy of the License at
  848. + *
  849. + * http://www.apache.org/licenses/LICENSE-2.0
  850. + *
  851. + * Unless required by applicable law or agreed to in writing, software
  852. + * distributed under the License is distributed on an "AS IS" BASIS,
  853. + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  854. + * See the License for the specific language governing permissions and
  855. + * limitations under the License.
  856. + */
  857. +
  858. +import java.util.Iterator;
  859. +
  860. +/**
  861. + * Wraps an iterator to ensure its unmodifiable
  862. + */
  863. +public class UnmodifiableIterator<T> implements Iterator<T> {
  864. + private final Iterator<T> in;
  865. +
  866. + public UnmodifiableIterator(Iterator<T> in) {
  867. + this.in = in;
  868. + }
  869. +
  870. + @Override
  871. + public boolean hasNext() {
  872. + return in.hasNext();
  873. + }
  874. +
  875. + @Override
  876. + public T next() {
  877. + return in.next();
  878. + }
  879. +
  880. + @Override
  881. + public void remove() {
  882. + throw new UnsupportedOperationException();
  883. + }
  884. +}
  885.  
  886. Property changes on: lucene/core/src/java/org/apache/lucene/util/UnmodifiableIterator.java
  887. ___________________________________________________________________
  888. Added: svn:eol-style
  889. ## -0,0 +1 ##
  890. +native
  891. Index: lucene/core/src/java/org/apache/lucene/util/MergedIterator.java
  892. ===================================================================
  893. --- lucene/core/src/java/org/apache/lucene/util/MergedIterator.java (revision 0)
  894. +++ lucene/core/src/java/org/apache/lucene/util/MergedIterator.java (working copy)
  895. @@ -0,0 +1,148 @@
  896. +package org.apache.lucene.util;
  897. +
  898. +/*
  899. + * Licensed to the Apache Software Foundation (ASF) under one or more
  900. + * contributor license agreements. See the NOTICE file distributed with
  901. + * this work for additional information regarding copyright ownership.
  902. + * The ASF licenses this file to You under the Apache License, Version 2.0
  903. + * (the "License"); you may not use this file except in compliance with
  904. + * the License. You may obtain a copy of the License at
  905. + *
  906. + * http://www.apache.org/licenses/LICENSE-2.0
  907. + *
  908. + * Unless required by applicable law or agreed to in writing, software
  909. + * distributed under the License is distributed on an "AS IS" BASIS,
  910. + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  911. + * See the License for the specific language governing permissions and
  912. + * limitations under the License.
  913. + */
  914. +
  915. +import java.util.Iterator;
  916. +import java.util.NoSuchElementException;
  917. +
  918. +/**
  919. + * Provides a merged sorted view from several sorted iterators, each
  920. + * iterating over a unique set of elements.
  921. + * <p>
  922. + * If an element appears in multiple iterators, it is deduplicated,
  923. + * that is this iterator returns the sorted union of elements.
  924. + * <p>
  925. + * Caveats:
  926. + * <ul>
  927. + * <li>The behavior is undefined if the iterators are not actually
  928. + * sorted according to their comparator, or if a single iterator
  929. + * contains duplicates.
  930. + * <li>Null elements are unsupported.
  931. + * <li>When an element E is a duplicate across multiple iterators,
  932. + * only one is returned, but it is undefined which one: not
  933. + * guaranteed to be a stable sort.
  934. + * </ul>
  935. + * @lucene.internal
  936. + */
  937. +public class MergedIterator<T extends Comparable<T>> implements Iterator<T> {
  938. + private T current;
  939. + private final TermMergeQueue<T> queue;
  940. + private final SubIterator<T>[] top;
  941. + private int numTop;
  942. +
  943. + @SuppressWarnings("unchecked")
  944. + public MergedIterator(Iterator<T>... iterators) {
  945. + queue = new TermMergeQueue<T>(iterators.length);
  946. + top = new SubIterator[iterators.length];
  947. + int index = 0;
  948. + for (Iterator<T> iterator : iterators) {
  949. + if (iterator.hasNext()) {
  950. + SubIterator<T> sub = new SubIterator<T>();
  951. + sub.current = iterator.next();
  952. + sub.iterator = iterator;
  953. + sub.index = index++;
  954. + queue.add(sub);
  955. + }
  956. + }
  957. + }
  958. +
  959. + @Override
  960. + public boolean hasNext() {
  961. + if (queue.size() > 0) {
  962. + return true;
  963. + }
  964. +
  965. + for (int i = 0; i < numTop; i++) {
  966. + if (top[i].iterator.hasNext()) {
  967. + return true;
  968. + }
  969. + }
  970. + return false;
  971. + }
  972. +
  973. + @Override
  974. + public T next() {
  975. + // restore queue
  976. + pushTop();
  977. +
  978. + // gather equal top elements
  979. + if (queue.size() > 0) {
  980. + pullTop();
  981. + } else {
  982. + current = null;
  983. + }
  984. + if (current == null) {
  985. + throw new NoSuchElementException();
  986. + }
  987. + return current;
  988. + }
  989. +
  990. + @Override
  991. + public void remove() {
  992. + throw new UnsupportedOperationException();
  993. + }
  994. +
  995. + private void pullTop() {
  996. + // extract all subs from the queue that have the same top element
  997. + assert numTop == 0;
  998. + while (true) {
  999. + top[numTop++] = queue.pop();
  1000. + if (queue.size() == 0
  1001. + || !(queue.top()).current.equals(top[0].current)) {
  1002. + break;
  1003. + }
  1004. + }
  1005. + current = top[0].current;
  1006. + }
  1007. +
  1008. + private void pushTop() {
  1009. + // call next() on each top, and put back into queue
  1010. + for (int i = 0; i < numTop; i++) {
  1011. + if (top[i].iterator.hasNext()) {
  1012. + top[i].current = top[i].iterator.next();
  1013. + queue.add(top[i]);
  1014. + } else {
  1015. + // no more elements
  1016. + top[i].current = null;
  1017. + }
  1018. + }
  1019. + numTop = 0;
  1020. + }
  1021. +
  1022. + private static class SubIterator<I extends Comparable<I>> {
  1023. + Iterator<I> iterator;
  1024. + I current;
  1025. + int index;
  1026. + }
  1027. +
  1028. + private static class TermMergeQueue<C extends Comparable<C>> extends PriorityQueue<SubIterator<C>> {
  1029. + TermMergeQueue(int size) {
  1030. + super(size);
  1031. + }
  1032. +
  1033. + @Override
  1034. + protected boolean lessThan(SubIterator<C> a, SubIterator<C> b) {
  1035. + final int cmp = a.current.compareTo(b.current);
  1036. + if (cmp != 0) {
  1037. + return cmp < 0;
  1038. + } else {
  1039. + return a.index < b.index;
  1040. + }
  1041. + }
  1042. + }
  1043. +}
  1044.  
  1045. Property changes on: lucene/core/src/java/org/apache/lucene/util/MergedIterator.java
  1046. ___________________________________________________________________
  1047. Added: svn:eol-style
  1048. ## -0,0 +1 ##
  1049. +native
  1050. Index: lucene/core/src/java/org/apache/lucene/codecs/FieldsConsumer.java
  1051. ===================================================================
  1052. --- lucene/core/src/java/org/apache/lucene/codecs/FieldsConsumer.java (revision 1372046)
  1053. +++ lucene/core/src/java/org/apache/lucene/codecs/FieldsConsumer.java (working copy)
  1054. @@ -22,7 +22,6 @@
  1055.  
  1056. import org.apache.lucene.index.FieldInfo;
  1057. import org.apache.lucene.index.Fields;
  1058. -import org.apache.lucene.index.FieldsEnum;
  1059. import org.apache.lucene.index.MergeState;
  1060. import org.apache.lucene.index.SegmentWriteState; // javadocs
  1061. import org.apache.lucene.index.Terms;
  1062. @@ -53,13 +52,10 @@
  1063. public abstract void close() throws IOException;
  1064.  
  1065. public void merge(MergeState mergeState, Fields fields) throws IOException {
  1066. - FieldsEnum fieldsEnum = fields.iterator();
  1067. - assert fieldsEnum != null;
  1068. - String field;
  1069. - while((field = fieldsEnum.next()) != null) {
  1070. + for (String field : fields) {
  1071. mergeState.fieldInfo = mergeState.fieldInfos.fieldInfo(field);
  1072. assert mergeState.fieldInfo != null : "FieldInfo for field is null: "+ field;
  1073. - Terms terms = fieldsEnum.terms();
  1074. + Terms terms = fields.terms(field);
  1075. if (terms != null) {
  1076. final TermsConsumer termsConsumer = addField(mergeState.fieldInfo);
  1077. termsConsumer.merge(mergeState, terms.iterator(null));
  1078. Index: lucene/core/src/java/org/apache/lucene/codecs/memory/MemoryPostingsFormat.java
  1079. ===================================================================
  1080. --- lucene/core/src/java/org/apache/lucene/codecs/memory/MemoryPostingsFormat.java (revision 1372046)
  1081. +++ lucene/core/src/java/org/apache/lucene/codecs/memory/MemoryPostingsFormat.java (working copy)
  1082. @@ -34,7 +34,6 @@
  1083. import org.apache.lucene.index.FieldInfo.IndexOptions;
  1084. import org.apache.lucene.index.FieldInfo;
  1085. import org.apache.lucene.index.FieldInfos;
  1086. -import org.apache.lucene.index.FieldsEnum;
  1087. import org.apache.lucene.index.IndexFileNames;
  1088. import org.apache.lucene.index.SegmentReadState;
  1089. import org.apache.lucene.index.SegmentWriteState;
  1090. @@ -49,6 +48,7 @@
  1091. import org.apache.lucene.util.Bits;
  1092. import org.apache.lucene.util.BytesRef;
  1093. import org.apache.lucene.util.IntsRef;
  1094. +import org.apache.lucene.util.UnmodifiableIterator;
  1095. import org.apache.lucene.util.fst.Builder;
  1096. import org.apache.lucene.util.fst.ByteSequenceOutputs;
  1097. import org.apache.lucene.util.fst.BytesRefFSTEnum;
  1098. @@ -874,24 +874,8 @@
  1099.  
  1100. return new FieldsProducer() {
  1101. @Override
  1102. - public FieldsEnum iterator() {
  1103. - final Iterator<TermsReader> iter = fields.values().iterator();
  1104. -
  1105. - return new FieldsEnum() {
  1106. -
  1107. - private TermsReader current;
  1108. -
  1109. - @Override
  1110. - public String next() {
  1111. - current = iter.next();
  1112. - return current.field.name;
  1113. - }
  1114. -
  1115. - @Override
  1116. - public Terms terms() {
  1117. - return current;
  1118. - }
  1119. - };
  1120. + public Iterator<String> iterator() {
  1121. + return new UnmodifiableIterator<String>(fields.keySet().iterator());
  1122. }
  1123.  
  1124. @Override
  1125. Index: lucene/core/src/java/org/apache/lucene/codecs/memory/DirectPostingsFormat.java
  1126. ===================================================================
  1127. --- lucene/core/src/java/org/apache/lucene/codecs/memory/DirectPostingsFormat.java (revision 1372046)
  1128. +++ lucene/core/src/java/org/apache/lucene/codecs/memory/DirectPostingsFormat.java (working copy)
  1129. @@ -32,7 +32,6 @@
  1130. import org.apache.lucene.index.FieldInfo.IndexOptions;
  1131. import org.apache.lucene.index.FieldInfo;
  1132. import org.apache.lucene.index.Fields;
  1133. -import org.apache.lucene.index.FieldsEnum;
  1134. import org.apache.lucene.index.OrdTermState;
  1135. import org.apache.lucene.index.SegmentReadState;
  1136. import org.apache.lucene.index.SegmentWriteState;
  1137. @@ -44,6 +43,7 @@
  1138. import org.apache.lucene.util.ArrayUtil;
  1139. import org.apache.lucene.util.Bits;
  1140. import org.apache.lucene.util.BytesRef;
  1141. +import org.apache.lucene.util.UnmodifiableIterator;
  1142. import org.apache.lucene.util.automaton.CompiledAutomaton;
  1143. import org.apache.lucene.util.automaton.RunAutomaton;
  1144. import org.apache.lucene.util.automaton.Transition;
  1145. @@ -124,36 +124,14 @@
  1146. private final Map<String,DirectField> fields = new TreeMap<String,DirectField>();
  1147.  
  1148. public DirectFields(SegmentReadState state, Fields fields, int minSkipCount, int lowFreqCutoff) throws IOException {
  1149. - FieldsEnum fieldsEnum = fields.iterator();
  1150. - String field;
  1151. - while ((field = fieldsEnum.next()) != null) {
  1152. - this.fields.put(field, new DirectField(state, field, fieldsEnum.terms(), minSkipCount, lowFreqCutoff));
  1153. + for (String field : fields) {
  1154. + this.fields.put(field, new DirectField(state, field, fields.terms(field), minSkipCount, lowFreqCutoff));
  1155. }
  1156. }
  1157.  
  1158. @Override
  1159. - public FieldsEnum iterator() {
  1160. -
  1161. - final Iterator<Map.Entry<String,DirectField>> iter = fields.entrySet().iterator();
  1162. -
  1163. - return new FieldsEnum() {
  1164. - Map.Entry<String,DirectField> current;
  1165. -
  1166. - @Override
  1167. - public String next() {
  1168. - if (iter.hasNext()) {
  1169. - current = iter.next();
  1170. - return current.getKey();
  1171. - } else {
  1172. - return null;
  1173. - }
  1174. - }
  1175. -
  1176. - @Override
  1177. - public Terms terms() {
  1178. - return current.getValue();
  1179. - }
  1180. - };
  1181. + public Iterator<String> iterator() {
  1182. + return new UnmodifiableIterator<String>(fields.keySet().iterator());
  1183. }
  1184.  
  1185. @Override
  1186. Index: lucene/core/src/java/org/apache/lucene/codecs/bloom/BloomFilteringPostingsFormat.java
  1187. ===================================================================
  1188. --- lucene/core/src/java/org/apache/lucene/codecs/bloom/BloomFilteringPostingsFormat.java (revision 1372046)
  1189. +++ lucene/core/src/java/org/apache/lucene/codecs/bloom/BloomFilteringPostingsFormat.java (working copy)
  1190. @@ -21,6 +21,7 @@
  1191. import java.util.ArrayList;
  1192. import java.util.Comparator;
  1193. import java.util.HashMap;
  1194. +import java.util.Iterator;
  1195. import java.util.List;
  1196. import java.util.Map;
  1197. import java.util.Map.Entry;
  1198. @@ -35,7 +36,6 @@
  1199. import org.apache.lucene.index.DocsAndPositionsEnum;
  1200. import org.apache.lucene.index.DocsEnum;
  1201. import org.apache.lucene.index.FieldInfo;
  1202. -import org.apache.lucene.index.FieldsEnum;
  1203. import org.apache.lucene.index.IndexFileNames;
  1204. import org.apache.lucene.index.SegmentReadState;
  1205. import org.apache.lucene.index.SegmentWriteState;
  1206. @@ -44,7 +44,6 @@
  1207. import org.apache.lucene.store.DataOutput;
  1208. import org.apache.lucene.store.IndexInput;
  1209. import org.apache.lucene.store.IndexOutput;
  1210. -import org.apache.lucene.util.AttributeSource;
  1211. import org.apache.lucene.util.Bits;
  1212. import org.apache.lucene.util.BytesRef;
  1213. import org.apache.lucene.util.FuzzySet;
  1214. @@ -187,9 +186,8 @@
  1215.  
  1216. }
  1217.  
  1218. - public FieldsEnum iterator() throws IOException {
  1219. - return new BloomFilteredFieldsEnum(delegateFieldsProducer.iterator(),
  1220. - bloomsByFieldName);
  1221. + public Iterator<String> iterator() {
  1222. + return delegateFieldsProducer.iterator();
  1223. }
  1224.  
  1225. public void close() throws IOException {
  1226. @@ -217,44 +215,6 @@
  1227. return delegateFieldsProducer.getUniqueTermCount();
  1228. }
  1229.  
  1230. - // Not all fields in a segment may be subject to a bloom filter. This class
  1231. - // wraps Terms objects appropriately if a filtering request is present
  1232. - class BloomFilteredFieldsEnum extends FieldsEnum {
  1233. - private FieldsEnum delegateFieldsEnum;
  1234. - private HashMap<String,FuzzySet> bloomsByFieldName;
  1235. - private String currentFieldName;
  1236. -
  1237. - public BloomFilteredFieldsEnum(FieldsEnum iterator,
  1238. - HashMap<String,FuzzySet> bloomsByFieldName) {
  1239. - this.delegateFieldsEnum = iterator;
  1240. - this.bloomsByFieldName = bloomsByFieldName;
  1241. - }
  1242. -
  1243. - public AttributeSource attributes() {
  1244. - return delegateFieldsEnum.attributes();
  1245. - }
  1246. -
  1247. - public String next() throws IOException {
  1248. - currentFieldName = delegateFieldsEnum.next();
  1249. - return currentFieldName;
  1250. - }
  1251. -
  1252. - public Terms terms() throws IOException {
  1253. - FuzzySet filter = bloomsByFieldName.get(currentFieldName);
  1254. - if (filter == null) {
  1255. - return delegateFieldsEnum.terms();
  1256. - } else {
  1257. - Terms result = delegateFieldsEnum.terms();
  1258. - if (result == null) {
  1259. - return null;
  1260. - }
  1261. - // wrap the terms object with a bloom filter
  1262. - return new BloomFilteredTerms(result, filter);
  1263. - }
  1264. - }
  1265. -
  1266. - }
  1267. -
  1268. class BloomFilteredTerms extends Terms {
  1269. private Terms delegateTerms;
  1270. private FuzzySet filter;
  1271. Index: lucene/core/src/java/org/apache/lucene/codecs/BlockTermsReader.java
  1272. ===================================================================
  1273. --- lucene/core/src/java/org/apache/lucene/codecs/BlockTermsReader.java (revision 1372046)
  1274. +++ lucene/core/src/java/org/apache/lucene/codecs/BlockTermsReader.java (working copy)
  1275. @@ -27,7 +27,6 @@
  1276. import org.apache.lucene.index.FieldInfo.IndexOptions;
  1277. import org.apache.lucene.index.FieldInfo;
  1278. import org.apache.lucene.index.FieldInfos;
  1279. -import org.apache.lucene.index.FieldsEnum;
  1280. import org.apache.lucene.index.IndexFileNames;
  1281. import org.apache.lucene.index.TermState;
  1282. import org.apache.lucene.index.Terms;
  1283. @@ -40,6 +39,7 @@
  1284. import org.apache.lucene.util.Bits;
  1285. import org.apache.lucene.util.BytesRef;
  1286. import org.apache.lucene.util.DoubleBarrelLRUCache;
  1287. +import org.apache.lucene.util.UnmodifiableIterator;
  1288.  
  1289. /** Handles a terms dict, but decouples all details of
  1290. * doc/freqs/positions reading to an instance of {@link
  1291. @@ -184,8 +184,8 @@
  1292. }
  1293.  
  1294. @Override
  1295. - public FieldsEnum iterator() {
  1296. - return new TermFieldsEnum();
  1297. + public Iterator<String> iterator() {
  1298. + return new UnmodifiableIterator<String>(fields.keySet().iterator());
  1299. }
  1300.  
  1301. @Override
  1302. @@ -199,32 +199,6 @@
  1303. return fields.size();
  1304. }
  1305.  
  1306. - // Iterates through all fields
  1307. - private class TermFieldsEnum extends FieldsEnum {
  1308. - final Iterator<FieldReader> it;
  1309. - FieldReader current;
  1310. -
  1311. - TermFieldsEnum() {
  1312. - it = fields.values().iterator();
  1313. - }
  1314. -
  1315. - @Override
  1316. - public String next() {
  1317. - if (it.hasNext()) {
  1318. - current = it.next();
  1319. - return current.fieldInfo.name;
  1320. - } else {
  1321. - current = null;
  1322. - return null;
  1323. - }
  1324. - }
  1325. -
  1326. - @Override
  1327. - public Terms terms() throws IOException {
  1328. - return current;
  1329. - }
  1330. - }
  1331. -
  1332. private class FieldReader extends Terms {
  1333. final long numTerms;
  1334. final FieldInfo fieldInfo;
  1335. Index: lucene/core/src/java/org/apache/lucene/codecs/simpletext/SimpleTextFieldsReader.java
  1336. ===================================================================
  1337. --- lucene/core/src/java/org/apache/lucene/codecs/simpletext/SimpleTextFieldsReader.java (revision 1372046)
  1338. +++ lucene/core/src/java/org/apache/lucene/codecs/simpletext/SimpleTextFieldsReader.java (working copy)
  1339. @@ -20,7 +20,10 @@
  1340. import java.io.IOException;
  1341. import java.util.Comparator;
  1342. import java.util.HashMap;
  1343. +import java.util.Iterator;
  1344. import java.util.Map;
  1345. +import java.util.TreeMap;
  1346. +import java.util.TreeSet;
  1347.  
  1348. import org.apache.lucene.codecs.FieldsProducer;
  1349. import org.apache.lucene.index.DocsAndPositionsEnum;
  1350. @@ -28,7 +31,6 @@
  1351. import org.apache.lucene.index.FieldInfo;
  1352. import org.apache.lucene.index.FieldInfo.IndexOptions;
  1353. import org.apache.lucene.index.FieldInfos;
  1354. -import org.apache.lucene.index.FieldsEnum;
  1355. import org.apache.lucene.index.SegmentReadState;
  1356. import org.apache.lucene.index.Terms;
  1357. import org.apache.lucene.index.TermsEnum;
  1358. @@ -41,6 +43,7 @@
  1359. import org.apache.lucene.util.OpenBitSet;
  1360. import org.apache.lucene.util.StringHelper;
  1361. import org.apache.lucene.util.UnicodeUtil;
  1362. +import org.apache.lucene.util.UnmodifiableIterator;
  1363. import org.apache.lucene.util.fst.Builder;
  1364. import org.apache.lucene.util.fst.BytesRefFSTEnum;
  1365. import org.apache.lucene.util.fst.FST;
  1366. @@ -49,7 +52,7 @@
  1367. import org.apache.lucene.util.fst.Util;
  1368.  
  1369. class SimpleTextFieldsReader extends FieldsProducer {
  1370. -
  1371. + private final TreeMap<String,Long> fields;
  1372. private final IndexInput in;
  1373. private final FieldInfos fieldInfos;
  1374.  
  1375. @@ -67,35 +70,22 @@
  1376. in = state.dir.openInput(SimpleTextPostingsFormat.getPostingsFileName(state.segmentInfo.name, state.segmentSuffix), state.context);
  1377.  
  1378. fieldInfos = state.fieldInfos;
  1379. + fields = readFields((IndexInput)in.clone());
  1380. }
  1381. -
  1382. - private class SimpleTextFieldsEnum extends FieldsEnum {
  1383. - private final IndexInput in;
  1384. - private final BytesRef scratch = new BytesRef(10);
  1385. - private String current;
  1386. -
  1387. - public SimpleTextFieldsEnum() {
  1388. - this.in = (IndexInput) SimpleTextFieldsReader.this.in.clone();
  1389. - }
  1390. -
  1391. - @Override
  1392. - public String next() throws IOException {
  1393. - while(true) {
  1394. - SimpleTextUtil.readLine(in, scratch);
  1395. - if (scratch.equals(END)) {
  1396. - current = null;
  1397. - return null;
  1398. - }
  1399. - if (StringHelper.startsWith(scratch, FIELD)) {
  1400. - return current = new String(scratch.bytes, scratch.offset + FIELD.length, scratch.length - FIELD.length, "UTF-8");
  1401. - }
  1402. +
  1403. + private TreeMap<String,Long> readFields(IndexInput in) throws IOException {
  1404. + BytesRef scratch = new BytesRef(10);
  1405. + TreeMap<String,Long> fields = new TreeMap<String,Long>();
  1406. +
  1407. + while (true) {
  1408. + SimpleTextUtil.readLine(in, scratch);
  1409. + if (scratch.equals(END)) {
  1410. + return fields;
  1411. + } else if (StringHelper.startsWith(scratch, FIELD)) {
  1412. + String fieldName = new String(scratch.bytes, scratch.offset + FIELD.length, scratch.length - FIELD.length, "UTF-8");
  1413. + fields.put(fieldName, in.getFilePointer());
  1414. }
  1415. }
  1416. -
  1417. - @Override
  1418. - public Terms terms() throws IOException {
  1419. - return SimpleTextFieldsReader.this.terms(current);
  1420. - }
  1421. }
  1422.  
  1423. private class SimpleTextTermsEnum extends TermsEnum {
  1424. @@ -628,8 +618,8 @@
  1425. }
  1426.  
  1427. @Override
  1428. - public FieldsEnum iterator() throws IOException {
  1429. - return new SimpleTextFieldsEnum();
  1430. + public Iterator<String> iterator() {
  1431. + return new UnmodifiableIterator<String>(fields.keySet().iterator());
  1432. }
  1433.  
  1434. private final Map<String,Terms> termsCache = new HashMap<String,Terms>();
  1435. @@ -638,15 +628,13 @@
  1436. synchronized public Terms terms(String field) throws IOException {
  1437. Terms terms = termsCache.get(field);
  1438. if (terms == null) {
  1439. - SimpleTextFieldsEnum fe = (SimpleTextFieldsEnum) iterator();
  1440. - String fieldUpto;
  1441. - while((fieldUpto = fe.next()) != null) {
  1442. - if (fieldUpto.equals(field)) {
  1443. - terms = new SimpleTextTerms(field, fe.in.getFilePointer());
  1444. - break;
  1445. - }
  1446. + Long fp = fields.get(field);
  1447. + if (fp == null) {
  1448. + return null;
  1449. + } else {
  1450. + terms = new SimpleTextTerms(field, fp);
  1451. + termsCache.put(field, terms);
  1452. }
  1453. - termsCache.put(field, terms);
  1454. }
  1455. return terms;
  1456. }
  1457. Index: lucene/core/src/java/org/apache/lucene/codecs/simpletext/SimpleTextTermVectorsReader.java
  1458. ===================================================================
  1459. --- lucene/core/src/java/org/apache/lucene/codecs/simpletext/SimpleTextTermVectorsReader.java (revision 1372046)
  1460. +++ lucene/core/src/java/org/apache/lucene/codecs/simpletext/SimpleTextTermVectorsReader.java (working copy)
  1461. @@ -29,7 +29,6 @@
  1462. import org.apache.lucene.index.DocsAndPositionsEnum;
  1463. import org.apache.lucene.index.DocsEnum;
  1464. import org.apache.lucene.index.Fields;
  1465. -import org.apache.lucene.index.FieldsEnum;
  1466. import org.apache.lucene.index.IndexFileNames;
  1467. import org.apache.lucene.index.SegmentInfo;
  1468. import org.apache.lucene.index.Terms;
  1469. @@ -45,6 +44,7 @@
  1470. import org.apache.lucene.util.IOUtils;
  1471. import org.apache.lucene.util.StringHelper;
  1472. import org.apache.lucene.util.UnicodeUtil;
  1473. +import org.apache.lucene.util.UnmodifiableIterator;
  1474.  
  1475. import static org.apache.lucene.codecs.simpletext.SimpleTextTermVectorsWriter.*;
  1476.  
  1477. @@ -240,26 +240,8 @@
  1478. }
  1479.  
  1480. @Override
  1481. - public FieldsEnum iterator() throws IOException {
  1482. - return new FieldsEnum() {
  1483. - private Iterator<Map.Entry<String,SimpleTVTerms>> iterator = fields.entrySet().iterator();
  1484. - private Map.Entry<String,SimpleTVTerms> current = null;
  1485. -
  1486. - @Override
  1487. - public String next() {
  1488. - if (!iterator.hasNext()) {
  1489. - return null;
  1490. - } else {
  1491. - current = iterator.next();
  1492. - return current.getKey();
  1493. - }
  1494. - }
  1495. -
  1496. - @Override
  1497. - public Terms terms() {
  1498. - return current.getValue();
  1499. - }
  1500. - };
  1501. + public Iterator<String> iterator() {
  1502. + return new UnmodifiableIterator<String>(fields.keySet().iterator());
  1503. }
  1504.  
  1505. @Override
  1506. Index: lucene/core/src/java/org/apache/lucene/codecs/BlockTreeTermsReader.java
  1507. ===================================================================
  1508. --- lucene/core/src/java/org/apache/lucene/codecs/BlockTreeTermsReader.java (revision 1372046)
  1509. +++ lucene/core/src/java/org/apache/lucene/codecs/BlockTreeTermsReader.java (working copy)
  1510. @@ -31,7 +31,6 @@
  1511. import org.apache.lucene.index.FieldInfo.IndexOptions;
  1512. import org.apache.lucene.index.FieldInfo;
  1513. import org.apache.lucene.index.FieldInfos;
  1514. -import org.apache.lucene.index.FieldsEnum;
  1515. import org.apache.lucene.index.IndexFileNames;
  1516. import org.apache.lucene.index.TermState;
  1517. import org.apache.lucene.index.Terms;
  1518. @@ -46,6 +45,7 @@
  1519. import org.apache.lucene.util.IOUtils;
  1520. import org.apache.lucene.util.RamUsageEstimator;
  1521. import org.apache.lucene.util.StringHelper;
  1522. +import org.apache.lucene.util.UnmodifiableIterator;
  1523. import org.apache.lucene.util.automaton.CompiledAutomaton;
  1524. import org.apache.lucene.util.automaton.RunAutomaton;
  1525. import org.apache.lucene.util.automaton.Transition;
  1526. @@ -199,8 +199,8 @@
  1527. }
  1528.  
  1529. @Override
  1530. - public FieldsEnum iterator() {
  1531. - return new TermFieldsEnum();
  1532. + public Iterator<String> iterator() {
  1533. + return new UnmodifiableIterator<String>(fields.keySet().iterator());
  1534. }
  1535.  
  1536. @Override
  1537. @@ -214,32 +214,6 @@
  1538. return fields.size();
  1539. }
  1540.  
  1541. - // Iterates through all fields
  1542. - private class TermFieldsEnum extends FieldsEnum {
  1543. - final Iterator<FieldReader> it;
  1544. - FieldReader current;
  1545. -
  1546. - TermFieldsEnum() {
  1547. - it = fields.values().iterator();
  1548. - }
  1549. -
  1550. - @Override
  1551. - public String next() {
  1552. - if (it.hasNext()) {
  1553. - current = it.next();
  1554. - return current.fieldInfo.name;
  1555. - } else {
  1556. - current = null;
  1557. - return null;
  1558. - }
  1559. - }
  1560. -
  1561. - @Override
  1562. - public Terms terms() throws IOException {
  1563. - return current;
  1564. - }
  1565. - }
  1566. -
  1567. // for debugging
  1568. String brToString(BytesRef b) {
  1569. if (b == null) {
  1570. Index: lucene/core/src/java/org/apache/lucene/codecs/TermVectorsWriter.java
  1571. ===================================================================
  1572. --- lucene/core/src/java/org/apache/lucene/codecs/TermVectorsWriter.java (revision 1372046)
  1573. +++ lucene/core/src/java/org/apache/lucene/codecs/TermVectorsWriter.java (working copy)
  1574. @@ -26,7 +26,6 @@
  1575. import org.apache.lucene.index.FieldInfo;
  1576. import org.apache.lucene.index.FieldInfos;
  1577. import org.apache.lucene.index.Fields;
  1578. -import org.apache.lucene.index.FieldsEnum;
  1579. import org.apache.lucene.index.MergeState;
  1580. import org.apache.lucene.index.PayloadProcessorProvider.PayloadProcessor;
  1581. import org.apache.lucene.index.PayloadProcessorProvider.ReaderPayloadProcessor;
  1582. @@ -211,8 +210,6 @@
  1583. }
  1584. startDocument(numFields);
  1585.  
  1586. - final FieldsEnum fieldsEnum = vectors.iterator();
  1587. - String fieldName;
  1588. String lastFieldName = null;
  1589.  
  1590. TermsEnum termsEnum = null;
  1591. @@ -221,13 +218,13 @@
  1592. final ReaderPayloadProcessor readerPayloadProcessor = mergeState.currentReaderPayloadProcessor;
  1593. PayloadProcessor payloadProcessor = null;
  1594.  
  1595. - while((fieldName = fieldsEnum.next()) != null) {
  1596. + for(String fieldName : vectors) {
  1597. final FieldInfo fieldInfo = mergeState.fieldInfos.fieldInfo(fieldName);
  1598.  
  1599. assert lastFieldName == null || fieldName.compareTo(lastFieldName) > 0: "lastFieldName=" + lastFieldName + " fieldName=" + fieldName;
  1600. lastFieldName = fieldName;
  1601.  
  1602. - final Terms terms = fieldsEnum.terms();
  1603. + final Terms terms = vectors.terms(fieldName);
  1604. if (terms == null) {
  1605. // FieldsEnum shouldn't lie...
  1606. continue;
  1607. Index: lucene/core/src/java/org/apache/lucene/codecs/perfield/PerFieldPostingsFormat.java
  1608. ===================================================================
  1609. --- lucene/core/src/java/org/apache/lucene/codecs/perfield/PerFieldPostingsFormat.java (revision 1372046)
  1610. +++ lucene/core/src/java/org/apache/lucene/codecs/perfield/PerFieldPostingsFormat.java (working copy)
  1611. @@ -30,11 +30,11 @@
  1612. import org.apache.lucene.codecs.PostingsFormat;
  1613. import org.apache.lucene.codecs.TermsConsumer;
  1614. import org.apache.lucene.index.FieldInfo;
  1615. -import org.apache.lucene.index.FieldsEnum;
  1616. import org.apache.lucene.index.SegmentReadState;
  1617. import org.apache.lucene.index.SegmentWriteState;
  1618. import org.apache.lucene.index.Terms;
  1619. import org.apache.lucene.util.IOUtils;
  1620. +import org.apache.lucene.util.UnmodifiableIterator;
  1621.  
  1622. /**
  1623. * Enables per field format support.
  1624. @@ -197,34 +197,9 @@
  1625. }
  1626. }
  1627.  
  1628. - private final class FieldsIterator extends FieldsEnum {
  1629. - private final Iterator<String> it;
  1630. - private String current;
  1631. -
  1632. - public FieldsIterator() {
  1633. - it = fields.keySet().iterator();
  1634. - }
  1635. -
  1636. - @Override
  1637. - public String next() {
  1638. - if (it.hasNext()) {
  1639. - current = it.next();
  1640. - } else {
  1641. - current = null;
  1642. - }
  1643. -
  1644. - return current;
  1645. - }
  1646. -
  1647. - @Override
  1648. - public Terms terms() throws IOException {
  1649. - return fields.get(current).terms(current);
  1650. - }
  1651. - }
  1652. -
  1653. @Override
  1654. - public FieldsEnum iterator() throws IOException {
  1655. - return new FieldsIterator();
  1656. + public Iterator<String> iterator() {
  1657. + return new UnmodifiableIterator<String>(fields.keySet().iterator());
  1658. }
  1659.  
  1660. @Override
  1661. Index: lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40TermVectorsReader.java
  1662. ===================================================================
  1663. --- lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40TermVectorsReader.java (revision 1372046)
  1664. +++ lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40TermVectorsReader.java (working copy)
  1665. @@ -21,7 +21,9 @@
  1666. import java.util.Arrays;
  1667. import java.util.Comparator;
  1668. import java.util.HashMap;
  1669. +import java.util.Iterator;
  1670. import java.util.Map;
  1671. +import java.util.NoSuchElementException;
  1672.  
  1673. import org.apache.lucene.codecs.CodecUtil;
  1674. import org.apache.lucene.codecs.TermVectorsReader;
  1675. @@ -30,7 +32,6 @@
  1676. import org.apache.lucene.index.FieldInfo;
  1677. import org.apache.lucene.index.FieldInfos;
  1678. import org.apache.lucene.index.Fields;
  1679. -import org.apache.lucene.index.FieldsEnum;
  1680. import org.apache.lucene.index.IndexFileNames;
  1681. import org.apache.lucene.index.SegmentInfo;
  1682. import org.apache.lucene.index.Terms;
  1683. @@ -249,9 +250,8 @@
  1684. }
  1685.  
  1686. @Override
  1687. - public FieldsEnum iterator() throws IOException {
  1688. -
  1689. - return new FieldsEnum() {
  1690. + public Iterator<String> iterator() {
  1691. + return new Iterator<String>() {
  1692. private int fieldUpto;
  1693.  
  1694. @Override
  1695. @@ -259,14 +259,19 @@
  1696. if (fieldNumbers != null && fieldUpto < fieldNumbers.length) {
  1697. return fieldInfos.fieldInfo(fieldNumbers[fieldUpto++]).name;
  1698. } else {
  1699. - return null;
  1700. + throw new NoSuchElementException();
  1701. }
  1702. }
  1703.  
  1704. @Override
  1705. - public Terms terms() throws IOException {
  1706. - return TVFields.this.terms(fieldInfos.fieldInfo(fieldNumbers[fieldUpto-1]).name);
  1707. + public boolean hasNext() {
  1708. + return fieldNumbers != null && fieldUpto < fieldNumbers.length;
  1709. }
  1710. +
  1711. + @Override
  1712. + public void remove() {
  1713. + throw new UnsupportedOperationException();
  1714. + }
  1715. };
  1716. }
  1717.  
  1718. Index: lucene/core/src/java/org/apache/lucene/index/CheckIndex.java
  1719. ===================================================================
  1720. --- lucene/core/src/java/org/apache/lucene/index/CheckIndex.java (revision 1372046)
  1721. +++ lucene/core/src/java/org/apache/lucene/index/CheckIndex.java (working copy)
  1722. @@ -685,12 +685,7 @@
  1723. DocsAndPositionsEnum postings = null;
  1724.  
  1725. String lastField = null;
  1726. - final FieldsEnum fieldsEnum = fields.iterator();
  1727. - while(true) {
  1728. - final String field = fieldsEnum.next();
  1729. - if (field == null) {
  1730. - break;
  1731. - }
  1732. + for (String field : fields) {
  1733. // MultiFieldsEnum relies upon this order...
  1734. if (lastField != null && field.compareTo(lastField) <= 0) {
  1735. throw new RuntimeException("fields out of order: lastField=" + lastField + " field=" + field);
  1736. @@ -713,7 +708,7 @@
  1737. // assert fields.terms(field) != null;
  1738. computedFieldCount++;
  1739.  
  1740. - final Terms terms = fieldsEnum.terms();
  1741. + final Terms terms = fields.terms(field);
  1742. if (terms == null) {
  1743. continue;
  1744. }
  1745. @@ -989,11 +984,7 @@
  1746. // only happen if it's a ghost field (field with
  1747. // no terms, eg there used to be terms but all
  1748. // docs got deleted and then merged away):
  1749. - // make sure TermsEnum is empty:
  1750. - final Terms fieldTerms2 = fieldsEnum.terms();
  1751. - if (fieldTerms2 != null && fieldTerms2.iterator(null).next() != null) {
  1752. - throw new RuntimeException("Fields.terms(field=" + field + ") returned null yet the field appears to have terms");
  1753. - }
  1754. +
  1755. } else {
  1756. if (fieldTerms instanceof BlockTreeTermsReader.FieldReader) {
  1757. final BlockTreeTermsReader.Stats stats = ((BlockTreeTermsReader.FieldReader) fieldTerms).computeStats();
  1758. @@ -1404,9 +1395,7 @@
  1759. status.docCount++;
  1760. }
  1761.  
  1762. - FieldsEnum fieldsEnum = tfv.iterator();
  1763. - String field = null;
  1764. - while((field = fieldsEnum.next()) != null) {
  1765. + for(String field : tfv) {
  1766. if (doStats) {
  1767. status.totVectors++;
  1768. }
  1769. Index: lucene/core/src/java/org/apache/lucene/index/ParallelAtomicReader.java
  1770. ===================================================================
  1771. --- lucene/core/src/java/org/apache/lucene/index/ParallelAtomicReader.java (revision 1372046)
  1772. +++ lucene/core/src/java/org/apache/lucene/index/ParallelAtomicReader.java (working copy)
  1773. @@ -27,6 +27,7 @@
  1774. import java.util.TreeMap;
  1775.  
  1776. import org.apache.lucene.util.Bits;
  1777. +import org.apache.lucene.util.UnmodifiableIterator;
  1778.  
  1779.  
  1780. /** An {@link AtomicReader} which reads multiple, parallel indexes. Each index
  1781. @@ -121,12 +122,10 @@
  1782. for (final AtomicReader reader : this.parallelReaders) {
  1783. final Fields readerFields = reader.fields();
  1784. if (readerFields != null) {
  1785. - final FieldsEnum it = readerFields.iterator();
  1786. - String name;
  1787. - while ((name = it.next()) != null) {
  1788. + for (String field : readerFields) {
  1789. // only add if the reader responsible for that field name is the current:
  1790. - if (fieldToReader.get(name) == reader) {
  1791. - this.fields.addField(name, it.terms());
  1792. + if (fieldToReader.get(field) == reader) {
  1793. + this.fields.addField(field, readerFields.terms(field));
  1794. }
  1795. }
  1796. }
  1797. @@ -151,33 +150,6 @@
  1798. return buffer.append(')').toString();
  1799. }
  1800.  
  1801. - private final class ParallelFieldsEnum extends FieldsEnum {
  1802. - private String currentField;
  1803. - private final Iterator<String> keys;
  1804. - private final ParallelFields fields;
  1805. -
  1806. - ParallelFieldsEnum(ParallelFields fields) {
  1807. - this.fields = fields;
  1808. - keys = fields.fields.keySet().iterator();
  1809. - }
  1810. -
  1811. - @Override
  1812. - public String next() {
  1813. - if (keys.hasNext()) {
  1814. - currentField = keys.next();
  1815. - } else {
  1816. - currentField = null;
  1817. - }
  1818. - return currentField;
  1819. - }
  1820. -
  1821. - @Override
  1822. - public Terms terms() {
  1823. - return fields.terms(currentField);
  1824. - }
  1825. -
  1826. - }
  1827. -
  1828. // Single instance of this, per ParallelReader instance
  1829. private final class ParallelFields extends Fields {
  1830. final Map<String,Terms> fields = new TreeMap<String,Terms>();
  1831. @@ -190,8 +162,8 @@
  1832. }
  1833.  
  1834. @Override
  1835. - public FieldsEnum iterator() {
  1836. - return new ParallelFieldsEnum(this);
  1837. + public Iterator<String> iterator() {
  1838. + return new UnmodifiableIterator<String>(fields.keySet().iterator());
  1839. }
  1840.  
  1841. @Override
  1842. Index: lucene/core/src/java/org/apache/lucene/index/FilterAtomicReader.java
  1843. ===================================================================
  1844. --- lucene/core/src/java/org/apache/lucene/index/FilterAtomicReader.java (revision 1372046)
  1845. +++ lucene/core/src/java/org/apache/lucene/index/FilterAtomicReader.java (working copy)
  1846. @@ -24,6 +24,7 @@
  1847.  
  1848. import java.io.IOException;
  1849. import java.util.Comparator;
  1850. +import java.util.Iterator;
  1851.  
  1852. /** A <code>FilterAtomicReader</code> contains another AtomicReader, which it
  1853. * uses as its basic source of data, possibly transforming the data along the
  1854. @@ -46,7 +47,7 @@
  1855. }
  1856.  
  1857. @Override
  1858. - public FieldsEnum iterator() throws IOException {
  1859. + public Iterator<String> iterator() {
  1860. return in.iterator();
  1861. }
  1862.  
  1863. @@ -127,29 +128,6 @@
  1864. }
  1865.  
  1866. /** Base class for filtering {@link TermsEnum} implementations. */
  1867. - public static class FilterFieldsEnum extends FieldsEnum {
  1868. - protected final FieldsEnum in;
  1869. - public FilterFieldsEnum(FieldsEnum in) {
  1870. - this.in = in;
  1871. - }
  1872. -
  1873. - @Override
  1874. - public String next() throws IOException {
  1875. - return in.next();
  1876. - }
  1877. -
  1878. - @Override
  1879. - public Terms terms() throws IOException {
  1880. - return in.terms();
  1881. - }
  1882. -
  1883. - @Override
  1884. - public AttributeSource attributes() {
  1885. - return in.attributes();
  1886. - }
  1887. - }
  1888. -
  1889. - /** Base class for filtering {@link TermsEnum} implementations. */
  1890. public static class FilterTermsEnum extends TermsEnum {
  1891. protected final TermsEnum in;
  1892.  
  1893. Index: lucene/core/src/java/org/apache/lucene/index/Fields.java
  1894. ===================================================================
  1895. --- lucene/core/src/java/org/apache/lucene/index/Fields.java (revision 1372046)
  1896. +++ lucene/core/src/java/org/apache/lucene/index/Fields.java (working copy)
  1897. @@ -18,15 +18,16 @@
  1898. */
  1899.  
  1900. import java.io.IOException;
  1901. +import java.util.Iterator;
  1902.  
  1903. /** Flex API for access to fields and terms
  1904. * @lucene.experimental */
  1905.  
  1906. -public abstract class Fields {
  1907. +public abstract class Fields implements Iterable<String> {
  1908.  
  1909. /** Returns an iterator that will step through all fields
  1910. * names. This will not return null. */
  1911. - public abstract FieldsEnum iterator() throws IOException;
  1912. + public abstract Iterator<String> iterator();
  1913.  
  1914. /** Get the {@link Terms} for this field. This will return
  1915. * null if the field does not exist. */
  1916. @@ -45,12 +46,7 @@
  1917. // TODO: deprecate?
  1918. public long getUniqueTermCount() throws IOException {
  1919. long numTerms = 0;
  1920. - FieldsEnum it = iterator();
  1921. - while(true) {
  1922. - String field = it.next();
  1923. - if (field == null) {
  1924. - break;
  1925. - }
  1926. + for (String field : this) {
  1927. Terms terms = terms(field);
  1928. if (terms != null) {
  1929. final long termCount = terms.size();
  1930. Index: lucene/core/src/java/org/apache/lucene/index/FieldsEnum.java
  1931. ===================================================================
  1932. --- lucene/core/src/java/org/apache/lucene/index/FieldsEnum.java (revision 1372046)
  1933. +++ lucene/core/src/java/org/apache/lucene/index/FieldsEnum.java (working copy)
  1934. @@ -1,79 +0,0 @@
  1935. -package org.apache.lucene.index;
  1936. -
  1937. -/*
  1938. - * Licensed to the Apache Software Foundation (ASF) under one or more
  1939. - * contributor license agreements. See the NOTICE file distributed with
  1940. - * this work for additional information regarding copyright ownership.
  1941. - * The ASF licenses this file to You under the Apache License, Version 2.0
  1942. - * (the "License"); you may not use this file except in compliance with
  1943. - * the License. You may obtain a copy of the License at
  1944. - *
  1945. - * http://www.apache.org/licenses/LICENSE-2.0
  1946. - *
  1947. - * Unless required by applicable law or agreed to in writing, software
  1948. - * distributed under the License is distributed on an "AS IS" BASIS,
  1949. - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  1950. - * See the License for the specific language governing permissions and
  1951. - * limitations under the License.
  1952. - */
  1953. -
  1954. -import java.io.IOException;
  1955. -
  1956. -import org.apache.lucene.util.AttributeSource;
  1957. -
  1958. -/** Enumerates indexed fields. You must first call {@link
  1959. - * #next} before calling {@link #terms}.
  1960. - *
  1961. - * @lucene.experimental */
  1962. -
  1963. -public abstract class FieldsEnum {
  1964. -
  1965. - // TODO: maybe allow retrieving FieldInfo for current
  1966. - // field, as optional method?
  1967. -
  1968. - private AttributeSource atts = null;
  1969. -
  1970. - /**
  1971. - * Returns the related attributes.
  1972. - */
  1973. - public AttributeSource attributes() {
  1974. - if (atts == null) {
  1975. - atts = new AttributeSource();
  1976. - }
  1977. - return atts;
  1978. - }
  1979. -
  1980. - /** Increments the enumeration to the next field. Returns
  1981. - * null when there are no more fields.*/
  1982. - public abstract String next() throws IOException;
  1983. -
  1984. - // TODO: would be nice to require/fix all impls so they
  1985. - // never return null here... we have to fix the writers to
  1986. - // never write 0-terms fields... or maybe allow a non-null
  1987. - // Terms instance in just this case
  1988. -
  1989. - /** Get {@link Terms} for the current field. After {@link #next} returns
  1990. - * null this method should not be called. This method may
  1991. - * return null in some cases, which means the provided
  1992. - * field does not have any terms. */
  1993. - public abstract Terms terms() throws IOException;
  1994. -
  1995. - // TODO: should we allow pulling Terms as well? not just
  1996. - // the iterator?
  1997. -
  1998. - public final static FieldsEnum[] EMPTY_ARRAY = new FieldsEnum[0];
  1999. -
  2000. - /** Provides zero fields */
  2001. - public final static FieldsEnum EMPTY = new FieldsEnum() {
  2002. -
  2003. - @Override
  2004. - public String next() {
  2005. - return null;
  2006. - }
  2007. -
  2008. - @Override
  2009. - public Terms terms() {
  2010. - throw new IllegalStateException("this method should never be called");
  2011. - }
  2012. - };
  2013. -}
  2014. Index: lucene/core/src/java/org/apache/lucene/index/MultiFieldsEnum.java
  2015. ===================================================================
  2016. --- lucene/core/src/java/org/apache/lucene/index/MultiFieldsEnum.java (revision 1372046)
  2017. +++ lucene/core/src/java/org/apache/lucene/index/MultiFieldsEnum.java (working copy)
  2018. @@ -1,133 +0,0 @@
  2019. -package org.apache.lucene.index;
  2020. -
  2021. -/*
  2022. - * Licensed to the Apache Software Foundation (ASF) under one or more
  2023. - * contributor license agreements. See the NOTICE file distributed with
  2024. - * this work for additional information regarding copyright ownership.
  2025. - * The ASF licenses this file to You under the Apache License, Version 2.0
  2026. - * (the "License"); you may not use this file except in compliance with
  2027. - * the License. You may obtain a copy of the License at
  2028. - *
  2029. - * http://www.apache.org/licenses/LICENSE-2.0
  2030. - *
  2031. - * Unless required by applicable law or agreed to in writing, software
  2032. - * distributed under the License is distributed on an "AS IS" BASIS,
  2033. - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  2034. - * See the License for the specific language governing permissions and
  2035. - * limitations under the License.
  2036. - */
  2037. -
  2038. -import org.apache.lucene.util.PriorityQueue;
  2039. -
  2040. -import java.io.IOException;
  2041. -import java.util.List;
  2042. -import java.util.ArrayList;
  2043. -
  2044. -/**
  2045. - * Exposes flex API, merged from flex API of sub-segments.
  2046. - * This does a merge sort, by field name, of the
  2047. - * sub-readers.
  2048. - *
  2049. - * @lucene.experimental
  2050. - */
  2051. -
  2052. -public final class MultiFieldsEnum extends FieldsEnum {
  2053. - private final FieldMergeQueue queue;
  2054. -
  2055. - // Holds sub-readers containing field we are currently
  2056. - // on, popped from queue.
  2057. - private final FieldsEnumWithSlice[] top;
  2058. -
  2059. - private int numTop;
  2060. -
  2061. - private final Fields fields;
  2062. -
  2063. - private String currentField;
  2064. -
  2065. - /** The subs array must be newly initialized FieldsEnum
  2066. - * (ie, {@link FieldsEnum#next} has not been called. */
  2067. - public MultiFieldsEnum(MultiFields fields, FieldsEnum[] subs, ReaderSlice[] subSlices) throws IOException {
  2068. - this.fields = fields;
  2069. - queue = new FieldMergeQueue(subs.length);
  2070. - top = new FieldsEnumWithSlice[subs.length];
  2071. - List<FieldsEnumWithSlice> enumWithSlices = new ArrayList<FieldsEnumWithSlice>();
  2072. -
  2073. - // Init q
  2074. - for(int i=0;i<subs.length;i++) {
  2075. - assert subs[i] != null;
  2076. - final String field = subs[i].next();
  2077. - if (field != null) {
  2078. - // this FieldsEnum has at least one field
  2079. - final FieldsEnumWithSlice sub = new FieldsEnumWithSlice(subs[i], subSlices[i], i);
  2080. - enumWithSlices.add(sub);
  2081. - sub.current = field;
  2082. - queue.add(sub);
  2083. - }
  2084. - }
  2085. - }
  2086. -
  2087. - @Override
  2088. - public String next() throws IOException {
  2089. -
  2090. - // restore queue
  2091. - for(int i=0;i<numTop;i++) {
  2092. - top[i].current = top[i].fields.next();
  2093. - if (top[i].current != null) {
  2094. - queue.add(top[i]);
  2095. - } else {
  2096. - // no more fields in this sub-reader
  2097. - }
  2098. - }
  2099. -
  2100. - numTop = 0;
  2101. -
  2102. - // gather equal top fields
  2103. - if (queue.size() > 0) {
  2104. - while(true) {
  2105. - top[numTop++] = queue.pop();
  2106. - if (queue.size() == 0 || !(queue.top()).current.equals(top[0].current)) {
  2107. - break;
  2108. - }
  2109. - }
  2110. - currentField = top[0].current;
  2111. - } else {
  2112. - currentField = null;
  2113. - }
  2114. -
  2115. - return currentField;
  2116. - }
  2117. -
  2118. - @Override
  2119. - public Terms terms() throws IOException {
  2120. - // Ask our parent MultiFields:
  2121. - return fields.terms(currentField);
  2122. - }
  2123. -
  2124. - public final static class FieldsEnumWithSlice {
  2125. - public static final FieldsEnumWithSlice[] EMPTY_ARRAY = new FieldsEnumWithSlice[0];
  2126. - final FieldsEnum fields;
  2127. - final ReaderSlice slice;
  2128. - final int index;
  2129. - String current;
  2130. -
  2131. - public FieldsEnumWithSlice(FieldsEnum fields, ReaderSlice slice, int index) {
  2132. - this.slice = slice;
  2133. - this.index = index;
  2134. - assert slice.length >= 0: "length=" + slice.length;
  2135. - this.fields = fields;
  2136. - }
  2137. - }
  2138. -
  2139. - private final static class FieldMergeQueue extends PriorityQueue<FieldsEnumWithSlice> {
  2140. - FieldMergeQueue(int size) {
  2141. - super(size);
  2142. - }
  2143. -
  2144. - @Override
  2145. - protected final boolean lessThan(FieldsEnumWithSlice fieldsA, FieldsEnumWithSlice fieldsB) {
  2146. - // No need to break ties by field name: TermsEnum handles that
  2147. - return fieldsA.current.compareTo(fieldsB.current) < 0;
  2148. - }
  2149. - }
  2150. -}
  2151. -
  2152. Index: lucene/core/src/java/org/apache/lucene/index/DocValues.java
  2153. ===================================================================
  2154. --- lucene/core/src/java/org/apache/lucene/index/DocValues.java (revision 1372046)
  2155. +++ lucene/core/src/java/org/apache/lucene/index/DocValues.java (working copy)
  2156. @@ -105,7 +105,7 @@
  2157. * <p>
  2158. * {@link Source} instances obtained from this method are closed / released
  2159. * from the cache once this {@link DocValues} instance is closed by the
  2160. - * {@link IndexReader}, {@link Fields} or {@link FieldsEnum} the
  2161. + * {@link IndexReader}, {@link Fields} or the
  2162. * {@link DocValues} was created from.
  2163. */
  2164. public Source getSource() throws IOException {
  2165. Index: lucene/core/src/java/org/apache/lucene/index/MultiFields.java
  2166. ===================================================================
  2167. --- lucene/core/src/java/org/apache/lucene/index/MultiFields.java (revision 1372046)
  2168. +++ lucene/core/src/java/org/apache/lucene/index/MultiFields.java (working copy)
  2169. @@ -19,6 +19,7 @@
  2170.  
  2171. import java.io.IOException;
  2172. import java.util.ArrayList;
  2173. +import java.util.Iterator;
  2174. import java.util.List;
  2175. import java.util.Map;
  2176. import java.util.Collection;
  2177. @@ -27,6 +28,7 @@
  2178.  
  2179. import org.apache.lucene.util.Bits;
  2180. import org.apache.lucene.util.BytesRef;
  2181. +import org.apache.lucene.util.MergedIterator;
  2182.  
  2183. /**
  2184. * Exposes flex API, merged from flex API of sub-segments.
  2185. @@ -180,22 +182,14 @@
  2186. this.subSlices = subSlices;
  2187. }
  2188.  
  2189. + @SuppressWarnings("unchecked")
  2190. @Override
  2191. - public FieldsEnum iterator() throws IOException {
  2192. -
  2193. - final List<FieldsEnum> fieldsEnums = new ArrayList<FieldsEnum>();
  2194. - final List<ReaderSlice> fieldsSlices = new ArrayList<ReaderSlice>();
  2195. + public Iterator<String> iterator() {
  2196. + Iterator<String> subIterators[] = new Iterator[subs.length];
  2197. for(int i=0;i<subs.length;i++) {
  2198. - fieldsEnums.add(subs[i].iterator());
  2199. - fieldsSlices.add(subSlices[i]);
  2200. + subIterators[i] = subs[i].iterator();
  2201. }
  2202. - if (fieldsEnums.size() == 0) {
  2203. - return FieldsEnum.EMPTY;
  2204. - } else {
  2205. - return new MultiFieldsEnum(this,
  2206. - fieldsEnums.toArray(FieldsEnum.EMPTY_ARRAY),
  2207. - fieldsSlices.toArray(ReaderSlice.EMPTY_ARRAY));
  2208. - }
  2209. + return new MergedIterator<String>(subIterators);
  2210. }
  2211.  
  2212. @Override
  2213. Index: lucene/core/src/java/org/apache/lucene/index/CoalescedDeletes.java
  2214. ===================================================================
  2215. --- lucene/core/src/java/org/apache/lucene/index/CoalescedDeletes.java (revision 1372046)
  2216. +++ lucene/core/src/java/org/apache/lucene/index/CoalescedDeletes.java (working copy)
  2217. @@ -24,7 +24,7 @@
  2218. import java.util.Map;
  2219.  
  2220. import org.apache.lucene.search.Query;
  2221. -import org.apache.lucene.util.PriorityQueue;
  2222. +import org.apache.lucene.util.MergedIterator;
  2223. import org.apache.lucene.index.BufferedDeletesStream.QueryAndLimit;
  2224.  
  2225. class CoalescedDeletes {
  2226. @@ -48,13 +48,14 @@
  2227.  
  2228. public Iterable<Term> termsIterable() {
  2229. return new Iterable<Term>() {
  2230. + @SuppressWarnings("unchecked")
  2231. @Override
  2232. public Iterator<Term> iterator() {
  2233. - ArrayList<Iterator<Term>> subs = new ArrayList<Iterator<Term>>(iterables.size());
  2234. - for (Iterable<Term> iterable : iterables) {
  2235. - subs.add(iterable.iterator());
  2236. + Iterator<Term> subs[] = new Iterator[iterables.size()];
  2237. + for (int i = 0; i < iterables.size(); i++) {
  2238. + subs[i] = iterables.get(i).iterator();
  2239. }
  2240. - return mergedIterator(subs);
  2241. + return new MergedIterator<Term>(subs);
  2242. }
  2243. };
  2244. }
  2245. @@ -86,106 +87,4 @@
  2246. }
  2247. };
  2248. }
  2249. -
  2250. - /** provides a merged view across multiple iterators */
  2251. - static Iterator<Term> mergedIterator(final List<Iterator<Term>> iterators) {
  2252. - return new Iterator<Term>() {
  2253. - Term current;
  2254. - TermMergeQueue queue = new TermMergeQueue(iterators.size());
  2255. - SubIterator[] top = new SubIterator[iterators.size()];
  2256. - int numTop;
  2257. -
  2258. - {
  2259. - int index = 0;
  2260. - for (Iterator<Term> iterator : iterators) {
  2261. - if (iterator.hasNext()) {
  2262. - SubIterator sub = new SubIterator();
  2263. - sub.current = iterator.next();
  2264. - sub.iterator = iterator;
  2265. - sub.index = index++;
  2266. - queue.add(sub);
  2267. - }
  2268. - }
  2269. - }
  2270. -
  2271. - public boolean hasNext() {
  2272. - if (queue.size() > 0) {
  2273. - return true;
  2274. - }
  2275. -
  2276. - for (int i = 0; i < numTop; i++) {
  2277. - if (top[i].iterator.hasNext()) {
  2278. - return true;
  2279. - }
  2280. - }
  2281. - return false;
  2282. - }
  2283. -
  2284. - public Term next() {
  2285. - // restore queue
  2286. - pushTop();
  2287. -
  2288. - // gather equal top fields
  2289. - if (queue.size() > 0) {
  2290. - pullTop();
  2291. - } else {
  2292. - current = null;
  2293. - }
  2294. - return current;
  2295. - }
  2296. -
  2297. - public void remove() {
  2298. - throw new UnsupportedOperationException();
  2299. - }
  2300. -
  2301. - private void pullTop() {
  2302. - // extract all subs from the queue that have the same top term
  2303. - assert numTop == 0;
  2304. - while (true) {
  2305. - top[numTop++] = queue.pop();
  2306. - if (queue.size() == 0
  2307. - || !(queue.top()).current.equals(top[0].current)) {
  2308. - break;
  2309. - }
  2310. - }
  2311. - current = top[0].current;
  2312. - }
  2313. -
  2314. - private void pushTop() {
  2315. - // call next() on each top, and put back into queue
  2316. - for (int i = 0; i < numTop; i++) {
  2317. - if (top[i].iterator.hasNext()) {
  2318. - top[i].current = top[i].iterator.next();
  2319. - queue.add(top[i]);
  2320. - } else {
  2321. - // no more terms
  2322. - top[i].current = null;
  2323. - }
  2324. - }
  2325. - numTop = 0;
  2326. - }
  2327. - };
  2328. - }
  2329. -
  2330. - private static class SubIterator {
  2331. - Iterator<Term> iterator;
  2332. - Term current;
  2333. - int index;
  2334. - }
  2335. -
  2336. - private static class TermMergeQueue extends PriorityQueue<SubIterator> {
  2337. - TermMergeQueue(int size) {
  2338. - super(size);
  2339. - }
  2340. -
  2341. - @Override
  2342. - protected boolean lessThan(SubIterator a, SubIterator b) {
  2343. - final int cmp = a.current.compareTo(b.current);
  2344. - if (cmp != 0) {
  2345. - return cmp < 0;
  2346. - } else {
  2347. - return a.index < b.index;
  2348. - }
  2349. - }
  2350. - }
  2351. }
  2352. Index: lucene/MIGRATE.txt
  2353. ===================================================================
  2354. --- lucene/MIGRATE.txt (revision 1372046)
  2355. +++ lucene/MIGRATE.txt (working copy)
  2356. @@ -9,7 +9,7 @@
  2357. by the BytesRef class (which provides an offset + length "slice"
  2358. into an existing byte[]).
  2359.  
  2360. - * Fields are separately enumerated (FieldsEnum) from the terms
  2361. + * Fields are separately enumerated (Fields.iterator()) from the terms
  2362. within each field (TermEnum). So instead of this:
  2363.  
  2364. TermEnum termsEnum = ...;
  2365. @@ -20,10 +20,8 @@
  2366.  
  2367. Do this:
  2368.  
  2369. - FieldsEnum fieldsEnum = ...;
  2370. - String field;
  2371. - while((field = fieldsEnum.next()) != null) {
  2372. - TermsEnum termsEnum = fieldsEnum.terms();
  2373. + for(String field : fields) {
  2374. + TermsEnum termsEnum = fields.terms(field);
  2375. BytesRef text;
  2376. while((text = termsEnum.next()) != null) {
  2377. System.out.println("field=" + field + "; text=" + text.utf8ToString());
  2378. Index: lucene/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java
  2379. ===================================================================
  2380. --- lucene/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java (revision 1372046)
  2381. +++ lucene/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java (working copy)
  2382. @@ -25,6 +25,7 @@
  2383. import java.util.HashMap;
  2384. import java.util.Iterator;
  2385. import java.util.Map;
  2386. +import java.util.NoSuchElementException;
  2387.  
  2388. import org.apache.lucene.analysis.Analyzer;
  2389. import org.apache.lucene.analysis.TokenStream;
  2390. @@ -42,7 +43,6 @@
  2391. import org.apache.lucene.index.FieldInfos;
  2392. import org.apache.lucene.index.FieldInvertState;
  2393. import org.apache.lucene.index.Fields;
  2394. -import org.apache.lucene.index.FieldsEnum;
  2395. import org.apache.lucene.index.OrdTermState;
  2396. import org.apache.lucene.index.StoredFieldVisitor;
  2397. import org.apache.lucene.index.TermState;
  2398. @@ -718,23 +718,28 @@
  2399.  
  2400. private class MemoryFields extends Fields {
  2401. @Override
  2402. - public FieldsEnum iterator() {
  2403. - return new FieldsEnum() {
  2404. + public Iterator<String> iterator() {
  2405. + return new Iterator<String>() {
  2406. int upto = -1;
  2407.  
  2408. @Override
  2409. public String next() {
  2410. upto++;
  2411. if (upto >= sortedFields.length) {
  2412. - return null;
  2413. + throw new NoSuchElementException();
  2414. }
  2415. return sortedFields[upto].getKey();
  2416. }
  2417.  
  2418. @Override
  2419. - public Terms terms() {
  2420. - return MemoryFields.this.terms(sortedFields[upto].getKey());
  2421. + public boolean hasNext() {
  2422. + return upto+1 < sortedFields.length;
  2423. }
  2424. +
  2425. + @Override
  2426. + public void remove() {
  2427. + throw new UnsupportedOperationException();
  2428. + }
  2429. };
  2430. }
  2431.  
  2432. Index: solr/core/src/java/org/apache/solr/handler/component/TermVectorComponent.java
  2433. ===================================================================
  2434. --- solr/core/src/java/org/apache/solr/handler/component/TermVectorComponent.java (revision 1372046)
  2435. +++ solr/core/src/java/org/apache/solr/handler/component/TermVectorComponent.java (working copy)
  2436. @@ -16,7 +16,6 @@
  2437. import org.apache.lucene.index.DocsAndPositionsEnum;
  2438. import org.apache.lucene.index.FieldInfo;
  2439. import org.apache.lucene.index.Fields;
  2440. -import org.apache.lucene.index.FieldsEnum;
  2441. import org.apache.lucene.index.IndexReader;
  2442. import org.apache.lucene.index.StoredFieldVisitor;
  2443. import org.apache.lucene.index.Terms;
  2444. @@ -318,10 +317,8 @@
  2445. } else {
  2446. // extract all fields
  2447. final Fields vectors = reader.getTermVectors(docId);
  2448. - final FieldsEnum fieldsEnum = vectors.iterator();
  2449. - String field;
  2450. - while((field = fieldsEnum.next()) != null) {
  2451. - Terms terms = fieldsEnum.terms();
  2452. + for (String field : vectors) {
  2453. + Terms terms = vectors.terms(field);
  2454. if (terms != null) {
  2455. termsEnum = terms.iterator(termsEnum);
  2456. mapOneVector(docNL, allFields, reader, docId, termsEnum, field);
Advertisement
Add Comment
Please, Sign In to add comment