Skip to content

Commit

Permalink
Merge branch 'master' of github.com:astigsen/tightdb
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenneth Geisshirt committed Jul 10, 2012
2 parents 7ca0bbb + 7209134 commit 613b95b
Show file tree
Hide file tree
Showing 27 changed files with 2,174 additions and 320 deletions.
28 changes: 22 additions & 6 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,30 @@ Format:

2012-mm-dd
----------
! Fixed bug [github issuenr]: .... (user visible bug fixed - passed on to release notes)
+ Added feature .... (user visible new feature - passed on to release notes)
- Removed/deprecated feature/method .... (user visible removed feature - passed on to release notes)
. Any other notes .... (internal changes)
! Fixed bug [github issuenr]: .... (user visible bug fixed - passed on to release notes)
+ Added feature .... (user visible new feature - passed on to release notes)
- Removed/deprecated feature/method .... (user visible removed feature - passed on to release notes)
. Any other notes .... (internal changes)

==================

2012-??-??
----------


2012-06-??
2012-06-27
----------
??
+ Added a the following methods to a TableView:
ColumnType get_mixed_type(size_t column_ndx, size_t row_ndx) const;
size_t get_subtable_size(size_t column_ndx, size_t row_ndx) const;
void clear_subtable(size_t column_ndx, size_t row_ndx);
size_t find_first_bool(size_t column_ndx, bool value) const;
size_t find_first_date(size_t column_ndx, time_t value) const;
void add_int(size_t column_ndx, int64_t value);
TableView find_all_bool(size_t column_ndx, bool value);
ConstTableView find_all_bool(size_t column_ndx, bool value) const; (for class TableView and ConstTableView)
TableView find_all_date(size_t column_ndx, time_t value);
ConstTableView find_all_date(size_t column_ndx, time_t value) const; (for class TableView and ConstTableView)

TableView get_sorted_view(size_t column_ndx, bool ascending=true);
ConstTableView get_sorted_view(size_t column_ndx, bool ascending=true) const; (for class TableView and ConstTableView)
5 changes: 3 additions & 2 deletions src/tightdb/alloc_slab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ void* SlabAlloc::Translate(size_t ref) const
if (ref < m_baseline) return m_shared + ref;
else {
const size_t ndx = m_slabs.cols().offset.find_pos(ref);
assert(ndx != size_t(-1));
assert(ndx != not_found);

const size_t offset = ndx ? m_slabs[ndx-1].offset : m_baseline;
return (char*)(intptr_t)m_slabs[ndx].pointer + (ref - offset);
Expand Down Expand Up @@ -421,7 +421,8 @@ bool SlabAlloc::ReMap(size_t filesize)
assert(m_freeReadOnly.is_empty());
assert(m_slabs.size() == m_freeSpace.size());

// If the file size have changed, we need to remap the readonly buffer
// We only need to remap the readonly buffer
// if the file size have changed.
if (filesize == m_baseline) return false;

assert(filesize >= m_baseline);
Expand Down
108 changes: 108 additions & 0 deletions src/tightdb/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "utilities.hpp"
#include "query_conditions.hpp"
#include "static_assert.hpp"
#include "column_string.hpp"

#ifdef _MSC_VER
#include <win32/types.h>
Expand Down Expand Up @@ -2067,6 +2068,7 @@ size_t get_header_len_direct(const uint8_t* const header);
int64_t GetDirect(const char* const data, size_t width, const size_t ndx);
size_t FindPosDirect(const uint8_t* const header, const char* const data, const size_t width, const int64_t target);
template<size_t width> size_t FindPosDirectImp(const uint8_t* const header, const char* const data, const int64_t target);
size_t FindPos2Direct_32(const uint8_t* const header, const char* const data, int32_t target);

bool get_header_isnode_direct(const uint8_t* const header)
{
Expand Down Expand Up @@ -2187,6 +2189,28 @@ template<size_t width> size_t FindPosDirectImp(const uint8_t* const header, cons
else return (size_t)high;
}

size_t FindPos2Direct_32(const uint8_t* const header, const char* const data, int32_t target)
{
const size_t len = get_header_len_direct(header);

int low = -1;
int high = (int)len;

// Binary search based on:
// http://www.tbray.org/ongoing/When/200x/2003/03/22/Binary
// Finds position of closest value BIGGER OR EQUAL to the target (for
// lookups in indexes)
while (high - low > 1) {
const size_t probe = ((unsigned int)low + (unsigned int)high) >> 1;
const int64_t v = GetDirect<32>(data, probe);

if (v < target) low = (int)probe;
else high = (int)probe;
}
if (high == (int)len) return (size_t)-1;
else return (size_t)high;
}

}


Expand Down Expand Up @@ -2391,4 +2415,88 @@ size_t Array::ColumnFind(int64_t target, size_t ref, Array& cache) const
}
}

size_t Array::IndexStringFindFirst(const char* value, const AdaptiveStringColumn& column) const
{
const char* v = value;
const char* data = (const char*)m_data;
const uint8_t* header = m_data - 8;
size_t width = m_width;
bool isNode = m_isNode;

top:
// Create 4 byte index key
int32_t key = 0;
if (*v) key = ((int32_t)(*v++) << 24);
if (*v) key |= ((int32_t)(*v++) << 16);
if (*v) key |= ((int32_t)(*v++) << 8);
if (*v) key |= (int32_t)(*v++);

for (;;) {
// Get subnode table
const size_t ref_offsets = GetDirect(data, width, 0);
const size_t ref_refs = GetDirect(data, width, 1);

// Find the position matching the key
const uint8_t* const offsets_header = (const uint8_t*)m_alloc.Translate(ref_offsets);
const char* const offsets_data = (const char*)offsets_header + 8;
const size_t pos = FindPos2Direct_32(offsets_header, offsets_data, key); // keys are always 32 bits wide

// If key is outside range, we know there can be no match
if (pos == not_found) return not_found;

// Get entry under key
const uint8_t* const refs_header = (const uint8_t*)m_alloc.Translate(ref_refs);
const char* const refs_data = (const char*)refs_header + 8;
const size_t refs_width = get_header_width_direct(refs_header);
const size_t ref = GetDirect(refs_data, refs_width, pos);

if (isNode) {
// Set vars for next iteration
header = (const uint8_t*)m_alloc.Translate(ref);
data = (const char*)header + 8;
width = get_header_width_direct(header);
isNode = get_header_isnode_direct(header);
continue;
}

const int32_t stored_key = (int32_t)GetDirect<32>(offsets_data, pos);

if (stored_key == key) {
// Literal row index
if (ref & 1) {
const size_t row_ref = (ref >> 1);
if (*v == '\0') return row_ref; // full string has been compared

const char* const str = column.Get(row_ref);
if (strcmp(str, value) == 0) return row_ref;
else return not_found;
}

const uint8_t* const sub_header = (const uint8_t*)m_alloc.Translate(ref);
const bool sub_hasrefs = get_header_hasrefs_direct(sub_header);

// List of matching row indexes
if (!sub_hasrefs) {
const char* const sub_data = (const char*)sub_header + 8;
const size_t sub_width = get_header_width_direct(sub_header);

const size_t row_ref = GetDirect(sub_data, sub_width, 0);
if (*v == '\0') return row_ref; // full string has been compared

const char* const str =column.Get(row_ref);
if (strcmp(str, value) == 0) return row_ref;
else return not_found;
}

// Recurse into sub-index;
header = (const uint8_t*)m_alloc.Translate(ref);
data = (const char*)header + 8;
width = get_header_width_direct(header);
isNode = get_header_isnode_direct(header);
goto top;
}
else return not_found;
}
}

} //namespace tightdb
71 changes: 24 additions & 47 deletions src/tightdb/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ static const size_t not_found = (size_t)-1;

// Pre-definitions
class Array;
class AdaptiveStringColumn;

#ifdef _DEBUG
class MemStats {
Expand Down Expand Up @@ -175,6 +176,7 @@ class Array: public ArrayParent {
int64_t ColumnGet(size_t ndx) const;
const char* ColumnStringGet(size_t ndx) const;
size_t ColumnFind(int64_t target, size_t ref, Array& cache) const;
size_t IndexStringFindFirst(const char* value, const AdaptiveStringColumn& column) const;

void SetAllToZero();
bool Increment(int64_t value, size_t start=0, size_t end=(size_t)-1);
Expand Down Expand Up @@ -352,7 +354,8 @@ template<class S> size_t Array::Write(S& out, bool recurse, bool persist) const
Array newRefs(m_isNode ? COLUMN_NODE : COLUMN_HASREFS);

// First write out all sub-arrays
for (size_t i = 0; i < Size(); ++i) {
const size_t count = Size();
for (size_t i = 0; i < count; ++i) {
const size_t ref = GetAsRef(i);
if (ref == 0 || ref & 0x1) {
// zero-refs and refs that are not 64-aligned do not point to sub-trees
Expand All @@ -365,6 +368,7 @@ template<class S> size_t Array::Write(S& out, bool recurse, bool persist) const
else {
const Array sub(ref, NULL, 0, GetAllocator());
const size_t sub_pos = sub.Write(out, true, persist);
assert((sub_pos & 0x7) == 0); // 64bit alignment
newRefs.add(sub_pos);
}
}
Expand All @@ -380,45 +384,32 @@ template<class S> size_t Array::Write(S& out, bool recurse, bool persist) const
return refs_pos; // Return position
}

// parse header
size_t len = get_header_len();
// TODO: replace capacity with checksum

// Calculate full lenght of array in bytes, including padding
// for 64bit alignment (that may be composed of random bits)
size_t len = m_len;
const WidthType wt = get_header_wtype();
int bits_in_partial_byte = 0;

// Adjust length to number of bytes
if (wt == TDB_BITS) {
const size_t bits = (len * m_width);
len = bits / 8;
bits_in_partial_byte = bits & 0x7;
if (bits & 0x7) ++len;
}
else if (wt == TDB_MULTIPLY) {
len *= m_width;
}

// TODO: replace capacity with checksum

// Calculate complete size
// Add bytes used for padding
const size_t rest = (~len & 0x7)+1;
if (rest < 8) len += rest; // 64bit blocks
len += 8; // include header in total

// Write array
const char* const data = reinterpret_cast<const char*>(m_data-8);
const size_t array_pos = out.write(data, len);

// Write last partial byte. Note: Since the memory is not
// explicitely cleared initially, we cannot rely on the unused
// bits to be in a well defined state here.
if (bits_in_partial_byte != 0) {
char_traits<char>::int_type i = char_traits<char>::to_int_type(data[len]);
i &= (1 << bits_in_partial_byte) - 1;
char b = char_traits<char>::to_char_type(i);
out.write(&b, 1);
++len;
}

// Add padding for 64bit alignment
const size_t rest = (~len & 0x7)+1;
if (rest < 8)
out.write("\0\0\0\0\0\0\0\0", rest);
assert((array_pos & 0x7) == 0); /// 64bit alignment

return array_pos; // Return position of this array
}
Expand All @@ -427,45 +418,31 @@ template<class S> void Array::WriteAt(size_t pos, S& out) const
{
assert(IsValid());

// parse header
size_t len = get_header_len();
// TODO: replace capacity with checksum

// Calculate full lenght of array in bytes, including padding
// for 64bit alignment (that may be composed of random bits)
size_t len = m_len;
const WidthType wt = get_header_wtype();
int bits_in_partial_byte = 0;

// Adjust length to number of bytes
if (wt == TDB_BITS) {
const size_t bits = (len * m_width);
len = bits / 8;
bits_in_partial_byte = bits & 0x7;
if (bits & 0x7) ++len;
}
else if (wt == TDB_MULTIPLY) {
len *= m_width;
}

// TODO: replace capacity with checksum

// Calculate complete size
// Add bytes used for padding
const size_t rest = (~len & 0x7)+1;
if (rest < 8) len += rest; // 64bit blocks
len += 8; // include header in total

// Write array
const char* const data = reinterpret_cast<const char*>(m_data-8);
out.WriteAt(pos, data, len);

// Write last partial byte. Note: Since the memory is not
// explicitely cleared initially, we cannot rely on the unused
// bits to be in a well defined state here.
if (bits_in_partial_byte != 0) {
char_traits<char>::int_type i = char_traits<char>::to_int_type(data[len]);
i &= (1 << bits_in_partial_byte) - 1;
char b = char_traits<char>::to_char_type(i);
out.WriteAt(pos+len, &b, 1);
++len;
}

// Add padding for 64bit alignment
const size_t rest = (~len & 0x7)+1;
if (rest < 8)
out.WriteAt(pos+len, "\0\0\0\0\0\0\0\0", rest);
}

inline void Array::move_assign(Array& a)
Expand Down
2 changes: 1 addition & 1 deletion src/tightdb/build/table_macros.hpp.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ DIR="$(dirname "$0")"

TABLE_MACROS_HPP="$1"

if python "$DIR/table_macros.hpp.py" 8 >/tmp/table_macros.hpp; then
if python "$DIR/table_macros.hpp.py" 15 >/tmp/table_macros.hpp; then
mv /tmp/table_macros.hpp "$TABLE_MACROS_HPP"
else
if [ -e "$TABLE_MACROS_HPP" ]; then
Expand Down
3 changes: 2 additions & 1 deletion src/tightdb/c-table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,12 @@ void table_insert_impl(Table* t, size_t ndx, va_list ap);

Table* table_new()
{
return new Table();
return tightdb::LangBindHelper::new_table();
}

void table_delete(Table* t)
{
tightdb::LangBindHelper::unbind_table_ref(t);
delete t;
}

Expand Down
18 changes: 18 additions & 0 deletions src/tightdb/column.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,24 @@ bool ColumnBase::NodeUpdateOffsets(size_t ndx)
return offsets.Increment(diff, ndx);
}

bool ColumnBase::NodeAddKey(size_t ref)
{
assert(ref);
assert(IsNode());

Array offsets = NodeGetOffsets();
Array refs = NodeGetRefs();
assert(offsets.Size() < MAX_LIST_SIZE);

const Array new_top(ref, NULL, 0,m_array->GetAllocator());
const Array new_offsets(new_top.GetAsRef(0), NULL, 0,m_array->GetAllocator());
assert(!new_offsets.is_empty());

const int64_t key = new_offsets.back();
if (!offsets.add(key)) return false;
return refs.add(ref);
}

void Column::Delete(size_t ndx)
{
assert(ndx < Size());
Expand Down
1 change: 1 addition & 0 deletions src/tightdb/column.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ template<typename T, class C, class F> size_t TreeFind(T value, size_t start, si
Array NodeGetRefs();
template<class C> bool NodeInsert(size_t ndx, size_t ref);
template<class C> bool NodeAdd(size_t ref);
bool NodeAddKey(size_t ref);
bool NodeUpdateOffsets(size_t ndx);
template<class C> bool NodeInsertSplit(size_t ndx, size_t newRef);
size_t GetRefSize(size_t ref) const;
Expand Down
Loading

0 comments on commit 613b95b

Please sign in to comment.