spring-integraton: Initiate a transaction after an Aggregator - spring

I have to transactionally process some group of files aggregating them by their name:
just say I have input files named like this: A1, A2, B1, B2, C1, C2, ...
I need to process them grouped in List like these:
[A1, A2], [B1, B2], [C1, C2]...
I made a common transactional chain with an FTP inboundAdapter, an aggregator and my custom processor and it works perfectly except from one detail.
When the processing of a List fail, only the single File that triggered the exception is handled by the rollback SpEL, while I'd want to handle the exception for the list of files it belongs to.
What I basically (think) I need is something like this:
+--+ +--+ +--+ +--+ +--+ +--+ +------+ +------+ +------+
|A1| |A2| |B1| |B2| |C1| |C2| |aggregator| -> |A1, A2| |B1, B2| |C1, C2|
+--+ +--+ +--+ +--+ +--+ +--+ +------+ +------+ +------+
\-----------------------------/
transaction's scope
If I initiate the transaction only after the aggregator, the SpEL expression would sees only List messages and not single File, right?
What I don't know how to do it, is initiate a transaction without using a Poller in the middle of the chain, that would introduce a delay.
Any hint?
Thank you!
EDIT: I've solved adding an Advice to the processing handle but I'm not 100% sure it's the "correct" solution

as suggested by Gary here's the solution I adopted:
return IntegrationFlows
.from(Ftp.inboundAdapter(ftpSessionFactory)
// my FTP inbound adapter
)
.aggregate(a ->
// my aggregator strategy
)
.handle(myAggregatedMessageProcessor(), c -> c.advice(myProcessorAdvice()))
.get();
the relevant part being c -> c.advice(processErrorAdvice())
and I also configured the Advice like this:
#Bean
Advice myProcessorAdvice() {
ExpressionEvaluatingRequestHandlerAdvice advice = new ExpressionEvaluatingRequestHandlerAdvice();
advice.setSuccessChannelName("successChannel");
advice.setFailureChannelName("failedChannel");
// this catches the Exceptions
advice.setTrapException(true);
return advice;
}

Related

Relational algebra, Find unique Names

Say I have the following relational schema:
+-------+------------+
| Name | Subject |
+--------------------+
| 1 | A |
| 1 | B |
| 1 | C |
| 2 | D |
| 2 | E |
| 3 | F |
| 4 | G |
| 5 | H |
| 5 | I |
+-------+------------+
and I seek these tuples:
3
4
How to get unique names for people only doing 1 subject?
Here's an example based on what AntC described:
NS={
name:string, subject:string
'1' , 'A'
'1' , 'B'
'1' , 'C'
'2' , 'D'
'2' , 'E'
'3' , 'F'
'4' , 'G'
'5' , 'H'
'5' , 'I'
}
(π name NS) - π name (σ s!=subject ((ρ s←subject NS) ⨝ NS))
If you want to test this out you can try it at:
https://dbis-uibk.github.io/relax/calc.htm
Choc Boo, welcome to stackoverflow [relational-algebra]. I strongly suggest you find yourself a textbook or online learning resource that will take you step-by-step -- preferably something you can share with other learners.
Are you sure it's RA you want to learn? Or SQL? They're very different: as a noob, learning one will probably completely confuse you wrt the other. (The connection between them won't become clear until you're much deeper in.)
There are places you can run RA online. But beware they use a specific dialect of RA, which might not be the one you're trying to learn.
Your query could perhaps be expressed as:
"Return all Names that appear in a single tuple."
Note I didn't mention "only doing 1 subject": a relation is a set; if any Name appears more than once it must be that it appears with multiple Subjects.
There's a 'quick and dirty' way to an answer: for each Name count how many tuples; restrict the result to return only those with count == 1. (That way will need using an advanced RA operator for counting.)
There's a longer-winded way that uses only more primitive operators: take two versions of your relation; pair each tuple with each from the other version; the Names that appear in more than one tuple will pair up in multiple ways same-Name-different-Subject; those are the Names you don't want.
What do I mean by "two versions"? Find out about the Rename operation (ρ). What do I mean by "pair ... with ..."? Find out about the Join operation (⋈). What do I mean by "don't want"? Find out about the set difference operator (-): I said relations are sets.

Queues in Ocaml - enq

I can't seem to get my code working enq. How am I supposed to declare the queue datattype as well?
let enq (q, x) =
match q with
queue ([], b) -> queue([], x::b)
| queue (f, []) -> queue(f, [x])
| queue (f, b) -> queue(f, x::b);;
You need to distinguish between the name of the type and the constructor names used to make elements of the type. For your code, the type name could be queue (type names begin with lower case) and you could have just a single constructor named Queue (constructors begin with upper case).
The type declaration would look like this then:
type 'a queue = Queue of 'a list * 'a list
Note that this type is parameterized by the type of the elements of the queue (denoted as 'a in the declaration).
In your code, the pattern matches should be against the constructor Queue, not against the type name queue.
I would also say as a side comment that all the cases in your code look the same to me. I suspect there might be just one case.

DFS algorithm vs Parent-child in tree category

I need to implement a category tree like this:
laguages
.... Web
........ PHP
........ Python
........ HTML
.... System
........ C
........ C++
........ GO
........ RUST
So I have a table like this:
id | title | parent_id
1 | system| 0
2 | c | 1
3 | c++ | 1
4 | rust | 1
5 | web | 0
6 | php | 5
7 | python| 5
8 | html | 5
If i want to implement tree structure I have two solutions.
using a recursive function and create a parent-child array
using
DFS search to make this structure that I need to add left and
right column in the database table.
So my question is: what is deferences between this two solutions? and which one is better?
If the search operations will be more frequent, I would suggest array based approach because of the advantage of random access. In tree based structures, the search operations tend to be O(height) costly and the best height for balanced trees is O(log n).
On the contrary if update operations are more frequent, then I would go for Tree-based approach, since insertion and deletion is not very efficient in arrays due to shifting of elements involved.
Custom Approach
My Approach would have been the adjacency list data structure if, your child arrays Web or System are reasonably small. One could access the list for Web or System in constant time O(1) . Coming to deletion linked lists overcome arrays in case of insertion and deletions, and since the lists are not that long, search wont cause that much of problem time-wise.
I think if the three structure isn't more than what you described (2 levels) and not n levels isn't a right question but one of things you should have in mind.
in php =script language we do it much simple(the case of 2 levels) :
<?php
$source=Array(' system',Array(' php ',' c++ ',' html '),' web ',Array(' c ',' rust ',' python'));
function br(){echo('<br>');}
function printr($ar){
if(is_array($ar)){
foreach($ar as $key){
print(' |____'.$key);
br();
}
}else{
print('['.$ar.']');br();
}
return true;
}
array_map("printr", $source);
?>
the output:
[ system]
|____ php
|____ c++
|____ html
[ web ]
|____ c
|____ rust
|____ python
this array :
$source=Array(
' system',
Array(' php ',' c++ ',' html '),
' web ',
Array(' c ',' rust ',' python')
);
you should obtain it with a "generator" input from a form ,from a internal file created with another generator (web,compiler any kind)...
the maximum results comes when perfect input optimizations is stored in a intelligent structure can avoid use of LOTS CALCULATIONS ,Iterations( DFS algorithm or Parent-child or any OTHER method)
/*
Also using of serialize
functions you can store an entirely array,
unserialize will help you to restore your array
*/
$source=unserialize($from_a_table_1_row_forever);
// $from_a_table_1_row_forever=obtained from some your function to read it from a database,file ...etc
//now is good to use in memory,i mean that multi array
//when you want to store changes in some generator:
$for_a_table_1_row_forever=serialize($source);
good_to_write_it_in_a_single_table_row($for_a_table_1_row_forever);
// (some your function to write in a database,file ...etc)

Production rules for a grammar

Before anything, yes, this is from coursework and I've been at it sporadically while dealing with another project.
A language consists of those strings (of terminals 'a' and 'b') where the number of a = number of b. Trying to find the production rules of the grammar that will define the above language.
More formally, L(G) = {w | Na(w) = Nb(w)}
So i guess it should go something like, L = {ϵ, ab, aabb, abab, abba, bbaa, ... and so on }
Any hints, or even related problems with solution would do which might help me better grasp the present problem.
I think this is it:
S -> empty (1)
S -> aSb (2)
S -> bSa (3)
S -> SS (4)
Edit: I changed the rules. Now here's how to produce bbaaabab
S ->(4) SS ->(4) SSS ->(3) bSaSS ->(3) bbSaaSS -> (1)bbaaSS
->(2) bbaaaSbS ->(2) bbaaaSbaSb ->(1)bbaaabaSb ->(1) bbaaabab
Another hint: Write all your production rules such that they guarantee Na(w) = Nb(w) at every step.

How to let the server inform game clients about other nearby visible players in an efficient way?

I'm working on a multiplayer flash game. The server informs each client what other players are near the player. To do this the server has to check which clients are near each other continuously. The following is what I am using at this moment, as a temporary solution:
private function checkVisibilities()
{
foreach ($this->socketClients as $socketClient1)
{ //loop every socket client
if (($socketClient1->loggedIn()) && ($socketClient1->inWorld()))
{ //if this client is logged in and in the world
foreach ($this->socketClients as $cid2 => $socketClient2)
{ //loop every client for this client to see if they are near
if ($socketClient1 != $socketClient2)
{ //if it is not the same client
if (($socketClient2->loggedIn()) && ($socketClient2->inWorld())
{ //if this client is also logged in and also in the world
if ((abs($socketClient1->getCharX() - $socketClient2->getCharX()) + abs($socketClient1->getCharY() - $socketClient2->getCharY())) < Settings::$visibilities_range)
{ //the clients are near each other
if (!$socketClient1->isVisible($cid2))
{ //not yet visible -> add
$socketClient1->addVisible($cid2);
}
}
else
{ //the clients are not near each other
if ($socketClient1->isVisible($cid2))
{ //still visible -> remove
$socketClient1->removeVisible($cid2);
}
}
}
else
{ //the client is not logged in
if ($socketClient1->isVisible($cid2))
{ //still visible -> remove
$socketClient1->removeVisible($cid2);
}
}
}
}
}
}
It works fine. However, so far I've only been playing with 2 players at a time. This function is looping every client for every client. So, with 100 players that would be 100 * 100 = 10.000 loops every time the function is run. This doesn't seem the best or most efficient way to do it.
Now I wonder what you folks think about my current setup and if you have any suggestions on a better way of handling these visibilities.
Update: I forgot to mention that the world is infinite. It is actually "the universe". There are no maps. Also, it is a two dimensional (2D) game.
Thanks in advance.
The first thing I would say is that your code looks inside-out. Why do you have a high level game logic function that has to do the grunt-work of checking which clients are logged in and in the world? All that networking stuff should be removed from the game logic so that it's done on a higher level and the in-game logic only has to handle the players who are currently playing and in the world. This leaves you with a simple question: are these 2 players near enough to each other? A simple distance check suffices here, as you already have.
The next thing is to reduce the amount of looping you do. Distance is generally a commutative property so you don't need to check the distance between A and B as well as between B and A. To do this, whereas your first loop goes through all the clients, the second loop only needs to iterate over all the clients that come after the first one. This halves the number of iterations you need to do.
You also don't have to to do this continuously, as you state. You just have to do it often enough to ensure that the game runs smoothly. If movement speed is not all that high then you might only have to do this every few seconds for it to be good enough.
If this is still not good enough for you then some sort of spatial hashing system as described by ianh is a good way of reducing the number of queries you do. A grid is easiest but some sort of tree structure (ideally self-balancing) is another option.
The most straightforward solution is to partition the world into a uniform grid, like so:
_|____|____|____|_
| | | |
_|____|____|____|_
| | | |
_|____|____|____|_
| | | |
_|____|____|____|_
| | | |
Then insert your objects into any grid tile that they intersect:
_|____|____|____|_
| # | | |
_|____|____|____|_
| |d d | |
_|____|____|____|_
| | d | d |
_|____|____|____|_
| | | |
Now to do a query for nearby objects, you only need to look at nearby cells. For example, to see who within one tile from the player (#), you only need to check in 9 tiles, not the whole map:
/|////|////|____|_
/|/#//|////| |
/|////|////|____|_
/|////|d/d/| |
/|////|////|____|_
| | d | d |
_|____|____|____|_
| | | |
Depending on your world, however, this technique can be quite wasteful: there could be a lot of empty cells. If this becomes a problem, you may want to implement a more complex spatial index.
Try using a quad tree to represent the players' locations.
The wiki article for this is here.
What it does is keeping the objects you give it in space (users) in a tree which partitions the space (plane) as much as needed.
As for the infinity problem - nothing in programming is really infinite, so define a border which cannot be passed by the users (go even for a very large number for a coordinate, something that will take a user 100 years or so to get to).

Resources