Skip to content

Commit

Permalink
MB-40360: Modularize Bucket ops implementation
Browse files Browse the repository at this point in the history
Change-Id: If4e8972af1b7bd7726dd6fb15d8de416cfbb085f
Reviewed-on: http://review.couchbase.org/c/eventing/+/136533
Well-Formed: Build Bot <build@couchbase.com>
Reviewed-by: Jeelan Basha Poola <jeelan.poola@couchbase.com>
Tested-by: <ankit.prabhu@couchbase.com>
  • Loading branch information
AnkitPrabhu committed Sep 21, 2020
1 parent e0bd3a4 commit 9b434b3
Show file tree
Hide file tree
Showing 11 changed files with 633 additions and 548 deletions.
114 changes: 86 additions & 28 deletions features/include/bucket.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,92 @@
#define BUCKET_H

#include <libcouchbase/couchbase.h>
#include <memory>
#include <string>
#include <tuple>
#include <utility>
#include <v8.h>

#include "error.h"
#include "info.h"
#include "isolate_data.h"
#include "lcb_utils.h"

class BucketFactory {
public:
BucketFactory(v8::Isolate *isolate, const v8::Local<v8::Context> &context);
~BucketFactory();

BucketFactory(const BucketFactory &) = delete;
BucketFactory(BucketFactory &&) = delete;
BucketFactory &operator=(const BucketFactory &) = delete;
BucketFactory &operator=(BucketFactory &&) = delete;

std::pair<Error, std::unique_ptr<v8::Local<v8::Object>>> NewBucketObj() const;

private:
v8::Isolate *isolate_;
v8::Persistent<v8::Context> context_;
v8::Persistent<v8::ObjectTemplate> bucket_template_;
};

class Bucket {
public:
Bucket(v8::Isolate *isolate, const v8::Local<v8::Context> &context,
const std::string &bucket_name, const std::string &alias,
bool block_mutation, bool is_source_bucket);
Bucket(v8::Isolate *isolate, std::string bucket_name)
: isolate_(isolate), bucket_name_(std::move(bucket_name)) {}
~Bucket();

bool InstallMaps();
Bucket(const Bucket &) = default;
Bucket(Bucket &&) = delete;
Bucket &operator=(const Bucket &) = delete;
Bucket &operator=(Bucket &&) = delete;

v8::Global<v8::ObjectTemplate> bucket_map_template_;
lcb_t bucket_lcb_obj_;
Error Connect();

std::tuple<Error, std::unique_ptr<lcb_error_t>, std::unique_ptr<Result>>
Get(const std::string &key);

std::tuple<Error, std::unique_ptr<lcb_error_t>, std::unique_ptr<Result>>
SetWithXattr(const std::string &key, const std::string &value);

std::tuple<Error, std::unique_ptr<lcb_error_t>, std::unique_ptr<Result>>
SetWithoutXattr(const std::string &key, const std::string &value);

std::tuple<Error, std::unique_ptr<lcb_error_t>, std::unique_ptr<Result>>
DeleteWithXattr(const std::string &key);

std::tuple<Error, std::unique_ptr<lcb_error_t>, std::unique_ptr<Result>>
DeleteWithoutXattr(const std::string &key);

lcb_t GetConnection() const { return connection_; }

private:
static void HandleBucketOpFailure(v8::Isolate *isolate,
lcb_t bucket_lcb_obj_ptr,
lcb_error_t error);
v8::Local<v8::ObjectTemplate> MakeBucketMapTemplate();
Error FormatErrorAndDestroyConn(const std::string &message,
const lcb_error_t &error) const;

v8::Isolate *isolate_{nullptr};
std::string bucket_name_;
lcb_t connection_{nullptr};
bool is_connected_{false};
};

class BucketBinding {
friend BucketFactory;

public:
BucketBinding(v8::Isolate *isolate, std::shared_ptr<BucketFactory> factory,
const std::string &bucket_name, std::string alias,
bool block_mutation, bool is_source_bucket)
: block_mutation_(block_mutation), is_source_bucket_(is_source_bucket),
bucket_name_(bucket_name), bucket_alias_(std::move(alias)),
factory_(std::move(factory)), bucket_(isolate, bucket_name) {}

Error InstallBinding(v8::Isolate *isolate,
const v8::Local<v8::Context> &context);

private:
static void HandleBucketOpFailure(v8::Isolate *isolate, lcb_t connection,
lcb_error_t error);
static Info ValidateKey(const v8::Local<v8::Name> &arg);
static Info ValidateValue(const v8::Local<v8::Value> &arg);
static Info ValidateKeyValue(const v8::Local<v8::Name> &key,
Expand All @@ -47,7 +109,7 @@ class Bucket {
// object in JavaScript
template <typename T>
static void
BucketGetDelegate(T key, const v8::PropertyCallbackInfo<v8::Value> &info);
BucketGetDelegate(T name, const v8::PropertyCallbackInfo<v8::Value> &info);
template <typename T>
static void
BucketSetDelegate(T key, v8::Local<v8::Value> value,
Expand Down Expand Up @@ -76,22 +138,21 @@ class Bucket {
static void BucketSet(uint32_t key, const v8::Local<v8::Value> &value,
const v8::PropertyCallbackInfo<v8::Value> &info);

static std::tuple<Error, std::unique_ptr<lcb_error_t>,
std::unique_ptr<Result>>
BucketSet(const std::string &key, const std::string &value,
bool is_source_bucket, Bucket *bucket);

template <typename>
static void BucketDelete(const v8::Local<v8::Name> &key,
const v8::PropertyCallbackInfo<v8::Boolean> &info);
template <typename>
static void BucketDelete(uint32_t key,
const v8::PropertyCallbackInfo<v8::Boolean> &info);

static void
BucketSetWithXattr(const v8::Local<v8::Name> &key,
const v8::Local<v8::Value> &value,
const v8::PropertyCallbackInfo<v8::Value> &info);

static void
BucketSetWithoutXattr(const v8::Local<v8::Name> &key,
const v8::Local<v8::Value> &value,
const v8::PropertyCallbackInfo<v8::Value> &info);
static std::tuple<Error, std::unique_ptr<lcb_error_t>,
std::unique_ptr<Result>>
BucketDelete(const std::string &key, bool is_source_bucket, Bucket *bucket);

static void
BucketDeleteWithXattr(const v8::Local<v8::Name> &key,
Expand All @@ -107,21 +168,18 @@ class Bucket {

static void HandleEnoEnt(v8::Isolate *isolate, lcb_t instance);

v8::Local<v8::Object> WrapBucketMap();

v8::Isolate *isolate_;
v8::Persistent<v8::Context> context_;

bool block_mutation_;
bool is_source_bucket_;
std::string bucket_name_;
std::string bucket_alias_;
std::shared_ptr<BucketFactory> factory_;
Bucket bucket_;

enum class InternalFields {
kLcbInstance,
enum InternalFields {
kBucketInstance,
kBlockMutation,
kIsSourceBucket,
kMaxInternalFields
kInternalFieldsCount
};
};

Expand Down
24 changes: 24 additions & 0 deletions features/include/error.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) 2019 Couchbase, 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.

#ifndef ERROR_H
#define ERROR_H

#include <memory>
#include <string>

using Error = std::unique_ptr<std::string>;

template <typename T> Error MakeError(T &&msg) {
return std::make_unique<T>(std::forward<T>(msg));
}

#endif
20 changes: 9 additions & 11 deletions features/include/lcb_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,16 @@
#ifndef COUCHBASE_LCB_UTILS_H
#define COUCHBASE_LCB_UTILS_H

#include "log.h"
#include "utils.h"
#include <libcouchbase/couchbase.h>
#include <thread>
#include "utils.h"

struct Result {
lcb_CAS cas;
lcb_error_t rc;
lcb_CAS cas{0};
lcb_error_t rc{LCB_SUCCESS};
std::string value;
uint32_t exptime;
int64_t counter;
Result() : cas(0), rc(LCB_SUCCESS) {}
uint32_t exptime{0};
int64_t counter{0};
};

constexpr int def_lcb_retry_count = 6;
Expand All @@ -37,13 +34,13 @@ const char *GetPassword(void *cookie, const char *host, const char *port,
const char *bucket);

// lcb related callbacks
void get_callback(lcb_t instance, int, const lcb_RESPBASE *rb);
void GetCallback(lcb_t instance, int, const lcb_RESPBASE *rb);

void set_callback(lcb_t instance, int cbtype, const lcb_RESPBASE *rb);
void SetCallback(lcb_t instance, int cbtype, const lcb_RESPBASE *rb);

void sdmutate_callback(lcb_t instance, int cbtype, const lcb_RESPBASE *rb);
void SubDocumentCallback(lcb_t instance, int cbtype, const lcb_RESPBASE *rb);

void del_callback(lcb_t instance, int cbtype, const lcb_RESPBASE *rb);
void DeleteCallback(lcb_t instance, int cbtype, const lcb_RESPBASE *rb);

void counter_callback(lcb_t instance, int cbtype, const lcb_RESPBASE *rb);

Expand Down Expand Up @@ -98,4 +95,5 @@ std::pair<lcb_error_t, Result> RetryLcbCommand(lcb_t instance, CmdType &cmd,
}

extern struct lcb_logprocs_st evt_logger;

#endif // COUCHBASE_LCB_UTILS_H
Loading

0 comments on commit 9b434b3

Please sign in to comment.