odfmt: use 4KiB as addressing unit
This commit is contained in:
parent
7277868170
commit
d09f3cf642
30
lib/db.c
30
lib/db.c
|
@ -13,6 +13,12 @@ typedef struct {
|
|||
int fd;
|
||||
} y16t_db_actx_t;
|
||||
|
||||
__attribute__((pure)) static off_t
|
||||
y16t_db_offstr(const uint32_t offset) { return ((off_t)offset) << 12; }
|
||||
|
||||
__attribute__((pure)) static uint32_t
|
||||
y16t_db_unoffstr(const off_t x) { return x >> 12; }
|
||||
|
||||
__attribute__((nonnull))
|
||||
static uint32_t y16t_db_palloc(void * context_)
|
||||
{
|
||||
|
@ -21,11 +27,9 @@ static uint32_t y16t_db_palloc(void * context_)
|
|||
const off_t eoffs_orig = lseek(fd, 0, SEEK_END);
|
||||
if(eoffs_orig == -1) return 0;
|
||||
const off_t eoffs = (eoffs_orig >= (1 << 18)) ? eoffs_orig : (1 << 18);
|
||||
int tmp = posix_fallocate(fd, eoffs, 1 << 18);
|
||||
if(tmp < 0) {
|
||||
errno = tmp;
|
||||
return 0;
|
||||
} else return eoffs >> 10;
|
||||
return (-1 == ftruncate(fd, eoffs + (1 << 18)))
|
||||
? 0
|
||||
: y16t_db_unoffstr(eoffs);
|
||||
}
|
||||
|
||||
__attribute__((nonnull))
|
||||
|
@ -35,11 +39,9 @@ static uint32_t y16t_db_palloc2(void * context_)
|
|||
const int fd = context->fd;
|
||||
const off_t eoffs = lseek(fd, 0, SEEK_END);
|
||||
if(eoffs == -1) return 0;
|
||||
int tmp = posix_fallocate(fd, eoffs, 1 << 12);
|
||||
if(tmp < 0) {
|
||||
errno = tmp;
|
||||
return 0;
|
||||
} else return eoffs >> 10;
|
||||
return (-1 == ftruncate(fd, eoffs + y16t_db_offstr(1)))
|
||||
? 0
|
||||
: y16t_db_unoffstr(eoffs);
|
||||
}
|
||||
|
||||
// @param bufexp must point to 44 valid bytes
|
||||
|
@ -107,7 +109,7 @@ int y16t_db_insert(
|
|||
memcpy(bufexp + 28, &z, 16);
|
||||
|
||||
// sequential data, read complete page
|
||||
const off_t start_offset = ((off_t)offset) << 10;
|
||||
const off_t start_offset = y16t_db_offstr(offset);
|
||||
uint32_t i;
|
||||
tmp = y16t_db_intern_pgsearch(db, start_offset, bufexp, &i);
|
||||
switch(tmp) {
|
||||
|
@ -153,7 +155,7 @@ int y16t_db_lookup(
|
|||
|
||||
// sequential data, read complete page
|
||||
uint32_t i;
|
||||
return y16t_db_intern_pgsearch(db, ((off_t)offset) << 10, bufexp, &i);
|
||||
return y16t_db_intern_pgsearch(db, y16t_db_offstr(offset), bufexp, &i);
|
||||
}
|
||||
|
||||
// iterate over the database
|
||||
|
@ -172,7 +174,7 @@ static int y16t_db_foreach_x3(uint32_t offset, void *context_)
|
|||
// sequential data, read complete page
|
||||
uint8_t buf[4096] = {0};
|
||||
const uint8_t zeros[44] = {0};
|
||||
const off_t start_offset = ((off_t)offset) << 10;
|
||||
const off_t start_offset = y16t_db_offstr(offset);
|
||||
ssize_t tmp = y16t_pread_all(context->db, buf, sizeof(buf), start_offset);
|
||||
if(tmp < 0) return (int)tmp;
|
||||
|
||||
|
@ -197,7 +199,7 @@ static int y16t_db_foreach_y3(uint32_t offset, void *context_)
|
|||
// sequential data, read complete page
|
||||
uint8_t buf[4096] = {0};
|
||||
const uint8_t zeros[44] = {0};
|
||||
const off_t start_offset = ((off_t)offset) << 10;
|
||||
const off_t start_offset = y16t_db_offstr(offset);
|
||||
ssize_t tmp = y16t_pread_all(context->db, buf, sizeof(buf), start_offset);
|
||||
if(tmp < 0) return (int)tmp;
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
static __attribute__((pure))
|
||||
uint32_t y16t_table_at(const y16t_idx_t idx, const uint8_t x, const uint8_t y)
|
||||
{
|
||||
return (idx.offset << 10)
|
||||
return (idx.offset << 12)
|
||||
| (y16t_hilbert_xy2d(1 << 16, x, y) << 2);
|
||||
}
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
|
||||
typedef struct {
|
||||
int fd;
|
||||
// offset is in 1KiB units
|
||||
// offset is in 4KiB units
|
||||
size_t offset;
|
||||
} y16t_idx_t;
|
||||
|
||||
|
|
Loading…
Reference in a new issue