Skip to content

Commit

Permalink
Core merge commands
Browse files Browse the repository at this point in the history
  • Loading branch information
mausch committed Feb 6, 2014
1 parent a5d28fd commit 797cad5
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ SampleSolrApp/log*
_NCrunch*/*
*~
packages/*
.nuget/*.exe
.nuget/*.exe
*.sln.ide
31 changes: 31 additions & 0 deletions SolrNet.Tests/MergeCommandTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using MbUnit.Framework;
using SolrNet.Commands.Cores;
using SolrNet.Utils;

namespace SolrNet.Tests {
[TestFixture]
public class MergeCommandTests {
[Test]
public void Index() {
var m = new MergeCommand("core0", new MergeCommand.IndexDir("/path/to/index1"), new MergeCommand.IndexDir("/path/to/index2"));
var parameters = m.GetParameters().ToList();
Assert.Contains(parameters, KV.Create("action", "mergeindexes"));
Assert.Contains(parameters, KV.Create("indexDir", "/path/to/index1"));
Assert.Contains(parameters, KV.Create("indexDir", "/path/to/index2"));
Assert.Contains(parameters, KV.Create("core", "core0"));
}

[Test]
public void Core() {
var m = new MergeCommand("core0", new MergeCommand.SrcCore("core1"), new MergeCommand.SrcCore("core2"));
var parameters = m.GetParameters().ToList();
Assert.Contains(parameters, KV.Create("action", "mergeindexes"));
Assert.Contains(parameters, KV.Create("srcCore", "core1"));
Assert.Contains(parameters, KV.Create("srcCore", "core2"));
Assert.Contains(parameters, KV.Create("core", "core0"));
}
}
}
1 change: 1 addition & 0 deletions SolrNet.Tests/SolrNet.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
<Compile Include="DateTimeOffsetFieldParserTests.cs" />
<Compile Include="DefaultResponseParserTests.cs" />
<Compile Include="LocationTests.cs" />
<Compile Include="MergeCommandTests.cs" />
<Compile Include="MoneyTests.cs" />
<Compile Include="ExtractCommandTests.cs" />
<Compile Include="AddCommandTests.cs" />
Expand Down
4 changes: 4 additions & 0 deletions SolrNet/Commands/CoreCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,9 @@ public string Execute(ISolrConnection connection) {
protected void AddParameter(string key, string value) {
Parameters.Add(new KeyValuePair<string, string>(key, value));
}

public IEnumerable<KeyValuePair<string, string>> GetParameters() {
return Parameters;
}
}
}
75 changes: 75 additions & 0 deletions SolrNet/Commands/Cores/MergeCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace SolrNet.Commands.Cores {
/// <summary>
/// Merges one or more cores into another core.
/// See https://wiki.apache.org/solr/MergingSolrIndexes for details.
/// </summary>
public class MergeCommand: CoreCommand {
/// <summary>
/// Use an index path as merge source
/// </summary>
public sealed class IndexDir {
/// <summary>
/// Index directory
/// </summary>
public readonly string Dir;

/// <summary>
/// Use an index path as merge source
/// </summary>
/// <param name="dir">Index path</param>
public IndexDir(string dir) {
Dir = dir;
}
}

/// <summary>
/// Use a core name as merge source
/// </summary>
public sealed class SrcCore {
/// <summary>
/// Core name
/// </summary>
public readonly string CoreName;

/// <summary>
/// Use a core name as merge source
/// </summary>
/// <param name="coreName">Core name</param>
public SrcCore(string coreName) {
CoreName = coreName;
}
}

/// <summary>
/// Merge indexes using their path to identify them.
/// Requires Solr 1.4+
/// </summary>
/// <param name="destinationCore"></param>
/// <param name="indexDir"></param>
/// <param name="indexDirs"></param>
public MergeCommand(string destinationCore, IndexDir indexDir, params IndexDir[] indexDirs) {
AddParameter("core", destinationCore);
AddParameter("action", "mergeindexes");
foreach (var d in new[] {indexDir}.Concat(indexDirs))
AddParameter("indexDir", d.Dir);
}

/// <summary>
/// Merge indexes using their core names to identify them.
/// Requires Solr 3.3+
/// </summary>
/// <param name="destinationCore"></param>
/// <param name="srcCore"></param>
/// <param name="srcCores"></param>
public MergeCommand(string destinationCore, SrcCore srcCore, params SrcCore[] srcCores) {
AddParameter("core", destinationCore);
AddParameter("action", "mergeindexes");
foreach (var c in new[] {srcCore}.Concat(srcCores))
AddParameter("srcCore", c.CoreName);
}
}
}
20 changes: 19 additions & 1 deletion SolrNet/ISolrCoreAdmin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,27 @@ public interface ISolrCoreAdmin {
/// If a core is registered under more than one name, only the given name is removed.
/// </summary>
/// <param name="coreName">The name of the core to be to be removed. If the persistent
/// attribute of &lt;solr&gt; is set to "true", the &lt;core&gt; element
/// attribute of &lt;solr&gt; is set to "true", the &lt;core &gt; element
/// with this "name" attribute will be removed from solr.xml.</param>
/// <param name="delete">If not null, deletes the index once the core is unloaded. (Only available in 3.3 and above).</param>
ResponseHeader Unload(string coreName, UnloadCommand.Delete delete);

/// <summary>
/// Merge indexes using their core names to identify them.
/// Requires Solr 3.3+
/// </summary>
/// <param name="destinationCore"></param>
/// <param name="srcCore"></param>
/// <param name="srcCores"></param>
ResponseHeader Merge(string destinationCore, MergeCommand.SrcCore srcCore, params MergeCommand.SrcCore[] srcCores);

/// <summary>
/// Merge indexes using their path to identify them.
/// Requires Solr 1.4+
/// </summary>
/// <param name="destinationCore"></param>
/// <param name="indexDir"></param>
/// <param name="indexDirs"></param>
ResponseHeader Merge(string destinationCore, MergeCommand.IndexDir indexDir, params MergeCommand.IndexDir[] indexDirs);
}
}
22 changes: 22 additions & 0 deletions SolrNet/Impl/SolrCoreAdmin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,28 @@ public ResponseHeader Unload(string coreName, UnloadCommand.Delete delete) {
return SendAndParseHeader(new UnloadCommand(coreName, delete));
}

/// <summary>
/// Merge indexes using their core names to identify them.
/// Requires Solr 3.3+
/// </summary>
/// <param name="destinationCore"></param>
/// <param name="srcCore"></param>
/// <param name="srcCores"></param>
public ResponseHeader Merge(string destinationCore, MergeCommand.SrcCore srcCore, params MergeCommand.SrcCore[] srcCores) {
return SendAndParseHeader(new MergeCommand(destinationCore, srcCore, srcCores));
}

/// <summary>
/// Merge indexes using their path to identify them.
/// Requires Solr 1.4+
/// </summary>
/// <param name="destinationCore"></param>
/// <param name="indexDir"></param>
/// <param name="indexDirs"></param>
public ResponseHeader Merge(string destinationCore, MergeCommand.IndexDir indexDir, params MergeCommand.IndexDir[] indexDirs) {
return SendAndParseHeader(new MergeCommand(destinationCore, indexDir, indexDirs));
}

/// <summary>
/// Sends a command and parses the ResponseHeader.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions SolrNet/SolrNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
<Compile Include="Commands\Cores\AliasCommand.cs" />
<Compile Include="Commands\Cores\CreateCommand.cs" />
<Compile Include="Commands\Cores\LoadCommand.cs" />
<Compile Include="Commands\Cores\MergeCommand.cs" />
<Compile Include="Commands\Cores\ReloadCommand.cs" />
<Compile Include="Commands\Cores\RenameCommand.cs" />
<Compile Include="Commands\Cores\StatusCommand.cs" />
Expand Down
2 changes: 1 addition & 1 deletion Tests.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ let testWithCultures (cultures: #seq<CultureInfo>) =


let test =
Fuchu.MbUnit.MbUnitTestToFuchu typeof<CollapseResponseParserTests>
Fuchu.MbUnit.MbUnitTestToFuchu typeof<MergeCommandTests>
//|> Test.filter (fun s -> s.Contains "ParseResultsWithGroups")
//|> testWithCultures [CultureInfo "en-US"; CultureInfo "fr-FR"]
run test

0 comments on commit 797cad5

Please sign in to comment.