@@ -21,29 +21,6 @@ ParallaxCatalogueWriter::ParallaxCatalogueWriter(const Key &key, const fdb5::Con
2121 : ParallaxCatalogue(key, config)
2222 , firstIndexWrite_(false )
2323{
24- par_handle db_handle = par_get_db (PARALLAX_GLOBAL_DB);
25- const char *error_msg = NULL ;
26-
27- par_key_value dbKey_kv;
28- dbKey_kv.k .size = sizeof (" key" );
29- dbKey_kv.k .data = " key" ;
30-
31- std::ostringstream dbKeyStream;
32- dbKeyStream << dbKey_;
33- std::string dbKeyStr = dbKeyStream.str ();
34-
35- if (dbKeyStr.size () + 1 > 512 ) {
36- throw eckit::Exception (" Serialised db key exceeded maximum length." );
37- }
38-
39- dbKey_kv.v .val_size = dbKeyStr.size () + 1 ;
40- dbKey_kv.v .val_buffer = (char *)dbKeyStr.c_str ();
41-
42- par_put (db_handle, &dbKey_kv, &error_msg);
43- if (error_msg) {
44- throw eckit::Exception (" Failed to insert dbKey: " + std::string (error_msg));
45- }
46-
4724 std::string path = config.schemaPath ();
4825
4926 std::stringstream schema_buffer;
@@ -56,7 +33,6 @@ ParallaxCatalogueWriter::ParallaxCatalogueWriter(const Key &key, const fdb5::Con
5633 }
5734
5835 std::string schema_str = schema_buffer.str ();
59- error_msg = NULL ;
6036 par_key_value schema_kv;
6137 std::string key_str = " schema" ;
6238
@@ -68,6 +44,13 @@ ParallaxCatalogueWriter::ParallaxCatalogueWriter(const Key &key, const fdb5::Con
6844 std::memcpy (schema_kv.v .val_buffer , schema_str.c_str (), schema_str.size ());
6945 schema_kv.v .val_buffer [schema_str.size ()] = ' \0 ' ;
7046
47+ size_t hash = std::hash<std::string>{}(key_str.c_str ());
48+ int db_index = hash % 16 ;
49+
50+ std::string db_name = " par_db" + std::to_string (db_index + 1 );
51+ par_handle db_handle = par_get_db (db_name);
52+ const char *error_msg = NULL ;
53+
7154 par_put (db_handle, &schema_kv, &error_msg);
7255 if (error_msg) {
7356 std::cout << " Sorry Parallax put failed reason: " << error_msg << std::endl;
@@ -99,9 +82,6 @@ bool ParallaxCatalogueWriter::selectIndex(const Key &key)
9982 currentIndexKey_ = key;
10083
10184 if (indexes_.find (key) == indexes_.end ()) {
102- par_handle db_handle = par_get_db (PARALLAX_GLOBAL_DB);
103- const char *error_msg = nullptr ;
104-
10585 par_key_value kv;
10686 std::string keyStr = key.valuesToString ();
10787 kv.k .size = keyStr.size () + 1 ;
@@ -115,6 +95,13 @@ bool ParallaxCatalogueWriter::selectIndex(const Key &key)
11595 throw eckit::Exception (" Memory allocation failed for index retrieval." );
11696 }
11797
98+ size_t hash = std::hash<std::string>{}(keyStr.c_str ());
99+ int db_index = hash % 16 ;
100+
101+ std::string db_name = " par_db" + std::to_string (db_index + 1 );
102+ par_handle db_handle = par_get_db (db_name);
103+ const char *error_msg = nullptr ;
104+
118105 par_get (db_handle, &kv.k , &kv.v , &error_msg);
119106
120107 error_msg = nullptr ;
@@ -130,6 +117,12 @@ bool ParallaxCatalogueWriter::selectIndex(const Key &key)
130117 strncpy (kv.v .val_buffer , placeholderValue.c_str (), kv.v .val_buffer_size - 1 );
131118 kv.v .val_buffer [kv.v .val_buffer_size - 1 ] = ' \0 ' ;
132119
120+ size_t hash = std::hash<std::string>{}(keyStr.c_str ());
121+ int db_index = hash % 16 ;
122+
123+ std::string db_name = " par_db" + std::to_string (db_index + 1 );
124+ db_handle = par_get_db (db_name);
125+
133126 par_put (db_handle, &kv, &error_msg);
134127
135128 if (error_msg) {
@@ -138,7 +131,6 @@ bool ParallaxCatalogueWriter::selectIndex(const Key &key)
138131 }
139132 }
140133 }
141-
142134 indexes_[key] = Index (new ParallaxIndex (key));
143135 current_ = indexes_[key];
144136 firstIndexWrite_ = true ;
@@ -174,6 +166,11 @@ const Index &ParallaxCatalogueWriter::currentIndex()
174166
175167void ParallaxCatalogueWriter::archive (const Key &key, std::unique_ptr<FieldLocation> fieldLocation)
176168{
169+ par_handle db_handle;
170+ size_t hash;
171+ int db_index;
172+ std::string db_name;
173+
177174 if (current_.null ()) {
178175 ASSERT (!currentIndexKey_.empty ());
179176 selectIndex (currentIndexKey_);
@@ -206,12 +203,6 @@ void ParallaxCatalogueWriter::archive(const Key &key, std::unique_ptr<FieldLocat
206203
207204 current_.put (key, field);
208205
209- par_handle db_handle = par_get_db (" par_db" );
210- if (!db_handle) {
211- std::cerr << " Failed to open Parallax database." << std::endl;
212- _exit (EXIT_FAILURE);
213- }
214-
215206 if (firstIndexWrite_) {
216207 par_key_value kv{};
217208 const char *error_message = nullptr ;
@@ -224,6 +215,12 @@ void ParallaxCatalogueWriter::archive(const Key &key, std::unique_ptr<FieldLocat
224215 kv.v .val_buffer = axisNames.data ();
225216 kv.v .val_size = axisNames.length ();
226217
218+ hash = std::hash<std::string>{}(indexKeyWithAxes.c_str ());
219+ db_index = hash % 16 ;
220+
221+ db_name = " par_db" + std::to_string (db_index + 1 );
222+ db_handle = par_get_db (db_name);
223+
227224 par_put (db_handle, &kv, &error_message);
228225 if (error_message) {
229226 std::cerr << " Parallax put failed: " << error_message << std::endl;
@@ -250,6 +247,12 @@ void ParallaxCatalogueWriter::archive(const Key &key, std::unique_ptr<FieldLocat
250247 .val_size = 0 ,
251248 .val_buffer = value_buf.data () };
252249
250+ hash = std::hash<std::string>{}(axisKey.c_str ());
251+ db_index = hash % 16 ;
252+
253+ db_name = " par_db" + std::to_string (db_index + 1 );
254+ db_handle = par_get_db (db_name);
255+
253256 par_get (db_handle, &existing_key, &existing_value, &error_message2);
254257
255258 std::string updatedValueStr;
@@ -275,6 +278,12 @@ void ParallaxCatalogueWriter::archive(const Key &key, std::unique_ptr<FieldLocat
275278 kv2.v .val_size = updatedValueStr.size ();
276279
277280 error_message2 = nullptr ;
281+
282+ size_t hash = std::hash<std::string>{}(axisKey.c_str ());
283+ int db_index = hash % 16 ;
284+
285+ std::string db_name = " par_db" + std::to_string (db_index + 1 );
286+ db_handle = par_get_db (db_name);
278287 par_put (db_handle, &kv2, &error_message2);
279288 if (error_message2) {
280289 _exit (EXIT_FAILURE);
@@ -298,4 +307,4 @@ void ParallaxCatalogueWriter::closeIndexes()
298307
299308static fdb5::CatalogueBuilder<fdb5::ParallaxCatalogueWriter> builder (" parallax.writer" );
300309
301- }
310+ }
0 commit comments