Skip to content

Commit

Permalink
Component categories and table statistics.
Browse files Browse the repository at this point in the history
  • Loading branch information
trieck committed Aug 15, 2021
1 parent 569d319 commit 2273906
Show file tree
Hide file tree
Showing 16 changed files with 311 additions and 35 deletions.
15 changes: 15 additions & 0 deletions cocats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,18 @@ void CoCategories::addClass(LPCWSTR catID, LPCWSTR clsID)
auto& it = m_catClsids[catID];
it.insert(clsID);
}

void CoCategories::cats(const std::function<void(const std::wstring& catID,
const std::unordered_set<std::wstring>& clsIDs)>& fn)
{
for (const auto& catid : m_catids) {
wstring_set clsIDs;

auto clsIt = m_catClsids.find(catid);
if (clsIt != m_catClsids.end()) {
clsIDs = clsIt->second;
}

fn(catid, clsIDs);
}
}
3 changes: 2 additions & 1 deletion cocats.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ class CoCategories
bool Construct();

void addClass(LPCWSTR catID, LPCWSTR clsID);
void cats(const std::function<void(const std::wstring& catID,
const std::unordered_set<std::wstring>& clsIDs)>& fn);

private:
using wstring_set = std::unordered_set<std::wstring>;

wstring_set m_catids;
std::unordered_map<std::wstring, wstring_set> m_catClsids;
};

30 changes: 30 additions & 0 deletions coobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,33 @@ coapp::coapp(const std::wstring& appID, const wstring_set& clsIDs)
auto root = builder.Finish();
FinishCoObjectBuffer(m_builder, root);
}

cocat::cocat(const std::wstring& catID, const wstring_set& clsIDs)
{
auto sCatID = utf8str(catID);
auto guidOffset = m_builder.CreateString(static_cast<LPCSTR>(sCatID));

Offset<Vector<Offset<String>>> clsidsOffset;
if (!clsIDs.empty()) {
std::vector<std::string> vClsIDs;
for (const auto& clsID : clsIDs) {
vClsIDs.emplace_back(utf8str(clsID));
}

clsidsOffset = m_builder.CreateVectorOfStrings(vClsIDs);
}

CoCategoryBuilder cbuilder(m_builder);
if (!clsidsOffset.IsNull()) {
cbuilder.add_cls_ids(clsidsOffset);
}
auto cocat = cbuilder.Finish().Union();

CoObjectBuilder builder(m_builder);
builder.add_type_type(CoType::CoCategory);
builder.add_type(cocat);
builder.add_guid(guidOffset);

auto root = builder.Finish();
FinishCoObjectBuffer(m_builder, root);
}
8 changes: 8 additions & 0 deletions coobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,11 @@ class coapp : public coobject
coapp() = delete;
coapp(const std::wstring& appID, const wstring_set& clsIDs = {});
};

/////////////////////////////////////////////////////////////////////////////
class cocat: public coobject
{
public:
cocat() = delete;
cocat(const std::wstring& catID, const wstring_set& clsIDs = {});
};
6 changes: 6 additions & 0 deletions coobjects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,9 @@ void CoObjects::apps(const std::function<void(const std::wstring& appID,
{
m_apps.apps(fn);
}

void CoObjects::cats(const std::function<void(const std::wstring& catID,
const wstring_set& clsIDs)>& fn)
{
m_cats.cats(fn);
}
4 changes: 4 additions & 0 deletions coobjects.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class CoObjects
const std::wstring& appID,
const wstring_set& clsIDs)>& fn);

void cats(const std::function<void(
const std::wstring& catID,
const wstring_set& clsIDs)>& fn);

private:
wstring_set m_clsids;
std::unordered_map<std::wstring, std::wstring> m_clsidApps;
Expand Down
14 changes: 14 additions & 0 deletions kvstorelib/coinit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

struct CoInit
{
CoInit()
{
CoInitialize(nullptr);
}

~CoInit()
{
CoUninitialize();
}
};
75 changes: 65 additions & 10 deletions kvstorelib/kvstore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,31 @@ void kvstore::resize()
}
}

uint64_t kvstore::runLength(void* pvpage, const digest_type& digest)
{
auto h = hash(digest);
auto pageno = h / BUCKETS_PER_PAGE;
auto bucket = h % BUCKETS_PER_PAGE;

auto ppage = static_cast<LPPAGE>(pvpage);

m_index.readblock(pageno, ppage);
if (IS_EMPTY(ppage, bucket)) {
return 0;
}

auto run = 1ULL;
for (auto i = 0ULL; i < m_tablesize; ++i, ++run) {
if (IS_EMPTY(ppage, bucket)) {
break;
}

nextbucket(pvpage, i, bucket, pageno);
}

return run;
}

bool kvstore::insert(LPCSTR key, const IValue& value)
{
uint32_t digest[SHA1_DIGEST_INTS];
Expand Down Expand Up @@ -374,32 +399,62 @@ void kvstore::unlink()

uint64_t kvstore::indexsize()
{
// TODO:
return 0;
return m_index.fileSize();
}

uint64_t kvstore::tablesize() const
{
// TODO:
return 0;
return m_tablesize;
}

uint64_t kvstore::fillcount() const
{
// TODO:
return 0;
return m_fillcount;
}

float kvstore::loadfactor() const
{
// TODO:
return 0;
if (m_tablesize == 0) {
return 0;
}

return 100 * (static_cast<float>(m_fillcount) / static_cast<float>(m_tablesize));
}

std::wstring kvstore::indexname() const
{
return m_idxfile;
}

uint64_t kvstore::maxrun()
{
// TODO:
return 0;
uint64_t maxrun = 0, bucket = 0, pageno = 0;
uint32_t digest[SHA1_DIGEST_INTS];

BlockIO::Block block{};

auto ppage = reinterpret_cast<LPPAGE>(m_page.data());
auto ppage2 = reinterpret_cast<LPPAGE>(block.data());

m_index.readblock(pageno, ppage);

for (; ;) {
if (IS_FILLED(ppage, bucket)) {
getDigest(ppage, bucket, digest);
maxrun = std::max(maxrun, runLength(ppage2, digest));
}

if ((bucket = ((bucket + 1) % BUCKETS_PER_PAGE)) == 0) {
// next page
if ((pageno = ((pageno + 1) % m_nbpages)) == 0) {
break; // wrapped
}

m_index.readblock(pageno, ppage);
}
}

return maxrun;
}

void kvstore::mktable(bool create)
Expand Down
6 changes: 4 additions & 2 deletions kvstorelib/kvstore.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ class kvstore
void open(LPCWSTR idxfile, uint32_t entries = DEFAULT_ENTRIES);
void unlink();

uint64_t indexsize();
uint64_t tablesize() const;
uint64_t maxrun();
uint64_t indexsize();
uint64_t fillcount() const;
std::wstring indexname() const;
float loadfactor() const;
uint64_t maxrun();

static constexpr auto DEFAULT_ENTRIES = 10000UL;

Expand All @@ -49,6 +50,7 @@ class kvstore
void nextbucket(uint64_t i, uint64_t& bucket, uint64_t& pageno);
void nextbucket(void* pvpage, uint64_t i, uint64_t& bucket, uint64_t& pageno);
void resize();
uint64_t runLength(void* pvpage, const digest_type& digest);
void setKey(uint64_t bucket, const digest_type& digest);
void setKey(uint64_t bucket, LPCSTR key);

Expand Down
5 changes: 5 additions & 0 deletions kvstorelib/kvstorelib.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="blockio.h" />
<ClInclude Include="coinit.h" />
<ClInclude Include="fnvhash64.h" />
<ClInclude Include="kvstore.h" />
<ClInclude Include="kvstorelib.h" />
<ClInclude Include="primes.h" />
<ClInclude Include="randperm.h" />
<ClInclude Include="repo.h" />
<ClInclude Include="sha1.h" />
<ClInclude Include="timer.h" />
<ClInclude Include="utf8str.h" />
<ClInclude Include="util.h" />
<ClInclude Include="value.h" />
<ClInclude Include="wexcept.h" />
</ItemGroup>
Expand All @@ -39,10 +42,12 @@
<ClCompile Include="randperm.cpp" />
<ClCompile Include="repo.cpp" />
<ClCompile Include="sha1.cpp" />
<ClCompile Include="timer.cpp" />
<ClCompile Include="utf8str.cpp">
<PrecompiledHeaderFile Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">kvstorelib.h</PrecompiledHeaderFile>
<PrecompiledHeaderFile Condition="'$(Configuration)|$(Platform)'=='Release|x64'">kvstorelib.h</PrecompiledHeaderFile>
</ClCompile>
<ClCompile Include="util.cpp" />
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
Expand Down
15 changes: 15 additions & 0 deletions kvstorelib/kvstorelib.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@
<ClInclude Include="value.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="timer.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="util.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="coinit.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="kvstore.cpp">
Expand All @@ -74,5 +83,11 @@
<ClCompile Include="sha1.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="timer.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="util.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>
36 changes: 36 additions & 0 deletions kvstorelib/timer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "kvstorelib.h"
#include "timer.h"

Timer::Timer()
{
start_ = Clock::now();
}

std::string Timer::str() const
{
using std::chrono::duration_cast;
auto now = Clock::now();

auto elapsed = duration_cast<std::chrono::milliseconds>(now - start_).count();

auto hours = (elapsed / 1000) / 3600;
auto minutes = ((elapsed / 1000) % 3600) / 60;
auto seconds = (elapsed / 1000) % 60;
auto millis = elapsed % 1000;

boost::format fmt;

if (hours)
fmt = boost::format("%d:%02d:%02d hours") % hours % minutes % seconds;
else if (minutes)
fmt = boost::format("%d:%02d minutes") % minutes % seconds;
else
fmt = boost::format("%d.%03d seconds") % seconds % millis;

return fmt.str();
}

void Timer::restart()
{
start_ = Clock::now();
}
23 changes: 23 additions & 0 deletions kvstorelib/timer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include <chrono>

class Timer
{
public:
Timer();
~Timer() = default;

void restart();
std::string str() const;

private:
using Clock = std::chrono::high_resolution_clock;
using TimePoint = std::chrono::time_point<std::chrono::high_resolution_clock>;
TimePoint start_;
};

inline std::ostream& operator<<(std::ostream& s, const Timer& timer)
{
return s << timer.str();
}
29 changes: 29 additions & 0 deletions kvstorelib/util.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "kvstorelib.h"
#include "util.h"

namespace utility {

std::string comma(uint64_t i)
{
std::ostringstream os;

std::ostringstream ss;
ss << i;
auto input = ss.str();

auto n = static_cast<int>(input.length());

for (auto j = n - 1, k = 1; j >= 0; j--, k++) {
os << input[j];
if (k % 3 == 0 && j > 0 && j < n - 1) {
os << ',';
}
}

auto output(os.str());
reverse(output.begin(), output.end());

return output;
}

} // utility
Loading

0 comments on commit 2273906

Please sign in to comment.