@@ -16,10 +16,9 @@ internal class LocalTempStorageIndexer
16
16
{
17
17
private string _tempPath ;
18
18
public Lucene . Net . Store . Directory LuceneDirectory { get ; private set ; }
19
- private static readonly object Locker = new object ( ) ;
19
+ private readonly object _locker = new object ( ) ;
20
20
public SnapshotDeletionPolicy Snapshotter { get ; private set ; }
21
- private bool _syncStorage = false ;
22
-
21
+
23
22
public LocalTempStorageIndexer ( )
24
23
{
25
24
IndexDeletionPolicy policy = new KeepOnlyLastCommitDeletionPolicy ( ) ;
@@ -32,121 +31,101 @@ public void Initialize(NameValueCollection config, string configuredPath, Lucene
32
31
33
32
_tempPath = Path . Combine ( codegenPath , configuredPath . TrimStart ( '~' , '/' ) . Replace ( "/" , "\\ " ) ) ;
34
33
35
- if ( config != null )
36
- {
37
- if ( config [ "syncTempStorage" ] != null )
38
- {
39
- var attempt = config [ "syncTempStorage" ] . TryConvertTo < bool > ( ) ;
40
- if ( attempt )
41
- {
42
- _syncStorage = attempt . Result ;
43
- }
44
- }
45
- }
34
+ var success = InitializeLocalIndexAndDirectory ( baseLuceneDirectory , analyzer , configuredPath ) ;
46
35
47
- InitializeLocalIndexAndDirectory ( baseLuceneDirectory , analyzer , configuredPath ) ;
36
+ //create the custom lucene directory which will keep the main and temp FS's in sync
37
+ LuceneDirectory = LocalTempStorageDirectoryTracker . Current . GetDirectory (
38
+ new DirectoryInfo ( _tempPath ) ,
39
+ baseLuceneDirectory ,
40
+ //flag to disable the mirrored folder if not successful
41
+ success == false ) ;
48
42
}
49
43
50
- private void InitializeLocalIndexAndDirectory ( Lucene . Net . Store . Directory baseLuceneDirectory , Analyzer analyzer , string configuredPath )
44
+ private bool InitializeLocalIndexAndDirectory ( Lucene . Net . Store . Directory baseLuceneDirectory , Analyzer analyzer , string configuredPath )
51
45
{
52
- lock ( Locker )
46
+ lock ( _locker )
53
47
{
54
48
if ( Directory . Exists ( _tempPath ) == false )
55
49
{
56
50
Directory . CreateDirectory ( _tempPath ) ;
57
51
}
58
52
59
- //if we are syncing storage to the main file system to temp files, then sync from the main FS to our temp FS
60
- if ( _syncStorage )
53
+ //copy index
54
+
55
+ using ( new IndexWriter (
56
+ //read from the underlying/default directory, not the temp codegen dir
57
+ baseLuceneDirectory ,
58
+ analyzer ,
59
+ Snapshotter ,
60
+ IndexWriter . MaxFieldLength . UNLIMITED ) )
61
61
{
62
- //copy index
63
-
64
- using ( new IndexWriter (
65
- //read from the underlying/default directory, not the temp codegen dir
66
- baseLuceneDirectory ,
67
- analyzer ,
68
- Snapshotter ,
69
- IndexWriter . MaxFieldLength . UNLIMITED ) )
62
+ try
70
63
{
71
- try
72
- {
73
- var basePath = IOHelper . MapPath ( configuredPath ) ;
64
+ var basePath = IOHelper . MapPath ( configuredPath ) ;
74
65
75
- var commit = Snapshotter . Snapshot ( ) ;
76
- var allSnapshotFiles = commit . GetFileNames ( ) . Concat ( new [ ] { commit . GetSegmentsFileName ( ) } ) . ToArray ( ) ;
66
+ var commit = Snapshotter . Snapshot ( ) ;
67
+ var allSnapshotFiles = commit . GetFileNames ( ) . Concat ( new [ ] { commit . GetSegmentsFileName ( ) } )
68
+ . Distinct ( )
69
+ . ToArray ( ) ;
77
70
78
- var tempDir = new DirectoryInfo ( _tempPath ) ;
71
+ var tempDir = new DirectoryInfo ( _tempPath ) ;
79
72
80
- //Get all files in the temp storage that don't exist in the snapshot collection, we want to remove these
81
- var toRemove = tempDir . GetFiles ( )
82
- . Select ( x => x . Name )
83
- . Except ( allSnapshotFiles ) ;
84
-
85
- using ( var tempDirectory = new SimpleFSDirectory ( tempDir ) )
73
+ //Get all files in the temp storage that don't exist in the snapshot collection, we want to remove these
74
+ var toRemove = tempDir . GetFiles ( )
75
+ . Select ( x => x . Name )
76
+ . Except ( allSnapshotFiles ) ;
77
+
78
+ using ( var tempDirectory = new SimpleFSDirectory ( tempDir ) )
79
+ {
80
+ if ( IndexWriter . IsLocked ( tempDirectory ) == false )
86
81
{
87
- if ( IndexWriter . IsLocked ( tempDirectory ) == false )
82
+ foreach ( var file in toRemove )
88
83
{
89
- foreach ( var file in toRemove )
84
+ try
90
85
{
91
- try
92
- {
93
- File . Delete ( Path . Combine ( _tempPath , file ) ) ;
94
- }
95
- catch ( IOException ex )
96
- {
97
- LogHelper . Error < LocalTempStorageIndexer > ( "Could not delete index file, could not sync from main storage" , ex ) ;
98
-
99
- //quit here and do not assign the lucene directory, this means that the app will now just be working from normal storage
100
- return ;
101
- }
86
+ File . Delete ( Path . Combine ( _tempPath , file ) ) ;
87
+ }
88
+ catch ( IOException ex )
89
+ {
90
+ LogHelper . Error < LocalTempStorageIndexer > ( "Could not delete index file, could not sync from main storage" , ex ) ;
91
+ //quit here
92
+ return false ;
102
93
}
103
- }
104
- else
105
- {
106
- LogHelper . Warn < LocalTempStorageIndexer > ( "Cannot sync index files from main storage, the index is currently locked" ) ;
107
-
108
- //quit here and do not assign the lucene directory, this means that the app will now just be working from normal storage
109
- return ;
110
94
}
111
95
}
112
-
113
- foreach ( var fileName in allSnapshotFiles . Where ( f => f . IsNullOrWhiteSpace ( ) == false ) )
96
+ else
114
97
{
115
- try
116
- {
117
- File . Copy (
118
- Path . Combine ( basePath , "Index" , fileName ) ,
119
- Path . Combine ( _tempPath , Path . GetFileName ( fileName ) ) , true ) ;
120
- }
121
- catch ( IOException ex )
122
- {
123
- LogHelper . Error < LocalTempStorageIndexer > ( "Could not copy index file, could not sync from main storage" , ex ) ;
124
-
125
- //quit here and do not assign the lucene directory, this means that the app will now just be working from normal storage
126
- return ;
127
- }
98
+ LogHelper . Warn < LocalTempStorageIndexer > ( "Cannot sync index files from main storage, the index is currently locked" ) ;
99
+ //quit here
100
+ return false ;
128
101
}
129
-
130
102
}
131
- finally
132
- {
133
- Snapshotter . Release ( ) ;
134
- }
135
- }
136
103
137
- //create the custom lucene directory which will keep the main and temp FS's in sync
104
+ foreach ( var fileName in allSnapshotFiles . Where ( f => f . IsNullOrWhiteSpace ( ) == false ) )
105
+ {
106
+ try
107
+ {
108
+ File . Copy (
109
+ Path . Combine ( basePath , "Index" , fileName ) ,
110
+ Path . Combine ( _tempPath , Path . GetFileName ( fileName ) ) , true ) ;
111
+ }
112
+ catch ( IOException ex )
113
+ {
114
+ LogHelper . Error < LocalTempStorageIndexer > ( "Could not copy index file, could not sync from main storage" , ex ) ;
138
115
139
- LuceneDirectory = new LocalTempStorageDirectory (
140
- new DirectoryInfo ( _tempPath ) ,
141
- baseLuceneDirectory ) ;
142
- }
143
- else
144
- {
145
- //just return a normal lucene directory that uses the codegen folder
116
+ //quit here
117
+ return false ;
118
+ }
119
+ }
146
120
147
- LuceneDirectory = FSDirectory . Open ( new DirectoryInfo ( _tempPath ) ) ;
121
+ }
122
+ finally
123
+ {
124
+ Snapshotter . Release ( ) ;
125
+ }
148
126
}
149
127
128
+ return true ;
150
129
}
151
130
}
152
131
}
0 commit comments