-
Notifications
You must be signed in to change notification settings - Fork 20
/
myscript.cc
3752 lines (3466 loc) · 103 KB
/
myscript.cc
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
/*************************************************************************************************
* The scripting extension
* Copyright (C) 2009-2012 FAL Labs
* This file is part of Kyoto Tycoon.
* This program is free software: you can redistribute it and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation, either version
* 3 of the License, or any later version.
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with this program.
* If not, see <http://www.gnu.org/licenses/>.
*************************************************************************************************/
#include "myscript.h"
#include "myconf.h"
#if _KT_LUA
extern "C" {
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}
/* precedent type declaration */
struct ScriptProcessorCore;
class StringBuilder;
struct SoftCursor;
struct SoftDB;
class SoftVisitor;
class SoftFileProcessor;
class SoftMapReduce;
typedef std::map<std::string, std::string> StringMap;
typedef std::vector<std::string> StringVector;
/* function prototypes */
static void reporterror(ScriptProcessorCore* core, const char* name);
static void throwinvarg(lua_State* lua, const char* func);
static void setfielduint(lua_State* lua, const char* name, uint32_t num);
static void setfieldstr(lua_State* lua, const char* name, const char* str);
static void setfieldfunc(lua_State* lua, const char* name, lua_CFunction func);
static void setfieldvalue(lua_State* lua, const char* name, int32_t index);
static void define_module(lua_State* lua);
static int kt_atoi(lua_State* lua);
static int kt_atoix(lua_State* lua);
static int kt_atof(lua_State* lua);
static int kt_hash_murmur(lua_State* lua);
static int kt_hash_fnv(lua_State* lua);
static int kt_levdist(lua_State* lua);
static int kt_time(lua_State* lua);
static int kt_sleep(lua_State* lua);
static int kt_pack(lua_State* lua);
static int kt_unpack(lua_State *lua);
static int kt_split(lua_State *lua);
static int kt_codec(lua_State *lua);
static int kt_bit(lua_State *lua);
static int kt_strstr(lua_State *lua);
static int kt_strfwm(lua_State *lua);
static int kt_strbwm(lua_State *lua);
static int kt_regex(lua_State *lua);
static int kt_arraydump(lua_State *lua);
static int kt_arrayload(lua_State *lua);
static int kt_mapdump(lua_State *lua);
static int kt_mapload(lua_State *lua);
static void define_err(lua_State* lua);
static int err_new(lua_State* lua);
static int err_tostring(lua_State* lua);
static int err_set(lua_State* lua);
static int err_code(lua_State* lua);
static int err_name(lua_State* lua);
static int err_message(lua_State* lua);
static void define_vis(lua_State* lua);
static int vis_new(lua_State* lua);
static int vis_visit_full(lua_State* lua);
static int vis_visit_empty(lua_State* lua);
static void define_fproc(lua_State* lua);
static int fproc_new(lua_State* lua);
static int fproc_process(lua_State* lua);
static void define_cur(lua_State* lua);
static int cur_new(lua_State* lua);
static int cur_gc(lua_State* lua);
static int cur_tostring(lua_State* lua);
static int cur_call(lua_State* lua);
static int cur_disable(lua_State* lua);
static int cur_accept(lua_State* lua);
static int cur_set_value(lua_State* lua);
static int cur_remove(lua_State* lua);
static int cur_get_key(lua_State* lua);
static int cur_get_value(lua_State* lua);
static int cur_get(lua_State* lua);
static int cur_seize(lua_State* lua);
static int cur_jump(lua_State* lua);
static int cur_jump_back(lua_State* lua);
static int cur_step(lua_State* lua);
static int cur_step_back(lua_State* lua);
static int cur_db(lua_State* lua);
static int cur_error(lua_State* lua);
static void define_db(lua_State* lua);
static int db_new(lua_State* lua);
static int db_gc(lua_State* lua);
static int db_tostring(lua_State* lua);
static int db_index(lua_State* lua);
static int db_newindex(lua_State* lua);
static int db_new_ptr(lua_State* lua);
static int db_delete_ptr(lua_State* lua);
static int db_process(lua_State* lua);
static int db_error(lua_State* lua);
static int db_open(lua_State* lua);
static int db_close(lua_State* lua);
static int db_accept(lua_State* lua);
static int db_accept_bulk(lua_State* lua);
static int db_iterate(lua_State* lua);
static int db_set(lua_State* lua);
static int db_add(lua_State* lua);
static int db_replace(lua_State* lua);
static int db_append(lua_State* lua);
static int db_increment(lua_State* lua);
static int db_increment_double(lua_State* lua);
static int db_cas(lua_State* lua);
static int db_remove(lua_State* lua);
static int db_get(lua_State* lua);
static int db_check(lua_State* lua);
static int db_seize(lua_State* lua);
static int db_set_bulk(lua_State* lua);
static int db_remove_bulk(lua_State* lua);
static int db_get_bulk(lua_State* lua);
static int db_clear(lua_State* lua);
static int db_synchronize(lua_State* lua);
static int db_occupy(lua_State* lua);
static int db_copy(lua_State* lua);
static int db_begin_transaction(lua_State* lua);
static int db_end_transaction(lua_State* lua);
static int db_transaction(lua_State* lua);
static int db_dump_snapshot(lua_State* lua);
static int db_load_snapshot(lua_State* lua);
static int db_count(lua_State* lua);
static int db_size(lua_State* lua);
static int db_path(lua_State* lua);
static int db_status(lua_State* lua);
static int db_match_prefix(lua_State* lua);
static int db_match_regex(lua_State* lua);
static int db_match_similar(lua_State* lua);
static int db_merge(lua_State* lua);
static int db_mapreduce(lua_State* lua);
static int db_mapreduce_emit(lua_State* lua);
static int db_mapreduce_iter(lua_State* lua);
static int db_cursor(lua_State* lua);
static int db_cursor_process(lua_State* lua);
static int db_pairs(lua_State* lua);
static int serv_log(lua_State* lua);
/**
* ScriptProcessor internal.
*/
struct ScriptProcessorCore {
std::string path;
int32_t thid;
kt::RPCServer* serv;
kt::TimedDB* dbs;
int32_t dbnum;
const std::map<std::string, int32_t>* dbmap;
lua_State *lua;
};
/**
* Wrapper of a string.
*/
class StringBuilder {
public:
StringBuilder(lua_State* lua, int32_t index) : str_(), ok_(false) {
char nbuf[kc::NUMBUFSIZ];
const char* ptr = NULL;
size_t size = 0;
switch (lua_type(lua, index)) {
case LUA_TNUMBER: {
double num = lua_tonumber(lua, index);
if (num == std::floor(num)) {
std::snprintf(nbuf, sizeof(nbuf) - 1, "%lld", (long long)num);
} else {
std::snprintf(nbuf, sizeof(nbuf) - 1, "%f", num);
}
nbuf[sizeof(nbuf)-1] = '\0';
ptr = nbuf;
size = std::strlen(ptr);
break;
}
case LUA_TBOOLEAN: {
ptr = lua_toboolean(lua, index) ? "true" : "false";
size = std::strlen(ptr);
break;
}
case LUA_TSTRING: {
ptr = lua_tolstring(lua, index, &size);
break;
}
}
if (ptr) {
str_.append(ptr, size);
ok_ = true;
}
}
const std::string& str() {
return str_;
}
bool ok() {
return ok_;
}
private:
std::string str_;
bool ok_;
};
/**
* Default constructor.
*/
ScriptProcessor::ScriptProcessor() {
_assert_(true);
ScriptProcessorCore* core = new ScriptProcessorCore;
core->thid = 0;
core->serv = NULL;
core->dbs = NULL;
core->dbnum = 0;
core->dbmap = NULL;
core->lua = NULL;
opq_ = core;
}
/**
* Destructor.
*/
ScriptProcessor::~ScriptProcessor() {
_assert_(true);
ScriptProcessorCore* core = (ScriptProcessorCore*)opq_;
lua_State* lua = core->lua;
if (lua) lua_close(lua);
delete core;
}
/**
* Set domain-specific resources.
*/
bool ScriptProcessor::set_resources(int32_t thid, kt::RPCServer* serv,
kt::TimedDB* dbs, int32_t dbnum,
const std::map<std::string, int32_t>* dbmap) {
_assert_(serv && dbs && dbnum >= 0 && dbmap);
ScriptProcessorCore* core = (ScriptProcessorCore*)opq_;
lua_State *lua = luaL_newstate();
if (!lua) return false;
luaL_openlibs(lua);
lua_settop(lua, 0);
lua_newtable(lua);
define_module(lua);
define_err(lua);
define_vis(lua);
define_fproc(lua);
define_cur(lua);
define_db(lua);
lua_setglobal(lua, "__kyototycoon__");
lua_getglobal(lua, "__kyototycoon__");
lua_pushlightuserdata(lua, serv);
lua_setfield(lua, -2, "__serv__");
setfieldstr(lua, "VERSION", kt::VERSION);
setfielduint(lua, "RVSUCCESS", kt::RPCClient::RVSUCCESS);
setfielduint(lua, "RVENOIMPL", kt::RPCClient::RVENOIMPL);
setfielduint(lua, "RVEINVALID", kt::RPCClient::RVEINVALID);
setfielduint(lua, "RVELOGIC", kt::RPCClient::RVELOGIC);
setfielduint(lua, "RVEINTERNAL", kt::RPCClient::RVEINTERNAL);
setfielduint(lua, "thid", thid);
lua_getfield(lua, -1, "DB");
lua_newtable(lua);
for (int32_t i = 0; i < dbnum; i++) {
lua_getfield(lua, -2, "new");
lua_pushvalue(lua, -3);
lua_pushlightuserdata(lua, dbs + i);
if (lua_pcall(lua, 2, 1, 0) != 0) {
lua_close(lua);
return false;
}
lua_rawseti(lua, -2, i + 1);
}
lua_rawgeti(lua, -1, 1);
lua_setfield(lua, -4, "db");
std::map<std::string, int32_t>::const_iterator it = dbmap->begin();
std::map<std::string, int32_t>::const_iterator itend = dbmap->end();
while (it != itend) {
lua_rawgeti(lua, -1, it->second + 1);
lua_setfield(lua, -2, it->first.c_str());
++it;
}
lua_setfield(lua, -3, "dbs");
lua_pop(lua, 1);
setfieldfunc(lua, "log", serv_log);
lua_settop(lua, 0);
core->thid = thid;
core->serv = serv;
core->dbs = dbs;
core->dbnum = dbnum;
core->dbmap = dbmap;
core->lua = lua;
return true;
}
/**
* Load a script file.
*/
bool ScriptProcessor::load(const std::string& path) {
_assert_(true);
ScriptProcessorCore* core = (ScriptProcessorCore*)opq_;
core->path = path;
lua_State* lua = core->lua;
int64_t size;
char* script = kc::File::read_file(path, &size);
if (!script) return false;
bool err = false;
lua_settop(lua, 0);
if (luaL_loadstring(lua, script) != 0 || lua_pcall(lua, 0, 0, 0) != 0) {
reporterror(core, "(init)");
err = true;
}
delete[] script;
return !err;
}
/**
* Clear the internal state.
*/
void ScriptProcessor::clear() {
_assert_(true);
ScriptProcessorCore* core = (ScriptProcessorCore*)opq_;
lua_State* lua = core->lua;
if (lua) lua_close(lua);
core->thid = 0;
core->serv = NULL;
core->dbs = NULL;
core->dbnum = 0;
core->dbmap = NULL;
core->lua = NULL;
}
/**
* Call a procedure.
*/
kt::RPCClient::ReturnValue ScriptProcessor::call(const std::string& name,
const std::map<std::string, std::string>& inmap,
std::map<std::string, std::string>& outmap) {
_assert_(true);
ScriptProcessorCore* core = (ScriptProcessorCore*)opq_;
lua_State* lua = core->lua;
lua_settop(lua, 0);
lua_newtable(lua);
lua_getglobal(lua, name.c_str());
if (!lua_isfunction(lua, -1)) {
lua_settop(lua, 0);
return kt::RPCClient::RVENOIMPL;
}
lua_newtable(lua);
std::map<std::string, std::string>::const_iterator it = inmap.begin();
std::map<std::string, std::string>::const_iterator itend = inmap.end();
while (it != itend) {
lua_pushlstring(lua, it->first.data(), it->first.size());
lua_pushlstring(lua, it->second.data(), it->second.size());
lua_settable(lua, -3);
++it;
}
lua_pushvalue(lua, 1);
kt::RPCClient::ReturnValue rv;
if (lua_pcall(lua, 2, 1, 0) == 0) {
lua_pushnil(lua);
while (lua_next(lua, 1) != 0) {
StringBuilder key(lua, -2);
StringBuilder value(lua, -1);
if (key.ok() && value.ok()) outmap[key.str()] = value.str();
lua_pop(lua, 1);
}
rv = (kt::RPCClient::ReturnValue)lua_tointeger(lua, -1);
} else {
reporterror(core, name.c_str());
rv = kt::RPCClient::RVEINTERNAL;
}
lua_settop(lua, 0);
return rv;
}
/**
* Burrow of cursors no longer in use.
*/
class CursorBurrow {
private:
typedef std::vector<kt::TimedDB::Cursor*> CursorList;
public:
explicit CursorBurrow() : lock_(), dcurs_() {}
~CursorBurrow() {
sweap();
}
void sweap() {
kc::ScopedSpinLock lock(&lock_);
if (dcurs_.size() > 0) {
CursorList::iterator dit = dcurs_.begin();
CursorList::iterator ditend = dcurs_.end();
while (dit != ditend) {
kt::TimedDB::Cursor* cur = *dit;
delete cur;
++dit;
}
dcurs_.clear();
}
}
void deposit(kt::TimedDB::Cursor* cur) {
kc::ScopedSpinLock lock(&lock_);
dcurs_.push_back(cur);
}
private:
kc::SpinLock lock_;
CursorList dcurs_;
} g_curbur;
/**
* Wrapper of a database.
*/
struct SoftCursor {
kt::TimedDB::Cursor* cur;
};
/**
* Wrapper of a database.
*/
struct SoftDB {
kt::TimedDB* db;
bool light;
};
/**
* Wrapper of a visitor.
*/
class SoftVisitor : public kt::TimedDB::Visitor {
public:
explicit SoftVisitor(lua_State* lua, bool writable) :
lua_(lua), writable_(writable), top_(0) {
top_ = lua_gettop(lua_);
}
private:
const char* visit_full(const char* kbuf, size_t ksiz,
const char* vbuf, size_t vsiz, size_t* sp, int64_t* xtp) {
lua_settop(lua_, top_);
int32_t argc;
if (lua_istable(lua_, -1)) {
lua_getfield(lua_, -1, "visit_full");
if (!lua_isfunction(lua_, -1)) return NOP;
lua_pushvalue(lua_, -2);
lua_pushlstring(lua_, kbuf, ksiz);
lua_pushlstring(lua_, vbuf, vsiz);
lua_pushnumber(lua_, *xtp < kt::TimedDB::XTMAX ? *xtp : kt::TimedDB::XTMAX);
argc = 4;
} else {
if (!lua_isfunction(lua_, -1)) return NOP;
lua_pushvalue(lua_, -1);
lua_pushlstring(lua_, kbuf, ksiz);
lua_pushlstring(lua_, vbuf, vsiz);
lua_pushnumber(lua_, *xtp < kt::TimedDB::XTMAX ? *xtp : kt::TimedDB::XTMAX);
argc = 3;
}
const char* rv = NOP;
if (lua_pcall(lua_, argc, 2, 0) == 0) {
if (lua_islightuserdata(lua_, -2)) {
const char* trv = (const char*)lua_touserdata(lua_, -2);
if (trv == kt::TimedDB::Visitor::REMOVE) rv = kt::TimedDB::Visitor::REMOVE;
} else {
size_t rsiz;
const char* rbuf = lua_tolstring(lua_, -2, &rsiz);
if (rbuf) {
rv = rbuf;
*sp = rsiz;
}
*xtp = lua_isnumber(lua_, -1) ? lua_tonumber(lua_, -1) : -*xtp;
}
}
if (!writable_) rv = NULL;
return rv;
}
const char* visit_empty(const char* kbuf, size_t ksiz, size_t* sp, int64_t* xtp) {
lua_settop(lua_, top_);
int32_t argc;
if (lua_istable(lua_, -1)) {
lua_getfield(lua_, -1, "visit_empty");
if (!lua_isfunction(lua_, -1)) return NOP;
lua_pushvalue(lua_, -2);
lua_pushlstring(lua_, kbuf, ksiz);
argc = 2;
} else {
if (!lua_isfunction(lua_, -1)) return NOP;
lua_pushvalue(lua_, -1);
lua_pushlstring(lua_, kbuf, ksiz);
lua_pushnil(lua_);
argc = 2;
}
const char* rv = NOP;
if (lua_pcall(lua_, argc, 2, 0) == 0) {
if (lua_islightuserdata(lua_, -1)) {
const char* trv = (const char*)lua_touserdata(lua_, -1);
if (trv == kt::TimedDB::Visitor::REMOVE) rv = kt::TimedDB::Visitor::REMOVE;
} else {
size_t rsiz;
const char* rbuf = lua_tolstring(lua_, -2, &rsiz);
if (rbuf) {
rv = rbuf;
*sp = rsiz;
}
*xtp = lua_isnumber(lua_, -1) ? lua_tonumber(lua_, -1) : kc::INT64MAX;
}
}
if (!writable_) rv = NULL;
return rv;
}
void visit_before() {
lua_settop(lua_, top_);
if (!lua_istable(lua_, -1)) return;
lua_getfield(lua_, -1, "visit_before");
if (!lua_isfunction(lua_, -1)) return;
lua_pushvalue(lua_, -2);
lua_pcall(lua_, 1, 0, 0);
}
void visit_after() {
lua_settop(lua_, top_);
if (!lua_istable(lua_, -1)) return;
lua_getfield(lua_, -1, "visit_after");
if (!lua_isfunction(lua_, -1)) return;
lua_pushvalue(lua_, -2);
lua_pcall(lua_, 1, 0, 0);
}
lua_State* lua_;
bool writable_;
int32_t top_;
};
/**
* Wrapper of a file processor.
*/
class SoftFileProcessor : public kc::BasicDB::FileProcessor {
public:
explicit SoftFileProcessor(lua_State* lua) : lua_(lua) {}
private:
bool process(const std::string& path, int64_t count, int64_t size) {
int32_t argc;
if (lua_istable(lua_, -1)) {
lua_getfield(lua_, -1, "process");
lua_pushvalue(lua_, -2);
lua_pushstring(lua_, path.c_str());
lua_pushnumber(lua_, count);
lua_pushnumber(lua_, size);
argc = 4;
} else {
lua_pushvalue(lua_, -1);
lua_pushstring(lua_, path.c_str());
lua_pushnumber(lua_, count);
lua_pushnumber(lua_, size);
argc = 3;
}
bool rv = false;
if (lua_pcall(lua_, argc, 1, 0) == 0) rv = lua_toboolean(lua_, -1);
return rv;
}
lua_State* lua_;
};
class SoftMapReduce : public kt::MapReduce {
public:
SoftMapReduce(lua_State* lua, int32_t logidx, int32_t procidx) :
lua_(lua), logidx_(logidx), procidx_(procidx) {}
bool map(const char* kbuf, size_t ksiz, const char* vbuf, size_t vsiz) {
int32_t top = lua_gettop(lua_);
lua_pushlightuserdata(lua_, (void*)this);
lua_setglobal(lua_, "__mr_self");
lua_pushvalue(lua_, 2);
lua_pushlstring(lua_, kbuf, ksiz);
lua_pushlstring(lua_, vbuf, vsiz);
lua_pushcfunction(lua_, db_mapreduce_emit);
bool rv;
if (lua_pcall(lua_, 3, 1, 0) == 0) {
rv = lua_toboolean(lua_, -1);
} else {
rv = false;
}
lua_settop(lua_, top);
return rv;
}
bool reduce(const char* kbuf, size_t ksiz, ValueIterator* iter) {
int32_t top = lua_gettop(lua_);
lua_pushlightuserdata(lua_, (void*)iter);
lua_setglobal(lua_, "__mr_iter");
lua_pushvalue(lua_, 3);
lua_pushlstring(lua_, kbuf, ksiz);
lua_pushcfunction(lua_, db_mapreduce_iter);
bool rv;
if (lua_pcall(lua_, 2, 1, 0) == 0) {
rv = lua_toboolean(lua_, -1);
} else {
rv = false;
}
lua_settop(lua_, top);
return rv;
}
bool log(const char* name, const char* message) {
if (logidx_ < 1) return true;
int32_t top = lua_gettop(lua_);
lua_pushvalue(lua_, logidx_);
lua_pushstring(lua_, name);
lua_pushstring(lua_, message);
bool rv;
if (lua_pcall(lua_, 2, 1, 0) == 0) {
rv = lua_toboolean(lua_, -1);
} else {
rv = false;
}
lua_settop(lua_, top);
return rv;
}
bool preprocess() {
if (procidx_ < 1) return true;
int32_t top = lua_gettop(lua_);
lua_pushlightuserdata(lua_, (void*)this);
lua_setglobal(lua_, "__mr_self");
lua_pushvalue(lua_, procidx_);
lua_pushcfunction(lua_, db_mapreduce_emit);
bool rv;
if (lua_pcall(lua_, 1, 1, 0) == 0) {
rv = lua_toboolean(lua_, -1);
} else {
rv = false;
}
lua_settop(lua_, top);
return rv;
}
bool midprocess() {
return preprocess();
}
bool postprocess() {
if (procidx_ < 1) return true;
int32_t top = lua_gettop(lua_);
lua_pushlightuserdata(lua_, (void*)this);
lua_setglobal(lua_, "__mr_self");
lua_pushvalue(lua_, procidx_);
bool rv;
if (lua_pcall(lua_, 0, 1, 0) == 0) {
rv = lua_toboolean(lua_, -1);
} else {
rv = false;
}
lua_settop(lua_, top);
return rv;
}
bool emit_public(const char* kbuf, size_t ksiz, const char* vbuf, size_t vsiz) {
return emit(kbuf, ksiz, vbuf, vsiz);
}
private:
lua_State* lua_;
int32_t logidx_;
int32_t procidx_;
};
/**
* Report error information.
*/
static void reporterror(ScriptProcessorCore* core, const char* name) {
lua_State* lua = core->lua;
int argc = lua_gettop(lua);
core->serv->log(kt::RPCServer::Logger::ERROR, "[SCRIPT]: %s: %s",
name, argc > 0 ? lua_tostring(lua, argc) : "unknown");
}
/**
* Throw the invalid argument error.
*/
static void throwinvarg(lua_State* lua, const char* func) {
char msg[256];
size_t len = std::sprintf(msg, "%s: invalid arguments", func);
lua_pushlstring(lua, msg, len);
lua_error(lua);
}
/**
* Set a field of unsigned integer.
*/
static void setfielduint(lua_State* lua, const char* name, uint32_t num) {
lua_pushinteger(lua, num);
lua_setfield(lua, -2, name);
}
/**
* Set a field of string.
*/
static void setfieldstr(lua_State* lua, const char* name, const char* str) {
lua_pushstring(lua, str);
lua_setfield(lua, -2, name);
}
/**
* Set a field of function.
*/
static void setfieldfunc(lua_State* lua, const char* name, lua_CFunction func) {
lua_pushcfunction(lua, func);
lua_setfield(lua, -2, name);
}
/**
* Set a field of stacked value.
*/
static void setfieldvalue(lua_State* lua, const char* name, int32_t index) {
lua_pushvalue(lua, index);
lua_setfield(lua, -2, name);
}
/**
* Define objects of the module.
*/
static void define_module(lua_State* lua) {
setfieldfunc(lua, "atoi", kt_atoi);
setfieldfunc(lua, "atoix", kt_atoix);
setfieldfunc(lua, "atof", kt_atof);
setfieldfunc(lua, "hash_murmur", kt_hash_murmur);
setfieldfunc(lua, "hash_fnv", kt_hash_fnv);
setfieldfunc(lua, "levdist", kt_levdist);
setfieldfunc(lua, "time", kt_time);
setfieldfunc(lua, "sleep", kt_sleep);
setfieldfunc(lua, "pack", kt_pack);
setfieldfunc(lua, "unpack", kt_unpack);
setfieldfunc(lua, "split", kt_split);
setfieldfunc(lua, "codec", kt_codec);
setfieldfunc(lua, "bit", kt_bit);
setfieldfunc(lua, "strstr", kt_strstr);
setfieldfunc(lua, "strfwm", kt_strfwm);
setfieldfunc(lua, "strbwm", kt_strbwm);
setfieldfunc(lua, "regex", kt_regex);
setfieldfunc(lua, "arraydump", kt_arraydump);
setfieldfunc(lua, "arrayload", kt_arrayload);
setfieldfunc(lua, "mapdump", kt_mapdump);
setfieldfunc(lua, "mapload", kt_mapload);
}
/**
* Implementation of atoi.
*/
static int kt_atoi(lua_State* lua) {
int32_t argc = lua_gettop(lua);
if (argc != 1) throwinvarg(lua, __KCFUNC__);
const char* str = lua_tostring(lua, 1);
if (!str) return 0;
lua_pushnumber(lua, kc::atoi(str));
return 1;
}
/**
* Implementation of atoix.
*/
static int kt_atoix(lua_State* lua) {
int32_t argc = lua_gettop(lua);
if (argc != 1) throwinvarg(lua, __KCFUNC__);
const char* str = lua_tostring(lua, 1);
if (!str) return 0;
lua_pushnumber(lua, kc::atoix(str));
return 1;
}
/**
* Implementation of atof.
*/
static int kt_atof(lua_State* lua) {
int32_t argc = lua_gettop(lua);
if (argc != 1) throwinvarg(lua, __KCFUNC__);
const char* str = lua_tostring(lua, 1);
if (!str) return 0;
lua_pushnumber(lua, kc::atof(str));
return 1;
}
/**
* Implementation of hash_murmur.
*/
static int kt_hash_murmur(lua_State* lua) {
int32_t argc = lua_gettop(lua);
if (argc != 1) throwinvarg(lua, __KCFUNC__);
size_t len;
const char* str = lua_tolstring(lua, 1, &len);
if (!str) return 0;
lua_pushinteger(lua, kc::hashmurmur(str, len) & ((1ULL << 48) - 1));
return 1;
}
/**
* Implementation of hash_fnv.
*/
static int kt_hash_fnv(lua_State* lua) {
int32_t argc = lua_gettop(lua);
if (argc != 1) throwinvarg(lua, __KCFUNC__);
size_t len;
const char* str = lua_tolstring(lua, 1, &len);
if (!str) return 0;
lua_pushinteger(lua, kc::hashfnv(str, len) & ((1ULL << 48) - 1));
return 1;
}
/**
* Implementation of levdist.
*/
static int kt_levdist(lua_State* lua) {
int32_t argc = lua_gettop(lua);
if (argc < 2) throwinvarg(lua, __KCFUNC__);
size_t asiz;
const char* abuf = lua_tolstring(lua, 1, &asiz);
size_t bsiz;
const char* bbuf = lua_tolstring(lua, 2, &bsiz);
if (!abuf || !bbuf) throwinvarg(lua, __KCFUNC__);
bool utf = argc > 2 ? lua_toboolean(lua, 3) : false;
size_t dist;
if (utf) {
uint32_t astack[128];
uint32_t* aary = asiz > sizeof(astack) / sizeof(*astack) ? new uint32_t[asiz] : astack;
size_t anum;
kc::strutftoucs(abuf, asiz, aary, &anum);
uint32_t bstack[128];
uint32_t* bary = bsiz > sizeof(bstack) / sizeof(*bstack) ? new uint32_t[bsiz] : bstack;
size_t bnum;
kc::strutftoucs(bbuf, bsiz, bary, &bnum);
dist = kc::strucsdist(aary, anum, bary, bnum);
if (bary != bstack) delete[] bary;
if (aary != astack) delete[] aary;
} else {
dist = kc::memdist(abuf, asiz, bbuf, bsiz);
}
lua_pushinteger(lua, dist);
return 1;
}
/**
* Implementation of hash_fnv.
*/
static int kt_time(lua_State* lua) {
int32_t argc = lua_gettop(lua);
if (argc != 0) throwinvarg(lua, __KCFUNC__);
lua_pushnumber(lua, kc::time());
return 1;
}
/**
* Implementation of sleep.
*/
static int kt_sleep(lua_State* lua) {
int32_t argc = lua_gettop(lua);
if (argc > 1) throwinvarg(lua, __KCFUNC__);
double sec = argc > 0 ? lua_tonumber(lua, 1) : 0;
if (sec > 0) {
kc::Thread::sleep(sec);
} else {
kc::Thread::yield();
}
return 0;
}
/**
* Implementation of pack.
*/
static int kt_pack(lua_State* lua) {
int32_t argc = lua_gettop(lua);
if (argc < 1) throwinvarg(lua, __KCFUNC__);
const char* format = lua_tostring(lua, 1);
if (!format) throwinvarg(lua, __KCFUNC__);
lua_newtable(lua);
int32_t aidx = argc + 1;
int32_t eidx = 1;
for (int32_t i = 2; i <= argc; i++) {
size_t len;
switch (lua_type(lua, i)) {
case LUA_TNUMBER: case LUA_TSTRING: {
lua_pushvalue(lua, i);
lua_rawseti(lua, aidx, eidx++);
break;
}
case LUA_TTABLE: {
len = lua_objlen(lua, i);
for (size_t j = 1; j <= len; j++) {
lua_rawgeti(lua, i, j);
lua_rawseti(lua, aidx, eidx++);
}
break;
}
default: {
lua_pushnumber(lua, 0);
lua_rawseti(lua, aidx, eidx++);
break;
}
}
}
lua_replace(lua, 2);
lua_settop(lua, 2);
std::string xstr;
int32_t emax = eidx - 1;
eidx = 1;
while (*format != '\0') {
int32_t c = *format;
int32_t loop = 1;
if (format[1] == '*') {
loop = kc::INT32MAX;
format++;
} else if (format[1] >= '0' && format[1] <= '9') {
char* suffix;
loop = std::strtol(format + 1, &suffix, 10);
format = suffix - 1;
}
loop = loop < emax ? loop : emax;
int32_t end = eidx + loop - 1;
if (end > emax) end = emax;
while (eidx <= end) {
lua_rawgeti(lua, 2, eidx);
double num = lua_tonumber(lua, 3);
lua_pop(lua, 1);
uint8_t cnum;
uint16_t snum;
uint32_t inum;
uint64_t lnum;
double dnum;
float fnum;
uint64_t wnum;
char wbuf[kc::NUMBUFSIZ], *wp;
switch (c) {
case 'c': case 'C': {
cnum = num;
xstr.append((char*)&cnum, sizeof(cnum));
break;
}
case 's': case 'S': {
snum = num;
xstr.append((char*)&snum, sizeof(snum));
break;
}
case 'i': case 'I': {
inum = num;
xstr.append((char*)&inum, sizeof(inum));
break;
}
case 'l': case 'L': {
lnum = num;
xstr.append((char*)&lnum, sizeof(lnum));
break;
}
case 'f': case 'F': {
fnum = num;
xstr.append((char*)&fnum, sizeof(fnum));
break;
}
case 'd': case 'D': {
dnum = num;
xstr.append((char*)&dnum, sizeof(dnum));
break;
}
case 'n': {
snum = num;
snum = kc::hton16(snum);
xstr.append((char*)&snum, sizeof(snum));
break;
}
case 'N': {
inum = num;
inum = kc::hton32(inum);
xstr.append((char*)&inum, sizeof(inum));
break;