BRL-CAD
Loading...
Searching...
No Matches
cache.h
Go to the documentation of this file.
1/* C A C H E . H
2 * BRL-CAD
3 *
4 * Copyright (c) 2018-2025 United States Government as represented by
5 * the U.S. Army Research Laboratory.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public License
9 * version 2.1 as published by the Free Software Foundation.
10 *
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this file; see the file named COPYING for more
18 * information.
19 */
20/** @file cache.h
21 *
22 * Routines for managing and accessing a key/value data store.
23 *
24 * NOTE: Data backend storage is NOT guaranteed by this API from one BRL-CAD
25 * version to the next. Older versions of libbu caches may use different
26 * internal storage than newer ones. Should a backend need to change, we will
27 * strive to maintain support for reading in the previous backend when the new
28 * one is released to allow for data migration. However, cache data is
29 * ultimately intended to be ephemeral and subject to regeneration - if the
30 * maintenance burden of backwards compatibilty becomes too high the decision
31 * will be made to drop support for reading older caches.
32 *
33 * For data that is canonical and not able to be regenerated from other sources
34 * (such as geometry or user settings) bu_cache is not an appropriate storage
35 * mechanism.
36 */
37
38#ifndef BU_CACHE_H
39#define BU_CACHE_H 1
40
41#include "common.h"
42
43#include "bu/defines.h"
44
46
47
48// Default maximum cache database size.
49//
50// Generally user code will want to use this, unless they want something larger
51// - a cache that gets filled up will typically cause problems for users.
52#define BU_CACHE_DEFAULT_DB_SIZE 4294967296
53
54// Due to the backend implementation, cache key strings have a maximum limit.
55// Set this to that max - we want to use it for string buffer allocations and
56// snprintf. NOTE: BRL-CAD compiled LMDB instances (which are what will
57// pretty much always be used) will match this default, but if a system LMDB
58// with a smaller max length is forced in it could be a problem.
59//
60// Also, BRL-CAD's API requires that keys be null terminated strings with no
61// embedded nulls. (Among other reasons, printing out a list of all keys
62// would get gnarly if we allowed something like that to be a valid key.)
63#define BU_CACHE_KEY_MAXLEN 511
64
65
66struct bu_cache_impl;
67struct bu_cache_txn;
68
69struct bu_cache {
71};
72
73/* Provide a struct data type that functions can use to return everything
74 * needed to execute a deferred bu_cache_write operation. */
77 void *data;
78 size_t data_len;
79};
80
81/**
82 * Opens the specified cache database from the BRL-CAD BU_DIR_CACHE folder.
83 * The cache_db string may contain a hierarchical path, but note that all
84 * paths will be interpreted as relative to the BU_DIR_CACHE location - no
85 * absolute paths can be specified.
86 *
87 * If the create flag is non-zero, bu_cache_open will create the specified
88 * cache_db if it does not already exist.
89 *
90 * A zero argument to max_cache_size will result in BU_CACHE_DEFAULT_DB_SIZE
91 * being used. The size limit is a limit for the current session, not a
92 * perminent upper limit on how large a given cache can grow.
93 *
94 * returns the bu_cache structure on success, NULL on failure.
95 */
96BU_EXPORT extern struct bu_cache *bu_cache_open(const char *cache_db, int create, size_t max_cache_size);
97
98/**
99 * Closes the bu_cache and frees all associated memory. Will NOT close
100 * if bu_cache_write still has an active txn - in that case, calling
101 * code needs to call bu_cache_write_commit or bu_cache_write_abort before
102 * bu_cache_close will succeed. returns BRLCAD_ERROR if close was not
103 * successful, else BRLCAD_OK.
104 */
105BU_EXPORT extern int bu_cache_close(struct bu_cache *c);
106
107/**
108 * Erase the specified cache from disk. Any open instances of the
109 * cache should be closed before calling this function.
110 */
112
113/**
114 * Retrieve data (read-only) from the cache using the specified key. User
115 * should call bu_cache_get_done once their use of data is complete.
116 *
117 * Returns the size of the retrieved data. If t is non-NULL and empty, a
118 * transaction token is returned that can be reused in subsequent calls.
119 * If it is non-NULL and non-empty, bu_cache_get treats *t as the current
120 * transaction. Note that when reusing a txn, the caller MUST call
121 * bu_cache_get_done to finalize a transaction.
122 *
123 * If t is NULL, the data returned in data must be freed by the caller.
124 */
125BU_EXPORT size_t bu_cache_get(void **data, const char *key, struct bu_cache *c, struct bu_cache_txn **t);
126
127/**
128 * Data retrieved using bu_cache_get is temporary if we are using transaction
129 * tokens - once the user is done either reading it or copying it, libbu needs
130 * to be told that the usage of the returned data is complete. */
132
133/**
134 * Assign data to the cache using the specified key.
135 *
136 * If t is NULL, the write is handled immediately as a complete operation, and
137 * bu_cache_write immediately validates that the written data can be
138 * successfully read.
139 *
140 * If t is non-NULL, the txn is not finalized and the calling code can queue up
141 * more write operations for eventual commit. This mode is MUCH more
142 * performant when lots of rapid writes are needed, but no per-write read-back
143 * validation is performed (since the write is not yet finalized.) This mode
144 * requires explicit management of when to finalize the operation for the
145 * database, and if the user wants to validate that reads successfully return
146 * the expected data they will need to do it themselves, but the speedup
147 * will almost always be worth it for any non-trivial data input. If an individual
148 * write DOES report failure (return size 0) in this mode, parent code should
149 * not try to continue writing - they should call bu_cache_write_abort
150 * immediately and handle the failure case, since the commit operation will
151 * also ultimately fail.
152 *
153 * A cache may only have one active write txn at a time - this is enforced
154 * internally in the implementation, and a bu_cache_write will fail if it
155 * attempts to create a new write when one is already active.
156 */
157BU_EXPORT size_t bu_cache_write(void *data, size_t dsize, const char *key, struct bu_cache *c, struct bu_cache_txn **t);
158
159/**
160 * Like bu_cache_get_done for data retrieval, a series of write operations may
161 * be either committed or (if the parent code has changed its mind) aborted.
162 * t is the bu_cache_txn returned by bu_cache write's t parameter.
163 *
164 * returns BRLCAD_OK on success and BRLCAD_ERROR on error.
165 */
167
168/**
169 * If the calling code decides not to commit a series of bu_cache_write calls staged
170 * with a non-NULL t param to a bu_cache_write, call bu_cache_write_abort to clear
171 * the decks. */
173
174/**
175 * Clear data associated with the specified key from the cache. Because this is a
176 * write operation as far as the database is concerned, it is treated the same way
177 * bu_cache_write operations are as far as txn behavior is concerned. If you have
178 * a batch bu_cache_write in progress and want to do a clear, the write txn should
179 * be provided to bu_cache_clear as well, and like other writes it will not be
180 * finalized until commit is called.
181 */
182BU_EXPORT void bu_cache_clear(const char *key, struct bu_cache *c, struct bu_cache_txn **t);
183
184/**
185 * Get an array of keys present in the cache. Caller is responsible
186 * for freeing the keysv output (recommend using bu_argv_free).
187 */
188BU_EXPORT int bu_cache_keys(char ***keysv, struct bu_cache *c);
189
190
192
193#endif /* BU_CACHE_H */
194
195/*
196 * Local Variables:
197 * tab-width: 8
198 * mode: C
199 * indent-tabs-mode: t
200 * c-file-style: "stroustrup"
201 * End:
202 * ex: shiftwidth=4 tabstop=8
203 */
void bu_cache_write_abort(struct bu_cache_txn **t)
int bu_cache_close(struct bu_cache *c)
void bu_cache_clear(const char *key, struct bu_cache *c, struct bu_cache_txn **t)
size_t bu_cache_write(void *data, size_t dsize, const char *key, struct bu_cache *c, struct bu_cache_txn **t)
struct bu_cache * bu_cache_open(const char *cache_db, int create, size_t max_cache_size)
int bu_cache_keys(char ***keysv, struct bu_cache *c)
#define BU_CACHE_KEY_MAXLEN
Definition cache.h:63
void bu_cache_erase(const char *cache_db)
void bu_cache_get_done(struct bu_cache_txn **t)
int bu_cache_write_commit(struct bu_cache *c, struct bu_cache_txn **t)
size_t bu_cache_get(void **data, const char *key, struct bu_cache *c, struct bu_cache_txn **t)
Definition dvec.h:74
Header file for the BRL-CAD common definitions.
void int * c
Definition tig.h:139
char key[BU_CACHE_KEY_MAXLEN]
Definition cache.h:76
void * data
Definition cache.h:77
size_t data_len
Definition cache.h:78
struct bu_cache_impl * i
Definition cache.h:70