-
Notifications
You must be signed in to change notification settings - Fork 1
/
entry.c
196 lines (145 loc) · 4.94 KB
/
entry.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
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
//
// entry.c
// SD_2014
//
// Created by Grupo SD015 on 22/09/14.
//
//
#include "tuple.h"
#include "tuple-private.h"
#include "entry.h"
#include <stdlib.h>
#include <string.h> // memcpy
#include "message-private.h"
#include <stdio.h>
#include <assert.h>
/* Função que cria um novo par chave-valor (isto é, que inicializa
* a estrutura e aloca a memória necessária), recebendo um timestamp a atribuir.
*/
struct entry_t * entry_create2(struct tuple_t *tuple, long long timestamp ) {
struct entry_t *newEntry = (struct entry_t*) malloc(sizeof(struct entry_t));
if ( newEntry != NULL ) {
newEntry->timestamp = timestamp;
newEntry->value = tuple;
}
return newEntry;
}
/* Função que cria um novo par chave-valor (isto é, que inicializa
* a estrutura e aloca a memória necessária).
*/
struct entry_t *entry_create(struct tuple_t *tuple) {
return entry_create2(tuple, 0);
}
/* Função que destroi um par chave-valor e liberta toda a memoria.
*/
void entry_destroy(struct entry_t *entry) {
if ( entry != NULL) {
tuple_destroy(entry->value);
}
free(entry);
}
/* Funcao que duplica um par chave-valor. */
struct entry_t *entry_dup(struct entry_t *entry){
if ( entry == NULL || entry->value == NULL )
return NULL;
//if entry is valid
return entry_create(tuple_dup(entry->value));
}
/********** Implementation of entry-private.h ***********/
/*
* Returns the key of a given entry.
*/
char * entry_key (struct entry_t * entry) {
return tuple_key(entry->value);
}
/*
* Returns the value (tuple) of a given entry.
*/
struct tuple_t * entry_value ( struct entry_t * entry ) {
return entry->value;
}
/*
* Returns the value (tuple) of a given entry.
*/
int entry_newer_than ( struct entry_t * entryA, long long timestamp) {
return entryA->timestamp > timestamp;
}
long long entry_timestamp (struct entry_t * entry ) {
if ( entry == NULL )
return -1;
return entry->timestamp;
}
int entry_size_bytes ( struct entry_t * entry) {
if ( entry == NULL || entry->value == NULL)
return -1;
return TIMESTAMP_SIZE + tuple_size_bytes(entry->value);
}
int entry_serialize(struct entry_t * entry, char **serialized_entry) {
//safety checks
if ( entry == NULL || entry->value == NULL )
return -1;
//the tuple serialized (to buffer)
char *serialized_tuple = NULL;
//serializes the tuple and gets its size
int serialized_tuple_size = tuple_serialize(entry_value(entry), &serialized_tuple);
//size (bytes) of the serialized entry (to buffer)
int serialized_entry_size = TIMESTAMP_SIZE + serialized_tuple_size;
//offset to serialized_entry
int offset = 0;
//allocs the needed space for the buffer
serialized_entry[0] = (char*) malloc( serialized_entry_size );
//converts the timestamp (long long) to network format
long long timestamp_to_network = swap_bytes_64(entry_timestamp(entry));
//moves the timestamp to the buffer
memcpy(serialized_entry[0]+offset, ×tamp_to_network, TIMESTAMP_SIZE);
//moves offset
offset+=TIMESTAMP_SIZE;
//adds the serialized tuple to the serialized entry buffer
memcpy(serialized_entry[0]+offset, serialized_tuple, serialized_tuple_size);
offset+=serialized_tuple_size;
assert( serialized_entry_size == offset);
return serialized_entry_size;
}
struct entry_t *entry_deserialize(char *buffer, int buffer_size) {
//safety checks
if ( buffer == NULL || buffer_size == -1 )
return NULL;
//creates an empty entry build from the buffer
//offset
int offset = 0;
//gets the timestamp
long long timestamp_network = 0;
//sets its value from the buffer
memcpy(×tamp_network, buffer+offset, TIMESTAMP_SIZE);
//moves the offset
offset+=TIMESTAMP_SIZE;
//saves it to entry
long long timestamp_host = swap_bytes_64 ( timestamp_network );
//gets the tuple
struct tuple_t * tuple = tuple_deserialize(buffer+offset, buffer_size-offset);
//checks
assert ( tuple_size_bytes(tuple) == buffer_size-offset );
//moves the offset
offset+= tuple_size_bytes(tuple);
//checks
assert ( buffer_size == offset );
//finally creates the entry with the elements retrived from the buffer
struct entry_t * entry = entry_create2(tuple, timestamp_host);
return entry;
}
struct entry_t * entry_create_from_string(const char * input ) {
char *search = " ";
long long timestamp = 0;
char * inception = strdup(input);
char * rest;
// passa o opcode
strtok_r(inception, search, &rest );
//passa o ctype
strtok_r(NULL, search, &rest);
//gets the timestamp
char * timestamp_s = strtok_r(NULL, search, &rest);
//saves it has a long long
timestamp = atoll(timestamp_s);
free(inception);
return entry_create2(create_tuple_from_input(input), timestamp);
}