mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-22 11:19:32 -06:00
added support for branches and explicit transaction commit
This commit is contained in:
@@ -54,6 +54,8 @@ private:
|
||||
const char *m_filepath;
|
||||
};
|
||||
|
||||
class PgfPGF;
|
||||
|
||||
#include "db.h"
|
||||
#include "text.h"
|
||||
#include "vector.h"
|
||||
@@ -137,12 +139,23 @@ typedef struct {
|
||||
Namespace<PgfAbsCat> cats;
|
||||
} PgfAbstr;
|
||||
|
||||
typedef struct PGF_INTERNAL_DECL {
|
||||
struct PGF_INTERNAL_DECL PgfPGF {
|
||||
uint16_t major_version;
|
||||
uint16_t minor_version;
|
||||
Namespace<PgfFlag> gflags;
|
||||
PgfAbstr abstract;
|
||||
//PgfConcrs* concretes;
|
||||
} PgfPGF;
|
||||
|
||||
// If the revision is transient, then it is in a double-linked list
|
||||
// with all other transient revisions.
|
||||
ref<PgfPGF> prev, next;
|
||||
|
||||
// The name lets the user to find a particular revision in
|
||||
// the database.
|
||||
PgfText name;
|
||||
};
|
||||
|
||||
extern PGF_INTERNAL_DECL
|
||||
PgfText master;
|
||||
|
||||
#endif
|
||||
|
||||
@@ -250,7 +250,7 @@ typedef struct mchunk mbin;
|
||||
*/
|
||||
#define FASTBIN_CONSOLIDATION_THRESHOLD (65536UL)
|
||||
|
||||
struct malloc_state
|
||||
struct PGF_INTERNAL_DECL malloc_state
|
||||
{
|
||||
/* Set if the fastbin chunks contain recently inserted free blocks. */
|
||||
bool have_fastchunks;
|
||||
@@ -264,8 +264,14 @@ struct malloc_state
|
||||
object bins[NBINS * 2 - 2];
|
||||
/* Bitmap of bins */
|
||||
unsigned int binmap[BINMAPSIZE];
|
||||
/* Reference to the root object */
|
||||
object root_offset;
|
||||
|
||||
/* The namespace of all persistant grammar revisions */
|
||||
Namespace<PgfPGF> revisions;
|
||||
|
||||
/* A reference to the first transient revision in
|
||||
* a double-linked list.
|
||||
*/
|
||||
ref<PgfPGF> transient_revisions;
|
||||
};
|
||||
|
||||
PGF_INTERNAL
|
||||
@@ -346,15 +352,17 @@ void PgfDB::sync()
|
||||
}
|
||||
|
||||
PGF_INTERNAL
|
||||
object PgfDB::get_root_internal()
|
||||
ref<PgfPGF> PgfDB::get_revision(PgfText *name)
|
||||
{
|
||||
return ms->root_offset;
|
||||
return namespace_lookup(current_db->ms->revisions, name);
|
||||
}
|
||||
|
||||
PGF_INTERNAL
|
||||
void PgfDB::set_root_internal(object root_offset)
|
||||
void PgfDB::set_revision(ref<PgfPGF> pgf)
|
||||
{
|
||||
ms->root_offset = root_offset;
|
||||
Namespace<PgfPGF> nmsp = namespace_insert(current_db->ms->revisions, pgf);
|
||||
namespace_release(current_db->ms->revisions);
|
||||
current_db->ms->revisions = nmsp;
|
||||
}
|
||||
|
||||
PGF_INTERNAL
|
||||
@@ -383,7 +391,8 @@ void PgfDB::init_state(size_t size)
|
||||
|
||||
memset(ms->binmap, 0, sizeof(ms->binmap));
|
||||
|
||||
ms->root_offset = 0;
|
||||
ms->revisions = 0;
|
||||
ms->transient_revisions = 0;
|
||||
}
|
||||
|
||||
/* Take a chunk off a bin list. */
|
||||
@@ -958,12 +967,41 @@ void PgfDB::free_internal(object o)
|
||||
}
|
||||
}
|
||||
|
||||
bool PgfDB::is_valid_object(object o, size_t bytes)
|
||||
PGF_INTERNAL
|
||||
ref<PgfPGF> PgfDB::revision2pgf(PgfRevision revision)
|
||||
{
|
||||
if (o <= sizeof(*ms) || o >= ms->top)
|
||||
return false;
|
||||
mchunk *chunk = mem2chunk(ptr(ms,o));
|
||||
return (chunksize(chunk) == request2size(bytes));
|
||||
if (revision <= sizeof(*current_db->ms) || revision >= current_db->ms->top)
|
||||
throw pgf_error("Invalid revision");
|
||||
|
||||
mchunk *chunk = mem2chunk(ptr(current_db->ms,revision));
|
||||
if (chunksize(chunk) < sizeof(PgfPGF))
|
||||
throw pgf_error("Invalid revision");
|
||||
|
||||
ref<PgfPGF> pgf = revision;
|
||||
if (chunksize(chunk) != request2size(sizeof(PgfPGF)+pgf->name.size+1))
|
||||
throw pgf_error("Invalid revision");
|
||||
|
||||
return pgf;
|
||||
}
|
||||
|
||||
PGF_INTERNAL
|
||||
void PgfDB::link_transient_revision(ref<PgfPGF> pgf)
|
||||
{
|
||||
pgf->next = current_db->ms->transient_revisions;
|
||||
if (current_db->ms->transient_revisions == 0)
|
||||
current_db->ms->transient_revisions->prev = pgf;
|
||||
current_db->ms->transient_revisions = pgf;
|
||||
}
|
||||
|
||||
PGF_INTERNAL
|
||||
void PgfDB::unlink_transient_revision(ref<PgfPGF> pgf)
|
||||
{
|
||||
if (pgf->next != 0)
|
||||
pgf->next->prev = pgf->prev;
|
||||
if (pgf->prev != 0)
|
||||
pgf->prev->next = pgf->next;
|
||||
else
|
||||
current_db->ms->transient_revisions = pgf->next;
|
||||
}
|
||||
|
||||
DB_scope::DB_scope(PgfDB *db, DB_scope_mode tp)
|
||||
|
||||
@@ -83,23 +83,11 @@ public:
|
||||
return current_db->free_internal(o.as_object());
|
||||
}
|
||||
|
||||
template<class A>
|
||||
static ref<A> get_root() {
|
||||
return current_db->get_root_internal();
|
||||
}
|
||||
|
||||
template<class A>
|
||||
static void set_root(ref<A> root) {
|
||||
current_db->set_root_internal(root.offset);
|
||||
}
|
||||
|
||||
template<class A>
|
||||
static ref<A> safe_object2ref(object o) {
|
||||
if (!current_db->is_valid_object(o, sizeof(A)))
|
||||
throw pgf_error("Invalid database object");
|
||||
return o;
|
||||
}
|
||||
|
||||
static PGF_INTERNAL_DECL ref<PgfPGF> get_revision(PgfText *name);
|
||||
static PGF_INTERNAL_DECL void set_revision(ref<PgfPGF> pgf);
|
||||
static PGF_INTERNAL_DECL ref<PgfPGF> revision2pgf(PgfRevision revision);
|
||||
static PGF_INTERNAL_DECL void link_transient_revision(ref<PgfPGF> pgf);
|
||||
static PGF_INTERNAL_DECL void unlink_transient_revision(ref<PgfPGF> pgf);
|
||||
|
||||
PGF_INTERNAL_DECL static void sync();
|
||||
|
||||
@@ -112,8 +100,6 @@ private:
|
||||
PGF_INTERNAL_DECL object get_root_internal();
|
||||
PGF_INTERNAL_DECL void set_root_internal(object root_offset);
|
||||
|
||||
PGF_INTERNAL_DECL bool is_valid_object(object o, size_t bytes);
|
||||
|
||||
PGF_INTERNAL_DECL unsigned char* relocate(unsigned char* ptr);
|
||||
|
||||
friend class DB_scope;
|
||||
|
||||
@@ -27,6 +27,9 @@ pgf_exn_clear(PgfExn* err)
|
||||
err->msg = strdup(e.what()); \
|
||||
}
|
||||
|
||||
PGF_INTERNAL
|
||||
PgfText master = {size: 6, text: {'m','a','s','t','e','r','0'}};
|
||||
|
||||
PGF_API
|
||||
PgfDB *pgf_read_pgf(const char* fpath,
|
||||
PgfRevision *revision,
|
||||
@@ -47,7 +50,7 @@ PgfDB *pgf_read_pgf(const char* fpath,
|
||||
PgfReader rdr(&in, fpath);
|
||||
ref<PgfPGF> pgf = rdr.read_pgf();
|
||||
|
||||
PgfDB::set_root(pgf);
|
||||
PgfDB::set_revision(pgf);
|
||||
*revision = pgf.as_object();
|
||||
}
|
||||
|
||||
@@ -82,7 +85,7 @@ PgfDB *pgf_boot_ngf(const char* pgf_path, const char* ngf_path,
|
||||
PgfReader rdr(&in, pgf_path);
|
||||
ref<PgfPGF> pgf = rdr.read_pgf();
|
||||
|
||||
db->set_root<PgfPGF>(pgf);
|
||||
PgfDB::set_revision(pgf);
|
||||
*revision = pgf.as_object();
|
||||
|
||||
PgfDB::sync();
|
||||
@@ -113,9 +116,9 @@ PgfDB *pgf_read_ngf(const char *fpath,
|
||||
{
|
||||
DB_scope scope(db, WRITER_SCOPE);
|
||||
|
||||
if (PgfDB::get_root<PgfPGF>() == 0) {
|
||||
if (PgfDB::get_revision(&master) == 0) {
|
||||
is_new = true;
|
||||
ref<PgfPGF> pgf = PgfDB::malloc<PgfPGF>();
|
||||
ref<PgfPGF> pgf = PgfDB::malloc<PgfPGF>(sizeof(PgfPGF)+master.size+1);
|
||||
pgf->major_version = 2;
|
||||
pgf->minor_version = 0;
|
||||
pgf->gflags = 0;
|
||||
@@ -124,10 +127,13 @@ PgfDB *pgf_read_ngf(const char *fpath,
|
||||
pgf->abstract.aflags = 0;
|
||||
pgf->abstract.funs = 0;
|
||||
pgf->abstract.cats = 0;
|
||||
PgfDB::set_root<PgfPGF>(pgf);
|
||||
pgf->prev = 0;
|
||||
pgf->next = 0;
|
||||
memcpy(&pgf->name, &master, sizeof(PgfText)+master.size+1);
|
||||
PgfDB::set_revision(pgf);
|
||||
*revision = pgf.as_object();
|
||||
} else {
|
||||
*revision = PgfDB::get_root<PgfPGF>().as_object();
|
||||
*revision = PgfDB::get_revision(&master).as_object();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -160,7 +166,7 @@ PgfText *pgf_abstract_name(PgfDB *db, PgfRevision revision,
|
||||
{
|
||||
PGF_API_BEGIN {
|
||||
DB_scope scope(db, READER_SCOPE);
|
||||
ref<PgfPGF> pgf = PgfDB::safe_object2ref<PgfPGF>(revision);
|
||||
ref<PgfPGF> pgf = PgfDB::revision2pgf(revision);
|
||||
|
||||
return textdup(&(*pgf->abstract.name));
|
||||
} PGF_API_END
|
||||
@@ -174,7 +180,7 @@ void pgf_iter_categories(PgfDB *db, PgfRevision revision,
|
||||
{
|
||||
PGF_API_BEGIN {
|
||||
DB_scope scope(db, READER_SCOPE);
|
||||
ref<PgfPGF> pgf = PgfDB::safe_object2ref<PgfPGF>(revision);
|
||||
ref<PgfPGF> pgf = PgfDB::revision2pgf(revision);
|
||||
|
||||
namespace_iter(pgf->abstract.cats, itor, err);
|
||||
} PGF_API_END
|
||||
@@ -187,7 +193,7 @@ PgfType pgf_start_cat(PgfDB *db, PgfRevision revision,
|
||||
{
|
||||
PGF_API_BEGIN {
|
||||
DB_scope scope(db, READER_SCOPE);
|
||||
ref<PgfPGF> pgf = PgfDB::safe_object2ref<PgfPGF>(revision);
|
||||
ref<PgfPGF> pgf = PgfDB::revision2pgf(revision);
|
||||
|
||||
PgfText *startcat = (PgfText *)
|
||||
alloca(sizeof(PgfText)+9);
|
||||
@@ -228,7 +234,7 @@ PgfTypeHypo *pgf_category_context(PgfDB *db, PgfRevision revision,
|
||||
{
|
||||
PGF_API_BEGIN {
|
||||
DB_scope scope(db, READER_SCOPE);
|
||||
ref<PgfPGF> pgf = PgfDB::safe_object2ref<PgfPGF>(revision);
|
||||
ref<PgfPGF> pgf = PgfDB::revision2pgf(revision);
|
||||
|
||||
ref<PgfAbsCat> abscat =
|
||||
namespace_lookup(pgf->abstract.cats, catname);
|
||||
@@ -262,7 +268,7 @@ prob_t pgf_category_prob(PgfDB *db, PgfRevision revision,
|
||||
{
|
||||
PGF_API_BEGIN {
|
||||
DB_scope scope(db, READER_SCOPE);
|
||||
ref<PgfPGF> pgf = PgfDB::safe_object2ref<PgfPGF>(revision);
|
||||
ref<PgfPGF> pgf = PgfDB::revision2pgf(revision);
|
||||
|
||||
ref<PgfAbsCat> abscat =
|
||||
namespace_lookup(pgf->abstract.cats, catname);
|
||||
@@ -282,7 +288,7 @@ void pgf_iter_functions(PgfDB *db, PgfRevision revision,
|
||||
{
|
||||
PGF_API_BEGIN {
|
||||
DB_scope scope(db, READER_SCOPE);
|
||||
ref<PgfPGF> pgf = PgfDB::safe_object2ref<PgfPGF>(revision);
|
||||
ref<PgfPGF> pgf = PgfDB::revision2pgf(revision);
|
||||
|
||||
pgf_exn_clear(err);
|
||||
namespace_iter(pgf->abstract.funs, itor, err);
|
||||
@@ -311,7 +317,7 @@ void pgf_iter_functions_by_cat(PgfDB *db, PgfRevision revision,
|
||||
{
|
||||
PGF_API_BEGIN {
|
||||
DB_scope scope(db, READER_SCOPE);
|
||||
ref<PgfPGF> pgf = PgfDB::safe_object2ref<PgfPGF>(revision);
|
||||
ref<PgfPGF> pgf = PgfDB::revision2pgf(revision);
|
||||
|
||||
PgfItorHelper helper;
|
||||
helper.fn = iter_by_cat_helper;
|
||||
@@ -329,7 +335,7 @@ PgfType pgf_function_type(PgfDB *db, PgfRevision revision,
|
||||
{
|
||||
PGF_API_BEGIN {
|
||||
DB_scope scope(db, READER_SCOPE);
|
||||
ref<PgfPGF> pgf = PgfDB::safe_object2ref<PgfPGF>(revision);
|
||||
ref<PgfPGF> pgf = PgfDB::revision2pgf(revision);
|
||||
|
||||
ref<PgfAbsFun> absfun =
|
||||
namespace_lookup(pgf->abstract.funs, funname);
|
||||
@@ -349,7 +355,7 @@ int pgf_function_is_constructor(PgfDB *db, PgfRevision revision,
|
||||
{
|
||||
PGF_API_BEGIN {
|
||||
DB_scope scope(db, READER_SCOPE);
|
||||
ref<PgfPGF> pgf = PgfDB::safe_object2ref<PgfPGF>(revision);
|
||||
ref<PgfPGF> pgf = PgfDB::revision2pgf(revision);
|
||||
|
||||
ref<PgfAbsFun> absfun =
|
||||
namespace_lookup(pgf->abstract.funs, funname);
|
||||
@@ -369,7 +375,7 @@ prob_t pgf_function_prob(PgfDB *db, PgfRevision revision,
|
||||
{
|
||||
PGF_API_BEGIN {
|
||||
DB_scope scope(db, READER_SCOPE);
|
||||
ref<PgfPGF> pgf = PgfDB::safe_object2ref<PgfPGF>(revision);
|
||||
ref<PgfPGF> pgf = PgfDB::revision2pgf(revision);
|
||||
|
||||
ref<PgfAbsFun> absfun =
|
||||
namespace_lookup(pgf->abstract.funs, funname);
|
||||
@@ -426,18 +432,20 @@ PgfType pgf_read_type(PgfText *input, PgfUnmarshaller *u)
|
||||
return res;
|
||||
}
|
||||
|
||||
PGF_API_DECL
|
||||
PGF_API
|
||||
PgfRevision pgf_clone_revision(PgfDB *db, PgfRevision revision,
|
||||
PgfText *name,
|
||||
PgfExn *err)
|
||||
{
|
||||
DB_scope scope(db, WRITER_SCOPE);
|
||||
PGF_API_BEGIN {
|
||||
DB_scope scope(db, WRITER_SCOPE);
|
||||
|
||||
pgf_exn_clear(err);
|
||||
ref<PgfPGF> pgf = PgfDB::revision2pgf(revision);
|
||||
|
||||
try {
|
||||
ref<PgfPGF> pgf = PgfDB::safe_object2ref<PgfPGF>(revision);
|
||||
size_t name_size =
|
||||
(name == NULL) ? pgf->name.size : name->size;
|
||||
|
||||
ref<PgfPGF> new_pgf = PgfDB::malloc<PgfPGF>();
|
||||
ref<PgfPGF> new_pgf = PgfDB::malloc<PgfPGF>(sizeof(PgfPGF)+name_size+1);
|
||||
new_pgf->major_version = pgf->major_version;
|
||||
new_pgf->minor_version = pgf->minor_version;
|
||||
|
||||
@@ -461,15 +469,44 @@ PgfRevision pgf_clone_revision(PgfDB *db, PgfRevision revision,
|
||||
if (pgf->abstract.cats != 0)
|
||||
pgf->abstract.cats->ref_count++;
|
||||
|
||||
new_pgf->prev = 0;
|
||||
new_pgf->next = 0;
|
||||
PgfDB::link_transient_revision(new_pgf);
|
||||
|
||||
memcpy(&new_pgf->name, ((name == NULL) ? &pgf->name : name),
|
||||
sizeof(PgfText)+name_size+1);
|
||||
|
||||
return new_pgf.as_object();
|
||||
} catch (pgf_systemerror& e) {
|
||||
err->type = PGF_EXN_SYSTEM_ERROR;
|
||||
err->code = e.code();
|
||||
err->msg = e.filepath();
|
||||
} catch (pgf_error& e) {
|
||||
err->type = PGF_EXN_PGF_ERROR;
|
||||
err->msg = strdup(e.what());
|
||||
}
|
||||
} PGF_API_END
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
PGF_API
|
||||
void pgf_commit_revision(PgfDB *db, PgfRevision revision,
|
||||
PgfExn *err)
|
||||
{
|
||||
PGF_API_BEGIN {
|
||||
DB_scope scope(db, WRITER_SCOPE);
|
||||
|
||||
ref<PgfPGF> new_pgf = PgfDB::revision2pgf(revision);
|
||||
ref<PgfPGF> old_pgf = PgfDB::get_revision(&new_pgf->name);
|
||||
|
||||
PgfDB::unlink_transient_revision(new_pgf);
|
||||
PgfDB::set_revision(new_pgf);
|
||||
|
||||
PgfDB::link_transient_revision(old_pgf);
|
||||
} PGF_API_END
|
||||
}
|
||||
|
||||
PGF_API
|
||||
PgfRevision pgf_checkout_revision(PgfDB *db, PgfText *name,
|
||||
PgfExn *err)
|
||||
{
|
||||
PGF_API_BEGIN {
|
||||
DB_scope scope(db, WRITER_SCOPE);
|
||||
return PgfDB::get_revision(name).as_object();
|
||||
} PGF_API_END
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -481,14 +518,12 @@ void pgf_create_function(PgfDB *db, PgfRevision revision,
|
||||
PgfMarshaller *m,
|
||||
PgfExn *err)
|
||||
{
|
||||
DB_scope scope(db, WRITER_SCOPE);
|
||||
PGF_API_BEGIN {
|
||||
DB_scope scope(db, WRITER_SCOPE);
|
||||
|
||||
pgf_exn_clear(err);
|
||||
|
||||
try {
|
||||
PgfDBUnmarshaller u(m);
|
||||
|
||||
ref<PgfPGF> pgf = PgfDB::safe_object2ref<PgfPGF>(revision);
|
||||
ref<PgfPGF> pgf = PgfDB::revision2pgf(revision);
|
||||
ref<PgfAbsFun> absfun = PgfDB::malloc<PgfAbsFun>(sizeof(PgfAbsFun)+name->size+1);
|
||||
absfun->type = m->match_type(&u, ty);
|
||||
absfun->arity = 0;
|
||||
@@ -503,12 +538,5 @@ void pgf_create_function(PgfDB *db, PgfRevision revision,
|
||||
namespace_insert(pgf->abstract.funs, absfun);
|
||||
namespace_release(pgf->abstract.funs);
|
||||
pgf->abstract.funs = nmsp;
|
||||
} catch (pgf_systemerror& e) {
|
||||
err->type = PGF_EXN_SYSTEM_ERROR;
|
||||
err->code = e.code();
|
||||
err->msg = e.filepath();
|
||||
} catch (pgf_error& e) {
|
||||
err->type = PGF_EXN_PGF_ERROR;
|
||||
err->msg = strdup(e.what());
|
||||
}
|
||||
} PGF_API_END
|
||||
}
|
||||
|
||||
@@ -318,8 +318,17 @@ PgfType pgf_read_type(PgfText *input, PgfUnmarshaller *u);
|
||||
|
||||
PGF_API_DECL
|
||||
PgfRevision pgf_clone_revision(PgfDB *db, PgfRevision revision,
|
||||
PgfText *name,
|
||||
PgfExn *err);
|
||||
|
||||
PGF_API
|
||||
void pgf_commit_revision(PgfDB *db, PgfRevision revision,
|
||||
PgfExn *err);
|
||||
|
||||
PGF_API_DECL
|
||||
PgfRevision pgf_checkout_revision(PgfDB *db, PgfText *name,
|
||||
PgfExn *err);
|
||||
|
||||
PGF_API_DECL
|
||||
void pgf_create_function(PgfDB *db, PgfRevision revision,
|
||||
PgfText *name,
|
||||
|
||||
@@ -428,7 +428,7 @@ void PgfReader::read_abstract(ref<PgfAbstr> abstract)
|
||||
|
||||
ref<PgfPGF> PgfReader::read_pgf()
|
||||
{
|
||||
ref<PgfPGF> pgf = PgfDB::malloc<PgfPGF>();
|
||||
ref<PgfPGF> pgf = PgfDB::malloc<PgfPGF>(sizeof(PgfPGF)+master.size+1);
|
||||
|
||||
pgf->major_version = read_u16be();
|
||||
pgf->minor_version = read_u16be();
|
||||
@@ -437,5 +437,10 @@ ref<PgfPGF> PgfReader::read_pgf()
|
||||
|
||||
read_abstract(ref<PgfAbstr>::from_ptr(&pgf->abstract));
|
||||
|
||||
pgf->prev = 0;
|
||||
pgf->next = 0;
|
||||
|
||||
memcpy(&pgf->name, &master, sizeof(PgfText)+master.size+1);
|
||||
|
||||
return pgf;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user