Commit 4621b5dc authored by Casualet's avatar Casualet

add

parent b8b07031
......@@ -892,8 +892,7 @@ std::string Analysis::getAnonIndexName(const TableMeta &tm,
}
bool Analysis::isAlias(const std::string &db,
const std::string &table) const
{
const std::string &table) const{
auto db_alias_pair = table_aliases.find(db);
if (table_aliases.end() == db_alias_pair) {
return false;
......
......@@ -257,12 +257,15 @@ private:
class RewritePlan;
/*
*1.A set of functions for manipulating metadata at high level.
*/
class Analysis {
Analysis() = delete;
Analysis(Analysis &&a) = delete;
Analysis &operator=(const Analysis &a) = delete;
Analysis &operator=(Analysis &&a) = delete;
public:
Analysis(const std::string &default_db, const SchemaInfo &schema,
const std::unique_ptr<AES_KEY> &master_key,
......@@ -276,14 +279,13 @@ public:
master_key(analysis.getMasterKey()),
default_sec_rating(analysis.getDefaultSecurityRating()) {}
unsigned int pos; // a counter indicating how many projection
// fields have been analyzed so far
//each field may or may not has a salt field
/*a counter indicating how many projection fields have been analyzed so far*/
unsigned int pos;
/*each field may or may not has a salt field*/
std::map<const FieldMeta *, const salt_type> salts;
//each Item has a rewrite plain, which lists possible way to encrypt this item, that is OLKs!
/*each Item has a rewrite plain, which lists possible way to encrypt this item, that is OLKs!*/
std::map<const Item *, std::unique_ptr<RewritePlan> > rewritePlans;
std::map<std::string, std::map<const std::string, const std::string>>
table_aliases;
std::map<std::string, std::map<const std::string, const std::string>> table_aliases;
std::map<const Item_field *, std::pair<Item_field *, OLK>> item_cache;
// information for decrypting results
......
......@@ -1288,6 +1288,10 @@ static std::string serilize_OnionAdjustExcept(OnionAdjustExcept &e){
// NOTE : This will probably choke on multidatabase queries.
/*
*parse the query, rewrite the query using handlers and then return an executor.
*possibly trigger an onion adjustment.
*/
AbstractQueryExecutor *
Rewriter::dispatchOnLex(Analysis &a, const std::string &query)
{
......@@ -1345,7 +1349,6 @@ Rewriter::dispatchOnLex(Analysis &a, const std::string &query)
out_data = adjustOnion(a, e.o, e.tm, e.fm, e.tolevel);
std::string resadjust = serilize_OnionAdjustExcept(e);
std::cout<<"###################################################**************************************"<<resadjust<<std::endl;
std::vector<std::unique_ptr<Delta> > &deltas = out_data.first;
const std::list<std::string> &adjust_queries = out_data.second;
return new OnionAdjustmentExecutor(std::move(deltas),
......@@ -1360,16 +1363,12 @@ Rewriter::dispatchOnLex(Analysis &a, const std::string &query)
QueryRewrite
Rewriter::rewrite(const std::string &q, const SchemaInfo &schema,
const std::string &default_db, const ProxyState &ps)
{
LOG(cdb_v) << "q " << q;
const std::string &default_db, const ProxyState &ps){
//LOG(cdb_v) << "q " << q;
assert(0 == mysql_thread_init());
Analysis analysis(default_db, schema, ps.getMasterKey(),
ps.defaultSecurityRating());
// NOTE: Care what data you try to read from Analysis
// at this height.
AbstractQueryExecutor *const executor =
Rewriter::dispatchOnLex(analysis, q);
if (!executor) {
......@@ -1385,9 +1384,9 @@ std::string ReturnField::stringify() {
res << " is_salt: " << is_salt << " filed_called " << field_called;
res << " fm " << olk.key << " onion " << olk.o;
res << " salt_pos " << salt_pos;
return res.str();
}
std::string ReturnMeta::stringify() {
std::stringstream res;
res << "rmeta contains " << rfmeta.size() << " elements: \n";
......
......@@ -149,6 +149,22 @@ disconnect(lua_State *const L) {
return 0;
}
/*
*Input: client name, query, thread_id
*client name is used for fetching the proxystate, plan query for rewritting, and thread_id for
*fetching the default db
*Functionality: rewrite the query and get the executor.
There are different types of query:
*SELECT ...:
*INSERT INTO ...:
*DELETE ...:
*UPDATE ...:
*SET ...:
*
*Output: status code. Store QueryRewrite in the wrapperstate
*/
static int
rewrite(lua_State *const L) {
// ANON_REGION(__func__, &perf_cg);
......@@ -169,7 +185,7 @@ rewrite(lua_State *const L) {
const std::string &query = xlua_tolstring(L, 2);
const unsigned long long _thread_id =
strtoull(xlua_tolstring(L, 3).c_str(), NULL, 10);
//this is not used??
c_wrapper->last_query = query;
if (EXECUTE_QUERIES) {
try {
......@@ -177,7 +193,7 @@ rewrite(lua_State *const L) {
&c_wrapper->default_db),
"proxy failed to retrieve default database!");
// save a reference so a second thread won't eat objects
// that DeltaOuput wants later
// that DeltaOuput wants later(a shared_ptr for the schemaInfo)
const std::shared_ptr<const SchemaInfo> &schema =
ps->getSchemaInfo();
c_wrapper->schema_info_refs.push_back(schema);
......@@ -185,8 +201,10 @@ rewrite(lua_State *const L) {
//parse, rewrite, delta, adjust, returnMeta,
std::unique_ptr<QueryRewrite> qr =
std::unique_ptr<QueryRewrite>(new QueryRewrite(
Rewriter::rewrite(query, *schema.get(),
Rewriter::rewrite(query,
*schema.get(),
c_wrapper->default_db, *ps)));
assert(qr);
c_wrapper->setQueryRewrite(std::move(qr));
} catch (const AbstractException &e) {
......@@ -285,8 +303,6 @@ getResTypeFromLuaTable(lua_State *const L, int fields_index,
lua_pop(L, 1);
}
//printrawReturnValue(myRawFromLua);
return ResType(status, lua_tointeger(L, affected_rows_index),
lua_tointeger(L, insert_id_index), std::move(names),
std::move(types), std::move(rows));
......@@ -307,21 +323,26 @@ nilBuffer(lua_State *const L, size_t count)
* */
static void
parseReturnMeta(const ReturnMeta & rtm){
}
/*
*Input: client
*/
static int
next(lua_State *const L) {
/*only one client can call next at a time*/
scoped_lock l(&big_lock);
assert(0 == mysql_thread_init());
//查找client
const std::string client = xlua_tolstring(L, 1);
if (clients.find(client) == clients.end()) {
xlua_pushlstring(L, "error");
xlua_pushlstring(L, "unknown client");
lua_pushinteger(L, 100);
xlua_pushlstring(L, "12345");
nilBuffer(L, 1);
return 5;
}
......@@ -332,13 +353,17 @@ next(lua_State *const L) {
ProxyState *const ps = thread_ps = c_wrapper->ps.get();
assert(ps);
/*???*/
ps->safeCreateEmbeddedTHD();
const ResType &res = getResTypeFromLuaTable(L, 2, 3, 4, 5, 6);
const std::unique_ptr<QueryRewrite> &qr = c_wrapper->getQueryRewrite();
parseReturnMeta(qr->rmeta);
try {
NextParams nparams(*ps, c_wrapper->default_db, c_wrapper->last_query);
......@@ -352,7 +377,7 @@ next(lua_State *const L) {
// > a given killzone will only apply to the next query translation
c_wrapper->setKillZone(qr->kill_zone);
}
switch (result_type) {
switch (result_type){
case AbstractQueryExecutor::ResultType::QUERY_COME_AGAIN: {
// more to do before we have the client's results
xlua_pushlstring(L, "again");
......@@ -391,12 +416,12 @@ next(lua_State *const L) {
xlua_pushlstring(L, e.getMessage());
lua_pushinteger(L, e.getErrorCode());
xlua_pushlstring(L, e.getSQLState());
nilBuffer(L, 1);
return 5;
}
}
static void
returnResultSet(lua_State *const L, const ResType &rd) {
TEST_GenericPacketException(true == rd.ok, "something bad happened");
......@@ -441,6 +466,9 @@ returnResultSet(lua_State *const L, const ResType &rd) {
return;
}
/*Init the lua library*/
static const struct luaL_reg
cryptdb_lib[] = {
#define F(n) { #n, n }
......
OBJDIRS += mysqlproxy
PROXY_SRCS := ConnectWrapper.cc
PROXY_SRCS := ConnectWrapper.cc lua_related.cc
PROXY_OBJS := $(patsubst %.cc,$(OBJDIR)/mysqlproxy/%.o,$(PROXY_SRCS))
all: $(OBJDIR)/libexecute.so
......
#include <lua5.1/lua.hpp>
#include <string>
std::string
xlua_tolstring(lua_State *const, int);
void
xlua_pushlstring(lua_State *const, const std::string &);
std::string
xlua_tolstring(lua_State *const l, int index){
size_t len;
char const *const s = lua_tolstring(l, index, &len);
return std::string(s, len);
}
void
xlua_pushlstring(lua_State *const l, const std::string &s){
lua_pushlstring(l, s.data(), s.length());
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment