-
Notifications
You must be signed in to change notification settings - Fork 0
/
kv.c
executable file
·1280 lines (1074 loc) · 34.9 KB
/
kv.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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "kv.h"
#include "common.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <inttypes.h>
#include <stdint.h>
#include <math.h> // compiler avec gcc -lm
#define taille_header_f 1
#define taille_header_b 4
#define TAILLE_BLOC 4096
#define NBIT(c,n) (((c) >> (n)) & 1) // macro qui détermine le n-ième bit d'un caractère
/*
* @brief Initialisation des têtes de lecture après les en-têtes
*
* @param kv descripteur d'accès à la base
*/
void kv_start(KV *kv)
{
lseek(kv->fd1, taille_header_f, SEEK_SET);
lseek(kv->fd2, taille_header_f, SEEK_SET);
lseek(kv->fd3, taille_header_f, SEEK_SET);
lseek(kv->fd4, taille_header_f, SEEK_SET);
}
/*
* @brief Récupère la longueur et la valeur associé à un index dans le .kv
*
* @param kv descripteur d'accès à la base
* @param val valeur modifiée par effet de bord
* @param offset index dans le .kv
*/
int readVal(KV *kv, kv_datum *val, len_t offset)
{
len_t lg_val;
if(lseek(kv->fd3, offset, SEEK_SET) < 0) {return -1;}
if(read(kv->fd3, &lg_val, 4) < 0) {return -1;}
if(val->ptr == NULL)
{
val->len = lg_val;
val->ptr = malloc(lg_val);
}
int val_retour;
if(val->len >= lg_val)
val_retour = read(kv->fd3, val->ptr, lg_val);
else
val_retour = read(kv->fd3, val->ptr, val->len);
return val_retour;
}
/*
* @brief 1ère fonction de hachage
*
* @param tab[]
*/
int hash0(const char tab[])
{
int i;
int somme = 0;
for(i = 0; tab[i] != '\0'; i++)
{
somme += tab[i];
}
return(somme % 999983);
}
/*
* @brief 2ème fonction de hachage
*
* @param tab[]
*/
int hash1(const char tab[])
{
int i;
int somme = 0;
for(i = 0; tab[i] != '\0'; i++)
{
somme += (i + 1) * tab[i];
}
return(somme % 999983);
}
/*
* @brief 3ème fonction de hachage limitée à des clés de 63 caractères
*
* Récupère à intervalle régulier des bits de tab[] afin d'en obtenir 32 et de construire un entier
*
* @param tab[]
*/
// int hash2(const char tab[])
// {
// int lg_tab = strlen(tab);
//
// int lg_bit = lg_tab * 8;
//
// int n_bit = (int)ceil((double)lg_bit/(double)32);
//
// int i, j, k;
//
// long long hash = 0;
//
// for(i = 0, j = 0, k = 0; i < lg_tab && k < 32; k++)
// {
// hash += NBIT(tab[i],j) * pow(2, k);
//
// j += n_bit;
//
// if(j > 7)
// {
// i++;
// j = j%8;
// }
// }
//
// return (int)(hash % 999983);
// }
/*
* @brief Détermine la fonction de hachage
*
* @param kv descripteur d'accès à la base
* @param tab[]
*/
int hash(const char tab[], KV *kv)
{
if(kv->hidx == 0)
{
return hash0(tab);
}
else if(kv->hidx == 1)
{
return hash1(tab);
}
// else if(kv->hidx == 2)
// {
// return hash1(tab);
// }
else
{
errno = EINVAL;
return -1;
}
}
/*
* @brief Initialise le .dkv
*
* Initialise le .dkv en écrivant un descripteur libre d'offset 1 et de taille 4294967295
*
* @param kv descripteur d'accès à la base
*/
int write_first_dkv(KV *kv)
{
int zero = 0;
len_t lg = 4294967295, offset = taille_header_f;
if(lseek(kv->fd4, taille_header_f, SEEK_SET) == -1){ return -1;}
if(write(kv->fd4, &zero, sizeof(int)) == -1) {return -1;}
if(write(kv->fd4, &lg, 4) == -1) {return -1;}
if(write(kv->fd4, &offset, 4) == -1) {return -1;}
return 42;
}
/*
* @brief Ouvre une base
*
* @param dbnamec nom de la base
* @param mode mode d'ouverture
* @param hidx fonction de hachage
* @param alloc mode d'allocation
*/
KV *kv_open (const char *dbnamec, const char *mode, int hidx, alloc_t alloc)
{
int fd1, fd2, fd3, fd4;
int longueur = 0;
KV *kv= malloc(sizeof(struct KV));
longueur = strlen(dbnamec);
char *namec= malloc((longueur+3)*sizeof(char));
char *nameblk= malloc((longueur+5)*sizeof(char));
char *namekv= malloc((longueur+4)*sizeof(char));
char *namedkv= malloc((longueur+5)*sizeof(char));
strcpy(namec, dbnamec);
strcpy(nameblk, dbnamec);
strcpy(namekv, dbnamec);
strcpy(namedkv, dbnamec);
strcat(namec, ".h");
strcat(nameblk, ".blk");
strcat(namekv, ".kv");
strcat(namedkv, ".dkv");
if(strcmp(mode, "r") == 0)
{
fd1=open(namec, O_RDONLY);
if(fd1 == -1){ perror(""); return NULL;}
fd2=open(nameblk, O_RDONLY);
if(fd2 == -1){ perror(""); return NULL;}
fd3=open(namekv, O_RDONLY);
if(fd3 == -1){ perror(""); return NULL;}
fd4=open(namedkv, O_RDONLY);
if(fd4 == -1){ perror(""); return NULL;}
}
else if(strcmp(mode, "r+") == 0)
{
fd1=open(namec, O_RDWR | O_CREAT, 0666);
if(fd1 == -1){ perror(""); return NULL;}
fd2=open(nameblk, O_RDWR | O_CREAT, 0666);
if(fd2 == -1){ perror(""); return NULL;}
fd3=open(namekv, O_RDWR | O_CREAT, 0666);
if(fd3 == -1){ perror(""); return NULL;}
fd4=open(namedkv, O_RDWR | O_CREAT, 0666);
if(fd4 == -1){ perror(""); return NULL;}
}
else if(strcmp(mode, "w") == 0)
{
fd1=open(namec, O_RDWR | O_CREAT | O_TRUNC, 0666);
if(fd1 == -1){ perror(""); return NULL;}
fd2=open(nameblk, O_RDWR | O_CREAT | O_TRUNC, 0666);
if(fd2 == -1){ perror(""); return NULL;}
fd3=open(namekv, O_RDWR | O_CREAT | O_TRUNC, 0666);
if(fd3 == -1){ perror(""); return NULL;}
fd4=open(namedkv, O_RDWR | O_CREAT | O_TRUNC, 0666);
if(fd4 == -1){ perror(""); return NULL;}
}
else if(strcmp(mode, "w+") == 0)
{
fd1=open(namec, O_RDWR | O_CREAT | O_TRUNC, 0666);
if(fd1 == -1){ perror(""); return NULL;}
fd2=open(nameblk, O_RDWR | O_CREAT | O_TRUNC, 0666);
if(fd2 == -1){ perror(""); return NULL;}
fd3=open(namekv, O_RDWR | O_CREAT | O_TRUNC, 0666);
if(fd3 == -1){ perror(""); return NULL;}
fd4=open(namedkv, O_RDWR | O_CREAT | O_TRUNC, 0666);
if(fd4 == -1){ perror(""); return NULL;}
}
kv->fd1 = fd1;
kv->fd2 = fd2;
kv->fd3 = fd3;
kv->fd4 = fd4;
kv->hidx = hidx;
kv->alloc = alloc;
char c_fd1, c_fd2, c_fd3, c_fd4;
int lg_fd1, lg_fd2, lg_fd3, lg_fd4;
if(lseek(fd1, 0, SEEK_SET) == -1){return NULL;}
if(lseek(fd2, 0, SEEK_SET) == -1){return NULL;}
if(lseek(fd3, 0, SEEK_SET) == -1){return NULL;}
if(lseek(fd4, 0, SEEK_SET) == -1){return NULL;}
lg_fd1 = read(fd1, &c_fd1, 1);
lg_fd2 = read(fd2, &c_fd2, 1);
lg_fd3 = read(fd3, &c_fd3, 1);
lg_fd4 = read(fd4, &c_fd4, 1);
if(lg_fd1 == -1 || lg_fd2 == -1 || lg_fd3 == -1 || lg_fd4 == -1){return NULL;}
char c1 = 'h';
char c2 = 'b';
char c3 = 'k';
char c4 = 'd';
if(lg_fd1 == 0 && (strcmp(mode, "w") == 0 || strcmp(mode, "w+") == 0 || strcmp(mode, "r+") == 0))
{
if(write(fd1, &c1, 1) == -1){return NULL;}
}
if(lg_fd2 == 0 && (strcmp(mode, "w") == 0 || strcmp(mode, "w+") == 0 || strcmp(mode, "r+") == 0))
{
if(write(fd2, &c2, 1) == -1){ return NULL;}
}
if(lg_fd3 == 0 && (strcmp(mode, "w") == 0 || strcmp(mode, "w+") == 0 || strcmp(mode, "r+") == 0))
{
if(write(fd3, &c3, 1) == -1){return NULL;}
}
if(lg_fd4 == 0 && (strcmp(mode, "w") == 0 || strcmp(mode, "w+") == 0 || strcmp(mode, "r+") == 0))
{
if(write(fd4, &c4, 1) == -1){return NULL;}
if(write_first_dkv(kv) == -1) {return NULL;}
}
if(lseek(fd1, 0, SEEK_SET) == -1){return NULL;}
if(lseek(fd2, 0, SEEK_SET) == -1){return NULL;}
if(lseek(fd3, 0, SEEK_SET) == -1){return NULL;}
if(lseek(fd4, 0, SEEK_SET) == -1){return NULL;}
lg_fd1 = read(fd1, &c_fd1, 1);
lg_fd2 = read(fd2, &c_fd2, 1);
lg_fd3 = read(fd3, &c_fd3, 1);
lg_fd4 = read(fd4, &c_fd4, 1);
if(lg_fd1 == -1 || lg_fd2 == -1 || lg_fd3 == -1 || lg_fd4 == -1){return NULL;}
if(c_fd1 != c1 || c_fd2 != c2 || c_fd3 != c3 || c_fd4 != c4 ) // vérification des numéros magiques
{
errno = EBADF;
return NULL;
}
if(strcmp(mode, "w") == 0)
{
close(fd1);
close(fd2);
close(fd3);
close(fd4);
fd1=open(namec, O_WRONLY | O_CREAT | O_TRUNC, 0666);
if(fd1 == -1){ perror(""); return NULL;}
fd2=open(nameblk, O_WRONLY | O_CREAT | O_TRUNC, 0666);
if(fd2 == -1){ perror(""); return NULL;}
fd3=open(namekv, O_WRONLY | O_CREAT | O_TRUNC, 0666);
if(fd3 == -1){ perror(""); return NULL;}
fd4=open(namedkv, O_WRONLY | O_CREAT | O_TRUNC, 0666);
if(fd4 == -1){ perror(""); return NULL;}
kv->fd1=fd1;
kv->fd2=fd2;
kv->fd3=fd3;
kv->fd4=fd4;
}
free(namec);
free(nameblk);
free(namekv);
free(namedkv);
return kv;
}
/*
* @brief Termine l'accès à la base
*
* @param kv descripteur d'accès à la base
*/
int kv_close(KV *kv)
{
close(kv->fd1);
close(kv->fd2);
close(kv->fd3);
close(kv->fd4);
free(kv);
return 0;
}
/*
* @brief Écrit un couple key/val dans le .kv
*
* @param kv descripteur d'accès à la base
* @param key clé
* @param val valeur
* @param offset index dans le .kv
*/
int writeData(KV *kv, const kv_datum *key, const kv_datum *val, len_t offset)
{
if(lseek(kv->fd3, offset, SEEK_SET) < 0) {return -1;}
if((write(kv->fd3, &(key->len), 4)) < 0) {return -1;}
if((write(kv->fd3, key->ptr, key->len)) < 0) {return -1;}
if((write(kv->fd3, &(val->len), 4)) < 0) {return -1;}
if((write(kv->fd3, val->ptr, val->len)) < 0) {return -1;}
return 42;
}
/*
* @brief Recherche de la clé dans la base et renvoi de l'offset correspondant
*
* @param kv descripteur d'accès à la base
* @param key clé
* @param offset index modifié par effet de bord
*/
int offset_cle(KV * kv, const kv_datum * key, len_t * offset)
{
kv_start(kv);
int val_hash = hash(key->ptr, kv);
len_t bloc_courant = 0, bloc_suivant = 0;
if(lseek(kv->fd1, val_hash * 4 , SEEK_CUR) < 0) {return -1;}
if(read(kv->fd1, &bloc_courant, 4) < 0){return -1;}
if(!bloc_courant)
{
errno = ENOENT;
return -1;
}
int boucle = 0;
while(boucle == 0)
{
if(lseek(kv->fd2, bloc_courant, SEEK_SET) < 0) {return -1;}
if(read(kv->fd2, &bloc_suivant, 4) < 0) {return -1;}
int i;
for(i=0; i<1023; i++)
{
len_t lg_cle, pos_cle;
if(read(kv->fd2, &pos_cle, 4) < 0) {return -1;}
if(pos_cle != 0)
{
if(lseek(kv->fd3, pos_cle, SEEK_SET) < 0) {return -1;}
if(read(kv->fd3, &lg_cle, 4) < 0) {return -1;}
if(lg_cle == key->len)
{
char * cle_lue = malloc(lg_cle + 1);
if(read(kv->fd3, cle_lue, lg_cle) < 0) {return -1;}
cle_lue[lg_cle]= '\0';
if(strcmp(key->ptr, cle_lue) == 0)
{
free(cle_lue);
*offset= lseek(kv->fd3, 0, SEEK_CUR);
return 1;
}
else
{
free(cle_lue);
}
}
}
}
if(bloc_suivant && bloc_suivant !=0)
{
bloc_courant = bloc_suivant;
}
else
{
boucle =1;
}
}
return -1;
}
/*
* @brief Recherche de la clé dans la base
*
* @param kv descripteur d'accès à la base
* @param key clé
* @param val valeur modifiée par effet de bord
*/
int kv_get(KV *kv, const kv_datum *key, kv_datum *val)
{
len_t offset;
if(offset_cle(kv, key, &offset) == 1)
{
return (readVal(kv, val, offset));
}
return 0;
}
/*
* @brief Renvoie la longueur totale de data
*
* @param data
*/
len_t get_size(const kv_datum *data)
{
return (4 + data->len);
}
/*
* @brief Écrit un descripteur dans le .dkv
*
* @param kv descripteur d'accès à la base
* @param offset_dkv index dans le .dkv
* @param est_occupe entier (emplacement libre ou non libre)
* @param longueur_couple len_t (longueur du couple dans le .kv)
* @param offset_couple len_t (offset du couple dans le .kv)
*/
int write_descripteur(KV *kv, const len_t offset_dkv, const int est_occupe, const len_t longueur_couple, const len_t offset_couple)
{
if(lseek(kv->fd4, offset_dkv, SEEK_SET) == -1) {return -1;}
if(write(kv->fd4, &est_occupe, sizeof(int)) == -1) {return -1;}
if(write(kv->fd4, &longueur_couple, 4) == -1) {return -1;}
if(write(kv->fd4, &offset_couple, 4) == -1) {return -1;}
return 1;
}
/*
* @brief Cherche la 1ère position dans laquelle écrire dans le .dkv
*
* @param kv descripteur d'accès à la base
* @param offset_dkv index dans le .dkv modifié par effet de bord
*/
int search_pos_dkv(KV *kv, len_t *offset_dkv)
{
kv_start(kv);
len_t emplacement_libre;
off_t offset;
while(read(kv->fd4, &emplacement_libre, sizeof(int)))
{
if(emplacement_libre == 2) // si le bit d'occupation est à 2 on peut réecrire dessus
{
offset = lseek(kv->fd4, 0, SEEK_CUR);
if(offset == -1){return -1;}
else
{
*offset_dkv = offset - sizeof(int);
return 42;
}
}
else
{
if(lseek(kv->fd4, 2 * 4, SEEK_CUR) < 0) {return -1;}
}
}
// sinon on renvoie l'index de la fin du fichier
off_t int_descripteur_max = lseek(kv->fd4, 0, SEEK_END);
if(int_descripteur_max == -1) {return -1;}
*offset_dkv = (len_t)int_descripteur_max;
return 42;
}
/*
* @brief Méthode d'allocation first_fit
*
* Écrit les nouveaux emplacements utilisés/libres dans le .dkv
*
* @param kv descripteur d'accès à la base
* @param key clé
* @param val valeur
* @param offset index dans le .kv modifié par effet de bord (doit être déjà alloué)
*/
int first_fit(KV *kv, const kv_datum *key, const kv_datum *val, len_t *offset)
{
int emplacement_libre = 0;
len_t taille_requise = get_size(key) + get_size(val), offset_descripteur_max;
if(search_pos_dkv(kv, &offset_descripteur_max) == -1) {return -1;}
if(lseek(kv->fd4, taille_header_f, SEEK_SET) == -1) {return -1;}
len_t offset_descripteur_sauvegarde, taille_courante, offset_courant, offset_min = UINT32_MAX, taille_sauvegarde = 0;
while(read(kv->fd4, &emplacement_libre, sizeof(int)))
{
if(emplacement_libre == 0) // si l'emplacement est libre
{
if(read(kv->fd4, &taille_courante, 4) < 0) {return -1;}
if(read(kv->fd4, &offset_courant, 4) < 0) {return -1;}
if(offset_courant < offset_min && taille_courante >= taille_requise) // si l'emplacement correspond
{
offset_min = offset_courant;
offset_descripteur_sauvegarde = lseek(kv->fd4, 0, SEEK_CUR) - (sizeof(int) + 2 * 4);
taille_sauvegarde = taille_courante;
}
}
else // si l'emplacement est occupé
{
if(lseek(kv->fd4, 2 * 4, SEEK_CUR) < 0) {return -1;}
}
}
if(taille_sauvegarde > 0)
{
// modification du .dkv
write_descripteur(kv, offset_descripteur_sauvegarde, 0, taille_sauvegarde - taille_requise, offset_min + taille_requise);
write_descripteur(kv, offset_descripteur_max, 1, taille_requise, offset_min);
*offset = offset_min;
return 42;
}
else{offset = NULL;errno=EDQUOT ; return -1;}
}
/*
* @brief Méthode d'allocation worst_fit
*
* Écrit les nouveaux emplacements utilisés/libres dans le .dkv
*
* @param kv descripteur d'accès à la base
* @param key clé
* @param val valeur
* @param offset index dans le .kv modifié par effet de bord (doit être déjà alloué)
*/
int worst_fit(KV *kv, const kv_datum *key, const kv_datum *val, len_t *offset)
{
int emplacement_libre = 0;
len_t taille_requise = get_size(key) + get_size(val), offset_descripteur_max;
if(search_pos_dkv(kv, &offset_descripteur_max) == -1) {return -1;}
if(lseek(kv->fd4, taille_header_f, SEEK_SET) == -1) {return -1;}
len_t taille_courante, offset_courant, taille_max = 0, offset_sauvegarde, offset_descripteur_sauvegarde, taille_max2 = 0, offset_sauvegarde2, offset_descripteur_sauvegarde2;
while(read(kv->fd4, &emplacement_libre, sizeof(int)))
{
if(emplacement_libre == 0) // si l'emplacement est libre
{
if(read(kv->fd4, &taille_courante, 4) < 0) {return -1;}
if(read(kv->fd4, &offset_courant, 4) < 0) {return -1;}
if(taille_courante == UINT32_MAX) // si rien n'est encore écrit dans la base
{
offset_sauvegarde = offset_courant;
offset_descripteur_sauvegarde = lseek(kv->fd4, 0, SEEK_CUR) - (sizeof(int) + 2 * 4);
taille_max = taille_courante;
// modification du .dkv
write_descripteur(kv, offset_descripteur_sauvegarde, 0, taille_max - taille_requise, offset_sauvegarde + taille_requise);
write_descripteur(kv, offset_descripteur_max, 1, taille_requise, offset_sauvegarde);
*offset = offset_sauvegarde;
return 42;
}
if((taille_courante > taille_max) && (taille_courante >= taille_requise) && ((taille_courante + offset_courant - 1) == UINT32_MAX))
{
offset_sauvegarde2 = offset_courant;
offset_descripteur_sauvegarde2 = lseek(kv->fd4, 0, SEEK_CUR) - (sizeof(int) + 2 * 4);
taille_max2 = taille_courante;
}
if((taille_courante > taille_max) && (taille_courante >= taille_requise) && ((taille_courante + offset_courant - 1) != UINT32_MAX)) // on vérifie si l'emplacement est plus grand
{
offset_sauvegarde = offset_courant;
offset_descripteur_sauvegarde = lseek(kv->fd4, 0, SEEK_CUR) - (sizeof(int) + 2 * 4);
taille_max = taille_courante;
}
}
else // si l'emplacement est occupé
{
if(lseek(kv->fd4, 2 * 4, SEEK_CUR) < 0) {return -1;}
}
}
if(taille_max >= taille_requise)
{
// modification du .dkv
write_descripteur(kv, offset_descripteur_sauvegarde, 0, taille_max - taille_requise, offset_sauvegarde + taille_requise);
write_descripteur(kv, offset_descripteur_max, 1, taille_requise, offset_sauvegarde);
*offset = offset_sauvegarde;
return 42;
}
else if(taille_max2 >= taille_requise)
{
write_descripteur(kv, offset_descripteur_sauvegarde2, 0, taille_max2 - taille_requise, offset_sauvegarde2 + taille_requise);
write_descripteur(kv, offset_descripteur_max, 1, taille_requise, offset_sauvegarde2);
*offset = offset_sauvegarde2;
return 42;
}
else{offset = NULL; errno=EDQUOT ; return -1;}
}
/*
* @brief Méthode d'allocation best_fit
*
* Écrit les nouveaux emplacements utilisés/libres dans le .dkv
*
* @param kv descripteur d'accès à la base
* @param key clé
* @param val valeur
* @param offset index dans le .kv modifié par effet de bord (doit être déjà alloué)
*/
int best_fit(KV *kv, const kv_datum *key, const kv_datum *val, len_t *offset)
{
int emplacement_libre = 0;
len_t taille_requise = get_size(key) + get_size(val), offset_descripteur_max;
if(search_pos_dkv(kv, &offset_descripteur_max) == -1) {return -1;}
if(lseek(kv->fd4, taille_header_f, SEEK_SET) == -1) {return -1;}
len_t taille_courante, taille_min = UINT32_MAX, offset_lue, offset_sauvegarde, offset_descripteur_sauvegarde;
while(read(kv->fd4, &emplacement_libre, sizeof(int)))
{
if(emplacement_libre == 0) // si l'emplacement est libre
{
if(read(kv->fd4, &taille_courante, 4) < 0) {return -1;}
if(taille_courante == UINT32_MAX)
{
if(read(kv->fd4, &offset_sauvegarde, 4) < 0) {return -1;}
offset_descripteur_sauvegarde = lseek(kv->fd4, 0, SEEK_CUR) - (sizeof(int) + 2 * 4);
taille_min = taille_courante;
// modification du .dkv
write_descripteur(kv, offset_descripteur_sauvegarde, 0, taille_min - taille_requise, offset_sauvegarde + taille_requise);
write_descripteur(kv, offset_descripteur_max, 1, taille_requise, offset_sauvegarde);
*offset = offset_sauvegarde;
return 42;
}
if(read(kv->fd4, &offset_lue, 4) < 0) {return -1;}
// on vérifie si l'emplacement est assez grand et plus petit ainsi que sa position
if((taille_requise <= taille_courante && taille_courante < taille_min) || (taille_requise <= taille_courante && taille_courante == taille_min && offset_lue < offset_sauvegarde))
{
offset_sauvegarde = offset_lue;
offset_descripteur_sauvegarde = lseek(kv->fd4, 0, SEEK_CUR) - (sizeof(int) + 2 * 4);
taille_min = taille_courante;
}
}
else // si l'emplacement est occupé
{
if(lseek(kv->fd4, 2 * 4, SEEK_CUR) < 0) {return -1;}
}
}
if(taille_min < UINT32_MAX)
{
// modification du .dkv
write_descripteur(kv, offset_descripteur_sauvegarde, 0, taille_min - taille_requise, offset_sauvegarde + taille_requise);
write_descripteur(kv, offset_descripteur_max, 1, taille_requise, offset_sauvegarde);
*offset = offset_sauvegarde;
return 42;
}
else {offset = NULL;errno=EDQUOT ; return -1;}
}
/*
* @brief Détermine la bonne méthode d'allocation
*
* @param kv descripteur d'accès à la base
* @param key clé
* @param val valeur
* @param offset index dans le .kv modifié par effet de bord
*/
int kv_put_dkv(KV *kv, const kv_datum *key, const kv_datum *val, len_t *offset)
{
if(kv->alloc == 0)
{
return first_fit(kv, key, val, offset);
}
else if(kv->alloc == 1)
{
return worst_fit(kv, key, val, offset);
}
else if(kv->alloc == 2)
{
return best_fit(kv, key, val, offset);
}
else
{
errno = EINVAL;
return -1;
}
}
/*
* @brief Lit une en-tête dans .blk
*
* @param kv descripteur d'accès à la base
* @param offset_bloc index dans le .blk
* @param nouveau_offset index du prochain bloc dans le .blk modifié par effet de bord
*/
int read_entete_bloc(KV *kv, const len_t offset_bloc, len_t * nouveau_offset)
{
if(lseek(kv->fd2, offset_bloc, SEEK_SET) < 0) {return -1;}
if(read(kv->fd2, nouveau_offset, 4) < 0) {return -1;}
return 42;
}
/*
* @brief Suppression de la clé dans la base
*
* Remet à zéro l'index adéquat dans le .blk
*
* Fusionne si besoin les emplacements dans le .dkv et met le bit d'occupation à 2
* pour les emplacements potentiellement restants et non utilisés
*
* On évite de décaler tous les descripteurs dans le .dkv
*
* @param kv descripteur d'accès à la base
* @param key clé
*/
int kv_del(KV * kv, const kv_datum * key)
{
kv_start(kv);
int val_hash = hash(key->ptr, kv);
len_t bloc_courant, bloc_suivant=0;
if(lseek(kv->fd1, val_hash*4 , SEEK_CUR) <0) {return -1;}
if(read(kv->fd1, &bloc_courant, 4) <0){return -1;}
if(!bloc_courant)
{
errno= ENOENT;
return -1;
}
int boucle=0;
while(boucle == 0)
{
if(lseek(kv->fd2, bloc_courant, SEEK_SET) <0){return -1;}
if(read(kv->fd2, &bloc_suivant, 4) <0){return -1;}
int i;
for(i=0; i<1023; i++)
{
len_t lg_cle, pos_cle;
if(read(kv->fd2, &pos_cle, 4) <0){return -1;}
if(pos_cle !=0)
{
if(lseek(kv->fd3, pos_cle, SEEK_SET) < 0) {return -1;}
if(read(kv->fd3, &lg_cle, 4) < 0) {return -1;}
if(lg_cle == key->len)
{
char * cle_lue = malloc(lg_cle + 1);
if(read(kv->fd3, cle_lue, lg_cle) < 0) {return -1;}
cle_lue[lg_cle] = '\0';
if(strcmp(key->ptr, cle_lue) == 0)
{
free(cle_lue);
len_t zero = 0;
if(lseek(kv->fd2, -4, SEEK_CUR) == -1) {return -1;}
if(write(kv->fd2, &zero, 4) < 0) {return -1;}
len_t off_lue;
int libre;
len_t lg_atruncate;
lseek(kv->fd4, taille_header_f, SEEK_SET);
while(read(kv->fd4, &libre, sizeof(int)))
{
if(lseek(kv->fd4, 4, SEEK_CUR) < 0) {return -1;}
if(read(kv->fd4, &off_lue, 4) < 0) {return -1;}
if(off_lue == pos_cle)
{
int zero_int =0;
if(lseek(kv->fd4, -12, SEEK_CUR) == -1) {return -1;}
if(write(kv->fd4, &zero_int, 4) < 0) {return -1;}
if(read(kv->fd4, &lg_atruncate, 4) != 4){return -1;}
len_t atruncate=lseek(kv->fd4, -8, SEEK_CUR);
// printf("on cherche: \n");
// printf("e: %" PRIu16 " lg %" PRIu16 " off %" PRIu16 "\n", libre, lg_atruncate, pos_cle);
if(lseek(kv->fd4, taille_header_f, SEEK_SET) == -1){return -1;}
int existe;
int flag_while=0;
while((read(kv->fd4, &existe, sizeof(int))) && (flag_while != 2))
{
if(existe==0)
{
len_t lg, off;
if(read(kv->fd4, &lg, 4) < 4){return -1;}
if(read(kv->fd4, &off, 4) < 4){return -1;}
if(lg+off == off_lue)
{
// printf("a fusionner: \n");
// printf("e: %" PRIu16 " lg %" PRIu16 " off %" PRIu16 "\n", existe, lg, off);
//modifier lg clé il faut ajouter la longueur total et pas celle de la clé
lg_atruncate= lg+lg_atruncate;
len_t pos_tmp;
if(lseek(kv->fd4, -8, SEEK_CUR) == -1){return -1;}
if(write(kv->fd4, &lg_atruncate, 4) != 4){return -1;}
pos_tmp=lseek(kv->fd4, 4, SEEK_CUR);
if(lseek(kv->fd4, atruncate, SEEK_SET) == -1){return -1;}
int deux = 2;
if(write(kv->fd4, &deux, 4) != 4){return -1;}
atruncate=pos_tmp-12;
off_lue= off;
flag_while++;
if(lseek(kv->fd4, pos_tmp, SEEK_SET) == -1){return -1;}
// printf("e: %" PRIu16 " lg %" PRIu16 " off %" PRIu16 "\n", existe, lg_atruncate, off_lue);
}
else if(off == off_lue + lg_atruncate)
{
// printf("a fusionner: \n");
// printf("e: %" PRIu16 " lg %" PRIu16 " off %" PRIu16 "\n", existe, lg, off);
//modifier lg clé il faut ajouter la longueur total et pas celle de la clé
lg_atruncate= lg + lg_atruncate;
len_t pos_tmp;
if(lseek(kv->fd4, -8, SEEK_CUR) == -1){return -1;}
if(write(kv->fd4, &lg_atruncate, 4) != 4){return -1;}
if(write(kv->fd4, &off_lue, 4) != 4){return -1;}
pos_tmp=lseek(kv->fd4, 0, SEEK_CUR);
if(lseek(kv->fd4, atruncate, SEEK_SET) == -1){return -1;}
int deux = 2;
if(write(kv->fd4, &deux, 4) != 4){return -1;}
atruncate=pos_tmp-12;
flag_while++;
if(lseek(kv->fd4, pos_tmp, SEEK_SET) == -1){return -1;}
// printf("e: %" PRIu16 " lg %" PRIu16 " off %" PRIu16 "\n", existe, lg_atruncate, off_lue);
}
}
else
{
if(lseek(kv->fd4, 8, SEEK_CUR) == -1){return -1;}
}
}
return 0;
}
}
}
else
free(cle_lue);
}
}
}
if(bloc_suivant && bloc_suivant != 0)
{
bloc_courant = bloc_suivant;
}
else
{
boucle =-1;
}
}
errno = ENOENT;
return -1;
}
/*
* @brief Lecture d'une entrée dans le .h
*
* @param kv descripteur d'accès à la base
* @param offset_h index dans le .h
* @param offset_blk index dans le .blk modifié par effet de bord
*/
int read_h(KV *kv, len_t offset_h, len_t * val_h)
{
kv_start(kv);
if(lseek(kv->fd1, offset_h * 4, SEEK_CUR) < 0) {return -1;}
int n = read(kv->fd1, val_h, 4);
if(n == -1) {return -1;}
return n;
}
/*
* @brief Écrit une entrée dans le .h