Skip to content

Commit

Permalink
Merge pull request #60 from kspangsege/table_copying
Browse files Browse the repository at this point in the history
Table copying is now supported.
  • Loading branch information
kspangsege committed Feb 25, 2013
2 parents a550ee1 + 1cb892d commit a595c90
Show file tree
Hide file tree
Showing 24 changed files with 669 additions and 176 deletions.
7 changes: 7 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@ Format:
==================


2013-02-24 (Kristian Spangsege)
+ Adding copy constructors for Table and BasicTable.
+ Adding Table::copy(), BasicTable::copy() and LangBindHelper::copy_table().
+ Adding BasicTable::create() for symmetry with Table::create().


2013-02-21 (Brian Munkholm
-+ Renamed Group::get_table_count() to Group::size()


2013-02-19 (Kristian Spangsege)
+ Type of Group::BufferSpec::m_data changed from <char*> to <const char*>.

Expand Down
2 changes: 1 addition & 1 deletion src/tightdb/column.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ void Column::sort()
sort(0, Size());
}

bool Column::Compare(const Column& c) const
bool Column::compare(const Column& c) const
{
const size_t n = Size();
if (c.Size() != n) return false;
Expand Down
8 changes: 5 additions & 3 deletions src/tightdb/column.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ class ColumnBase {

virtual size_t Size() const TIGHTDB_NOEXCEPT = 0;

virtual void add() = 0;
virtual void insert(size_t ndx) = 0;
virtual void add() = 0; // Add an entry to this column using the columns default value
virtual void insert(size_t ndx) = 0; // Insert an entry into this column using the columns default value
virtual void Clear() = 0;
virtual void Delete(size_t ndx) = 0;
void Resize(size_t ndx) {m_array->Resize(ndx);}
Expand All @@ -64,6 +64,8 @@ class ColumnBase {

virtual void invalidate_subtables_virtual() {}

const Array* get_root_array() const TIGHTDB_NOEXCEPT { return m_array; }

#ifdef TIGHTDB_DEBUG
virtual void Verify() const = 0; // Must be upper case to avoid conflict with macro in ObjC
virtual void ToDot(std::ostream& out, const char* title=NULL) const;
Expand Down Expand Up @@ -205,7 +207,7 @@ class Column : public ColumnBase {
void sort();

/// Compare two columns for equality.
bool Compare(const Column&) const;
bool compare(const Column&) const;

// Debug
#ifdef TIGHTDB_DEBUG
Expand Down
2 changes: 1 addition & 1 deletion src/tightdb/column_basic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class BasicColumn : public ColumnBase {
void SetParent(ArrayParent* parent, size_t pndx) TIGHTDB_OVERRIDE {m_array->SetParent(parent, pndx);}

/// Compare two columns for equality.
bool Compare(const BasicColumn&) const;
bool compare(const BasicColumn&) const;

#ifdef TIGHTDB_DEBUG
void Verify() const {}; // Must be upper case to avoid conflict with macro in ObjC
Expand Down
2 changes: 1 addition & 1 deletion src/tightdb/column_basic_tpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ void BasicColumn<T>::fill(size_t count)
}

template<typename T>
bool BasicColumn<T>::Compare(const BasicColumn& c) const
bool BasicColumn<T>::compare(const BasicColumn& c) const
{
const size_t n = Size();
if (c.Size() != n)
Expand Down
2 changes: 1 addition & 1 deletion src/tightdb/column_binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ void ColumnBinary::Resize(size_t ndx)
((ArrayBinary*)m_array)->Resize(ndx);
}

bool ColumnBinary::Compare(const ColumnBinary& c) const
bool ColumnBinary::compare(const ColumnBinary& c) const
{
const size_t n = Size();
if (c.Size() != n) return false;
Expand Down
2 changes: 1 addition & 1 deletion src/tightdb/column_binary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class ColumnBinary : public ColumnBase {
void UpdateParentNdx(int diff) {m_array->UpdateParentNdx(diff);}

/// Compare two binary columns for equality.
bool Compare(const ColumnBinary&) const;
bool compare(const ColumnBinary&) const;

#ifdef TIGHTDB_DEBUG
void Verify() const {}; // Must be upper case to avoid conflict with macro in ObjC
Expand Down
2 changes: 1 addition & 1 deletion src/tightdb/column_mixed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ void ColumnMixed::set_binary(size_t ndx, const char* value, size_t len)
}
}

bool ColumnMixed::Compare(const ColumnMixed& c) const
bool ColumnMixed::compare(const ColumnMixed& c) const
{
const size_t n = Size();
if (c.Size() != n)
Expand Down
6 changes: 3 additions & 3 deletions src/tightdb/column_mixed.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class ColumnMixed : public ColumnBase {
void set_double(size_t ndx, double value);
void set_string(size_t ndx, const char* value);
void set_binary(size_t ndx, const char* value, size_t len);
void set_subtable(size_t ndx);
void set_subtable(size_t ndx, const Table*);

void insert_int(size_t ndx, int64_t value);
void insert_bool(size_t ndx, bool value);
Expand All @@ -109,7 +109,7 @@ class ColumnMixed : public ColumnBase {
void insert_double(size_t ndx, double value);
void insert_string(size_t ndx, const char* value);
void insert_binary(size_t ndx, const char* value, size_t len);
void insert_subtable(size_t ndx);
void insert_subtable(size_t ndx, const Table*);

void add() TIGHTDB_OVERRIDE { insert_int(Size(), 0); }
void insert(size_t ndx) TIGHTDB_OVERRIDE { insert_int(ndx, 0); invalidate_subtables(); }
Expand All @@ -125,7 +125,7 @@ class ColumnMixed : public ColumnBase {
size_t GetRef() const {return m_array->GetRef();}

/// Compare two mixed columns for equality.
bool Compare(const ColumnMixed&) const;
bool compare(const ColumnMixed&) const;

void invalidate_subtables();

Expand Down
20 changes: 16 additions & 4 deletions src/tightdb/column_mixed_tpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,16 @@ inline void ColumnMixed::set_date(size_t ndx, time_t value)
set_value(ndx, static_cast<const int64_t>(value), mixcol_Date);
}

inline void ColumnMixed::set_subtable(std::size_t ndx)
inline void ColumnMixed::set_subtable(std::size_t ndx, const Table* t)
{
TIGHTDB_ASSERT(ndx < m_types->Size());
const std::size_t ref = Table::create_empty_table(m_array->GetAllocator()); // Throws
std::size_t ref;
if (t) {
ref = t->clone(m_array->GetAllocator()); // Throws
}
else {
ref = Table::create_empty_table(m_array->GetAllocator()); // Throws
}
clear_value(ndx, mixcol_Table); // Remove any previous refs or binary data
m_refs->Set(ndx, ref);
}
Expand Down Expand Up @@ -333,10 +339,16 @@ inline void ColumnMixed::insert_binary(size_t ndx, const char* value, size_t len
m_refs->Insert(ndx, v);
}

inline void ColumnMixed::insert_subtable(std::size_t ndx)
inline void ColumnMixed::insert_subtable(std::size_t ndx, const Table* t)
{
TIGHTDB_ASSERT(ndx <= m_types->Size());
const std::size_t ref = Table::create_empty_table(m_array->GetAllocator()); // Throws
std::size_t ref;
if (t) {
ref = t->clone(m_array->GetAllocator()); // Throws
}
else {
ref = Table::create_empty_table(m_array->GetAllocator()); // Throws
}
m_types->Insert(ndx, mixcol_Table);
m_refs->Insert(ndx, ref);
}
Expand Down
4 changes: 2 additions & 2 deletions src/tightdb/column_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ bool AdaptiveStringColumn::AutoEnumerate(size_t& ref_keys, size_t& ref_values) c
size_t pos;
const bool res = keys.FindKeyPos(v, pos); // todo/fixme, res isn't used
TIGHTDB_ASSERT(res);
(void)res;
static_cast<void>(res);

values.add(pos);
}
Expand All @@ -478,7 +478,7 @@ bool AdaptiveStringColumn::AutoEnumerate(size_t& ref_keys, size_t& ref_values) c
return true;
}

bool AdaptiveStringColumn::Compare(const AdaptiveStringColumn& c) const
bool AdaptiveStringColumn::compare(const AdaptiveStringColumn& c) const
{
const size_t n = Size();
if (c.Size() != n) return false;
Expand Down
2 changes: 1 addition & 1 deletion src/tightdb/column_string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class AdaptiveStringColumn : public ColumnBase {
bool AutoEnumerate(size_t& ref_keys, size_t& ref_values) const;

/// Compare two string columns for equality.
bool Compare(const AdaptiveStringColumn&) const;
bool compare(const AdaptiveStringColumn&) const;

#ifdef TIGHTDB_DEBUG
void Verify() const; // Must be upper case to avoid conflict with macro in ObjC
Expand Down
14 changes: 13 additions & 1 deletion src/tightdb/column_string_enum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,19 @@ size_t ColumnStringEnum::GetKeyNdxOrAdd(const char* value)
}
}

bool ColumnStringEnum::Compare(const ColumnStringEnum& c) const
bool ColumnStringEnum::compare(const AdaptiveStringColumn& c) const
{
const size_t n = Size();
if (c.Size() != n) return false;
for (size_t i=0; i<n; ++i) {
const char* s1 = Get(i);
const char* s2 = c.Get(i);
if (strcmp(s1, s2) != 0) return false;
}
return true;
}

bool ColumnStringEnum::compare(const ColumnStringEnum& c) const
{
const size_t n = Size();
if (c.Size() != n) return false;
Expand Down
5 changes: 3 additions & 2 deletions src/tightdb/column_string_enum.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ class ColumnStringEnum : public Column {
void ReuseIndex(StringIndex& index);
void RemoveIndex() {m_index = NULL;}

/// Compare two string enumeration columns for equality
bool Compare(const ColumnStringEnum&) const;
// Compare two string columns for equality
bool compare(const AdaptiveStringColumn&) const;
bool compare(const ColumnStringEnum&) const;

#ifdef TIGHTDB_DEBUG
void Verify() const; // Must be upper case to avoid conflict with macro in ObjC
Expand Down
20 changes: 15 additions & 5 deletions src/tightdb/column_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,25 @@ size_t ColumnTable::get_subtable_size(size_t ndx) const TIGHTDB_NOEXCEPT

void ColumnTable::add()
{
Insert(Size()); // zero-ref indicates empty table
invalidate_subtables();
add(0); // Null-pointer indicates empty table
}

void ColumnTable::insert(size_t ndx)
{
invalidate_subtables();
insert(ndx, 0); // Null-pointer indicates empty table
}

void ColumnTable::Insert(size_t ndx)
void ColumnTable::insert(size_t ndx, const Table* subtable)
{
TIGHTDB_ASSERT(ndx <= Size());

// zero-ref indicates empty table
Column::Insert(ndx, 0);
size_t columns_ref = 0;
if (subtable)
columns_ref = clone_table_columns(subtable);

Column::Insert(ndx, columns_ref);
}

void ColumnTable::fill(size_t count)
Expand Down Expand Up @@ -93,7 +103,7 @@ void ColumnTable::ClearTable(size_t ndx)
Set(ndx, 0);
}

bool ColumnTable::Compare(const ColumnTable& c) const
bool ColumnTable::compare(const ColumnTable& c) const
{
const size_t n = Size();
if (c.Size() != n) return false;
Expand Down
39 changes: 27 additions & 12 deletions src/tightdb/column_table.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ class ColumnSubtableParent: public Column, public Table::Parent {
/// Assumes that the two tables have the same spec.
static bool compare_subtable_rows(const Table&, const Table&);

/// Construct a copy of the columns array of the specified table
/// and return just the ref to that array.
///
/// In the clone, no string column will be of the enumeration
/// type.
std::size_t clone_table_columns(const Table*);

#ifdef TIGHTDB_ENABLE_REPLICATION
size_t* record_subtable_path(size_t* begin, size_t* end) TIGHTDB_NOEXCEPT TIGHTDB_OVERRIDE
{
Expand Down Expand Up @@ -167,24 +174,22 @@ class ColumnTable: public ColumnSubtableParent {
return ColumnSubtableParent::get_subtable_ptr(subtable_ndx, m_ref_specSet);
}

// When passing a table to add() or insert() it is assumed that
// the table spec is compatible with this column. The number of
// columns must be the same, and the corresponding columns must
// have the same data type (as returned by
// Table::get_column_type()).

void add() TIGHTDB_OVERRIDE;
void Insert(size_t ndx);
void add(const Table*);
void insert(std::size_t ndx) TIGHTDB_OVERRIDE;
void insert(std::size_t ndx, const Table*);
void Delete(size_t ndx) TIGHTDB_OVERRIDE;
void ClearTable(size_t ndx);
void fill(size_t count);

// FIXME: This one is virtual and overrides
// Column::insert(size_t). Insert(size_t) is not virtual. Do we
// really need both? Insert(size_t) is at least called from
// Table::insert_subtable().
void insert(size_t ndx) TIGHTDB_OVERRIDE
{
ColumnSubtableParent::insert(ndx);
invalidate_subtables();
}

/// Compare two subtable columns for equality.
bool Compare(const ColumnTable&) const;
bool compare(const ColumnTable&) const;

void invalidate_subtables_virtual() TIGHTDB_OVERRIDE;

Expand Down Expand Up @@ -351,6 +356,11 @@ inline bool ColumnSubtableParent::compare_subtable_rows(const Table& a, const Ta
return a.compare_rows(b);
}

inline std::size_t ColumnSubtableParent::clone_table_columns(const Table* t)
{
return t->clone_columns(m_array->GetAllocator());
}


inline ColumnTable::ColumnTable(Allocator& alloc, const Table* table, std::size_t column_ndx,
std::size_t spec_ref):
Expand All @@ -362,6 +372,11 @@ inline ColumnTable::ColumnTable(Allocator& alloc, const Table* table, std::size_
ColumnSubtableParent(alloc, table, column_ndx, parent, ndx_in_parent, column_ref),
m_ref_specSet(spec_ref) {}

inline void ColumnTable::add(const Table* subtable)
{
insert(Size(), subtable);
}

inline void ColumnTable::invalidate_subtables_virtual()
{
invalidate_subtables();
Expand Down
Loading

0 comments on commit a595c90

Please sign in to comment.