rulururu

post ext4 is on the rise

January 30th, 2008

Filed under: Linux — Kai @ 6:09 pm

tuxJust after Linus Torvalds has released Linux 2.6.24 kernel the development on 2.6.25 has started. With 2.6.25 lots of changes concerning ext4 filesystem are suggested.

The ext4 development ist not yet finished (it comes with the 2.6.19 kernel) - that’s the reason why Theodore Ts’o (tytso), leading developer on ext4, discourages from the usage in this early state for productive use. Nevertheless some distros soon will activate it in the kernel options. That’s why the developers of ext4 are going to implement new mount options to reveal that the filesystem is not ready for productive use but for test runs.

The major question is what the advantages of ext4 are.

  • Bigger data can be stored:
    Volumes up to 1024 petabytes (equiv. to 1,000,000 terabytes) are supported. With ext3 just 32 terabytes are possible.
  • Multiblock allocation:
    Addressing of data can be done with so-called extents, it merges data into blocks. This reduces time of access greatly. Moreover it’s more robust concerning hard disc crashes.
  • Number of subfolders increases:
    In comparison to ext3 it can store more subfolders (ext3 can hold 32,768 subdirs). This number will be broken with ext4.
  • More precise timestamps:
    Timestamps are more precise - they’d be set in nanoseconds.
  • Usage of checksums:
    A great feature will be that checksumming is used for journaling.

Besides those main features they still have some more features up their sleeve (e.g. increase the allowed filesize, online defragmentation, …).

I think those are great features that gives us lots of new possibilites - but some of them are not yet relevant (like 1024 petabytes storage). I’m really anxious how fast time will come when we think in those dimension of quantity.

Mounting ext4 can be done just the common way:

mount -t ext4dev -o extents /dev/sdc1 /mnt/test

It’s not necessary to convert ext3 partitions for the usage of ext4, they can be used by activating ext4 in the kernel.

If you don’t have a filesystem large enough and want to test 64bit feature, there is a script that creates a sparse file to simulate a large device (See explanation by Stephen Tweedie).
An other script is used to remove this sparse (./sparse_remove $map_name).

Ext4 is mostly based on parts of ext3 – similar to ext3 which is mainly based on ext2. Kernel developers think about deleting ext3 from the kernel source ’cause ext4 can manage ext3 perfectly and it would save affort to care for two sources the ext3 and ext4 one.

In the end some performace facts about ext4:
First benchmarks of the ext4 file system

post Save Application data before shutdown

January 28th, 2008

Filed under: .NET — Kai @ 4:19 pm

Often in application before the OS shuts down some data is saved. In some cases a messagebox asks you for that e.g. in MS Word.

The way to implent that is that the app has to recognize that the OS is going to shut down.

FormClosingEventArgs is derived from CancelEventArgs. FormClosingEventArgs has property (CloseReason) which tells you reason of close. This may be used for identifying and handling close reason.

At first the OnClosing method has to be overridden:

protected override void OnClosing(CancelEventArgs e) 
        { 
            //Your alternate implementation
            FormClosingEventArgs args = e as FormClosingEventArgs; 
            if (args != null) 
            {
                //do something with args
            }
            base.OnClosing(e); 
        }

Referred to MSDN documentation there are some properties of CloseReason.

  • None: The cause of the closure was not defined or could not be determined.
  • WindowsShutDown: The operating system is closing all applications before shutting down.
  • MdiFormClosing: The parent form of this multiple document interface (MDI) form is closing.
  • UserClosing: The user is closing the form through the user interface (UI), for example by clicking the Close button on the form window, selecting Close from the window’s control menu, or pressing ALT+F4.
  • TaskManagerClosing: The Microsoft Windows Task Manager is closing the application.
  • FormOwnerClosing: The owner form is closing.
  • ApplicationExitCall: The Exit method of the Application class was invoked.

The implementation of the CloseReason senario is very easy:

switch (ce.CloseReason)
        {
            case CloseReason.UserClosing:
                SaveData(); //data is saved
                break;
            case CloseReason.WindowsShutDown:
                SaveData(); //data is saved
                break;
default: break;
       }

post Data binding with WPF

January 28th, 2008

Filed under: .NET, C++, General Programming, MFC, WPF — Kai @ 2:50 pm

Today I had a close look at the Data binding possibilities provided by the Windows Presentation Foundation.

Before telling you more about that I’d like to show you good-old know alternatives and then compare them to WPF’s solution.

MFC with C++

DDX (Dialog Data Exchange) and DDV (Dialog Data Validation) are two great tools that allow us to easily set and access the values of certain MFC controls.

DDX allows us to link the value of a variable to a control. To use some DDX functions, you should override the DoDataExchange function of a dialog:

afx_msg void DoDataExchange(CDataExchange*);

The function itself can look like this, it just binds the “resource” to a member variable.

afx_msg void DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Text(pDX, TXT_NAME, m_strName);
}

The first line calls the base class, the seconds calls that function:

void DDX_Text(CDataExchange* pDX, int nIDC, CString& value);

If we want to set the value of the controls, we can call the UpdateData passing FALSE to the UpdateData function. On the other hand, if we want to retrieve the value of the control into our CString variable, we would pass TRUE to the UpdateData function.

BOOL CWnd::UpdateData(BOOL bSaveAndValidate)
{
	CDataExchange dx(this, bSaveAndValidate);
	try
	{
		DoDataExchange(&dx);
	}
	catch(CUserException e)
	{
		return FALSE;
	}
	return TRUE;
}

For other controls such as the list box, radio button and check box, we would use the similar DDX functions DDX_CBString, DDX_Radio and DDX_Check respectively.

When using DDX for the exchange of data between controls and variables we easily can validate them.

void CSample::DoDataExchange(CDataExchange* dx)
{
	CDialog::DoDataExchange(dx);
	//{{AFX_DATA_MAP(CDlg)
	...
	DDX_Text (dx, TXT_AGE, m_iAge);
	DDV_MinMaxInt(dx, m_iAge, 0, 150);  // <- validation
	. . .
	//}}AFX_DATA_MAP
}

The DDX_Text() call transfers the contents of the edit control into the m_age data member, converting it into an integer. After that is has to pass validation.

DDV_MinMaxInt looks as follows:

void DDV_MinMaxInt(CDataExchange* dx, int value, int min, int max)
{
	if (value < min || value > max)
	{
		AfxMessageBox("Please enter a value between ...");
		dx->Fail();
	}
}

If the value doesn’t yield the condtions of the validation method a error message is shown and Fail() is invoked on the data exchange object.

Windows Forms with C#

Data binding in C# provides a more advanced way for developers to create a read/write link between the controls on a form. It’s often used for binding forms to complex data structures such as SQL databases. Windows Forms data binding allows you to access data from databases as well as data in other structures, such as arrays and collections.

Each Windows Form has at least one BindingContext object that manages the access to and from the controls. For example if you add use a textbox control to a form and bind it to a column of a table (e.g. “tblAddress.Name”) in a dataset(e.g. called “dsAddr”), the control communicates with the BindingContext object for that form.

txtname.DataBindings.Add("Tom Morello",Adresses,"tblAddress.Name");
CurrencyManager cm = (CurrencyManager)this.BindingContext[dsAddr,"tblAddress"];
foreach(object s in cm)
{
Console.WriteLine(s.ToString());
}

The CurrencyManager is used to keep data-bound controls synchronized with each other.

That way of data binding is very useful when dealing with collection controls such as ListView or ListBox.
It’s common practice to bind them to any kind of Array, Collection or List.

A quick and clever way is the usage of data bindings in C# with ADO.NET Data Objects because those data structures are suitable for binding to.

DataTables such as GridView provide DataSource and DataMember properties:

gv1.DataSource = dsAddr;
gv1.DataMember = "tlbAddresses";

other appropriate objects are:

  • DataColumn
  • DataView
  • DataSet
  • DataViewManager

You also can bind controls to other controls. Binding data of a ComboBox to a ListBox follows the same model and is no problem.

DataBinding with WPF

Now we come to the point the article originally was about.
When working with collection-like data sources in WPF, you can read properties of the selected item by writing your binding expressions directly into the XAML (eXtensible Application Markup Language) code that defines the structure of every WPF-GUI.

<DockPanel DataContext="{x:Static Persons}">
  <TextBlock DockPanel.Dock="Top"
             Text="{Binding Lastname}" >
  <ListBox ItemsSource="{Binding}"
           IsSynchronizedWithCurrentItem="True" />
</DockPanel>

In that example the source is an array of persons names and the ListBox should show the lastname of every person the array contains.
When WPF encounters a binding to a non-existent property on a collection, WPF has a fallback strategy: it looks for the named property on the current item.

If source and destination are both collections you can do

Property="{Binding /}"

to bind them to each other.

That’s nice behavoir but just the beginning of some great ideas.
There are several things you can define in the binding object:

  • OneWay binding causes changes to the source property to automatically update the target property, but changes to the target property are not propagated back to the source property.
  • TwoWay binding causes changes to either the source property or the target property to automatically update the other. This type of binding is appropriate for editable forms or other fully-interactive UI scenarios.
  • OneWayToSource is the reverse of OneWay binding; it updates the source property when the target property changes.
  • OneTime binding causes the source property to initialize the target property, but subsequent changes do not propagate. This means that if the data context undergoes a change or the object in the data context changes, then the change is reflected in the target property. This type of binding is appropriate if you are using data where either a snapshot of the current state is appropriate to use or the data is truly static.

The UpdateSourceTrigger property of the binding determines when binding should appear.

  • LostFocus (default e.g. for TextBox)
  • PropertyChanged
  • Explecit (when calling UpdateSource)

Due data binding you could for example bind a textbox value to its background color. The property of type string in the binding source object is connected to a Color property of type Color.

IValueConverter makes it possible:

public class ColorBrushConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        Color color = (Color)value;
        return new SolidColorBrush(color);
    }
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return null;
    }
}

To ensure that the user has entered the expected information we user input validate it with some logic.
At first we add a validation in description section of the GUI.

<TextBox Name="Birthday" 
    Style="{StaticResource textStyleTextBox}">
  <TextBox.Text>
    <Binding Path="BDay" UpdateSourceTrigger="PropertyChanged">
        <Binding.ValidationRules>
        <ExceptionValidationRule />
      </Binding.ValidationRules>
    </Binding&gt;
 </TextBox.Text>
</TextBox>

The ValidationRules property takes a collection of ValidationRule objects. When the user enters a value that cannot be converted to an the expected type, an exception is thrown, causing the binding to be marked as invalid.

You also can define your own rule:

<Binding.ValidationRules>
                <src:MyRule />
            </Binding.ValidationRules>
class MyRule : ValidationRule
{
    public override ValidationResult Validate(object value, CultureInfo cultureInfo)
    {
        DateTime date;
        try
        {
            date = DateTime.Parse(value.ToString());
        }
        catch (FormatException)
        {
            return new ValidationResult(false, "Invalid date!");
        }
        if (DateTime.Now.Date < date)
        {
            return new ValidationResult(false, "You cannot be born in the future.");
        }
        else
        {
            return ValidationResult.ValidResult;
        }
    }
}

You also might visualize a wrong input for the user.

<Style x:Key="textStyleTextBox" TargetType="TextBox">
  <Setter Property="Foreground" Value="#fff000" />
  <Setter Property="MaxLength" Value="10" />
  <Style.Triggers>
    <Trigger Property="Validation.HasError" Value="true">
      <Setter Property="ToolTip"
        Value="{Binding RelativeSource={RelativeSource Self},
                        Path=(Validation.Errors)[0].ErrorContent}"/>
    </Trigger>
  </Style.Triggers>
</Style>

Now a tooltip appears when the HasError property of Validation is true. You can define a so called Error Template that is used for invalid inputs, if you don’t the default is used.

There are enough and to spare features you can use when working with bindings in WPF, but telling about all of them would carry things too far.

Comparison and forecast

The way MFC programmers use is clear and easy to use. It’s not very casual to handle but it can be extented unique for the project’s necessities.

Win Forms are mostly the same - also the spreading of Win Forms applications on the software market seems to be similar to MFC apps.

From my point of view software development can be really enhanced by technolgies like those provided by the WPF. Also for non-WPF applications its always worth to think about those concepts.

post C# params keyword

January 25th, 2008

Filed under: .NET — Kai @ 5:58 pm

Lets take a look at what the params keyword does.

It mainly gives you the ability to create functions that take a variable number of arguments - often called “variadic” or “variable arity” functions. This is not a really new feature only provided by C# you can also find it in C and C++, in the form of the “…” syntax, and Java uses that syntax as well. (e.g. Exception{} catch(…))

I’d like to show how useful the usage in C# can be:

public int Sum(params int[] list)
{
  int res = 0;
  foreach (int i in list)
    res += i;
  return res;
}

As you can see, the params keyword is used as an argument list for a number of typed vars.

You can call that small function with zero or more arguments.

int result = Sum(5);
result = Sum(5,4,3,4);
result  = Sum(new int[] { 3,4,4,2,11 });
result  = Sum();

This would report an error because the arguments are invalid:

int result = Sum(1,2,3 new int[] { 4,5,6 });

Even though you can pass in lists/array for the params, you can’t mix it up with regular values at all.

post Null coalescing operator and LINQ

January 24th, 2008

Filed under: .NET — Kai @ 11:40 pm

I recently wrote about the coalescing operator and its usage, now I’d like to show you a really effective way to use in with LINQ.

First Step:

What’s LINQ (Language Integrated Query)?
LINQ was released as a part of .NET Framework 3.5. It defines a set of query operators that can be used to query, project and filter data in arrays, enumerable classes, XML, relational database, and third party data sources. While it allows any data source to be queried, it requires that the data be encapsulated as objects.
The syntax is mostly like SQL and IntelliSense helps you greatly with it ;-)

For some introduction I can advice two articles by Thiru Thangarathinam on 15Seconds:

Seconds Step:

I create a sample XML file filled with some test data:

<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?> 
 <contacts> 
      <member> 
           <firstname>Tom</firstname>
           <lastname>Morello</lastname>
      </member>
<member> 
           <firstname>Jon</firstname> 
           <lastname>Bon Jovi</lastname> 
      </member> 
 </contacts>

When handling raw XML are cases where XML shapes are irregular and/or missing elements/attributes.

<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?> 
 <contacts> 
      <member> 
           <firstname>Tom</firstname>
           <lastname>Morello</lastname>
      </member>
<member> 
           <firstname>Jon</firstname> 
           <lastname>Bon Jovi</lastname> 
<age>46</age> 
      </member> 
 </contacts>

We got a tag that is not always present for every contact in the file. The ?? operator can be very useful in these scenarios.

Third Step - Solution:

Reading the XML data with LINQ is quite simple and can be done with something along those lines:

XDocument contFile = XDocument.Load(File.FromFile("my.xml"));
var contacts = from c in contFile.Descendants("contact")
	select new {
		_sName = (string) c.Element("firstname"),
		_sFName = (string) c.Element("lastname"),
		_iAge = (int) c.Element("age")
	};

This will yield poorly when not finding the element “age” in the first block.
I’m using an explecit conversation for writing the tags into the vars by casting them e.g. to int. One way to fix this is to modify our LINQ to XML query so that we indicate that _iAge is a nullable integer.

Cast to be (int?) instead of (int):

_iAge = (int?) c.Element("age");

But what can we do if we don’t want an empty value for missing tags in the XML? We can use the coalescing operator to set a default value for every missing element.

_iAge = (int?) c.Element("age")?? 0;

Now instead of a blank output the defaultvalue 0 is shown.

Conclusion:

You can use the .NET ?? operator in many types of scenarios. Not just to prevent yourself from annoying excessive code writing also for better maintainability of the source and clear programming.

post Own HashMap

January 22nd, 2008

Filed under: C++, General Programming — Kai @ 10:25 pm

When needing a container hash tables are very common data structures. They provide efficient key based operations to insert and search for data in containers.

1. What are the advantages of a hashmap in comparison to a simple array or vector and when to use it?

  • Hash tables are damn fast because keys are transformed to a hash using a hash-function and a number that is used as an index in an array to locate the desired location (”bucket”) where the values should be.
  • Hash tables support the efficient insertion of new entries
  • Searching for the required data is often independent of the number of items stored

Hash tables are most useful when large numbers of records are to be stored, especially if the size of the data set can be predicted.

2. Things you should consider when designing a hash map

Most important is the hash function and the collision resolution mechanism. The hash function is responsible for the arithmetic operation that transforms a particular key into a particular table address. The collision resolution mechanism is responsible for dealing with keys that hash to the same address.

A good hash function is essential for good hash table performance.
Recommented for typical hash tables is Bob Jenkin’s LOOKUP3 hash function which is very well performing.

A mathematical byte-by-byte implementation that performs particularly well is the Jenkins One-at-a-time hash, adapted here from an article by Bob Jenkins, its creator. The good thing about it is that it’s very simple and can be understood easily.

uint32_t jenkins_one_at_a_time_hash(unsigned char *key, size_t key_len)
{
    uint32_t hash = 0;
    size_t i;
for (i = 0; i < key_len; i++) {
        hash += key[i];
        hash += (hash << 10);
        hash ^= (hash >> 6);
    }
    hash += (hash << 3);
    hash ^= (hash >> 11);
    hash += (hash << 15);
    return hash;
}

If two keys hash to the same index, the corresponding records cannot be stored in the same location. This can cause fatal errors in data storage.

3. How to use

Although the C++ STL (Standard Template Library) does not support hash based containers, most compilers like GCC or Visual C++ have their own implementation.

When getting a bit more comprehension for those maps I tried myself to make a small example:

template <
  class key, 
  class value, 
  class hash_function = hash<key>, 
  class increment = unit_increment<key>,
  class equal_key = std::equal_to<key>, 
  class allocate = std::allocator<std::pair<key, value> > >
class OwnHashMap
{
private:
  typedef std::pair<key, value> Map_pair;
  typedef hash_table <
    key,
    Map_pair,
    hash_function,
    increment,
    equal_key,
    selectFirst<Map_pair>,
};

For better accessing the table an iterator can be useful:

template <
  class hash_container,
  class constness_traits ></code>
 
<code>struct hash_table_iterator
{  
  typedef typename constness_traits::pointer ptr;
  typedef typename constness_traits::reference ref;</code>
 
  <code>reference operator*()const 
  {
    return (*this->container)[this->current].item;
  }
  pointer operator->()const 
  {
  return &(operator*());
  }
};

Now it can be used that way:

typedef MyMap<int,double> Map;
  typedef Map::value_type value_type;
  typedef Map::iterator iterator;
Map map;  
  MyMap.add(value_type(1, 1.1));
  MyMap.add(value_type(3, 3.23));
int n = map.size();
//access values
  for ( iterator it = map.begin(); 
it != map.end(); 
++it )
  {
    std::cout << it->first;
    std::cout << it->second;
  }

That’s just the easiest way of implementing a hashmap. That solution could easily be extented with sort algorithms or whatever you need for better data-storing.

Finally I’d like to advice Google SparseHash to you.

Google SparseHash project contains several hash-map implementations in use at Google, with different performance characteristics, including an implementation that optimizes for space and one that optimizes for speed. The memory-optimized one is extremely memory-efficient with only 2 bits/entry of overhead.

ruldrurd
Next Page »
Powered by WordPress, Content and Design by Kai Bellmann
Entries (RSS) and Comments (RSS)