-
Notifications
You must be signed in to change notification settings - Fork 1
/
LedgerManager.c
88 lines (73 loc) · 2.42 KB
/
LedgerManager.c
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
// Implementation of ledger manager (both for VOL & voting mechanism use)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <limits.h>
#include "proposal.h"
#include "util_queue.h"
#include "metadata_update_helper.h"
#include "LedgerManager.h"
// ========================== Public functions ==========================
ledger_mgr* LM_ledger_manager_init(){
ledger_mgr* lm = calloc(1, sizeof(ledger_mgr));
gen_queue_init(&(lm->ledger_q));
return lm;
}
int LM_ledger_manager_term(ledger_mgr* lm){
assert(lm);
return -1;
}
int LM_add_ledger(ledger_mgr* lm, Queue_node* new_node){
gen_queue_append(&(lm->ledger_q), new_node);
return 0;
}
int LM_remove_ledger(ledger_mgr* lm, Queue_node* to_remove){
gen_queue_remove(&(lm->ledger_q), to_remove, 0);// remove but not to free it.
return 0;
}
int LM_iterate(ledger_mgr *lm, gen_queue_iter_cb cb, void *cb_ctx)
{
gen_queue_iterate(&(lm->ledger_q), cb, cb_ctx);
return 0;
}
int LM_ledger_cnt(ledger_mgr* lm){
assert(lm);
return lm->ledger_q.node_cnt;
}
Queue_node* LM_get_oldest_record(ledger_mgr* lm, time_stamp* pp_time_out){
assert(lm && pp_time_out);
if(lm->ledger_q.node_cnt == 0 || lm->ledger_q.q_state != Q_ACTIVE)
return NULL;
Queue_node* cur = lm->ledger_q.head;
Queue_node* old = lm->ledger_q.head;
proposal* r_next;
proposal* old_prop;
extern int MY_RANK_DEBUG;
while(cur){
if(!cur->next)//end of queue
break;
assert(cur->data);
assert(cur->next->data);
//proposal_buf_test(old->data);
//proposal_buf_test(cur->next->data);
old_prop = proposal_decoder(old->data);
r_next = proposal_decoder(cur->next->data);
if(old_prop->time > r_next->time)// found older recorder
old = cur->next;
else if(old_prop->time == r_next->time){
if(old_prop->pid > r_next->pid)//small pid wins
old = cur->next;
}
cur = cur->next;
free(old_prop);
free(r_next);
}
old_prop = proposal_decoder(old->data);
*pp_time_out = old_prop->time;
free(old_prop);
// printf("%s:rank %d: CHECKING ORDER: oldest proposal id = %d, time = %.ld\n",
// __func__, MY_RANK_DEBUG, ((proposal*)(old->data))->pid, ((proposal*)(old->data))->time);
return old;
}
// ========================== Private functions ==========================