|
| 1 | +/* |
| 2 | + Copyright 2025 Bloomberg Finance L.P. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | + */ |
| 16 | +#include <build/db.h> |
| 17 | +#include <bdb_int.h> |
| 18 | +#include <log_info.h> |
| 19 | +#include <strings.h> |
| 20 | +#include <logmsg.h> |
| 21 | +#include <string.h> |
| 22 | + |
| 23 | +static LOG_INFO get_lsn_internal(bdb_state_type *bdb_state, int flags) |
| 24 | +{ |
| 25 | + int rc; |
| 26 | + |
| 27 | + /* get db internals */ |
| 28 | + DB_LOGC *logc; |
| 29 | + DBT logrec; |
| 30 | + DB_LSN log_lsn; |
| 31 | + LOG_INFO log_info = {0}; |
| 32 | + |
| 33 | + rc = bdb_state->dbenv->log_cursor(bdb_state->dbenv, &logc, 0); |
| 34 | + if (rc) { |
| 35 | + logmsg(LOGMSG_ERROR, "%s: Can't get log cursor rc %d\n", __func__, rc); |
| 36 | + return log_info; |
| 37 | + } |
| 38 | + bzero(&logrec, sizeof(DBT)); |
| 39 | + logrec.flags = DB_DBT_MALLOC; |
| 40 | + rc = logc->get(logc, &log_lsn, &logrec, flags); |
| 41 | + if (rc) { |
| 42 | + logmsg(LOGMSG_ERROR, "%s: Can't get last log record rc %d\n", __func__, rc); |
| 43 | + logc->close(logc, 0); |
| 44 | + return log_info; |
| 45 | + } |
| 46 | + |
| 47 | + log_info.file = log_lsn.file; |
| 48 | + log_info.offset = log_lsn.offset; |
| 49 | + log_info.size = logrec.size; |
| 50 | + |
| 51 | + if (logrec.data) |
| 52 | + free(logrec.data); |
| 53 | + |
| 54 | + logc->close(logc, 0); |
| 55 | + |
| 56 | + return log_info; |
| 57 | +} |
| 58 | + |
| 59 | +LOG_INFO get_last_lsn(bdb_state_type *bdb_state) |
| 60 | +{ |
| 61 | + return get_lsn_internal(bdb_state, DB_LAST); |
| 62 | +} |
| 63 | + |
| 64 | +LOG_INFO get_first_lsn(bdb_state_type *bdb_state) |
| 65 | +{ |
| 66 | + return get_lsn_internal(bdb_state, DB_FIRST); |
| 67 | +} |
| 68 | + |
| 69 | +uint32_t get_next_offset(DB_ENV *dbenv, LOG_INFO log_info) |
| 70 | +{ |
| 71 | + return log_info.offset + log_info.size + dbenv->get_log_header_size(dbenv); |
| 72 | +} |
| 73 | + |
| 74 | +int log_info_compare(LOG_INFO *a, LOG_INFO *b) |
| 75 | +{ |
| 76 | + if (a->file < b->file) |
| 77 | + return -1; |
| 78 | + if (a->file > b->file) |
| 79 | + return 1; |
| 80 | + if (a->offset < b->offset) |
| 81 | + return -1; |
| 82 | + if (a->offset > b->offset) |
| 83 | + return 1; |
| 84 | + return 0; |
| 85 | +} |
0 commit comments