Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various improvements #52

Merged
merged 2 commits into from
Feb 12, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 13 additions & 46 deletions src/tightdb/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

#include <tightdb/array.hpp>
#include <tightdb/column.hpp>
#include <tightdb/utilities.hpp>
#include <tightdb/query_conditions.hpp>
#include <tightdb/column_string.hpp>

Expand Down Expand Up @@ -164,8 +163,11 @@ void Array::SetType(ColumnDef type)
if (m_ref) CopyOnWrite(); // Throws

bool is_node = false, has_refs = false;
if (type == coldef_Node) is_node = has_refs = true;
else if (type == coldef_HasRefs) has_refs = true;
switch (type) {
case coldef_Normal: break;
case coldef_InnerNode: has_refs = is_node = true; break;
case coldef_HasRefs: has_refs = true; break;
}
m_isNode = is_node;
m_hasRefs = has_refs;

Expand Down Expand Up @@ -344,43 +346,6 @@ void Array::Delete(size_t ndx)
set_header_len(m_len);
}

int64_t Array::Get(size_t ndx) const TIGHTDB_NOEXCEPT
{
TIGHTDB_ASSERT(ndx < m_len);
return (this->*m_getter)(ndx);

// Two ideas that are not efficient but may be worth looking into again:
/*
// Assume correct width is found early in TIGHTDB_TEMPEX, which is the case for B tree offsets that
// are probably either 2^16 long. Turns out to be 25% faster if found immediately, but 50-300% slower
// if found later
TIGHTDB_TEMPEX(return Get, (ndx));
*/
/*
// Slightly slower in both of the if-cases. Also needs an matchcount m_len check too, to avoid
// reading beyond array.
if (m_width >= 8 && m_len > ndx + 7)
return Get<64>(ndx >> m_shift) & m_widthmask;
else
return (this->*m_getter)(ndx);
*/
}

size_t Array::GetAsRef(size_t ndx) const TIGHTDB_NOEXCEPT
{
TIGHTDB_ASSERT(ndx < m_len);
TIGHTDB_ASSERT(m_hasRefs);
const int64_t v = Get(ndx);
return to_ref(v);
}

size_t Array::GetAsSizeT(size_t ndx) const TIGHTDB_NOEXCEPT
{
TIGHTDB_ASSERT(ndx < m_len);
const int64_t v = Get(ndx);
return to_size_t(v);
}

void Array::Set(size_t ndx, int64_t value)
{
TIGHTDB_ASSERT(ndx < m_len);
Expand Down Expand Up @@ -1340,10 +1305,11 @@ void Array::CopyOnWrite()
size_t Array::create_empty_array(ColumnDef type, WidthType width_type, Allocator& alloc)
{
bool is_node = false, has_refs = false;
if (type == coldef_Node)
is_node = has_refs = true;
else if (type == coldef_HasRefs)
has_refs = true;
switch (type) {
case coldef_Normal: break;
case coldef_InnerNode: has_refs = is_node = true; break;
case coldef_HasRefs: has_refs = true; break;
}

const size_t capacity = initial_capacity;
const MemRef mem_ref = alloc.Alloc(capacity); // Throws
Expand Down Expand Up @@ -1778,7 +1744,7 @@ template<size_t w> void Array::QuickSort(size_t lo, size_t hi)
if (i < (int)hi) QuickSort(i, hi);
}

std::vector<int64_t> Array::ToVector(void) const
std::vector<int64_t> Array::ToVector() const
{
std::vector<int64_t> v;
const size_t count = size();
Expand Down Expand Up @@ -2247,7 +2213,7 @@ int64_t Array::ColumnGet(size_t ndx) const TIGHTDB_NOEXCEPT
// FIXME: Shouldn't ColumnStringGet() be locaterd in ColumnString?
const char* Array::ColumnStringGet(size_t ndx) const TIGHTDB_NOEXCEPT
{
const char* data = reinterpret_cast<char*>(m_data);
const char* data = reinterpret_cast<char*>(m_data);
const uint8_t* header = m_data - 8;
size_t width = m_width;
bool isNode = m_isNode;
Expand Down Expand Up @@ -2688,4 +2654,5 @@ size_t Array::IndexStringCount(const char* value, void* column, StringGetter get
}
}


} //namespace tightdb
75 changes: 61 additions & 14 deletions src/tightdb/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class MemStats {

enum ColumnDef {
coldef_Normal,
coldef_Node,
coldef_InnerNode, ///< Inner node of B-tree
coldef_HasRefs
};

Expand Down Expand Up @@ -299,8 +299,15 @@ class Array: public ArrayParent {
void ReferenceSort(Array &ref);
void Resize(size_t count);

bool IsNode() const TIGHTDB_NOEXCEPT {return m_isNode;}
/// Returns true if type is not coldef_InnerNode
bool is_leaf() const TIGHTDB_NOEXCEPT {return !m_isNode;}

/// Returns true if type is either coldef_HasRefs or coldef_InnerNode
bool HasRefs() const TIGHTDB_NOEXCEPT {return m_hasRefs;}

// FIXME: Remove this, wrong terminology
bool IsNode() const TIGHTDB_NOEXCEPT {return m_isNode;}

bool IsIndexNode() const {return get_header_indexflag();}
void SetIsIndexNode(bool value) {set_header_indexflag(value);}
Array GetSubArray(size_t ndx) const TIGHTDB_NOEXCEPT; // FIXME: Constness is not propagated to the sub-array. This constitutes a real problem, because modifying the returned array may cause the parent to be modified too.
Expand Down Expand Up @@ -382,16 +389,20 @@ class Array: public ArrayParent {
template <bool gt, Action action, size_t width, class Callback>
bool FindGTLT(int64_t v, uint64_t chunk, QueryState<int64_t>* state, size_t baseindex, Callback callback) const;

// Debug
size_t GetBitWidth() const {return m_width;}
/// The meaning of 'width' depends on the context in which this
/// array is used.
std::size_t get_width() const TIGHTDB_NOEXCEPT { return m_width; }

#ifdef TIGHTDB_DEBUG
void Print() const;
void Verify() const;
void ToDot(std::ostream& out, const char* title=NULL) const;
void Stats(MemStats& stats) const;
#endif // TIGHTDB_DEBUG
mutable unsigned char* m_data; // FIXME: Should be 'char' not 'unsigned char'

// FIXME: Should be 'char' not 'unsigned char'
// FIXME: Should not be public
mutable unsigned char* m_data;

private:

Expand Down Expand Up @@ -489,8 +500,8 @@ class Array: public ArrayParent {
// FIXME: below should be moved to a specific ArrayNumber class
protected:
// Getters and Setters for adaptive-packed arrays
typedef int64_t(Array::*Getter)(size_t) const; // Note: getters must not throw
typedef void(Array::*Setter)(size_t, int64_t);
typedef int64_t (Array::*Getter)(size_t) const; // Note: getters must not throw
typedef void (Array::*Setter)(size_t, int64_t);
typedef void (Array::*Finder)(int64_t, size_t, size_t, size_t, QueryState<int64_t>*) const;

Getter m_getter;
Expand Down Expand Up @@ -684,13 +695,50 @@ inline int64_t Array::back() const TIGHTDB_NOEXCEPT
return Get(m_len-1);
}

inline int64_t Array::Get(std::size_t ndx) const TIGHTDB_NOEXCEPT
{
TIGHTDB_ASSERT(ndx < m_len);
return (this->*m_getter)(ndx);

// Two ideas that are not efficient but may be worth looking into again:
/*
// Assume correct width is found early in TIGHTDB_TEMPEX, which is the case for B tree offsets that
// are probably either 2^16 long. Turns out to be 25% faster if found immediately, but 50-300% slower
// if found later
TIGHTDB_TEMPEX(return Get, (ndx));
*/
/*
// Slightly slower in both of the if-cases. Also needs an matchcount m_len check too, to avoid
// reading beyond array.
if (m_width >= 8 && m_len > ndx + 7)
return Get<64>(ndx >> m_shift) & m_widthmask;
else
return (this->*m_getter)(ndx);
*/
}

inline std::size_t Array::GetAsRef(std::size_t ndx) const TIGHTDB_NOEXCEPT
{
TIGHTDB_ASSERT(ndx < m_len);
TIGHTDB_ASSERT(m_hasRefs);
const int64_t v = Get(ndx);
return to_ref(v);
}

inline std::size_t Array::GetAsSizeT(std::size_t ndx) const TIGHTDB_NOEXCEPT
{
TIGHTDB_ASSERT(ndx < m_len);
const int64_t v = Get(ndx);
return to_size_t(v);
}


inline Array Array::GetSubArray(std::size_t ndx) const TIGHTDB_NOEXCEPT
{
TIGHTDB_ASSERT(ndx < m_len);
TIGHTDB_ASSERT(m_hasRefs);

const size_t ref = std::size_t(Get(ndx));
const std::size_t ref = std::size_t(Get(ndx));
TIGHTDB_ASSERT(ref);

return Array(ref, const_cast<Array*>(this), ndx, m_alloc); // FIXME: Constness is not propagated to the sub-array. This constitutes a real problem, because modifying the returned array genrally causes the parent to be modified too.
Expand Down Expand Up @@ -783,7 +831,6 @@ inline void Array::init_header(void* header, bool is_node, bool has_refs, int wi

//-------------------------------------------------


template<class S> size_t Array::Write(S& out, bool recurse, bool persist) const
{
TIGHTDB_ASSERT(IsValid());
Expand All @@ -793,7 +840,7 @@ template<class S> size_t Array::Write(S& out, bool recurse, bool persist) const

if (recurse && m_hasRefs) {
// Temp array for updated refs
Array newRefs(m_isNode ? coldef_Node : coldef_HasRefs);
Array newRefs(m_isNode ? coldef_InnerNode : coldef_HasRefs);

// Make sure that all flags are retained
if (IsIndexNode())
Expand Down Expand Up @@ -1540,12 +1587,12 @@ template <class cond, Action action, size_t bitwidth, class Callback> void Array
#ifdef TIGHTDB_DEBUG
Array r_arr;
QueryState<int64_t> r_state;
Array *akku = (Array*)state->m_state;
Array *accu = (Array*)state->m_state;
r_state.m_state = (int64_t)&r_arr;

if (action == act_FindAll) {
for (size_t t = 0; t < akku->size(); t++)
r_arr.add(akku->Get(t));
for (size_t t = 0; t < accu->size(); t++)
r_arr.add(accu->Get(t));
}
else {
r_state.m_state = state->m_state;
Expand All @@ -1558,7 +1605,7 @@ template <class cond, Action action, size_t bitwidth, class Callback> void Array
if (action == act_Max || action == act_Min || action == act_Sum || action == act_Count || action == act_ReturnFirst || action == act_Count) {
find_reference<cond, action, bitwidth, Callback>(value, start, end, baseindex, &r_state, callback);
if (action == act_FindAll)
TIGHTDB_ASSERT(akku->Compare(r_arr));
TIGHTDB_ASSERT(accu->Compare(r_arr));
else
TIGHTDB_ASSERT(state->m_state == r_state.m_state);
}
Expand Down
1 change: 0 additions & 1 deletion src/tightdb/array_basic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ class BasicArray : public Array {
/// Compare two arrays for equality.
bool Compare(const BasicArray<T>&) const;


private:
virtual size_t CalcByteLen(size_t count, size_t width) const;
virtual size_t CalcItemCount(size_t bytes, size_t width) const TIGHTDB_NOEXCEPT;
Expand Down
4 changes: 2 additions & 2 deletions src/tightdb/array_basic_tpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ inline size_t BasicArray<T>::create_empty_basic_array(Allocator& alloc)
}

template<typename T>
inline BasicArray<T>::BasicArray(ArrayParent *parent, size_t ndx_in_parent, Allocator& alloc)
:Array(alloc)
inline BasicArray<T>::BasicArray(ArrayParent *parent, size_t ndx_in_parent, Allocator& alloc):
Array(alloc)
{
const size_t ref = create_empty_basic_array(alloc); // Throws
init_from_ref(ref);
Expand Down
8 changes: 4 additions & 4 deletions src/tightdb/array_blob.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class ArrayBlob : public Array {
ArrayBlob(ArrayParent *parent=NULL, size_t pndx=0,
Allocator& alloc = Allocator::get_default());
ArrayBlob(size_t ref, const ArrayParent *parent, size_t pndx,
Allocator& alloc = Allocator::get_default());
ArrayBlob(Allocator& alloc);
Allocator& alloc = Allocator::get_default()) TIGHTDB_NOEXCEPT;
ArrayBlob(Allocator& alloc) TIGHTDB_NOEXCEPT;

const char* Get(size_t pos) const TIGHTDB_NOEXCEPT;

Expand Down Expand Up @@ -66,7 +66,7 @@ inline ArrayBlob::ArrayBlob(ArrayParent *parent, std::size_t pndx, Allocator& al
}

inline ArrayBlob::ArrayBlob(std::size_t ref, const ArrayParent *parent, std::size_t pndx,
Allocator& alloc): Array(alloc)
Allocator& alloc) TIGHTDB_NOEXCEPT: Array(alloc)
{
// Manually create array as doing it in initializer list
// will not be able to call correct virtual functions
Expand All @@ -75,7 +75,7 @@ inline ArrayBlob::ArrayBlob(std::size_t ref, const ArrayParent *parent, std::siz
}

// Creates new array (but invalid, call UpdateRef to init)
inline ArrayBlob::ArrayBlob(Allocator& alloc) : Array(alloc) {}
inline ArrayBlob::ArrayBlob(Allocator& alloc) TIGHTDB_NOEXCEPT: Array(alloc) {}

inline const char* ArrayBlob::Get(std::size_t pos) const TIGHTDB_NOEXCEPT
{
Expand Down
20 changes: 10 additions & 10 deletions src/tightdb/array_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,29 +320,29 @@ void ArrayString::ToDot(FILE* f) const
}
*/

void ArrayString::ToDot(std::ostream& out, const char* title) const
void ArrayString::ToDot(ostream& out, const char* title) const
{
const size_t ref = GetRef();

if (title) {
out << "subgraph cluster_" << ref << " {" << std::endl;
out << " label = \"" << title << "\";" << std::endl;
out << " color = white;" << std::endl;
out << "subgraph cluster_" << ref << " {" << endl;
out << " label = \"" << title << "\";" << endl;
out << " color = white;" << endl;
}

out << "n" << std::hex << ref << std::dec << "[shape=none,label=<";
out << "<TABLE BORDER=\"0\" CELLBORDER=\"1\" CELLSPACING=\"0\" CELLPADDING=\"4\"><TR>" << std::endl;
out << "n" << hex << ref << dec << "[shape=none,label=<";
out << "<TABLE BORDER=\"0\" CELLBORDER=\"1\" CELLSPACING=\"0\" CELLPADDING=\"4\"><TR>" << endl;

// Header
out << "<TD BGCOLOR=\"lightgrey\"><FONT POINT-SIZE=\"7\">";
out << "0x" << std::hex << ref << std::dec << "</FONT></TD>" << std::endl;
out << "0x" << hex << ref << dec << "</FONT></TD>" << endl;

for (size_t i = 0; i < m_len; ++i) {
out << "<TD>\"" << Get(i) << "\"</TD>" << std::endl;
out << "<TD>\"" << Get(i) << "\"</TD>" << endl;
}

out << "</TR></TABLE>>];" << std::endl;
if (title) out << "}" << std::endl;
out << "</TR></TABLE>>];" << endl;
if (title) out << "}" << endl;
}

#endif // TIGHTDB_DEBUG
Expand Down
2 changes: 1 addition & 1 deletion src/tightdb/array_string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ inline const char* ArrayString::Get(std::size_t ndx) const TIGHTDB_NOEXCEPT
TIGHTDB_ASSERT(ndx < m_len);

if (m_width == 0) return "";
else return reinterpret_cast<char*>(m_data + (ndx * m_width));
else return reinterpret_cast<char*>(m_data) + (ndx * m_width);
}

inline void ArrayString::Set(std::size_t ndx, const char* value)
Expand Down
Loading