database.h 42.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Copyright (C) 2011  Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

#ifndef __DATABASE_DATASRC_H
#define __DATABASE_DATASRC_H

18
19
#include <string>

20
21
#include <boost/scoped_ptr.hpp>

22
#include <dns/rrclass.h>
23
24
#include <dns/rrclass.h>
#include <dns/rrset.h>
25

26
27
#include <datasrc/client.h>

28
#include <dns/name.h>
29
#include <exceptions/exceptions.h>
30

31
32
33
#include <map>
#include <set>

34
35
36
namespace isc {
namespace datasrc {

37
/**
38
 * \brief Abstraction of lowlevel database with DNS data
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
 *
 * This class is defines interface to databases. Each supported database
 * will provide methods for accessing the data stored there in a generic
 * manner. The methods are meant to be low-level, without much or any knowledge
 * about DNS and should be possible to translate directly to queries.
 *
 * On the other hand, how the communication with database is done and in what
 * schema (in case of relational/SQL database) is up to the concrete classes.
 *
 * This class is non-copyable, as copying connections to database makes little
 * sense and will not be needed.
 *
 * \todo Is it true this does not need to be copied? For example the zone
 *     iterator might need it's own copy. But a virtual clone() method might
 *     be better for that than copy constructor.
 *
 * \note The same application may create multiple connections to the same
56
57
58
 *     database, having multiple instances of this class. If the database
 *     allows having multiple open queries at one connection, the connection
 *     class may share it.
59
 */
60
class DatabaseAccessor : boost::noncopyable {
61
public:
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
    /**
     * Definitions of the fields as they are required to be filled in
     * by IteratorContext::getNext()
     *
     * When implementing getNext(), the columns array should
     * be filled with the values as described in this enumeration,
     * in this order, i.e. TYPE_COLUMN should be the first element
     * (index 0) of the array, TTL_COLUMN should be the second element
     * (index 1), etc.
     */
    enum RecordColumns {
        TYPE_COLUMN = 0,    ///< The RRType of the record (A/NS/TXT etc.)
        TTL_COLUMN = 1,     ///< The TTL of the record (a
        SIGTYPE_COLUMN = 2, ///< For RRSIG records, this contains the RRTYPE
                            ///< the RRSIG covers. In the current implementation,
                            ///< this field is ignored.
        RDATA_COLUMN = 3,   ///< Full text representation of the record's RDATA
        NAME_COLUMN = 4,    ///< The domain name of this RR
        COLUMN_COUNT = 5    ///< The total number of columns, MUST be value of
                            ///< the largest other element in this enum plus 1.
    };
83

JINMEI Tatuya's avatar
JINMEI Tatuya committed
84
85
86
87
    /**
     * Definitions of the fields to be passed to addRecordToZone().
     *
     * Each derived implementation of addRecordToZone() should expect
JINMEI Tatuya's avatar
JINMEI Tatuya committed
88
     * the "columns" array to be filled with the values as described in this
JINMEI Tatuya's avatar
JINMEI Tatuya committed
89
90
91
92
93
     * enumeration, in this order.
     */
    enum AddRecordColumns {
        ADD_NAME = 0, ///< The owner name of the record (a domain name)
        ADD_REV_NAME = 1, ///< Reversed name of NAME (used for DNSSEC)
94
        ADD_TTL = 2,     ///< The TTL of the record (in numeric form)
JINMEI Tatuya's avatar
JINMEI Tatuya committed
95
96
97
98
99
100
101
102
103
104
105
        ADD_TYPE = 3,    ///< The RRType of the record (A/NS/TXT etc.)
        ADD_SIGTYPE = 4, ///< For RRSIG records, this contains the RRTYPE
                            ///< the RRSIG covers.
        ADD_RDATA = 5,    ///< Full text representation of the record's RDATA
        ADD_COLUMN_COUNT = 6 ///< Number of columns
    };

    /**
     * Definitions of the fields to be passed to deleteRecordInZone().
     *
     * Each derived implementation of deleteRecordInZone() should expect
JINMEI Tatuya's avatar
JINMEI Tatuya committed
106
     * the "params" array to be filled with the values as described in this
JINMEI Tatuya's avatar
JINMEI Tatuya committed
107
108
109
110
111
112
113
114
     * enumeration, in this order.
     */
    enum DeleteRecordParams {
        DEL_NAME = 0, ///< The owner name of the record (a domain name)
        DEL_TYPE = 1, ///< The RRType of the record (A/NS/TXT etc.)
        DEL_RDATA = 2, ///< Full text representation of the record's RDATA
        DEL_PARAM_COUNT = 3 ///< Number of parameters
    };
115

116
117
118
119
120
    /**
     * Operation mode when adding a record diff.
     *
     * This is used as the "operation" parameter value of addRecordDiff().
     */
121
    enum DiffOperation {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
122
123
        DIFF_ADD = 0,           ///< This diff is for adding an RR
        DIFF_DELETE = 1         ///< This diff is for deleting an RR
124
125
    };

JINMEI Tatuya's avatar
JINMEI Tatuya committed
126
127
128
129
130
131
132
    /**
     * Definitions of the fields to be passed to addRecordDiff().
     *
     * Each derived implementation of addRecordDiff() should expect
     * the "params" array to be filled with the values as described in this
     * enumeration, in this order.
     */
133
    enum DiffRecordParams {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
134
135
136
137
138
        DIFF_NAME = 0, ///< The owner name of the record (a domain name)
        DIFF_TYPE = 1, ///< The RRType of the record (A/NS/TXT etc.)
        DIFF_TTL = 2,  ///< The TTL of the record (in numeric form)
        DIFF_RDATA = 3, ///< Full text representation of the record's RDATA
        DIFF_PARAM_COUNT = 4    ///< Number of parameters
139
140
    };

141
142
143
144
145
146
    /**
     * \brief Destructor
     *
     * It is empty, but needs a virtual one, since we will use the derived
     * classes in polymorphic way.
     */
147
    virtual ~DatabaseAccessor() { }
148

149
150
151
152
153
154
155
156
157
158
159
    /**
     * \brief Retrieve a zone identifier
     *
     * This method looks up a zone for the given name in the database. It
     * should match only exact zone name (eg. name is equal to the zone's
     * apex), as the DatabaseClient will loop trough the labels itself and
     * find the most suitable zone.
     *
     * It is not specified if and what implementation of this method may throw,
     * so code should expect anything.
     *
160
161
     * \param name The (fully qualified) domain name of the zone's apex to be
     *             looked up.
162
163
164
165
     * \return The first part of the result indicates if a matching zone
     *     was found. In case it was, the second part is internal zone ID.
     *     This one will be passed to methods finding data in the zone.
     *     It is not required to keep them, in which case whatever might
166
     *     be returned - the ID is only passed back to the database as
167
168
     *     an opaque handle.
     */
169
    virtual std::pair<bool, int> getZone(const std::string& name) const = 0;
170

171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
    /**
     * \brief This holds the internal context of ZoneIterator for databases
     *
     * While the ZoneIterator implementation from DatabaseClient does all the
     * translation from strings to DNS classes and validation, this class
     * holds the pointer to where the database is at reading the data.
     *
     * It can either hold shared pointer to the connection which created it
     * and have some kind of statement inside (in case single database
     * connection can handle multiple concurrent SQL statements) or it can
     * create a new connection (or, if it is more convenient, the connection
     * itself can inherit both from DatabaseConnection and IteratorContext
     * and just clone itself).
     */
    class IteratorContext : public boost::noncopyable {
    public:
        /**
         * \brief Destructor
         *
         * Virtual destructor, so any descendand class is destroyed correctly.
         */
        virtual ~IteratorContext() { }
Jelte Jansen's avatar
Jelte Jansen committed
193

194
195
196
197
        /**
         * \brief Function to provide next resource record
         *
         * This function should provide data about the next resource record
Jelte Jansen's avatar
Jelte Jansen committed
198
         * from the data that is searched. The data is not converted yet.
199
         *
Jelte Jansen's avatar
Jelte Jansen committed
200
201
         * Depending on how the iterator was constructed, there is a difference
         * in behaviour; for a 'full zone iterator', created with
202
203
204
205
         * getAllRecords(), all COLUMN_COUNT elements of the array are
         * overwritten.
         * For a 'name iterator', created with getRecords(), the column
         * NAME_COLUMN is untouched, since what would be added here is by
Jelte Jansen's avatar
Jelte Jansen committed
206
207
         * definition already known to the caller (it already passes it as
         * an argument to getRecords()).
208
         *
209
210
211
212
213
         * Once this function returns false, any subsequent call to it should
         * result in false.  The implementation of a derived class must ensure
         * it doesn't cause any disruption due to that such as a crash or
         * exception.
         *
Jelte Jansen's avatar
Jelte Jansen committed
214
215
         * \note The order of RRs is not strictly set, but the RRs for single
         * RRset must not be interleaved with any other RRs (eg. RRsets must be
216
217
         * "together").
         *
218
         * \param columns The data will be returned through here. The order
Jelte Jansen's avatar
Jelte Jansen committed
219
220
         *     is specified by the RecordColumns enum, and the size must be
         *     COLUMN_COUNT
221
222
         * \todo Do we consider databases where it is stored in binary blob
         *     format?
223
224
225
         * \throw DataSourceError if there's database-related error. If the
         *     exception (or any other in case of derived class) is thrown,
         *     the iterator can't be safely used any more.
226
227
228
         * \return true if a record was found, and the columns array was
         *         updated. false if there was no more data, in which case
         *         the columns array is untouched.
229
         */
230
        virtual bool getNext(std::string (&columns)[COLUMN_COUNT]) = 0;
231
    };
Jelte Jansen's avatar
Jelte Jansen committed
232

233
    typedef boost::shared_ptr<IteratorContext> IteratorContextPtr;
Jelte Jansen's avatar
Jelte Jansen committed
234

235
236
237
    /**
     * \brief Creates an iterator context for a specific name.
     *
238
239
     * Returns an IteratorContextPtr that contains all records of the
     * given name from the given zone.
240
     *
Jelte Jansen's avatar
Jelte Jansen committed
241
     * The implementation of the iterator that is returned may leave the
242
     * NAME_COLUMN column of the array passed to getNext() untouched, as that
Jelte Jansen's avatar
Jelte Jansen committed
243
244
     * data is already known (it is the same as the name argument here)
     *
245
246
247
248
     * \exception any Since any implementation can be used, the caller should
     *            expect any exception to be thrown.
     *
     * \param name The name to search for. This should be a FQDN.
249
     * \param id The ID of the zone, returned from getZone().
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
250
251
252
     * \param subdomains If set to true, match subdomains of name instead
     *     of name itself. It is used to find empty domains and match
     *     wildcards.
253
254
     * \return Newly created iterator context. Must not be NULL.
     */
255
    virtual IteratorContextPtr getRecords(const std::string& name,
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
256
257
                                          int id,
                                          bool subdomains = false) const = 0;
258

259
    /**
260
     * \brief Creates an iterator context for the whole zone.
261
     *
262
263
     * Returns an IteratorContextPtr that contains all records of the
     * zone with the given zone id.
264
     *
265
266
267
268
269
270
     * Each call to getNext() on the returned iterator should copy all
     * column fields of the array that is passed, as defined in the
     * RecordColumns enum.
     *
     * \exception any Since any implementation can be used, the caller should
     *            expect any exception to be thrown.
271
272
273
274
     *
     * \param id The ID of the zone, returned from getZone().
     * \return Newly created iterator context. Must not be NULL.
     */
275
    virtual IteratorContextPtr getAllRecords(int id) const = 0;
276

JINMEI Tatuya's avatar
JINMEI Tatuya committed
277
278
279
280
281
282
283
284
285
286
287
    /// Start a transaction for updating a zone.
    ///
    /// Each derived class version of this method starts a database
    /// transaction to make updates to the given name of zone (whose class was
    /// specified at the construction of the class).
    ///
    /// If \c replace is true, any existing records of the zone will be
    /// deleted on successful completion of updates (after
    /// \c commitUpdateZone()); if it's false, the existing records will be
    /// intact unless explicitly deleted by \c deleteRecordInZone().
    ///
288
    /// A single \c DatabaseAccessor instance can perform at most one
JINMEI Tatuya's avatar
JINMEI Tatuya committed
289
    /// transaction; a duplicate call to this method before
290
291
292
293
294
295
    /// \c commitUpdateZone() or \c rollbackUpdateZone(), or a call to this
    /// method within another transaction started by \c startTransaction()
    /// will result in a \c DataSourceError exception.
    /// If multiple update attempts need to be performed concurrently (and
    /// if the underlying database allows such operation), separate
    /// \c DatabaseAccessor instance must be created.
JINMEI Tatuya's avatar
JINMEI Tatuya committed
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
    ///
    /// \note The underlying database may not allow concurrent updates to
    /// the same database instance even if different "connections" (or
    /// something similar specific to the database implementation) are used
    /// for different sets of updates.  For example, it doesn't seem to be
    /// possible for SQLite3 unless different databases are used.  MySQL
    /// allows concurrent updates to different tables of the same database,
    /// but a specific operation may block others.  As such, this interface
    /// doesn't require derived classes to allow concurrent updates with
    /// multiple \c DatabaseAccessor instances; however, the implementation
    /// is encouraged to do the best for making it more likely to succeed
    /// as long as the underlying database system allows concurrent updates.
    ///
    /// This method returns a pair of \c bool and \c int.  Its first element
    /// indicates whether the given name of zone is found.  If it's false,
    /// the transaction isn't considered to be started; a subsequent call to
    /// this method with an existing zone name should succeed.  Likewise,
    /// if a call to this method results in an exception, the transaction
    /// isn't considered to be started.  Note also that if the zone is not
    /// found this method doesn't try to create a new one in the database.
    /// It must have been created by some other means beforehand.
    ///
    /// The second element is the internal zone ID used for subsequent
    /// updates.  Depending on implementation details of the actual derived
    /// class method, it may be different from the one returned by
    /// \c getZone(); for example, a specific implementation may use a
    /// completely new zone ID when \c replace is true.
    ///
324
325
326
    /// \exception DataSourceError Duplicate call to this method, call to
    /// this method within another transaction, or some internal database
    /// related error.
JINMEI Tatuya's avatar
JINMEI Tatuya committed
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
    ///
    /// \param zone_name A string representation of the zone name to be updated
    /// \param replace Whether to replace the entire zone (see above)
    ///
    /// \return A pair of bool and int, indicating whether the specified zone
    /// exists and (if so) the zone ID to be used for the update, respectively.
    virtual std::pair<bool, int> startUpdateZone(const std::string& zone_name,
                                                 bool replace) = 0;

    /// Add a single record to the zone to be updated.
    ///
    /// This method provides a simple interface to insert a new record
    /// (a database "row") to the zone in the update context started by
    /// \c startUpdateZone().  The zone to which the record to be added
    /// is the one specified at the time of the \c startUpdateZone() call.
    ///
    /// A successful call to \c startUpdateZone() must have preceded to
    /// this call; otherwise a \c DataSourceError exception will be thrown.
    ///
    /// The row is defined as a vector of strings that has exactly
    /// ADD_COLUMN_COUNT number of elements.  See AddRecordColumns for
    /// the semantics of each element.
    ///
    /// Derived class methods are not required to check whether the given
    /// values in \c columns are valid in terms of the expected semantics;
    /// in general, it's the caller's responsibility.
    /// For example, TTLs would normally be expected to be a textual
    /// representation of decimal numbers, but this interface doesn't require
    /// the implementation to perform this level of validation.  It may check
    /// the values, however, and in that case if it detects an error it
    /// should throw a \c DataSourceError exception.
    ///
    /// Likewise, derived class methods are not required to detect any
    /// duplicate record that is already in the zone.
    ///
    /// \note The underlying database schema may not have a trivial mapping
    /// from this style of definition of rows to actual database records.
    /// It's the implementation's responsibility to implement the mapping
    /// in the actual derived method.
    ///
    /// \exception DataSourceError Invalid call without starting a transaction,
368
    /// or other internal database error.
JINMEI Tatuya's avatar
JINMEI Tatuya committed
369
    ///
370
    /// \param columns An array of strings that defines a record to be added
JINMEI Tatuya's avatar
JINMEI Tatuya committed
371
    /// to the zone.
372
373
    virtual void addRecordToZone(
        const std::string (&columns)[ADD_COLUMN_COUNT]) = 0;
JINMEI Tatuya's avatar
JINMEI Tatuya committed
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404

    /// Delete a single record from the zone to be updated.
    ///
    /// This method provides a simple interface to delete a record
    /// (a database "row") from the zone in the update context started by
    /// \c startUpdateZone().  The zone from which the record to be deleted
    /// is the one specified at the time of the \c startUpdateZone() call.
    ///
    /// A successful call to \c startUpdateZone() must have preceded to
    /// this call; otherwise a \c DataSourceError exception will be thrown.
    ///
    /// The record to be deleted is specified by a vector of strings that has
    /// exactly DEL_PARAM_COUNT number of elements.  See DeleteRecordParams
    /// for the semantics of each element.
    ///
    /// \note In IXFR, TTL may also be specified, but we intentionally
    /// ignore that in this interface, because it's not guaranteed
    /// that all records have the same TTL (unlike the RRset
    /// assumption) and there can even be multiple records for the
    /// same name, type and rdata with different TTLs.  If we only
    /// delete one of them, subsequent lookup will still return a
    /// positive answer, which would be confusing.  It's a higher
    /// layer's responsibility to check if there is at least one
    /// record in the database that has the given TTL.
    ///
    /// Like \c addRecordToZone, derived class methods are not required to
    /// validate the semantics of the given parameters or to check if there
    /// is a record that matches the specified parameter; if there isn't
    /// it simply ignores the result.
    ///
    /// \exception DataSourceError Invalid call without starting a transaction,
405
    /// or other internal database error.
JINMEI Tatuya's avatar
JINMEI Tatuya committed
406
    ///
407
    /// \param params An array of strings that defines a record to be deleted
JINMEI Tatuya's avatar
JINMEI Tatuya committed
408
409
    /// from the zone.
    virtual void deleteRecordInZone(
410
        const std::string (&params)[DEL_PARAM_COUNT]) = 0;
JINMEI Tatuya's avatar
JINMEI Tatuya committed
411

412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
    /// Start a general transaction.
    ///
    /// Each derived class version of this method starts a database
    /// transaction in a way specific to the database details.  Any subsequent
    /// operations on the accessor are guaranteed to be not susceptible to
    /// any update attempts made during the transaction.  The transaction
    /// must be terminated by either \c commit() or \c rollback().
    ///
    /// In practice, this transaction is intended to be used to perform
    /// a set of atomic reads and work as a read-only lock.  So, in many
    /// cases \c commit() and \c rollback() will have the same effect.
    ///
    /// This transaction cannot coexist with an update transaction started
    /// by \c startUpdateZone().  Such an attempt will result in
    /// \c DataSourceError.
    ///
    /// \exception DataSourceError An attempt of nested transaction, or some
    /// internal database related error.
    virtual void startTransaction() = 0;

432
    /// Commit a transaction.
JINMEI Tatuya's avatar
JINMEI Tatuya committed
433
    ///
434
435
    /// This method completes a transaction started by \c startTransaction
    /// or \c startUpdateZone.
JINMEI Tatuya's avatar
JINMEI Tatuya committed
436
    ///
437
    /// A successful call to one of the "start" methods must have preceded to
JINMEI Tatuya's avatar
JINMEI Tatuya committed
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
    /// this call; otherwise a \c DataSourceError exception will be thrown.
    /// Once this method successfully completes, the transaction isn't
    /// considered to exist any more.  So a new transaction can now be
    /// started.  On the other hand, a duplicate call to this method after
    /// a successful completion of it is invalid and should result in
    /// a \c DataSourceError exception.
    ///
    /// If some internal database error happens, a \c DataSourceError
    /// exception must be thrown.  In that case the transaction is still
    /// considered to be valid; the caller must explicitly rollback it
    /// or (if it's confident that the error is temporary) try to commit it
    /// again.
    ///
    /// \exception DataSourceError Call without a transaction, duplicate call
    /// to the method or internal database error.
453
    virtual void commit() = 0;
JINMEI Tatuya's avatar
JINMEI Tatuya committed
454

455
    /// Rollback any changes in a transaction made so far.
JINMEI Tatuya's avatar
JINMEI Tatuya committed
456
    ///
457
458
459
460
    /// This method rollbacks a transaction started by \c startTransaction or
    /// \c startUpdateZone.  When it succeeds (it normally should, but see
    /// below), the underlying database should be reverted to the point
    /// before performing the corresponding "start" method.
JINMEI Tatuya's avatar
JINMEI Tatuya committed
461
    ///
462
    /// A successful call to one of the "start" method must have preceded to
JINMEI Tatuya's avatar
JINMEI Tatuya committed
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
    /// this call; otherwise a \c DataSourceError exception will be thrown.
    /// Once this method successfully completes, the transaction isn't
    /// considered to exist any more.  So a new transaction can now be
    /// started.  On the other hand, a duplicate call to this method after
    /// a successful completion of it is invalid and should result in
    /// a \c DataSourceError exception.
    ///
    /// Normally this method should not fail.  But it may not always be
    /// possible to guarantee it depending on the characteristics of the
    /// underlying database system.  So this interface doesn't require the
    /// actual implementation for the error free property.  But if a specific
    /// implementation of this method can fail, it is encouraged to document
    /// when that can happen with its implication.
    ///
    /// \exception DataSourceError Call without a transaction, duplicate call
    /// to the method or internal database error.
479
    virtual void rollback() = 0;
JINMEI Tatuya's avatar
JINMEI Tatuya committed
480

JINMEI Tatuya's avatar
JINMEI Tatuya committed
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
    /// Install a single RR diff in difference sequences for zone update.
    ///
    /// This method inserts parameters of an update operation for a single RR
    /// (either adding or deleting one) in the underlying database.
    /// (These parameters would normally be a separate database table, but
    /// actual realization can differ in specific implementations).
    /// The information given via this method generally corresponds to either
    /// a single call to \c addRecordToZone() or \c deleteRecordInZone(),
    /// and this method is expected to be called immediately after (or before)
    /// a call to either of those methods.
    ///
    /// Note, however, that this method passes more detailed information
    /// than those update methods: it passes "serial", even if the diff
    /// is not for the SOA RR; it passes TTL for a diff that deletes an RR
    /// while in \c deleteRecordInZone() it's omitted.  This is because
    /// the stored diffs are expected to be retrieved in the form that
    /// \c getRecordDiffs() is expected to meet.  This means if the caller
    /// wants to use this method with other update operations, it must
    /// ensure the additional information is ready when this method is called.
    ///
    /// \note \c getRecordDiffs() is not yet implemented.
    ///
    /// The caller of this method must ensure that the added diffs via
    /// this method in a single transaction form an IXFR-style difference
    /// sequences: Each difference sequence is a sequence of RRs:
    /// an older version of SOA (to be deleted), zero or more other deleted
    /// RRs, the post-transaction SOA (to be added), and zero or more other
    /// added RRs.  So, for example, the first call to this method in a
    /// transaction must always be deleting an SOA.  Also, the \c serial
    /// parameter must be equal to the value of the serial field of the
    /// SOA that was last added or deleted (if the call is to add or delete
    /// an SOA RR, \c serial must be identical to the serial of that SOA).
    /// The underlying derived class implementation may or may not check
    /// this condition, but if the caller doesn't meet the condition
    /// a subsequent call to \c getRecordDiffs() will not work as expected.
    ///
    /// Any call to this method must be in a transaction, and, for now,
    /// it must be a transaction triggered by \c startUpdateZone() (that is,
    /// it cannot be a transaction started by \c startTransaction()).
    /// All calls to this method are considered to be part of an atomic
    /// transaction: Until \c commit() is performed, the added diffs are
    /// not visible outside the transaction; if \c rollback() is performed,
    /// all added diffs are canceled; and the added sequences are not
    /// affected by any concurrent attempt of adding diffs (conflict resolution
    /// is up to the database implementation).
    ///
    /// Also for now, all diffs are assumed to be for the zone that is
    /// being updated in the context of \c startUpdateZone().  So the
    /// \c zone_id parameter must be identical to the zone ID returned by
    /// \c startUpdateZone().
    ///
    /// In a future version we may loosen this condition so that diffs can be
    /// added in a generic transaction and may not even have to belong to
    /// a single zone.  For this possible extension \c zone_id parameter is
    /// included even if it's redundant under the current restriction.
    ///
537
538
539
540
    /// The support for adding (or retrieving) diffs is optional; if it's
    /// not supported in a specific data source, this method for the
    /// corresponding derived class will throw an \c NotImplemented exception.
    ///
JINMEI Tatuya's avatar
JINMEI Tatuya committed
541
542
543
    /// \exception DataSourceError Invalid call without starting a transaction,
    /// zone ID doesn't match the zone being updated, or other internal
    /// database error.
544
545
    /// \exception NotImplemented Adding diffs is not supported in the
    /// data source.
JINMEI Tatuya's avatar
JINMEI Tatuya committed
546
547
548
549
550
551
552
    /// \exception Other The concrete derived method may throw other
    /// data source specific exceptions.
    ///
    /// \param zone_id The zone for the diff to be added.
    /// \param serial The SOA serial to which the diff belongs.
    /// \param operation Either \c DIFF_ADD or \c DIFF_DELETE.
    /// \param params An array of strings that defines a record for the diff.
553
554
555
556
    virtual void addRecordDiff(
        int zone_id, uint32_t serial, DiffOperation operation,
        const std::string (&params)[DIFF_PARAM_COUNT]) = 0;

557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
    /// Clone the accessor with the same configuration.
    ///
    /// Each derived class implementation of this method will create a new
    /// accessor of the same derived class with the same configuration
    /// (such as the database server address) as that of the caller object
    /// and return it.
    ///
    /// Note that other internal states won't be copied to the new accessor
    /// even though the name of "clone" may indicate so.  For example, even
    /// if the calling accessor is in the middle of a update transaction,
    /// the new accessor will not start a transaction to trace the same
    /// updates.
    ///
    /// The intended use case of cloning is to create a separate context
    /// where a specific set of database operations can be performed
    /// independently from the original accessor.  The updater will use it
    /// so that multiple updaters can be created concurrently even if the
    /// underlying database system doesn't allow running multiple transactions
    /// in a single database connection.
    ///
    /// The underlying database system may not support the functionality
    /// that would be needed to implement this method.  For example, it
    /// may not allow a single thread (or process) to have more than one
    /// database connections.  In such a case the derived class implementation
    /// should throw a \c DataSourceError exception.
    ///
    /// \return A shared pointer to the cloned accessor.
584
    virtual boost::shared_ptr<DatabaseAccessor> clone() = 0;
585
586

    /**
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
587
588
589
590
591
592
593
594
595
596
597
     * \brief Returns a string identifying this dabase backend
     *
     * The returned string is mainly intended to be used for
     * debugging/logging purposes.
     *
     * Any implementation is free to choose the exact string content,
     * but it is advisable to make it a name that is distinguishable
     * from the others.
     *
     * \return the name of the database
     */
598
    virtual const std::string& getDBName() const = 0;
599
600

    /**
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
601
     * \brief It returns the previous name in DNSSEC order.
602
603
604
605
     *
     * This is used in DatabaseClient::findPreviousName and does more
     * or less the real work, except for working on strings.
     *
606
     * \param rname The name to ask for previous of, in reversed form.
607
608
609
610
     *     We use the reversed form (see isc::dns::Name::reverse),
     *     because then the case insensitive order of string representation
     *     and the DNSSEC order correspond (eg. org.example.a is followed
     *     by org.example.a.b which is followed by org.example.b, etc).
611
     * \param zone_id The zone to look through.
612
613
     * \return The previous name.
     * \note This function must return previous name even in case
614
     *     the queried rname does not exist in the zone.
615
616
617
     * \note This method must skip under-the-zone-cut data (glue data).
     *     This might be implemented by looking for NSEC records (as glue
     *     data don't have them) in the zone or in some other way.
618
619
     *
     * \throw DataSourceError if there's a problem with the database.
620
621
622
623
     * \throw NotImplemented if this database doesn't support DNSSEC
     *     or there's no previous name for the queried one (the NSECs
     *     might be missing or the queried name is less or equal the
     *     apex of the zone).
624
625
     */
    virtual std::string findPreviousName(int zone_id,
626
                                         const std::string& rname) const = 0;
627
628
};

629
630
631
632
633
/**
 * \brief Concrete data source client oriented at database backends.
 *
 * This class (together with corresponding versions of ZoneFinder,
 * ZoneIterator, etc.) translates high-level data source queries to
634
 * low-level calls on DatabaseAccessor. It calls multiple queries
635
 * if necessary and validates data from the database, allowing the
636
 * DatabaseAccessor to be just simple translation to SQL/other
637
638
639
640
 * queries to database.
 *
 * While it is possible to subclass it for specific database in case
 * of special needs, it is not expected to be needed. This should just
641
 * work as it is with whatever DatabaseAccessor.
642
 */
643
644
class DatabaseClient : public DataSourceClient {
public:
645
646
647
    /**
     * \brief Constructor
     *
648
     * It initializes the client with a database via the given accessor.
649
     *
650
     * \exception isc::InvalidParameter if accessor is NULL. It might throw
651
652
     * standard allocation exception as well, but doesn't throw anything else.
     *
653
     * \param rrclass The RR class of the zones that this client will handle.
654
655
656
     * \param accessor The accessor to the database to use to get data.
     *  As the parameter suggests, the client takes ownership of the accessor
     *  and will delete it when itself deleted.
657
     */
658
    DatabaseClient(isc::dns::RRClass rrclass,
659
                   boost::shared_ptr<DatabaseAccessor> accessor);
660

661
662
663
664
665
    /**
     * \brief Corresponding ZoneFinder implementation
     *
     * The zone finder implementation for database data sources. Similarly
     * to the DatabaseClient, it translates the queries to methods of the
666
     * database.
667
668
669
670
671
672
673
674
675
676
677
678
679
680
     *
     * Application should not come directly in contact with this class
     * (it should handle it trough generic ZoneFinder pointer), therefore
     * it could be completely hidden in the .cc file. But it is provided
     * to allow testing and for rare cases when a database needs slightly
     * different handling, so it can be subclassed.
     *
     * Methods directly corresponds to the ones in ZoneFinder.
     */
    class Finder : public ZoneFinder {
    public:
        /**
         * \brief Constructor
         *
681
         * \param database The database (shared with DatabaseClient) to
682
683
         *     be used for queries (the one asked for ID before).
         * \param zone_id The zone ID which was returned from
684
         *     DatabaseAccessor::getZone and which will be passed to further
685
         *     calls to the database.
686
687
688
         * \param origin The name of the origin of this zone. It could query
         *     it from database, but as the DatabaseClient just searched for
         *     the zone using the name, it should have it.
689
         */
690
691
        Finder(boost::shared_ptr<DatabaseAccessor> database, int zone_id,
               const isc::dns::Name& origin);
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
692
693
        // The following three methods are just implementations of inherited
        // ZoneFinder's pure virtual methods.
694
695
        virtual isc::dns::Name getOrigin() const;
        virtual isc::dns::RRClass getClass() const;
696
697
698

        /**
         * \brief Find an RRset in the datasource
699
         *
Jelte Jansen's avatar
Jelte Jansen committed
700
701
702
703
704
705
706
707
708
709
710
         * Searches the datasource for an RRset of the given name and
         * type. If there is a CNAME at the given name, the CNAME rrset
         * is returned.
         * (this implementation is not complete, and currently only
         * does full matches, CNAMES, and the signatures for matches and
         * CNAMEs)
         * \note target was used in the original design to handle ANY
         *       queries. This is not implemented yet, and may use
         *       target again for that, but it might also use something
         *       different. It is left in for compatibility at the moment.
         * \note options are ignored at this moment
Jelte Jansen's avatar
Jelte Jansen committed
711
         *
712
713
714
715
716
717
718
719
720
721
722
723
724
725
         * \note Maybe counter intuitively, this method is not a const member
         * function.  This is intentional; some of the underlying implementations
         * are expected to use a database backend, and would internally contain
         * some abstraction of "database connection".  In the most strict sense
         * any (even read only) operation might change the internal state of
         * such a connection, and in that sense the operation cannot be considered
         * "const".  In order to avoid giving a false sense of safety to the
         * caller, we indicate a call to this method may have a surprising
         * side effect.  That said, this view may be too strict and it may
         * make sense to say the internal database connection doesn't affect
         * external behavior in terms of the interface of this method.  As
         * we gain more experiences with various kinds of backends we may
         * revisit the constness.
         *
Jelte Jansen's avatar
Jelte Jansen committed
726
727
728
729
730
731
732
733
         * \exception DataSourceError when there is a problem reading
         *                            the data from the dabase backend.
         *                            This can be a connection, code, or
         *                            data (parse) error.
         *
         * \param name The name to find
         * \param type The RRType to find
         * \param target Unused at this moment
734
735
         * \param options Options about how to search.
         *     See ZoneFinder::FindOptions.
736
         */
737
738
739
        virtual FindResult find(const isc::dns::Name& name,
                                const isc::dns::RRType& type,
                                isc::dns::RRsetList* target = NULL,
Jelte Jansen's avatar
Jelte Jansen committed
740
                                const FindOptions options = FIND_DEFAULT);
Jelte Jansen's avatar
Jelte Jansen committed
741

742
743
744
745
746
747
        /**
         * \brief Implementation of ZoneFinder::findPreviousName method.
         */
        virtual isc::dns::Name findPreviousName(const isc::dns::Name& query)
            const;

748
749
750
751
752
753
754
755
        /**
         * \brief The zone ID
         *
         * This function provides the stored zone ID as passed to the
         * constructor. This is meant for testing purposes and normal
         * applications shouldn't need it.
         */
        int zone_id() const { return (zone_id_); }
756

757
        /**
758
         * \brief The database accessor.
759
         *
760
         * This function provides the database accessor stored inside as
761
762
763
         * passed to the constructor. This is meant for testing purposes and
         * normal applications shouldn't need it.
         */
764
765
        const DatabaseAccessor& getAccessor() const {
            return (*accessor_);
766
        }
767
    private:
768
        boost::shared_ptr<DatabaseAccessor> accessor_;
769
        const int zone_id_;
770
        const isc::dns::Name origin_;
771
772
773
774
775
776
        //
        /// \brief Shortcut name for the result of getRRsets
        typedef std::pair<bool, std::map<dns::RRType, dns::RRsetPtr> >
            FoundRRsets;
        /// \brief Just shortcut for set of types
        typedef std::set<dns::RRType> WantedTypes;
777
778
779
780
781
782
783
784
785
786
787
788
        /**
         * \brief Searches database for RRsets of one domain.
         *
         * This method scans RRs of single domain specified by name and
         * extracts any RRsets found and requested by parameters.
         *
         * It is used internally by find(), because it is called multiple
         * times (usually with different domains).
         *
         * \param name Which domain name should be scanned.
         * \param types List of types the caller is interested in.
         * \param check_ns If this is set to true, it checks nothing lives
789
         *     together with NS record (with few little exceptions, like RRSIG
790
791
792
793
794
795
796
797
798
799
800
801
802
803
         *     or NSEC). This check is meant for non-apex NS records.
         * \param construct_name If this is NULL, the resulting RRsets have
         *     their name set to name. If it is not NULL, it overrides the name
         *     and uses this one (this can be used for wildcard synthesized
         *     records).
         * \return A pair, where the first element indicates if the domain
         *     contains any RRs at all (not only the requested, it may happen
         *     this is set to true, but the second part is empty). The second
         *     part is map from RRtypes to RRsets of the corresponding types.
         *     If the RRset is not present in DB, the RRtype is not there at
         *     all (so you'll not find NULL pointer in the result).
         * \throw DataSourceError If there's a low-level error with the
         *     database or the database contains bad data.
         */
804
805
806
        FoundRRsets getRRsets(const std::string& name,
                              const WantedTypes& types, bool check_ns,
                              const std::string* construct_name = NULL);
807
808
809
810
811
812
813
814
815
        /**
         * \brief Checks if something lives below this domain.
         *
         * This looks if there's any subdomain of the given name. It can be
         * used to test if domain is empty non-terminal.
         *
         * \param name The domain to check.
         */
        bool hasSubdomains(const std::string& name);
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832

        /**
         * \brief Get the NSEC covering a name.
         *
         * This one calls findPreviousName on the given name and extracts an NSEC
         * record on the result. It handles various error cases. The method exists
         * to share code present at more than one location.
         */
        dns::RRsetPtr findNSECCover(const dns::Name& name);

        /**
         * \brief Convenience type shortcut.
         *
         * To find stuff in the result of getRRsets.
         */
        typedef std::map<dns::RRType, dns::RRsetPtr>::const_iterator
            FoundIterator;
833
    };
834

835
836
837
    /**
     * \brief Find a zone in the database
     *
838
     * This queries database's getZone to find the best matching zone.
839
840
841
842
     * It will propagate whatever exceptions are thrown from that method
     * (which is not restricted in any way).
     *
     * \param name Name of the zone or data contained there.
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
843
844
845
846
847
     * \return FindResult containing the code and an instance of Finder, if
     *     anything is found. However, application should not rely on the
     *     ZoneFinder being instance of Finder (possible subclass of this class
     *     may return something else and it may change in future versions), it
     *     should use it as a ZoneFinder only.
848
     */
849
    virtual FindResult findZone(const isc::dns::Name& name) const;
850

851
852
853
854
855
856
857
858
859
860
    /**
     * \brief Get the zone iterator
     *
     * The iterator allows going through the whole zone content. If the
     * underlying DatabaseConnection is implemented correctly, it should
     * be possible to have multiple ZoneIterators at once and query data
     * at the same time.
     *
     * \exception DataSourceError if the zone doesn't exist.
     * \exception isc::NotImplemented if the underlying DatabaseConnection
861
862
     *     doesn't implement iteration. But in case it is not implemented
     *     and the zone doesn't exist, DataSourceError is thrown.
863
864
865
     * \exception Anything else the underlying DatabaseConnection might
     *     want to throw.
     * \param name The origin of the zone to iterate.
866
867
868
869
870
871
     * \param separate_rrs If true, the iterator will return each RR as a
     *                     new RRset object. If false, the iterator will
     *                     combine consecutive RRs with the name and type
     *                     into 1 RRset. The capitalization of the RRset will
     *                     be that of the first RR read, and TTLs will be
     *                     adjusted to the lowest one found.
872
873
     * \return Shared pointer to the iterator (it will never be NULL)
     */
874
    virtual ZoneIteratorPtr getIterator(const isc::dns::Name& name,
875
                                        bool separate_rrs = false) const;
Jelte Jansen's avatar
Jelte Jansen committed
876

877
878
879
880
    /// This implementation internally clones the accessor from the one
    /// used in the client and starts a separate transaction using the cloned
    /// accessor.  The returned updater will be able to work separately from
    /// the original client.
881
882
    virtual ZoneUpdaterPtr getUpdater(const isc::dns::Name& name,
                                      bool replace) const;
883

884
private:
885
886
887
    /// \brief The RR class that this client handles.
    const isc::dns::RRClass rrclass_;

888
889
    /// \brief The accessor to our database.
    const boost::shared_ptr<DatabaseAccessor> accessor_;
890
891
892
893
894
};

}
}

895
896
897
898
899
#endif  // __DATABASE_DATASRC_H

// Local Variables:
// mode: c++
// End: