Skip to content

Commit edc504b

Browse files
committed
comdb2_unused_files systable
Changes from review- Tx Morgan! Signed-off-by: Mark Hannum <mhannum@bloomberg.net>
1 parent 8cf3dbd commit edc504b

14 files changed

Lines changed: 165 additions & 0 deletions

File tree

bdb/bdb_api.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2501,4 +2501,8 @@ int release_locks_int(const char *trace, const char *func, int line, struct sqlc
25012501
int bdb_keylen(bdb_state_type *bdb_state, int ixnum);
25022502

25032503
void llmeta_collect_tablename_alias(void);
2504+
typedef int (*collect_unused_files_f)(void *args, int lognum, char *filename);
2505+
2506+
void oldfile_hash_collect(collect_unused_files_f func, void *arg);
2507+
25042508
#endif

bdb/file.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7748,6 +7748,29 @@ void oldfile_dump(void)
77487748
hash_for(oldfile_hash, oldfile_hash_dump, NULL);
77497749
}
77507750

7751+
struct collect_unused_files {
7752+
collect_unused_files_f func;
7753+
void *arg;
7754+
};
7755+
7756+
static int oldfile_collect(void *ptr, void *arg)
7757+
{
7758+
struct unused_file *obj = (struct unused_file *)ptr;
7759+
struct collect_unused_files *u = (struct collect_unused_files *)arg;
7760+
(*u->func)(u->arg, obj->lognum, obj->fname);
7761+
return 0;
7762+
}
7763+
7764+
void oldfile_hash_collect(collect_unused_files_f func, void *arg)
7765+
{
7766+
Pthread_mutex_lock(&unused_files_mtx);
7767+
if (oldfile_hash) {
7768+
struct collect_unused_files u = {.func = func, .arg = arg};
7769+
hash_for(oldfile_hash, oldfile_collect, &u);
7770+
}
7771+
Pthread_mutex_unlock(&unused_files_mtx);
7772+
}
7773+
77517774
/* use hash tbl to check if we have added file to list previously */
77527775
static int oldfile_list_contains(const char *filename)
77537776
{

sqlite/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ add_library(sqlite
6868
ext/comdb2/triggers.c
6969
ext/comdb2/tunables.c
7070
ext/comdb2/typesamples.c
71+
ext/comdb2/unused_files.c
7172
ext/comdb2/users.c
7273
ext/comdb2/views.c
7374
ext/comdb2/fdb.c

sqlite/ext/comdb2/comdb2systblInt.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ int systblSchemaVersionsInit(sqlite3 *db);
106106
int systblTableMetricsInit(sqlite3 *db);
107107
int systblApiHistoryInit(sqlite3 *db);
108108
int systblDbInfoInit(sqlite3 *db);
109+
int systblUnusedFilesInit(sqlite3 *db);
109110

110111
/* Simple yes/no answer for booleans */
111112
#define YESNO(x) ((x) ? "Y" : "N")

sqlite/ext/comdb2/tables.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ int comdb2SystblInit(
248248
rc = systblApiHistoryInit(db);
249249
if (rc == SQLITE_OK)
250250
rc = systblDbInfoInit(db);
251+
if (rc == SQLITE_OK)
252+
rc = systblUnusedFilesInit(db);
251253
#endif
252254
return rc;
253255
}

sqlite/ext/comdb2/unused_files.c

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
17+
#include <assert.h>
18+
#include <comdb2systblInt.h>
19+
#include <ezsystables.h>
20+
#include <bdb_api.h>
21+
22+
typedef struct systable_unused_files {
23+
char *filename;
24+
int64_t reflog;
25+
} systable_unused_files_t;
26+
27+
typedef struct getunusedfiles {
28+
int count;
29+
int alloc;
30+
systable_unused_files_t *records;
31+
} getunusedfiles_t;
32+
33+
static int collect(void *args, int reflog, char *filename)
34+
{
35+
getunusedfiles_t *a = (getunusedfiles_t *)args;
36+
systable_unused_files_t *u;
37+
a->count++;
38+
if (a->count >= a->alloc) {
39+
if (a->alloc == 0) a->alloc = 16;
40+
else a->alloc = a->alloc * 2;
41+
a->records = realloc(a->records, a->alloc * sizeof(systable_unused_files_t));
42+
if (!a->records) {
43+
logmsg(LOGMSG_FATAL, "%s: realloc failed\n", __func__);
44+
abort();
45+
}
46+
}
47+
u = &a->records[a->count - 1];
48+
u->filename = strdup(filename);
49+
if (!u->filename) {
50+
logmsg(LOGMSG_FATAL, "%s: strdup failed\n", __func__);
51+
abort();
52+
}
53+
u->reflog = reflog;
54+
return 0;
55+
}
56+
57+
static int get_unused_files(void **data, int *records)
58+
{
59+
getunusedfiles_t a = {0};
60+
oldfile_hash_collect(collect, &a);
61+
*data = a.records;
62+
*records = a.count;
63+
return 0;
64+
}
65+
66+
static void free_unused_files(void *p, int n)
67+
{
68+
systable_unused_files_t *a, *begin = p;
69+
systable_unused_files_t *end = begin + n;
70+
for (a = begin; a < end; ++a) {
71+
free(a->filename);
72+
}
73+
free(p);
74+
}
75+
76+
sqlite3_module systblUnusedFilesModule = {
77+
.access_flag = CDB2_ALLOW_USER
78+
};
79+
80+
int systblUnusedFilesInit(sqlite3 *db) {
81+
return create_system_table(db, "comdb2_unused_files", &systblUnusedFilesModule,
82+
get_unused_files, free_unused_files, sizeof(systable_unused_files_t),
83+
CDB2_CSTRING, "filename", -1, offsetof(systable_unused_files_t, filename),
84+
CDB2_INTEGER, "reflog", -1, offsetof(systable_unused_files_t, reflog),
85+
SYSTABLE_END_OF_FIELDS);
86+
}

tests/auth.test/t09.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@
293293
(candidate='comdb2_triggers')
294294
(candidate='comdb2_tunables')
295295
(candidate='comdb2_type_samples')
296+
(candidate='comdb2_unused_files')
296297
(candidate='comdb2_users')
297298
(candidate='comdb2_views')
298299
(candidate='abs()')

tests/cdb2sql.test/t00.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ unknown @ls sub-command foo
8585
(name='comdb2_triggers')
8686
(name='comdb2_tunables')
8787
(name='comdb2_type_samples')
88+
(name='comdb2_unused_files')
8889
(name='comdb2_users')
8990
(name='comdb2_views')
9091
table name required
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
ifeq ($(TESTSROOTDIR),)
2+
include ../testcase.mk
3+
else
4+
include $(TESTSROOTDIR)/testcase.mk
5+
endif
6+
7+
ifeq ($(TEST_TIMEOUT),)
8+
export TEST_TIMEOUT=5m
9+
endif
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Simple test to verify that comdb2_unused_files is working

0 commit comments

Comments
 (0)