Tuesday, July 31, 2018

Storing Files in Server. File System or Database?

When me and my teammates were building our senior project, we needed to store files in the server. The easiest option for us was to keep the files in server file system and add a link to each file in the database. After graduation, I came across a new concept in database management which is the BLOB. According to MySQL documentation, BLOB stands for Binary Large Object. It is a datatype that is used to store a variable amount of data. Many database administrators use it to store resources files like images or small videos. The reason that I had to learn about the blob is that one of the requirements of developing a web application is to store files in the database rather than using file system.

After a while, one question popped in my mind. Which of the two is better to use? Storing files in server ‘s file system or storing them in the database? As usual, Googling is the best way to find answers for many questions. After searching and reading, I found that many database administrators store files in the database. And some uses file system. This post will go through the two approaches and gives the advantages and the disadvantages of using one of the two.

Storing Files in File System, The Advantages

Speed

If one cares about the speed, file system is the option to go with. The speed of serving multiple files using file system is faster according to many. Also, the speed of creating backups of the files will be much faster than creating a backup of the files while they are stored in the database.

Portability

Using file system to store files will help in portability. This means that if the application will migrate to use different database management system, it will be easy to move the files without any compatibility issues since the database will have only file reference. By storing files in database, the database management system might have an additional layer that wrap the file or alter its structure.

Storing Files in File System, The Disadvantages:

Backup and Recovery

If you are using file system to store files, you will have to create two backups, one for the files in the file system and one for the database. Same thing applies to recovering data. In addition to that, many database management systems come with a utility to create backups automatically. While in file systems, there are no such tool in many cases.

Reference Structure

Usually, files in the server must be accessed using some Kind of a reference if they stored in file system (Usually a URL). If a file or a directory is moved to somewhere else, the structure of the reference will change for sure. In this case, the reference to many files must be updated in the database which might take some time.

Storing Files in Database, The Advantages:

Integrity

One of the biggest advantages is data integrity. This means that any data needed by the application can be accessed from only one place. Also, this means that the information that is related to each other will be in one place. For example, if a company needs to store the pictures of its employees, they will be in the same record of employee information.

Easy to Manage

Since files are in the database, most likely that the management will be performed through the application that will use the files.

Storing Files in Database, The Disadvantages:

Performance

One issue of storing files in database is performance. This issue can be seen when the application needs to fetch many resources from the database. Also, it can be noticed while creating a backup of the database.

No FTP Access

Some prefer to use file system over database storage for this reason. They prefer to mange files using FTP clients instead of using application logic to add, update or remove files.

Configuration

If anyone plan to store very large files in database, sure he might struggle with modifying many settings in the server and the database management system. Myself, I had many issues in building one web application. The files that I was trying to store are video files of size 20 to 30 megabytes in MySQL server using Apache Server with PHP. The first issue was PHP’s upload limit. The second was the global variable max_allowed_packet. I had to update the settings of both till I was able to upload files.

My Opinion

Simply, there is no best option. It depends on your needs. If you thrive for speed and you have very large files to store, go with storing files in file system. For small files (Around 1 to 5 Megabytes), It would be better to go with storing files in a database. There might be other factors that can affect the final decision. So, before deciding, do your homework then select the option that suits your needs.

References:

Most of the information in this page are collected from the following websites. The answers are provided by many people who had different situations in storing files in a server. You can go and read more if you like.

Wednesday, May 23, 2018

The Basics of Prolog Programming Language

Prolog is a logic programming language that is based on facts and rules. The idea is to create a set of facts related to specific domain. After that, we create rules that can be used to answer questions (or queries). For example, we may create a facts that can be used to identify a human if he is a man or a woman. In top of that, we can add more facts to state that this human is a father or a mother of another human. Once we have the given facts, we can create rules. For example, we can define a rule that can be used to find  the father of another human.


In this post, we will learn the basics of Prolog language in the simplest way. For this purpose, we will be using an online editor that can be used to write Prolog code and test it. The tool is called SWISH and can be fount at http://swish.swi-prolog.org/.

When we open the link, we will see that the page has 3 parts, one for creating new file, one for the output and one for asking questions.

The Layout of The Tool
To start writing Prolog code, simply click 'Program' and the code editor will appear.


Prolog Facts

In order to create Prolog rules, first we have to provide some facts. Suppose that we have 5 humans, one has the name 'john', another one has the name 'sara', another one has the name 'ali' and one has the name 'paul' and the last one with the name 'hannah'. The first facts that we are going to write is to state that a human can be a man or a woman. We can till from the names that 'john' is a man and 'sara' is a woman. Writing such facts in Prolog can be done as follows:

man(john).
man(ali).
man(paul).
woman(sara).
woman(hannah).


In Prolog, a fact is written as follows

fact_name(fact_attrs...).

Every fact we write in Prolog must end with a 'dot', and each fact can have one or more parameters (or attributes). Now that we know how to write facts, let's add more facts to our program. Let's say that 'john' is a father of 'ali', 'paul' and 'hannah'. Also let's say that 'sara' is the mother of 'ali', 'paul' and 'hannah'.

man(john).
man(ali).
man(paul).
woman(sara).
woman(hannah).
father(john,ali).
father(john,paul).
father(john,hannah).
mother(sara,ali).
mother(sara,paul).
mother(sara,hannah).


Once we have the given facts, we can ask questions about them and get answers. For example, we can ask the question 'Is john a man?'. Another question we could ask is 'Who is the mother of ali?'. A question in Prolog is called "query". In the next section, we will learn how to ask questions in Prolog.

Prolog Queries

One of the simplest queries in Prolog are the queries that are applied to the facts. The result of such queries will be either "true" or "false" if all the attributes of the query are known. For example, the result of running a query in one of the first 5 facts with one of the 5 names will be either "true" or "false".

In the example that we will see, we want to know if a person is a man or not. Prolog query has the following basic form: "?- rule_or_fact(vars...)". It is possible to construct more complex queries but we will do it later. For now, let's focus at the basics. The "?-" part is provided by the editor by default. All what we have to do is to write the other part.

In SWISH, we write the query in the place where it says " Your query goes here ...". After writing the query, All what we have to do is to click "run" and the result will appear. Let's give it a try.



As we can see, "ali" is a man since we got "true". Now we want to try something else. Suppose that we would like to get All women. To do that, we have to learn about new concept in Prolog which is variables.

Prolog Variables

A variable in Prolog can be used to answer queries. Variables are passed as an arguments to rules and facts to get specific values. Any parameter that starts with a capital letter is considered as a variable. For example, if we say 'woman(Sara)', 'Sara' is now a variable not an attribute. The  final value of the variable is based on the provided facts and rules. Now, to get all women, we pass a variable to the fact 'woman()'. Prolog will set the value of that variable to the name of the woman.

 As we can see, we have got our first woman. To get the rest, all what we have to do is to click "Next" till the execution is finished.

Now let's try something new. Suppose that we would like to know who is the father of 'ali'. To ask such as query, we write it as 'father(X, ali)'. In this case, 'X' will be equal to the father of 'ali'.


Suppose that we have a person that has unknown father. What will be the value of 'X' in this case? Let's try it. To do that, we will add new man called 'ibrahim' and try to find his father.

Simply we got 'false'. Whenever we try to apply a rule that does not exist, we will get 'false'. Also if we try the rule with a name that does not exist, we will get false. For example, if we try 'father(X, dania)', this will also give false.

Monday, January 22, 2018

New Music Instrumental Uploaded

One of the hings that I enjoy doing in my free time is to compose music using FL Studio 🎶. It is very powerful tool that can be used to create any type of music.

In the last few years (Since I joined KFUPM), I was learning how to use it since music composition is one of my hobbies. The next musical instrumental was composed by me in 2014. It is not that perfect but the final result was good enough for me.

Enjoy listening 😁. use your headsets for better result.

Every one month or two, I will try to upload one instrumental from the ones that I have completed since I started learning FL Studio till today. By the way, I'm still learning 🙂.

Also feel free to listen to the ones that has been already uploaded. I will be more than happy if you give me your feedback on them.


Saturday, November 25, 2017

جامعة الملك فهد و شركة موقف

السلام عليكم و رحمة الله و بركاته. خلال هذا الإسبوع, تم سحب سيارتي مِن قبل شركة موقف بجامعة الملك فهد للبترول و المعادن. طبعا سبب السحب كان غير مقنع بالنسبة لي الا وهو "عدم عرض تصريح صالح". بعد نقاش طويل مع إثنين من موظفي شركة موقف, إضطررت لأن ادفع قيمة المخالفة بحرقة شديدة. سبب كتابتي لهذا الموضوع هو لكي أُخرج ما في نفسي بما ان الكتابة واحدة من الأشياء اللتي احبها و أستخدمها لتفريغ ما في نفسي. و ايضا لِكي تَعُم الفائدة حتى لا يسقط طالب آخر في نفس موقفي. حتى أدفع قيمة المُخالفة, إضطررت لِأخذ سِلفة من الجامعة بسبب مروري بضائقة مالية في الوقت الحالي و عدم وجود مكافئة جامعية.

في هذا المقال, سوف اذكر جميع النقاط اللتي تحدثت عنها مع الموظفين اللذين كانوا هناك.

عند وصولي لمكان الحجز, سألت اولا عن الشخص المسؤول. عندما قال لي انه غير موجود, ذهب و نادى شخص كان موجود بالخارج. عند وصوله, اخبرته بأنه حسب التعاميم اللتي أُرسِلت مِن قِبل الجامعة لا يحق لكم سحب سيارتي. قبل ذهابي لهم, قُمت بِطباعة التعاميم اللتي اتحدث عنهُما. "الوقوف في المدينة الجامعية" و "نظام الوقوف في الجامعة" .

حسب ما كُتِب بالتعاميم, يحق لِشركة موقف سحب السيارات من ناحية التصريح في أحد الحالات التالية:

  • الوقوف في مواقف ذوي الإحتياجات الخاصة دون تصريح.
  • عدم حمل تصريح.
  • حمل تصريح مُنهي الصلاحية.

من الأحوال الثلاثة المذكورة, لا تنطبق اي واحدة منها على سيارتي. التصريح موجود بالسيارة و من الممكن قراءة جميع معلومات التصريح بدون اي مشاكل (تاريخ الإنتهاء و رقم التصريح). للمعلومية, التصريح هو الملصق الأخضر اللذي يوضع على سيارات الطلاب الساكنين بالحرم الجامعي. الصورة التالية تُبين وضع التصريح بعد دخول السيارة الحجز.

عِند مشاهدة التصريح مِن الجانب, تستطيع مشاهدة رقم التصريح. سبب عدم إلتصاق التصريح هو حصول حادث للسيارة قبل فترة و على إثره تم تغير الزجاج الأمامي.

بعد إخبارهم بأنني لم ارتكب اي مخالفة, جلس الموظفون يفكرون. و قام أحدهم و ذهب إلى سيارتي لتفحص التصريح. بعد عودته قال ان تصريحك يعتبر مُنتهي بسبب عدم إلتصاقه بزجاج السيارة. سألته هل يوجد بند بالجامعة يقول انه عِند عدم إلتصاق التصريح يعتبر مُنتهي؟ فكانت إجابته بأن هذا الشيء متعارف عليه بالجامعة ولا يحتاج ذكره. فقلت له يوجد لدى الجامعة شيء إسمه "دليل الطالب". هذا الدليل يتم فيه ذكر كُل ما يحتاج معرفته الطالب حول الجامعة مِن نظام القبول إلى نظام الأمن و غيرها. طبعا انا قرأت الدليل و عارف تقريباً جميع الأتظمة. حسب البند الموجود تحت قِسم "إدارة الأمن" تحت "العقوبات المترتبة على تكرار المخالفات المرورية" في صفحة رقم 52, مذكور انه يتم سحب السيارة في حالة عدم حمل التصريح فقط.

هذه الصورة مأخوذة مِن أحدث دليل للطالب و هو موجود على الرابط التالي: دليل الطالب.

بعد النقاش مع الموظفين, قالوا بأنني على حق فيما قلته لانه مُودون في دليل الطالب و التعميم المُرسلة. لكن حسب كلامهم, لا يُمكنهم إخراج السيارة إلا بِدفع قيمة المُخالفة. الشيء الجميل انهم اثنوا على طريقتي في إثبات بأنني على حق.

طبعا بعد كُل هذه الأدلة على براءتي اتهمت بأني خالفت النظام و على هذا تم سحب سيارتي. كما ترون, حتى إذا كنت على حق, فلا تستطيع أخذ حقك. المشكلة الوحيدة اللتي كانت سبب سحب سيارتي هي عدم إلتصاق التصريح بالزجاج علماً بأنه لا يوجد اي بند يقول انه تسحب السيارة بهذه الحالة. المُخالفة الوحيدة اللتي يستطيعون إتهامي بها هي نزع التصريح و وضعه على سيارة أُخرى. طبعا لم أقُم بِتصعيد الأمر إلى إدارة امن الجامعة حتى لا يتِم إتهامي بِهذه المخالفة في سجلي لأنني لم أقُم بِوضع التصريح على سيارة أُخرى. كما ترون, جميع الأدلة اللتي قُمت بِجمعها هي من قِبل الجامعة نفسها بالنهاية, اترككم مع صورة المخالفة اللتي لم أرتكبها.