Line data Source code
1 : #include "../inc/header.h"
2 : #include "../inc/param_manager.h"
3 : #include "../inc/global_param.h"
4 : #include "../inc/utils.h"
5 :
6 : #include <stdlib.h>
7 :
8 : #ifndef ONLY_CPU
9 : #define testCUDA(error){testCUDA(error, __FILE__, __LINE__);}
10 : #endif
11 :
12 :
13 0 : void saveListIndexToFile(std::vector<uint32_t> & ids, size_t nb_data, std::vector<uint32_t> & type_data,
14 : std::string filename_list_index_file, std::vector<uint32_t> & id_index_ids, int verbose)
15 : {
16 0 : int i;
17 0 : if (verbose)
18 0 : printf("in saveListIndexToFile : %s\n", filename_list_index_file.c_str());
19 0 : FILE * fd = fopen(filename_list_index_file.c_str(), "w+");
20 0 : if (verbose)
21 0 : printf("fd opened : %p\n", fd);
22 0 : if (fd == NULL)
23 : {
24 0 : printf("%s doesn't exists in saveListIndexToFile !\n", filename_list_index_file.c_str());
25 : }
26 0 : for (i = 0; i < nb_data; i++)
27 : {
28 0 : fprintf(fd, "%d,%d,%d\n", ids[i], id_index_ids[i], type_data[i]);
29 : }
30 0 : fclose(fd);
31 0 : if (verbose)
32 0 : puts("end save list");
33 0 : }
34 :
35 : // VR TODO : delete doesn't manage type
36 :
37 0 : int deleteTokenFree(std::string completeFileName_aux, std::string &list_ids_str, size_t nb_data_in_file, std::vector<uint32_t>& ids_data, int type_data, int verbose)
38 : {
39 : /**
40 : * deleteTokenFree : Je crois que ca supprime des données de descripteur sans utiliser de token !
41 : * A utiliser uniquement dans le le deleteWithTokenManagement qui organise l'utilisation de Token
42 : * ca supprime toutes des données d'un descripteur "type_data"
43 : */
44 0 : std::vector<std::string> ids;
45 :
46 0 : boost::split(ids, list_ids_str, boost::is_any_of(","));
47 0 : std::vector<uint32_t> ids_data_tmp_vec, ids_index_tmp_vec, type_data_tmp_vec;
48 0 : int descriptor_type_tmp = -1;
49 0 : size_t nb_data_in_file_tmp;
50 0 : nb_data_in_file_tmp = loadListIndexFile(ids_data_tmp_vec, ids_index_tmp_vec, type_data_tmp_vec , completeFileName_aux, 0, descriptor_type_tmp, 0);
51 : // MG : 22/03/2016 : l api devrait stocker les ids onerror et envoyer une requete a vse toutes les 10 ou 15.
52 : // VR 4-5-16 : j'ai l'impression d'avoir regarder cela, sans etre totalement sur que ce soit effectif
53 0 : for (int i = 0; i < ids.size(); ++i)
54 : {
55 0 : int id_query = atoi(&(ids[i].at(0)));
56 0 : if (ids_data.size() > 0)
57 : {
58 0 : std::vector<uint32_t>::iterator ids_data_it;
59 0 : ids_data_it = std::find(ids_data.begin(), ids_data.end(), id_query);
60 0 : int64_t index_del;
61 0 : if (ids_data_it != ids_data.end())
62 0 : index_del = ids_data_it- ids_data.begin();
63 : else
64 : index_del = -1;
65 0 : if (index_del != -1)
66 : {
67 0 : ids_data[index_del] = 0;
68 : }
69 : }
70 0 : std::vector<uint32_t>::iterator it;
71 0 : it = std::find(ids_data_tmp_vec.begin(), ids_data_tmp_vec.end(), id_query);
72 0 : int query_index;
73 0 : if (it != ids_data_tmp_vec.end())
74 0 : query_index = (int) (it - ids_data_tmp_vec.begin());
75 : else
76 : query_index = -1;
77 0 : if (verbose)
78 0 : printf("About to delete with query_index : %d\n", query_index);
79 0 : if (query_index >= 0 && query_index < nb_data_in_file_tmp)
80 : {
81 0 : ids_data_tmp_vec[query_index] = 0;
82 : // TODO VR 29-2-16 : j'aimerai bien faire ce save a la fin de la boucle, il serait intelligent de tester le multi delete avant
83 0 : saveListIndexToFile(ids_data_tmp_vec, nb_data_in_file_tmp, type_data_tmp_vec, std::string(completeFileName_aux), ids_index_tmp_vec, verbose);
84 0 : if (verbose)
85 0 : puts(" saved ");
86 : } else {
87 0 : if (verbose)
88 0 : printf("id_query %d not in VSE\n", id_query);
89 : }
90 : }
91 0 : return 0;
92 : }
93 :
94 :
95 1 : int getTokenWriteIndex(int pid, std::string rootDirectory_loc)
96 : {
97 : /**
98 : * getTokenWriteIndex permet de savoir s'il y a un index.token present dans le de dossier du descripteur
99 : * Si ce fichier est present c'est qu'un processus est en train de lire les fichiers sur les descripteurs.
100 : * renvoie le pid du processus si il y a deja un processus en lecture, et créer un token renvoie -1 sinon.
101 : */
102 2 : std::string file;
103 1 : file += rootDirectory_loc;
104 1 : file += "index.token";
105 1 : FILE *fptrRead = fopen(&(file.at(0)), "r");
106 1 : if (fptrRead == NULL) //if file does not exist, create it
107 : {
108 1 : FILE *fptw = fopen(&(file.at(0)), "w");
109 1 : fprintf(fptw, "%d", pid);
110 1 : fclose(fptw);
111 : return -1;
112 : }
113 : else
114 : {
115 0 : char linebuf[10000];
116 : // size_t dummy_ret =
117 0 : if(fscanf(fptrRead, "%[^\n]\n", linebuf))
118 : ;
119 : //if (EOF == fscanf(fptrRead, "%[^\n]\n", linebuf))
120 : // break;
121 0 : int pid = 0;
122 0 : sscanf(linebuf, "%d\n", &pid);
123 0 : return pid;
124 : }
125 : }
126 :
127 1 : void giveBackToken(std::string rootDirectory_loc)
128 : {
129 : /**
130 : * giveBackToken supprime le fichier index.token c.a.d libere le token en écriture dur le descripteur.
131 : */
132 1 : std::string file;
133 1 : file += rootDirectory_loc;
134 1 : file += "index.token";
135 1 : int t = remove(&(file.at(0)));
136 1 : printf("giveBackToken, removed : %d\n", t);
137 1 : }
138 :
139 0 : int deleteWithTokenManagement(int pid, std::string rootDirectory_loc, std::string completeFileName_aux, std::string &list_ids, size_t nb_data_in_file, std::vector<uint32_t>& ids_data, int type_data, int verbose)
140 : {
141 0 : int token = getTokenWriteIndex(pid, rootDirectory_loc);
142 0 : if (token == -1)
143 : {
144 0 : int res = deleteTokenFree(completeFileName_aux, list_ids, nb_data_in_file, ids_data, type_data, verbose);
145 0 : giveBackToken(std::string(rootDirectory_loc));
146 0 : return res;
147 : }
148 : else
149 : {
150 0 : puts("Can't delete because token on index file is already used \n");
151 0 : return 1;
152 : }
153 : }
154 :
155 :
156 0 : void saveWithTokenCheck(int pid_vad, std::string rootDirectory_loc, std::string completeFileName_aux, std::vector<to_insert_t> & to_insert, std::string velours_root, int max, int verbose, bool dontCheckDuplicateIdObject)
157 : {
158 0 : int token = getTokenWriteIndex(pid_vad, rootDirectory_loc);
159 0 : if (token == -1)
160 : {
161 0 : save(std::string(rootDirectory_loc), completeFileName_aux, to_insert, std::string(velours_root), max, verbose, dontCheckDuplicateIdObject);
162 0 : giveBackToken(std::string(rootDirectory_loc));
163 : }
164 : else
165 : {
166 0 : puts("Token is present, data can't be modified by this thread (or pid) : You have certainly two server running and accessing data !\n");
167 : }
168 0 : }
169 :
170 :
171 :
172 0 : void commitInsert(std::vector<to_insert_t> & insert, int &indexCurrent, types_datas_t &read_cache, int verbose, bool dontCheckDuplicateIdObject,
173 : int insert_save_file, int insert_sync_cache)
174 : {
175 0 : if (indexCurrent == 0)
176 : return;
177 0 : if(insert_sync_cache){
178 0 : std::cout<<insert.at(0).dire << std::endl;
179 0 : puts("");
180 0 : int current_total = read_cache.nb_data + indexCurrent;
181 0 : read_cache.datas_vec.resize(current_total * read_cache.size);
182 0 : read_cache.ids_vec.resize(current_total);
183 0 : read_cache.index_vec.resize(current_total);
184 : #ifndef ONLY_CPU
185 : testCUDA(cudaFree(read_cache.dataGPU));
186 : #endif
187 :
188 0 : std::cout<<" In Commitinsert"<<std::endl;
189 0 : if(verbose){
190 0 : std::cout<<" read cache size : "<<read_cache.size<<std::endl;
191 : }
192 :
193 0 : std::cout<<" read cache size : "<<read_cache.size<<std::endl;
194 0 : std::cout<<"photo_id : "<<std::endl;
195 0 : for (int i = 0; i < indexCurrent; ++i)
196 : {
197 :
198 0 : if(insert.at(i).type_data == read_cache.type)
199 : {
200 0 : if(read_cache.nb_data < current_total)
201 : {
202 0 : if (insert.at(i).length != read_cache.size) {
203 0 : printf("Internal Error\n");
204 : break;
205 : }
206 0 : for (int k = 0; k < read_cache.size; k++)
207 : {
208 0 : size_t idx = (size_t) read_cache.nb_data * (size_t) read_cache.size + k;
209 0 : read_cache.datas_vec.at(idx) = insert.at(i).datas_vec[k];
210 : }
211 0 : read_cache.ids_vec[read_cache.nb_data] = insert.at(i).id;
212 0 : read_cache.index_vec[read_cache.nb_data] = 0;
213 0 : read_cache.nb_data++;
214 : }
215 : }
216 : }
217 0 : std::cout<<std::endl;
218 :
219 0 : read_cache.resizeDatas(read_cache.datas_vec.size() / read_cache.size);
220 : #ifndef ONLY_CPU
221 : size_t sizemalloc = ((size_t)read_cache.size * (size_t)read_cache.nb_data) * sizeof(data);
222 : testCUDA(cudaMalloc(&read_cache.dataGPU, sizemalloc));
223 : testCUDA(cudaMemcpy(read_cache.dataGPU, &(read_cache.datas_vec.front()), sizemalloc, cudaMemcpyHostToDevice));
224 : #endif
225 : }
226 0 : if(insert_save_file){
227 0 : std::cout<<"insert in file "<<insert.at(0).dire << std::endl;
228 0 : std::string completeFileNameStr = read_cache.dir + GlobalParam::Instance().filename_photo_id_list_str_;
229 0 : saveWithTokenCheck(GlobalParam::Instance().pid_, std::string(insert.at(0).dire), completeFileNameStr, insert, std::string(insert.at(0).dire), indexCurrent, verbose, dontCheckDuplicateIdObject);
230 : }
231 0 : indexCurrent = 0;
232 : }
233 :
234 1 : int cleanTokenProof(GlobalParam & gp, LocalParam & lp)
235 : {
236 : // VR 4-11-16 : a remplacer par
237 : // int cleanTokenProof()
238 :
239 1 : printf("About to clean datas_vec !\n");
240 1 : if (lp.limit_data != 0)
241 : {
242 0 : printf("We can't clean since we have not loaded all the data !");
243 0 : exit(0);
244 : }
245 1 : puts(" before m alloc data_loaded !\n");
246 1 : int token = getTokenWriteIndex(gp.pid_, gp.velours_std_);
247 1 : if (token == -1)
248 : {
249 10 : for (int i = 0; i < gp.nbtype_; ++i)
250 : {
251 9 : if (lp.ReadCacheCPU[i].nb_data)
252 : {
253 0 : std::string completeFileNameStrAux = lp.ReadCacheCPU[i].dir + gp.filename_photo_id_list_str_;
254 : // VR 22-9-16 : je crains introduire un crash lorsqu'il n'y a pas de data et que l'on clean, sans doute pas avec le ternaire, on m'a virer le ternaire VR 13-10-16 ! :-( sans virer le commentaire
255 0 : cleanDataBinary(lp.ReadCacheCPU[i].ids_vec, lp.ReadCacheCPU[i].datas_vec, lp.ReadCacheCPU[i].nb_data, lp.ReadCacheCPU[i].dir, completeFileNameStrAux, lp.ReadCacheCPU[i].size);
256 : }
257 : }
258 2 : giveBackToken(gp.velours_std_);
259 : }
260 : else
261 0 : printf("Lock exists on write index file with pid : %d \n", token);
262 :
263 1 : puts("Now exiting !\n");
264 1 : return (0);
265 : }
|