tdb2: unify tdb1_store into tdb_store
[ccan] / ccan / tdb2 / tdb.c
index a1ec332a09b1af790cff0934fac37b084dfae914..a7aa4572382b0f8b10ef03c4adc08599fd7e7293 100644 (file)
@@ -1,10 +1,24 @@
+ /*
+   Trivial Database 2: fetch, store and misc routines.
+   Copyright (C) Rusty Russell 2010
+
+   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 3 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, see <http://www.gnu.org/licenses/>.
+*/
 #include "private.h"
 #include <ccan/asprintf/asprintf.h>
 #include <stdarg.h>
 
-/* The null return. */
-struct tdb_data tdb_null = { .dptr = NULL, .dsize = 0 };
-
 static enum TDB_ERROR update_rec_hdr(struct tdb_context *tdb,
                                     tdb_off_t off,
                                     tdb_len_t keylen,
@@ -41,10 +55,11 @@ static enum TDB_ERROR replace_data(struct tdb_context *tdb,
 
        /* We didn't like the existing one: remove it. */
        if (old_off) {
-               add_stat(tdb, frees, 1);
+               tdb->stats.frees++;
                ecode = add_free_record(tdb, old_off,
                                        sizeof(struct tdb_used_record)
-                                       + key.dsize + old_room);
+                                       + key.dsize + old_room,
+                                       TDB_LOCK_WAIT, true);
                if (ecode == TDB_SUCCESS)
                        ecode = replace_in_hash(tdb, h, new_off);
        } else {
@@ -55,18 +70,20 @@ static enum TDB_ERROR replace_data(struct tdb_context *tdb,
        }
 
        new_off += sizeof(struct tdb_used_record);
-       ecode = tdb->methods->twrite(tdb, new_off, key.dptr, key.dsize);
+       ecode = tdb->tdb2.io->twrite(tdb, new_off, key.dptr, key.dsize);
        if (ecode != TDB_SUCCESS) {
                return ecode;
        }
 
        new_off += key.dsize;
-       ecode = tdb->methods->twrite(tdb, new_off, dbuf.dptr, dbuf.dsize);
+       ecode = tdb->tdb2.io->twrite(tdb, new_off, dbuf.dptr, dbuf.dsize);
        if (ecode != TDB_SUCCESS) {
                return ecode;
        }
 
-       /* FIXME: tdb_increment_seqnum(tdb); */
+       if (tdb->flags & TDB_SEQNUM)
+               tdb_inc_seqnum(tdb);
+
        return TDB_SUCCESS;
 }
 
@@ -77,11 +94,14 @@ static enum TDB_ERROR update_data(struct tdb_context *tdb,
 {
        enum TDB_ERROR ecode;
 
-       ecode = tdb->methods->twrite(tdb, off, dbuf.dptr, dbuf.dsize);
+       ecode = tdb->tdb2.io->twrite(tdb, off, dbuf.dptr, dbuf.dsize);
        if (ecode == TDB_SUCCESS && extra) {
                /* Put a zero in; future versions may append other data. */
-               ecode = tdb->methods->twrite(tdb, off + dbuf.dsize, "", 1);
+               ecode = tdb->tdb2.io->twrite(tdb, off + dbuf.dsize, "", 1);
        }
+       if (tdb->flags & TDB_SEQNUM)
+               tdb_inc_seqnum(tdb);
+
        return ecode;
 }
 
@@ -94,9 +114,15 @@ enum TDB_ERROR tdb_store(struct tdb_context *tdb,
        struct tdb_used_record rec;
        enum TDB_ERROR ecode;
 
+       if (tdb->flags & TDB_VERSION1) {
+               if (tdb1_store(tdb, key, dbuf, flag) == -1)
+                       return tdb->last_error;
+               return TDB_SUCCESS;
+       }
+
        off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
        if (TDB_OFF_IS_ERR(off)) {
-               return off;
+               return tdb->last_error = off;
        }
 
        /* Now we have lock on this hash bucket. */
@@ -126,7 +152,7 @@ enum TDB_ERROR tdb_store(struct tdb_context *tdb,
                                }
                                tdb_unlock_hashes(tdb, h.hlock_start,
                                                  h.hlock_range, F_WRLCK);
-                               return TDB_SUCCESS;
+                               return tdb->last_error = TDB_SUCCESS;
                        }
                } else {
                        if (flag == TDB_MODIFY) {
@@ -143,7 +169,7 @@ enum TDB_ERROR tdb_store(struct tdb_context *tdb,
        ecode = replace_data(tdb, &h, key, dbuf, off, old_room, off);
 out:
        tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
-       return ecode;
+       return tdb->last_error = ecode;
 }
 
 enum TDB_ERROR tdb_append(struct tdb_context *tdb,
@@ -159,7 +185,7 @@ enum TDB_ERROR tdb_append(struct tdb_context *tdb,
 
        off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
        if (TDB_OFF_IS_ERR(off)) {
-               return off;
+               return tdb->last_error = off;
        }
 
        if (off) {
@@ -191,7 +217,7 @@ enum TDB_ERROR tdb_append(struct tdb_context *tdb,
                                                    + dbuf.dsize));
                        goto out;
                }
-               ecode = tdb->methods->tread(tdb, off + sizeof(rec) + key.dsize,
+               ecode = tdb->tdb2.io->tread(tdb, off + sizeof(rec) + key.dsize,
                                            newdata, old_dlen);
                if (ecode != TDB_SUCCESS) {
                        goto out_free_newdata;
@@ -211,7 +237,7 @@ out_free_newdata:
        free(newdata);
 out:
        tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
-       return ecode;
+       return tdb->last_error = ecode;
 }
 
 enum TDB_ERROR tdb_fetch(struct tdb_context *tdb, struct tdb_data key,
@@ -224,7 +250,7 @@ enum TDB_ERROR tdb_fetch(struct tdb_context *tdb, struct tdb_data key,
 
        off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL);
        if (TDB_OFF_IS_ERR(off)) {
-               return off;
+               return tdb->last_error = off;
        }
 
        if (!off) {
@@ -240,7 +266,24 @@ enum TDB_ERROR tdb_fetch(struct tdb_context *tdb, struct tdb_data key,
        }
 
        tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
-       return ecode;
+       return tdb->last_error = ecode;
+}
+
+bool tdb_exists(struct tdb_context *tdb, TDB_DATA key)
+{
+       tdb_off_t off;
+       struct tdb_used_record rec;
+       struct hash_info h;
+
+       off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL);
+       if (TDB_OFF_IS_ERR(off)) {
+               tdb->last_error = off;
+               return false;
+       }
+       tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
+
+       tdb->last_error = TDB_SUCCESS;
+       return off ? true : false;
 }
 
 enum TDB_ERROR tdb_delete(struct tdb_context *tdb, struct tdb_data key)
@@ -252,7 +295,7 @@ enum TDB_ERROR tdb_delete(struct tdb_context *tdb, struct tdb_data key)
 
        off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
        if (TDB_OFF_IS_ERR(off)) {
-               return off;
+               return tdb->last_error = off;
        }
 
        if (!off) {
@@ -266,16 +309,20 @@ enum TDB_ERROR tdb_delete(struct tdb_context *tdb, struct tdb_data key)
        }
 
        /* Free the deleted entry. */
-       add_stat(tdb, frees, 1);
+       tdb->stats.frees++;
        ecode = add_free_record(tdb, off,
                                sizeof(struct tdb_used_record)
                                + rec_key_length(&rec)
                                + rec_data_length(&rec)
-                               + rec_extra_padding(&rec));
+                               + rec_extra_padding(&rec),
+                               TDB_LOCK_WAIT, true);
+
+       if (tdb->flags & TDB_SEQNUM)
+               tdb_inc_seqnum(tdb);
 
 unlock:
        tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
-       return ecode;
+       return tdb->last_error = ecode;
 }
 
 unsigned int tdb_get_flags(struct tdb_context *tdb)
@@ -283,11 +330,43 @@ unsigned int tdb_get_flags(struct tdb_context *tdb)
        return tdb->flags;
 }
 
+static bool inside_transaction(const struct tdb_context *tdb)
+{
+       if (tdb->flags & TDB_VERSION1)
+               return tdb->tdb1.transaction != NULL;
+       else
+               return tdb->tdb2.transaction != NULL;
+}
+
+static bool readonly_changable(struct tdb_context *tdb, const char *caller)
+{
+       if (inside_transaction(tdb)) {
+               tdb->last_error = tdb_logerr(tdb, TDB_ERR_EINVAL,
+                                            TDB_LOG_USE_ERROR,
+                                            "%s: can't change"
+                                            " TDB_RDONLY inside transaction",
+                                            caller);
+               return false;
+       }
+
+       if (tdb->file->allrecord_lock.count != 0
+           || tdb->file->num_lockrecs != 0) {
+               tdb->last_error = tdb_logerr(tdb, TDB_ERR_EINVAL,
+                                            TDB_LOG_USE_ERROR,
+                                            "%s: can't change"
+                                            " TDB_RDONLY holding locks",
+                                            caller);
+               return false;
+       }
+       return true;
+}
+
 void tdb_add_flag(struct tdb_context *tdb, unsigned flag)
 {
        if (tdb->flags & TDB_INTERNAL) {
-               tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
-                          "tdb_add_flag: internal db");
+               tdb->last_error = tdb_logerr(tdb, TDB_ERR_EINVAL,
+                                            TDB_LOG_USE_ERROR,
+                                            "tdb_add_flag: internal db");
                return;
        }
        switch (flag) {
@@ -301,17 +380,30 @@ void tdb_add_flag(struct tdb_context *tdb, unsigned flag)
        case TDB_NOSYNC:
                tdb->flags |= TDB_NOSYNC;
                break;
+       case TDB_SEQNUM:
+               tdb->flags |= TDB_SEQNUM;
+               break;
+       case TDB_ALLOW_NESTING:
+               tdb->flags |= TDB_ALLOW_NESTING;
+               break;
+       case TDB_RDONLY:
+               if (readonly_changable(tdb, "tdb_add_flag"))
+                       tdb->flags |= TDB_RDONLY;
+               break;
        default:
-               tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
-                          "tdb_add_flag: Unknown flag %u", flag);
+               tdb->last_error = tdb_logerr(tdb, TDB_ERR_EINVAL,
+                                            TDB_LOG_USE_ERROR,
+                                            "tdb_add_flag: Unknown flag %u",
+                                            flag);
        }
 }
 
 void tdb_remove_flag(struct tdb_context *tdb, unsigned flag)
 {
        if (tdb->flags & TDB_INTERNAL) {
-               tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
-                          "tdb_remove_flag: internal db");
+               tdb->last_error = tdb_logerr(tdb, TDB_ERR_EINVAL,
+                                            TDB_LOG_USE_ERROR,
+                                            "tdb_remove_flag: internal db");
                return;
        }
        switch (flag) {
@@ -325,9 +417,29 @@ void tdb_remove_flag(struct tdb_context *tdb, unsigned flag)
        case TDB_NOSYNC:
                tdb->flags &= ~TDB_NOSYNC;
                break;
+       case TDB_SEQNUM:
+               tdb->flags &= ~TDB_SEQNUM;
+               break;
+       case TDB_ALLOW_NESTING:
+               tdb->flags &= ~TDB_ALLOW_NESTING;
+               break;
+       case TDB_RDONLY:
+               if ((tdb->open_flags & O_ACCMODE) == O_RDONLY) {
+                       tdb->last_error = tdb_logerr(tdb, TDB_ERR_EINVAL,
+                                                    TDB_LOG_USE_ERROR,
+                                                    "tdb_remove_flag: can't"
+                                                    " remove TDB_RDONLY on tdb"
+                                                    " opened with O_RDONLY");
+                       break;
+               }
+               if (readonly_changable(tdb, "tdb_remove_flag"))
+                       tdb->flags &= ~TDB_RDONLY;
+               break;
        default:
-               tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
-                          "tdb_remove_flag: Unknown flag %u", flag);
+               tdb->last_error = tdb_logerr(tdb, TDB_ERR_EINVAL,
+                                            TDB_LOG_USE_ERROR,
+                                            "tdb_remove_flag: Unknown flag %u",
+                                            flag);
        }
 }
 
@@ -348,6 +460,11 @@ const char *tdb_errorstr(enum TDB_ERROR ecode)
        return "Invalid error code";
 }
 
+enum TDB_ERROR tdb_error(struct tdb_context *tdb)
+{
+       return tdb->last_error;
+}
+
 enum TDB_ERROR COLD tdb_logerr(struct tdb_context *tdb,
                               enum TDB_ERROR ecode,
                               enum tdb_log_level level,
@@ -359,7 +476,7 @@ enum TDB_ERROR COLD tdb_logerr(struct tdb_context *tdb,
        /* tdb_open paths care about errno, so save it. */
        int saved_errno = errno;
 
-       if (!tdb->logfn)
+       if (!tdb->log_fn)
                return ecode;
 
        va_start(ap, fmt);
@@ -367,13 +484,71 @@ enum TDB_ERROR COLD tdb_logerr(struct tdb_context *tdb,
        va_end(ap);
 
        if (len < 0) {
-               tdb->logfn(tdb, TDB_LOG_ERROR, tdb->log_private,
-                          "out of memory formatting message:");
-               tdb->logfn(tdb, level, tdb->log_private, fmt);
+               tdb->log_fn(tdb, TDB_LOG_ERROR, TDB_ERR_OOM,
+                           "out of memory formatting message:", tdb->log_data);
+               tdb->log_fn(tdb, level, ecode, fmt, tdb->log_data);
        } else {
-               tdb->logfn(tdb, level, tdb->log_private, message);
+               tdb->log_fn(tdb, level, ecode, message, tdb->log_data);
                free(message);
        }
        errno = saved_errno;
        return ecode;
 }
+
+enum TDB_ERROR tdb_parse_record_(struct tdb_context *tdb,
+                                TDB_DATA key,
+                                enum TDB_ERROR (*parse)(TDB_DATA k,
+                                                        TDB_DATA d,
+                                                        void *data),
+                                void *data)
+{
+       tdb_off_t off;
+       struct tdb_used_record rec;
+       struct hash_info h;
+       enum TDB_ERROR ecode;
+
+       off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL);
+       if (TDB_OFF_IS_ERR(off)) {
+               return tdb->last_error = off;
+       }
+
+       if (!off) {
+               ecode = TDB_ERR_NOEXIST;
+       } else {
+               const void *dptr;
+               dptr = tdb_access_read(tdb, off + sizeof(rec) + key.dsize,
+                                      rec_data_length(&rec), false);
+               if (TDB_PTR_IS_ERR(dptr)) {
+                       ecode = TDB_PTR_ERR(dptr);
+               } else {
+                       TDB_DATA d = tdb_mkdata(dptr, rec_data_length(&rec));
+
+                       ecode = parse(key, d, data);
+                       tdb_access_release(tdb, dptr);
+               }
+       }
+
+       tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
+       return tdb->last_error = ecode;
+}
+
+const char *tdb_name(const struct tdb_context *tdb)
+{
+       return tdb->name;
+}
+
+int64_t tdb_get_seqnum(struct tdb_context *tdb)
+{
+       tdb_off_t off = tdb_read_off(tdb, offsetof(struct tdb_header, seqnum));
+       if (TDB_OFF_IS_ERR(off))
+               tdb->last_error = off;
+       else
+               tdb->last_error = TDB_SUCCESS;
+       return off;
+}
+       
+
+int tdb_fd(const struct tdb_context *tdb)
+{
+       return tdb->file->fd;
+}