• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

MerginMaps / geodiff / 29455580701

15 Jul 2026 10:29PM UTC coverage: 87.279% (-0.3%) from 87.592%
29455580701

Pull #252

github

web-flow
Merge 545f6cbd5 into e71dfe1a2
Pull Request #252: Allow schema changes in diffs

1112 of 1244 new or added lines in 13 files covered. (89.39%)

13 existing lines in 3 files now uncovered.

4336 of 4968 relevant lines covered (87.28%)

623.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.76
/geodiff/src/changesetreader.cpp
1
/*
2
 GEODIFF - MIT License
3
 Copyright (C) 2020 Martin Dobias
4
*/
5

6
#include "changesetreader.h"
7

8
#include "changeset.h"
9
#include "geodiffutils.hpp"
10
#include "changesetgetvarint.h"
11
#include "portableendian.h"
12
#include "sqliteutils.h"
13
#include "tableschema.h"
14

15
#include <assert.h>
16
#include <memory.h>
17

18
#include <sstream>
19

20

21
ChangesetReader::ChangesetReader() = default;
928✔
22

23
ChangesetReader::~ChangesetReader() = default;
928✔
24

25

26
bool ChangesetReader::open( const std::string &filename )
929✔
27
{
28
  try
29
  {
30
    mBuffer.reset( new Buffer );
929✔
31
    mBuffer->read( filename );
929✔
32
  }
33
  catch ( const GeoDiffException & )
10✔
34
  {
35
    return false;
10✔
36
  }
10✔
37

38
  return true;
919✔
39
}
40

41
bool ChangesetReader::nextEntry( ChangesetEntry &entry )
2,583✔
42
{
43
  while ( 1 )
44
  {
45
    if ( mOffset >= mBuffer->size() )
3,531✔
46
      break;   // EOF
797✔
47

48
    ChangesetEntryType type = static_cast<ChangesetEntryType>( readByte() );
2,734✔
49
    if ( type == ChangesetEntryType::OpTableRecord )
2,734✔
50
    {
51
      readTableRecord();
948✔
52
      // and now continue reading, we want an entry
53
    }
54
    else if ( type == ChangesetEntryType::OpInsert || type == ChangesetEntryType::OpUpdate || type == ChangesetEntryType::OpDelete )
1,786✔
55
    {
56
      entry = readDataEntry( type );
1,732✔
57
      return true;  // we're done!
1,732✔
58
    }
59
    else if ( type == ChangesetEntryType::OpCreateTable )
54✔
60
    {
61
      entry = readCreateTableEntry();
13✔
62
      return true;
13✔
63
    }
64
    else if ( type == ChangesetEntryType::OpDropTable )
41✔
65
    {
66
      entry = readDropTableEntry();
8✔
67
      return true;
8✔
68
    }
69
    else if ( type == ChangesetEntryType::OpAddColumn )
33✔
70
    {
71
      entry = readAddColumnEntry();
19✔
72
      return true;
19✔
73
    }
74
    else if ( type == ChangesetEntryType::OpDropColumn )
14✔
75
    {
76
      entry = readDropColumnEntry();
12✔
77
      return true;
12✔
78
    }
79
    else
80
    {
81
      throwReaderError( "Unknown entry type " + std::to_string( static_cast<int>( type ) ) );
6✔
82
    }
83
  }
948✔
84
  return false;
797✔
85
}
86

87
bool ChangesetReader::isEmpty() const
413✔
88
{
89
  return mBuffer->size() == 0;
413✔
90
}
91

92
void ChangesetReader::rewind()
69✔
93
{
94
  mOffset = 0;
69✔
95
  mCurrentTable = {};
69✔
96
}
69✔
97

98
char ChangesetReader::readByte()
15,967✔
99
{
100
  if ( mOffset >= mBuffer->size() )
15,967✔
101
    throwReaderError( "readByte: at the end of buffer" );
×
102
  const char *ptr = mBuffer->c_buf() + mOffset;
15,967✔
103
  ++mOffset;
15,967✔
104
  return *ptr;
15,967✔
105
}
106

107
int ChangesetReader::readVarint()
3,810✔
108
{
109
  u32 value;
110
  const unsigned char *ptr = reinterpret_cast<const unsigned char *>( mBuffer->c_buf() ) + mOffset;
3,810✔
111
  int nBytes = getVarint32( ptr, value );
3,810✔
112
  mOffset += nBytes;
3,810✔
113
  return value;
3,810✔
114
}
115

116
std::string ChangesetReader::readNullTerminatedString()
1,188✔
117
{
118
  const char *ptr = mBuffer->c_buf() + mOffset;
1,188✔
119
  int count = 0;
1,188✔
120
  while ( mOffset + count < mBuffer->size() && ptr[count] )
8,659✔
121
    ++count;
7,471✔
122

123
  if ( mOffset + count >= mBuffer->size() )
1,188✔
124
    throwReaderError( "readNullTerminatedString: at the end of buffer" );
×
125

126
  mOffset += count + 1;
1,188✔
127
  return std::string( ptr, count );
2,376✔
128
}
129

130
void ChangesetReader::readRowValues( std::vector<Value> &values )
2,148✔
131
{
132
  // let's ensure we have the right size of array
133
  if ( !mCurrentTable )
2,148✔
NEW
134
    throwReaderError( "Tried to read row without table" );
×
135
  if ( values.size() != mCurrentTable->columnCount() )
2,148✔
136
  {
137
    values.resize( mCurrentTable->columnCount() );
2,148✔
138
  }
139

140
  for ( size_t i = 0; i < mCurrentTable->columnCount(); ++i )
10,030✔
141
  {
142
    int type = readByte();
7,882✔
143
    if ( type == Value::TypeInt ) // 0x01
7,882✔
144
    {
145
      // 64-bit int (big endian)
146
      int64_t v;
147
      uint64_t x;
148
      memcpy( &x, mBuffer->c_buf() + mOffset, 8 );
2,838✔
149
      mOffset += 8;
2,838✔
150
      x = be64toh( x ); // convert big endian to host
2,838✔
151
      memcpy( &v, &x, 8 );
2,838✔
152
      values[i].setInt( v );
2,838✔
153
    }
154
    else if ( type == Value::TypeDouble ) // 0x02
5,044✔
155
    {
156
      // 64-bit double (big endian)
157
      double v;
158
      uint64_t x;
159
      memcpy( &x, mBuffer->c_buf() + mOffset, 8 );
63✔
160
      mOffset += 8;
63✔
161
      x = be64toh( x ); // convert big endian to host
63✔
162
      memcpy( &v, &x, 8 );
63✔
163
      values[i].setDouble( v );
63✔
164
    }
165
    else if ( type == Value::TypeText || type == Value::TypeBlob ) // 0x03 or 0x04
4,981✔
166
    {
167
      int len = readVarint();
2,747✔
168
      if ( mOffset + len > mBuffer->size() )
2,747✔
169
        throwReaderError( "readRowValues: text/blob: at the end of buffer" );
×
170
      values[i].setString( type == Value::TypeText ? Value::TypeText : Value::TypeBlob, mBuffer->c_buf() + mOffset, len );
2,747✔
171
      mOffset += len;
2,747✔
172
    }
2,747✔
173
    else if ( type == Value::TypeNull ) // 0x05
2,234✔
174
    {
175
      values[i].setNull();
558✔
176
    }
177
    else if ( type == Value::TypeUndefined )  // undefined value  (different from NULL)
1,676✔
178
    {
179
      values[i].setUndefined();
1,676✔
180
    }
181
    else
182
    {
183
      throwReaderError( "readRowValues: unexpected entry type" );
×
184
    }
185
  }
186
}
2,148✔
187

188
void ChangesetReader::readTableRecord()
948✔
189
{
190
  /* A 'table' record consists of:
191
  **
192
  **   * A constant 'T' character,
193
  **   * Number of columns in said table (a varint),
194
  **   * An array of nCol bytes (sPK),
195
  **   * A nul-terminated table name.
196
  */
197

198
  int nCol = readVarint();
948✔
199
  if ( nCol < 0 || nCol > 65536 )
948✔
200
    throwReaderError( "readByte: unexpected number of columns" );
×
201

202
  mCurrentTable = std::make_shared<ChangesetTable>();
948✔
203
  mCurrentTable->primaryKeys.clear();
948✔
204

205
  for ( int i = 0; i < nCol; ++i )
4,379✔
206
  {
207
    mCurrentTable->primaryKeys.push_back( readByte() );
3,431✔
208
  }
209

210
  mCurrentTable->name = readNullTerminatedString();
948✔
211
}
948✔
212

213
ChangesetDataEntry ChangesetReader::readDataEntry( ChangesetEntryType type )
1,732✔
214
{
215
  ChangesetDataEntry entry;
1,732✔
216
  readByte();
1,732✔
217
  if ( type != ChangesetEntryType::OpInsert )
1,732✔
218
    readRowValues( entry.oldValues );
696✔
219
  else
220
    entry.oldValues.erase( entry.oldValues.begin(), entry.oldValues.end() );
1,036✔
221
  if ( type != ChangesetEntryType::OpDelete )
1,732✔
222
    readRowValues( entry.newValues );
1,452✔
223
  else
224
    entry.newValues.erase( entry.newValues.begin(), entry.newValues.end() );
280✔
225

226
  entry.op = static_cast<ChangesetDataEntry::OperationType>( type );
1,732✔
227
  entry.table = mCurrentTable;
1,732✔
228
  return entry;
1,732✔
NEW
229
}
×
230

231
TableColumnInfo ChangesetReader::readColumnInfo()
94✔
232
{
233
  TableColumnInfo column;
94✔
234
  column.name = readNullTerminatedString();
94✔
235
  column.type.baseType = static_cast<TableColumnType::BaseType>( readByte() );
94✔
236
  column.type.dbType = column.type.baseTypeToString( column.type.baseType );
94✔
237
  char flags = readByte();
94✔
238
  column.isPrimaryKey = flags & 1;
94✔
239
  column.isNotNull = flags & ( 1 << 1 );
94✔
240
  column.isAutoIncrement = flags & ( 1 << 2 );
94✔
241
  column.isGeometry = flags & ( 1 << 3 );
94✔
242
  column.geomHasZ = flags & ( 1 << 4 );
94✔
243
  column.geomHasM = flags & ( 1 << 5 );
94✔
244
  column.geomType = readNullTerminatedString();
94✔
245
  column.geomSrsId = readVarint();
94✔
246
  return column;
94✔
UNCOV
247
}
×
248

249
ChangesetCreateTableEntry ChangesetReader::readCreateTableEntry()
13✔
250
{
251
  ChangesetCreateTableEntry entry;
13✔
252
  entry.tableName = readNullTerminatedString();
13✔
253
  int columnCount = readVarint();
13✔
254
  if ( columnCount < 0 || columnCount > 65536 )
13✔
NEW
255
    throwReaderError( "readCreateTableEntry: unexpected number of columns" );
×
256
  entry.columns.resize( columnCount );
13✔
257
  for ( size_t i = 0; i < entry.columns.size(); i++ )
52✔
258
  {
259
    entry.columns[i] = readColumnInfo();
39✔
260
  }
261
  return entry;
13✔
NEW
262
}
×
263

264
ChangesetDropTableEntry ChangesetReader::readDropTableEntry()
8✔
265
{
266
  ChangesetDropTableEntry entry;
8✔
267
  entry.tableName = readNullTerminatedString();
8✔
268
  int columnCount = readVarint();
8✔
269
  if ( columnCount < 0 || columnCount > 65536 )
8✔
NEW
270
    throwReaderError( "readDropTableEntry: unexpected number of columns" );
×
271
  entry.columns.resize( columnCount );
8✔
272
  for ( size_t i = 0; i < entry.columns.size(); i++ )
32✔
273
  {
274
    entry.columns[i] = readColumnInfo();
24✔
275
  }
276
  return entry;
8✔
NEW
277
}
×
278

279
ChangesetAddColumnEntry ChangesetReader::readAddColumnEntry()
19✔
280
{
281
  ChangesetAddColumnEntry entry;
19✔
282
  entry.tableName = readNullTerminatedString();
19✔
283
  entry.column = readColumnInfo();
19✔
284
  return entry;
19✔
NEW
285
}
×
286

287
ChangesetDropColumnEntry ChangesetReader::readDropColumnEntry()
12✔
288
{
289
  ChangesetDropColumnEntry entry;
12✔
290
  entry.tableName = readNullTerminatedString();
12✔
291
  entry.column = readColumnInfo();
12✔
292
  return entry;
12✔
NEW
293
}
×
294

295
void ChangesetReader::throwReaderError( const std::string &message ) const
2✔
296
{
297
  std::ostringstream stringStream;
2✔
298
  stringStream << "Reader error at offset " << mOffset << ":\n" << message;
2✔
299
  std::string str = stringStream.str();
2✔
300
  throw GeoDiffException( str );
2✔
301
}
4✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc