1
0
Fork 0

Finish first version of JsonLegend, not activated in Makefile.

This commit is contained in:
Max Neunhoeffer 2014-05-20 13:24:29 +02:00
parent b5df264cdb
commit 0dab3706c5
8 changed files with 179 additions and 25 deletions

View File

@ -38,6 +38,8 @@
"ERROR_CANNOT_CREATE_TEMP_FILE" : { "code" : 20, "message" : "cannot create temporary file" },
"ERROR_REQUEST_CANCELED" : { "code" : 21, "message" : "canceled request" },
"ERROR_DEBUG" : { "code" : 22, "message" : "intentional debug error" },
"ERROR_AID_NOT_FOUND" : { "code" : 23, "message" : "internal error with attribute ID in shaper" },
"ERROR_LEGEND_INCOMPLETE" : { "code" : 24, "message" : "internal error if a legend could not be created" },
"ERROR_HTTP_BAD_PARAMETER" : { "code" : 400, "message" : "bad parameter" },
"ERROR_HTTP_UNAUTHORIZED" : { "code" : 401, "message" : "unauthorized" },
"ERROR_HTTP_FORBIDDEN" : { "code" : 403, "message" : "forbidden" },

View File

@ -38,6 +38,8 @@
"ERROR_CANNOT_CREATE_TEMP_FILE" : { "code" : 20, "message" : "cannot create temporary file" },
"ERROR_REQUEST_CANCELED" : { "code" : 21, "message" : "canceled request" },
"ERROR_DEBUG" : { "code" : 22, "message" : "intentional debug error" },
"ERROR_AID_NOT_FOUND" : { "code" : 23, "message" : "internal error with attribute ID in shaper" },
"ERROR_LEGEND_INCOMPLETE" : { "code" : 24, "message" : "internal error if a legend could not be created" },
"ERROR_HTTP_BAD_PARAMETER" : { "code" : 400, "message" : "bad parameter" },
"ERROR_HTTP_UNAUTHORIZED" : { "code" : 401, "message" : "unauthorized" },
"ERROR_HTTP_FORBIDDEN" : { "code" : 403, "message" : "forbidden" },

View File

@ -25,6 +25,8 @@ ERROR_CANNOT_CREATE_DIRECTORY,19,"cannot create directory","Will be raised when
ERROR_CANNOT_CREATE_TEMP_FILE,20,"cannot create temporary file","Will be raised when an attempt to create a temporary file fails."
ERROR_REQUEST_CANCELED,21,"canceled request","Will be raised when a request is canceled by the user."
ERROR_DEBUG,22,"intentional debug error","Will be raised intentionally during debugging."
ERROR_AID_NOT_FOUND,23,"internal error with attribute ID in shaper","Will be raised if an attribute ID is not found in the shaper but should have been."
ERROR_LEGEND_INCOMPLETE,24,"internal error if a legend could not be created","Will be raised if the legend generator was only given access to the shape and some sids are in the data object (inhomogeneous lists)."
################################################################################
## HTTP standard errors

View File

@ -34,6 +34,8 @@ void TRI_InitialiseErrorMessages (void) {
REG_ERROR(ERROR_CANNOT_CREATE_TEMP_FILE, "cannot create temporary file");
REG_ERROR(ERROR_REQUEST_CANCELED, "canceled request");
REG_ERROR(ERROR_DEBUG, "intentional debug error");
REG_ERROR(ERROR_AID_NOT_FOUND, "internal error with attribute ID in shaper");
REG_ERROR(ERROR_LEGEND_INCOMPLETE, "internal error if a legend could not be created");
REG_ERROR(ERROR_HTTP_BAD_PARAMETER, "bad parameter");
REG_ERROR(ERROR_HTTP_UNAUTHORIZED, "unauthorized");
REG_ERROR(ERROR_HTTP_FORBIDDEN, "forbidden");

View File

@ -57,6 +57,12 @@ extern "C" {
/// Will be raised when a request is canceled by the user.
/// - 22: @LIT{intentional debug error}
/// Will be raised intentionally during debugging.
/// - 23: @LIT{internal error with attribute ID in shaper}
/// Will be raised if an attribute ID is not found in the shaper but should
/// have been.
/// - 24: @LIT{internal error if a legend could not be created}
/// Will be raised if the legend generator was only given access to the shape
/// and some sids are in the data object (inhomogeneous lists).
/// - 400: @LIT{bad parameter}
/// Will be raised when the HTTP request does not fulfill the requirements.
/// - 401: @LIT{unauthorized}
@ -760,6 +766,28 @@ void TRI_InitialiseErrorMessages (void);
#define TRI_ERROR_DEBUG (22)
////////////////////////////////////////////////////////////////////////////////
/// @brief 23: ERROR_AID_NOT_FOUND
///
/// internal error with attribute ID in shaper
///
/// Will be raised if an attribute ID is not found in the shaper but should
/// have been.
////////////////////////////////////////////////////////////////////////////////
#define TRI_ERROR_AID_NOT_FOUND (23)
////////////////////////////////////////////////////////////////////////////////
/// @brief 24: ERROR_LEGEND_INCOMPLETE
///
/// internal error if a legend could not be created
///
/// Will be raised if the legend generator was only given access to the shape
/// and some sids are in the data object (inhomogeneous lists).
////////////////////////////////////////////////////////////////////////////////
#define TRI_ERROR_LEGEND_INCOMPLETE (24)
////////////////////////////////////////////////////////////////////////////////
/// @brief 400: ERROR_HTTP_BAD_PARAMETER
///

View File

@ -89,7 +89,6 @@ lib_libarango_a_SOURCES = \
lib/Rest/InitialiseRest.cpp \
lib/Rest/SslInterface.cpp \
lib/Rest/Version.cpp \
lib/ShapedJson/Legends.cpp \
lib/ShapedJson/json-shaper.cpp \
lib/ShapedJson/shape-accessor.cpp \
lib/ShapedJson/shaped-json.cpp \

View File

@ -42,30 +42,34 @@ void JsonLegend::clear () {
_shape_data.clear();
}
bool JsonLegend::addAttributeId (TRI_shape_aid_t aid) {
int JsonLegend::addAttributeId (TRI_shape_aid_t aid) {
unordered_set<TRI_shape_aid_t>::const_iterator it = _have_attribute.find(aid);
if (it != _have_attribute.end()) {
return true;
return TRI_ERROR_NO_ERROR;
}
char const* p = _shaper->lookupAttributeId(_shaper, aid);
if (0 == p) {
return false;
return TRI_ERROR_AID_NOT_FOUND;
}
_have_attribute.insert(aid);
size_t len = strlen(p);
_attribs.emplace_back(aid, _att_data.length());
_att_data.appendText(p, len+1); // including the zero byte
return true;
return TRI_ERROR_NO_ERROR;
}
bool JsonLegend::addShape (TRI_shape_sid_t sid,
char const* data, uint32_t len) {
int JsonLegend::addShape (TRI_shape_sid_t sid,
char const* data, uint32_t len) {
// data can be 0, then no data is associated, note that if the shape
// contains an inhomogeneous list as one of its subobjects, then the
// shape legend could be incomplete, because the actual shapes of
// the subobject(s) are held only in the data and not in the shaper.
// In this case this method includes all shapes it can but then
// returns TRI_ERROR_LEGEND_INCOMPLETE.
int res = TRI_ERROR_NO_ERROR;
TRI_shape_t const* shape = 0;
@ -74,13 +78,13 @@ bool JsonLegend::addShape (TRI_shape_sid_t sid,
shape = TRI_LookupSidBasicShapeShaper(sid);
}
else {
shape = _shaper->lookupShapeId(_shaper, sid);
if (0 == shape) {
return TRI_ERROR_LEGEND_INCOMPLETE;
}
unordered_set<TRI_shape_sid_t>::const_iterator it = _have_shape.find(sid);
if (it == _have_shape.end()) {
TRI_shape_t const* shape = _shaper->lookupShapeId(_shaper, sid);
if (0 == shape) {
return false;
}
_have_shape.insert(sid);
Shape sh(sid, _shape_data.length(), shape->_size);
_shapes.push_back(sh);
@ -92,11 +96,16 @@ bool JsonLegend::addShape (TRI_shape_sid_t sid,
// Now we have to add all attribute IDs and all shapes used by this
// one recursively, note that the data of this object is in a
// consistent state, such that we can call ourselves recursively.
if (shape->_type == TRI_SHAPE_HOMOGENEOUS_SIZED_LIST) {
// Handle a homogeneous list with equal size entries:
// Subobjects have fixed size, so in particular no subobject can
// contain any inhomogeneous list as one of its subobjects.
// ...
// contain any inhomogeneous list as one of its subobjects,
// therefore we do not have to hand down actual shaped JSON data.
TRI_homogeneous_sized_list_shape_t const* shape_spec
= reinterpret_cast<TRI_homogeneous_sized_list_shape_t const*>
(shape);
res = addShape(shape_spec->_sidEntry, 0, 0);
}
else if (shape->_type == TRI_SHAPE_HOMOGENEOUS_LIST) {
// Handle a homogeneous list:
@ -104,31 +113,128 @@ bool JsonLegend::addShape (TRI_shape_sid_t sid,
// inhomogeneous list. We first scan the shape without data, if this
// goes well, there was no subshape containing an inhomogeneous
// list! Otherwise, we have to scan all entries of the list.
// ...
TRI_homogeneous_list_shape_t const* shape_spec
= reinterpret_cast<TRI_homogeneous_list_shape_t const*>
(shape);
res = addShape(shape_spec->_sidEntry, 0, 0);
if (res == TRI_ERROR_LEGEND_INCOMPLETE) {
// The subdocuments contain inhomogeneous lists, so we have to
// scan them all:
res = TRI_ERROR_NO_ERROR; // just in case the length is 0
TRI_shape_length_list_t const* len
= reinterpret_cast<TRI_shape_length_list_t const*>(data);
TRI_shape_size_t const* offsets
= reinterpret_cast<TRI_shape_size_t const*>(len+1);
TRI_shape_length_list_t i;
for (i = 0;i < *len;i++) {
res = addShape(shape_spec->_sidEntry, data + offsets[i],
offsets[i+1]-offsets[i]);
if (res != TRI_ERROR_NO_ERROR) {
break;
}
}
}
}
else if (shape->_type == TRI_SHAPE_LIST) {
// Handle an inhomogeneous list:
// We have to scan recursively all entries of the list since they
// contain sids in the data area.
// ...
TRI_shape_length_list_t const* len
= reinterpret_cast<TRI_shape_length_list_t const*>(data);
TRI_shape_sid_t const* sids
= reinterpret_cast<TRI_shape_sid_t const*>(len+1);
TRI_shape_size_t const* offsets
= reinterpret_cast<TRI_shape_size_t const*>(sids + *len);
TRI_shape_length_list_t i;
for (i = 0;i < *len;i++) {
res = addShape(sids[i], data + offsets[i], offsets[i+1]-offsets[i]);
if (res != TRI_ERROR_NO_ERROR) {
break;
}
}
}
else if (shape->_type == TRI_SHAPE_ARRAY) {
// Handle an array ...
// Handle an array:
// Distinguish between fixed size subobjects and variable size
// subobjects. The fixed ones cannot contain inhomogeneous lists.
// ...
TRI_array_shape_t const* shape_spec
= reinterpret_cast<TRI_array_shape_t const*> (shape);
TRI_shape_sid_t const* sids
= reinterpret_cast<TRI_shape_sid_t const*>(shape_spec+1);
TRI_shape_aid_t const* aids
= reinterpret_cast<TRI_shape_aid_t const*>
(sids + (shape_spec->_fixedEntries + shape_spec->_variableEntries));
TRI_shape_size_t const* offsets
= reinterpret_cast<TRI_shape_size_t const*>
(aids + (shape_spec->_fixedEntries + shape_spec->_variableEntries));
uint64_t i;
for (i = 0; res == TRI_ERROR_NO_ERROR &&
i < shape_spec->_fixedEntries + shape_spec->_variableEntries;
i++) {
res = addAttributeId(aids[i]);
}
for (i = 0; res == TRI_ERROR_NO_ERROR && i < shape_spec->_fixedEntries;
i++) {
// Fixed size subdocs cannot have inhomogeneous lists as subdocs:
res = addShape(sids[i], 0, 0);
}
for (i = 0; res == TRI_ERROR_NO_ERROR && i < shape_spec->_variableEntries;
i++) {
addShape(sids[i + shape_spec->_fixedEntries],
data + offsets[i], offsets[i+1] - offsets[i]);
}
}
return true;
return res;
}
static inline TRI_shape_size_t roundup8(TRI_shape_size_t x) {
return (x + 7) - ((x + 7) & 7);
}
size_t JsonLegend::getSize () {
// Add string pool size and shape pool size and add space for header
// and tables.
return 0;
// and tables in bytes.
return sizeof(TRI_shape_size_t) // number of aids
+ sizeof(AttributeId) * _attribs.size() // aid entries
+ sizeof(TRI_shape_size_t) // number of sids
+ sizeof(Shape) * _shapes.size() // sid entries
+ roundup8(_att_data.length()) // string data, padded
+ roundup8(_shape_data.length()); // shape data, padded
}
void JsonLegend::dump (void* buf) {
// Dump the resulting legend to a given buffer.
// First sort the aids in ascending order:
sort(_attribs.begin(), _attribs.end(), AttributeComparerObject);
// Then sort the sids in ascending order:
sort(_shapes.begin(), _shapes.end(), ShapeComparerObject);
TRI_shape_size_t* p = reinterpret_cast<TRI_shape_size_t*>(buf);
TRI_shape_size_t i;
*p++ = _attribs.size();
AttributeId* a = reinterpret_cast<AttributeId*>(p);
for (i = 0; i < _attribs.size(); i++) {
*a++ = _attribs[i];
}
p = reinterpret_cast<TRI_shape_size_t*>(a);
*p++ = _shapes.size();
Shape* s = reinterpret_cast<Shape*>(p);
for (i = 0; i < _shapes.size(); i++) {
*s++ = _shapes[i];
}
char* c = reinterpret_cast<char*>(s);
memcpy(c, _att_data.c_str(), _att_data.length());
i = roundup8(_att_data.length());
if (i > _att_data.length()) {
memset( c + _att_data.length(), 0, i-_att_data.length());
}
c += i;
memcpy(c, _shape_data.c_str(), _shape_data.length());
i = roundup8(_shape_data.length());
if (i > _shape_data.length()) {
memset( c + _shape_data.length(), 0, i-_shape_data.length());
}
}

View File

@ -60,18 +60,18 @@ namespace triagens {
void clear ();
bool addAttributeId (TRI_shape_aid_t aid);
int addAttributeId (TRI_shape_aid_t aid);
bool addShape (TRI_shaped_json_t const* sh_json) {
int addShape (TRI_shaped_json_t const* sh_json) {
return addShape(sh_json->_sid, sh_json->_data.data,
sh_json->_data.length);
}
bool addShape (TRI_shape_sid_t sid, TRI_blob_t const* blob) {
int addShape (TRI_shape_sid_t sid, TRI_blob_t const* blob) {
return addShape(sid, blob->data, blob->length);
}
bool addShape (TRI_shape_sid_t sid, char const* data, uint32_t len);
int addShape (TRI_shape_sid_t sid, char const* data, uint32_t len);
size_t getSize ();
@ -90,6 +90,12 @@ namespace triagens {
};
static struct AttributeComparerClass {
bool operator() (AttributeId const& a, AttributeId const& b) {
return a.aid < b.aid;
}
} AttributeComparerObject;
struct Shape {
TRI_shape_sid_t sid;
TRI_shape_size_t offset;
@ -100,12 +106,19 @@ namespace triagens {
};
static struct ShapeComparerClass {
bool operator() (Shape const& a, Shape const& b) {
return a.sid < b.sid;
}
} ShapeComparerObject;
unordered_set<TRI_shape_aid_t> _have_attribute;
vector<AttributeId> _attribs;
StringBuffer _att_data;
unordered_set<TRI_shape_sid_t> _have_shape;
vector<Shape> _shapes;
StringBuffer _shape_data;
};
}