Arrived so we're going to take a look at the implementation of the flyway design pattern by first of
all discussing the example of repeating user names.
So let's imagine that you have a system which is a multi-user system and let's imagine that you have
let's say a multi massively online player game with lots and lots of users and they have similar names.
So the first attempt at defining a user might be something like the following You simply put their full
name as a string.
And what you'll end up having is you'll end up having lots of users with similar names so you'll have
to use a code John Doe.
And then you'll have a user called let's say John Smith.
So here the first name is Rupi which means that you're effectively wasting however many bytes are actually
being repeated and you don't really want to waste that much memory because you're going to have lots
of people called Xion and lots of people with the last name Smith.
So how about simply keeping them in a single dictionary and then having the indices into that dictionary.
So the first thing you would do is you would split the first name and last name into separate fields.
But even if you do this and even if you decide to kind of split things you should keep in mind that
C++ does not string interning.
So you're going to be allocating the string Jaun twice not once and as a result you're going to be kind
of stuck with wasted data.
In this case you're going to be wasting a whole five bytes as you kind of construct the string John
twice.
So instead of keeping names as strings of what you should keep them as is indices into some sort of
database.
And here we're going to be using a map to store the indices.
First of all I'm going to define the key into our database.
So I'm going to use you and 32 on this course he asks the key.
Now it might seem a bit excessive because you're never going to have four billion different first names
and last names so you're welcome to optimize this.
And obviously if you use a few in 16 for example that kind of half the amount of memory you need for
storage of uses first and last names not their actual last names but the keys into the map that we're
going to construct.
That's exactly what we're going to do now.
By the way so essentially what I'm going to do is I'll make a protected section and here we're going
to have two things so first of all we'll have a map of keys to actual strings and I'll have it as static
by map from key to a string.
Let's call it names and then we're going to have a static seed that's going to turn and basically the
index that we're going to be using for every single one of the consecutive string.
So instead of generating some sort of hash code for each string I'm just going to have a linear kind
of count.
So let's initialize these static members.
So let's initialize the key and by the way the seed should be of type key.
So the user seed is going to start with 0 and symbole will have the binary map and then explain what
the biomass is.
And a second from key to string user calling call names.
There we go.
So this is the initialization of the static members.
Now what exactly is it by a map.
Why are we using it well by map is kind of similar to the SDL map that you're all used to except it
allows us to search on both sides so you can find the key from the value as well as the value from the
keys so you can feed the map a key and it's going to give you the corresponding string or you can feed
that string and is going to give you the corresponding key which is what we're going to be needing in
this particular approach because what we're essentially doing is we're building a kind of caching mechanism
for strings.
Now the question is how do you add a string to these by a map.
So let's have a function.
Once again it's going to be a static function which returns a key of the output string and it's going
to be called add.
So we're going to be taking a string ass and we're going to be first of all checking whether the string
is already in the map because it's already in the map then there is nothing for us to do really.
So I'm going to take names dot right dot find.
So essentially right now what I'm using the right side for is I'm searching the values of the map I'm
searching the right hand side so I'm looking for this particular string.
And if I do find it then I will give the key.
But if I don't find it then I have to insert the whole thing.
So if I don't find it then Id is going to be quoting names right.
And and in this particular case what I need to do is I need to actually insert this particular value
together with its key so I'm going to get the key by just incrementing the sieb that I have.
And then I'm going to say names though the inserts and I'm going to insert a pair of seed and the string
that we're inserting into the by map and then I'm be returning the idea here.
So this is what happens if you don't find a string in there if you do find it then you just return the
key which is I-T second.
That's how you actually get the the key part of the iterator.
So there you have it.
This is how you can insert a string unless it's already there in which case you just get the key.
And in any case you get the key to the valid string.
So another question is Well how do we actually construct our user because our user still has to take
the first name and last name.
But now the initialization is going to be slightly different because we don't store the strings themselves.
Instead what we do is we store the first and last names as keys.
So if we go up here we're going to have a key called first name and a key called last name.
There we go.
So when I went to initialize first name I went to initialize it by calling the add function with the
string first name that's been provided into the function.
And I'm going to do the same last name as well.
So I'll call add with lost me.
There we go.
So this is how you initialize the user and then of course the question is Well how do you get the first
name or indeed the last name as a string.
Because while that is a particular challenge and in actual fact you need to have getters for both of
these.
So if for example you won the first name you would have a concert string reference called Get first
name.
I'm assuming these are immutable.
For now will you can fix this later on but I would just return everything else Konst.
So we have a good first name.
Let me just fix this here quickly get first name and here you search for the name in the set of keys
so here you say names dot left dot find you look for the string corresponding to the last name key because
remember we're just storing the keys we're not storing the values themselves.
And then second gives us the actual string and you can sort of do the same thing to get the last name.
So let me just paste this in here so forget last name.
It's a very similar situation you get last name and here you have.
Well here it should be first name actually name and here it's going to be last name.
So this is how you can find the appropriate parts of whatever name whatever user name you actually want.
So let's take a look at how all of this can be implemented.
For example let's implement the stream map of the operator.
Here we get two prints.
The first and last name of the user.
So let's go into a stream output operator and now obviously we cannot just print the integers that's
not particularly exciting but what we can do is we can print to first name by just getting it.
So here we would say gets on the score first name.
And this is obviously a function call and the same goes for the last name.
So get on the school last name as a function call as well.
We can now start using all of this API.
So now I can create two uses user user one let's call them John Smith and then I'm going to have use
it to call Jane Smith and we can actually print them both out so user of one use it too.
All right let's see what we get here.
Okay so we get the first name as John last name Smith and the same goes for Jane Smith.
Now the only question is Well what about actually verifying that the the last names actually refer to
the same value.
So how would we do that.
Well we can actually stick them somewhere in the stream the output the operator the actual values as
opposed to the string representation.
So let's see what we can do here.
So I'm going to basically change the map with operator.
So here I will put some round brackets and here I'm just going to output the index of the last name
and hopefully we get the identical last name for both of the participants.
That should be that should have a use a prefix and from a user dot like this.
So let's wait for this to fail and we can run this again and hopefully this summer and we get to see
the actual results.
All right so as you can see we're getting an identical index for the last name Smith because it was
only added to the map once and it only generated the key once and that key has the value 2.
So this is how you implement a very simple kind of handmade flyway design pattern.