-
Notifications
You must be signed in to change notification settings - Fork 2
/
mqdb.cpp
1011 lines (824 loc) · 30.6 KB
/
mqdb.cpp
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
/********************************
/ MQDB
/ very simple server using V8 0MQ and LevelDB
/ BSD License
/
/**********************************/
#include <ucontext.h>
#include <stdlib.h>
#include <v8.h>
#include <pthread.h>
#include <assert.h>
#include <iostream>
#include <string>
#include <sstream>
#include <zmq.hpp>
#include <unistd.h>
#include "leveldb/db.h"
#include "leveldb/cache.h"
#include "lru_cache/lru_cache.h"
#include "EWAHBoolArray/headers/ewah.h"
#include "fstools.cc"
#define NUM_THREADS 5
#define JSVAL Handle<Value>
#define JSOBJ Handle<Object>
#define JSOBJT Handle<ObjectTemplate>
#define JSARRAY Handle<Array>
#define JSARGS const Arguments&
using namespace std;
using namespace v8;
/// LRUCache for keeping open databases
typedef LRUCache<std::string, leveldb::DB*> db_lru_type;
struct dbcontext {
void *context;
db_lru_type* dbl;
char* init_code;
};
//global context
struct dbcontext *gcontext;
//helper db functions
//opens or creates db for given name
leveldb::DB* getDb(db_lru_type* dbl, const String::Utf8Value &name, leveldb::Status& s) {
leveldb::DB* ret = 0;
//first check global lru cache
if (dbl->exists(*name)) {
//cout << "getDB exists for: " << *name << endl;
ret = dbl->fetch(*name); //->db;
}
else {
//cout << "getDB create for: " << *name << endl;
leveldb::Options options;
//TODO: setting cache size by user, default is 8MB
//options.block_cache = leveldb::NewLRUCache(100 * 1048576); // 100MB cache
options.create_if_missing = true;
s = leveldb::DB::Open(options, *name, &ret);
if (s.ok()) {
dbl->insert(*name, ret);
} else {
//we return s with error set and caller of this function will handle error
//TODO: change to log
std::cerr << s.ToString() << std::endl;
}
}
return ret;
}
void closeDb(leveldb::DB* db) {
cout << "closeDb for: " << db << endl;
delete db;
}
//*********************************
//PER THREAD OBECT MANAGEMENT
pthread_key_t tlsKey = 0;
//used on exit from thread
void globalDestructor(void *value)
{
printf("In the globalDestructor\n");
//TODO: free vector
//free(value);
pthread_setspecific(tlsKey, NULL);
}
//struct when we keep pointer to allocated value and pointer to function to deallocate
struct deobj {
void* val;
void (*des)(void*);
};
void th_alloc_array(int size)
{
vector<deobj> * SS = new vector<deobj>();
SS->reserve(size);
//printf("alloc_array Inside secondary thread\n");
pthread_setspecific(tlsKey, (void*)SS);
}
void add_obj(void* val, void (*des)(void*)) {
struct deobj dob;
void* global = pthread_getspecific(tlsKey);
vector<deobj> * SS = static_cast< vector<deobj>* >(global);
dob.val = val;
dob.des = des;
SS->push_back(dob);
}
void free_objects() {
struct deobj dob;
void* global = pthread_getspecific(tlsKey);
vector<deobj> * SS = static_cast< vector<deobj>* >(global);
while (!SS->empty())
{
dob = SS->back();
SS->pop_back();
dob.des(dob.val);
}
}
// Javascript API
static JSVAL addnum(JSARGS args) {
HandleScope scope;
int x = args[0]->IntegerValue();
int y = args[1]->IntegerValue();
return scope.Close(Integer::New(x+y));
}
static JSVAL print(JSARGS args) {
HandleScope scope;
String::Utf8Value s(args[0]->ToString());
cout << *s << endl;
return scope.Close(Null());
}
static JSVAL put(JSARGS args) {
HandleScope scope;
String::Utf8Value dbname(args[0]->ToString());
String::Utf8Value key(args[1]->ToString());
String::Utf8Value val(args[2]->ToString());
leveldb::Status s;
leveldb::DB* db = getDb(gcontext->dbl, dbname, s);
if (!db) {
return v8::ThrowException(v8::String::New(s.ToString().c_str()));
}
s = db->Put(leveldb::WriteOptions(), *key, *val);
if (!s.ok()) {
return v8::ThrowException(v8::String::New(s.ToString().c_str()));
}
return scope.Close(Integer::New(1));
}
static JSVAL get(JSARGS args) {
HandleScope scope;
String::Utf8Value dbname(args[0]->ToString());
String::Utf8Value key(args[1]->ToString());
std::string val;
leveldb::Status s;
leveldb::DB* db = getDb(gcontext->dbl, dbname,s);
if (!db) {
return v8::ThrowException(v8::String::New(s.ToString().c_str()));
}
s = db->Get(leveldb::ReadOptions(), *key, &val);
if (!s.ok()) {
//if not found we return Null otherwise we throw exception
if (s.IsNotFound())
return scope.Close(Null());
else
return v8::ThrowException(v8::String::New(s.ToString().c_str()));
}
Handle<String> res = String::New(val.c_str());
return scope.Close(res);
}
static JSVAL del(JSARGS args) {
HandleScope scope;
String::Utf8Value dbname(args[0]->ToString());
String::Utf8Value key(args[1]->ToString());
leveldb::Status s;
leveldb::DB* db = getDb(gcontext->dbl, dbname,s);
if (!db) {
return v8::ThrowException(v8::String::New(s.ToString().c_str()));
}
s = db->Delete(leveldb::WriteOptions(), *key);
if (!s.ok()) {
//if not found we return Null otherwise we throw exception
if (s.IsNotFound())
return scope.Close(Null());
else
return v8::ThrowException(v8::String::New(s.ToString().c_str()));
}
return scope.Close(Integer::New(1));
}
// ITERATOR API
void auto_del_it(void *val) {
//cout << "Auto deleting Iterator" << endl;
leveldb::Iterator* it = static_cast< leveldb::Iterator* >(val);
delete it;
}
static JSVAL it_new(JSARGS args) {
HandleScope scope;
String::Utf8Value dbname(args[0]->ToString());
//String::Utf8Value key(args[1]->ToString());
leveldb::Status s;
leveldb::DB* db = getDb(gcontext->dbl, dbname,s);
if (!db) {
return v8::ThrowException(v8::String::New(s.ToString().c_str()));
}
leveldb::Iterator* it = db->NewIterator(leveldb::ReadOptions());
//add iterator for auto del
add_obj((void*)it, auto_del_it);
return scope.Close(External::Wrap(it));
}
static JSVAL it_first(JSARGS args) {
HandleScope scope;
leveldb::Iterator* it = (leveldb::Iterator*) External::Unwrap(args[0]);
if (!it) {
return v8::ThrowException(v8::String::New("Iterator is Null"));
}
it->SeekToFirst();
return scope.Close(Integer::New(1));
}
static JSVAL it_last(JSARGS args) {
HandleScope scope;
leveldb::Iterator* it = (leveldb::Iterator*) External::Unwrap(args[0]);
if (!it) {
return v8::ThrowException(v8::String::New("Iterator is Null"));
}
it->SeekToLast();
return scope.Close(Integer::New(1));
}
static JSVAL it_seek(JSARGS args) {
HandleScope scope;
leveldb::Iterator* it = (leveldb::Iterator*) External::Unwrap(args[0]);
String::Utf8Value val(args[1]->ToString());
if (!it) {
return v8::ThrowException(v8::String::New("Iterator is Null"));
}
it->Seek(*val);
return scope.Close(Integer::New(1));
}
static JSVAL it_next(JSARGS args) {
HandleScope scope;
leveldb::Iterator* it = (leveldb::Iterator*) External::Unwrap(args[0]);
//leveldb::Iterator* it = (leveldb::Iterator*) args[0]->IntegerValue();
if (!it) {
return v8::ThrowException(v8::String::New("Iterator is Null"));
}
it->Next();
return scope.Close(Integer::New(1));
}
static JSVAL it_prev(JSARGS args) {
HandleScope scope;
leveldb::Iterator* it = (leveldb::Iterator*) External::Unwrap(args[0]);
//leveldb::Iterator* it = (leveldb::Iterator*) args[0]->IntegerValue();
if (!it) {
return v8::ThrowException(v8::String::New("Iterator is Null"));
}
it->Prev();
return scope.Close(Integer::New(1));
}
static JSVAL it_valid(JSARGS args) {
HandleScope scope;
leveldb::Iterator* it = (leveldb::Iterator*) External::Unwrap(args[0]);
//leveldb::Iterator* it = (leveldb::Iterator*) args[0]->IntegerValue();
if (!it) {
return v8::ThrowException(v8::String::New("Iterator is Null"));
}
return scope.Close(Integer::New(it->Valid()));
}
static JSVAL it_key(JSARGS args) {
HandleScope scope;
leveldb::Iterator* it = (leveldb::Iterator*) External::Unwrap(args[0]);
//leveldb::Iterator* it = (leveldb::Iterator*) args[0]->IntegerValue();
if (!it) {
return v8::ThrowException(v8::String::New("Iterator is Null"));
}
Handle<String> res = String::New(it->key().ToString().c_str());
return scope.Close(res);
}
static JSVAL it_val(JSARGS args) {
HandleScope scope;
leveldb::Iterator* it = (leveldb::Iterator*) External::Unwrap(args[0]);
//leveldb::Iterator* it = (leveldb::Iterator*) args[0]->IntegerValue();
if (!it) {
return v8::ThrowException(v8::String::New("Iterator is Null"));
}
Handle<String> res = String::New(it->value().ToString().c_str());
return scope.Close(res);
}
static JSVAL it_del(JSARGS args) {
HandleScope scope;
leveldb::Iterator* it = (leveldb::Iterator*) External::Unwrap(args[0]);
//leveldb::Iterator* it = (leveldb::Iterator*) args[0]->IntegerValue();
if (!it) {
return v8::ThrowException(v8::String::New("Iterator is Null"));
}
delete it;
return scope.Close(Integer::New(1));
}
//*******************
//EWAH BITSET
#define LENGTH uword64
void auto_del_bs(void *val) {
//cout << "Auto deleting Iterator" << endl;
EWAHBoolArray<LENGTH>* bs = static_cast< EWAHBoolArray<LENGTH>* >(val);
delete bs;
}
void auto_del_bs_it(void *val) {
//cout << "Auto deleting Iterator" << endl;
EWAHBoolArray<LENGTH>::const_iterator* it = static_cast< EWAHBoolArray<LENGTH>::const_iterator* >(val);
delete it;
}
static JSVAL bs_new(JSARGS args) {
HandleScope scope;
EWAHBoolArray<LENGTH>* bs = new EWAHBoolArray<LENGTH>();
//add ewah for auto del
add_obj((void*)bs, auto_del_bs);
return scope.Close(External::Wrap(bs));
}
//bitset1.reset();
static JSVAL bs_reset(JSARGS args) {
HandleScope scope;
EWAHBoolArray<LENGTH>* bs = (EWAHBoolArray<LENGTH>*) External::Unwrap(args[0]);
if (!bs) {
return v8::ThrowException(v8::String::New("In reset bitset is Null"));
}
bs->reset();
return scope.Close(Null());
}
/**
* From EWAH code:
* set the ith bit to true (starting at zero).
* Auto-expands the bitmap. It has constant running time complexity.
* Note that you must set the bits in increasing order:
* set(1), set(2) is ok; set(2), set(1) is not ok.
*/
//bitset1.set(1);
static JSVAL bs_set(JSARGS args) {
HandleScope scope;
EWAHBoolArray<LENGTH>* bs = (EWAHBoolArray<LENGTH>*) External::Unwrap(args[0]);
if (!bs) {
return v8::ThrowException(v8::String::New("In set bitset is Null"));
}
int v = args[1]->IntegerValue();
bs->set(v);
return scope.Close(Null());
}
//bitset1.makeSameSize(bitset2);
static JSVAL bs_makeSameSize(JSARGS args) {
HandleScope scope;
EWAHBoolArray<LENGTH>* bs = (EWAHBoolArray<LENGTH>*) External::Unwrap(args[0]);
if (!bs) {
return v8::ThrowException(v8::String::New("In makeSameSize bitset 1 is Null"));
}
EWAHBoolArray<LENGTH>* bs2 = (EWAHBoolArray<LENGTH>*) External::Unwrap(args[1]);
if (!bs2) {
return v8::ThrowException(v8::String::New("In makeSameSize bitset 2 is Null"));
}
bs->makeSameSize(*bs2);
return scope.Close(Null());
}
//bitset2.logicalnot(notbitset);
static JSVAL bs_logicalnot(JSARGS args) {
HandleScope scope;
EWAHBoolArray<LENGTH>* bs = (EWAHBoolArray<LENGTH>*) External::Unwrap(args[0]);
if (!bs) {
return v8::ThrowException(v8::String::New("In logicalnot bitset 1 is Null"));
}
EWAHBoolArray<LENGTH>* bs2 = (EWAHBoolArray<LENGTH>*) External::Unwrap(args[1]);
if (!bs2) {
return v8::ThrowException(v8::String::New("In logicalnot bitset 2 is Null"));
}
bs->logicalnot(*bs2);
return scope.Close(Null());
}
//bitset2.inplace_logicalnot();
static JSVAL bs_inplace_logicalnot(JSARGS args) {
HandleScope scope;
EWAHBoolArray<LENGTH>* bs = (EWAHBoolArray<LENGTH>*) External::Unwrap(args[0]);
if (!bs) {
return v8::ThrowException(v8::String::New("In inplace_logicalnot bitset is Null"));
}
bs->inplace_logicalnot();
return scope.Close(Null());
}
//bitset1.logicalor(bitset2,orbitset);
static JSVAL bs_logicalor(JSARGS args) {
HandleScope scope;
EWAHBoolArray<LENGTH>* bs = (EWAHBoolArray<LENGTH>*) External::Unwrap(args[0]);
if (!bs) {
return v8::ThrowException(v8::String::New("In logicalor bitset 1 is Null"));
}
EWAHBoolArray<LENGTH>* bs2 = (EWAHBoolArray<LENGTH>*) External::Unwrap(args[1]);
if (!bs2) {
return v8::ThrowException(v8::String::New("In logicalor bitset 2 is Null"));
}
EWAHBoolArray<LENGTH>* bs3 = (EWAHBoolArray<LENGTH>*) External::Unwrap(args[2]);
if (!bs3) {
return v8::ThrowException(v8::String::New("In logicalor bitset 3 is Null"));
}
try {
bs->logicalor(*bs2, *bs3);
} catch(std::out_of_range e) {
return v8::ThrowException(v8::String::New("In logicalor std::out_of_range"));
}
return scope.Close(Null());
}
//bitset1.sparselogicaland(notbitset,andbitset);
static JSVAL bs_sparselogicaland(JSARGS args) {
HandleScope scope;
EWAHBoolArray<LENGTH>* bs = (EWAHBoolArray<LENGTH>*) External::Unwrap(args[0]);
if (!bs) {
return v8::ThrowException(v8::String::New("In sparselogicaland bitset 1 is Null"));
}
EWAHBoolArray<LENGTH>* bs2 = (EWAHBoolArray<LENGTH>*) External::Unwrap(args[1]);
if (!bs2) {
return v8::ThrowException(v8::String::New("In sparselogicaland bitset 2 is Null"));
}
EWAHBoolArray<LENGTH>* bs3 = (EWAHBoolArray<LENGTH>*) External::Unwrap(args[2]);
if (!bs3) {
return v8::ThrowException(v8::String::New("In sparselogicaland bitset 3 is Null"));
}
bs->sparselogicaland(*bs2, *bs3);
return scope.Close(Null());
}
//bitset1.logicaland(bitset2,andbitset);
static JSVAL bs_logicaland(JSARGS args) {
HandleScope scope;
EWAHBoolArray<LENGTH>* bs = (EWAHBoolArray<LENGTH>*) External::Unwrap(args[0]);
if (!bs) {
return v8::ThrowException(v8::String::New("In logicaland bitset 1 is Null"));
}
EWAHBoolArray<LENGTH>* bs2 = (EWAHBoolArray<LENGTH>*) External::Unwrap(args[1]);
if (!bs2) {
return v8::ThrowException(v8::String::New("In logicaland bitset 2 is Null"));
}
EWAHBoolArray<LENGTH>* bs3 = (EWAHBoolArray<LENGTH>*) External::Unwrap(args[2]);
if (!bs3) {
return v8::ThrowException(v8::String::New("In logicaland bitset 2 is Null"));
}
bs->logicaland(*bs2, *bs3);
return scope.Close(Null());
}
//bool operator==(const EWAHBoolArray & x) const
static JSVAL bs_eq(JSARGS args) {
HandleScope scope;
EWAHBoolArray<LENGTH>* bs = (EWAHBoolArray<LENGTH>*) External::Unwrap(args[0]);
if (!bs) {
return v8::ThrowException(v8::String::New("In eq bitset 1 is Null"));
}
EWAHBoolArray<LENGTH>* bs2 = (EWAHBoolArray<LENGTH>*) External::Unwrap(args[1]);
if (!bs2) {
return v8::ThrowException(v8::String::New("In eq bitset 2 is Null"));
}
int ret = ((*bs) == (*bs2));
//cout << "eq: " << ret << endl;
return scope.Close(Integer::New(ret));
}
//EWAHBoolArray<LENGTH>::const_iterator i = bitset1.begin(); i!=bitset1.end(); ++i
static JSVAL bs_it_new(JSARGS args) {
HandleScope scope;
EWAHBoolArray<LENGTH>* bs = (EWAHBoolArray<LENGTH>*) External::Unwrap(args[0]);
if (!bs) {
return v8::ThrowException(v8::String::New("In it_new bitset is Null"));
}
EWAHBoolArray<LENGTH>::const_iterator* it = new EWAHBoolArray<LENGTH>::const_iterator(bs->begin());
cout << "it : " << **it << endl;
//*it = bitset1.begin();
//add iterator for auto del
add_obj((void*)it, auto_del_bs_it);
return scope.Close(External::Wrap(it));
}
//EWAHBoolArray<LENGTH>::const_iterator i = bitset1.begin(); i!=bitset1.end(); ++i
static JSVAL bs_it_end(JSARGS args) {
HandleScope scope;
EWAHBoolArray<LENGTH>* bs = (EWAHBoolArray<LENGTH>*) External::Unwrap(args[0]);
if (!bs) {
return v8::ThrowException(v8::String::New("In it_end bitset is Null"));
}
EWAHBoolArray<LENGTH>::const_iterator* it = new EWAHBoolArray<LENGTH>::const_iterator(bs->end());
//*it = bitset1.end();
//add iterator for auto del
add_obj((void*)it, auto_del_bs_it);
return scope.Close(External::Wrap(it));
}
//EWAHBoolArray<LENGTH>::const_iterator i = bitset1.begin(); i!=bitset1.end(); ++i
static JSVAL bs_it_isend(JSARGS args) {
HandleScope scope;
EWAHBoolArray<LENGTH>* bs = (EWAHBoolArray<LENGTH>*) External::Unwrap(args[0]);
if (!bs) {
return v8::ThrowException(v8::String::New("In it_isend bitset is Null"));
}
EWAHBoolArray<LENGTH>::const_iterator* it = (EWAHBoolArray<LENGTH>::const_iterator*) External::Unwrap(args[1]);
if (!it) {
return v8::ThrowException(v8::String::New("In it_isend it is Null"));
}
int ret = ( (*it)==(bs->end()) );
return scope.Close(Integer::New(ret));
}
//EWAHBoolArray<LENGTH>::const_iterator i = bitset1.begin(); i!=bitset1.end(); ++i
static JSVAL bs_it_next(JSARGS args) {
HandleScope scope;
EWAHBoolArray<LENGTH>::const_iterator* it = (EWAHBoolArray<LENGTH>::const_iterator*) External::Unwrap(args[0]);
if (!it) {
return v8::ThrowException(v8::String::New("In it_next it is Null"));
}
(*it)++;
return scope.Close(External::Wrap(it));
}
static JSVAL bs_it_eq(JSARGS args) {
HandleScope scope;
EWAHBoolArray<LENGTH>::const_iterator* it = (EWAHBoolArray<LENGTH>::const_iterator*) External::Unwrap(args[0]);
if (!it) {
return v8::ThrowException(v8::String::New("In it_eq it is Null"));
}
EWAHBoolArray<LENGTH>::const_iterator* it2 = (EWAHBoolArray<LENGTH>::const_iterator*) External::Unwrap(args[1]);
if (!it2) {
return v8::ThrowException(v8::String::New("In it_eq it 2 is Null"));
}
int ret = (*it)==(*it2);
return scope.Close(Integer::New(ret));
}
static JSVAL bs_it_val(JSARGS args) {
HandleScope scope;
EWAHBoolArray<LENGTH>::const_iterator* it = (EWAHBoolArray<LENGTH>::const_iterator*) External::Unwrap(args[0]);
if (!it) {
return v8::ThrowException(v8::String::New("In it_val it is Null"));
}
int ret = *(*it);
return scope.Close(Integer::New(ret));
}
static JSVAL bs_tostring(JSARGS args) {
HandleScope scope;
EWAHBoolArray<LENGTH>* bs = (EWAHBoolArray<LENGTH>*) External::Unwrap(args[0]);
if (!bs) {
return v8::ThrowException(v8::String::New("In tostring bitset is Null"));
}
std::stringstream ss;
//cout<<"first bitset : "<<endl;
for(EWAHBoolArray<LENGTH>::const_iterator i = bs->begin(); i!=bs->end(); ++i)
ss<<*i<<",";
Handle<String> res = String::New(ss.str().c_str());
return scope.Close(res);
}
//inline void write(ostream & out, const bool savesizeinbits=true) const;
static JSVAL putbs(JSARGS args) {
HandleScope scope;
String::Utf8Value dbname(args[0]->ToString());
String::Utf8Value key(args[1]->ToString());
EWAHBoolArray<LENGTH>* bs = (EWAHBoolArray<LENGTH>*) External::Unwrap(args[2]);
if (!bs) {
return v8::ThrowException(v8::String::New("In putbs bitset is Null"));
}
leveldb::Status s;
leveldb::DB* db = getDb(gcontext->dbl, dbname, s);
if (!db) {
return v8::ThrowException(v8::String::New(s.ToString().c_str()));
}
std::stringstream ss;
bs->write(ss, /*const bool savesizeinbits=*/ true);
//cout<< "putbs:" << ss.str().size() << " "<< ss.str() << endl;
s = db->Put(leveldb::WriteOptions(), *key, ss.str());
if (!s.ok()) {
return v8::ThrowException(v8::String::New(s.ToString().c_str()));
}
return scope.Close(Integer::New(1));
}
//inline void read(istream & in, const bool savesizeinbits=true);
static JSVAL getbs(JSARGS args) {
HandleScope scope;
String::Utf8Value dbname(args[0]->ToString());
String::Utf8Value key(args[1]->ToString());
EWAHBoolArray<LENGTH>* bs = (EWAHBoolArray<LENGTH>*) External::Unwrap(args[2]);
if (!bs) {
return v8::ThrowException(v8::String::New("In getbs bitset is Null"));
}
std::string val;
leveldb::Status s;
leveldb::DB* db = getDb(gcontext->dbl, dbname,s);
if (!db) {
return v8::ThrowException(v8::String::New(s.ToString().c_str()));
}
s = db->Get(leveldb::ReadOptions(), *key, &val);
if (!s.ok()) {
//if not found we return Null otherwise we throw exception
if (s.IsNotFound())
return scope.Close(Null());
else
return v8::ThrowException(v8::String::New(s.ToString().c_str()));
}
//cout<< "getbs:" << val.size() << " " << val << endl;
std::stringstream ss(val);
bs->read(ss, /*const bool savesizeinbits=*/true);
return scope.Close(Integer::New(1));
}
//*******************************************************************
//Taken from google shell example
// Extracts a C string from a V8 Utf8Value.
const char* ToCString(const v8::String::Utf8Value& value) {
return *value ? *value : "<string conversion failed>";
}
void ReportException(v8::TryCatch* try_catch) {
v8::HandleScope handle_scope;
v8::String::Utf8Value exception(try_catch->Exception());
const char* exception_string = ToCString(exception);
v8::Handle<v8::Message> message = try_catch->Message();
if (message.IsEmpty()) {
// V8 didn't provide any extra information about this error; just
// print the exception.
printf("%s\n", exception_string);
} else {
// Print (filename):(line number): (message).
v8::String::Utf8Value filename(message->GetScriptResourceName());
const char* filename_string = ToCString(filename);
int linenum = message->GetLineNumber();
printf("%s:%i: %s\n", filename_string, linenum, exception_string);
// Print line of source code.
v8::String::Utf8Value sourceline(message->GetSourceLine());
const char* sourceline_string = ToCString(sourceline);
printf("%s\n", sourceline_string);
// Print wavy underline (GetUnderline is deprecated).
int start = message->GetStartColumn();
for (int i = 0; i < start; i++) {
printf(" ");
}
int end = message->GetEndColumn();
for (int i = start; i < end; i++) {
printf("^");
}
printf("\n");
v8::String::Utf8Value stack_trace(try_catch->StackTrace());
if (stack_trace.length() > 0) {
const char* stack_trace_string = ToCString(stack_trace);
printf("%s\n", stack_trace_string);
}
}
}
void* worker_routine(void *vdbc) {
struct dbcontext * dbc = (struct dbcontext *) vdbc;
//allocate array for objects that will be automatically removed after
th_alloc_array(1000);
// Socket to talk to dispatcher
void *receiver = zmq_socket (dbc->context, ZMQ_REP);
zmq_connect (receiver, "inproc://workers");
//Initialize our Isoloate
Isolate *isolate = Isolate::New();
if(!isolate)
{
cout << "Failed to initialize Isolate, we can't work";
return 0;
}
Locker lock(isolate);
Isolate::Scope isolateScope(isolate);
HandleScope scope;
Local<ObjectTemplate> globals = ObjectTemplate::New();
#define SETGLOB(name) globals->Set(String::New(""#name), FunctionTemplate::New(name));
globals->Set(String::New("addnum"), FunctionTemplate::New(addnum));
SETGLOB(print)
globals->Set(String::New("put"), FunctionTemplate::New(put));
globals->Set(String::New("get"), FunctionTemplate::New(get));
globals->Set(String::New("del"), FunctionTemplate::New(del));
//iterator
globals->Set(String::New("it_del"), FunctionTemplate::New(it_del));
globals->Set(String::New("it_new"), FunctionTemplate::New(it_new));
globals->Set(String::New("it_first"), FunctionTemplate::New(it_first));
globals->Set(String::New("it_last"), FunctionTemplate::New(it_last));
globals->Set(String::New("it_seek"), FunctionTemplate::New(it_seek));
globals->Set(String::New("it_next"), FunctionTemplate::New(it_next));
globals->Set(String::New("it_prev"), FunctionTemplate::New(it_prev));
globals->Set(String::New("it_valid"), FunctionTemplate::New(it_valid));
globals->Set(String::New("it_key"), FunctionTemplate::New(it_key));
globals->Set(String::New("it_val"), FunctionTemplate::New(it_val));
//BITSET
SETGLOB(bs_new)
SETGLOB(bs_reset)
SETGLOB(bs_set)
SETGLOB(bs_logicalor)
SETGLOB(bs_logicalnot)
SETGLOB(bs_inplace_logicalnot)
SETGLOB(bs_logicaland)
SETGLOB(bs_sparselogicaland)
SETGLOB(bs_tostring)
SETGLOB(bs_makeSameSize)
SETGLOB(bs_eq)
SETGLOB(putbs)
SETGLOB(getbs)
SETGLOB(bs_it_new)
SETGLOB(bs_it_end)
SETGLOB(bs_it_isend)
SETGLOB(bs_it_next)
SETGLOB(bs_it_val)
SETGLOB(bs_it_end)
SETGLOB(bs_it_eq)
Handle<Context> context = Context::New(NULL, globals);
context->Enter();
//Running init script with library functions
if (dbc->init_code != NULL) {
TryCatch try_catch;
Local<String> source = String::New(dbc->init_code);
Local<Script> script = Script::Compile(source);
if(script.IsEmpty())
{
ReportException(&try_catch);
} else {
// Run the function once and catch the error to generate machine code
Local<Value> result = script->Run();
}
}
while (1) {
zmq_msg_t req;
int rc = zmq_msg_init (&req);
assert (rc == 0);
rc = zmq_recv (receiver, &req, 0);
assert (rc == 0);
zmq_msg_t reply;
//rc = zmq_msg_init_size (&reply, 200);
//assert (rc == 0);
//test mq speed only
#if 0
rc = zmq_msg_init_size (&reply, 200);
assert (rc == 0);
rc = zmq_msg_copy (&reply, &req);
assert (rc == 0);
rc = zmq_send (receiver, &reply, 0);
assert (rc == 0);
//s_sleep(1);
zmq_msg_close (&req);
zmq_msg_close (&reply);
continue;
#endif
//std::cout << "thread: " << pthread_self() << " Received script: " << (char*) zmq_msg_data(&req) << std::endl;
//leveldb::Status s;
#if 1
Context::Scope contextScope(context);
// Compile a function which will keep using the stack until v8 cuts it off
//Local<Script> script = Local<Script>::New(Script::Compile(String::New( (char*) zmq_msg_data(&req) )));
TryCatch try_catch;
Local<String> source = String::New((char*) zmq_msg_data(&req));
Local<Script> script = Script::Compile(source);
if(script.IsEmpty())
{
ReportException(&try_catch);
String::Utf8Value error(try_catch.Exception());
v8::Handle<v8::Message> message = try_catch.Message();
int line_num = 0;
int col_num = 0;
if (message.IsEmpty()) {
}else {
line_num = message->GetLineNumber();
col_num = message->GetStartColumn();
}
int mess_size = error.length()+100;
rc = zmq_msg_init_size (&reply, mess_size);
assert (rc == 0);
//cerr << "compile error line: " << " message: " << *error << endl;
snprintf ((char*) zmq_msg_data (&reply), mess_size, "2; {res: 'COMPILE ERROR', line: %d, col: %d, message: '%s'}", line_num, col_num, *error);
}
else {
// Run the function once and catch the error to generate machine code
Local<Value> result = script->Run();
if (try_catch.HasCaught()) {
//TODO: escape string
String::Utf8Value message(try_catch.Exception());
int mess_size = message.length()+100;
rc = zmq_msg_init_size (&reply, mess_size);
assert (rc == 0);
std::cout <<"exception->" <<*message <<"\n";
snprintf ((char*) zmq_msg_data (&reply), mess_size, "1; {res: 'ERROR', message: '%s'}", *message);
}
else {
//TODO: escape string
String::Utf8Value message(result->ToString());
int mess_size = message.length()+2;
rc = zmq_msg_init_size (&reply, mess_size);
assert (rc == 0);
snprintf ((char*) zmq_msg_data (&reply), mess_size, " %s", *message);
}
}
#endif
/* Send the message to the socket */
rc = zmq_send (receiver, &reply, 0);
assert (rc == 0);
//s_sleep(1);
zmq_msg_close (&req);
zmq_msg_close (&reply);
free_objects();
}
context->Exit();
//printf("Iter: %d , Result: %ld ; from thread: %ld \n", i, res, tid);
//context.Dispose();
//script.Dispose();
Unlocker unlock(isolate);
//isolate->Exit();
//isolate->Dispose();
zmq_close (receiver);
return NULL;
}
int main() {
//register key for vector when we keep per thread allocated objects to free them automatically
int rc = pthread_key_create(&tlsKey, globalDestructor);
assert(rc==0);
struct dbcontext dbc;
gcontext = &dbc;
dbc.context = zmq_init (1);
//max open 100 databases
dbc.dbl = new db_lru_type(100);
dbc.dbl->setOnRemove(&closeDb);
//char* init_code = readFile2("js/init.js");
system("cat js/*.js >> all.js");
char* init_code = readFile2("all.js");
dbc.init_code = init_code;
if (dbc.init_code) {
printf("init code: |%s|", dbc.init_code);
}
// Socket to talk to clients
void *clients = zmq_socket (dbc.context, ZMQ_ROUTER);
rc = zmq_bind (clients, "tcp://*:5555");
//zmq_bind (clients, "epgm://eth0;*:5555");
//rc = zmq_bind(clients, "ipc:///tmp/feeds_mq");
assert(rc==0);
// Socket to talk to workers
void *workers = zmq_socket (dbc.context, ZMQ_DEALER);
zmq_bind (workers, "inproc://workers");
// Launch pool of worker threads
int thread_nbr;
for (thread_nbr = 0; thread_nbr < 6; thread_nbr++) {
pthread_t worker;
pthread_create (&worker, NULL, worker_routine, &dbc);