2012-01-19 16:49:54 +00:00
|
|
|
/*
|
|
|
|
* LSMServerHandler.cc
|
|
|
|
*
|
|
|
|
* Copyright 2011-2012 Yahoo! Inc.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Author: sears
|
|
|
|
*/
|
2011-05-11 22:27:48 +00:00
|
|
|
#include <stasis/transactional.h>
|
|
|
|
#include <stasis/logger/safeWrites.h>
|
|
|
|
|
2011-05-12 00:30:02 +00:00
|
|
|
#include <iostream>
|
2011-05-11 22:27:48 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include "merger.h"
|
|
|
|
#include "logstore.h"
|
|
|
|
#include "LSMServerHandler.h"
|
|
|
|
|
2011-11-04 20:50:50 +00:00
|
|
|
int blind_update = 0; // updates check preimage by default.
|
|
|
|
|
2012-02-11 01:55:24 +00:00
|
|
|
FILE* trace = 0;
|
|
|
|
|
2011-05-11 22:27:48 +00:00
|
|
|
LSMServerHandler::
|
|
|
|
LSMServerHandler(int argc, char **argv)
|
|
|
|
{
|
|
|
|
signal(SIGPIPE, SIG_IGN);
|
|
|
|
|
|
|
|
// how big the in-memory tree should be (512MB).
|
|
|
|
int64_t c0_size = 1024 * 1024 * 512 * 1;
|
|
|
|
// write-ahead log
|
|
|
|
// 1 -> sync on each commit
|
|
|
|
// 2 -> sync on each 2 commits
|
|
|
|
// ...
|
|
|
|
int log_mode = 0; // do not log by default.
|
|
|
|
int64_t expiry_delta = 0; // do not gc by default
|
2012-01-13 00:13:33 +00:00
|
|
|
port = 9090;
|
2012-02-11 01:55:24 +00:00
|
|
|
char * tracefile = 0;
|
2011-05-11 22:27:48 +00:00
|
|
|
stasis_buffer_manager_size = 1 * 1024 * 1024 * 1024 / PAGE_SIZE; // 1.5GB total
|
|
|
|
|
|
|
|
for(int i = 1; i < argc; i++) {
|
|
|
|
if(!strcmp(argv[i], "--test")) {
|
|
|
|
stasis_buffer_manager_size = 3 * 1024 * 1024 * 128 / PAGE_SIZE; // 228MB total
|
|
|
|
c0_size = 1024 * 1024 * 100;
|
|
|
|
printf("warning: running w/ tiny c0 for testing\n"); // XXX build a separate test server and deployment server?
|
|
|
|
} else if(!strcmp(argv[i], "--benchmark")) {
|
2011-11-04 20:50:50 +00:00
|
|
|
stasis_buffer_manager_size = (1024LL * 1024LL * 1024LL * 2LL) / PAGE_SIZE; // 10GB total
|
|
|
|
c0_size = 1024LL * 1024LL * 1024LL * 8LL;
|
|
|
|
printf("note: running w/ 10GB of memory for benchmarking\n"); // XXX build a separate test server and deployment server?
|
2012-01-09 19:29:17 +00:00
|
|
|
} else if(!strcmp(argv[i], "--benchmark-small")) {
|
|
|
|
stasis_buffer_manager_size = (1024LL * 1024LL * 1024LL * 2LL) / PAGE_SIZE; // 5GB total
|
|
|
|
c0_size = 1024LL * 1024LL * 1024LL * 3LL;
|
|
|
|
printf("note: running w/ 5GB of memory for benchmarking on small box\n");
|
2011-05-11 22:27:48 +00:00
|
|
|
} else if(!strcmp(argv[i], "--log-mode")) {
|
|
|
|
i++;
|
|
|
|
log_mode = atoi(argv[i]);
|
2012-01-13 00:13:33 +00:00
|
|
|
} else if(!strcmp(argv[i], "--port")) {
|
|
|
|
i++;
|
|
|
|
port = atoi(argv[i]);
|
2012-02-11 01:55:24 +00:00
|
|
|
} else if(!strcmp(argv[i], "--trace")) {
|
|
|
|
i++;
|
|
|
|
tracefile = argv[i];
|
2012-01-13 00:13:33 +00:00
|
|
|
} else if(!strcmp(argv[i], "--blind-update")) {
|
|
|
|
blind_update = 1;
|
2011-05-11 22:27:48 +00:00
|
|
|
} else if(!strcmp(argv[i], "--expiry-delta")) {
|
|
|
|
i++;
|
|
|
|
expiry_delta = atoi(argv[i]);
|
|
|
|
} else {
|
2012-01-09 19:29:17 +00:00
|
|
|
fprintf(stderr, "Usage: %s [--test|--benchmark|--benchmark-small] [--log-mode <int>] [--expiry-delta <int>]", argv[0]);
|
2011-05-11 22:27:48 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-11 01:55:24 +00:00
|
|
|
if(tracefile) {
|
|
|
|
trace = fopen(tracefile, "w");
|
|
|
|
if(trace == 0) {
|
|
|
|
perror("Couldn't open trace file!");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-12 20:41:18 +00:00
|
|
|
pthread_mutex_init(&mutex_, 0);
|
2011-06-09 01:43:46 +00:00
|
|
|
logtable::init_stasis();
|
2011-05-11 22:27:48 +00:00
|
|
|
|
|
|
|
int xid = Tbegin();
|
|
|
|
|
|
|
|
|
|
|
|
recordid table_root = ROOT_RECORD;
|
|
|
|
{
|
2011-06-09 01:43:46 +00:00
|
|
|
ltable_ = new logtable(log_mode, c0_size);
|
2011-05-11 22:27:48 +00:00
|
|
|
ltable_->expiry = expiry_delta;
|
|
|
|
|
|
|
|
if(TrecordType(xid, ROOT_RECORD) == INVALID_SLOT) {
|
|
|
|
printf("Creating empty logstore\n");
|
|
|
|
table_root = ltable_->allocTable(xid);
|
|
|
|
assert(table_root.page == ROOT_RECORD.page &&
|
|
|
|
table_root.slot == ROOT_RECORD.slot);
|
|
|
|
} else {
|
|
|
|
printf("Opened existing logstore\n");
|
|
|
|
table_root.size = TrecordSize(xid, ROOT_RECORD);
|
|
|
|
ltable_->openTable(xid, table_root);
|
|
|
|
}
|
|
|
|
|
|
|
|
Tcommit(xid);
|
|
|
|
merge_scheduler * mscheduler = new merge_scheduler(ltable_);
|
|
|
|
mscheduler->start();
|
|
|
|
ltable_->replayLog();
|
|
|
|
|
|
|
|
/*
|
|
|
|
printf("Stopping merge threads...\n");
|
|
|
|
mscheduler->shutdown();
|
|
|
|
delete mscheduler;
|
|
|
|
|
|
|
|
printf("Deinitializing stasis...\n");
|
|
|
|
fflush(stdout);
|
|
|
|
*/
|
|
|
|
}
|
2011-06-09 01:43:46 +00:00
|
|
|
//logtable::deinit_stasis();
|
2011-05-12 20:41:18 +00:00
|
|
|
initNextDatabaseId();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LSMServerHandler::
|
|
|
|
initNextDatabaseId()
|
|
|
|
{
|
2011-05-12 00:30:02 +00:00
|
|
|
nextDatabaseId_ = 1;
|
2011-05-12 20:41:18 +00:00
|
|
|
uint32_t id = 0;
|
|
|
|
datatuple* start = buildTuple(id, "");
|
|
|
|
datatuple* end = buildTuple(id + 1, "");
|
2011-06-09 01:43:46 +00:00
|
|
|
logtable::iterator* itr = new logtable::iterator(ltable_, start);
|
2011-05-12 20:41:18 +00:00
|
|
|
datatuple* current;
|
|
|
|
while ((current = itr->getnext())) {
|
|
|
|
// are we at the end of range?
|
|
|
|
if (datatuple::compare_obj(current, end) >= 0) {
|
|
|
|
datatuple::freetuple(current);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
uint32_t currentId = *((uint32_t*)(current->data()));
|
|
|
|
if (currentId > nextDatabaseId_) {
|
|
|
|
nextDatabaseId_ = currentId;
|
|
|
|
}
|
|
|
|
datatuple::freetuple(current);
|
|
|
|
}
|
|
|
|
nextDatabaseId_++;
|
|
|
|
delete itr;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t LSMServerHandler::
|
|
|
|
nextDatabaseId()
|
|
|
|
{
|
|
|
|
uint32_t id;
|
|
|
|
pthread_mutex_lock(&mutex_);
|
|
|
|
nextDatabaseId_++;
|
|
|
|
id = nextDatabaseId_;
|
|
|
|
pthread_mutex_unlock(&mutex_);
|
|
|
|
return id;
|
2011-05-11 22:27:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ResponseCode::type LSMServerHandler::
|
|
|
|
ping()
|
|
|
|
{
|
2012-02-11 01:55:24 +00:00
|
|
|
if(trace) { fprintf(trace, "Success = ping()\n"); fflush(trace); }
|
2011-08-23 00:10:43 +00:00
|
|
|
return mapkeeper::ResponseCode::Success;
|
2011-05-11 22:27:48 +00:00
|
|
|
}
|
|
|
|
|
2011-06-08 23:58:29 +00:00
|
|
|
ResponseCode::type LSMServerHandler::
|
|
|
|
shutdown()
|
|
|
|
{
|
2012-02-11 01:55:24 +00:00
|
|
|
if(trace) { fprintf(trace, "Success = shutdown()\n"); fflush(trace); }
|
2011-06-08 23:58:29 +00:00
|
|
|
exit(0); // xxx hack
|
2011-08-23 00:10:43 +00:00
|
|
|
return mapkeeper::ResponseCode::Success;
|
2011-06-08 23:58:29 +00:00
|
|
|
}
|
2012-02-11 01:55:24 +00:00
|
|
|
std::string pp_tuple(datatuple * tuple) {
|
|
|
|
std::string key((const char*)tuple->rawkey(), (size_t)tuple->rawkeylen());
|
|
|
|
return key;
|
|
|
|
}
|
2011-05-11 22:27:48 +00:00
|
|
|
ResponseCode::type LSMServerHandler::
|
2011-05-12 00:30:02 +00:00
|
|
|
insert(datatuple* tuple)
|
2011-05-11 22:27:48 +00:00
|
|
|
{
|
2011-05-12 00:30:02 +00:00
|
|
|
ltable_->insertTuple(tuple);
|
2011-05-16 21:16:36 +00:00
|
|
|
datatuple::freetuple(tuple);
|
2011-08-23 00:10:43 +00:00
|
|
|
return mapkeeper::ResponseCode::Success;
|
2011-05-11 22:27:48 +00:00
|
|
|
}
|
|
|
|
|
2011-05-12 00:30:02 +00:00
|
|
|
ResponseCode::type LSMServerHandler::
|
2011-08-23 00:10:43 +00:00
|
|
|
addMap(const std::string& databaseName)
|
2011-05-12 00:30:02 +00:00
|
|
|
{
|
2011-05-12 20:41:18 +00:00
|
|
|
uint32_t id = nextDatabaseId();
|
|
|
|
datatuple* tup = buildTuple(0, databaseName, (void*)&id, (uint32_t)(sizeof(id)));
|
|
|
|
datatuple* ret = get(tup);
|
|
|
|
if (ret) {
|
|
|
|
datatuple::freetuple(ret);
|
2012-02-11 01:55:24 +00:00
|
|
|
if(trace) { fprintf(trace, "MapExists = addMap(%s)\n", databaseName.c_str()); fflush(trace); }
|
2011-08-23 00:10:43 +00:00
|
|
|
return mapkeeper::ResponseCode::MapExists;
|
2011-05-12 20:41:18 +00:00
|
|
|
}
|
2012-02-11 01:55:24 +00:00
|
|
|
if(trace) { fprintf(trace, "Success = addMap(%s)\n", databaseName.c_str()); fflush(trace); }
|
2011-05-12 00:30:02 +00:00
|
|
|
return insert(tup);
|
|
|
|
}
|
|
|
|
|
2011-05-11 22:27:48 +00:00
|
|
|
ResponseCode::type LSMServerHandler::
|
2011-08-23 00:10:43 +00:00
|
|
|
dropMap(const std::string& databaseName)
|
2011-05-11 22:27:48 +00:00
|
|
|
{
|
2012-01-09 19:29:17 +00:00
|
|
|
uint32_t id = getDatabaseId(databaseName);
|
|
|
|
if(id == 0) {
|
2012-02-11 01:55:24 +00:00
|
|
|
if(trace) { fprintf(trace, "MapNotFound = dropMap(%s)\n", databaseName.c_str()); fflush(trace); }
|
2012-01-09 19:29:17 +00:00
|
|
|
return mapkeeper::ResponseCode::MapNotFound;
|
|
|
|
}
|
|
|
|
datatuple * tup = buildTuple(0, databaseName);
|
|
|
|
datatuple * exists = get(tup);
|
|
|
|
|
|
|
|
if(exists) {
|
|
|
|
datatuple::freetuple(exists);
|
|
|
|
|
|
|
|
datatuple * startKey = buildTuple(id, "");
|
|
|
|
logtable::iterator * itr = new logtable::iterator(ltable_, startKey);
|
|
|
|
datatuple::freetuple(startKey);
|
|
|
|
datatuple * current;
|
|
|
|
|
|
|
|
// insert tombstone; deletes metadata entry for map; frees tup
|
|
|
|
insert(tup);
|
|
|
|
|
|
|
|
while(NULL != (current = itr->getnext())) {
|
|
|
|
if(*((uint32_t*)current->strippedkey()) != id) {
|
|
|
|
datatuple::freetuple(current);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
datatuple * del = datatuple::create(current->strippedkey(), current->strippedkeylen());
|
|
|
|
ltable_->insertTuple(del);
|
|
|
|
datatuple::freetuple(del);
|
|
|
|
datatuple::freetuple(current);
|
2011-05-11 22:27:48 +00:00
|
|
|
}
|
2012-01-09 19:29:17 +00:00
|
|
|
delete itr;
|
2012-02-11 01:55:24 +00:00
|
|
|
if(trace) { fprintf(trace, "Success = dropMap(%s)\n", databaseName.c_str()); fflush(trace); }
|
2012-01-09 19:29:17 +00:00
|
|
|
return mapkeeper::ResponseCode::Success;
|
|
|
|
} else {
|
|
|
|
datatuple::freetuple(tup);
|
2012-02-11 01:55:24 +00:00
|
|
|
if(trace) { fprintf(trace, "MapNotFound = dropMap(%s)\n", databaseName.c_str()); fflush(trace); }
|
2012-01-09 19:29:17 +00:00
|
|
|
return mapkeeper::ResponseCode::MapNotFound;
|
|
|
|
}
|
2011-05-11 22:27:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LSMServerHandler::
|
2011-08-23 00:10:43 +00:00
|
|
|
listMaps(StringListResponse& _return)
|
2011-05-11 22:27:48 +00:00
|
|
|
{
|
2012-01-09 19:29:17 +00:00
|
|
|
datatuple * startKey = buildTuple(0, "");
|
|
|
|
logtable::iterator * itr = new logtable::iterator(ltable_, startKey);
|
|
|
|
datatuple::freetuple(startKey);
|
|
|
|
datatuple * current;
|
|
|
|
while(NULL != (current = itr->getnext())) {
|
|
|
|
if(*((uint32_t*)current->strippedkey()) != 0) {
|
|
|
|
datatuple::freetuple(current);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_return.values.push_back(
|
|
|
|
std::string((char*)(current->strippedkey()) + sizeof(uint32_t),
|
|
|
|
current->strippedkeylen() - sizeof(uint32_t)));
|
|
|
|
datatuple::freetuple(current);
|
|
|
|
}
|
|
|
|
delete itr;
|
2012-02-11 01:55:24 +00:00
|
|
|
if(trace) { fprintf(trace, "... = listMaps()\n"); fflush(trace); }
|
2012-01-09 19:29:17 +00:00
|
|
|
_return.responseCode = mapkeeper::ResponseCode::Success;
|
2011-05-11 22:27:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LSMServerHandler::
|
|
|
|
scan(RecordListResponse& _return, const std::string& databaseName, const ScanOrder::type order,
|
|
|
|
const std::string& startKey, const bool startKeyIncluded,
|
|
|
|
const std::string& endKey, const bool endKeyIncluded,
|
|
|
|
const int32_t maxRecords, const int32_t maxBytes)
|
|
|
|
{
|
2011-05-12 20:41:18 +00:00
|
|
|
uint32_t id = getDatabaseId(databaseName);
|
|
|
|
if (id == 0) {
|
|
|
|
// database not found
|
2012-02-11 01:55:24 +00:00
|
|
|
if(trace) { fprintf(trace, "MapNotFound = scan(...)\n"); fflush(trace); }
|
2011-08-23 00:10:43 +00:00
|
|
|
_return.responseCode = mapkeeper::ResponseCode::MapNotFound;
|
2011-05-11 22:27:48 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-05-12 20:41:18 +00:00
|
|
|
|
|
|
|
datatuple* start = buildTuple(id, startKey);
|
|
|
|
datatuple* end;
|
2011-05-11 22:27:48 +00:00
|
|
|
if (endKey.empty()) {
|
2011-05-12 20:41:18 +00:00
|
|
|
end = buildTuple(id + 1, endKey);
|
2011-05-11 22:27:48 +00:00
|
|
|
} else {
|
2011-05-12 20:41:18 +00:00
|
|
|
end = buildTuple(id, endKey);
|
2011-05-11 22:27:48 +00:00
|
|
|
}
|
2011-06-09 01:43:46 +00:00
|
|
|
logtable::iterator* itr = new logtable::iterator(ltable_, start);
|
2011-05-11 22:27:48 +00:00
|
|
|
|
|
|
|
int32_t resultSize = 0;
|
2011-05-12 20:41:18 +00:00
|
|
|
|
2011-05-11 22:27:48 +00:00
|
|
|
while ((maxRecords == 0 || (int32_t)(_return.records.size()) < maxRecords) &&
|
|
|
|
(maxBytes == 0 || resultSize < maxBytes)) {
|
2011-05-12 20:41:18 +00:00
|
|
|
datatuple* current = itr->getnext();
|
|
|
|
if (current == NULL) {
|
2011-08-23 00:10:43 +00:00
|
|
|
_return.responseCode = mapkeeper::ResponseCode::ScanEnded;
|
2012-02-11 01:55:24 +00:00
|
|
|
if(trace) { fprintf(trace, "ScanEnded = scan(...)\n"); fflush(trace); }
|
2011-05-11 22:27:48 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-05-12 20:41:18 +00:00
|
|
|
|
|
|
|
int cmp = datatuple::compare_obj(current, start);
|
|
|
|
if ((!startKeyIncluded) && cmp == 0) {
|
|
|
|
datatuple::freetuple(current);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// are we at the end of range?
|
|
|
|
cmp = datatuple::compare_obj(current, end);
|
|
|
|
if ((!endKeyIncluded && cmp >= 0) ||
|
|
|
|
(endKeyIncluded && cmp > 0)) {
|
|
|
|
datatuple::freetuple(current);
|
2011-08-23 00:10:43 +00:00
|
|
|
_return.responseCode = mapkeeper::ResponseCode::ScanEnded;
|
2012-02-11 01:55:24 +00:00
|
|
|
if(trace) { fprintf(trace, "ScanEnded = scan(...)\n"); fflush(trace); }
|
2011-05-12 20:41:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-05-11 22:27:48 +00:00
|
|
|
Record rec;
|
2011-05-12 20:41:18 +00:00
|
|
|
int32_t keySize = current->strippedkeylen() - sizeof(id);
|
|
|
|
int32_t dataSize = current->datalen();
|
|
|
|
|
|
|
|
rec.key.assign((char*)(current->strippedkey()) + sizeof(id), keySize);
|
|
|
|
rec.value.assign((char*)(current->data()), dataSize);
|
2011-05-11 22:27:48 +00:00
|
|
|
_return.records.push_back(rec);
|
2011-05-12 20:41:18 +00:00
|
|
|
resultSize += keySize + dataSize;
|
|
|
|
datatuple::freetuple(current);
|
|
|
|
}
|
|
|
|
delete itr;
|
2011-05-11 22:27:48 +00:00
|
|
|
}
|
|
|
|
|
2011-05-12 00:30:02 +00:00
|
|
|
datatuple* LSMServerHandler::
|
|
|
|
get(datatuple* tuple)
|
|
|
|
{
|
2011-11-04 20:50:50 +00:00
|
|
|
// -1 is invalid txn id
|
2011-05-12 20:56:04 +00:00
|
|
|
datatuple* tup = ltable_->findTuple_first(-1, tuple->rawkey(), tuple->rawkeylen());
|
|
|
|
return tup;
|
2011-05-12 00:30:02 +00:00
|
|
|
}
|
|
|
|
|
2011-05-11 22:27:48 +00:00
|
|
|
void LSMServerHandler::
|
|
|
|
get(BinaryResponse& _return, const std::string& databaseName, const std::string& recordName)
|
|
|
|
{
|
2011-05-12 00:30:02 +00:00
|
|
|
uint32_t id = getDatabaseId(databaseName);
|
|
|
|
if (id == 0) {
|
|
|
|
// database not found
|
2012-02-11 01:55:24 +00:00
|
|
|
if(trace) { fprintf(trace, "MapNotFound = get(%s, %s)\n", databaseName.c_str(), recordName.c_str()); fflush(trace); }
|
2011-08-23 00:10:43 +00:00
|
|
|
_return.responseCode = mapkeeper::ResponseCode::MapNotFound;
|
2011-05-11 22:27:48 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-05-12 00:30:02 +00:00
|
|
|
|
|
|
|
datatuple* recordBody = get(id, recordName);
|
|
|
|
if (recordBody == NULL) {
|
|
|
|
// record not found
|
2012-02-11 01:55:24 +00:00
|
|
|
if(trace) { fprintf(trace, "RecordNotFound = get(%s, %s)\n", databaseName.c_str(), recordName.c_str()); fflush(trace); }
|
2011-08-23 00:10:43 +00:00
|
|
|
_return.responseCode = mapkeeper::ResponseCode::RecordNotFound;
|
2011-05-12 00:30:02 +00:00
|
|
|
return;
|
2011-05-11 22:27:48 +00:00
|
|
|
}
|
2012-02-11 01:55:24 +00:00
|
|
|
if(trace) { fprintf(trace, "Success = get(%s, %s)\n", databaseName.c_str(), recordName.c_str()); fflush(trace); }
|
2011-08-23 00:10:43 +00:00
|
|
|
_return.responseCode = mapkeeper::ResponseCode::Success;
|
2011-05-12 00:30:02 +00:00
|
|
|
_return.value.assign((const char*)(recordBody->data()), recordBody->datalen());
|
|
|
|
datatuple::freetuple(recordBody);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t LSMServerHandler::
|
|
|
|
getDatabaseId(const std::string& databaseName)
|
|
|
|
{
|
|
|
|
datatuple* tup = buildTuple(0, databaseName);
|
|
|
|
datatuple* databaseId = get(tup);
|
|
|
|
datatuple::freetuple(tup);
|
|
|
|
if (databaseId == NULL) {
|
|
|
|
// database not found
|
|
|
|
std::cout << "db not found" << std::endl;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
uint32_t id = *((uint32_t*)(databaseId->data()));
|
|
|
|
datatuple::freetuple(databaseId);
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
datatuple* LSMServerHandler::
|
|
|
|
get(uint32_t databaseId, const std::string& recordName)
|
|
|
|
{
|
|
|
|
datatuple* recordKey = buildTuple(databaseId, recordName);
|
2011-05-16 21:16:36 +00:00
|
|
|
datatuple* ret = get(recordKey);
|
|
|
|
datatuple::freetuple(recordKey);
|
|
|
|
return ret;
|
2011-05-11 22:27:48 +00:00
|
|
|
}
|
|
|
|
|
2011-08-23 00:10:43 +00:00
|
|
|
ResponseCode::type LSMServerHandler::
|
|
|
|
put(const std::string& databaseName,
|
|
|
|
const std::string& recordName,
|
|
|
|
const std::string& recordBody)
|
|
|
|
{
|
2012-01-09 19:29:17 +00:00
|
|
|
uint32_t id = getDatabaseId(databaseName);
|
|
|
|
if (id == 0) {
|
2012-02-11 01:55:24 +00:00
|
|
|
if(trace) { fprintf(trace, "MapNotFound = put(%s, %s)\n", databaseName.c_str(), recordName.c_str()); fflush(trace); }
|
2012-01-09 19:29:17 +00:00
|
|
|
return mapkeeper::ResponseCode::MapNotFound;
|
|
|
|
}
|
|
|
|
datatuple* tup = buildTuple(id, recordName, recordBody);
|
2012-02-11 01:55:24 +00:00
|
|
|
if(trace) { fprintf(trace, "Success = put(%s, %s)\n", databaseName.c_str(), recordName.c_str()); fflush(trace); }
|
2012-01-09 19:29:17 +00:00
|
|
|
return insert(tup);
|
2011-08-23 00:10:43 +00:00
|
|
|
}
|
|
|
|
|
2011-05-11 22:27:48 +00:00
|
|
|
ResponseCode::type LSMServerHandler::
|
|
|
|
insert(const std::string& databaseName,
|
|
|
|
const std::string& recordName,
|
|
|
|
const std::string& recordBody)
|
|
|
|
{
|
2011-05-12 00:30:02 +00:00
|
|
|
uint32_t id = getDatabaseId(databaseName);
|
|
|
|
if (id == 0) {
|
2012-02-11 01:55:24 +00:00
|
|
|
if(trace) { fprintf(trace, "MapNotFound = insert(%s, %s)\n", databaseName.c_str(), recordName.c_str()); fflush(trace); }
|
2011-08-23 00:10:43 +00:00
|
|
|
return mapkeeper::ResponseCode::MapNotFound;
|
2011-05-11 22:27:48 +00:00
|
|
|
}
|
2011-11-04 20:50:50 +00:00
|
|
|
if(!blind_update) {
|
|
|
|
datatuple* oldRecordBody = get(id, recordName);
|
|
|
|
if (oldRecordBody != NULL) {
|
2011-06-08 23:58:29 +00:00
|
|
|
if(oldRecordBody->isDelete()) {
|
|
|
|
datatuple::freetuple(oldRecordBody);
|
|
|
|
} else {
|
|
|
|
datatuple::freetuple(oldRecordBody);
|
2012-02-11 01:55:24 +00:00
|
|
|
if(trace) { fprintf(trace, "RecordExists = insert(%s, %s)\n", databaseName.c_str(), recordName.c_str()); fflush(trace); }
|
2011-08-23 00:10:43 +00:00
|
|
|
return mapkeeper::ResponseCode::RecordExists;
|
2011-06-08 23:58:29 +00:00
|
|
|
}
|
2011-11-04 20:50:50 +00:00
|
|
|
}
|
2011-05-11 22:27:48 +00:00
|
|
|
}
|
2011-05-12 00:30:02 +00:00
|
|
|
|
|
|
|
datatuple* tup = buildTuple(id, recordName, recordBody);
|
2012-02-11 01:55:24 +00:00
|
|
|
if(trace) { fprintf(trace, "Success = insert(%s, %s)\n", databaseName.c_str(), recordName.c_str()); fflush(trace); }
|
2011-05-12 00:30:02 +00:00
|
|
|
return insert(tup);
|
2011-05-11 22:27:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ResponseCode::type LSMServerHandler::
|
|
|
|
insertMany(const std::string& databaseName, const std::vector<Record> & records)
|
|
|
|
{
|
2012-02-11 01:55:24 +00:00
|
|
|
if(trace) { fprintf(trace, "Error = insertMany(%s, ...) (not supported)\n", databaseName.c_str()); fflush(trace); }
|
2011-08-23 00:10:43 +00:00
|
|
|
return mapkeeper::ResponseCode::Error;
|
2011-05-11 22:27:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ResponseCode::type LSMServerHandler::
|
|
|
|
update(const std::string& databaseName,
|
|
|
|
const std::string& recordName,
|
|
|
|
const std::string& recordBody)
|
|
|
|
{
|
2011-05-12 20:41:18 +00:00
|
|
|
uint32_t id = getDatabaseId(databaseName);
|
|
|
|
if (id == 0) {
|
2012-02-11 01:55:24 +00:00
|
|
|
if(trace) { fprintf(trace, "MapNotFound = update(%s, %s)\n", databaseName.c_str(), recordName.c_str()); fflush(trace); }
|
2011-08-23 00:10:43 +00:00
|
|
|
return mapkeeper::ResponseCode::MapNotFound;
|
2011-05-11 22:27:48 +00:00
|
|
|
}
|
2011-11-04 20:50:50 +00:00
|
|
|
if(!blind_update) {
|
|
|
|
datatuple* oldRecordBody = get(id, recordName);
|
|
|
|
if (oldRecordBody == NULL) {
|
2012-02-11 01:55:24 +00:00
|
|
|
if(trace) { fprintf(trace, "RecordNotFound = update(%s, %s)\n", databaseName.c_str(), recordName.c_str()); fflush(trace); }
|
2011-08-23 00:10:43 +00:00
|
|
|
return mapkeeper::ResponseCode::RecordNotFound;
|
2011-11-04 20:50:50 +00:00
|
|
|
}
|
|
|
|
datatuple::freetuple(oldRecordBody);
|
2011-05-11 22:27:48 +00:00
|
|
|
}
|
2011-05-12 20:41:18 +00:00
|
|
|
datatuple* tup = buildTuple(id, recordName, recordBody);
|
2012-02-11 01:55:24 +00:00
|
|
|
if(trace) { fprintf(trace, "Success = update(%s, %s)\n", databaseName.c_str(), recordName.c_str()); fflush(trace); }
|
2011-05-12 20:41:18 +00:00
|
|
|
return insert(tup);
|
2011-05-11 22:27:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ResponseCode::type LSMServerHandler::
|
|
|
|
remove(const std::string& databaseName, const std::string& recordName)
|
|
|
|
{
|
2011-05-12 20:56:04 +00:00
|
|
|
uint32_t id = getDatabaseId(databaseName);
|
|
|
|
if (id == 0) {
|
2012-02-11 01:55:24 +00:00
|
|
|
if(trace) { fprintf(trace, "MapNotFound = remove(%s, %s)\n", databaseName.c_str(), recordName.c_str()); fflush(trace); }
|
2011-08-23 00:10:43 +00:00
|
|
|
return mapkeeper::ResponseCode::MapNotFound;
|
2011-05-11 22:27:48 +00:00
|
|
|
}
|
2011-05-12 20:56:04 +00:00
|
|
|
datatuple* oldRecordBody = get(id, recordName);
|
|
|
|
if (oldRecordBody == NULL) {
|
2012-02-11 01:55:24 +00:00
|
|
|
if(trace) { fprintf(trace, "RecordNotFound = remove(%s, %s)\n", databaseName.c_str(), recordName.c_str()); fflush(trace); }
|
2011-08-23 00:10:43 +00:00
|
|
|
return mapkeeper::ResponseCode::RecordNotFound;
|
2011-05-11 22:27:48 +00:00
|
|
|
}
|
2011-05-12 20:56:04 +00:00
|
|
|
datatuple::freetuple(oldRecordBody);
|
|
|
|
datatuple* tup = buildTuple(id, recordName);
|
2012-02-11 01:55:24 +00:00
|
|
|
if(trace) { fprintf(trace, "Success = remove(%s, %s)\n", databaseName.c_str(), recordName.c_str()); fflush(trace); }
|
2011-05-12 20:56:04 +00:00
|
|
|
return insert(tup);
|
2011-05-11 22:27:48 +00:00
|
|
|
}
|
2011-05-12 00:30:02 +00:00
|
|
|
|
|
|
|
datatuple* LSMServerHandler::
|
|
|
|
buildTuple(uint32_t databaseId, const std::string& recordName)
|
|
|
|
{
|
|
|
|
return buildTuple(databaseId, recordName, NULL, DELETE);
|
|
|
|
}
|
|
|
|
|
|
|
|
datatuple* LSMServerHandler::
|
|
|
|
buildTuple(uint32_t databaseId, const std::string& recordName, const std::string& recordBody)
|
|
|
|
{
|
|
|
|
return buildTuple(databaseId, recordName, recordBody.c_str(), recordBody.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
datatuple* LSMServerHandler::
|
|
|
|
buildTuple(uint32_t databaseId, const std::string& recordName, const void* body, uint32_t bodySize)
|
|
|
|
{
|
|
|
|
uint32_t keySize = sizeof(databaseId) + recordName.size();
|
|
|
|
unsigned char* key = (unsigned char*)malloc(keySize);
|
2011-05-13 06:53:39 +00:00
|
|
|
*(uint32_t*)key = htonl(databaseId);
|
2011-05-12 00:30:02 +00:00
|
|
|
memcpy(((uint32_t*)key) + 1, recordName.c_str(), recordName.size());
|
|
|
|
datatuple *tup = datatuple::create(key, keySize, body, bodySize);
|
2011-05-16 21:16:36 +00:00
|
|
|
free(key);
|
2011-05-12 00:30:02 +00:00
|
|
|
return tup;
|
|
|
|
}
|