Skip to content

Commit

Permalink
Addded binary seek for const sized indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
drolbr committed Oct 11, 2022
1 parent e8632d7 commit d311e7b
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 46 deletions.
19 changes: 5 additions & 14 deletions src/overpass_api/core/basic_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,10 @@ struct Uint32_Index
bool leq(void* rhs) const { return value <= *(uint32*)rhs; }
bool equal(void* rhs) const { return value == *(uint32*)rhs; }

uint32 size_of() const
{
return 4;
}

static uint32 max_size_of()
{
return 4;
}

static uint32 size_of(void* data)
{
return 4;
}
uint32 size_of() const { return 4; }
static constexpr uint32 const_size() { return 4; }
static uint32 max_size_of() { return 4; }
static uint32 size_of(void* data) { return 4; }

void to_data(void* data) const
{
Expand Down Expand Up @@ -187,6 +177,7 @@ struct Uint64
bool equal(void* rhs) const { return value == *(uint32*)rhs; }

uint32 size_of() const { return 8; }
static constexpr uint32 const_size() { return 8; }
static uint32 max_size_of() { return 8; }
static uint32 size_of(void* data) { return 8; }

Expand Down
12 changes: 3 additions & 9 deletions src/overpass_api/core/datatypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -693,15 +693,9 @@ struct Timestamp
bool equal(void* rhs) const
{ return (timestamp>>32) == *(((uint8*)rhs) + 4) && (timestamp & 0xffffffffull) == *(uint32*)rhs; }

uint32 size_of() const
{
return 5;
}

static uint32 size_of(void* data)
{
return 5;
}
uint32 size_of() const { return 5; }
static constexpr uint32 const_size() { return 5; }
static uint32 size_of(void* data) { return 5; }

void to_data(void* data) const
{
Expand Down
4 changes: 4 additions & 0 deletions src/overpass_api/core/type_tags.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ struct Tag_Index_Local
return 7 + key.length() + value.length();
}

static constexpr uint32 const_size() { return 0; }

static uint32 size_of(void* data)
{
return (*((uint16*)data) + *((uint16*)data + 1) + 7);
Expand Down Expand Up @@ -303,6 +305,8 @@ struct Tag_Index_Global
return 4 + key.length() + value.length();
}

static constexpr uint32 const_size() { return 0; }

static uint32 size_of(void* data)
{
return (*((uint16*)data) + *((uint16*)data + 1) + 4);
Expand Down
12 changes: 3 additions & 9 deletions src/template_db/block_backend.test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,9 @@ struct IntIndex
bool leq(void* rhs) const { return value <= *(uint32*)rhs; }
bool equal(void* rhs) const { return value == *(uint32*)rhs; }

uint32 size_of() const
{
return 4;
}

static uint32 size_of(void* data)
{
return 4;
}
uint32 size_of() const { return 4; }
static constexpr uint32 const_size() { return 4; }
static uint32 size_of(void* data) { return 4; }

void to_data(void* data) const
{
Expand Down
18 changes: 4 additions & 14 deletions src/template_db/dispatcher.test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,10 @@ struct IntIndex
bool leq(void* rhs) const { return value <= *(uint32*)rhs; }
bool equal(void* rhs) const { return value == *(uint32*)rhs; }

uint32 size_of() const
{
return 4;
}

static uint32 size_of(void* data)
{
return 4;
}

static uint32 max_size_of()
{
return 4;
}
uint32 size_of() const { return 4; }
static constexpr uint32 const_size() { return 4; }
static uint32 size_of(void* data) { return 4; }
static uint32 max_size_of() { return 4; }

void to_data(void* data) const
{
Expand Down
1 change: 1 addition & 0 deletions src/template_db/file_blocks.test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ struct IntIndex
bool equal(void* rhs) const { return value == *(uint32*)rhs; }

uint32 size_of() const { return (value < 24 ? 12 : value-12); }
static constexpr uint32 const_size() { return 0; }
static uint32 size_of(void* data) { return ((*(uint32*)data) < 24 ? 12 : (*(uint32*)data)-12); }

void to_data(void* data) const
Expand Down
28 changes: 28 additions & 0 deletions src/template_db/file_blocks_index.h
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,39 @@ inline uint64 file_size_of(const std::string& data_file_name)
}


template< typename Index >
const uint8* binary_seek(const uint8* begin, const uint8* end, const Index& target)
{
auto min = begin;
auto max = end;
while (max - min > 2*(Index::const_size() + 12))
{
auto middle = min + ((max - min)/(Index::const_size() + 12)/2*(Index::const_size() + 12));
if (target.less((void*)(middle+12)))
max = middle;
else
min = middle;
}
return min;
}


template< typename Index >
void File_Blocks_Index_Iterator< Index >::seek(const Index& target)
{
if (ptr == end || target.less((void*)(ptr+12)))
return;
if (Index::const_size() && ((end - ptr) > 32*(Index::const_size() + 12))
&& !target.less((void*)(ptr + 32*(Index::const_size() + 12) + 12)))
{
decltype(ptr) next = binary_seek(ptr, end, target);
if (next != end)
{
while (ptr < next && target.equal((void*)(next+12)))
next -= (Index::const_size() + 12);
}
ptr = next;
}
while (!target.equal((void*)(ptr+12)))
{
decltype(ptr) next = ptr+12;
Expand Down

0 comments on commit d311e7b

Please sign in to comment.