Zero  0.1.0
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Friends | List of all members
w_keystr_t Class Reference

Key string class which can represent a few special values. More...

#include <w_key.h>

Public Member Functions

 w_keystr_t ()
 
 w_keystr_t (const w_keystr_t &r)
 
w_keystr_toperator= (const w_keystr_t &r)
 
 ~w_keystr_t ()
 
bool construct_regularkey (const void *nonkeystr, w_keystr_len_t length)
 
bool construct_neginfkey ()
 
bool construct_posinfkey ()
 
bool construct_from_keystr (const void *keystr, w_keystr_len_t length)
 
bool construct_from_keystr (const void *keystr_prefix, w_keystr_len_t prefix_length, const void *keystr_suffix, w_keystr_len_t suffix_length)
 
bool construct_from_keystr_poormkey_16 (const void *keystr_prefix, w_keystr_len_t prefix_length, w_keystr_len_t total_suffix_length, uint16_t poormkey, const void *keystr_suffix)
 
bool construct_from_vec (const cvec_t &vect)
 
bool copy_from_vec (const cvec_t &vect)
 
bool is_constructed () const
 
bool is_neginf () const
 
bool is_posinf () const
 
bool is_regular () const
 
w_keystr_len_t common_leading_bytes (const w_keystr_t &r) const
 
w_keystr_len_t common_leading_bytes (const unsigned char *str, w_keystr_len_t len) const
 
int compare (const w_keystr_t &r) const
 
int compare_keystr (const void *keystr, w_keystr_len_t length) const
 
int compare_nonkeystr (const void *nonkeystr, w_keystr_len_t length) const
 
bool operator== (const w_keystr_t &r) const
 
bool operator!= (const w_keystr_t &r) const
 
bool operator< (const w_keystr_t &r) const
 
bool operator> (const w_keystr_t &r) const
 
bool operator<= (const w_keystr_t &r) const
 
bool operator>= (const w_keystr_t &r) const
 
void serialize_as_nonkeystr (void *buffer) const
 
std::basic_string< unsigned char > serialize_as_nonkeystr () const
 
w_keystr_len_t get_length_as_nonkeystr () const
 
void serialize_as_keystr (void *buffer) const
 
const void * buffer_as_keystr () const
 
w_keystr_len_t get_length_as_keystr () const
 
void clear ()
 

Static Public Member Functions

static w_keystr_len_t common_leading_bytes (const unsigned char *str1, w_keystr_len_t len1, const unsigned char *str2, w_keystr_len_t len2)
 
static int compare_bin_str (const void *str1, int len1, const void *str2, int len2)
 

Private Member Functions

void _assure (w_keystr_len_t required_len)
 

Private Attributes

unsigned char * _data
 
uint32_t _strlen
 
uint32_t _memlen
 

Friends

class w_keystr_t_test
 
std::ostream & operator<< (std::ostream &, const w_keystr_t &v)
 

Detailed Description

Key string class which can represent a few special values.

This class differs from vec_t/cvec_t mainly for two points.

First, w_keystr_t keeps its copy of string data like std::string. vec_t/cvec_t just keeps pointers to original data, which is efficient but cannot be used for some cases. For example, when the original data is being changed.

Second, w_keystr_t represents a few special values, NULL, Infimum and Supremum both as on-memory data and on-disk data. vec_t/cvec_t loses neg_inf/pos_inf information when it's written out to disk. As we need to store infimum/supremum in B+Tree fence keys, we need a new class to store key data.

In sum, when you just want on-memory data representation, use vec_t/cvec_t. When you need to represent a key to be stored on disk, or to be compared with other keys on disk, use this class.

This is a tiny header-only class. All functions are defined here.

Constructor & Destructor Documentation

§ w_keystr_t() [1/2]

w_keystr_t::w_keystr_t ( )
inline

This class provides only this empty constructor and copy constructor from another w_keystr_t. This is by design to prohibit incorrect use. Explicitly use construct_regularkey()/construct_from_keystr() to make sure if the input already has sign bytes.

§ w_keystr_t() [2/2]

w_keystr_t::w_keystr_t ( const w_keystr_t r)
inline

§ ~w_keystr_t()

w_keystr_t::~w_keystr_t ( )
inline

Member Function Documentation

§ _assure()

void w_keystr_t::_assure ( w_keystr_len_t  required_len)
inlineprivate

re-allocate _data for required length if needed.

§ buffer_as_keystr()

const void * w_keystr_t::buffer_as_keystr ( ) const
inline

Returns the internal content WITH sign byte of this key string.

§ clear()

void w_keystr_t::clear ( )
inline

discards all data and releases all resources.

§ common_leading_bytes() [1/3]

w_keystr_len_t w_keystr_t::common_leading_bytes ( const w_keystr_t r) const
inline

Returns the count of common leading bytes with the given key string.

§ common_leading_bytes() [2/3]

w_keystr_len_t w_keystr_t::common_leading_bytes ( const unsigned char *  str,
w_keystr_len_t  len 
) const
inline

Returns the count of common leading bytes with the given key string.

§ common_leading_bytes() [3/3]

w_keystr_len_t w_keystr_t::common_leading_bytes ( const unsigned char *  str1,
w_keystr_len_t  len1,
const unsigned char *  str2,
w_keystr_len_t  len2 
)
inlinestatic

Returns the count of common leading bytes in the two given strings.

§ compare()

int w_keystr_t::compare ( const w_keystr_t r) const
inline

Compares this string with another w_keystr_t object.

Parameters
[in]ranother string to compare with
Returns
0 if equal. <0 if this<r, >0 if this>r

§ compare_bin_str()

int w_keystr_t::compare_bin_str ( const void *  str1,
int  len1,
const void *  str2,
int  len2 
)
inlinestatic

Compares two strings.

Returns
0 if equal in string and length. <0 if str1<str2 OR str1=str2 and len1<len2, >0 therwise.

§ compare_keystr()

int w_keystr_t::compare_keystr ( const void *  keystr,
w_keystr_len_t  length 
) const
inline

Compares this string with a string data WITH sign byte which should be a serialized w_keystr_t.

Parameters
[in]keystranother string data WITH sign byte to compare with
[in]lengthlength of the keystr
Returns
0 if equal. <0 if this<r, >0 if this>r

§ compare_nonkeystr()

int w_keystr_t::compare_nonkeystr ( const void *  nonkeystr,
w_keystr_len_t  length 
) const
inline

Compares this string with a string data WITH sign byte which should be a serialized w_keystr_t. Note that non-key string can't represent infinity.

Parameters
[in]nonkeystranother non-key string data WITH sign byte to compare with
[in]lengthlength of the nonkeystr
Returns
0 if equal. <0 if this<r, >0 if this>r

§ construct_from_keystr() [1/2]

bool w_keystr_t::construct_from_keystr ( const void *  keystr,
w_keystr_len_t  length 
)
inline

Constructs from a key string WITH sign byte.

Parameters
keystrThe string WITH sign byte.
lengthThe length WITH sign byte.
See also
construct_regularkey (const void *, w_keystr_len_t)

§ construct_from_keystr() [2/2]

bool w_keystr_t::construct_from_keystr ( const void *  keystr_prefix,
w_keystr_len_t  prefix_length,
const void *  keystr_suffix,
w_keystr_len_t  suffix_length 
)
inline

Used when the key string consists of prefix and suffix.

§ construct_from_keystr_poormkey_16()

bool w_keystr_t::construct_from_keystr_poormkey_16 ( const void *  keystr_prefix,
w_keystr_len_t  prefix_length,
w_keystr_len_t  total_suffix_length,
uint16_t  poormkey,
const void *  keystr_suffix 
)
inline

Used when the key string consists of prefix, poor-man's key and suffix.

Parameters
total_suffix_lengthkey length including poorman's key and suffix. Note that this might be smaller than sizeof(poormkey)! E.g., total_suffix_length=1: returns prefix and only the first byte of poormkey.

§ construct_from_vec()

bool w_keystr_t::construct_from_vec ( const cvec_t vect)

Construct from cvec_t. This is implemented in vec_t.cpp. So, link to libcommon if you want to use this method.

§ construct_neginfkey()

bool w_keystr_t::construct_neginfkey ( )
inline

Creates an object that represents negative infinity.

§ construct_posinfkey()

bool w_keystr_t::construct_posinfkey ( )
inline

Creates an object that represents positive infinity.

§ construct_regularkey()

bool w_keystr_t::construct_regularkey ( const void *  nonkeystr,
w_keystr_len_t  length 
)
inline

Constructs from a general string without sign byte. Use this method to convert user inputs to a keystr object. Do NOT use this method to copy from other keystr object. You will duplicate the sign byte and mess up all comparisons! To copy from existing keystr object, just use copy constructor or assignment operator.

Usage Example:

w_keystr_t str1;
str1.construct_regularkey ("your_key", 8);
Parameters
nonkeystrThe string WITHOUT sign byte.
lengthThe length WITHOUT sign byte.
Returns
whether this object is constructed. false if out-of-memory.
See also
construct_from_keystr (const void *, w_keystr_len_t)

§ copy_from_vec()

bool w_keystr_t::copy_from_vec ( const cvec_t vect)

Copy data from cvec_t without inserting the type byte. This is implemented in vec_t.cpp. So, link to libcommon if you want to use this method.

§ get_length_as_keystr()

w_keystr_len_t w_keystr_t::get_length_as_keystr ( ) const
inline

length of this string WITH sign byte.

§ get_length_as_nonkeystr()

w_keystr_len_t w_keystr_t::get_length_as_nonkeystr ( ) const
inline

length of this string WITHOUT sign byte.

§ is_constructed()

bool w_keystr_t::is_constructed ( ) const
inline

This class does NOT throw exceptions on out-of-memory. Use this function to check if it's constructed.

Returns
whether this object is constructed. false if out-of-memory.

§ is_neginf()

bool w_keystr_t::is_neginf ( ) const
inline

Returns if this object represents negative infinity.

§ is_posinf()

bool w_keystr_t::is_posinf ( ) const
inline

Returns if this object represents positive infinity.

§ is_regular()

bool w_keystr_t::is_regular ( ) const
inline

Returns if this object represents non-inifinity value.

§ operator!=()

bool w_keystr_t::operator!= ( const w_keystr_t r) const
inline

§ operator<()

bool w_keystr_t::operator< ( const w_keystr_t r) const
inline

§ operator<=()

bool w_keystr_t::operator<= ( const w_keystr_t r) const
inline

§ operator=()

w_keystr_t & w_keystr_t::operator= ( const w_keystr_t r)
inline

§ operator==()

bool w_keystr_t::operator== ( const w_keystr_t r) const
inline

operator overloads.

§ operator>()

bool w_keystr_t::operator> ( const w_keystr_t r) const
inline

§ operator>=()

bool w_keystr_t::operator>= ( const w_keystr_t r) const
inline

§ serialize_as_keystr()

void w_keystr_t::serialize_as_keystr ( void *  buffer) const
inline

Copy the string data of this object to the given buffer WITH sign byte.

§ serialize_as_nonkeystr() [1/2]

void w_keystr_t::serialize_as_nonkeystr ( void *  buffer) const
inline

Copy the string data of this object to the given buffer WITHOUT sign byte.

§ serialize_as_nonkeystr() [2/2]

std::basic_string< unsigned char > w_keystr_t::serialize_as_nonkeystr ( ) const
inline

For convenience. Returns std::string object.

Friends And Related Function Documentation

§ operator<<

std::ostream& operator<< ( std::ostream &  o,
const w_keystr_t v 
)
friend

§ w_keystr_t_test

friend class w_keystr_t_test
friend

Member Data Documentation

§ _data

unsigned char* w_keystr_t::_data
private

raw data. this internal data starts with a sign byte.

§ _memlen

uint32_t w_keystr_t::_memlen
private

allocated length of _data, which could be larger than _strlen.

§ _strlen

uint32_t w_keystr_t::_strlen
private

length of _data, which is 1 byte longer than the actual data.


The documentation for this class was generated from the following file: