2013-04-17 00:00:59 -04:00
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
// vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
2013-04-16 23:59:03 -04:00
# ident "$Id$"
2013-04-17 00:00:59 -04:00
# ident "Copyright (c) 2007-2012 Tokutek Inc. All rights reserved."
2013-04-16 23:58:01 -04:00
# ident "The technology is licensed by the Massachusetts Institute of Technology, Rutgers State University of New Jersey, and the Research Foundation of State University of New York at Stony Brook under United States of America Serial No. 11 / 760379 and to the patents and / or patent applications resulting from it."
# include "includes.h"
// for now, implement with singlely-linked-list
// first = oldest (delete from beginning)
// last = newest (add to end)
struct lfm_entry {
TOKULOGFILEINFO lf_info ;
struct lfm_entry * next ;
} ;
struct toku_logfilemgr {
struct lfm_entry * first ;
struct lfm_entry * last ;
int n_entries ;
} ;
int toku_logfilemgr_create ( TOKULOGFILEMGR * lfm ) {
// malloc a logfilemgr
2013-04-17 00:00:58 -04:00
TOKULOGFILEMGR XMALLOC ( mgr ) ;
2013-04-16 23:58:01 -04:00
mgr - > first = NULL ;
mgr - > last = NULL ;
2013-04-17 00:00:27 -04:00
mgr - > n_entries = 0 ;
2013-04-16 23:58:01 -04:00
* lfm = mgr ;
return 0 ;
}
int toku_logfilemgr_destroy ( TOKULOGFILEMGR * lfm ) {
int r = 0 ;
if ( * lfm ! = NULL ) { // be tolerant of being passed a NULL
TOKULOGFILEMGR mgr = * lfm ;
while ( mgr - > n_entries > 0 ) {
toku_logfilemgr_delete_oldest_logfile_info ( mgr ) ;
}
toku_free ( * lfm ) ;
* lfm = NULL ;
}
return r ;
}
2013-04-17 00:00:57 -04:00
int toku_logfilemgr_init ( TOKULOGFILEMGR lfm , const char * log_dir , TXNID * last_xid_if_clean_shutdown ) {
invariant_notnull ( lfm ) ;
invariant_notnull ( last_xid_if_clean_shutdown ) ;
2013-04-16 23:58:01 -04:00
int r ;
int n_logfiles ;
char * * logfiles ;
r = toku_logger_find_logfiles ( log_dir , & logfiles , & n_logfiles ) ;
if ( r ! = 0 )
return r ;
TOKULOGCURSOR cursor ;
struct log_entry * entry ;
TOKULOGFILEINFO lf_info ;
long long index = - 1 ;
char * basename ;
2013-04-16 23:58:02 -04:00
LSN tmp_lsn = { 0 } ;
2013-04-17 00:00:57 -04:00
TXNID last_xid = TXNID_NONE ;
2013-04-16 23:58:01 -04:00
for ( int i = 0 ; i < n_logfiles ; i + + ) {
2013-04-17 00:00:58 -04:00
XMALLOC ( lf_info ) ;
2013-04-16 23:58:01 -04:00
// find the index
2013-04-16 23:59:25 -04:00
// basename is the filename of the i-th logfile
2013-04-16 23:58:01 -04:00
basename = strrchr ( logfiles [ i ] , ' / ' ) + 1 ;
2013-04-16 23:59:17 -04:00
int version ;
r = sscanf ( basename , " log%lld.tokulog%d " , & index , & version ) ;
assert ( r = = 2 ) ; // found index and version
2013-04-16 23:59:25 -04:00
assert ( version > = TOKU_LOG_MIN_SUPPORTED_VERSION ) ;
assert ( version < = TOKU_LOG_VERSION ) ;
2013-04-16 23:58:01 -04:00
lf_info - > index = index ;
2013-04-16 23:59:25 -04:00
lf_info - > version = version ;
// find last LSN in logfile
2013-04-16 23:58:01 -04:00
r = toku_logcursor_create_for_file ( & cursor , log_dir , basename ) ;
2013-04-17 00:00:57 -04:00
if ( r ! = 0 ) {
2013-04-16 23:58:01 -04:00
return r ;
2013-04-17 00:00:57 -04:00
}
2013-04-16 23:59:25 -04:00
r = toku_logcursor_last ( cursor , & entry ) ; // set "entry" to last log entry in logfile
2013-04-17 00:00:57 -04:00
if ( r = = 0 ) {
2013-04-16 23:58:01 -04:00
lf_info - > maxlsn = toku_log_entry_get_lsn ( entry ) ;
2013-04-17 00:00:57 -04:00
invariant ( lf_info - > maxlsn . lsn > = tmp_lsn . lsn ) ;
2013-04-16 23:58:02 -04:00
tmp_lsn = lf_info - > maxlsn ;
2013-04-17 00:00:57 -04:00
if ( entry - > cmd = = LT_shutdown ) {
last_xid = entry - > u . shutdown . last_xid ;
} else {
last_xid = TXNID_NONE ;
}
2013-04-16 23:58:01 -04:00
}
else {
2013-04-16 23:58:02 -04:00
lf_info - > maxlsn = tmp_lsn ; // handle empty logfile (no LSN in file) case
2013-04-16 23:58:01 -04:00
}
2013-04-16 23:58:01 -04:00
// add to logfilemgr
toku_logfilemgr_add_logfile_info ( lfm , lf_info ) ;
toku_logcursor_destroy ( & cursor ) ;
}
for ( int i = 0 ; i < n_logfiles ; i + + ) {
toku_free ( logfiles [ i ] ) ;
}
toku_free ( logfiles ) ;
2013-04-17 00:00:57 -04:00
* last_xid_if_clean_shutdown = last_xid ;
2013-04-16 23:58:01 -04:00
return 0 ;
}
int toku_logfilemgr_num_logfiles ( TOKULOGFILEMGR lfm ) {
2013-04-17 00:00:27 -04:00
assert ( lfm ) ;
2013-04-16 23:58:01 -04:00
return lfm - > n_entries ;
}
int toku_logfilemgr_add_logfile_info ( TOKULOGFILEMGR lfm , TOKULOGFILEINFO lf_info ) {
2013-04-17 00:00:27 -04:00
assert ( lfm ) ;
2013-04-17 00:00:58 -04:00
struct lfm_entry * XMALLOC ( entry ) ;
2013-04-17 00:00:27 -04:00
entry - > lf_info = lf_info ;
entry - > next = NULL ;
if ( lfm - > n_entries ! = 0 )
lfm - > last - > next = entry ;
lfm - > last = entry ;
lfm - > n_entries + + ;
if ( lfm - > n_entries = = 1 ) {
lfm - > first = lfm - > last ;
2013-04-16 23:58:01 -04:00
}
2013-04-17 00:00:27 -04:00
return 0 ;
2013-04-16 23:58:01 -04:00
}
TOKULOGFILEINFO toku_logfilemgr_get_oldest_logfile_info ( TOKULOGFILEMGR lfm ) {
2013-04-17 00:00:27 -04:00
assert ( lfm ) ;
2013-04-16 23:58:01 -04:00
return lfm - > first - > lf_info ;
}
void toku_logfilemgr_delete_oldest_logfile_info ( TOKULOGFILEMGR lfm ) {
2013-04-17 00:00:27 -04:00
assert ( lfm ) ;
2013-04-16 23:58:01 -04:00
if ( lfm - > n_entries > 0 ) {
struct lfm_entry * entry = lfm - > first ;
toku_free ( entry - > lf_info ) ;
lfm - > first = entry - > next ;
toku_free ( entry ) ;
lfm - > n_entries - - ;
if ( lfm - > n_entries = = 0 ) {
lfm - > last = lfm - > first = NULL ;
}
}
}
LSN toku_logfilemgr_get_last_lsn ( TOKULOGFILEMGR lfm ) {
2013-04-17 00:00:27 -04:00
assert ( lfm ) ;
2013-04-16 23:58:01 -04:00
if ( lfm - > n_entries = = 0 ) {
LSN lsn ;
lsn . lsn = 0 ;
return lsn ;
}
return lfm - > last - > lf_info - > maxlsn ;
}
void toku_logfilemgr_update_last_lsn ( TOKULOGFILEMGR lfm , LSN lsn ) {
2013-04-17 00:00:27 -04:00
assert ( lfm ) ;
2013-04-16 23:58:01 -04:00
assert ( lfm - > last ! = NULL ) ;
lfm - > last - > lf_info - > maxlsn = lsn ;
}
void toku_logfilemgr_print ( TOKULOGFILEMGR lfm ) {
2013-04-17 00:00:27 -04:00
assert ( lfm ) ;
2013-04-16 23:58:01 -04:00
printf ( " toku_logfilemgr_print [%p] : %d entries \n " , lfm , lfm - > n_entries ) ;
struct lfm_entry * entry = lfm - > first ;
2013-04-17 00:00:27 -04:00
for ( int i = 0 ; i < lfm - > n_entries ; i + + ) {
2013-04-17 00:00:58 -04:00
printf ( " entry %d : index = % " PRId64 " , maxlsn = % " PRIu64 " \n " , i , entry - > lf_info - > index , entry - > lf_info - > maxlsn . lsn ) ;
2013-04-16 23:58:01 -04:00
entry = entry - > next ;
}
}