2012-01-19 16:49:54 +00:00
|
|
|
/*
|
|
|
|
* datapage.h
|
|
|
|
*
|
|
|
|
* Copyright 2009-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: makdere
|
|
|
|
*/
|
2012-02-23 01:11:55 +00:00
|
|
|
#ifndef DATA_PAGE_H_
|
|
|
|
#define DATA_PAGE_H_
|
2010-01-23 02:13:59 +00:00
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
#include <stasis/page.h>
|
|
|
|
#include <stasis/constants.h>
|
2012-02-23 01:25:24 +00:00
|
|
|
#include "dataTuple.h"
|
2012-02-23 01:11:55 +00:00
|
|
|
#include "regionAllocator.h"
|
2010-01-23 02:13:59 +00:00
|
|
|
|
2010-02-16 00:44:13 +00:00
|
|
|
//#define CHECK_FOR_SCRIBBLING
|
2010-01-23 02:13:59 +00:00
|
|
|
|
2012-02-23 01:11:55 +00:00
|
|
|
class dataPage
|
2010-01-23 02:13:59 +00:00
|
|
|
{
|
|
|
|
public:
|
2011-06-09 00:07:42 +00:00
|
|
|
class iterator
|
|
|
|
{
|
|
|
|
private:
|
2012-02-23 01:11:55 +00:00
|
|
|
void scan_to_key(dataTuple * key) {
|
2011-06-09 00:07:42 +00:00
|
|
|
if(key) {
|
|
|
|
len_t old_off = read_offset_;
|
2012-02-23 01:11:55 +00:00
|
|
|
dataTuple * t = getnext();
|
|
|
|
while(t && dataTuple::compare(key->strippedkey(), key->strippedkeylen(), t->strippedkey(), t->strippedkeylen()) > 0) {
|
|
|
|
dataTuple::freetuple(t);
|
2011-06-09 00:07:42 +00:00
|
|
|
old_off = read_offset_;
|
|
|
|
t = getnext();
|
|
|
|
}
|
|
|
|
if(t) {
|
|
|
|
DEBUG("datapage opened at %s\n", t->key());
|
2012-02-23 01:11:55 +00:00
|
|
|
dataTuple::freetuple(t);
|
2011-06-09 00:07:42 +00:00
|
|
|
read_offset_ = old_off;
|
|
|
|
} else {
|
|
|
|
DEBUG("datapage key not found. Offset = %lld", read_offset_);
|
|
|
|
dp = NULL;
|
|
|
|
}
|
2010-02-25 01:29:32 +00:00
|
|
|
}
|
2011-06-09 00:07:42 +00:00
|
|
|
}
|
|
|
|
public:
|
2012-02-23 01:11:55 +00:00
|
|
|
iterator(dataPage *dp, dataTuple * key=NULL) : read_offset_(0), dp(dp) {
|
2011-06-09 00:07:42 +00:00
|
|
|
scan_to_key(key);
|
|
|
|
}
|
2010-01-23 02:13:59 +00:00
|
|
|
|
2011-06-09 00:07:42 +00:00
|
|
|
void operator=(const iterator &rhs) {
|
|
|
|
this->read_offset_ = rhs.read_offset_;
|
|
|
|
this->dp = rhs.dp;
|
|
|
|
}
|
2010-01-23 02:13:59 +00:00
|
|
|
|
2011-06-09 00:07:42 +00:00
|
|
|
//returns the next tuple and also advances the iterator
|
2012-02-23 01:11:55 +00:00
|
|
|
dataTuple *getnext();
|
2010-02-15 23:02:01 +00:00
|
|
|
|
2011-06-09 00:07:42 +00:00
|
|
|
private:
|
|
|
|
off_t read_offset_;
|
2012-02-23 01:11:55 +00:00
|
|
|
dataPage *dp;
|
2011-06-09 00:07:42 +00:00
|
|
|
};
|
2010-01-23 02:13:59 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2011-06-09 00:07:42 +00:00
|
|
|
/**
|
|
|
|
* if alloc is non-null, then reads will be optimized for sequential access
|
|
|
|
*/
|
2012-02-23 01:11:55 +00:00
|
|
|
dataPage( int xid, regionAllocator* alloc, pageid_t pid );
|
2010-01-23 02:13:59 +00:00
|
|
|
|
2011-06-09 00:07:42 +00:00
|
|
|
//to be used to create new data pages
|
2012-02-23 01:11:55 +00:00
|
|
|
dataPage( int xid, pageid_t page_count, regionAllocator* alloc);
|
2010-01-23 02:13:59 +00:00
|
|
|
|
2012-02-23 01:11:55 +00:00
|
|
|
~dataPage() {
|
2011-06-09 00:07:42 +00:00
|
|
|
assert(write_offset_ == -1);
|
|
|
|
}
|
2010-01-23 02:13:59 +00:00
|
|
|
|
2011-06-09 00:07:42 +00:00
|
|
|
void writes_done() {
|
|
|
|
if(write_offset_ != -1) {
|
|
|
|
len_t dat_len = 0; // write terminating zero.
|
2010-01-23 02:13:59 +00:00
|
|
|
|
2011-06-09 00:07:42 +00:00
|
|
|
write_data((const byte*)&dat_len, sizeof(dat_len), false);
|
2010-01-23 02:13:59 +00:00
|
|
|
|
2011-06-09 00:07:42 +00:00
|
|
|
// if writing the zero fails, later reads will fail as well, and assume EOF.
|
2010-01-23 02:13:59 +00:00
|
|
|
|
2011-06-09 00:07:42 +00:00
|
|
|
write_offset_ = -1;
|
2010-02-15 23:02:01 +00:00
|
|
|
}
|
2010-01-23 02:13:59 +00:00
|
|
|
|
2011-06-09 00:07:42 +00:00
|
|
|
}
|
2010-01-23 02:13:59 +00:00
|
|
|
|
2012-02-23 01:11:55 +00:00
|
|
|
bool append(dataTuple const * dat);
|
|
|
|
bool recordRead(const dataTuple::key_t key, size_t keySize, dataTuple ** buf);
|
2010-01-23 02:13:59 +00:00
|
|
|
|
2011-06-09 00:07:42 +00:00
|
|
|
inline uint16_t recordCount();
|
2010-01-23 02:13:59 +00:00
|
|
|
|
2011-06-09 00:07:42 +00:00
|
|
|
iterator begin(){return iterator(this);}
|
2010-02-15 23:02:01 +00:00
|
|
|
|
2011-06-09 00:07:42 +00:00
|
|
|
pageid_t get_start_pid(){return first_page_;}
|
|
|
|
int get_page_count(){return page_count_;}
|
2010-01-23 02:13:59 +00:00
|
|
|
|
2011-06-09 00:07:42 +00:00
|
|
|
static void register_stasis_page_impl();
|
2010-02-11 20:04:42 +00:00
|
|
|
|
2010-01-23 02:13:59 +00:00
|
|
|
private:
|
|
|
|
|
2011-06-09 00:07:42 +00:00
|
|
|
void initialize();
|
|
|
|
|
|
|
|
static const uint16_t DATA_PAGE_HEADER_SIZE = sizeof(int32_t);
|
|
|
|
static const uint16_t DATA_PAGE_SIZE = USABLE_SIZE_OF_PAGE - DATA_PAGE_HEADER_SIZE;
|
|
|
|
typedef uint32_t len_t;
|
|
|
|
|
|
|
|
static inline int32_t* is_another_page_ptr(Page *p) {
|
|
|
|
return stasis_page_int32_ptr_from_start(p,0);
|
|
|
|
}
|
|
|
|
static inline byte * data_at_offset_ptr(Page *p, slotid_t offset) {
|
|
|
|
return ((byte*)(is_another_page_ptr(p)+1))+offset;
|
|
|
|
}
|
|
|
|
static inline len_t * length_at_offset_ptr(Page *p, slotid_t offset) {
|
|
|
|
return (len_t*)data_at_offset_ptr(p,offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline recordid calc_chunk_from_offset(off_t offset) {
|
|
|
|
recordid ret;
|
|
|
|
ret.page = first_page_ + offset / DATA_PAGE_SIZE;
|
|
|
|
ret.slot = offset % DATA_PAGE_SIZE;
|
|
|
|
ret.size = DATA_PAGE_SIZE - ret.slot;
|
|
|
|
assert(ret.size);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t write_bytes(const byte * buf, ssize_t remaining, Page ** latch_p = NULL);
|
|
|
|
size_t read_bytes(byte * buf, off_t offset, ssize_t remaining);
|
|
|
|
Page * write_data_and_latch(const byte * buf, size_t len, bool init_next = true, bool latch = true);
|
|
|
|
bool write_data(const byte * buf, size_t len, bool init_next = true);
|
|
|
|
bool read_data(byte * buf, off_t offset, size_t len);
|
|
|
|
bool initialize_next_page();
|
|
|
|
void initialize_page(pageid_t pageid);
|
|
|
|
|
|
|
|
int xid_;
|
|
|
|
pageid_t page_count_;
|
|
|
|
const pageid_t initial_page_count_;
|
2012-02-23 01:11:55 +00:00
|
|
|
regionAllocator *alloc_;
|
2011-06-09 00:07:42 +00:00
|
|
|
const pageid_t first_page_;
|
|
|
|
off_t write_offset_; // points to the next free byte (ignoring page boundaries)
|
2010-01-23 02:13:59 +00:00
|
|
|
};
|
|
|
|
#endif
|