repairBlockStore.c 3.08 KB
Newer Older
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
#include <pthread.h>
#include "../../util/dbg.h"
#include "../../util/common.h"
#include "repairBlockStore.h"

PrrtRepairBlockStore *PrrtRepairBlockStore_create(void)
{
    PrrtRepairBlockStore *store = (PrrtRepairBlockStore *) calloc(1, sizeof(PrrtRepairBlockStore));
    check_mem(store);

    pthread_mutex_init(&store->lock, NULL);
    store->blockTree = NULL;

    return store;

    error:
    PERROR("Out of memory%s.", "");
    return NULL;
}

bool PrrtRepairBlockStore_destroy(PrrtRepairBlockStore *store)
{
    List *blockList = List_create();
    BPTree_get_range(store->blockTree, blockList, 0, SEQNO_SPACE - 1);
    while (List_count(blockList) > 0) {
        PrrtBlock *block = List_shift(blockList);
        PrrtBlock_destroy(block);
    }

    List_destroy(blockList);

    store->blockTree = BPTree_destroy(store->blockTree);

    pthread_mutex_destroy(&store->lock);
    free(store);
    return true;
}

bool PrrtRepairBlockStore_delete(PrrtRepairBlockStore *store, prrtSequenceNumber_t sequenceNumber)
{
    check(pthread_mutex_lock(&store->lock) == EXIT_SUCCESS, "Lock failed.");
    store->blockTree = BPTree_delete(store->blockTree, sequenceNumber);
    check(pthread_mutex_unlock(&store->lock) == EXIT_SUCCESS, "Unlock failed.");
    return true;
    error:
        PERROR("Could not delete%s.","");
        return false;
}

PrrtBlock *PrrtRepairBlockStore_get_block(PrrtRepairBlockStore *store, prrtSequenceNumber_t sequenceNumber)
{
    check(pthread_mutex_lock(&store->lock) == EXIT_SUCCESS, "Lock failed.");
    void *block = BPTree_get(store->blockTree, sequenceNumber);
    check(pthread_mutex_unlock(&store->lock) == EXIT_SUCCESS, "Unlock failed.");
    return block;
    error:
    PERROR("Could not get block%s.","");
    return NULL;
}

bool PrrtRepairBlockStore_insert(PrrtRepairBlockStore *store, prrtSequenceNumber_t sequenceNumber, PrrtBlock *block)
{
    check(pthread_mutex_lock(&store->lock) == EXIT_SUCCESS, "Lock failed.");
    store->blockTree = BPTree_insert(store->blockTree, sequenceNumber, block);
    check(pthread_mutex_unlock(&store->lock) == EXIT_SUCCESS, "Unlock failed.");
    return true;
    error:
    PERROR("Could not insert%s.","");
    return false;
}

uint32_t PrrtRepairBlockStore_size(PrrtRepairBlockStore *store)
{
    check(pthread_mutex_lock(&store->lock) == EXIT_SUCCESS, "Lock failed.");
    uint32_t size = BPTree_size(store->blockTree);
    check(pthread_mutex_unlock(&store->lock) == EXIT_SUCCESS, "Unlock failed.");
    return size;
    error:
    PERROR("Could not get size%s.","");
    return 0;
}

bool PrrtRepairBlockStore_expire_block_range(PrrtRepairBlockStore *store, prrtSequenceNumber_t start,
                                             prrtSequenceNumber_t stop)
{
    check(pthread_mutex_lock(&store->lock) == EXIT_SUCCESS, "Lock failed.");
    while(start != stop) {
        store->blockTree = BPTree_delete(store->blockTree, start);
        debug("EXPIRE BLOCK: %d", start);
        start++;
    }
    check(pthread_mutex_unlock(&store->lock) == EXIT_SUCCESS, "Unlock failed.");
    return true;
    error:
    PERROR("Could not expire%s.","");
    return false;
}