/
Configuration.cs
255 lines (216 loc) · 7.97 KB
/
Configuration.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// /*!
// * Project: SODDI v.10
// * http://skysanders.net/tools/se/
// *
// * Copyright 2010, Sky Sanders
// * Dual licensed under the MIT or GPL Version 2 licenses.
// * http://skysanders.net/tools/se/LICENSE.TXT
// *
// * Date: April 01 2010
// */
#region
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using Salient.StackExchange.Import.Loaders;
using Salient.StackExchange.Import.TableTypes;
#endregion
namespace Salient.StackExchange.Import.Configuration
{
public class Configuration
{
#region ImportOptions enum
[Flags]
public enum ImportOptions
{
None = 0,
Indices = 1,
FullText = 2,
Split = 4,
GUI = 8,
FieldCount = 16
}
#endregion
private const int DefaultBatchSize = 5000;
public Configuration()
{
SetDefaults();
}
public Configuration(string[] args)
{
SetDefaults();
ParseArguments(args);
}
public int BatchSize { get; set; }
public bool FullText
{
get { return (Options & ImportOptions.FullText) == ImportOptions.FullText; }
set
{
Options = value
? Options | ImportOptions.FullText
: Options & (ImportOptions)(ImportOptions.FieldCount - ImportOptions.FullText);
}
}
public bool GUI
{
get { return (Options & ImportOptions.GUI) == ImportOptions.GUI; }
set
{
Options = value
? Options | ImportOptions.GUI
: Options & (ImportOptions)(ImportOptions.FieldCount - ImportOptions.GUI);
}
}
public bool Indices
{
get { return (Options & ImportOptions.Indices) == ImportOptions.Indices; }
set
{
Options = value
? Options | ImportOptions.Indices
: Options & (ImportOptions)(ImportOptions.FieldCount - ImportOptions.Indices);
}
}
internal ImportOptions Options { get; set; }
public DbProviderInfo Provider { get; set; }
public string Source { get; set; }
public bool Split
{
get { return (Options & ImportOptions.Split) == ImportOptions.Split; }
set
{
Options = value
? Options | ImportOptions.Split
: Options & (ImportOptions)(ImportOptions.FieldCount - ImportOptions.Split);
}
}
public string Target { get; set; }
public List<ImportTarget> Targets { get; set; }
public Type[] GetAllTableTypes()
{
return
typeof(Posts).Assembly.GetTypes().Where(
t => t.Namespace == typeof(Posts).Namespace && (Split || t != typeof(PostTags))).ToArray();
}
public string ToString(bool commandLine)
{
if (commandLine)
{
string targets = string.Join(" ", Targets.Select(t => t.Name == t.Schema ? t.Name : t.Name + ":" + t.Schema).ToArray());
DbConnectionStringBuilder csb = new DbConnectionStringBuilder();
csb.ConnectionString = Provider.ConnectionString;
csb.Add("Provider", Provider.Name);
string batch = BatchSize != DefaultBatchSize ? " batch:" + BatchSize : "";
string cmdLine = string.Format("{7} source:\"{0}\" target:\"{1}\"{2}{3}{4}{5} {6}", Source,
csb.ConnectionString, batch, Indices ? " indices" : "",
FullText ? " fulltext" : "", Split ? " split" : "", targets,
Path.GetFileName(Assembly.GetExecutingAssembly().Location));
return cmdLine;
}
else
{
return
string.Format("Source : {0}\r\nTarget : {1}\r\nOptions : {2}", Source, Target, Options);
}
}
public static List<string> GetAllSites(string source)
{
List<string> sites = new List<string>();
Regex dirRx = new Regex(@"^(\d+) ([A-Za-z]+)$", RegexOptions.IgnoreCase);
string[] dirs = Directory.GetDirectories(source);
foreach (string dir in dirs)
{
Match match = dirRx.Match(Path.GetFileName(dir));
if (match.Success)
{
sites.Add(match.Groups[2].Value);
}
}
return sites;
}
public static ImportTarget GetSite(string source, string arg)
{
string[] segments = arg.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
string siteId = segments[0];
string siteSchema = segments.Length > 1 ? segments[1] : siteId;
string[] dirs = Directory.GetDirectories(source, "* " + siteId);
return dirs.Length == 0 ? null : new ImportTarget(segments[0], dirs[0], siteSchema);
}
public static List<ImportTarget> GetTargets(string source, IEnumerable<string> unparsed)
{
List<ImportTarget> targets = new List<ImportTarget>();
foreach (string arg in unparsed)
{
ImportTarget site = GetSite(source, arg);
if (site != null)
{
targets.Add(site);
}
}
return targets;
}
private void ParseArguments(string[] args)
{
List<string> unparsed = new List<string>();
for (int i = 0; i < args.Length; i++)
{
string value = null;
string arg = args[i].Trim();
if (arg.IndexOf(":") > -1)
{
value = arg.Substring(arg.IndexOf(":") + 1).Trim('"', '\'');
arg = arg.Substring(0, arg.IndexOf(":"));
}
switch (arg.ToLowerInvariant())
{
case "source":
Source = value;
break;
case "target":
Target = value;
Provider = DbProviders.ParseArg(value);
break;
case "batch":
int batchSize;
if (int.TryParse(value, out batchSize))
{
BatchSize = batchSize;
}
break;
case "indices":
Options = Options | ImportOptions.Indices;
break;
case "fulltext":
Options = Options | ImportOptions.FullText;
break;
case "split":
Options = Options | ImportOptions.Split;
break;
case "gui":
Options = Options | ImportOptions.GUI;
break;
default:
unparsed.Add(args[i].Trim());
break;
}
}
// unparsed args MUST be sites
// if no sites specified, get them all
if (unparsed.Count == 0 && !string.IsNullOrEmpty(Source))
{
unparsed = GetAllSites(Source);
}
Targets = GetTargets(Source, unparsed);
}
private void SetDefaults()
{
Targets = new List<ImportTarget>();
BatchSize = DefaultBatchSize;
}
}
}