comparison src/luan/modules/lucene/LuceneIndex.java @ 1430:103d0ce70385

fix postgres check
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 30 Nov 2019 21:32:14 -0700
parents 225808b90cee
children 3aae37efbf40
comparison
equal deleted inserted replaced
1429:82415c9c0015 1430:103d0ce70385
902 if( hasPostgres ) 902 if( hasPostgres )
903 checkPostgres(completer); 903 checkPostgres(completer);
904 luanLogger.info("end check"); 904 luanLogger.info("end check");
905 } 905 }
906 906
907 private void checkPostgres(LuanFunction completer) throws IOException, SQLException, LuanException { 907 private void checkPostgres(LuanFunction completer)
908 throws IOException, SQLException, LuanException
909 {
908 //luanLogger.info("start postgres check"); 910 //luanLogger.info("start postgres check");
909 final PostgresBackup.Checker postgresChecker; 911 final PostgresBackup.Checker postgresChecker = postgresBackup.newChecker();
910 final IndexSearcher searcher; 912 final IndexSearcher searcher = openSearcher();
911 writeLock.lock();
912 try {
913 postgresChecker = postgresBackup.newChecker();
914 searcher = openSearcher();
915 } finally {
916 writeLock.unlock();
917 }
918 try { 913 try {
919 final List<Long> idsLucene = new ArrayList<Long>(); 914 final List<Long> idsLucene = new ArrayList<Long>();
920 Query query = new PrefixQuery(new Term("id")); 915 Query query = new PrefixQuery(new Term("id"));
921 MyCollector col = new MyCollector() { 916 MyCollector col = new MyCollector() {
922 @Override public void collect(int iDoc) throws IOException { 917 @Override public void collect(int iDoc) throws IOException {
938 while( iLucene < nLucene && iPostgres < nPostgres ) { 933 while( iLucene < nLucene && iPostgres < nPostgres ) {
939 long idLucene = idsLucene.get(iLucene); 934 long idLucene = idsLucene.get(iLucene);
940 long idPostgres = idsPostgres.get(iPostgres); 935 long idPostgres = idsPostgres.get(iPostgres);
941 if( idLucene < idPostgres ) { 936 if( idLucene < idPostgres ) {
942 iLucene++; 937 iLucene++;
943 luanLogger.error("id "+idLucene+" found in lucene but not postgres"); 938 checkPostgres(completer,postgresChecker,lts,idLucene);
944 } else if( idLucene > idPostgres ) { 939 } else if( idLucene > idPostgres ) {
945 iPostgres++; 940 iPostgres++;
946 luanLogger.error("id "+idPostgres+" found in postgres but not lucene"); 941 checkPostgres(completer,postgresChecker,lts,idPostgres);
947 } else { // == 942 } else { // ==
948 LuanTable docPostgres = postgresChecker.getDoc(idPostgres); 943 LuanTable docPostgres = postgresChecker.getDoc(idPostgres);
949 TopDocs td = searcher.search(new TermQuery(term("id",idLucene)),1); 944 TopDocs td = searcher.search(new TermQuery(term("id",idLucene)),1);
950 if( td.totalHits != 1 ) throw new RuntimeException(); 945 if( td.totalHits != 1 ) throw new RuntimeException();
951 Document doc = searcher.doc( td.scoreDocs[0].doc ); 946 Document doc = searcher.doc( td.scoreDocs[0].doc );
952 LuanTable docLucene = toTable(completer.luan(),doc); 947 LuanTable docLucene = toTable(completer.luan(),doc);
953 docLucene = (LuanTable)completer.call(docLucene); 948 docLucene = (LuanTable)completer.call(docLucene);
954 if( !equal(docPostgres,docLucene) ) { 949 if( !equal(docPostgres,docLucene) ) {
955 luanLogger.error("id "+idLucene+" not equal"); 950 checkPostgres(completer,postgresChecker,lts,idPostgres);
956 luanLogger.error("lucene = "+lts.toString(docLucene));
957 luanLogger.error("postgres = "+lts.toString(docPostgres));
958 } 951 }
959 iLucene++; 952 iLucene++;
960 iPostgres++; 953 iPostgres++;
961 } 954 }
962 } 955 }
963 while( iLucene < nLucene ) { 956 while( iLucene < nLucene ) {
964 long idLucene = idsLucene.get(iLucene++); 957 long idLucene = idsLucene.get(iLucene++);
965 luanLogger.error("id "+idLucene+" found in lucene but not postgres"); 958 checkPostgres(completer,postgresChecker,lts,idLucene);
966 } 959 }
967 while( iPostgres < nPostgres ) { 960 while( iPostgres < nPostgres ) {
968 long idPostgres = idsPostgres.get(iPostgres++); 961 long idPostgres = idsPostgres.get(iPostgres++);
969 luanLogger.error("id "+idPostgres+" found in postgres but not lucene"); 962 checkPostgres(completer,postgresChecker,lts,idPostgres);
970 } 963 }
971 } finally { 964 } finally {
972 close(searcher); 965 close(searcher);
973 postgresChecker.close(); 966 postgresChecker.close();
974 } 967 }
975 } 968 }
976 969
970 private void checkPostgres(LuanFunction completer,PostgresBackup.Checker postgresChecker,LuanToString lts,long id)
971 throws IOException, SQLException, LuanException
972 {
973 //luanLogger.info("check id "+id);
974 writeLock.lock();
975 try {
976 final IndexSearcher searcher = openSearcher();
977 try {
978 LuanTable docPostgres = postgresChecker.getDoc(id);
979 TopDocs td = searcher.search(new TermQuery(term("id",id)),1);
980 LuanTable docLucene;
981 if( td.totalHits == 0 ) {
982 docLucene = null;
983 } else if( td.totalHits == 1 ) {
984 Document doc = searcher.doc( td.scoreDocs[0].doc );
985 docLucene = toTable(completer.luan(),doc);
986 docLucene = (LuanTable)completer.call(docLucene);
987 } else
988 throw new RuntimeException();
989 if( docPostgres == null ) {
990 if( docLucene != null )
991 luanLogger.error("id "+id+" found in lucene but not postgres");
992 return;
993 }
994 if( docLucene == null ) {
995 luanLogger.error("id "+id+" found in postgres but not lucene");
996 return;
997 }
998 if( !equal(docPostgres,docLucene) ) {
999 luanLogger.error("id "+id+" not equal");
1000 luanLogger.error("lucene = "+lts.toString(docLucene));
1001 luanLogger.error("postgres = "+lts.toString(docPostgres));
1002 }
1003 } finally {
1004 close(searcher);
1005 }
1006 } finally {
1007 writeLock.unlock();
1008 }
1009 }
1010
977 private boolean equal(LuanTable t1,LuanTable t2) throws LuanException { 1011 private boolean equal(LuanTable t1,LuanTable t2) throws LuanException {
978 return t1!=null && t2!=null && t1.asMap().equals(t2.asMap()); 1012 return t1!=null && t2!=null && t1.asMap().equals(t2.asMap());
979 } 1013 }
980 1014
981 } 1015 }