-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrajUpdateAlgebra.cpp
836 lines (732 loc) · 24.7 KB
/
TrajUpdateAlgebra.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
#include "Algebra.h"
#include "NestedList.h"
#include "QueryProcessor.h"
#include "StandardTypes.h"
#include "Symbols.h"
#include "DateTime.h"
#include "Stream.h"
#include "TBTree.h"
#include <iostream>
#include <fstream>
#include <map>
#include "WinUnix.h"
#include "SecondoSystem.h"
#include "ListUtils.h"
#include "Attribute.h"
#include "DateTime.h"
#include "RTreeAlgebra.h"
#include "RectangleAlgebra.h"
#include "TemporalAlgebra.h"
#include "UploadUnit.h"
#include "Symbols.h"
extern NestedList *nl;
extern QueryProcessor *qp;
/****************************************************************
1.operator convertMP2UU2
***************************************************************/
// type map function
//
bool tuisMPoint = true;
ListExpr ConvertMP2UU2TM(ListExpr args)
{
NList type(args);
if ( !type.hasLength(4) )
{
return listutils::typeError("Expecting four arguments.");
}
NList first = type.first();
if ( !first.hasLength(2) ||
!first.first().isSymbol(Symbol::STREAM()) ||
!first.second().hasLength(2) ||
!first.second().first().isSymbol(Tuple::BasicType()) ||
!IsTupleDescription( first.second().second().listExpr() ))
{
return listutils::typeError("Error in first argument!");
}
if ( !nl->IsEqual(nl->Second(args), CcInt::BasicType()) )
{
return NList::typeError( "int for second argument expected!" );
}
NList third = type.third();
if ( !third.isSymbol() )
{
return NList::typeError( "Attribute name for third argument expected!" );
}
NList fourth = type.fourth();
if ( !fourth.isSymbol() )
{
return NList::typeError( "Attribute name for fourth argument expected!" );
}
string attrname1 = type.third().str();
ListExpr attrtype1 = nl->Empty();
int j = FindAttribute(first.second().second().listExpr(),attrname1,attrtype1);
string attrname2 = type.fourth().str();
ListExpr attrtype2 = nl->Empty();
int k = FindAttribute(first.second().second().listExpr(),attrname2,attrtype2);
if ( j != 0 )
{
if ( nl->SymbolValue(attrtype1) != CcInt::BasicType() )
{
return NList::typeError("First attribute type is not of type int.");
}
}
else
{
return NList::typeError("Unknown attribute name '" + attrname1 + "' !");
}
if ( k != 0 )
{
if ( nl->SymbolValue(attrtype2) != "mpoint" &&
nl->SymbolValue(attrtype2) != UPoint::BasicType())
{
return NList::typeError("Second attribute is not of type"
" mpoint or upoint.");
}
if ( nl->SymbolValue(attrtype2) == UPoint::BasicType()) tuisMPoint = false;
else tuisMPoint = true;
}
else
{
return NList::typeError("Unknown attribute name '" + attrname2 + "' !");
}
// Create output list
ListExpr tupleList = nl->TwoElemList(
nl->TwoElemList(nl->SymbolAtom("Upload"),
nl->SymbolAtom(UploadUnit::BasicType())),
nl->TwoElemList(nl->SymbolAtom("Time"),
nl->SymbolAtom(CcReal::BasicType())));
ListExpr outputstream = nl->TwoElemList(nl->SymbolAtom(Symbol::STREAM()),
nl->TwoElemList(nl->SymbolAtom(Tuple::BasicType()),
tupleList));
return NList( NList(Symbol::APPEND()),
nl->TwoElemList(nl->IntAtom(j), nl->IntAtom(k)),
outputstream ).listExpr();
}
//value map function
//
int ConvertMP2UU2VM(Word* args, Word& result, int message,
Word& local, Supplier s)
{
struct Iterator
{
Iterator()
{
it = 0;
cnt = 0;
}
int it;
int cnt;
Word currentTupleWord;
TupleType *tupletype;
};
Iterator* iterator = static_cast<Iterator*>(local.addr);
switch( message )
{
case OPEN:
{
iterator = new Iterator();
local.addr = iterator;
iterator->tupletype = new TupleType(nl->Second(GetTupleResultType(s)));
qp->Open(args[0].addr);
return 0;
}
case REQUEST:
{
int numUploads = static_cast<CcInt*>( args[1].addr )->GetIntval();
int attr1 = static_cast<CcInt*>( args[4].addr )->GetIntval() - 1;
int attr2 = static_cast<CcInt*>( args[5].addr )->GetIntval() - 1;
bool received = true;
if (iterator->it == 0)
{
qp->Request( args[0].addr, iterator->currentTupleWord );
received = qp->Received(args[0].addr);
}
if ( received && iterator->cnt < numUploads )
{
Tuple* currentTuple = static_cast<Tuple*>
( iterator->currentTupleWord.addr );
int moID = static_cast<CcInt*>(currentTuple->GetAttribute(attr1))
->GetIntval();
if (tuisMPoint)
{
MPoint* mp = static_cast<MPoint*>(currentTuple->GetAttribute(attr2));
UPoint up;
mp->Get(iterator->it,up);
UnitPos pos( up.p1.GetX(), up.p1.GetY() );
UploadUnit* uu = new UploadUnit(moID, up.timeInterval.end, pos );
CcReal *cctime = new CcReal(up.timeInterval.end.ToDouble());
Tuple* tup = new Tuple( iterator->tupletype );
tup->PutAttribute( 0, ( Attribute* ) uu );
tup->PutAttribute( 1, ( Attribute* ) cctime );
result.addr = tup;
iterator->it++;
iterator->cnt++;
if (iterator->it == mp->GetNoComponents()) iterator->it = 0;
}
else
{
UPoint* up = static_cast<UPoint*>(currentTuple->GetAttribute(attr2));
UnitPos pos( up->p1.GetX(), up->p1.GetY() );
UploadUnit* uu = new UploadUnit(moID, up->timeInterval.end, pos );
CcReal *cctime = new CcReal(up->timeInterval.end.ToDouble());
Tuple* tup = new Tuple( iterator->tupletype );
tup->PutAttribute( 0, ( Attribute* ) uu );
tup->PutAttribute( 1, ( Attribute* ) cctime );
result.addr = tup;
iterator->cnt++;
}
return YIELD;
}
else
{
delete iterator;
local.addr = 0;
result.addr = 0;
return CANCEL;
}
}
case CLOSE:
{
return 0;
}
default:
{
return -1;
}
}
}
//
// operator info
struct ConvertMP2UU2Info : OperatorInfo {
ConvertMP2UU2Info()
{
name = "convertMP2UU2";
signature = "((stream (tuple([a1:d1, ..., ai:int, ..., "
"aj:mpoint|upoint, ..., an:dn]))) x int x ai x aj)"
" -> stream (tuple (Upload uploadunit))";
syntax = "_ convertMP2UU [ _, _, _ ]";
meaning = "Converts mpoints into upload units.";
}
};
/****************************************************************************
Insert
***************************************************************************/
/*
//
// UpdateTrajectory
//
ListExpr UpdateTrajectoryTypeMap(ListExpr args)
{
NList type(args);
if(! type.hasLength(3)){
return listutils::typeError("Expecting four arguments");
}
if(! nl->IsEqual(nl->First(args), Upload::BasicType())){
return listutlis::typeError("upload unit for first argument expected!");
}
if(! listutils::isRelDescription(nl->Second(args))){
return listutils::typeError("relation for second argument expected!");
}
string attrname = type.third().str();
ListExpr attrtype = nl->Empty();
int j = FindAttribute(nl->Second(nl->Second(args)), attrname, attrtype);
if(j != 0){
if(nl->SymbolValue(attrtype) != MPoint::BasicType())
{
return listutils::typeError("first attribute type is not type mpoint");
}
}else{
return listutils::typeError("unknown attribute name " + attrname + " !");
}
//
// create output list
ListExpr resulttype = nl->SymbolAtom(CcBool::BasicType());
return NList( NList(Symbol::APPEND()),
NL->OneElemList(nl->IntAtom(j)),
resulttype).listExpr();
}
//
// UpdateTrajectory value map
//
int UpdateTrajectoryValueMap(Word *args, Word &result, int message, Word &local, Supplier s)
{
UploadUnit *unitptr = (UploadUnit*)args[0].addr;
Relation *rel = (Relation*)args[1].addr;
int attrindex = ((CcInt*)(args[3].addr))->GetValue()-1;
}
*/
/****************************************************************************
TrajUpdate
***************************************************************************/
/*
TrajUpdate(const ListExpr typeInfo_current_pos, const ListExpr typeInfo_trajectories)
{
// create a new relation of current position and a new relation storing all trajectories of objects
//
current_pos = new Relation(typeInfo_current_pos);
relDesc_current_pos = current_pos->get
trajectories = new Relation(typeInfo_trajectories);
// create btree and rtree
//
}
TrajUpdate(TupleType *typeInfo_current_pos, TupleType *typeInfo_trajectories)
{
}
TrajUpdate::TrajUpdate(){
cout<<"TrajUpdate::TrajUpdate()"<<endl;
// Create SmiUpdateFile
suf = new SmiUpdateFile(MyPageSize);
suf->Create();
fid = suf->GetFileId();
// Init the TrajUpdate Header
header = new TUHeader();
size = header->size = 0;
pagesize = header->pagesize = MyPageSize;
// Create the header page
SmiUpdatePage *headerpage;
int AppendedPage = suf->AppendNewPage(headerpage);
assert(AppendedPage);
assert(1 == headerpage->GetPageNo());
UpdateHeader();
}
TrajUpdate::TrajUpdate(SmiFileId fileid){
}
TrajUpdate::~TrajUpdate() {
cout<<"TrajUpdate::~TrajUpdate"<<endl;
//
}
// The mandatory set of algebra support function
//
Word TrajUpdate::In(const ListExpr typeinfo, const ListExpr instance, const int errorPos, ListExpr &errorInfo, bool & correct){
cout<<"TrajUpdate::In"<<endl;
correct = false;
Word result = SetWord(Address(0));
//check the list length
if(nl->ListLength(instance) != 1){
cmsg.inFunError("A list of length one expected!");
return result;
}
//check string types
ListExpr strlist = nl->First(instance);
if( !(nl->ListLength(strlist) == 1 &&
nl->IsAtom(nl->First(strlist)) &&
nl->AtomType(nl->First(strlist)) == StringType))
{
cmsg.inFunError("A list of one string type values is expected for the first argument");
return result;
}
string str = nl->StringValue(nl->First(strlist));
result.addr = new TrajUpdate();
((TrajUpdate*)result.addr)->AddString(str);
correct = true;
return result;
}
//
//
ListExpr TrajUpdate::Out(ListExpr typeInfo, Word value){
cout<<"TrajUpdate::Out"<<endl;
TrajUpdate *tu = static_cast<TrajUpdate*>(value.addr);
string str = tu->GetString();
return nl->OneElemList(nl->StringAtom(str, true));
}
//
//
Word TrajUpdate::Create(const ListExpr typeinfo){
cout<<"TrajUpdate::Create"<<endl;
return SetWord(new TrajUpdate());
}
//
//
void TrajUpdate::Delete(const ListExpr typeinfo, Word &w){
cout<<"TrajUpdate::Delete"<<endl;
TrajUpdate *tu = static_cast<TrajUpdate *>(w.addr);
if(tu->suf->IsOpen())
tu->suf->Close();
delete tu;
w.addr = 0;
}
//
//
bool TrajUpdate::Open(SmiRecord &valueRecord, size_t &offset, const ListExpr typeinfo, Word &value){
cout<<"TrajUpdate::Open"<<endl;
SmiFileId fileid;
//db_pgno_t headerPageNo;
bool ok = true;
ok = ok && valueRecord.Read(&fileid, sizeof(SmiFileId), offset);
offset += sizeof(SmiFileId);
TrajUpdate *tu = new TrajUpdate(fileid);
value.addr = tu;
return ok;
}
//
//
bool TrajUpdate::Save(SmiRecord &valueRecord, size_t &offset, const ListExpr typeinfo, Word &w){
cout<<"TrajUpdate::Save"<<endl;
TrajUpdate *tu = static_cast<TrajUpdate *>(w.addr);
bool ok = true;
SmiFileId fileid = tu->fid;
ok = ok && valueRecord.Write(&fileid, sizeof(SmiFileId), offset);
offset += sizeof(SmiFileId);
tu->suf->SyncFile();
tu->suf->Close();
return ok;
}
//
//
void TrajUpdate::Close(const ListExpr typeinfo, Word &w){
cout<<"TrajUpdate::Close"<<endl;
TrajUpdate *tu = static_cast<TrajUpdate *>(w.addr);
delete tu;
w.addr = 0;
}
//
//
Word TrajUpdate::Clone(const ListExpr typeinfo, const Word &w){
cout<<"TrajUpdate::Clone"<<endl;
return SetWord(Address(0));
}
//
//
bool TrajUpdate::KindCheck(ListExpr type, ListExpr &errorInfo){
cout<<"TrajUpdate::KindCheck"<<endl;
return (nl->IsEqual(type, TrajUpdate::BasicType()));
}
//
//
void* TrajUpdate::Cast(void *addr){
cout<<"TrajUpdate::Cast"<<endl;
return (0);
}
//
//
int TrajUpdate::SizeOfObj(){
cout<<"TrajUpdate::SizeOfObj"<<endl;
return sizeof(TrajUpdate);
}
//
//
ListExpr TrajUpdate::Property(){
cout<<"TrajUpdate::Property"<<endl;
return nl->TwoElemList(
nl->FiveElemList(
nl->StringAtom("Signature"),
nl->StringAtom("Example Type List"),
nl->StringAtom("List Rep"),
nl->StringAtom("Example List"),
nl->StringAtom("Remarks")),
nl->FiveElemList(
nl->StringAtom("-> DATA"),
nl->StringAtom(TrajUpdate::BasicType()),
nl->StringAtom("(string)"),
nl->StringAtom("(hello world)"),
nl->StringAtom("a string")));
}
//
//
*/
/****************************************************************
1.operator GenUnitTrip
***************************************************************/
// type map function
// stream(tuple(Uploads uploadunit)) x btree(Id->TupleId) x rel(Id int, pos_x double, pos_y double, timestamp double)
// -> stream(tuple(Id int, UTrip upoint))
ListExpr GenUnitTripTM(ListExpr args)
{
ListExpr first_stream, second_btree, third_rel;
if(nl->ListLength(args) != 3){
return listutils::typeError("three arguments expected!");
}
first_stream = nl->First(args);
second_btree = nl->Second(args);
third_rel = nl->Third(args);
// check the uploadunit
if(! listutils::isTupleStream(first_stream)){
return listutils::typeError("first argument should be stream");
}
ListExpr uu = nl->Second(nl->First(first_stream));
if(! listutils::isSymbol(uu, UploadUnit::BasicType())){
return listutils::typeError("first argument should be UploadUnit");
}
// check the btree
if( nl->IsAtom(second_btree)
|| (nl->ListLength(second_btree) != 3)){
return listutils::typeError("second argument should be btree");
}
ListExpr btree_symbol = nl->First(second_btree);
if(! nl->IsAtom(btree_symbol)
|| (nl->AtomType(btree_symbol) != SymbolType)
|| (nl->SymbolValue(btree_symbol) != BTree::BasicType())){
return listutils::typeError("second argument should be btree");
}
// check the relation of current position
if(! listutils::isRelDescription(third_rel)){
return listutils::typeError("third argument should be relation");
}
//
//
//
//
//
return nl->Empty();
}
//value map function
//
int GenUnitTripVM(Word* args, Word& result, int message, Word& local, Supplier s)
{
switch( message )
{
case OPEN:
{
}
case REQUEST:
{
}
case CLOSE:
{
return 0;
}
}
return 0;
}
//
// operator info
struct GenUnitTripInfo : OperatorInfo {
GenUnitTripInfo()
{
name = "genunittrip";
signature = "((stream (tuple([a1:d1, ..., ai:int, ..., "
"aj:uploaduint, ..., an:dn]))) x btree "
"x rel( tuple( b1:int, b2:double, b3:double, b4:double)))"
" -> stream (tuple (Id int, Utrip upoint))";
syntax = "_ genunittrip [ _, _ ]";
meaning = "generate a unit trip of type upoint from upload unit,"
"btree and relation of current moving object position";
}
};
/****************************************************************
operator UpdateTrajectory
***************************************************************/
// type map function
// stream(tuple(Id int, UTrip upoint)) x rel(tuple(Id int, Trip mpoint)
// x btree x rtree x Id x UTrip x Trip
// -> bool
ListExpr UpdateTrajectoryTM(ListExpr args)
{
ListExpr first, second, third, fourth, fifth, sixth, seventh;
int i1, i2, i3, i4; //i1: Id, i2: UTrip, i3:Id, i4:Trip
string attrname;
ListExpr attrtype, attrlist;
// check the number of arguments
if(nl->ListLength(args) != 7){
listutils::typeError("Seven arguments expected!");
}
//
first = nl->First(args); //stream
second = nl->Second(args); //relation
third = nl->Third(args); //btree
fourth = nl->Fourth(args); //rtree
fifth = nl->Fifth(args); //Id
sixth = nl->Sixth(args); //UTrip
seventh = nl->Seventh(args); //Trip
//
//
// check the stream
if(! listutils::isTupleStream(first)){
return listutils::typeError("First argument should be stream!");
}
// check the relation
if(! listutils::isRelDescription(second)){
return listutils::typeError("Second argument should be relation!");
}
// check the btree
if(nl->IsAtom(third) || nl->ListLength(third) != 3){
return listutils::typeError("Third argument should be btree!");
}
ListExpr btreeSymbol = nl->First(third);
if(! nl->IsAtom(btreeSymbol)
|| (nl->AtomType(btreeSymbol) != SymbolType)
|| (nl->SymbolValue(btreeSymbol) != BTree::BasicType())){
return listutils::typeError("Third argument should be btree!");
}
// check the rtree
if(! listutils::isRTreeDescription(fourth)){
return listutils::typeError("Fourth argument should be rtree!");
}
ListExpr rtreeKeyType = listutils::getRTreeType(fourth);
if((! listutils::isSpatialType(rtreeKeyType)) && (! listutils::isRectangle(rtreeKeyType))){
return listutils::typeError("Rtree not over a spatial attribute!");
}
ListExpr rtreeTupleDesc = nl->Second(fourth);
if(! nl->Equal(rtreeTupleDesc, nl->Second(second))){
return listutils::typeError("Type of rtree and relation are different!");
}
// get the index of Id, UTrip and Trip
// get the index of Id in stream
attrname = nl->SymbolValue(fifth);
attrtype = nl->Empty();
attrlist = nl->Second(nl->Second(first));
i1 = FindAttribute(attrlist, attrname, attrtype);
if(i1 == 0){
return listutils::typeError("Attribute name '"+attrname+"' is not know!");
}else{
if(nl->SymbolValue(attrtype) != CcInt::BasicType()){
return listutils::typeError("Attribute type is not of type int!");
}
}
// get the index of UTrip in stream
attrname = nl->SymbolValue(sixth);
attrtype = nl->Empty();
i2 = FindAttribute(attrlist, attrname, attrtype);
if(i2 == 0){
return listutils::typeError("Attribute name '"+attrname+"' is not know!");
}else{
if(nl->SymbolValue(attrtype) != UPoint::BasicType()){
return listutils::typeError("Attribute type is not of type upoint!");
}
}
// get the index of Id in relation
attrname = nl->SymbolValue(fifth);
attrtype = nl->Empty();
attrlist = nl->Second(nl->Second(second));
i3 = FindAttribute(attrlist, attrname, attrtype);
if(i3 == 0){
return listutils::typeError("Attribute name '"+attrname+"' is not know!");
}else{
if(nl->SymbolValue(attrtype) != CcInt::BasicType()){
return listutils::typeError("Attribute type is not of type int!");
}
}
// get the index of Trip in relation
attrname = nl->SymbolValue(seventh);
attrtype = nl->Empty();
i4 = FindAttribute(attrlist, attrname, attrtype);
if(i4 == 0){
return listutils::typeError("Attribute name '"+attrname+"' is not know!");
}else{
if(nl->SymbolValue(attrtype) != MPoint::BasicType()){
return listutils::typeError("Attribute type is not of type mpoint!");
}
}
// construct a result type, combine the result and attribute index
ListExpr resulttype = nl->SymbolAtom("bool");
return nl->ThreeElemList(
nl->SymbolAtom(Symbol::APPEND()),
nl->FourElemList(
nl->IntAtom(i1),
nl->IntAtom(i2),
nl->IntAtom(i3),
nl->IntAtom(i4)),
resulttype);
}
//
// get tuple id
//
TupleId getTupleId(CcInt *id, BTree *btree)
{
BTreeIterator *it;
//TupleId tid;
assert(id->IsDefined());
assert(btree);
it = btree->ExactMatch(id);
if(it == 0){
return -1;
}
return it->GetId();
}
//value map function
//
// stream(tuple(Id int, UTrip upoint)) x rel(tuple(Id int, Trip mpoint)
// x btree x rtree x Id x UTrip x Trip
// -> bool
int UpdateTrajectoryVM(Word* args, Word& result, int message, Word& local, Supplier s)
{
Relation *rel;
R_Tree<3, TupleId> *rtree;
BTree *btree;
Word elem;
Tuple *tuple_old, *tuple_new, *tuple_stream, *tuple_rel;
int iid1, iutrip, iid2, itrip;
CcInt *vid;
TupleId tid;
UPoint *up;
MPoint *mp;
vector<int> *changedIndeices = NULL;
vector<Attribute *> *newAttrs = NULL;
// get the local addr
rel = (Relation *)args[1].addr;
btree = (BTree *)args[2].addr;
rtree = (R_Tree<3, TupleId> *)args[3].addr;
iid1 = ((CcInt *)args[7].addr)->GetIntval() - 1;
iutrip = ((CcInt *)args[8].addr)->GetIntval() - 1;
iid2 = ((CcInt *)args[9].addr)->GetIntval() - 1;
itrip = ((CcInt *)args[10].addr)->GetIntval() - 1;
changedIndeices = new vector<int>(1);
newAttrs = new vector<Attribute *>(1);
//
qp->Open(args[0].addr);
qp->Request(args[0].addr, elem);
while(qp->Received(args[0].addr)){
tuple_stream = (Tuple *)elem.addr;
vid = (CcInt *)tuple_stream->GetAttribute(iid1);
up = ((UPoint *)tuple_stream->GetAttribute(iutrip))->Clone();
// get the tupleid of object in relation, using btree
tid = getTupleId(vid, btree);
// get the tuple storing the trajectory of object
tuple_rel = rel->GetTuple(tid, true);
mp = (MPoint *)tuple_rel->GetAttribute(itrip)->Clone();
// update the trajectory
mp->StartBulkLoad();
mp->MergeAdd(*up);
mp->EndBulkLoad();
// save the change to relation
(*changedIndeices)[0] = itrip;
(*newAttrs)[0] = mp;
rel->UpdateTuple(tuple_rel, *changedIndeices, *newAttrs);
// update the rtree
// very important
// get next tuple in stream
qp->Request(args[0].addr, elem);
}
qp->Close(args[0].addr);
delete changedIndeices;
delete newAttrs;
result.addr = new CcBool(true);
return 0;
}
//
// operator info
struct UpdateTrajectoryInfo : OperatorInfo {
UpdateTrajectoryInfo()
{
name = "updatetrajectory";
signature = "((stream (tuple([Id:int, UTrip:upoint])))"
"(rel(tuple(Id:int, Trip:mpoint))) x btree x rtree x Id x UTrip x Trip)"
" -> stream (tuple (Upload uploadunit))";
syntax = "_ updatetrajectory [ _, _, _, _, _, _]";
meaning = "add a unit trajectory to trajectories";
}
};
/****************************************************************************
TrajUpdateAlgebra
***************************************************************************/
class TrajUpdateAlgebra : public Algebra
{
public:
TrajUpdateAlgebra() : Algebra()
{
AddOperator( ConvertMP2UU2Info(), ConvertMP2UU2VM, ConvertMP2UU2TM );
AddOperator( UpdateTrajectoryInfo(), UpdateTrajectoryVM, UpdateTrajectoryTM );
}
};
/******************************************************************************
Initialization of SETIAlgebra
******************************************************************************/
extern "C"
Algebra*
InitializeTrajUpdateAlgebra( NestedList *nlRef,
QueryProcessor *qpRef)
{
nl = nlRef;
qp = qpRef;
return (new TrajUpdateAlgebra());
}