Saturday, March 31, 2012

bash - alias, function and script

In Bash, when to alias, when to script, and when to write a function is as trivial as important it is. Here is a look at the underlying implementation, the "why" behind the "what".

aliases and functions
  • are stored in memory (once they are read).
  • When invoked, they are read from memory and executed by the current shell.
Note that aliases and functions are grouped together for their similarities despite being functionally different.

scripts
  • are stored in files. If the script is located in a $PATH directory, many shells store a hash of its pathname to save time on future $PATH lookups.
  • When invoked, they are read from files (i.e. file I/O takes place), and executed by a subshell (i.e. a fork() will occur), unless sourced directly.
What does this mean? Ideal candidates for functions and aliases are commands that are very frequently used and very short, i.e. the type of things that you want to keep in memory and execute in the current shell for speed (although most OS's will cache commonly used files in memory). Ideal candidates for scripts are very long sequences of commands that are very rarely used, i.e. the type of things you don't want or need to permanently take up memory. A good example is initialization command sequences that only run once. 

This could be important for performance -- function is faster, since it doesn't fork() and exec(). In normal circumstances, the difference is trivial, but if you are debugging a system that is out of memory and is page-thrashing, it could make a big difference.

Also, if you want to modify your current shell environment, you should use a function. For example, a function could change the command lookup $PATH for the current shell, but a script cannot, because it operates on a fork/exec copy of $PATH.

With that said, these considerations probably will not make much of a difference on most modern systems with average memory space and disk speeds. However, if you are ever developing scripts for systems with more extreme requirements (like tiny amounts of RAM or uncommon permanent storage media types), it may be useful to keep them in mind. Also, the difference between scripts and functions might be noticeable if you execute them a very large number of times in a row, like iterating over a long file list or a bunch of lines in very long file. Chances are, the function / alias will run faster because it does not need to fork a process or read a disk file, although again, OS-level caching and other optimizations may reduce the differences somewhat.

PS: According to Bash Reference Manual
"For almost every purpose, shell functions are preferred over aliases."

Thursday, March 8, 2012

OLTP vs. OLAP

We can divide IT systems into transactional (OLTP) and analytical (OLAP). In general we can assume that OLTP systems provide source data to data warehouses, whereas OLAP systems help to analyze it. 

OLTP (On-line Transaction Processing) is characterized by a large number of short on-line transactions (INSERT, UPDATE, DELETE). The main emphasis for OLTP systems is put on very fast query processing, maintaining data integrity in multi-access environments and an effectiveness measured by number of transactions per second. In OLTP database there is detailed and current data, and schema used to store transactional databases is the entity model (usually 3NF).

OLAP (On-line Analytical Processing) is characterized by relatively low volume of transactions. Queries are often very complex and involve aggregations. For OLAP systems a response time is an effectiveness measure. OLAP applications are widely used by Data Mining techniques. In OLAP database there is aggregated, historical data, stored in multi-dimensional schemas (usually star schema).

The following table summarizes the major differences between OLTP and OLAP system design.



OLTP System
Online Transaction Processing
(Operational System)

OLAP System
Online Analytical Processing
(Data Warehouse)

Source of data
Operational data; OLTPs are the original source of the data.
Consolidation data; OLAP data comes from the various OLTP Databases
Purpose of data
To control and run fundamental business tasks
To help with planning, problem solving, and decision support
What the data
Reveals a snapshot of ongoing business processes
Multi-dimensional views of various kinds of business activities
Inserts and Updates
Short and fast inserts and updates initiated by end users
Periodic long-running batch jobs refresh the data
Queries
Relatively standardized and simple queries Returning relatively few records
Often complex queries involving aggregations
Processing Speed
Typically very fast
Depends on the amount of data involved; batch data refreshes and complex queries may take many hours; query speed can be improved by creating indexes
Space Requirements
Can be relatively small if historical data is archived
Larger due to the existence of aggregation structures and history data; requires more indexes than OLTP
Database Design
Highly normalized with many tables
Typically de-normalized with fewer tables; use of star and/or snowflake schemas
Backup and Recovery
Backup religiously; operational data is critical to run the business, data loss is likely to entail significant monetary loss and legal liability
Instead of regular backups, some environments may consider simply reloading the OLTP data as a recovery method



Comparison of Bill Inmon and Ralph Kimball paradigm:

In the data warehousing field, we often hear about discussions on where a person / organization's philosophy falls into Bill Inmon's camp or into Ralph Kimball's camp. We describe below the difference between the two.

Bill Inmon's paradigm: Data warehouse is one part of the overall business intelligence system. An enterprise has one data warehouse, and data marts source their information from the data warehouse. In the data warehouse, information is stored in 3rd normal form.

Bill Inmon: Endorses a Top-Down design
Independent data marts cannot comprise an effective EDW. Organizations must focus on building EDW.
"You can catch all the minnows in the ocean and stack them together and they still do not make a whale,"
Bill Inmon, January 8, 1998.

Ralph Kimball's paradigm: Data warehouse is the conglomerate of all data marts within the enterprise . Information is always stored in the dimensional model.
Kimball model also proposes the data warehouse bus architecture. This architecture is comprised of:
  • A staging area (which can have an E/R or relationally designed 3NF design or flat file format), which cannot be accessed by an end-user of the data warehouse bus (Presentation Layer) .
  • The Data Warehouse Bus itself which includes several atomic data marts, several aggregated data marts and a personal data mart but no single or centralized data warehouse component.
The Data Warehouse Bus:
  • Is dimensional;
  • Contains transaction and summary data;
  • Includes data marts, which have single subject or fact tables; and
  • Can consist of multiple data marts in a single data base.
There is no right or wrong between these two ideas, as they represent different data warehousing philosophies. In reality, the data warehouse in most enterprises are closer to Ralph Kimball's idea. This is because most data warehouses started out as a departmental effort, and hence they originated as a data mart. Only when more data marts are built later do they evolve into a data warehouse.

Ralph Kimball: Endorses a Bottom-Up design
EDW effectively grows up around many of the several independent data marts – such as for sales, inventory, or marketing
"...The data warehouse is nothing more than the union of all the data marts...,"
Ralph Kimball, December 29, 1997.

 
The main difference is Top-Down vs. Bottom-Up Approach.

Advantages of Top-Down Approach: [Bill Inmon Approach]:
  • A truly corporate effort, an enterprise view of data
  • Inherently architect ed- not a union of disparate DataMarts
  • Central rules and control
  • May be developed fast using iterative approach
Disadvantages of Top-Down [Bill Inmon Approach]:
  • Takes longer to build even with iterative method
  • High exposure/risk to failure
  • Needs high level of cross functional skills
  • High outlay without proof of concept
  • Difficult to sell this approach to senior management and sponsors
Advantages of Bottom-Up Approach [Ralph Kimball Approach]:
  • Faster and easier implementation of manageable pieces
  • Favorable ROI and proof of concept
  • Less risk of failure
  • Inherently incremental; can schedule important DataMarts first
  • Allows project team to learn and grow
Disadvantages of Bottom-Up Approach [Ralph Kimball Approach]:
  • Each DataMart has its own narrow view of data
  • Permeates redundant data in every DataMart
  • Difficult to integrate if the overall requirements are not considered in the beginning
Kimball encourages to have perspective of both “vertically” and “horizontally”
while gathering business requirements while developing/designing data marts using Bus Architecture to integrate in to Enterprise Data warehouse.

Vertical
  • Don’t just rely on the business data analyst to determine requirements
  • Inputs from senior managers about their vision, objectives, and challenges are critical
  • Ignoring this vertical span might cause failure in understanding the organization’s direction and likely future trends
Horizontal
  • Look horizontally across the departments before designing the Data Warehouse
  • Critical in establishing the enterprise view i.e Bus Architecture
  • Challenging to do if one particular department if funding the project. [Even one departments funds this project , but all the departments together become the company]
  • Ignoring horizontal span will create isolated, department-centric databases that are inconsistent and can’t be integrated while considering for Enterprise Data Warehouse
  • Complete coverage in a large organization is difficult , but CEO or CTO represents all of the departments. They should have the proper understanding of the full business and recommend what is good for whole company not just one department.
  • One rep. from each dept. interacting with the core development team can be of immense help
  • Creating a Business Decision Making group when there is a difference in the perspective from each department would help in resolve these kind of conflicts.
New Practical approach by Kimball
  • Plan and define requirements at the overall corporate level
  • Create a surrounding architecture for a complete warehouse
  • Conform and standardize the data content
  • Implement the Data Warehouse as a series of Supermarts, one at a time

SUPERMARTS
  • Totally monolithic approach vs. totally stovepipe approach
  • A step-by-step approach for building an Enterprise Data Warehouse from granular data
  • A Supermart s a data mart that has been carefully built with a disciplined architectural framework
  • A Supermart is naturally a complete subset of the Data Warehouse.
  • A Supermart is based on the most granular data that can possible be collected and stored
  • Conformed dimensions and standardized fact definitions

OLTP and Data Warehousing Environments

One major difference between the types of system is that data warehouses are not usually in third normal form (3NF), a type of data normalization common in OLTP environments.
Data warehouses and OLTP systems have very different requirements. Here are some examples of differences between typical data warehouses and OLTP systems:
  • Workload
    • Data warehouses are designed to accommodate ad hoc queries. You might not know the workload of your data warehouse in advance, so a data warehouse should be optimized to perform well for a wide variety of possible query operations.
    • OLTP systems support only predefined operations. Your applications might be specifically tuned or designed to support only these operations.
  • Data modifications
    • A data warehouse is updated on a regular basis by the ETL process (run nightly or weekly) using bulk data modification techniques. The end users of a data warehouse do not directly update the data warehouse.
    • In OLTP systems, end users routinely issue individual data modification statements to the database. The OLTP database is always up to date, and reflects the current state of each business transaction.
  • Schema design
    • Data warehouses often use denormalized or partially denormalized schemas (such as a star schema) to optimize query performance.
    • OLTP systems often use fully normalized schemas to optimize update/insert/delete performance, and to guarantee data consistency.
  • Typical operations
    • A typical data warehouse query scans thousands or millions of rows. For example, "Find the total sales for all customers last month."
    • A typical OLTP operation accesses only a handful of records. For example, "Retrieve the current order for this customer."
  • Historical data
    • Data warehouses usually store many months or years of data. This is to support historical analysis.
    • OLTP systems usually store data from only a few weeks or months. The OLTP system stores only historical data as needed to successfully meet the requirements of the current transaction.

Sunday, March 4, 2012

Win Rar - Compression and Recovery

The popular RAR software is the creation of Eugene Roshal, who started it as his university doctoral dissertation. RAR is an acronym that stands for Roshal ARchive (or Roshal ARchiver). The current developer is Eugene’s brother Alexander Roshal. 

The following is a list of its most important features:
  • RAR is currently available from , as shareware, for Windows (WinRAR), Pocket PC, Macintosh OS X, Linux, DOS, and FreeBSD. WinRAR has a graphical user interface, whereas the other versions support only a command line interface.
  • WinRAR provides complete support for RAR and ZIP archives and can decompress (but not compress) CAB, ARJ, LZH, TAR, GZ, ACE, UUE, BZ2, JAR, ISO, 7Z, and Z archives. In addition to compressing data, WinRAR can encrypt data with the advanced encryption standard (AES-128). WinRAR can compress files up to 8,589 billion Gb in size (approximately 9×1018 bytes). Files compressed in WinRAR can be self-extracting (SFX) and can come from different volumes.
  • It is possible to combine many files, large and small, into a so-called “solid” archive, where they are compressed together. This may save 10–50% compared to compressing each file individually. The RAR software can optionally generate recovery records and recovery volumes that make it possible to reconstruct damaged archives.
  • Redundant data, in the form of a Reed-Solomon error-correcting code, can optionally be added to the compressed file for increased reliability. The user can specify the amount of redundancy (as a percentage of the original data size) to be built into the recovery records of a RAR archive. A large amount of redundancy makes the RAR file more resistant to data corruption, thereby allowing recovery from more serious damage. However, any redundant data reduces compression efficiency, which is why compression and reliability are opposite concepts.
  • Authenticity information may be included for extra security. RAR software saves information on the last update and name of the archive. An intuitive wizard especially designed for novice users. The wizard makes it easy to use all of RAR’s features through a simple question and answer procedure. Excellent drag-and-drop features. The user can drag files from WinRAR to other programs, to the desktop, or to any folder. An archive dropped on WinRAR is immediately opened to display its files. A file dropped on an archive that’s open in WinRAR is immediately added to the archive.
  • A group of files or folders dropped on WinRAR automatically becomes a new archive. RAR offers NTFS and Unicode support WinRAR is available in over 35 languages. These features, combined with excellent compression, good compression speed, an attractive graphical user interface, and backup facilities, make RAR one of the best overall compression tools currently available.
  • The RAR algorithm is generally considered proprietary, and the following quote sheds some light on what this implies The fact that the RAR encoding algorithm is proprietary is an issue worth considering. It means that, unlike ZIP and 7z and almost all other compression algorithms, only the official WinRAR programs can create RAR archives (although other programs can decompress them).
  • Some would argue that this is unhealthy for the software industry and that standardizing on an open format would be better for everyone in the long run. But for most users, these issues are academic; WinRAR offers great support for both ZIP and RAR formats.  

The following illuminating description was obtained directly from Eugene Roshal, the designer of RAR.
  • RAR has two compression modes, general and special. The general mode employs an LZSS-based algorithm similar to ZIP Deflate . The size of the sliding dictionary in RAR can be varied from 64 KB to 4 MB (with a 4 MB default value), and the minimum match length is 2. Literals, offsets, and match lengths are compressed further by a Huffman coder (recall that Deflate works similarly).
  • Starting at version 3.0, RAR also uses a special compression mode to improve the compression of text-like data. This mode is based on the PPMD algorithm (also known as PPMII) by Dmitry Shkarin. RAR contains a set of filters to preprocess audio (in wav or au formats), true color data, tables, and executables (32-bit x86 and Itanium, see note below). A data-analyzing module selects the appropriate filter and the best algorithm (general or PPMD), depending on the data to be compressed. 

Dictionary Methods
In addition to its use as Roshal ARchive, the acronym RAR has many other meanings, a few of which are listed here.
  • Remote Access Router (a network device used to connect remote sites via private lines or public carriers).
  • Royal Anglian Regiment, a unit of the British Army.
  • Royal Australian Regiment, a unit of the Australian Army.
  • Resource Adapter, a specific module of the Java EE platform.
  • The airport code of Rarotonga, Cook Islands.
  • Revise As Required (editors’ favorite).
  • Road Accident Rescue.

Rarissimo, is a file utility intended to automatically compress and decompress files in WinRAR. Rarissimo by itself is useless. It can be used only if WinRAR is already installed in the computer. The Rarissimo user specifies a number of folders for Rarissimo to watch, and Rarissimo compresses or decompresses any files that have been modified in these folders. It can also move the modified files to target folders. The user also specifies how often (in multiples of 10 sec) Rarissimo should check the folders. For each folder to be watched, the user has to specify RAR or UnRAR and a target folder. 

If RAR is specified, then Rarissimo employs WinRAR to compress each file that has been modified since the last check, and then moves that file to the target folder. If the target folder resides on another computer, this move amounts to an FTP transfer. If UnRAR has been specified, then Rarissimo employs WinRAR to decompress all the RAR-compressed files found in the folder and then moves them to the target folder. An important feature of Rarissimo is that it preserves NTFS alternate streams. This means that Rarissimo can handle Macintosh files that happen to reside on the PC; it can compress and decompress them while preserving their data and resource forks.