Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 313 lines (256 sloc) 7.128 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
// Copyright (c) 2012, Cornell University
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of HyperDex nor the names of its contributors may be
// used to endorse or promote products derived from this software without
// specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

// C
#include <cassert>

// POSIX
#include <sys/mman.h>

// e
#include <e/atomic.h>
#include <e/guard.h>

// HyperDex
#include "disk/cuckoo_index.h"
#include "disk/cuckoo_table.h"

using hyperdex::cuckoo_index;
using hyperdex::cuckoo_returncode;
using hyperdex::cuckoo_table;

class cuckoo_index::table_list
{
    public:
        table_list();
        ~table_list() throw ();

    public:
        size_t size();
        table_info* info(size_t sz);

    private:
        friend class e::intrusive_ptr<table_list>;

    private:
        table_list(const table_list&);

    private:
        void inc();
        void dec();

    private:
        table_list& operator = (const table_list&);

    private:
        size_t m_ref;
        size_t m_sz;
        e::intrusive_ptr<table_info>* m_tables;
};

class cuckoo_index::table_info
{
    public:
        table_info();
        ~table_info() throw ();

    public:
        void* base;
        uint64_t lock;
        uint64_t lower_bound;
        cuckoo_table table;

    private:
        friend class e::intrusive_ptr<table_info>;

    private:
        table_info(const table_info&);

    private:
        void inc();
        void dec();

    private:
        table_info& operator = (const table_info&);

    private:
        size_t m_ref;
};

cuckoo_index :: cuckoo_index()
    : m_tables()
    , m_tables_lock()
    , m_table_locks(256)
{
    po6::threads::spinlock::hold hold(&m_tables_lock);
    m_tables = new table_list();
}

cuckoo_index :: ~cuckoo_index() throw ()
{
}

cuckoo_returncode
cuckoo_index :: insert(uint64_t key, uint64_t old_val, uint64_t new_val)
{
    while (true)
    {
        table_list_ptr t;

        {
            po6::threads::spinlock::hold hold(&m_tables_lock);
            t = m_tables;
        }

        size_t table_no = 0;
        assert(t->size() > 0);

        for (table_no = 0; table_no < t->size(); ++table_no)
        {
            if (key < t->info(table_no)->lower_bound)
            {
                break;
            }
        }

        --table_no;
        e::striped_lock<po6::threads::spinlock>::hold hold(&m_table_locks, t->info(table_no)->lock);

        {
            po6::threads::spinlock::hold hold2(&m_tables_lock);

            if (t != m_tables)
            {
                continue;
            }
        }

        switch (t->info(table_no)->table.insert(key, old_val, new_val))
        {
            case SUCCESS:
                return SUCCESS;
            case FULL:
                break;
            case NOT_FOUND:
            default:
                abort();
        }

        abort();
        e::intrusive_ptr<table_info> newtable = new table_info();
        newtable->lock = 0;
        t->info(table_no)->table.split(&newtable->table, &newtable->lower_bound);

        // XXX replace m_tables
    }
}

cuckoo_returncode
cuckoo_index :: lookup(uint64_t key, std::vector<uint64_t>* vals)
{
    table_list_ptr t;

    {
        po6::threads::spinlock::hold hold(&m_tables_lock);
        t = m_tables;
    }

    size_t table_no = 0;
    assert(t->size() > 0);

    for (table_no = 0; table_no < t->size(); ++table_no)
    {
        if (key < t->info(table_no)->lower_bound)
        {
            break;
        }
    }

    --table_no;
    e::striped_lock<po6::threads::spinlock>::hold hold(&m_table_locks, t->info(table_no)->lock);
    return t->info(table_no)->table.lookup(key, vals);
}

cuckoo_returncode
cuckoo_index :: remove(uint64_t key, uint64_t val)
{
    while (true)
    {
        table_list_ptr t;

        {
            po6::threads::spinlock::hold hold(&m_tables_lock);
            t = m_tables;
        }

        size_t table_no = 0;
        assert(t->size() > 0);

        for (table_no = 0; table_no < t->size(); ++table_no)
        {
            if (key < t->info(table_no)->lower_bound)
            {
                break;
            }
        }

        --table_no;
        e::striped_lock<po6::threads::spinlock>::hold hold(&m_table_locks, t->info(table_no)->lock);

        {
            po6::threads::spinlock::hold hold2(&m_tables_lock);

            if (t != m_tables)
            {
                continue;
            }
        }

        return t->info(table_no)->table.remove(key, val);
    }
}

cuckoo_index :: table_list :: table_list()
    : m_ref(0)
    , m_sz(1)
    , m_tables(new e::intrusive_ptr<table_info>[1])
{
    m_tables[0] = new table_info();
}

cuckoo_index :: table_list :: ~table_list() throw ()
{
    delete[] m_tables;
}

size_t
cuckoo_index :: table_list :: size()
{
    return m_sz;
}

cuckoo_index::table_info*
cuckoo_index :: table_list :: info(size_t i)
{
    assert(i < m_sz);
    return m_tables[i].get();
}

void
cuckoo_index :: table_list :: inc()
{
    e::atomic::increment_64_nobarrier(&m_ref, 1);
}

void
cuckoo_index :: table_list :: dec()
{
    e::atomic::increment_64_nobarrier(&m_ref, -1);

    if (m_ref == 0)
    {
        delete this;
    }
}

cuckoo_index :: table_info :: table_info()
    : base(mmap(NULL, 65536ULL*2ULL*64ULL, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_POPULATE, -1 , 0))
    , lock(0)
    , lower_bound(0)
    , table(base)
    , m_ref(0)
{
    if (base == MAP_FAILED)
    {
        throw std::bad_alloc();
    }
}

cuckoo_index :: table_info :: ~table_info() throw ()
{
}

void
cuckoo_index :: table_info :: inc()
{
    e::atomic::increment_64_nobarrier(&m_ref, 1);
}

void
cuckoo_index :: table_info :: dec()
{
    e::atomic::increment_64_nobarrier(&m_ref, -1);

    if (m_ref == 0)
    {
        delete this;
    }
}
Something went wrong with that request. Please try again.