Skip to content

Commit

Permalink
PORT: Changes for SWI-cpp2.h version 2
Browse files Browse the repository at this point in the history
  • Loading branch information
kamahen committed Apr 9, 2023
1 parent 92ea372 commit 1c306f9
Showing 1 changed file with 60 additions and 64 deletions.
124 changes: 60 additions & 64 deletions cpp/rocksdb4pl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
#include <SWI-Prolog.h>
#include <SWI-cpp2.h>

#include <SWI-cpp2.cpp> // TODO: this should possibly be separate


using namespace rocksdb;

/*******************************
Expand Down Expand Up @@ -213,10 +216,10 @@ release_rocks_ref_(PlAtom aref)
}
}
if ( ref->merger )
{ PL_erase(ref->merger);
{ Plx_erase(ref->merger);
ref->merger = 0;
}
PL_free(ref);
Plx_free(ref);

return true;
}
Expand Down Expand Up @@ -270,15 +273,15 @@ unify_rocks(PlTerm t, dbref *ref)
{ if ( ref->name.not_null() )
{ if ( ref->symbol.is_null() )
{ PlTerm_var tmp;
PlCheck(tmp.unify_blob(&ref, sizeof ref, &rocks_blob));
PlCheckFail(tmp.unify_blob(&ref, sizeof ref, &rocks_blob));
ref->symbol = t.as_atom();
rocks_alias(ref->name, ref->symbol);
}
return t.unify_atom(ref->name);
} else if ( ref->symbol.not_null() )
{ return t.unify_atom(ref->symbol);
} else return ( t.unify_blob(&ref, sizeof ref, &rocks_blob) &&
t.get_if_atom(&ref->symbol) );
t.get_atom(&ref->symbol) );
}


Expand All @@ -304,7 +307,7 @@ get_rocks(PlTerm t, dbref **erp, bool warn=true)
if ( warn )
a = t.as_atom();
else
(void)t.get_if_atom(&a);
(void)t.get_atom(&a); // TODO: get_atom_ex(&a);
if ( t.not_null() )
{ for(int i=0; i<2 && a.not_null(); i++)
{ dbref *ref;
Expand All @@ -314,7 +317,7 @@ get_rocks(PlTerm t, dbref **erp, bool warn=true)
{ *erp = ref;
return true;
} else if ( warn )
{ throw PlExistenceError("rocksdb", t);
{ throw PlExistenceError("rocksdb", t);
}
}

Expand Down Expand Up @@ -371,15 +374,15 @@ class PlSlice : public Slice

void clear()
{ if ( must_free )
PL_erase_external(const_cast<char *>(data_));
Plx_erase_external(const_cast<char *>(data_));
must_free = 0;
data_ = nullptr;
size_ = 0;
}

~PlSlice()
{ if ( must_free )
PL_erase_external(const_cast<char *>(data_));
Plx_erase_external(const_cast<char *>(data_));
}
};

Expand Down Expand Up @@ -424,15 +427,13 @@ get_slice(PlTerm t, PlSlice *s, blob_type type)
return;
case BLOB_TERM:
{ size_t len;
char *str;
if ( (str=PL_record_external(t.C_, &len)) )
{ s->data_ = str;
s->size_ = len;
s->must_free = TRUE;
return;
}
char *str = Plx_record_external(t.C_, &len);
s->data_ = str;
s->size_ = len;
s->must_free = TRUE;
return;
}
throw PlException_qid();
break;
default:
assert(0);
}
Expand Down Expand Up @@ -479,9 +480,8 @@ unify(PlTerm t, const Slice &s, blob_type type)
case BLOB_TERM:
{ PlTerm_var tmp;

return ( PL_recorded_external(s.data_, tmp.C_) &&
tmp.unify_term(t)
);
Plx_recorded_external(s.data_, tmp.C_);
return tmp.unify_term(t);
}
default:
assert(0);
Expand Down Expand Up @@ -538,37 +538,33 @@ unify_value(PlTerm t, const Slice &s, merger_t merge, blob_type type)
{ int i;
memcpy(&i, data, sizeof i);
data += sizeof i;
if ( !PL_put_integer(tmp.C_, i) )
return false;
Plx_put_integer(tmp.C_, i);
}
break;
break;
case BLOB_INT64:
{ int64_t i;
memcpy(&i, data, sizeof i);
data += sizeof i;
if ( !PL_put_int64(tmp.C_, i) )
return false;
Plx_put_int64(tmp.C_, i);
}
break;
break;
case BLOB_FLOAT32:
{ float i;
memcpy(&i, data, sizeof i);
data += sizeof i;
if ( !PL_put_float(tmp.C_, i) )
return false;
Plx_put_float(tmp.C_, i);
}
break;
break;
case BLOB_FLOAT64:
{ double i;
memcpy(&i, data, sizeof i);
data += sizeof i;
if ( !PL_put_float(tmp.C_, i) )
return false;
Plx_put_float(tmp.C_, i);
}
break;
break;
default:
assert(0);
return false;
return false;
}

if ( !list.append(tmp) )
Expand Down Expand Up @@ -596,7 +592,7 @@ static const PlAtom ATOM_full("full");

static bool
log_exception(Logger* logger)
{ PlException_qid ex;
{ PlTerm_term_t ex(Plx_exception(0));

Log(logger, "%s", ex.as_string(EncUTF8).c_str());
return false;
Expand All @@ -607,13 +603,13 @@ class engine

public:
engine()
{ if ( PL_thread_self() == -1 )
{ if ( (tid=PL_thread_attach_engine(nullptr)) < 0 )
{ PlException_qid ex;
if ( ex.not_null() )
throw ex;
else
throw PlResourceError("memory");
{ if ( Plx_thread_self() == -1 )
{ if ( (tid=Plx_thread_attach_engine(nullptr)) < 0 )
{ PlTerm_term_t ex(Plx_exception(0));
if ( ex.not_null() )
throw PlException(ex);
else
throw PlResourceError("memory");
}
}
}
Expand Down Expand Up @@ -670,11 +666,11 @@ class PrologMergeOperator : public MergeOperator
PlTerm_var tmp;

for (const auto& value : operand_list)
{ PlCheck(PL_put_variable(tmp.C_));
{ PlCheckFail(PL_put_variable(tmp.C_));
unify(tmp, value, ref->type.value);
PlCheck(list.append(tmp));
PlCheckFail(list.append(tmp));
}
PlCheck(list.close());
PlCheckFail(list.close());

if ( PL_recorded(ref->merger, av[0].C_) &&
av[1].unify_atom(ATOM_full) &&
Expand Down Expand Up @@ -888,12 +884,12 @@ get_blob_type(PlTerm t, blob_type *key_type, merger_t *m)

if ( m && t.is_functor(FUNCTOR_list1) )
{ *m = MERGE_LIST;
rc = t[1].get_if_atom(&a);
rc = t[1].get_atom(&a);
} else if ( m && t.is_functor(FUNCTOR_set1) )
{ *m = MERGE_SET;
rc = t[1].get_if_atom(&a);
rc = t[1].get_atom(&a);
} else
rc = t.get_if_atom(&a);
rc = t.get_atom(&a);

if ( rc )
{ if ( !m || *m == MERGE_NONE )
Expand All @@ -903,7 +899,7 @@ get_blob_type(PlTerm t, blob_type *key_type, merger_t *m)
else if ( ATOM_term == a ) { *key_type = BLOB_TERM; return; }
}

if ( ATOM_int32 == a ) { *key_type = BLOB_INT32; return; }
if ( ATOM_int32 == a ) { *key_type = BLOB_INT32; return; }
else if ( ATOM_int64 == a ) { *key_type = BLOB_INT64; return; }
else if ( ATOM_float == a ) { *key_type = BLOB_FLOAT32; return; }
else if ( ATOM_double == a ) { *key_type = BLOB_FLOAT64; return; }
Expand Down Expand Up @@ -961,7 +957,7 @@ static ReadOptdef read_optdefs[] =
// "timestamp" Slice*
// "iter_start_ts" Slice*
{
"deadline", RD_ODEF {
"deadline", RD_ODEF {
options->deadline = static_cast<std::chrono::microseconds>(arg.as_int64_t()); }, PlAtom(PlAtom::null) },
{ "io_timeout", RD_ODEF {
options->io_timeout = static_cast<std::chrono::microseconds>(arg.as_int64_t()); }, PlAtom(PlAtom::null) },
Expand Down Expand Up @@ -1273,7 +1269,7 @@ PREDICATE(rocks_open_, 3)
{ PlAtom name(PlAtom::null);
size_t arity;

PlCheck(opt.name_arity(&name, &arity));
PlCheckFail(opt.name_arity(&name, &arity));
if ( arity == 1 )
{ if ( ATOM_key == name )
get_blob_type(opt[1], &key_type, static_cast<merger_t *>(nullptr));
Expand All @@ -1291,12 +1287,12 @@ PREDICATE(rocks_open_, 3)
throw PlDomainError("open_option", opt[1]);
} else if ( ATOM_mode == name )
{ PlAtom a = opt[1].as_atom();
if ( ATOM_read_write == a )
if ( ATOM_read_write == a )
read_only = false;
else if ( ATOM_read_only == a )
read_only = true;
else
throw PlDomainError("mode_option", opt[1]);
else if ( ATOM_read_only == a )
read_only = true;
else
throw PlDomainError("mode_option", opt[1]);
} else
{ lookup_optdef_and_apply(&options, optdefs, name, opt);
}
Expand Down Expand Up @@ -1371,7 +1367,7 @@ write_options(PlTerm options_term)
{ PlAtom name(PlAtom::null);
size_t arity;

PlCheck(opt.name_arity(&name, &arity));
PlCheckFail(opt.name_arity(&name, &arity));
if ( arity == 1 )
{ lookup_write_optdef_and_apply(&options, write_optdefs, name, opt);
} else
Expand Down Expand Up @@ -1438,7 +1434,7 @@ read_options(PlTerm options_term)
while(tail.next(opt))
{ PlAtom name(PlAtom::null);
size_t arity;
PlCheck(opt.name_arity(&name, &arity));
PlCheckFail(opt.name_arity(&name, &arity));
if ( arity == 1 )
{ lookup_read_optdef_and_apply(&options, read_optdefs, name, opt);
} else
Expand Down Expand Up @@ -1544,11 +1540,11 @@ enum_key_prefix(const enum_state *state)


static foreign_t
rocks_enum(PlTermv PL_av, int ac, enum_type type, control_t handle, ReadOptions options)
rocks_enum(PlTermv PL_av, int ac, enum_type type, PlControl handle, ReadOptions options)
{ enum_state state_buf = {0};
enum_state *state = &state_buf;

switch(PL_foreign_control(handle))
switch(handle.foreign_control())
{ case PL_FIRST_CALL:
get_rocks(A1, &state->ref);
if ( ac >= 4 )
Expand All @@ -1557,10 +1553,10 @@ rocks_enum(PlTermv PL_av, int ac, enum_type type, control_t handle, ReadOptions
state->ref->type.key == BLOB_BINARY) )
throw PlPermissionError("enum", "rocksdb", A1);

std::string prefix = A4.get_nchars(REP_UTF8|CVT_IN|CVT_EXCEPTION);
std::string prefix = A4.get_nchars(REP_UTF8|CVT_IN|CVT_EXCEPTION);

if ( type == ENUM_PREFIX )
{ state->prefix.length = prefix.size();
{ state->prefix.length = prefix.size();
state->prefix.string = prefix.data();
}
state->it = state->ref->db->NewIterator(options);
Expand All @@ -1573,7 +1569,7 @@ rocks_enum(PlTermv PL_av, int ac, enum_type type, control_t handle, ReadOptions
state->saved = false;
goto next;
case PL_REDO:
state = static_cast<enum_state *>(PL_foreign_context_address(handle));
state = static_cast<enum_state *>(handle.foreign_context_address());
next:
{ PlFrame fr;
for(; state->it->Valid(); state->it->Next())
Expand All @@ -1594,7 +1590,7 @@ rocks_enum(PlTermv PL_av, int ac, enum_type type, control_t handle, ReadOptions
return false;
}
case PL_PRUNED:
state = static_cast<enum_state *>(PL_foreign_context_address(handle));
state = static_cast<enum_state *>(handle.foreign_context_address());
free_enum_state(state);
return true;
default:
Expand Down Expand Up @@ -1624,7 +1620,7 @@ batch_operation(const dbref *ref, WriteBatch &batch, PlTerm e)
{ PlAtom name(PlAtom::null);
size_t arity;

PlCheck(e.name_arity(&name, &arity));
PlCheckFail(e.name_arity(&name, &arity));
if ( ATOM_delete == name && arity == 1 )
{ PlSlice key;

Expand Down Expand Up @@ -1670,7 +1666,7 @@ PREDICATE(rocks_property, 3)
{ uint64_t value;

return ref->db->GetIntProperty("rocksdb.estimate-num-keys", &value) &&
A3.unify_integer(value);
A3.unify_integer(value);
} else
throw PlDomainError("rocks_property", A2);
}

0 comments on commit 1c306f9

Please sign in to comment.