2021-08-24 10:59:19 -04:00
|
|
|
////////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
|
|
|
|
// This file contains the code to compare the contents of tables.
|
|
|
|
|
// The top level functions in this file are:
|
|
|
|
|
//
|
2021-09-07 14:38:46 -04:00
|
|
|
// World::diff_numbered_tables
|
2021-08-24 10:59:19 -04:00
|
|
|
// World::diff_tangible_databases
|
2021-09-07 14:38:46 -04:00
|
|
|
// World::patch_numbered_tables
|
|
|
|
|
// World::patch_tangible_databases
|
|
|
|
|
//
|
|
|
|
|
// It also contains these unit testing support routines:
|
|
|
|
|
//
|
|
|
|
|
// table.diffcompare
|
|
|
|
|
// table.diffapply
|
2021-08-24 10:59:19 -04:00
|
|
|
//
|
|
|
|
|
// This file also contains all the support code needed to implement
|
|
|
|
|
// this stuff.
|
|
|
|
|
//
|
|
|
|
|
////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2021-08-23 23:34:30 -04:00
|
|
|
#include "luastack.hpp"
|
|
|
|
|
#include "streambuffer.hpp"
|
2021-08-31 20:03:33 -04:00
|
|
|
#include "table.hpp"
|
2021-08-24 10:59:19 -04:00
|
|
|
#include "world.hpp"
|
2021-08-23 23:34:30 -04:00
|
|
|
|
|
|
|
|
// Given a table and an tnmap, return the table number of the table.
|
|
|
|
|
// Returns zero if the table doesn't have a table number.
|
|
|
|
|
//
|
2023-04-06 20:12:03 -04:00
|
|
|
static int get_table_number(LuaCoreStack &MLS, LuaSlot mval, LuaSlot mtnmap) {
|
2021-08-23 23:34:30 -04:00
|
|
|
lua_State *L = MLS.state();
|
|
|
|
|
lua_pushvalue(L, mval.index());
|
|
|
|
|
lua_rawget(L, mtnmap.index());
|
|
|
|
|
int result = 0;
|
|
|
|
|
if (lua_type(L, -1) == LUA_TNUMBER) {
|
|
|
|
|
result = lua_tointeger(L, -1);
|
|
|
|
|
}
|
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-04-06 20:12:03 -04:00
|
|
|
static bool equivalent_values(LuaCoreStack &MLS, LuaSlot mval, LuaSlot mtnmap,
|
|
|
|
|
LuaCoreStack &SLS, LuaSlot sval, LuaSlot stnmap) {
|
2021-08-23 23:34:30 -04:00
|
|
|
switch (MLS.xtype(mval)) {
|
|
|
|
|
case LUA_TBOOLEAN: {
|
|
|
|
|
if (SLS.type(sval) != LUA_TBOOLEAN) return false;
|
|
|
|
|
return MLS.ckboolean(mval) == SLS.ckboolean(sval);
|
|
|
|
|
}
|
|
|
|
|
case LUA_TNUMBER: {
|
|
|
|
|
if (SLS.type(sval) != LUA_TNUMBER) return false;
|
|
|
|
|
return MLS.cknumber(mval) == SLS.cknumber(sval);
|
|
|
|
|
}
|
|
|
|
|
case LUA_TSTRING: {
|
|
|
|
|
// This could be faster if I used lua_tolstring directly.
|
|
|
|
|
if (SLS.type(sval) != LUA_TSTRING) return false;
|
|
|
|
|
return MLS.ckstring(mval) == SLS.ckstring(sval);
|
|
|
|
|
}
|
2022-06-06 23:03:26 -04:00
|
|
|
case LUA_TLIGHTUSERDATA: {
|
|
|
|
|
if (SLS.type(sval) != LUA_TLIGHTUSERDATA) return false;
|
|
|
|
|
return MLS.cktoken(mval) == SLS.cktoken(sval);
|
|
|
|
|
}
|
2021-08-23 23:34:30 -04:00
|
|
|
case LUA_TFUNCTION: {
|
|
|
|
|
// Cannot really compare. Just return true if the types match.
|
|
|
|
|
return SLS.type(sval) == MLS.type(mval);
|
|
|
|
|
}
|
|
|
|
|
case LUA_TT_GENERAL: {
|
|
|
|
|
int midx = get_table_number(MLS, mval, mtnmap);
|
2021-08-31 20:03:33 -04:00
|
|
|
if (midx == 0) {
|
|
|
|
|
return SLS.isnil(sval);
|
|
|
|
|
}
|
2021-08-23 23:34:30 -04:00
|
|
|
int sidx = get_table_number(SLS, sval, stnmap);
|
|
|
|
|
return midx == sidx;
|
|
|
|
|
}
|
|
|
|
|
case LUA_TT_CLASS: {
|
|
|
|
|
if (SLS.xtype(sval) != LUA_TT_CLASS) return false;
|
2021-11-26 12:28:59 -05:00
|
|
|
// What if it's an ill-formed class?
|
2021-08-23 23:34:30 -04:00
|
|
|
return MLS.classname(mval) == SLS.classname(sval);
|
|
|
|
|
}
|
|
|
|
|
case LUA_TT_TANGIBLE: {
|
|
|
|
|
if (SLS.xtype(sval) != LUA_TT_TANGIBLE) return false;
|
2021-11-26 12:28:59 -05:00
|
|
|
// What if it's an ill-formed tangible?
|
|
|
|
|
return MLS.tanid(mval) == SLS.tanid(sval);
|
2021-08-23 23:34:30 -04:00
|
|
|
}
|
|
|
|
|
case LUA_TT_GLOBALENV: {
|
|
|
|
|
return (SLS.xtype(sval) == LUA_TT_GLOBALENV);
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
// We're forcing anything else to go to NIL,
|
|
|
|
|
// and once it's NIL, we consider it 'equal'.
|
|
|
|
|
return SLS.type(sval) == LUA_TNIL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-06 20:12:03 -04:00
|
|
|
static void transmit_value(LuaCoreStack &MLS, LuaSlot mval, LuaSlot mtnmap, StreamBuffer *sb) {
|
2021-08-23 23:34:30 -04:00
|
|
|
switch (MLS.xtype(mval)) {
|
|
|
|
|
case LUA_TBOOLEAN: {
|
|
|
|
|
sb->write_uint8(LUA_TBOOLEAN);
|
|
|
|
|
sb->write_bool(MLS.ckboolean(mval));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
case LUA_TNUMBER: {
|
|
|
|
|
sb->write_uint8(LUA_TNUMBER);
|
|
|
|
|
sb->write_double(MLS.cknumber(mval));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
case LUA_TSTRING: {
|
|
|
|
|
sb->write_uint8(LUA_TSTRING);
|
|
|
|
|
sb->write_string(MLS.ckstring(mval));
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-06-06 23:03:26 -04:00
|
|
|
case LUA_TLIGHTUSERDATA: {
|
|
|
|
|
sb->write_uint8(LUA_TLIGHTUSERDATA);
|
|
|
|
|
sb->write_uint64(MLS.cktoken(mval).value);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-08-23 23:34:30 -04:00
|
|
|
case LUA_TT_GENERAL: {
|
2021-08-31 20:03:33 -04:00
|
|
|
int midx = get_table_number(MLS, mval, mtnmap);
|
|
|
|
|
if (midx == 0) {
|
|
|
|
|
sb->write_uint8(LUA_TNIL);
|
|
|
|
|
} else {
|
|
|
|
|
sb->write_uint8(LUA_TT_GENERAL);
|
|
|
|
|
sb->write_uint32(midx);
|
|
|
|
|
}
|
2021-08-23 23:34:30 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
case LUA_TT_CLASS: {
|
|
|
|
|
sb->write_uint8(LUA_TT_CLASS);
|
|
|
|
|
sb->write_string(MLS.classname(mval));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
case LUA_TT_TANGIBLE: {
|
|
|
|
|
sb->write_uint8(LUA_TT_TANGIBLE);
|
2021-11-26 12:28:59 -05:00
|
|
|
sb->write_int64(MLS.tanid(mval));
|
2021-08-23 23:34:30 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
case LUA_TT_GLOBALENV: {
|
|
|
|
|
sb->write_uint8(LUA_TT_GLOBALENV);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
sb->write_uint8(LUA_TNIL);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-24 02:17:41 -05:00
|
|
|
static void transmit_value_debug_string(StreamBuffer *sb, eng::ostringstream &oss) {
|
2021-08-24 10:59:19 -04:00
|
|
|
int kind = sb->read_uint8();
|
|
|
|
|
switch (kind) {
|
|
|
|
|
case LUA_TBOOLEAN: {
|
|
|
|
|
bool b = sb->read_bool();
|
|
|
|
|
oss << (b ? "true":"false");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
case LUA_TNUMBER: {
|
|
|
|
|
oss << sb->read_double();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
case LUA_TSTRING: {
|
|
|
|
|
oss << sb->read_string();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-06-06 23:03:26 -04:00
|
|
|
case LUA_TLIGHTUSERDATA: {
|
|
|
|
|
LuaToken token(sb->read_uint64());
|
|
|
|
|
oss << "[" << token.str() << "]";
|
|
|
|
|
}
|
2021-08-24 10:59:19 -04:00
|
|
|
case LUA_TT_GENERAL: {
|
|
|
|
|
oss << "table " << sb->read_int32();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
case LUA_TT_CLASS: {
|
|
|
|
|
oss << "class " << sb->read_string();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
case LUA_TT_TANGIBLE: {
|
|
|
|
|
oss << "tan " << sb->read_int64();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
case LUA_TT_GLOBALENV: {
|
|
|
|
|
oss << "globals";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
case LUA_TNIL: {
|
|
|
|
|
oss << "nil";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
assert(false); // Should not get here.
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-06 20:12:03 -04:00
|
|
|
static bool diff_tables(LuaCoreStack &SLS0, LuaSlot stnmap, LuaSlot stab,
|
|
|
|
|
LuaCoreStack &MLS0, LuaSlot mtnmap, LuaSlot mtab,
|
2021-08-31 20:48:01 -04:00
|
|
|
bool cmeta, StreamBuffer *sb) {
|
2021-08-23 23:34:30 -04:00
|
|
|
LuaVar skey, mkey, sval, mval, mnil;
|
2023-04-06 20:12:03 -04:00
|
|
|
LuaOldStack SLS(SLS0.state(), skey, sval);
|
|
|
|
|
LuaOldStack MLS(MLS0.state(), mkey, mval, mnil);
|
2021-11-17 15:06:10 -05:00
|
|
|
assert(MLS.istable(mtab));
|
|
|
|
|
assert(SLS.istable(stab));
|
2021-08-23 23:34:30 -04:00
|
|
|
MLS.set(mnil, LuaNil);
|
|
|
|
|
int nupdates = 0;
|
|
|
|
|
|
|
|
|
|
sb->write_int32(0);
|
|
|
|
|
int wc = sb->total_writes();
|
|
|
|
|
|
|
|
|
|
MLS.set(mkey, LuaNil);
|
|
|
|
|
while (MLS.next(mtab, mkey, mval)) {
|
|
|
|
|
if (!MLS.issortablekey(mkey)) continue;
|
|
|
|
|
MLS.movesortablekey(mkey, SLS, skey);
|
|
|
|
|
SLS.rawget(sval, stab, skey);
|
|
|
|
|
if (!equivalent_values(MLS, mval, mtnmap, SLS, sval, stnmap)) {
|
|
|
|
|
transmit_value(MLS, mkey, mtnmap, sb);
|
|
|
|
|
transmit_value(MLS, mval, mtnmap, sb);
|
|
|
|
|
nupdates += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SLS.set(skey, LuaNil);
|
|
|
|
|
while (SLS.next(stab, skey, sval)) {
|
|
|
|
|
if (!SLS.issortablekey(skey)) continue;
|
|
|
|
|
SLS.movesortablekey(skey, MLS, mkey);
|
|
|
|
|
MLS.rawget(mval, mtab, mkey);
|
|
|
|
|
if (MLS.isnil(mval)) {
|
|
|
|
|
transmit_value(MLS, mkey, mtnmap, sb);
|
|
|
|
|
transmit_value(MLS, mval, mtnmap, sb);
|
|
|
|
|
nupdates += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (cmeta) {
|
|
|
|
|
SLS.getmetatable(sval, stab);
|
|
|
|
|
MLS.getmetatable(mval, mtab);
|
|
|
|
|
if (!equivalent_values(MLS, mval, mtnmap, SLS, sval, stnmap)) {
|
|
|
|
|
transmit_value(MLS, mnil, mtnmap, sb);
|
|
|
|
|
transmit_value(MLS, mval, mtnmap, sb);
|
|
|
|
|
nupdates += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sb->overwrite_int32(wc, nupdates);
|
|
|
|
|
|
|
|
|
|
SLS.result();
|
|
|
|
|
MLS.result();
|
|
|
|
|
return (nupdates > 0);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-24 02:17:41 -05:00
|
|
|
static eng::string diff_tables_debug_string(StreamBuffer *sb) {
|
|
|
|
|
eng::vector<eng::string> sorted;
|
|
|
|
|
eng::ostringstream oss;
|
2021-08-23 23:34:30 -04:00
|
|
|
int ndiffs = sb->read_int32();
|
|
|
|
|
for (int i = 0; i < ndiffs; i++) {
|
2021-08-24 10:59:19 -04:00
|
|
|
transmit_value_debug_string(sb, oss);
|
2021-08-23 23:34:30 -04:00
|
|
|
oss << "=";
|
2021-08-24 10:59:19 -04:00
|
|
|
transmit_value_debug_string(sb, oss);
|
2021-08-23 23:34:30 -04:00
|
|
|
sorted.push_back(oss.str());
|
|
|
|
|
oss.str("");
|
|
|
|
|
}
|
|
|
|
|
std::sort(sorted.begin(), sorted.end());
|
2022-02-24 02:17:41 -05:00
|
|
|
for (const eng::string &s : sorted) {
|
2021-08-23 23:34:30 -04:00
|
|
|
oss << s << ";";
|
|
|
|
|
}
|
|
|
|
|
return oss.str();
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-06 20:12:03 -04:00
|
|
|
static void set_transmitted_value(LuaCoreStack &LS, LuaSlot tangibles, LuaSlot ntmap, LuaSlot target, StreamBuffer *sb, const char *dbinfo, DebugCollector *dbc) {
|
2021-08-31 20:03:33 -04:00
|
|
|
int kind = sb->read_uint8();
|
|
|
|
|
switch (kind) {
|
|
|
|
|
case LUA_TBOOLEAN: {
|
2021-11-21 13:35:39 -05:00
|
|
|
bool value = sb->read_bool();
|
|
|
|
|
DebugLine(dbc) << dbinfo << (value ? "true" : "false");
|
|
|
|
|
LS.set(target, value);
|
2021-08-31 20:03:33 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
case LUA_TNUMBER: {
|
2021-11-21 13:35:39 -05:00
|
|
|
double value = sb->read_double();
|
|
|
|
|
DebugLine(dbc) << dbinfo << value;
|
|
|
|
|
LS.set(target, value);
|
2021-08-31 20:03:33 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
case LUA_TSTRING: {
|
2022-02-24 02:17:41 -05:00
|
|
|
eng::string value = sb->read_string();
|
2021-11-21 13:35:39 -05:00
|
|
|
DebugLine(dbc) << dbinfo << "'" << value << "'";
|
|
|
|
|
LS.set(target, value);
|
2021-08-31 20:03:33 -04:00
|
|
|
return;
|
2022-06-06 23:03:26 -04:00
|
|
|
}
|
|
|
|
|
case LUA_TLIGHTUSERDATA: {
|
|
|
|
|
LuaToken value(sb->read_uint64());
|
|
|
|
|
DebugLine(dbc) << dbinfo << "[" << value.str() << "]";
|
|
|
|
|
LS.set(target, value);
|
|
|
|
|
return;
|
2021-08-31 20:03:33 -04:00
|
|
|
}
|
|
|
|
|
case LUA_TT_GENERAL: {
|
2021-11-21 13:35:39 -05:00
|
|
|
int index = sb->read_int32();
|
|
|
|
|
DebugLine(dbc) << dbinfo << "table " << index;
|
|
|
|
|
LS.rawget(target, ntmap, index);
|
2021-08-31 20:03:33 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
case LUA_TT_CLASS: {
|
2022-02-24 02:17:41 -05:00
|
|
|
eng::string value = sb->read_string();
|
2021-11-21 13:35:39 -05:00
|
|
|
DebugLine(dbc) << dbinfo << "class " << value;
|
|
|
|
|
LS.makeclass(target, value);
|
2021-08-31 20:03:33 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
case LUA_TT_TANGIBLE: {
|
|
|
|
|
int64_t id = sb->read_int64();
|
2021-11-21 13:35:39 -05:00
|
|
|
DebugLine(dbc) << dbinfo << "tan " << id;
|
2023-03-16 23:31:29 -04:00
|
|
|
LS.maketan(target, id);
|
2021-08-31 20:03:33 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
case LUA_TT_GLOBALENV: {
|
2021-11-21 13:35:39 -05:00
|
|
|
DebugLine(dbc) << dbinfo << "global env";
|
2021-08-31 20:03:33 -04:00
|
|
|
LS.getglobaltable(target);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
case LUA_TNIL: {
|
2021-11-21 13:35:39 -05:00
|
|
|
DebugLine(dbc) << dbinfo << "nil";
|
2021-08-31 20:03:33 -04:00
|
|
|
LS.set(target, LuaNil);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
assert(false); // Should not get here.
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-06 20:12:03 -04:00
|
|
|
static void patch_table(LuaCoreStack &LS0, LuaSlot tangibles, LuaSlot ntmap, LuaSlot tab, StreamBuffer *sb, DebugCollector *dbc) {
|
2021-08-31 20:03:33 -04:00
|
|
|
LuaVar key, val;
|
2023-04-06 20:12:03 -04:00
|
|
|
LuaOldStack LS(LS0.state(), key, val);
|
2021-08-31 20:03:33 -04:00
|
|
|
int ndiffs = sb->read_int32();
|
|
|
|
|
for (int i = 0; i < ndiffs; i++) {
|
2021-11-21 13:35:39 -05:00
|
|
|
set_transmitted_value(LS, tangibles, ntmap, key, sb, "key=", dbc);
|
|
|
|
|
set_transmitted_value(LS, tangibles, ntmap, val, sb, "val=", dbc);
|
2021-09-01 18:13:30 -04:00
|
|
|
if (LS.isnil(key)) {
|
|
|
|
|
LS.setmetatable(tab, val);
|
|
|
|
|
} else {
|
|
|
|
|
LS.rawset(tab, key, val);
|
|
|
|
|
}
|
2021-08-31 20:03:33 -04:00
|
|
|
}
|
|
|
|
|
LS.result();
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-21 13:35:39 -05:00
|
|
|
void World::patch_numbered_tables(StreamBuffer *sb, DebugCollector *dbc) {
|
2021-09-10 17:06:07 -04:00
|
|
|
lua_State *L = state();
|
|
|
|
|
LuaVar tangibles, ntmap, tab;
|
2023-04-06 20:12:03 -04:00
|
|
|
LuaOldStack LS(L, tangibles, ntmap, tab);
|
2021-09-10 17:06:07 -04:00
|
|
|
LS.rawget(tangibles, LuaRegistry, "tangibles");
|
|
|
|
|
LS.rawget(ntmap, LuaRegistry, "ntmap");
|
|
|
|
|
assert(LS.istable(tangibles));
|
|
|
|
|
assert(LS.istable(ntmap));
|
|
|
|
|
|
|
|
|
|
int nmodified = sb->read_int32();
|
|
|
|
|
for (int i = 0; i < nmodified; i++) {
|
|
|
|
|
int index = sb->read_int32();
|
2021-11-17 15:11:55 -05:00
|
|
|
LS.rawget(tab, ntmap, index);
|
2021-09-10 17:06:07 -04:00
|
|
|
assert(LS.istable(tab));
|
2021-11-21 13:35:39 -05:00
|
|
|
DebugHeader(dbc) << "Lua Table " << index << ":";
|
|
|
|
|
patch_table(LS, tangibles, ntmap, tab, sb, dbc);
|
2021-09-10 17:06:07 -04:00
|
|
|
}
|
|
|
|
|
LS.result();
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-31 20:03:33 -04:00
|
|
|
void World::diff_numbered_tables(lua_State *master, StreamBuffer *sb) {
|
2021-08-24 10:59:19 -04:00
|
|
|
lua_State *synch = state();
|
2021-08-31 20:48:01 -04:00
|
|
|
LuaVar sntmap, mntmap, stnmap, mtnmap, stab, mtab;
|
2023-04-06 20:12:03 -04:00
|
|
|
LuaOldStack SLS(synch, sntmap, stnmap, stab);
|
|
|
|
|
LuaOldStack MLS(master, mntmap, mtnmap, mtab);
|
2021-08-24 10:59:19 -04:00
|
|
|
SLS.rawget(sntmap, LuaRegistry, "ntmap");
|
|
|
|
|
MLS.rawget(mntmap, LuaRegistry, "ntmap");
|
|
|
|
|
SLS.rawget(stnmap, LuaRegistry, "tnmap");
|
|
|
|
|
MLS.rawget(mtnmap, LuaRegistry, "tnmap");
|
|
|
|
|
int m_ntables = MLS.rawlen(mntmap);
|
|
|
|
|
int s_ntables = SLS.rawlen(sntmap);
|
|
|
|
|
assert(m_ntables == s_ntables);
|
|
|
|
|
|
|
|
|
|
sb->write_int32(0);
|
|
|
|
|
int write_count_after = sb->total_writes();
|
|
|
|
|
int nmodified = 0;
|
|
|
|
|
int s_top = lua_gettop(synch);
|
|
|
|
|
int m_top = lua_gettop(master);
|
|
|
|
|
for (int id = 1; id <= m_ntables; id++) {
|
2021-11-17 15:11:55 -05:00
|
|
|
MLS.rawget(mtab, mntmap, id);
|
2021-08-31 20:48:01 -04:00
|
|
|
if (MLS.istable(mtab)) {
|
2021-11-17 15:11:55 -05:00
|
|
|
SLS.rawget(stab, sntmap, id);
|
2021-09-07 14:38:46 -04:00
|
|
|
assert(SLS.istable(stab));
|
2021-08-24 10:59:19 -04:00
|
|
|
int tw = sb->total_writes();
|
|
|
|
|
sb->write_int32(id);
|
|
|
|
|
nmodified += 1;
|
2021-08-31 20:48:01 -04:00
|
|
|
if (!diff_tables(SLS, stnmap, stab, MLS, mtnmap, mtab, true, sb)) {
|
2021-08-24 10:59:19 -04:00
|
|
|
sb->unwrite_to(tw);
|
|
|
|
|
nmodified -= 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-31 20:48:01 -04:00
|
|
|
assert(lua_gettop(synch) == s_top);
|
|
|
|
|
assert(lua_gettop(master) == m_top);
|
2021-08-24 10:59:19 -04:00
|
|
|
}
|
|
|
|
|
sb->overwrite_int32(write_count_after, nmodified);
|
2021-09-09 18:23:17 -04:00
|
|
|
SLS.result();
|
|
|
|
|
MLS.result();
|
2021-08-24 10:59:19 -04:00
|
|
|
}
|
|
|
|
|
|
2021-11-21 13:35:39 -05:00
|
|
|
void World::patch_tangible_databases(StreamBuffer *sb, DebugCollector *dbc) {
|
2021-09-10 17:06:07 -04:00
|
|
|
lua_State *L = state();
|
|
|
|
|
LuaVar tangibles, ntmap, tab;
|
2023-04-06 20:12:03 -04:00
|
|
|
LuaOldStack LS(L, tangibles, ntmap, tab);
|
2021-09-10 17:06:07 -04:00
|
|
|
LS.rawget(tangibles, LuaRegistry, "tangibles");
|
|
|
|
|
LS.rawget(ntmap, LuaRegistry, "ntmap");
|
|
|
|
|
assert(LS.istable(tangibles));
|
|
|
|
|
assert(LS.istable(ntmap));
|
|
|
|
|
|
|
|
|
|
int nmodified = sb->read_int32();
|
|
|
|
|
for (int i = 0; i < nmodified; i++) {
|
|
|
|
|
int64_t id = sb->read_int64();
|
2021-11-17 15:11:55 -05:00
|
|
|
LS.rawget(tab, tangibles, id);
|
2021-09-10 17:06:07 -04:00
|
|
|
assert(LS.istable(tab));
|
2021-11-21 13:35:39 -05:00
|
|
|
DebugHeader(dbc) << "Tangible DB " << id << ":";
|
|
|
|
|
patch_table(LS, tangibles, ntmap, tab, sb, dbc);
|
2021-09-10 17:06:07 -04:00
|
|
|
}
|
|
|
|
|
LS.result();
|
|
|
|
|
}
|
2021-08-24 10:59:19 -04:00
|
|
|
|
|
|
|
|
void World::diff_tangible_databases(const IdVector &basis, lua_State *master, StreamBuffer *sb) {
|
|
|
|
|
lua_State *synch = state();
|
2021-08-31 20:48:01 -04:00
|
|
|
LuaVar stnmap, mtnmap, stangibles, mtangibles, stab, mtab;
|
2023-04-06 20:12:03 -04:00
|
|
|
LuaOldStack SLS(synch, stnmap, stangibles, stab);
|
|
|
|
|
LuaOldStack MLS(master, mtnmap, mtangibles, mtab);
|
2021-08-24 10:59:19 -04:00
|
|
|
SLS.rawget(stnmap, LuaRegistry, "tnmap");
|
|
|
|
|
MLS.rawget(mtnmap, LuaRegistry, "tnmap");
|
|
|
|
|
SLS.rawget(stangibles, LuaRegistry, "tangibles");
|
|
|
|
|
MLS.rawget(mtangibles, LuaRegistry, "tangibles");
|
|
|
|
|
|
|
|
|
|
sb->write_int32(0);
|
|
|
|
|
int write_count_after = sb->total_writes();
|
|
|
|
|
int nmodified = 0;
|
|
|
|
|
int s_top = lua_gettop(synch);
|
|
|
|
|
int m_top = lua_gettop(master);
|
|
|
|
|
for (int64_t id : basis) {
|
2021-11-17 15:06:10 -05:00
|
|
|
MLS.rawget(mtab, mtangibles, id);
|
|
|
|
|
SLS.rawget(stab, stangibles, id);
|
|
|
|
|
assert(MLS.istable(mtab));
|
|
|
|
|
assert(SLS.istable(stab));
|
2021-08-24 10:59:19 -04:00
|
|
|
int tw = sb->total_writes();
|
|
|
|
|
sb->write_int64(id);
|
|
|
|
|
nmodified += 1;
|
2021-08-31 20:48:01 -04:00
|
|
|
if (!diff_tables(SLS, stnmap, stab, MLS, mtnmap, mtab, false, sb)) {
|
2021-08-24 10:59:19 -04:00
|
|
|
sb->unwrite_to(tw);
|
|
|
|
|
nmodified -= 1;
|
|
|
|
|
}
|
|
|
|
|
assert(lua_gettop(synch) == s_top);
|
|
|
|
|
assert(lua_gettop(master) == m_top);
|
|
|
|
|
}
|
|
|
|
|
sb->overwrite_int32(write_count_after, nmodified);
|
2021-09-09 18:23:17 -04:00
|
|
|
SLS.result();
|
|
|
|
|
MLS.result();
|
2021-08-24 10:59:19 -04:00
|
|
|
}
|
|
|
|
|
|
2021-12-15 23:03:43 -05:00
|
|
|
LuaDefine(table_diffcompare, "mtnmap,mtab,stnmap,stab", "for unit testing only") {
|
|
|
|
|
LuaArg mtnmap, mtab, mstnmap, mstab;
|
2021-08-23 23:34:30 -04:00
|
|
|
LuaRet dbgstring;
|
|
|
|
|
LuaVar tthread;
|
2023-04-06 20:12:03 -04:00
|
|
|
LuaOldStack MLS(L, mtnmap, mtab, mstnmap, mstab, dbgstring, tthread);
|
2021-08-31 20:03:33 -04:00
|
|
|
// Check the arguments.
|
2022-07-22 16:00:37 -04:00
|
|
|
MLS.checktable(mtnmap, "mtnmap");
|
|
|
|
|
MLS.checktable(mstnmap, "mstnmap");
|
|
|
|
|
MLS.checktable(mtab, "mtab");
|
|
|
|
|
MLS.checktable(mstab, "mstab");
|
2021-08-31 20:48:01 -04:00
|
|
|
|
2021-08-23 23:34:30 -04:00
|
|
|
// Create a temporary thread to be the 'synch model'. We'll use the
|
2021-08-31 20:48:01 -04:00
|
|
|
// existing thread as the 'master model'. Move two tables to the synch thread.
|
2021-08-23 23:34:30 -04:00
|
|
|
lua_State *synch = lua_newthread(L);
|
|
|
|
|
lua_replace(L, tthread.index());
|
2021-08-31 20:48:01 -04:00
|
|
|
lua_pushvalue(L, mstnmap.index());
|
|
|
|
|
lua_pushvalue(L, mstab.index());
|
|
|
|
|
lua_xmove(L, synch, 2);
|
2021-12-15 23:03:43 -05:00
|
|
|
LuaArg stnmap,stab;
|
2023-04-06 20:12:03 -04:00
|
|
|
LuaOldStack SLS(synch, stnmap, stab);
|
2021-08-31 20:48:01 -04:00
|
|
|
|
2021-08-23 23:34:30 -04:00
|
|
|
// Call tablecmp_diff.
|
|
|
|
|
StreamBuffer sb;
|
2021-08-31 20:48:01 -04:00
|
|
|
diff_tables(SLS, stnmap, stab, MLS, mtnmap, mtab, true, &sb);
|
|
|
|
|
|
2021-08-23 23:34:30 -04:00
|
|
|
// Convert the output to a debug string.
|
2021-08-31 20:48:01 -04:00
|
|
|
MLS.set(dbgstring, diff_tables_debug_string(&sb));
|
|
|
|
|
return MLS.result();
|
2021-08-31 20:03:33 -04:00
|
|
|
}
|
|
|
|
|
|
2021-12-15 23:03:43 -05:00
|
|
|
LuaDefine(table_diffapply, "mtnmap,mtab,mstab", "for unit testing only") {
|
|
|
|
|
LuaArg mtnmap, mtab, mstab;
|
2021-08-31 20:03:33 -04:00
|
|
|
LuaRet eql, eqlstr, rtab;
|
2021-08-31 20:48:01 -04:00
|
|
|
LuaVar tthread, tangibles, mntmap, key, val;
|
2023-04-06 20:12:03 -04:00
|
|
|
LuaOldStack MLS(L, mtnmap, mtab, mstab, eql, eqlstr, rtab, tthread, tangibles, mntmap, key, val);
|
2021-08-31 20:03:33 -04:00
|
|
|
// Check the arguments.
|
2022-07-22 16:00:37 -04:00
|
|
|
MLS.checktable(mtnmap, "mtnmap");
|
|
|
|
|
MLS.checktable(mtab, "mtab");
|
|
|
|
|
MLS.checktable(mstab, "mstab");
|
2021-08-31 20:03:33 -04:00
|
|
|
|
|
|
|
|
// Get the tangibles map.
|
2021-08-31 20:48:01 -04:00
|
|
|
MLS.rawget(tangibles, LuaRegistry, "tangibles");
|
2021-08-31 20:03:33 -04:00
|
|
|
|
|
|
|
|
// Invert the tnmap to make the ntmap.
|
2021-08-31 20:48:01 -04:00
|
|
|
MLS.set(mntmap, LuaNewTable);
|
|
|
|
|
MLS.set(key, LuaNil);
|
|
|
|
|
while (MLS.next(mtnmap, key, val)) {
|
|
|
|
|
MLS.rawset(mntmap, val, key);
|
2021-08-31 20:03:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create a temporary thread to be the 'synch model'. We'll use the
|
|
|
|
|
// existing thread as the 'master model'.
|
|
|
|
|
lua_State *synch = lua_newthread(L);
|
|
|
|
|
lua_replace(L, tthread.index());
|
2021-08-31 20:48:01 -04:00
|
|
|
lua_pushvalue(L, mtnmap.index());
|
|
|
|
|
lua_pushvalue(L, mstab.index());
|
2021-08-31 20:03:33 -04:00
|
|
|
lua_xmove(L, synch, 2);
|
2021-12-15 23:03:43 -05:00
|
|
|
LuaArg stnmap, stab;
|
2023-04-06 20:12:03 -04:00
|
|
|
LuaOldStack SLS(synch, stnmap, stab);
|
2021-08-31 20:48:01 -04:00
|
|
|
|
|
|
|
|
// Call diff_tables and patch_tables
|
|
|
|
|
StreamBuffer sb;
|
|
|
|
|
diff_tables(SLS, stnmap, stab, MLS, mtnmap, mtab, true, &sb);
|
2021-08-31 20:03:33 -04:00
|
|
|
|
2021-11-21 13:35:39 -05:00
|
|
|
patch_table(MLS, tangibles, mntmap, mstab, &sb, nullptr);
|
2021-08-31 20:03:33 -04:00
|
|
|
|
2021-09-07 17:37:23 -04:00
|
|
|
bool eq = table_equal(MLS, mstab, mtab);
|
|
|
|
|
MLS.set(eql, eq);
|
|
|
|
|
if (eq) {
|
2021-08-31 20:48:01 -04:00
|
|
|
MLS.set(eqlstr, "tables equal");
|
2021-08-31 20:03:33 -04:00
|
|
|
} else {
|
2021-08-31 20:48:01 -04:00
|
|
|
MLS.set(eqlstr, "tables were supposed to be equal");
|
2021-08-31 20:03:33 -04:00
|
|
|
}
|
2021-08-31 20:48:01 -04:00
|
|
|
MLS.set(rtab, stab);
|
2021-08-31 20:03:33 -04:00
|
|
|
|
2021-08-31 20:48:01 -04:00
|
|
|
return MLS.result();
|
2021-08-23 23:34:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|