vendredi 8 avril 2022

In Python blacklist warnings that have the path with the pattern rtaos/**/src/gen/** - Here ** can be any text present before src and after gen

The pattern rtaos//src/gen/ is stored in a text file. When I print the compiler warnings, if the warnings path belong to this pattern, i.e anything can be present between rtaos and src and anything can be present after gen. That warning should not be printed. How to do this with regex in python or is there any other way.

how to push data into different queues based on the data type in cpp?

I have problems to push data into different queues based on the data type.

To be specific, I have multiple request classes. For example:

class RequestA {
};
class RequestB {
};
class RequestC {
};
...

These requests might be inherited by certain class (e.g., class Request).

Each object of these data types must be put into different queues. For example:

std::vector<RequestA> queueA;
std::vector<RequestB> queueB;
std::vector<RequestC> queueC;
...

The reason why I need different queues for each class is that each request at the top (front) of the queue requires different (post-)processing steps later.

For instance,

class RequestHandler {
public:
  void tick() {
     if (!queueA.empty()) {
        processA(queueA.front())
        queueA.pop_front(); // assume std::vector has pop_front()
     }
     if (!queueB.empty()) {
       ...
     }
     ...
   }
private:
 std::vector<RequestA> queueA;
 std::vector<RequestB> queueB;
 std::vector<RequestC> queueC;
...
};

However, the problem is that ,to push data into the queue, I might use followings:

if (typeid()) {
   queueA.push_back();
} else if (typeid()) {
   queueB.push_back();
} ...

or if RequestA, RequestB, ... are inherited by class Request:

std::map<std::type_index, std::vector<Request*>> queue;

queue[typeid()].push_back(...);

might be used.

However, it might be better to avoid using typeid or dynamic_cast.

How to efficiently handle this scenario?

Database design: coherence issue when managing timestamped data

I have a SQL database with two tables.

A sessions table containing:

  • session id: the primary key
  • login timestamp: the unix timestamp of the start of the session
  • logout timestamp: the unix timestamp of the end of the session
  • user id of the logged user

An events table containing:

  • event id: the primary key
  • event timestamp: the unix timestamp of the event
  • action id: the type of event occurred

An event may occurs during a user's session, or it may occur while no one is logged in. In my scenario only one user can be logged in at a time (think about a kiosk-like machine; sure, I am omitting project details to better clarify the essence of the problem).

Sample data:

events table: | event_id | event_timestamp | action_id | | - | - | - | | 1 | 1649400005 | 421 | | 2 | 1649400010 | 112 | | 3 | 1649402000 | 331 | | 4 | 1649402010 | 112 | | 5 | 1649405005 | 421 |

sessions table: | session_id | login_timestamp | logout_timestamp | user_id | | - | - | - | - | | 1 | 1649400000 | 1649400020 | 11 | | 2 | 1649405000 | 1649405030 | 22 |

What I need:

I need to get the list of events occurred during each user session like so:

user_id session_id event_id event_timestamp action_id
11 1 1 1649400005 421
11 1 2 1649400010 112
22 4 5 1649405000 421

What I've tried:

I've tried to add a nullable FK column session_id on the events table to rely on FK capability of the database to retrieve the data in an optimized way. In this case, if an event occurs without a user being logged in the FK key will be NULL, otherwise it will contain the session id.

But as I stated in the title I see a potential coherence issue with this solution. It is infact too easy to enter an invalid data by mistake linking an event to a session having a time window (login-logout) that does not include the event time itself.

My current solution:

In order to avoid potential errors, I'm thinking of not using any FK and using join queries based on timestamp values. In this way a session is linked to an event if the event timestamp is between the login and logout timestamps of a session.

What I'm asking for:

The question is about good design practices and tips about my current design.

I'd like to know if the choice of not using an FK makes sense for this scenario or violates the principles of good database design.

I'm worried about performance problems in data retrieval since the conceptual relation between sessions and events don't rely on FKs but on a "weak" conceptual relationship not explicitly modeled by my design.

side note: This is a very simple example and performance issues are not very likely. But the important bit is the concept. If a better design exists for this situation I'd like to learn it now and use the right design also for this simple scenario in order to avoid potential problems in more complex projects.

exercises with practical problem statements to solve with design patterns

I've been trying to get better at recognizing and implementing design patterns recently, and although I've had no problem finding theoretical information I'm trying to find actual exercises that can be solved using design patterns. Does anyone have good resources that provide just that?

jeudi 7 avril 2022

which design pattern to use, so client can define their own version of that type

I have a interface of IType which has solve() method in it. Their are two concrete class concreteA, concreteB, which implement solve() method. Their is also a TypeFactory which takes care of creating object for concreateA and concreteB respectively. This object creation part is exposed using Facade design pattern to client.

My question is how I can expose functionlity in facade class to let client define their own defination of IType to support creation of UserDefinedType?

I think adapter or decorator pattern can be used, but I am not sure how to expose it through Facade class.

How to use iterator pattern in java to load file into hashmap in batches

I have a large file containing two million lines . I'm looking to traverse through each line of the file and, process it into a key value pair and store it into a hashmap to make comparisons later on. However, I do not want to have a hashmap with 2 million key value pairs in the interest of space complexity. Instead , I would like to iterate through N lines of the files and load their key value pairs in the hashmap , make comparisons and then load the next N lines into the hashmap and so on.

An example of the use case :

File.txt:

1 Jack London
2 Mary Boston
3 Jay  Chicago
4 Mia  Amsterdam
5 Leah New York
6 Bob  Denver
.
.
.

Assuming N=3 as the size of my hashmap, at the first iteration my hashmap would store key value pairs for the first three lines of the file i.e

1 Jack London
2 Mary Boston
3 Jay  Chicago

After making comparisons on these key value pairs , the next 3 lines are loaded into the hashmap as key value pairs:

4 Mia  Amsterdam
5 Leah New York
6 Bob  Denver

and so on until all the lines in the file have been iterated over. How do I implement this using the iterator design pattern in java?

Database table relationships based on timestamps

I have a SQL database with two tables.

A sessions table containing:

  • session id: the primary key
  • login timestamp: the unix timestamp of the start of the session
  • logout timestamp: the unix timestamp of the end of the session
  • user id of the logged user

An events table containing:

  • event id: the primary key
  • event timestamp: the unix timestamp of the event
  • action id: the type of event occurred

An event may occurs during a user's session, or it may occur while no one is logged in. In the described scenario only one user can be logged at a time.

I need to get the list of events occurred during each user session so I need a way to link each event to a session, whether the event occurred during a session.

As a first solution I thought of including a nullable foreign key in the events table referring to a session. In this case, if an event occurs without a user being logged in the FK key will be NULL, otherwise it will contain the session id.

But with this solution there is a data consistency problem: it would be possible to create an event linked to a session and give it a timestamp that is not between the start and end timestamps of the associated session. This is not desired since the event may relate to a session having a time window that does not include the event time itself.

A second solution would be not to use the FK described and to retrieve the data by means of a query that takes into account the start and end timestamps of each session and returns for each event the id of the session to which the event belongs.

The final question is whether the choice of not using an FK makes sense for this scenario or violates the principles of good database design, and if such a technique can lead to performance problems in data retrieval since the conceptual relation between sessions and events don't rely on FKs.