pqueue.cc 7.37 KB
Newer Older
1 2
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
// vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
3
#ident "$Id$"
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
/*
COPYING CONDITIONS NOTICE:

  This program is free software; you can redistribute it and/or modify
  it under the terms of version 2 of the GNU General Public License as
  published by the Free Software Foundation, and provided that the
  following conditions are met:

      * Redistributions of source code must retain this COPYING
        CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the
        DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the
        PATENT MARKING NOTICE (below), and the PATENT RIGHTS
        GRANT (below).

      * Redistributions in binary form must reproduce this COPYING
        CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the
        DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the
        PATENT MARKING NOTICE (below), and the PATENT RIGHTS
        GRANT (below) in the documentation and/or other materials
        provided with the distribution.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  02110-1301, USA.

COPYRIGHT NOTICE:

  TokuDB, Tokutek Fractal Tree Indexing Library.
  Copyright (C) 2007-2013 Tokutek, Inc.

DISCLAIMER:

  This program is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.

UNIVERSITY PATENT NOTICE:

  The technology is licensed by the Massachusetts Institute of
  Technology, Rutgers State University of New Jersey, and the Research
  Foundation of State University of New York at Stony Brook under
  United States of America Serial No. 11/760379 and to the patents
  and/or patent applications resulting from it.

PATENT MARKING NOTICE:

  This software is covered by US Patent No. 8,185,551.
53
  This software is covered by US Patent No. 8,489,638.
54 55 56

PATENT RIGHTS GRANT:

57
  "THIS IMPLEMENTATION" means the copyrightable works distributed by
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
  Tokutek as part of the Fractal Tree project.

  "PATENT CLAIMS" means the claims of patents that are owned or
  licensable by Tokutek, both currently or in the future; and that in
  the absence of this license would be infringed by THIS
  IMPLEMENTATION or by using or running THIS IMPLEMENTATION.

  "PATENT CHALLENGE" shall mean a challenge to the validity,
  patentability, enforceability and/or non-infringement of any of the
  PATENT CLAIMS or otherwise opposing any of the PATENT CLAIMS.

  Tokutek hereby grants to you, for the term and geographical scope of
  the PATENT CLAIMS, a non-exclusive, no-charge, royalty-free,
  irrevocable (except as stated in this section) patent license to
  make, have made, use, offer to sell, sell, import, transfer, and
  otherwise run, modify, and propagate the contents of THIS
  IMPLEMENTATION, where such license applies only to the PATENT
  CLAIMS.  This grant does not include claims that would be infringed
  only as a consequence of further modifications of THIS
  IMPLEMENTATION.  If you or your agent or licensee institute or order
  or agree to the institution of patent litigation against any entity
  (including a cross-claim or counterclaim in a lawsuit) alleging that
  THIS IMPLEMENTATION constitutes direct or contributory patent
  infringement, or inducement of patent infringement, then any rights
  granted to you under this License shall terminate as of the date
  such litigation is filed.  If you or your agent or exclusive
  licensee institute or order or agree to the institution of a PATENT
  CHALLENGE, then Tokutek may terminate any rights granted to you
  under this License.
*/

89
#ident "Copyright (c) 2010-2013 Tokutek Inc.  All rights reserved."
90 91
#ident "The technology is licensed by the Massachusetts Institute of Technology, Rutgers State University of New Jersey, and the Research Foundation of State University of New York at Stony Brook under United States of America Serial No. 11/760379 and to the patents and/or patent applications resulting from it."

Yoni Fogel's avatar
Yoni Fogel committed
92 93
#include <toku_portability.h>
#include "toku_os.h"
94
#include "ft-internal.h"
95 96
#include "loader/loader-internal.h"
#include "loader/pqueue.h"
97 98 99 100 101

#define pqueue_left(i)   ((i) << 1)
#define pqueue_right(i)  (((i) << 1) + 1)
#define pqueue_parent(i) ((i) >> 1)

102
int pqueue_init(pqueue_t **result, size_t n, int which_db, DB *db, ft_compare_func compare, struct error_callback_s *err_callback)
103
{
104 105 106 107
    pqueue_t *MALLOC(q);
    if (!q) {
        return get_error_errno();
    }
108 109

    /* Need to allocate n+1 elements since element 0 isn't used. */
110 111 112
    MALLOC_N(n + 1, q->d);
    if (!q->d) {
        int r = get_error_errno();
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
        toku_free(q);
        return r;
    }
    q->size = 1;
    q->avail = q->step = (n+1);  /* see comment above about n+1 */

    q->which_db = which_db;
    q->db = db;
    q->compare = compare;
    q->dup_error = 0;

    q->error_callback = err_callback;

    *result = q;
    return 0;
}

void pqueue_free(pqueue_t *q)
{
    toku_free(q->d);
    toku_free(q);
}


size_t pqueue_size(pqueue_t *q)
{
    /* queue element 0 exists but doesn't count since it isn't used. */
    return (q->size - 1);
}

static int pqueue_compare(pqueue_t *q, DBT *next_key, DBT *next_val, DBT *curr_key)
{
    int r = q->compare(q->db, next_key, curr_key);
    if ( r == 0 ) { // duplicate key : next_key == curr_key
        q->dup_error = 1; 
148
        if (q->error_callback)
149
            ft_loader_set_error_and_callback(q->error_callback, DB_KEYEXIST, q->db, q->which_db, next_key, next_val);
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
    }
    return ( r > -1 );
}

static void pqueue_bubble_up(pqueue_t *q, size_t i)
{
    size_t parent_node;
    pqueue_node_t *moving_node = q->d[i];
    DBT *moving_key = moving_node->key;

    for (parent_node = pqueue_parent(i);
         ((i > 1) && pqueue_compare(q, q->d[parent_node]->key, q->d[parent_node]->val, moving_key));
         i = parent_node, parent_node = pqueue_parent(i))
    {
        q->d[i] = q->d[parent_node];
    }

    q->d[i] = moving_node;
}


static size_t pqueue_maxchild(pqueue_t *q, size_t i)
{
    size_t child_node = pqueue_left(i);

    if (child_node >= q->size)
        return 0;

    if ((child_node+1) < q->size &&
        pqueue_compare(q, q->d[child_node]->key, q->d[child_node]->val, q->d[child_node+1]->key))
        child_node++; /* use right child instead of left */

    return child_node;
}


static void pqueue_percolate_down(pqueue_t *q, size_t i)
{
    size_t child_node;
    pqueue_node_t *moving_node = q->d[i];
    DBT *moving_key = moving_node->key;
    DBT *moving_val = moving_node->val;

    while ((child_node = pqueue_maxchild(q, i)) &&
           pqueue_compare(q, moving_key, moving_val, q->d[child_node]->key))
    {
        q->d[i] = q->d[child_node];
        i = child_node;
    }

    q->d[i] = moving_node;
}


int pqueue_insert(pqueue_t *q, pqueue_node_t *d)
{
    size_t i;

    if (!q) return 1;
    if (q->size >= q->avail) return 1;

    /* insert item */
    i = q->size++;
    q->d[i] = d;
    pqueue_bubble_up(q, i);

    if ( q->dup_error ) return DB_KEYEXIST;
    return 0;
}

int pqueue_pop(pqueue_t *q, pqueue_node_t **d)
{
    if (!q || q->size == 1) {
        *d = NULL;
        return 0;
    }

    *d = q->d[1];
    q->d[1] = q->d[--q->size];
    pqueue_percolate_down(q, 1);

    if ( q->dup_error ) return DB_KEYEXIST;
    return 0;
}