Skip to content

Commit

Permalink
Start work on sparse mid2l arrays for use with dirty lists and as an …
Browse files Browse the repository at this point in the history
…index for free-space-by-block-size
  • Loading branch information
kriszyp committed Sep 4, 2024
1 parent 5d9035b commit 6ff4998
Showing 1 changed file with 30 additions and 19 deletions.
49 changes: 30 additions & 19 deletions dependencies/lmdb/libraries/liblmdb/midl.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ unsigned mdb_midl_search( MDB_IDL ids, MDB_ID id )
return cursor;
}

int mdb_midl_insert( MDB_IDL* ids_ref, MDB_ID id, int insertion_count )
int mdb_midl_insert( MDB_IDL* ids_ref, MDB_ID id, int insertion_count, MDB_ID2L* size_index )
{
MDB_IDL ids = *ids_ref;
unsigned x, i;
Expand Down Expand Up @@ -148,15 +148,21 @@ int mdb_midl_insert( MDB_IDL* ids_ref, MDB_ID id, int insertion_count )
if (next_count == 1) {
// we can safely add the new count to the empty space
ids[x - 1] = -count; // update the count
if (size_index) mdb_mid2l_insert(size_index, &(MDB_ID2){.mid = count, .mptr = id});
return 0;
}
}
}
if (next_count > 1) {
if (size_index) mdb_mid2l_insert(size_index, &(MDB_ID2){.mid = count, .mptr = id});
ids[x - 1] = -count; // update the count
} else if (ids[x - 1] == 0) {
count = 1 - insertion_count;
if (size_index) mdb_mid2l_insert(size_index, &(MDB_ID2){.mid = count, .mptr = id});
ids[x - 1] = -1 - insertion_count; // we can switch to length-2 block in place
} else {
count = 1 - insertion_count;
if (size_index) mdb_mid2l_insert(size_index, &(MDB_ID2){.mid = count, .mptr = id});
id = -1 - insertion_count; // switching a single entry to a block size of 2
goto insert_id;
}
Expand Down Expand Up @@ -526,31 +532,40 @@ unsigned mdb_mid2l_search( MDB_ID2L ids, MDB_ID id )
unsigned cursor = 1;
int val = 0;
unsigned n = (unsigned)ids[0].mid;

unsigned end = n;
binary_search:
while( 0 < n ) {
unsigned pivot = n >> 1;
cursor = base + pivot + 1;
val = CMP( id, ids[cursor].mid );
unsigned x = cursor;
// skip past empty and block length entries
while(((intptr_t)ids[x].mid) <= 0) {
if (++x > end) { // reached the end, go to lower half
n = pivot;
val = 0;
end = cursor;
goto binary_search;
}
}
val = CMP( id, ids[x].mid );

if( val < 0 ) {
n = pivot;

end = cursor;
} else if ( val > 0 ) {
base = cursor;
n -= pivot + 1;

} else {
return cursor;
}
}

if( val > 0 ) {
++cursor;
}
if( val > 0 && (intptr_t)ids[cursor].mid > 0) ++cursor;
return cursor;
}

int mdb_mid2l_insert( MDB_ID2L ids, MDB_ID2 *id )
int mdb_mid2l_insert( MDB_ID2L ids, MDB_ID2 *id, bool allow_duplicates )
{
unsigned x, i;

Expand All @@ -563,20 +578,16 @@ int mdb_mid2l_insert( MDB_ID2L ids, MDB_ID2 *id )

if ( x <= ids[0].mid && ids[x].mid == id->mid ) {
/* duplicate */
return -1;
}

if ( ids[0].mid >= MDB_IDL_UM_MAX ) {
if (!allow_duplicates) return -1;
} else if (ids[0].mid >= MDB_IDL_UM_MAX) {
/* too big */
return -2;

} else {
/* insert id */
ids[0].mid++;
for (i=(unsigned)ids[0].mid; i>x; i--)
ids[i] = ids[i-1];
ids[x] = *id;
}
/* insert id */
ids[0].mid++;
for (i=(unsigned)ids[0].mid; i>x; i--)
ids[i] = ids[i-1];
ids[x] = *id;

return 0;
}
Expand Down

0 comments on commit 6ff4998

Please sign in to comment.