@@ -29,16 +29,14 @@ namespace fs = ghc::filesystem;
29
29
using namespace std ;
30
30
31
31
const 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;
33
33
// unsigned int WRITER_FLAGS = 0;
34
34
35
35
// 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;
37
37
// unsigned int READER_FLAGS = 0;
38
38
const auto DB_SIZE = 100UL * 1024UL * 1024UL * 1024UL ;
39
39
40
- std::mutex super_mutex;
41
-
42
40
int fast_atoi ( const char * str ) {
43
41
int val = 0 ;
44
42
while ( *str ) {
@@ -52,7 +50,7 @@ int fast_atoi( const char * str ) {
52
50
*/
53
51
std::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 ) {
54
52
55
- // @TODO make unordered
53
+ // @TODO make unordered (if faster)
56
54
std::map<string, std::map<string, Roaring>> filters_indexes;
57
55
std::map<string, std::map<string, Roaring>> not_filters_indexes;
58
56
std::map<string, Roaring> combination;
@@ -642,9 +640,46 @@ std::vector<int> itemsjs::sort_index_2(const char *&index_path, const Roaring &i
642
640
return sorted_ids;
643
641
}
644
642
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
+ }
645
676
646
677
std::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 ) {
647
678
679
+ if (!fs::is_directory (index_path) || !fs::exists (index_path)) {
680
+ fs::create_directory (index_path);
681
+ }
682
+
648
683
string file_lock_path = (string)index_path + " /lock" ;
649
684
std::ofstream output (file_lock_path.c_str ());
650
685
@@ -994,6 +1029,20 @@ void itemsjs::DeleteItemWrapped(const Napi::CallbackInfo& info) {
994
1029
itemsjs::delete_item (index_path, id);
995
1030
}
996
1031
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
+
997
1046
void itemsjs::LoadSortIndexWrapped (const Napi::CallbackInfo& info) {
998
1047
999
1048
Napi::String index_name = info[0 ].As <Napi::String>();
@@ -1237,6 +1286,7 @@ Napi::String itemsjs::IndexWrapped(const Napi::CallbackInfo& info) {
1237
1286
Napi::Object itemsjs::Init (Napi::Env env, Napi::Object exports) {
1238
1287
1239
1288
exports.Set (" delete_item" , Napi::Function::New (env, itemsjs::DeleteItemWrapped));
1289
+ exports.Set (" set_configuration" , Napi::Function::New (env, itemsjs::SetConfigurationWrapped));
1240
1290
exports.Set (" sort_index" , Napi::Function::New (env, itemsjs::SortIndexWrapped));
1241
1291
exports.Set (" sort_index_2" , Napi::Function::New (env, itemsjs::SortIndex2Wrapped));
1242
1292
exports.Set (" load_sort_index" , Napi::Function::New (env, itemsjs::LoadSortIndexWrapped));
0 commit comments