jeudi 3 novembre 2016

Given a set of string patterns, find which ones match a particular string

I have an array of known patterns with optional wildcards. For example this 5 elements array:

ab*e
hkl
abcd*
ab
sdfs?f

Now I need to find the elements within the array that match an arbitrary string.

This can be solved with regular expression by returning all the matched pattern in the array, but it is too slow when my patterns array has thousands of patterns, and I need to perform this check very fast for a large number of arbitrary strings.

I'm looking for some sort of hashing to the patterns array (could be slow as it only happens once), which makes the search of matched patterns to a gives string significantly faster.

In the example above if I'm looking for all matched patterns for abcde I would get

ab*d
abcd*

Design issue regarding serializing , deserializing

I have many classes , say,

class A
{
/*member variables*/
};

class B .. Class Z

I am copying objects of these classes into a buffer (char*). Now I have a reader object that continuously polls this buffer and deserializes the bytes into objects and prints them. My question is how should the deserialzer class be designed.

The approach I have is to embed the type of the object as a header and then send it through the buffer. Upon reading the buffer, I know the type and then I can deserialize the appropriate object. The problem with the deserializing part is now it should be aware of all the message types and call the appropriate function based on the type. I am planning to have this deserialization as a separate entity that is not aware of the message types or the code to deserialze it should not be a part of it.

I am looking for solutions like interfaces, polymorphism etc. to tackle this problem. Is there a design pattern for this kind of a problem ?

JMS transactional message processing pattern with async listener

I will shorty describe our system infrastructure and messages flow:

  1. There are multiple producers that enqueue messages to QUEUE

  2. There is one asynchronous listener (AUTO_ACKNOWLEDGE) that collects messages and puts them on a list.

Simplified code:

private List<Message> messages;
public void onMessage(Message message) {
      messages.add(messages)
}

  1. There is one poller that wakes up every interval, grabs all the messages so far dequeued and processes them.

Simplified code (from the listener):

   public List<Message> retrieveAndClearMessages() {
      synchronized (messages) {
         List<Message> returnList = new ArrayList<Message>(messages);
         messages.clear();
         return returnList;
      }
   }

The problem is that for the duration of the poller are the memory - which means if encounter an outage all the currently processes messages are lost. Are there any enterprise pattern that solves such problems? Maybe some backup queue or other workaround.

mercredi 2 novembre 2016

Design pattern and OOP in PHP?

I've been working with PHP and few more OOP languages and still wondering about the meaning of design pattern and OOP in PHP and similar languages.

I have some modules in my project: news , forum... Each module can be access by calling it main class.

  • The main class routes the page ~> point to news/article , news/categories.
  • Have some module settings / variables.

And the child classes in each module handle those specific features / pages.

What's the best design pattern for this model? Create instance of the class or it's child?

Now, i initialize a class, and access to the child class static methods like the example bellow. But i've read using static method can slow down the code?

class news {
  __construct() {
     // define module settings
     ...
     // routing all module pages
     categories::list();
  }
  function modulesetting() {}
}
class categories extends news {
  static function list() {}
}

Calling class method to keep controller slim

Normally, I inject class in the controller and so the methods can access to $team object.

class TeamMemberController extends Controller {

    public $team;

    public function __construct(TeamRepository $team) {
        $this->team = $team;
    }

    public function update(Request $request, $id) 
    {
        $this->team->updateAddress($request->all(), $id);
    }

I was thinking alternative pattern to create a execute() method to keep controller slim and I wouldnt need a __construct anymore. For example:

class TeamMemberController extends Controller {

    public function update(Request $request, $id) 
    {
        // execute updateAddress() method 
        execute(TeamRepository::class)->method('updateAddress',[$request->all(), $id])

        // execute handle() method (default)
        execute(TeamRepository::class, [$request->all(), $id]); 

    }
}

Inside the execute method would use call_user_func_array.

Do you see this is good implementation or is it bad pattern?

Duplicate master data set into multiple clones with special inheritance rules

The problem starts with having a master set of data on which CRUD operations are applied and sent to clients.

  1. From the master data set, a client might want the entire data set.
  2. From the master data set, a client might want only X data piece.[X being an identifier].
  3. From the master data set, a client might not want X data piece and want Y data piece.

Extra info: A 'not want' operation might be turned to 'want all except' operation, but not desirable. If 'i want x' represents a small piece of data, this will lead to a lot of rules of type 'i want y1, y2, y3' etc. On the master feed, we apply CRUD operations, so each 'create' on master, will mean a create for every N clients.

Until now, i thought at:

  1. Creating a domain which contains some rules. Client inherits X, Client does not inherits Y, Client inherits X but not Y, etc. This is doable in our system, but not recommended as we are dealing with some indexes based on ids of type long, which cannot be modified into a special key without damaging clients. Based on this rules, when we deal with CRUD operations, we check those rules and take a decision if we need to pass the info to the client or not.

  2. Having Entity in the master data set, for the sake of example ignore the naming, create a new Entity2(which has reference to the master Entity) and has less properties that Entity(only those modified), thus, saving memory but increasing the no of entities in our system. I think this is kind of similar with Liskov principle, same type, but can be replaced with a subtype- wild reference, but kind of close to the idea.

Having these said, have you ever encountered such an example? Do you have any advice?

Thanks,

Handling OAuth 2 Refresh Tokens

One of the applications I am developing is going to need to access a service on behalf of the the client authenticated with OAuth 2.0.

OAuth 2.0 gives you both a access and refresh tokens for your client (resource owner). You then will make requests and at some point the token will expire and will need to be refreshed (possibly half way through multiple requests to the service).

I wanted to get some thoughts on how other people have handled this as it's new to me and there may be a much simpler way of dealing with OAuth's refresh tokens.

This is the way I'm currently thinking about approaching it. This basically sudo code at this point to get the idea across - it most certainly wont run :). The idea is to write a wrapper and decorate all oauth authenticated calls that, on failure, will try to refresh the token and retry.

from functools import wraps

def oauth_expirable(f):
    @wraps(f)
    def wrapper(*args, refresh_token, **kwds):
        try:
            return f(*args, refresh_token=refresh_token, **kwds)
        except HTTPError as http_error:
            if http_error.code == 403:
                http.post('http://ift.tt/2eofHj8', body={'refresh_token': refresh_token})
                return f(*args, refresh_token=refresh_token, **kwds)
            else:
                raise
    return wrapper

@oauth_expirable
def get_resource(uid, *, auth_token, refresh_token):
    return http.get(
        url='http://ift.tt/2fio6C0{}'.format(uid), 
        headers={'Authentication': 'Bearer: {}'.format(auth_token)})

Any thoughts on this solution or on alternative solutions.

Thanks