comparison src/luan/modules/lucene/LuceneIndex.java @ 1395:9dfff82dfc59

finish postgres work
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 09 Sep 2019 01:22:23 -0600
parents cc0dbca576dc
children 0dc9837c16be
comparison
equal deleted inserted replaced
1394:8fe777ba5045 1395:9dfff82dfc59
116 } 116 }
117 } 117 }
118 118
119 private static Map<String,LuceneIndex> indexes = new HashMap<String,LuceneIndex>(); 119 private static Map<String,LuceneIndex> indexes = new HashMap<String,LuceneIndex>();
120 120
121 public static Object[] getLuceneIndex(Luan luan,File indexDir,FieldParser defaultFieldParser,String[] defaultFields,LuanTable postgresSpec) 121 public static Object[] getLuceneIndex(Luan luan,File indexDir,FieldParser defaultFieldParser,String[] defaultFields,LuanFunction completer,LuanTable postgresSpec)
122 throws LuanException, IOException, ClassNotFoundException, SQLException 122 throws LuanException, IOException, ClassNotFoundException, SQLException
123 { 123 {
124 String key = indexDir.getCanonicalPath(); 124 String key = indexDir.getCanonicalPath();
125 synchronized(indexes) { 125 synchronized(indexes) {
126 LuceneIndex li = indexes.get(key); 126 LuceneIndex li = indexes.get(key);
127 if( li == null ) { 127 if( li == null ) {
128 li = new LuceneIndex(luan,indexDir,defaultFieldParser,defaultFields,key,postgresSpec); 128 li = new LuceneIndex(luan,indexDir,defaultFieldParser,defaultFields,key,completer,postgresSpec);
129 li.openCount = 1; 129 li.openCount = 1;
130 indexes.put(key,li); 130 indexes.put(key,li);
131 } else { 131 } else {
132 if( defaultFieldParser != li.defaultFieldParser ) 132 if( defaultFieldParser != li.defaultFieldParser )
133 throw new LuanException("default_type doesn't match previous use"); 133 throw new LuanException("default_type doesn't match previous use");
167 private final FieldParser defaultFieldParser; 167 private final FieldParser defaultFieldParser;
168 private final String[] defaultFields; 168 private final String[] defaultFields;
169 169
170 private final PostgresBackup postgresBackup; 170 private final PostgresBackup postgresBackup;
171 171
172 private LuceneIndex(Luan luan,File indexDir,FieldParser defaultFieldParser,String[] defaultFields,String key,LuanTable postgresSpec) 172 private LuceneIndex(Luan luan,File indexDir,FieldParser defaultFieldParser,String[] defaultFields,String key,LuanFunction completer,LuanTable postgresSpec)
173 throws LuanException, IOException, ClassNotFoundException, SQLException 173 throws LuanException, IOException, ClassNotFoundException, SQLException
174 { 174 {
175 this.luanLogger = luan.getLogger(LuceneIndex.class); 175 this.luanLogger = luan.getLogger(LuceneIndex.class);
176 this.key = key; 176 this.key = key;
177 this.defaultFieldParser = defaultFieldParser; 177 this.defaultFieldParser = defaultFieldParser;
188 this.analyzer = analyzer; 188 this.analyzer = analyzer;
189 boolean wasCreated = reopen(); 189 boolean wasCreated = reopen();
190 if( postgresSpec == null ) { 190 if( postgresSpec == null ) {
191 postgresBackup = null; 191 postgresBackup = null;
192 } else { 192 } else {
193 if( completer == null )
194 throw new LuanException("completer is required for postgres_spec");
193 Map spec = postgresSpec.asMap(); 195 Map spec = postgresSpec.asMap();
194 LuanFunction completer = Utils.removeRequiredFunction(spec,"completer");
195 postgresBackup = new PostgresBackup(luan,spec); 196 postgresBackup = new PostgresBackup(luan,spec);
196 if( postgresBackup != null ) { 197 if( postgresBackup != null ) {
197 if( !wasCreated && postgresBackup.wasCreated ) { 198 if( !wasCreated && postgresBackup.wasCreated ) {
198 luanLogger.error("rebuilding postgres backup"); 199 luanLogger.error("rebuilding postgres backup");
199 rebuild_postgres_backup(completer); 200 rebuild_postgres_backup(completer);
874 try { 875 try {
875 writer.deleteAll(); 876 writer.deleteAll();
876 long nextId = postgresBackup.maxId() + 1; 877 long nextId = postgresBackup.maxId() + 1;
877 postgresBackup.restoreLucene(this); 878 postgresBackup.restoreLucene(this);
878 id = idLim = nextId; 879 id = idLim = nextId;
880 saveNextId(nextId);
879 ok = true; 881 ok = true;
880 writer.commit(); 882 writer.commit();
881 } finally { 883 } finally {
882 if( !ok ) { 884 if( !ok ) {
883 writer.rollback(); 885 writer.rollback();
904 checkPostgres(completer); 906 checkPostgres(completer);
905 luanLogger.info("end check"); 907 luanLogger.info("end check");
906 } 908 }
907 909
908 private void checkPostgres(LuanFunction completer) throws IOException, SQLException, LuanException { 910 private void checkPostgres(LuanFunction completer) throws IOException, SQLException, LuanException {
911 luanLogger.info("start postgres check");
909 final PostgresBackup.Checker postgresChecker; 912 final PostgresBackup.Checker postgresChecker;
910 final IndexSearcher searcher; 913 final IndexSearcher searcher;
911 writeLock.lock(); 914 writeLock.lock();
912 try { 915 try {
913 postgresChecker = postgresBackup.newChecker(); 916 postgresChecker = postgresBackup.newChecker();