CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Raymond Lewallen

Professional Learner

January 2006 - Posts

  • Quantum componentry in your computers - super computers on the way!

    Received an email from a guy I know through our blogs and the shared blogs we follow regarding quantum computing.  I posted awhile back on quantum cryptography.

    If you haven’t seen some of what AtomChip Corporation is doing, you should check it out.  I believe this is the future of componentry.  Here are the specifications of one of the laptops they manufacture.  Check out their website and look at the sizes of these components, ultra tiny!

    SPECIFICATIONS:

    1TB AtomChip® Quantum-Optical Non-Volatile RAM and storage / 6.8GHz AtomChip® CPU (AtomChip® Quantum® II processor) / 2TB Additional AtomChip® Quantum Storage / 12.1" WXGA 1280x800,6:10 TFT Glare Type LCD display with 1.3 Mega pixel camera / Integrated graphic controller [AtomChip® DVM (Dynamic Video Memory) /802.11a/802.11b/802.11g WLAN /Intel® PRO/Wireless 2100/2200BG/2915ABG network connection / Bluetoth (with antenna build-in), GPS (with Bluetooth antenna) / PCMCIA slot x 1 / IEEE1394A x 1 / USB2.0 x 3 / SVGA-out port / Optical Drive: DVD Super Multi / LAN (RJ45) / Modem(RJ11), / Stereo Earphone-out / Built-in two stereo speakers / Built-in Microphone / Interface Ports Front Side:4 in 1 Card reader [SDIO, SD, MS Pro, MS] / Application Launch Key: E-mail,Internet, Capture, WLAN / Pointer: Synaptics touchpad with 4 way scrolling button / Rechargeable Li-Ion battery / Windows XP Professional and Linux / Voice Command / PROMT-Translator (8 languages) / AC Adapter / BOSE Headphone Music System with noise Canceling / Dimensions: 320(W)x242(D)x22(H)mm/28mm (front/back) /Weight around 4.18lb (1900g) when fully equipped.

    The expected selling price is $18,500.

  • Turning 30 and the expectations that come with the big birthday

    Today is the day.  My 30th birthday.  The big benchmark.  No longer a young 20–something.  No longer can I fool myself into believing I’m still just right out of high school and those teenage years.  I’m now 30.  It sounds freaky just saying it.

    Now I’m probably going to have to start writing code like a 30 year old.  Like I really do have that 12 years of professional experience behind me. I personally expect more out of developers in their 30s than I do those in their 20s, because they have the experience behind them in most cases.  Now I’m one of those 30–somethings, and have to expect more out of myself and my capabilities.  Everyone's expectations increase when you hit 30 I think.  But none more that the expectations we put on ourselves.

    A couple of other friends, who you know, but I won’t mention because they haven’t, also turn 30 this year (1 of them did just last week).  I wonder if they feel the same about, about expectations.

    Some other notable people who celebrate their birthday’s with me are:

  • 1979 Tatyana Ali (actress)

    1968 Will Smith (singer, actor)

  • 1968 Mary Lou Retton (Olympic Gold Medalist for Gymnastics)
  • 1960 Nastasssia Kinski (actress)
  • 1949 John Belushi (comedian)
  • 1943 Sharon Tate (actress)
  • 1941 Neil Diamond (singer)
  • FireFox - The Hungry-Hungry Hippo?

    Firefox!  Please!  I love you so much, please relenquish your iron grip on my resources, as you are rendering my computer useless.

    I like to have just one browser open, and sometimes get up to about 10 tabs open in that browser.  Its becoming more common that Firefox (I’m using 1.0.7) locks up and I have to kill it.  This time, I had 4 tabs open, none of which were running anything dynamic that _should_ cause the CPU usage for the browser to remain high.  I didn’t screen shot what the resources were like before I had to kill it, but I can tell you it was running 100% for the longest time, using 289MB of memory and 371MB of virtual memory.  Total that up kids, that a whopping 660MB of memory!  The high memory usage seems to be the norm for FireFox, when used the way I use it (one instance, mutliple tabs).

    Why on earth does my browser feel the need to consume that much memory?  I have the memory available, but still.  Here’s the screenshot of my task manager directly after having to kill the firefox.exe process.  You can see by the history where its just sucking up the CPU, and its been doing this more and more often.

    Has anybody else been experiencing their FireFox taking on the persona of a Hungry-Hungry Hippo(TM)? It seems to do this randomly, not when I'm visiting a particular site of sets of sites.  Its not picky about the sites I visit or the number of tabs I have open, it just likes to lock-up.  Sometimes, I just wait it out, and after about 5 minutes or so, the majority of the time is returns back to reality and frees up my computer.  I have yet to figure out what it could be doing and what is causing it to behave this way.

    I'm going to upgrade to 1.5 to see if it helps out.

    Update: I upgraded to 1.5, and now half of my add-ons and plug-ins don't work anymore.  But we'll see how it goes with the resource usage.

    Update: I now have Firefox 1.5 with 3 tabs open, Gmail, Codebetter and MSNBC. Using just over 300MB of total memory.

  • A word wrapping solution using VB.Net

    While helping a friend with some code, I ran into a situation where I needed to do something to word wrap lines that were going to print.  The lines were actually data coming from a database where the max length of the field is over 300 characters.  When making this data show up in a multiline textbox, it isn’t a big deal because we can word wrap it easily enough.  The problem comes when you want to print this data.  I needed to take the data stored in the textboxes and write it to a text file for printing at a later time.  That was all fine, except when I printed, naturally, the lines would run off the side of the page.

    So here is what I came up with.  It was very difficult looking for solutions to handle word wrapping out on google.  The solutions I did find seems overly complex for the problem that needed to be solved.  If you have written a better solution, please let me know.  This is the best I came up with in the time constraint I had to write it in (2 hours).

    Keep in mind, this word wrapping is rather specific, because it is setup for a basic print layout in Portrait mode, 1” margins and printing 10pt Times New Roman font, which is where the 86 characters per line comes from.

    My next update to this routine will be to handle changing printer settings and adjusting for those for proper word wrapping.

    Word wrapping

        Private Sub SetupForPrint(ByVal currentText As TextBox, ByVal filename As String)

            Dim currentStreamWriter As System.IO.StreamWriter = New System.IO.StreamWriter(filename)

     

            ' This is the max length we want any line to be.  This is based on printing using

            '    the font fact Times New Roman at a font size of 10.

            Dim maxLengthOfALine As Integer = 86

     

            ' This is our starting position within the text body wherewe start a new line

            Dim startingPosition As Integer

     

            ' This is the ending position within the text body where we end a new line

            Dim endingPosition As Integer

     

            ' This is used for the substring for the length of the line we need to pull.

            '    This has to be used because you can't always break apart strings for

            '    word wrapping at exactly 86 characters.  You have to account for not

            '    breaking words in half, so you have to break apart at spaces.  You'll

            '    see this below

            Dim lineLength As Integer = maxLengthOfALine

     

            ' This is the line that we will be writing to the file.

            Dim line As String

     

     

            ' Start looping through the text of the textbox until we reach the end.

            While startingPosition < currentText.Text.Length

                ' This locates the ending position of a line identified by a CRLF.

                endingPosition = currentText.Text.IndexOf(vbCrLf, startingPosition)

     

                ' This tells us that the complete line, from start to CRLF, is less than

                '    86 characters, so no word wrapping needs to be handled.

                If endingPosition - startingPosition < maxLengthOfALine Then

     

                    ' This tells us that we have reached the end of the file, so get

                    '    the line and get out of the loop.

                    If endingPosition = -1 Then

                        line = currentText.Text.Substring(startingPosition, currentText.Text.Length - startingPosition)

                        currentStreamWriter.Write(line)

                        Exit While

                    Else

                        ' We are not at the end of the file, but we have a suitable line

                        '    to write that doesn't need word wrapping.  Get the line and

                        '    set the start position of the next line to the end position of the

                        '    current line + 1.

                        line = currentText.Text.Substring(startingPosition, endingPosition - startingPosition)

                        startingPosition = endingPosition + 1

                        currentStreamWriter.Write(line)

                    End If

     

                Else

                    ' THIS IS WHERE WORD WRAPPING IS HANDLED

                    ' This continues to word wrap for every 86 characters in the line

                    While lineLength + startingPosition <= endingPosition

                        ' This backtracks in the line in order to find a suitable place to word wrap,

                        ' in this case, a SPACE

                        While currentText.Text.Substring(startingPosition + lineLength - 1, 1) <> Chr(32)

                            lineLength -= 1

                        End While

     

                        ' Get the line.

                        line = currentText.Text.Substring(startingPosition, lineLength)

     

                        ' Write the line.

                        currentStreamWriter.Write(line & vbCrLf)

     

                        ' If we had to backtrack, we can't add 1 to the count.  This

                        ' prevents us from cutting off the first letter of the next word.

                        If lineLength < maxLengthOfALine Then

                            startingPosition += lineLength

                        Else

                            startingPosition += lineLength + 1

                        End If

     

                        ' Reset the lineLength back to the default.

                        lineLength = maxLengthOfALine

     

                    End While

     

                    ' This is the end of the lines we had to word wrap.  This writes

                    ' the last line that got word wrapped to the file.

                    line = currentText.Text.Substring(startingPosition, endingPosition - startingPosition)

                    currentStreamWriter.Write(line)

     

                    ' Set the start position of the next line to the end position of the

                    '    current line + 1.

                    startingPosition = endingPosition + 1

                End If

            End While

     

            currentStreamWriter.Close()

        End Sub

  • Database Basics Part Three - Isolation

    In an effort to provide information for those of you who are newcomers to databases, especially with the wonderul new Sql Server Express, I have committed myself to about half a dozen posts or so targeted directly at you to help you understand and build better databases.  Stay tuned to this blog for weekly updates to this series, or subscribe to the rss feed.

    Previous articles in this series:

    Today we are going to look at isolation levels, specifically isolation levels supported within Sql Server.  By definition, an isolation level determines the degree of isolation, or seperation, of data among concurrent transactions.  See the post on the ACID model for how isolation is a cornerstone of database design and its transactions.

    There are 5 levels of isolation available in Sql Server, 1 of them being new in Sql Server 2005.

     

    Read Uncommitted Isolation

    This is also referred to as “dirty read” isolation.  This is the lowest level of isolation available and does nothing more than ensure that physically corrupt data cannot be read, but allows reads of logically corrupt data, if the data exists in that state.  Not even exclusive locks prevent other transactions from acting on this data.  Let’s take a look at what this means.

    Take the following table:

    Cars table
    Manufactuer Make Color
    GM Buick Black

    Now let’s examine what occurs given the following series of events, which will result in what are called “dirty reads”, which are a type of non-repeatable read; a type of data inconsistency.

    • TransactionA starts a transaction.
    • TransactionA updates the row to change the color to Red.
    • TransactionB starts a transaction.
    • TransactionB reads the row from the database.
    • TransactionA rolls back the transaction, therefore leaving the color intact as Black.
    • TransactionB ends.

    What data does TransactionB have?  TransactionB shows the color of the car as Red, even though, logically and transactionally, this data never really even existed in the database because TransactionA never committed that change to the database.  This is known as a “dirty read”. 

    You can also lose updates this way.  Let’s take the following scenario:

    • TransactionA starts a transaction.
    • TransactionA updates the row to change the color to Red.
    • TransactionB starts a transaction.
    • TransactionB updates the row to change the color to Blue.
    • TransactionA commits its transaction, changing the color to Red.
    • TransactionB commits its transaction, changing the color to Blue.

    What color is the car?  Its blue.  This is also known as the “last in wins” rule.  If multiple transactions change the same data at the same time, the last one to commit is going to win.

    You can see from this simple example what kind of troubles you can get into by using Read Uncommitted isolation levels.

     

    Read Committed Isolation

    This is the default isolation level used in Sql Server.  Sql Server issues shared locks under this isolation level which prevents dirty reads from occurring, like what we saw above in read uncommitted isolation levels.  In the same scenario given above, this time using read committed isolation, TransactionB would have in fact shown the color as Black, because it would be reading data as it existed in a committed state.

    What read committed isolation does not do is prevent other transactions from from changing the same data at the same time the data is already being held by another transaction.  There are 2 primary results for this type of behavior, known as phantom data and again, like we saw in read uncommitted, non-repeatable reads, but this time they are not the special type of non-repeatable reads called “dirty reads” like we saw above.

    • Phantom data occurs under the following circumstances under read committed
      • TransactionA begins a transaction.
      • TransactionA reads a row.
      • TransactionB begins a transaction.
      • TransactionB deletes the row read by TransactionA.
      • TransactionB commits its transaction.
      • TransactionA can no longer repeat its initial read in order to do an update, because the row no longer exists, resulting in phantom data.

    • Non-repeatable reads (not dirty reads) occur under the following circumstances under read committed
      • TransactionA begins a transaction.
      • TransactionA reads a row.
      • TransactionB begins a transaction.
      • TransactionB changes the color of our car to red.  TransactionA read the color of the row to be black.
      • TransactionB commits its transaction.
      • TransactionA reads the row again.
      • TransactionA has inconsistent data because the color now reads red instead of black, all within the scope of the same transaction that TransactionA began.

     

    Repeatable Read Isolation

    Here, Sql Server places locks on the data used in a query within the transaction, and this prevents other transactions from modifying data in that data set locked by the initial transaction.  This prevents non-repeatable reads, including dirty reads, from occurring.  However, another concurrent transaction can add new data to that scope of data of the original transaction, but the original transaction does not include the new row as part of its lock or data set, because it didn’t exist at the time the lock was issued.  The initial transaction will, however, get that inserted row in subsequent reads of the data.  Again, this is phantom data.

    Cars table
    Manufactuer Make Color
    GM Buick Black
    Ford Lincoln Black
    • Phantom data will occur under the following circumstances under repeatable read
      • TransactionA begins a transaction.
      • TransactionA reads all rows that have color = “Black”.
      • TransactionB begins a transaction.
      • TransactionB inserts new row with values “GM”, “Pontiac”, “Black”.
      • TransactionB commits its transaction.
      • TransactionA updates all the data from its query to color = “Red”.  This will also update the row that TransactionB inserted, because TransactionA must read the data again in order to update it.
      • TransactionA commits its transaction.

     

    Serializable Isolation

    This is the most restrictive isolation level available, and not a commonly used level of isolation, because it is also referred to as “the perfect transaction”, and perfect transactions just aren’t always the best choice.  Once a serializable transaction has started working on a particular data set, no other transaction can do anything to it until the initial transaction releases its locks.  This means no changing data, no adding new rows into the same data set, nothing.  Phantom data cannot exist and non-repeatable reads cannot occur.  However, this affects your performance because this isolation level reduces your concurrency levels because of the restrictiveness of the isolation.  Other transaction must wait for the locks to release. 

     

    Snapshot Isolation – New to Sql Server 2005

    Other databases have had a snapshot isolation level for awhile now.  Sql Server just caught up.  Snapshot isolation is an optimistic locking approach to the pessimistic locking schemes of read committed and serializable isolation levels.  There are 2 types of snapshot isolation levels:  Transaction-level snapshots provide the same level of isolation as serializable transactions, and statement-level snapshots provide read committed levels of isolation.

    Snapshot isolation basically works by row versioning.  Row versioning is nothing more than Sql Server keeping up with changes made to the database while a transaction is occuring.  What happens, and this is the beauty of snapshot isolation, a copy of a row is made for the transaction is that is where the read occurs.  This prevents a lock on the actual row in the database.  When the transaction wants to write the data, it checks to see if the actual data matches the copy of the data, and if it does, it commits the write.  If not, then the snapshot transaction fails.  This is the same as serializable isolation, except we have better performance because we didn’t have to place locks on the data that we read.  This helps to support more concurrent transactions.

    I haven’t played too much with snapshot isolation in Sql Server 2005, so please experiement for yourselves and as always, do your own research.  If you want to read what is probably the best source of information on snapshot isolation, read this 60 page whitepaper on it written by Kimberly L. Tripp.  That’s how much info there is to cover on the topic of just one isolation level, much less all 5.  Again, this is a series for newcomers to databases, so I don’t go into that level of depth.  Not to mention the fact that I can never go into Sql Server as deeply and with as much knowledge as the wonderful Kimberly L. Tripp!

  • Database Basics Part Two - Normalization

    In an effort to provide information for those of you who are newcomers to databases, especially with the wonderul new Sql Server Express, I have committed myself to about half a dozen posts or so targeted directly at you to help you understand and build better databases.  Stay tuned to this blog for weekly updates to this series, or subscribe to the rss feed.

    Normalization: organizing data in your database is an efficient way for both you and the database to use and understand.  What this really means is that normalization helps us to elimate redundant data and to store the data we have in a manner that makes sense.  Last week we talked about ACID, and you will recall the “atomicity” of a database.  Atomicity also must be maintained in tables via normalization.  Normalization helps us to create atomic tables by removing redundant data within rows of a table, thus creating atomic tables.

    Guidelines exist for normalizing data (referred to as normal forms) and exists as 5 sets of rules, labled first normal form through fifth normal form.  In most practical applications, you rarely see 4th or 5th normal forms (herein referred to as 1NF-5NF), so we’re going to look at 1NF, 2NF and 3NF, along with denormalized data.

    Denormalized Data

    Let’s take a look at some denormalized data.  I’m going to use the simplest example that comes to mind, and that is a auto repair shop that specializes in domestic automobiles (domestic to the USA, that is).  Our table lists automobile manufactuers and the makes of automobiles they manufactuer that the shop services.

    Fig. 1
    Manufactuer Make 1 Make 2 Make 3
    Ford Mercury Lincoln Ford
    GM Chevrolet Pontiac Saturn, Buick
    Chrysler Dodge      

    Fig. 1 shows us denormalized data.  For GM, we didn’t have enough columns to store all the data we wanted, so we put Buick in with Saturn.

    First Normal Form (1NF)

    We want to take fig. 1 and normalize it a bit.  Before we can do that, we need to know what the rules of 1NF are.  There are 2 rules to 1NF.

    • Remove duplicate columns.
    • Each column by row position must have a unique value.

    As you can see, 1NF is already enforcing the atomic nature of a table.  So first we need to remove duplicate columns from the table, at the same time creating column by row positions that have unique values, which means we also have to seperate Saturn, Buick into individual values.

    Fig. 2
    Manufactuer Make
    Ford Mercury
    Ford Lincoln
    Ford Ford
    GM Chevrolet
    GM Pontiac
    GM Saturn
    GM Buick
    Chrysler Dodge

    Fig. 2 meets the 2 rules of 1NF.  We have normalized our Fig. 1 data into First Normal Form.

    Second Normal Form (2NF)

    For each level of normal form, the next level must adhere to the rules of the previous level, in addition to adding its own rules.  This means in order to create a data structure that meets the requirements of 2NF, we must first meet the requirements of 1NF.  We have already discussed the rules for 1NF, so let’s look at the rules required to meet 2NF.

    • Remove duplicate data in a single column and place the data in seperate tables.
    • Create relationships between the sets of data.

    In Fig. 2, if GM gets bought by another manufactuer, say Honda, then we’d have to replace each row in the table with Honda everywhere we find GM.  That is a bit inefficient.  By meeting the 1st rule of 2NF, we can replace GM with Honda in our manufactuer table in Fig. 3, and this is must more efficient and a logical storage of data.  In order to meet the 2nd rule of 2NF, we must have keys and relationships.  The ManufactuerId in the manufactuer table is a Primary Key.  It is a unique identifier used to reference the description for the manufactuer.  We create a relationship beween it and the Make table using Manufactuer column in the Make table as the foreign key.  Fig. 4 shows the database diagram for this.  Now we are starting to adhere to consistency of our ACID model.  Fig. 3 satisfies all the rules of 1NF and 2NF.  We have normalized our Fig.2 to Second Normal Form.

    Fig. 3Manufactuer Table
    ManufactuerId Manufactuer
    1 Ford
    2 GM
    3 Chrysler

    Make table
    Manufactuer Make
    1 Mercury
    1 Lincoln
    1 Ford
    2 Chevrolet
    2 Pontiac
    2 Saturn
    2 Buick
    3 Dodge

    Fig. 4

     

    Third Normal Form (3NF)

    Again, in order to meet the rules of 3NF, we must first meet the rules of 1NF and 2NF.  In addition to meeting these requirements, 3NF introduces 1 other rule:

    • All columns that are not dependent on the primary key must be removed.

    Sounds simple enough.  First, lets take our Make table from Fig. 3 and add a column to it that shows how much the auto repair shop charges per hour for each different make, and also a column that shows if the shop works on cars or trucks for each particular make.

    Fig. 5Make table
    Manufactuer Make Charge Style
    1 Mercury $60 Cars
    1 Lincoln $60 Cars
    1 Ford $75 Trucks
    2 Chevrolet $55 Both
    2 Pontiac $60 Cars
    2 Saturn $60 Cars
    2 Buick $60 Cars
    3 Dodge $75 Trucks

    Not let’s examine the data and see if they meet the requirements of 3NF.  First, we must consider that Make is the primary key in this table.  It is a unique value that can be used as an identifier.  Ideally, that would be an integer column called MakeId and Make would be a description column.  We’ll look at that in a moment.  Knowing that Make is the primary key, are all columns in the Make table dependent on the primary key?  Manufactuer: yes.  The manufactuer depends on the make of the automobile.  Style?  Yes, the style the shop works on is dependent on the make of automobile.  Charge?  Hrm.  If we look closely, it appears as though the charge is dependent on the style, not the make.  This table does not meet 3NF.  We must remove the charge column and relate it to the Style, not to the make.  We do this by creating another table called charge.

    Fig. 6
    Style Charge
    Cars $60
    Trucks $75
    Both $55

    Now we can remove charge from the Make table in Fig. 5, because they style that is dependent on the make, gives us the charge.  The complete 3NF structure when all is said and done looks like Fig. 7 below.

    Fig. 7

    The Final DataManufactuer Table
    ManufactuerId (PK) Manufactuer
    1 Ford
    2 GM
    3 Chrysler

    Make table
    MakeId (PK) Manufactuer (FK) Make Style (FK)
    1 1 Mercury 1
    2 1 Lincoln 1
    3 1 Ford 2
    4 2 Chevrolet 3
    5 2 Pontiac 1
    6 2 Saturn 1
    7 2 Buick 1
    8 3 Dodge 2

    Style Table
    StyleId (PK) Style Charge
    1 Cars $60
    2 Trucks $75
    3 Both $55

    Next topic in this series: Isolation

  • The Power of Computing aka Computing Power

    This is amazing.  It took over 700 computers working for years, but researchers (who coulda spent these kinda resources on something much more worthwhile, but that is another story) have identified the largest known prime number back on December 15th, 2005, which is over 9.1 million digits long.  That is absolutely amazing.  Also see the story on MSNBC.
    "...on December 15, 2005 at 8:46:48 CST on the Communications Lab computer no. 7, the 43rd Mersenne prime and largest known prime number at the time, 2^30402457-1 with its 9,152,052 decimal digits, was discovered"
More Posts

Our Sponsors

Free Tech Publications