Skip to content

Commit

Permalink
(elastic-search) Implements export for es range command
Browse files Browse the repository at this point in the history
  • Loading branch information
violet-dev committed Apr 6, 2021
1 parent 2d74d78 commit 0b31077
Showing 1 changed file with 79 additions and 4 deletions.
83 changes: 79 additions & 4 deletions hsync/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ public class Options : IConsoleOption
Info = "Export database bulk datas for elastic-search to json", Help = "use --export-for-es")]
public bool ExportForES;

[CommandLine("--export-for-es-range", CommandType.ARGUMENTS, ArgumentsCount = 2,
Info = "Export database bulk datas for elastic-search to json using id range", Help = "--export-for-es-range")]
public string[] ExportForESRange;

/// <summary>
/// User Option
/// </summary>
Expand Down Expand Up @@ -178,6 +182,10 @@ public static void Start(string[] arguments)
{
ProcessExportToES(null);
}
else if (option.ExportForESRange != null)
{
ProcessExportForESRange(option.ExportForESRange);
}
else if (option.Error)
{
Console.WriteLine(option.ErrorMessage);
Expand Down Expand Up @@ -763,16 +771,16 @@ static void ProcessExportToES(string[] args)
ss.Add(JsonConvert.SerializeObject(article));
}

RETRY_PUSH:
RETRY_PUSH:

var request = (HttpWebRequest)WebRequest.Create(Settings.Instance.Model.ElasticSearchHost);

request.Method = "POST";
request.ContentType = "application/json";

var request_stream = new StreamWriter(request.GetRequestStream());

request_stream.Write(string.Join("\n", ss) + "\n");
request_stream.Write(string.Join("\n", ss) + "\n");
request_stream.Close();

try
Expand All @@ -781,14 +789,81 @@ static void ProcessExportToES(string[] args)
{
}
}
catch (WebException)
catch (WebException we)
{
using (var stream = we.Response.GetResponseStream())
using (var reader = new StreamReader(stream))
{
Console.WriteLine(reader.ReadToEnd());
}
Thread.Sleep(1000);
goto RETRY_PUSH;
}
Thread.Sleep(1000);
}
}

static void ProcessExportForESRange(string[] args)
{
var arg1 = Convert.ToInt32(args[0]);
var arg2 = Convert.ToInt32(args[1]);

var db = new SQLiteConnection("data.db");
var count = db.ExecuteScalar<int>("SELECT COUNT(*) FROM HitomiColumnModel");

const int perLoop = 6000;

for (int i = 0; i < count; i += perLoop)
{
var query = db.Query<HitomiColumnModel>($"SELECT * FROM HitomiColumnModel ORDER BY Id LIMIT {perLoop} OFFSET {i}");
Console.WriteLine($"{i}/{count}");

query = query.Where(x => arg1 <= x.Id && x.Id <= arg2).ToList();

if (query.Count == 0) continue;

var ss = new List<string>();
foreach (var article in query)
{
JObject id = new JObject();
id.Add("_id", article.Id);
JObject index = new JObject();
index.Add("index", id);
ss.Add(JsonConvert.SerializeObject(index));
article.Thumbnail = null;
ss.Add(JsonConvert.SerializeObject(article));
}

RETRY_PUSH:

var request = (HttpWebRequest)WebRequest.Create(Settings.Instance.Model.ElasticSearchHost);

request.Method = "POST";
request.ContentType = "application/json";

var request_stream = new StreamWriter(request.GetRequestStream());

request_stream.Write(string.Join("\n", ss) + "\n");
request_stream.Close();

try
{
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
}
}
catch (WebException we)
{
using (var stream = we.Response.GetResponseStream())
using (var reader = new StreamReader(stream))
{
Console.WriteLine(reader.ReadToEnd());
}
Thread.Sleep(1000);
goto RETRY_PUSH;
}
Thread.Sleep(1000);
}
}
}

Expand Down

0 comments on commit 0b31077

Please sign in to comment.