Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 520 lines (424 sloc) 15.106 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
/*
* Copyright (C) 2005-2012 Christoph Rupp (chris@crupp.de).
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* See files COPYING.* for License information.
*/

/**
* device management; a device encapsulates the physical device, either a
* file or memory chunks (for in-memory-databases)
*
*/

#ifndef HAM_DEVICE_H__
#define HAM_DEVICE_H__

#include "internal_fwd_decl.h"
#include "os.h"
#include "mem.h"
#include "db.h"

class Page;
class Device;
class DeviceImplInMemory;
class DeviceImplDisk;


class DeviceImplementation {
  public:
    /** constructor */
    DeviceImplementation(Device *device)
      : m_device(device), m_pagesize(0) {
    }

    /** Create a new device */
    virtual ham_status_t create(const char *filename, ham_u32_t flags,
                ham_u32_t mode) = 0;

    /** opens an existing device */
    virtual ham_status_t open(const char *filename, ham_u32_t flags) = 0;

    /** closes the device */
    virtual ham_status_t close() = 0;

    /** flushes the device */
    virtual ham_status_t flush() = 0;

    /** truncate/resize the device */
    virtual ham_status_t truncate(ham_offset_t newsize) = 0;

    /** returns true if the device is open */
    virtual bool is_open() = 0;

    /** get the current file/storage size */
    virtual ham_status_t get_filesize(ham_offset_t *length) = 0;

    /** seek position in a file */
    virtual ham_status_t seek(ham_offset_t offset, int whence) = 0;

    /** tell the position in a file */
    virtual ham_status_t tell(ham_offset_t *offset) = 0;

    /** reads from the device; this function does not use mmap */
    virtual ham_status_t read(ham_offset_t offset, void *buffer,
                ham_offset_t size) = 0;

    /** writes to the device; this function does not use mmap,
* and is responsible for writing the data is run through the file
* filters */
    virtual ham_status_t write(ham_offset_t offset, void *buffer,
                ham_offset_t size) = 0;

    /** reads a page from the device; this function CAN use mmap */
    virtual ham_status_t read_page(Page *page) = 0;

    /** writes a page to the device */
    virtual ham_status_t write_page(Page *page) = 0;

    /** allocate storage from this device; this function
* will *NOT* use mmap. */
    virtual ham_status_t alloc(ham_size_t size, ham_offset_t *address) = 0;

    /**
* allocate storage for a page from this device; this function
* can use mmap if available
*
* @note
* The caller is responsible for flushing the page; the @ref free_page
* function will assert that the page is not dirty.
*/
    virtual ham_status_t alloc_page(Page *page) = 0;

    /** frees a page on the device; plays counterpoint to @ref alloc_page */
    virtual ham_status_t free_page(Page *page) = 0;

    /** sets the pagesize */
    void set_pagesize(ham_size_t ps) {
        m_pagesize=ps;
    }

    /** gets the pagesize */
    ham_size_t get_pagesize() {
        return (m_pagesize);
    }

  protected:
    /** the Device object which created this DeviceImplementation */
    Device *m_device;

    /** the pagesize */
    ham_size_t m_pagesize;
};


/**
* a File-based device
*/
class DeviceImplDisk : public DeviceImplementation {
  public:
    DeviceImplDisk(Device *device)
      : DeviceImplementation(device) {
    }

    /** Create a new device */
    virtual ham_status_t create(const char *filename, ham_u32_t flags,
                ham_u32_t mode) {
        return (os_create(filename, flags, mode, &m_fd));
    }

    /** opens an existing device */
    virtual ham_status_t open(const char *filename, ham_u32_t flags) {
        return (os_open(filename, flags, &m_fd));
    }

    /** closes the device */
    virtual ham_status_t close() {
        ham_status_t st=os_close(m_fd);
        if (st==HAM_SUCCESS)
            m_fd=HAM_INVALID_FD;
        return (st);
    }

    /** flushes the device */
    virtual ham_status_t flush() {
        return (os_flush(m_fd));
    }

    /** truncate/resize the device */
    virtual ham_status_t truncate(ham_offset_t newsize) {
        return (os_truncate(m_fd, newsize));
    }

    /** returns true if the device is open */
    virtual bool is_open() {
        return (HAM_INVALID_FD!=m_fd);
    }

    /** get the current file/storage size */
    virtual ham_status_t get_filesize(ham_offset_t *length) {
        *length=0;
        return (os_get_filesize(m_fd, length));
    }

    /** seek position in a file */
    virtual ham_status_t seek(ham_offset_t offset, int whence) {
return (os_seek(m_fd, offset, whence));
    }

    /** tell the position in a file */
    virtual ham_status_t tell(ham_offset_t *offset) {
return (os_tell(m_fd, offset));
    }

    /** reads from the device; this function does not use mmap */
    virtual ham_status_t read(ham_offset_t offset, void *buffer,
                ham_offset_t size);

    /** writes to the device; this function does not use mmap,
* and is responsible for writing the data is run through the file
* filters */
    virtual ham_status_t write(ham_offset_t offset, void *buffer,
                ham_offset_t size);

    /** reads a page from the device; this function CAN use mmap */
    virtual ham_status_t read_page(Page *page);

    /** writes a page to the device */
    virtual ham_status_t write_page(Page *page);

    /** allocate storage from this device; this function
* will *NOT* use mmap. */
    virtual ham_status_t alloc(ham_size_t size, ham_offset_t *address) {
        ham_status_t st=os_get_filesize(m_fd, address);
        if (st)
            return (st);
        return (os_truncate(m_fd, (*address)+size));
    }

    /**
* allocate storage for a page from this device; this function
* can use mmap if available
*
* @note
* The caller is responsible for flushing the page; the @ref free_page
* function will assert that the page is not dirty.
*/
    virtual ham_status_t alloc_page(Page *page);

    /** frees a page on the device; plays counterpoint to @ref alloc_page */
    virtual ham_status_t free_page(Page *page);

  private:
    ham_fd_t m_fd;
};


/**
* an In-Memory device
*/
class DeviceImplInMemory : public DeviceImplementation {
  public:
    /** constructor */
    DeviceImplInMemory(Device *device)
      : DeviceImplementation(device) {
    }

    /** Create a new device */
    virtual ham_status_t create(const char *filename, ham_u32_t flags,
                ham_u32_t mode) {
        m_is_open=true;
        return (0);
    }

    /** opens an existing device */
    virtual ham_status_t open(const char *filename, ham_u32_t flags) {
        ham_assert(!"can't open an in-memory-device", (0));
        return (HAM_NOT_IMPLEMENTED);
    }

    /** closes the device */
    virtual ham_status_t close() {
        ham_assert(m_is_open, (0));
        m_is_open=false;
        return (HAM_SUCCESS);
    }

    /** flushes the device */
    virtual ham_status_t flush() {
        return (HAM_SUCCESS);
    }

    /** truncate/resize the device */
    virtual ham_status_t truncate(ham_offset_t newsize) {
        return (HAM_SUCCESS);
    }

    /** returns true if the device is open */
    virtual bool is_open() {
        return (m_is_open);
    }

    /** get the current file/storage size */
    virtual ham_status_t get_filesize(ham_offset_t *length) {
        ham_assert(!"this operation is not possible for in-memory-databases",
                    (0));
        return (HAM_NOT_IMPLEMENTED);
    }

    /** seek position in a file */
    virtual ham_status_t seek(ham_offset_t offset, int whence) {
        ham_assert(!"can't seek in an in-memory-device", (0));
        return (HAM_NOT_IMPLEMENTED);
    }

    /** tell the position in a file */
    virtual ham_status_t tell(ham_offset_t *offset) {
        ham_assert(!"can't tell in an in-memory-device", (0));
        return (HAM_NOT_IMPLEMENTED);
    }

    /** reads from the device; this function does not use mmap */
    virtual ham_status_t read(ham_offset_t offset, void *buffer,
                ham_offset_t size) {
        ham_assert(!"operation is not possible for in-memory-databases", (0));
        return (HAM_NOT_IMPLEMENTED);
    }

    /** writes to the device; this function does not use mmap,
* and is responsible for writing the data is run through the file
* filters */
    virtual ham_status_t write(ham_offset_t offset, void *buffer,
                ham_offset_t size) {
        ham_assert(!"operation is not possible for in-memory-databases", (0));
        return (HAM_NOT_IMPLEMENTED);
    }

    /** reads a page from the device; this function CAN use mmap */
    virtual ham_status_t read_page(Page *page) {
        ham_assert(!"operation is not possible for in-memory-databases", (0));
        return (HAM_NOT_IMPLEMENTED);
    }

    /** writes a page to the device */
    virtual ham_status_t write_page(Page *page) {
        ham_assert(!"operation is not possible for in-memory-databases", (0));
        return (HAM_NOT_IMPLEMENTED);
    }

    /** allocate storage from this device; this function
* will *NOT* use mmap. */
    virtual ham_status_t alloc(ham_size_t size, ham_offset_t *address) {
        ham_assert(!"can't alloc from an in-memory-device", (0));
        return (HAM_NOT_IMPLEMENTED);
    }

    /**
* allocate storage for a page from this device; this function
* can use mmap if available
*
* @note
* The caller is responsible for flushing the page; the @ref free_page
* function will assert that the page is not dirty.
*/
    virtual ham_status_t alloc_page(Page *page);

    /** frees a page on the device; plays counterpoint to @ref alloc_page */
    virtual ham_status_t free_page(Page *page);

  private:
    bool m_is_open;
};


class Device {
  public:
    /** constructor */
    Device(Environment *env, ham_u32_t flags)
      : m_env(env), m_flags(flags) {
        if (flags&HAM_IN_MEMORY_DB)
            m_impl=new DeviceImplInMemory(this);
        else
            m_impl=new DeviceImplDisk(this);

        /*
* initialize the pagesize with a default value - this will be
* overwritten i.e. by ham_open, ham_create when the pagesize
* of the file is known
*/
        set_pagesize(get_pagesize());
    }

    /** virtual destructor */
    ~Device() {
        ScopedLock lock(m_mutex);
        delete m_impl;
        m_impl=0;
    }

    /** set the flags */
    void set_flags(ham_u32_t flags) {
        ScopedLock lock(m_mutex);
        m_flags=flags;
    }

    /** get the flags */
    ham_u32_t get_flags() {
        ScopedLock lock(m_mutex);
        return (m_flags);
    }

    /** Create a new device */
    ham_status_t create(const char *filename, ham_u32_t flags,
                ham_u32_t mode) {
        ScopedLock lock(m_mutex);
        m_flags=flags;
        return (m_impl->create(filename, flags, mode));
    }

    /** opens an existing device */
    ham_status_t open(const char *filename, ham_u32_t flags) {
        ScopedLock lock(m_mutex);
        m_flags=flags;
        return (m_impl->open(filename, flags));
    }

    /** closes the device */
    ham_status_t close() {
        ScopedLock lock(m_mutex);
        return (m_impl->close());
    }

    /** flushes the device */
    ham_status_t flush() {
        ScopedLock lock(m_mutex);
        return (m_impl->flush());
    }

    /** truncate/resize the device */
    ham_status_t truncate(ham_offset_t newsize) {
        ScopedLock lock(m_mutex);
        return (m_impl->truncate(newsize));
    }

    /** returns true if the device is open */
    bool is_open() {
        ScopedLock lock(m_mutex);
        return (m_impl->is_open());
    }

    /** get the current file/storage size */
    ham_status_t get_filesize(ham_offset_t *length) {
        ScopedLock lock(m_mutex);
        return (m_impl->get_filesize(length));
    }

    /** seek position in a file */
    ham_status_t seek(ham_offset_t offset, int whence) {
        ScopedLock lock(m_mutex);
        return (m_impl->seek(offset, whence));
    }

    /** tell the position in a file */
    ham_status_t tell(ham_offset_t *offset) {
        ScopedLock lock(m_mutex);
        return (m_impl->tell(offset));
    }

    /** reads from the device; this function does not use mmap */
    ham_status_t read(ham_offset_t offset, void *buffer, ham_offset_t size) {
        ScopedLock lock(m_mutex);
        return (m_impl->read(offset, buffer, size));
    }

    /** writes to the device; this function does not use mmap,
* and is responsible for writing the data is run through the file
* filters */
    ham_status_t write(ham_offset_t offset, void *buffer, ham_offset_t size) {
        ScopedLock lock(m_mutex);
        return (m_impl->write(offset, buffer, size));
    }

    /** reads a page from the device; this function CAN use mmap */
    ham_status_t read_page(Page *page) {
        ScopedLock lock(m_mutex);
        return (m_impl->read_page(page));
    }

    /** writes a page to the device */
    ham_status_t write_page(Page *page) {
        ScopedLock lock(m_mutex);
        return (m_impl->write_page(page));
    }

    /** allocate storage from this device; this function
* will *NOT* use mmap. */
    ham_status_t alloc(ham_size_t size, ham_offset_t *address) {
        ScopedLock lock(m_mutex);
        return (m_impl->alloc(size, address));
    }

    /**
* allocate storage for a page from this device; this function
* can use mmap if available
*
* @note
* The caller is responsible for flushing the page; the @ref free_page
* function will assert that the page is not dirty.
*/
    ham_status_t alloc_page(Page *page) {
        ScopedLock lock(m_mutex);
        return (m_impl->alloc_page(page));
    }

    /** frees a page on the device; plays counterpoint to @ref alloc_page */
    ham_status_t free_page(Page *page) {
        ScopedLock lock(m_mutex);
        return (m_impl->free_page(page));
    }

    /** get the Environment */
    Environment *get_env() {
        ScopedLock lock(m_mutex);
        return (m_env);
    }

    /** set the pagesize for this device */
    void set_pagesize(ham_size_t pagesize) {
        ScopedLock lock(m_mutex);
        m_impl->set_pagesize(pagesize);
    }

    /** get the pagesize for this device */
    ham_size_t get_pagesize() {
        ScopedLock lock(m_mutex);
        return (m_impl->get_pagesize());
    }

  protected:
    friend class DeviceImplDisk;
    friend class DeviceImplInMemory;

    /** a mutex to protect the device */
    Mutex m_mutex;

    /** The actual implementation */
    DeviceImplementation *m_impl;

    /** the environment which employs this device */
    Environment *m_env;

    /** the device flags */
    ham_u32_t m_flags;
};


#endif /* HAM_DEVICE_H__ */
Something went wrong with that request. Please try again.