1
0
Fork 0
arangodb/lib/ShapedJson/Legends.h

224 lines
7.9 KiB
C++

////////////////////////////////////////////////////////////////////////////////
/// @brief legends for shaped JSON objects to make them self-contained
///
/// @file
/// Declaration for legends.
///
/// DISCLAIMER
///
/// Copyright 2004-2014 triAGENS GmbH, Cologne, Germany
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Copyright holder is triAGENS GmbH, Cologne, Germany
///
/// @author Max Neunhoeffer
/// @author Copyright 2014-2014, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////
#ifndef TRIAGENS_SHAPED_JSON_LEGENDS_H
#define TRIAGENS_SHAPED_JSON_LEGENDS_H 1
#include "Basics/Common.h"
#include "BasicsC/structures.h"
#include "Basics/StringBuffer.h"
#include "ShapedJson/shaped-json.h"
#include "ShapedJson/json-shaper.h"
namespace triagens {
namespace basics {
////////////////////////////////////////////////////////////////////////////////
/// @brief create a legend for one or more shaped json objects
////////////////////////////////////////////////////////////////////////////////
class JsonLegend {
////////////////////////////////////////////////////////////////////////////////
/// @brief disable default constructor because we need a shaper
////////////////////////////////////////////////////////////////////////////////
JsonLegend () = delete;
public:
////////////////////////////////////////////////////////////////////////////////
/// @brief constructor, taking a shaper
////////////////////////////////////////////////////////////////////////////////
JsonLegend (TRI_shaper_t* shaper)
: _shaper(shaper), _att_data(TRI_UNKNOWN_MEM_ZONE),
_shape_data(TRI_UNKNOWN_MEM_ZONE) {
}
////////////////////////////////////////////////////////////////////////////////
/// @brief destructor
////////////////////////////////////////////////////////////////////////////////
~JsonLegend () {
};
////////////////////////////////////////////////////////////////////////////////
/// @brief clear all data and register a new shaper
////////////////////////////////////////////////////////////////////////////////
void reset (TRI_shaper_t* shaper) {
clear();
_shaper = shaper;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief clear all data to build a new legend, keep shaper
////////////////////////////////////////////////////////////////////////////////
void clear ();
////////////////////////////////////////////////////////////////////////////////
/// @brief add an attribute ID to the legend
////////////////////////////////////////////////////////////////////////////////
int addAttributeId (TRI_shape_aid_t aid);
////////////////////////////////////////////////////////////////////////////////
/// @brief add a shape to the legend
////////////////////////////////////////////////////////////////////////////////
int addShape (TRI_shaped_json_t const* sh_json) {
return addShape(sh_json->_sid, sh_json->_data.data,
sh_json->_data.length);
}
int addShape (TRI_shape_sid_t sid, TRI_blob_t const* blob) {
return addShape(sid, blob->data, blob->length);
}
int addShape (TRI_shape_sid_t sid, char const* data, uint32_t len);
////////////////////////////////////////////////////////////////////////////////
/// @brief get the total size in bytes of the legend
////////////////////////////////////////////////////////////////////////////////
size_t getSize () const;
////////////////////////////////////////////////////////////////////////////////
/// @brief dump the legend to the buffer pointed to by buf
////////////////////////////////////////////////////////////////////////////////
void dump (void* buf);
private:
////////////////////////////////////////////////////////////////////////////////
/// @brief the underlying shaper
////////////////////////////////////////////////////////////////////////////////
TRI_shaper_t* _shaper;
////////////////////////////////////////////////////////////////////////////////
/// @brief one entry in the table of attribute IDs
////////////////////////////////////////////////////////////////////////////////
struct AttributeId {
TRI_shape_aid_t aid;
TRI_shape_size_t offset;
AttributeId (TRI_shape_aid_t a, TRI_shape_size_t o)
: aid(a), offset(o) {}
};
////////////////////////////////////////////////////////////////////////////////
/// @brief used to sort attribute ID table by attribute ID
////////////////////////////////////////////////////////////////////////////////
static struct AttributeComparerClass {
bool operator() (AttributeId const& a, AttributeId const& b) {
return a.aid < b.aid;
}
} AttributeComparerObject;
////////////////////////////////////////////////////////////////////////////////
/// @brief one entry in the table of shapes
////////////////////////////////////////////////////////////////////////////////
struct Shape {
TRI_shape_sid_t sid;
TRI_shape_size_t offset;
TRI_shape_size_t size;
Shape (TRI_shape_sid_t sid, TRI_shape_size_t o, TRI_shape_size_t siz)
: sid(sid), offset(o), size(siz) {}
};
////////////////////////////////////////////////////////////////////////////////
/// @brief used to sort shapes by shape ID
////////////////////////////////////////////////////////////////////////////////
static struct ShapeComparerClass {
bool operator() (Shape const& a, Shape const& b) {
return a.sid < b.sid;
}
} ShapeComparerObject;
////////////////////////////////////////////////////////////////////////////////
/// @brief remember which aids we already have
////////////////////////////////////////////////////////////////////////////////
unordered_set<TRI_shape_aid_t> _have_attribute;
////////////////////////////////////////////////////////////////////////////////
/// @brief table of attribute IDs
////////////////////////////////////////////////////////////////////////////////
vector<AttributeId> _attribs;
////////////////////////////////////////////////////////////////////////////////
/// @brief here we collect the string data for the attributes
////////////////////////////////////////////////////////////////////////////////
StringBuffer _att_data;
////////////////////////////////////////////////////////////////////////////////
/// @brief remember which sids we already have
////////////////////////////////////////////////////////////////////////////////
unordered_set<TRI_shape_sid_t> _have_shape;
////////////////////////////////////////////////////////////////////////////////
/// @brief table of shapes
////////////////////////////////////////////////////////////////////////////////
vector<Shape> _shapes;
////////////////////////////////////////////////////////////////////////////////
/// @brief here we collect the actual shape data
////////////////////////////////////////////////////////////////////////////////
StringBuffer _shape_data;
};
}
}
#endif
// Local Variables:
// mode: outline-minor
// outline-regexp: "/// @brief\\|/// {@inheritDoc}\\|/// @addtogroup\\|/// @page\\|// --SECTION--\\|/// @\\}"
// End: