@@ -29,16 +29,14 @@ namespace fs = ghc::filesystem;
2929using namespace std ;
3030
3131const char *DELIMITERS = " !\" #$%&'()*+,-./:;<=>?@\[\\ ]^_`{|}~\n\v\f\r " ;
32- unsigned int WRITER_FLAGS = MDB_NOSYNC | MDB_WRITEMAP | MDB_MAPASYNC | MDB_NOMETASYNC | MDB_NORDAHEAD;
32+ // unsigned int WRITER_FLAGS = MDB_NOSYNC | MDB_WRITEMAP | MDB_MAPASYNC | MDB_NOMETASYNC | MDB_NORDAHEAD;
3333// unsigned int WRITER_FLAGS = 0;
3434
3535// mutex is used so MDB_NOTLS not needed
36- unsigned int READER_FLAGS = MDB_NOLOCK | MDB_NOTLS;
36+ // unsigned int READER_FLAGS = MDB_NOLOCK | MDB_NOTLS;
3737// unsigned int READER_FLAGS = 0;
3838const auto DB_SIZE = 100UL * 1024UL * 1024UL * 1024UL ;
3939
40- std::mutex super_mutex;
41-
4240int fast_atoi ( const char * str ) {
4341 int val = 0 ;
4442 while ( *str ) {
@@ -52,7 +50,7 @@ int fast_atoi( const char * str ) {
5250 */
5351std::tuple<std::string, std::optional<Roaring>, std::optional<Roaring>> itemsjs::search_facets (const char *&index_path, nlohmann::json input, nlohmann::json filters_array, nlohmann::json config, nlohmann::json facets_fields, std::optional<Roaring> query_ids, bool testing = false ) {
5452
55- // @TODO make unordered
53+ // @TODO make unordered (if faster)
5654 std::map<string, std::map<string, Roaring>> filters_indexes;
5755 std::map<string, std::map<string, Roaring>> not_filters_indexes;
5856 std::map<string, Roaring> combination;
@@ -642,9 +640,46 @@ std::vector<int> itemsjs::sort_index_2(const char *&index_path, const Roaring &i
642640 return sorted_ids;
643641}
644642
643+ void itemsjs::set_configuration (const char *&index_path, const std::string& json) {
644+
645+ if (!fs::is_directory (index_path) || !fs::exists (index_path)) {
646+ fs::create_directory (index_path);
647+ }
648+
649+ string file_lock_path = (string)index_path + " /lock" ;
650+ std::ofstream output (file_lock_path.c_str ());
651+
652+ try {
653+ auto lock = std::make_unique<boost::interprocess::file_lock>(file_lock_path.c_str ());
654+ if (!lock->try_lock ()) {
655+ lock->lock ();
656+ }
657+
658+ auto env = lmdb::env::create ();
659+
660+ env.set_mapsize (DB_SIZE);
661+ env.set_max_dbs (20 );
662+ env.open (index_path, 0 , 0664 );
663+
664+ auto wtxn = lmdb::txn::begin (env);
665+ auto dbi = lmdb::dbi::open (wtxn, nullptr );
666+ dbi.put (wtxn, " configuration" , json.c_str ());
667+
668+ wtxn.commit ();
669+ env.close ();
670+
671+ } catch (const boost::interprocess::interprocess_exception &e) {
672+ cout << " There was an error with setting configuration interprocess mutex" << endl;
673+ cout << e.what () << endl;
674+ }
675+ }
645676
646677std::string itemsjs::index (const char *&index_path, string json_path, const string& json_string, vector<string> &faceted_fields, std::vector<std::string> &sorting_fields, bool append = true ) {
647678
679+ if (!fs::is_directory (index_path) || !fs::exists (index_path)) {
680+ fs::create_directory (index_path);
681+ }
682+
648683 string file_lock_path = (string)index_path + " /lock" ;
649684 std::ofstream output (file_lock_path.c_str ());
650685
@@ -994,6 +1029,20 @@ void itemsjs::DeleteItemWrapped(const Napi::CallbackInfo& info) {
9941029 itemsjs::delete_item (index_path, id);
9951030}
9961031
1032+ void itemsjs::SetConfigurationWrapped (const Napi::CallbackInfo& info) {
1033+
1034+ Napi::String index_name = info[0 ].As <Napi::String>();
1035+ string name_a (index_name.ToString ());
1036+ const char *index_path = name_a.c_str ();
1037+
1038+ // json already stringified
1039+ Napi::String json_object = info[1 ].As <Napi::String>();
1040+ string json_string (json_object.ToString ());
1041+
1042+ itemsjs::set_configuration (index_path, json_string);
1043+ }
1044+
1045+
9971046void itemsjs::LoadSortIndexWrapped (const Napi::CallbackInfo& info) {
9981047
9991048 Napi::String index_name = info[0 ].As <Napi::String>();
@@ -1237,6 +1286,7 @@ Napi::String itemsjs::IndexWrapped(const Napi::CallbackInfo& info) {
12371286Napi::Object itemsjs::Init (Napi::Env env, Napi::Object exports) {
12381287
12391288 exports.Set (" delete_item" , Napi::Function::New (env, itemsjs::DeleteItemWrapped));
1289+ exports.Set (" set_configuration" , Napi::Function::New (env, itemsjs::SetConfigurationWrapped));
12401290 exports.Set (" sort_index" , Napi::Function::New (env, itemsjs::SortIndexWrapped));
12411291 exports.Set (" sort_index_2" , Napi::Function::New (env, itemsjs::SortIndex2Wrapped));
12421292 exports.Set (" load_sort_index" , Napi::Function::New (env, itemsjs::LoadSortIndexWrapped));
0 commit comments