Welcome to mirror list, hosted at ThFree Co, Russian Federation.

LocalListAffectedDatabase.cs « Database « Main « Library « Duplicati - github.com/duplicati/duplicati.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c3a9d1833de61086916998cfc0ad572ed24c2c80 (plain)
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
//  Copyright (C) 2015, The Duplicati Team

//  http://www.duplicati.com, info@duplicati.com
//
//  This library is free software; you can redistribute it and/or modify
//  it under the terms of the GNU Lesser General Public License as
//  published by the Free Software Foundation; either version 2.1 of the
//  License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful, but
//  WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using System.Linq;
using System.Collections.Generic;

namespace Duplicati.Library.Main.Database
{
    internal class LocalListAffectedDatabase : LocalDatabase
    {
        public LocalListAffectedDatabase(string path)
            : base(path, "ListAffected")
        {
        }

        private class ListResultFileset : Duplicati.Library.Interface.IListResultFileset
        {
            public long Version { get; set; }
            public DateTime Time { get; set; }
            public long FileCount { get; set; }
            public long FileSizes { get; set; }
        }

        private class ListResultFile : Duplicati.Library.Interface.IListResultFile
        {
            public string Path { get; set; }
            public IEnumerable<long> Sizes { get; set; }
        }

        private class ListResultRemoteLog : Duplicati.Library.Interface.IListResultRemoteLog
        {
            public DateTime Timestamp { get; set; }
            public string Message { get; set; }
        }

        private class ListResultRemoteVolume : Duplicati.Library.Interface.IListResultRemoteVolume
        {
            public string Name { get; set; }
        }

        public IEnumerable<Duplicati.Library.Interface.IListResultFileset> GetFilesets(IEnumerable<string> items)
        {
            var filesets = FilesetTimes.ToArray();
            var dict = new Dictionary<long, long>();
            for(var i = 0; i < filesets.Length; i++)
                dict[filesets[i].Key] = i;

            var sql = string.Format(
                @"SELECT DISTINCT ""FilesetID"" FROM (" +
                @"SELECT ""FilesetID"" FROM ""FilesetEntry"" WHERE ""FileID"" IN ( SELECT ""ID"" FROM ""File"" WHERE ""BlocksetID"" IN ( SELECT ""BlocksetID"" FROM ""BlocksetEntry"" WHERE ""BlockID"" IN ( SELECT ""ID"" From ""Block"" WHERE ""VolumeID"" IN ( SELECT ""ID"" FROM ""RemoteVolume"" WHERE ""Name"" IN ({0})))))" +
                " UNION " +
                @"SELECT ""ID"" FROM ""Fileset"" WHERE ""VolumeID"" IN ( SELECT ""ID"" FROM ""RemoteVolume"" WHERE ""Name"" IN ({0}))" +
                ")",
                string.Join(",", items.Select(x => "?"))
            );

            var it = new List<string>(items);
            it.AddRange(items);

            using(var cmd = m_connection.CreateCommand())
            using(var rd = cmd.ExecuteReader(sql, it.ToArray()))
                while (rd.Read())
                {
                    var v = dict[Convert.ToInt64(rd.GetValue(0))];
                    yield return new ListResultFileset() {
                        Version = v,
                        Time = filesets[v].Value
                    };
                }
        }

        public IEnumerable<Duplicati.Library.Interface.IListResultFile> GetFiles(IEnumerable<string> items)
        {
            var sql = string.Format(
                @"SELECT DISTINCT ""Path"" FROM (" +
                @"SELECT ""Path"" FROM ""File"" WHERE ""BlocksetID"" IN (SELECT ""BlocksetID"" FROM ""BlocksetEntry"" WHERE ""BlockID"" IN (SELECT ""ID"" FROM ""Block"" WHERE ""VolumeID"" IN (SELECT ""ID"" from ""RemoteVolume"" WHERE ""Name"" IN ({0}))))" +
                @" UNION " +
                @"SELECT ""Path"" FROM ""File"" WHERE ""MetadataID"" IN (SELECT ""ID"" FROM ""Metadataset"" WHERE ""BlocksetID"" IN (SELECT ""BlocksetID"" FROM ""BlocksetEntry"" WHERE ""BlockID"" IN (SELECT ""ID"" FROM ""Block"" WHERE ""VolumeID"" IN (SELECT ""ID"" from ""RemoteVolume"" WHERE ""Name"" IN ({0})))))" +
                @" UNION " +
                @"SELECT ""Path"" FROM ""File"" WHERE ""ID"" IN ( SELECT ""FileID"" FROM ""FilesetEntry"" WHERE ""FilesetID"" IN ( SELECT ""ID"" FROM ""Fileset"" WHERE ""VolumeID"" IN ( SELECT ""ID"" FROM ""RemoteVolume"" WHERE ""Name"" IN ({0}))))" +
                @") ORDER BY ""Path"" ",
                string.Join(",", items.Select(x => "?"))
            );
                
            var it = new List<string>(items);
            it.AddRange(items);
            it.AddRange(items);

            using(var cmd = m_connection.CreateCommand())
            using(var rd = cmd.ExecuteReader(sql, it.ToArray()))
                while (rd.Read())
                    yield return new ListResultFile() {
                        Path = Convert.ToString(rd.GetValue(0))
                    };
            
        }

        public IEnumerable<Duplicati.Library.Interface.IListResultRemoteLog> GetLogLines(IEnumerable<string> items)
        {
            var sql = string.Format(
                @"SELECT ""TimeStamp"", ""Message"" || "" "" || CASE WHEN ""Exception"" IS NULL THEN """" ELSE ""Exception"" END FROM ""LogData"" WHERE {0}" +
                @" UNION " +
                @"SELECT ""Timestamp"", ""Data"" FROM ""RemoteOperation"" WHERE ""Path"" IN ({1})",
                string.Join(" OR ", items.Select(x => @"""Message"" LIKE ?")),
                string.Join(",", items.Select(x => "?"))
            );

            var it = new List<string>(from n in items select "%" + n + "%");
            it.AddRange(items);

            using(var cmd = m_connection.CreateCommand())
            using(var rd = cmd.ExecuteReader(sql, it.ToArray()))
                while (rd.Read())
                    yield return new ListResultRemoteLog() {
                        Timestamp = ParseFromEpochSeconds(Convert.ToInt64(rd.GetValue(0))),
                        Message = Convert.ToString(rd.GetValue(1))
                    };
        }

        public IEnumerable<Duplicati.Library.Interface.IListResultRemoteVolume> GetVolumes(IEnumerable<string> items)
        {
            var sql = string.Format(
                @"SELECT DISTINCT ""Name"" FROM ( " +
                @" SELECT ""Name"" FROM ""Remotevolume"" WHERE ""ID"" IN ( SELECT ""VolumeID"" FROM ""Block"" WHERE ""ID"" IN ( SELECT ""BlockID"" FROM ""BlocksetEntry"" WHERE ""BlocksetID"" IN ( SELECT ""BlocksetID"" FROM ""File"" WHERE ""ID"" IN ( SELECT ""FileID"" FROM ""FilesetEntry"" WHERE ""FilesetID"" IN ( SELECT ""ID"" FROM ""Fileset"" WHERE ""VolumeID"" IN ( SELECT ""ID"" FROM ""RemoteVolume"" WHERE ""Name"" IN ({0}))))))) " +
                @" UNION " +
                @" SELECT ""Name"" FROM ""Remotevolume"" WHERE ""ID"" IN ( SELECT ""VolumeID"" FROM ""Block"" WHERE ""ID"" IN ( SELECT ""BlockID"" FROM ""BlocksetEntry"" WHERE ""BlocksetID"" IN ( SELECT ""BlocksetID"" FROM ""Metadataset"" WHERE ""ID"" IN ( SELECT ""MetadataID"" FROM ""File"" WHERE ""ID"" IN ( SELECT ""FileID"" FROM ""FilesetEntry"" WHERE ""FilesetID"" IN ( SELECT ""ID"" FROM ""Fileset"" WHERE ""VolumeID"" IN ( SELECT ""ID"" FROM ""RemoteVolume"" WHERE ""Name"" IN ({0}))))))))" +
                @")",
                string.Join(",", items.Select(x => "?"))
            );

            var it = new List<string>(items);
            it.AddRange(items);

            using(var cmd = m_connection.CreateCommand())
            using(var rd = cmd.ExecuteReader(sql, it.ToArray()))
                while (rd.Read())
                    yield return new ListResultRemoteVolume() {
                        Name = Convert.ToString(rd.GetValue(0))
                    };
        }
    }
}