~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to solr/core/src/java/org/apache/solr/util/VersionedFile.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 
 * contributor license agreements.  See the NOTICE file distributed with
4
 
 * this work for additional information regarding copyright ownership.
5
 
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 
 * (the "License"); you may not use this file except in compliance with
7
 
 * the License.  You may obtain a copy of the License at
8
 
 *
9
 
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 
 *
11
 
 * Unless required by applicable law or agreed to in writing, software
12
 
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 
 * See the License for the specific language governing permissions and
15
 
 * limitations under the License.
16
 
 */
17
 
 
18
 
package org.apache.solr.util;
19
 
 
20
 
import java.io.File;
21
 
import java.io.FileInputStream;
22
 
import java.io.FileNotFoundException;
23
 
import java.io.FilenameFilter;
24
 
import java.io.InputStream;
25
 
import java.util.ArrayList;
26
 
import java.util.Arrays;
27
 
import java.util.Collection;
28
 
import java.util.HashSet;
29
 
import java.util.List;
30
 
import java.util.Set;
31
 
 
32
 
 
33
 
/**
34
 
 * 
35
 
 * @since solr 1.3
36
 
 */
37
 
public class VersionedFile 
38
 
{
39
 
  /* Open the latest version of a file... fileName if that exists, or
40
 
   * the last fileName.* after being sorted lexicographically.
41
 
   * Older versions of the file are deleted (and queued for deletion if
42
 
   * that fails).
43
 
   */
44
 
  public static InputStream getLatestFile(String dirName, String fileName) throws FileNotFoundException 
45
 
  {
46
 
    Collection<File> oldFiles=null;
47
 
    final String prefix = fileName+'.';
48
 
    File f = new File(dirName, fileName);
49
 
    InputStream is = null;
50
 
 
51
 
    // there can be a race between checking for a file and opening it...
52
 
    // the user may have just put a new version in and deleted an old version.
53
 
    // try multiple times in a row.
54
 
    for (int retry=0; retry<10 && is==null; retry++) {
55
 
      try {
56
 
        if (!f.exists()) {
57
 
          File dir = new File(dirName);
58
 
          String[] names = dir.list(new FilenameFilter() {
59
 
            public boolean accept(File dir, String name) {
60
 
              return name.startsWith(prefix);
61
 
            }
62
 
          });
63
 
          Arrays.sort(names);
64
 
          f = new File(dir, names[names.length-1]);
65
 
          oldFiles = new ArrayList<File>();
66
 
          for (int i=0; i<names.length-1; i++) {
67
 
            oldFiles.add(new File(dir, names[i]));
68
 
          }
69
 
        }
70
 
 
71
 
        is = new FileInputStream(f);
72
 
      } catch (Exception e) {
73
 
        // swallow exception for now
74
 
      }
75
 
    }
76
 
 
77
 
    // allow exception to be thrown from the final try.
78
 
    if (is == null) {
79
 
      is = new FileInputStream(f);
80
 
    }
81
 
 
82
 
    // delete old files only after we have successfuly opened the newest
83
 
    if (oldFiles != null) {
84
 
      delete(oldFiles);
85
 
    }
86
 
 
87
 
    return is;
88
 
  }
89
 
 
90
 
  private static final Set<File> deleteList = new HashSet<File>();
91
 
  private static synchronized void delete(Collection<File> files) {
92
 
    synchronized (deleteList) {
93
 
      deleteList.addAll(files);
94
 
      List<File> deleted = new ArrayList<File>();
95
 
      for (File df : deleteList) {
96
 
        try {
97
 
          df.delete();
98
 
          // deleteList.remove(df);
99
 
          deleted.add(df);
100
 
        } catch (SecurityException e) {
101
 
          if (!df.exists()) {
102
 
            deleted.add(df);
103
 
          }
104
 
        }
105
 
      }
106
 
      deleteList.removeAll(deleted);
107
 
    }
108
 
  }
109
 
}