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

Query #80

Merged
merged 16 commits into from
Apr 8, 2013
Merged
Show file tree
Hide file tree
Changes from 14 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
1,320 changes: 660 additions & 660 deletions TightDB.vcxproj

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions src/tightdb/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2551,6 +2551,35 @@ pair<const char*, size_t> Array::find_leaf(const Array* root, size_t i) TIGHTDB_
}


pair<size_t, size_t> Array::find_leaf_ref(const Array* root, size_t i) TIGHTDB_NOEXCEPT
{
TIGHTDB_ASSERT(!root->is_leaf());
size_t offsets_ref = root->GetAsRef(0);
size_t refs_ref = root->GetAsRef(1);
for (;;) {
const char* header = static_cast<char*>(root->m_alloc.Translate(offsets_ref));
int width = get_width_from_header(header);
pair<size_t, size_t> p;
TIGHTDB_TEMPEX(p = find_child_offset, width, (header, i));
size_t child_ndx = p.first;
size_t local_tree_offset = p.second;
i -= local_tree_offset; // local index

header = static_cast<char*>(root->m_alloc.Translate(refs_ref));
width = get_width_from_header(header);
size_t child_ref = to_size_t(get_direct(get_data_from_header(header), width, child_ndx));

header = static_cast<char*>(root->m_alloc.Translate(child_ref));
bool child_is_leaf = !get_isnode_from_header(header);
if (child_is_leaf) return make_pair(child_ref, i);

width = get_width_from_header(header);
TIGHTDB_TEMPEX(p = ::get_two_as_size, width, (header, 0));
offsets_ref = p.first;
refs_ref = p.second;
}
}

size_t Array::get_as_size(const char* header, size_t ndx) TIGHTDB_NOEXCEPT
{
const char* data = get_data_from_header(header);
Expand Down
6 changes: 3 additions & 3 deletions src/tightdb/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,10 +444,10 @@ class Array: public ArrayParent {

protected:
friend class GroupWriter;

friend class AdaptiveStringColumn;
void init_from_ref(size_t ref) TIGHTDB_NOEXCEPT;
// void AddPositiveLocal(int64_t value);

void init_from_ref(size_t ref) TIGHTDB_NOEXCEPT;
void CreateFromHeader(char* header, size_t ref=0) TIGHTDB_NOEXCEPT;
void CreateFromHeaderDirect(char* header, size_t ref=0) TIGHTDB_NOEXCEPT;
void update_ref_in_parent();
Expand Down Expand Up @@ -498,6 +498,7 @@ class Array: public ArrayParent {
// the local index within that leaf corresponding to the specified
// column-level index.
static std::pair<const char*, std::size_t> find_leaf(const Array* root, std::size_t i) TIGHTDB_NOEXCEPT;
static std::pair<size_t, std::size_t> find_leaf_ref(const Array* root, std::size_t i) TIGHTDB_NOEXCEPT;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't have to clone the documentation, but please provide at least a small note that states how this one differs from find_leaf().


static std::size_t get_as_size(const char* header, std::size_t ndx) TIGHTDB_NOEXCEPT;

Expand Down Expand Up @@ -1742,7 +1743,6 @@ template <class cond, Action action, size_t bitwidth, class Callback> void Array
find_optimized<cond, action, bitwidth, Callback>(value, start, end, baseindex, state, callback);

#ifdef TIGHTDB_DEBUG

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)
Expand Down
14 changes: 9 additions & 5 deletions src/tightdb/column.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ class ColumnBase {
template<typename T, class C, class F>
size_t TreeFind(T value, size_t start, size_t end) const;

const Array* GetBlock(size_t ndx, Array& arr, size_t& off, bool use_retval = false) const
{
return m_array->GetBlock(ndx, arr, off, use_retval);
}

protected:
friend class StringIndex;

Expand All @@ -91,8 +96,10 @@ class ColumnBase {
// Tree functions
public:
template<typename T, class C> T TreeGet(size_t ndx) const; // FIXME: This one should probably be eliminated or redesiged because it throws due to dynamic memory allocation
template<class C> size_t TreeGetLeafRef(size_t ndx) const; // FIXME: This one should probably be eliminated or redesiged because it throws due to dynamic memory allocation

protected:
template<typename T, class C> void TreeSet(size_t ndx, T value);
template<typename T, class C> void TreeSet(size_t ndx, T value);
template<typename T, class C> void TreeInsert(size_t ndx, T value);
template<typename T, class C> NodeChange DoInsert(size_t ndx, T value);
template<typename T, class C> void TreeDelete(size_t ndx);
Expand Down Expand Up @@ -189,10 +196,7 @@ class Column : public ColumnBase {

// Query support methods
void LeafFindAll(Array &result, int64_t value, size_t add_offset, size_t start, size_t end) const;
const Array* GetBlock(size_t ndx, Array& arr, size_t& off, bool use_retval = false) const
{
return m_array->GetBlock(ndx, arr, off, use_retval);
}


// Index
bool HasIndex() const {return m_index != NULL;}
Expand Down
43 changes: 38 additions & 5 deletions src/tightdb/column_string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,48 @@ class AdaptiveStringColumn : public ColumnBase {
/// Compare two string columns for equality.
bool compare(const AdaptiveStringColumn&) const;

bool GetBlock(size_t ndx, ArrayParent** ap, size_t& off) const
{
if (IsNode()) {
std::pair<size_t, size_t> p = m_array->find_leaf_ref(m_array, ndx);
bool longstr = m_array->get_hasrefs_from_header(static_cast<const char*>(m_array->GetAllocator().Translate(p.first)));
if(longstr) {
ArrayStringLong* asl2 = new ArrayStringLong(p.first, NULL, 0, m_array->GetAllocator());
*ap = asl2;
}
else {
ArrayString* as2 = new ArrayString(p.first, NULL, 0, m_array->GetAllocator());
*ap = as2;
}
off = ndx - p.second;
return longstr;
}
else {
off = 0;
if(IsLongStrings()) {
ArrayStringLong* asl2 = new ArrayStringLong(m_array->GetRef(), NULL, 0, m_array->GetAllocator());
*ap = asl2;
return true;
}
else {
ArrayString* as2 = new ArrayString(m_array->GetRef(), NULL, 0, m_array->GetAllocator());
*ap = as2;
return false;
}
}

TIGHTDB_ASSERT(false);
}

#ifdef TIGHTDB_DEBUG
void Verify() const; // Must be upper case to avoid conflict with macro in ObjC
#endif // TIGHTDB_DEBUG

// Assumes that this column has only a single leaf node, no
// internal nodes. In this case HasRefs indicates a long string
// array.
bool IsLongStrings() const TIGHTDB_NOEXCEPT {return m_array->HasRefs();}

protected:
friend class ColumnBase;
void UpdateRef(size_t ref);
Expand All @@ -90,11 +128,6 @@ class AdaptiveStringColumn : public ColumnBase {

void LeafDelete(size_t ndx);

// Assumes that this column has only a single leaf node, no
// internal nodes. In this case HasRefs indicates a long string
// array.
bool IsLongStrings() const TIGHTDB_NOEXCEPT {return m_array->HasRefs();}

bool FindKeyPos(const char* target, size_t& pos) const;

#ifdef TIGHTDB_DEBUG
Expand Down
27 changes: 27 additions & 0 deletions src/tightdb/column_tpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,33 @@ template<typename T, class C> void ColumnBase::TreeDelete(size_t ndx)
}
}

template<class C> size_t ColumnBase::TreeGetLeafRef(size_t ndx) const
{
if(IsNode())
return NULL;

// Get subnode table
const Array offsets = NodeGetOffsets();
Array refs = NodeGetRefs();

// Find the subnode containing the item
const size_t node_ndx = offsets.FindPos(ndx);

// Calc index in subnode
const size_t offset = node_ndx ? to_ref(offsets.Get(node_ndx-1)) : 0;
const size_t local_ndx = ndx - offset;

// Get item
const C target = GetColumnFromRef<C>(refs, node_ndx); // Throws

if(!target.IsNode()) {
return refs.Get(node_ndx);
}

return target.template TreeGetLeafRef<C>(local_ndx);
}


template<typename T, class C, class F>
size_t ColumnBase::TreeFind(T value, size_t start, size_t end) const
{
Expand Down
Loading