Wednesday, March 13, 2024

Heavy Memory Usage of the Regression Test in XCTest

While recently wrestling with a regression test for my Swift project using the XCTest framework, I ventured into the tumultuous world of a heavily multithreaded application. Ensuring the absence of timing issues was like defusing a bomb – one wrong wire, and boom! So, what's a developer to do? Run the code tens of thousands of times, of course (or even more for the marathon runners of applications), and keep your fingers crossed for any anomalies.

My trusty XCTest framework was the stage, and my test code, which cheerfully returned results asynchronously, demanded I master the classic XCTest dance of expectation(...) and waitForExpectations(...) moves. All seemed well as the test passed with flying colors for a few thousand iterations. However, as I ambitiously cranked up the numbers, strange gremlins began to pop up. The memory usage skyrocketed, adding a whopping 90 MB per 10,000 iterations – definitely not a feature I had intended to implement!

I dove into the depths of Instruments, yet no memory leaks waved back at me. Puzzling! Yet, I noticed waitForExpectations was having a bit too much fun allocating objects like NSString left and right. My online detective work on Google and StackOverflow turned up zilch – it seemed I was charting uncharted waters.

Refusing to be defeated, me and my digital buddy ChatGPT kept on going into the uncharted territory with me as novis to Swift while ChatGPT blind beyond 20 ft. I scrutinized the allocation patterns revealed by Instruments with a detective's eye. This careful observation led to a pivotal "Eureka!" moment. It dawned on me to employ an autoreleasepool within the scope of each iteration. This strategic move, akin to a well-crafted chess play, was not just a stroke of luck but a calculated decision based on the insights gleaned. And, like a charm, this adept adjustment worked wonders, effectively dispelling the memory bloat gremlins back into the abyss from whence they came.


Here is a sample test code anyone can run in our XCTest project, to reproduce the issue and verify the solution.

  1.     func testAsyncOperationInLoop() {
  2.         let iterationCount = 50000  // Number of iterations in the loop
  3.         
  4.         for i in 1...iterationCount {
  5.             autoreleasepool {
  6.                 
  7.                 // Create a new expectation for each iteration of the loop
  8.                 let expectation = self.expectation(description: "Async operation \(i)")
  9.                 
  10.                 if i > 0 && i % (10000) == 0 {
  11.                     print("breaking for a breath")
  12.                 }
  13.                 
  14.                 // Simulate an asynchronous operation
  15.                 DispatchQueue.global().asyncAfter(deadline: .now() + 1.0/100000) {
  16.                     expectation.fulfill()  // Fulfill the expectation once the operation is completed
  17.                 }
  18.                 
  19.                 // Wait for the expectation to be fulfilled, with a timeout
  20.                 waitForExpectations(timeout: 2) { error in
  21.                     if let error = error {
  22.                         XCTFail("waitForExpectations errored: \(error)")
  23.                     }
  24.                 }
  25.             }
  26.         }
  27.         
  28.         print("All iterations completed")
  29.     }

Wednesday, February 07, 2024

Decoding the Mystery: How Food Label Decoder Simplifies Your Grocery Shopping

This blog is about a custom ChatGPT model that you can use if you have ChatGPT 4 account. Follow this link to use the custom model: https://chat.openai.com/g/g-c2AWb78Mr-food-label-decoder 


In today’s fast-paced world, our dependence on packaged foods has become more a necessity than a choice. Amidst the rush of daily life, who has the time to decode the complex list of ingredients on the back of every package? Yet, understanding these labels is crucial, as it's about more than just counting calories—it's about knowing what we're truly feeding ourselves and our loved ones.

The Hidden Truth Behind Packaged Foods

Packaged food items are a marvel of modern convenience, but this convenience often comes with a hidden cost. To stay competitive, ensure a long shelf life, and appeal to consumer tastes, companies often resort to using a range of additives, preservatives, and sugars. These ingredients can be cleverly disguised under scientific names, making them nearly impossible for the average shopper to recognize.

Moreover, with the ever-evolving "health trends," many products are marketed as "healthy" options, leveraging buzzwords like "natural," "organic," or "low-fat." However, a closer inspection of their ingredient lists often reveals a different story. How many times have you picked up a product, enticed by its health claims, only to find it packed with unpronounceable chemicals or excessive sugars?

Enter the Food Label Decoder

This is where the Food Label Decoder comes into play—a tool designed to bridge the gap between confusing labels and informed dietary choices. Powered by ChatGPT's Artificial Intelligence, it offers a simple yet revolutionary approach: just take a picture of the food label, share any specific dietary preferences or health concerns, and let the AI do the rest.
The Decoder breaks down the jargon into simple, understandable terms. It evaluates the major ingredients, sugars, preservatives, artificial components, potential allergens, and more. It even gives a health snapshot using emojis, making it easier to grasp the nutritional value at a glance.

Why Trust Food Label Decoder?

You might wonder, why trust an AI with something as personal as your dietary choices? The answer is simple: because it was created to solve real, everyday dilemmas—ones that I faced in my own life. Choosing between Product A and Product B, both claiming to be the healthier option, was a constant struggle due to the coded language of ingredients lists.

Food Label Decoder was born out of a need for transparency and simplicity. It cuts through the marketing noise, offering clear, unbiased information. It doesn't just tell you what's in your food; it helps you understand what that means for you, based on your unique health goals or dietary restrictions.

Making Informed Choices Has Never Been Easier

The beauty of the Food Label Decoder lies in its simplicity and accessibility. It empowers you to make informed decisions, turning the daunting task of reading labels into a quick, straightforward process. Whether you're navigating dietary restrictions, managing health conditions, or simply striving for a healthier lifestyle, the Decoder is your personal guide in the complex world of food shopping.

Act on Your Health

So, the next time you find yourself standing in the grocery aisle, perplexed by the list of ingredients on a product, remember that help is just a click away. The Food Label Decoder is more than just a tool; it's a movement towards a healthier, more informed society. It's time to take control of what we eat, one product at a time.

Why not start today? Take the first step towards becoming a more informed consumer and see the difference it makes in your shopping habits. After all, the best dietary choices are those made with confidence and clarity. Let's embark on this journey together, towards a healthier, happier you.

Wednesday, September 07, 2022

Useful Perforce Commands

 Perfoce Commands are especially useful when you are working on multiple branches and you don't want to download all branches but want to get some information. 

Before you proceed make sure you understand how wild cards work in perforce command: https://www.perforce.com/manuals/cmdref/Content/CmdRef/filespecs.html 

Search Text in All Readme.txt at any subfolder level of //depot/xxx/yyy/abc/

p4 grep -i -e "mytext" //depot/xxx/yyy/abc/.../Readme.txt

or (to cover .md and .txt files)

p4 grep -s -e -i "punta" //depot/iView/GTM_Branches/Release/.../Readme.*


Search Directory by name

p4 dirs -i "//depot/xxx/yyy/abc/*/def/dir_to_search"

also,

p4 dirs -i "//depot/xxx/yyy/abc/*/def/*dir_to_search*"

for searching file, use "files" command instead of "dirs command.

Use @@1 instead of * for single level search

Get Last Check-in info for a directory

p4 changes -m 1 "//depot/xxx/yyy/abc/*/def/…"


Diff between two files

p4 diff "//depot/xxx/def.txt" "//depot/yyy/def.txt"

File Sizes by name

p4 sizes -h "//depot/.../def.txt" 




Thursday, October 07, 2021

Break the sleep and leave the scope immediately immediately!

I wanted to write a quick and dirty code... but I didn't want to make it lousy in execution. Had simple requirement: Run a piece of code in a separate thread every 6 seconds but have ability to terminate (almost) immediately. Like any other lousy programmer, I didn't want to implement a timer (which is lengthy process in  C++). So I stumbled upon a solution which uses std::future to terminate the sleep immediately and doesn't execute anything once the future times out. Even if I was using while loop instead of do..while, it would have come out of the scope immediately. I am publishing it here as my example is more clear and that "Bad bad code." is present in that solution. Happy programming!


to compile: > g++ -pthread test.cpp


#include <thread>
#include <iostream>
#include <assert.h>
#include <chrono>
#include <future>
using namespace std;
void threadFunction(std::future<void> future){
   std::cout << "Starting the thread" << std::endl;
   do {
      std::cout << "Executing the thread....." << std::endl;
      //std::this_thread::sleep_for(std::chrono::milliseconds(6000)); //bad bad code
   } while (future.wait_for(std::chrono::milliseconds(6000)) == std::future_status::timeout);
   std::cout << "Thread Terminated" << std::endl;
}
main(){
   std::promise<void> signal_exit; //create promise object
   std::future<void> future = signal_exit.get_future();//create future objects
   std::thread my_thread(&threadFunction, std::move(future)); //start thread, and move future
   std::this_thread::sleep_for(std::chrono::seconds(7)); //wait for 7 seconds
   std::cout << "Threads will be stopped soon...." << std::endl;
   signal_exit.set_value(); //set value into promise
   my_thread.join(); //join the thread with the main thread
   std::cout << "Doing task in main function" << std::endl;
}

Tuesday, September 22, 2020

Simple GroupBy Function in for Python

After a lot of research I found that Python has no good groupby function that you can use in a single line (like Linq GroupBy of C#). For that, I've made a sample reusable groupby function that works with one or more keys, I wouldn't say it has great performance (actually far from it). But, it works and it is easier to use :) 

You may want to make changes based on your input:

import copy
import itertools

def yogee_groupby(collkeys):

    copied_collection = copy.deepcopy(coll)
    master_key = ""

    if len(keys) > 1:
        copied_collection = copy.deepcopy(coll)
        for key in keys:
            master_key = master_key + key
        
        for an_element in copied_collection:
            element_key = ""
            for key in keys:
                element_key = element_key + an_element[key]
            an_element[master_key] = element_key
    else:
        master_key = keys[0]

    grouped = {}
    sorted_collection = sorted(copied_collection, key = lambda item: item[master_key])
    for k, g in itertools.groupby(sorted_collection,  lambda item: item[master_key]):
        group = list(g)
        if len(keys) > 1:
            for an_element in group:
                an_element.pop(master_key, None)
        
        grouped[k] = group
    return grouped


def test_groupby_2():
    test_data = [
        {
            "E1""V1",
            "E2""V2",
            "E3": ["V31","V32","V33","V34"],
            "E3": {"E31":"V31","E32""V32","E33""V33","E34":"V34"}
        },
        {
            "E1""W1",
            "E2""W2",
            "E3": ["W31","W32","W33","W34"],
            "E3": {"E31":"W31","E32""W32","E33""W33","E34":"W34"}
        },
        {
            "E1""V1",
            "E2""V2",
            "E3": ["VV31","VV32","VV33","VV34"],
            "E3": {"E31":"VV31","E32""VV32","E33""VV33","E34":"VV34"}
        },
        {
            "E1""V1",
            "E2""V22",
            "E3": ["X31","X32","X33","X34"],
            "E3": {"E31":"X31","E32""X32","E33""X33","E34":"X34"}
        },
    ]
    grouped = yogee_groupby(test_data, ["E1""E2"])
    print(grouped)
    grouped = yogee_groupby(test_data, ["E1"])
    print(grouped)

test_groupby_2()



Tuesday, January 26, 2016

Anonymous method with Parameters 'i' -> Thread -> Iteration with 'i' -> Mess!

Problem:
You see IndexOutOfRangeException with message  "Index was outside the bounds of the array." when you thread out an anonymous method with iterator value as parameter.


Cause:
Surprised? How is it possible to see value i >= 4 (it was 4) in the 'for' loop there?
Well, it's 'possible' if your have written code as above.

Secret is that the anonymous method doesn't get called unless the Thread created doesn't becomes live. Once thread becomes live, it searches for it's anonymous method which is: MethodToCompute(NumberOfIterationPerThread[i])
Here, value of 'i' might have changed as 'for' loop to create thread runs in a different thread!

Solution:
solution is NOT to pass any value to threaded anonymous method which the parent thread may modify. In above case, I would create an extra variable to store value to pass to anonymous method which will be threaded out.

    for (int i = 0; i < NumberOfThreads; i++)
    {
        int iterationToRun = NumberOfIterationPerThread[i]; //magic line!
        threads[i] = new Thread(new ThreadStart(() => MethodToCompute(iterationToRun)));
        threads[i].IsBackground = true;
        threads[i].Start();
    }

Wednesday, June 10, 2015

NDK Directory Location on Windows 7

I am not big fan of Android on Windows 7 but that's what I've at present and now I've to install NDK for some reason. So, I downloaded Android NDK from https://developer.android.com/ndk/downloads/index.html According to the documentation, I've to double click the NDK download and it will automatically extract file. I ran and now I don't know where it has extracted the files are. I have to mention installed NDK path (ndk.dir) in "local.properties" (btw, I am using Android Studio). After searching whole C: suing below DOS command (I saw my nkd folder was "android-ndk-r10e"),


    dir /b /s /ad | findstr "android-ndk-r10e"

Found the directory. As I've downloaded 64 bit NDK, it's

    "C:\Windows\SysWOW64\android-ndk-r10e"

on my 64bit Windows 7 machine.

Hope this helps and you don't have to search C: like me.

Friday, September 19, 2014

The Ultimate Rounding Function

Your search for any requirement for rounding function is over. You need to round amount to quarter? dime? nickel? penny? a dollar? 10 dollar? Below function of single linen can provide you more than what you are looking for. for sure!
 decimal NearestOfPenny(decimal amountToRound)
 {
     return UltimateRoundingFunction(amountToRound, 0.01m, 0.5m);
 }

 decimal NearestOfNickel(decimal amountToRound)
 {
     return UltimateRoundingFunction(amountToRound, 0.05m, 0.5m);
 }

 decimal NearestOfDime(decimal amountToRound)
 {
     return UltimateRoundingFunction(amountToRound, 0.10m, 0.5m);
 }

 decimal NearestOfQuarter(decimal amountToRound)
 {
     return UltimateRoundingFunction(amountToRound, 0.25m, 0.5m);
 }

 decimal NearestOfDollar(decimal amountToRound)
 {
     return UltimateRoundingFunction(amountToRound, 1m, 0.5m);
 }

 decimal UpwardDollarOnlyIfReminderIsMoreThan70Cents(decimal amountToRound)
 {
     return UltimateRoundingFunction(amountToRound, 1m, 0.3m); 
     //i.e. 0.70 will round up. but 0.69 will be rounded to 0.. this magic is from value of 'fairness'.
 }
 
 //amountToRound => input amount
 //nearestOf => .25 if round to quater, 0.01 for rounding to 1 cent, 1 for rounding to $1
 //fairness => btween 0 to 0.9999999___. 
 //            0 means floor and 0.99999... means ceiling. But for ceiling, I would recommend, Math.Ceiling
 //            0.5 = Standard Rounding function. It will round up the border case. i.e. 1.5 to 2 and not 1.
 //            0.4999999... Non-standard rounding function. Where border case is rounded down. i.e. 1.5 to 1 and not 2.
 //            0.75 means first 75% values will be rounded down, rest 25% value will be rounded up.
 decimal UltimateRoundingFunction(decimal amountToRound, decimal nearstOf, decimal fairness)
 {
     return Math.Floor(amountToRound / nearstOf + fairness) * nearstOf;
 }
 

Saturday, August 30, 2014

Completely Read-Only Object as Return Value

NOTE: I am not proposing a 'secure' code. It's just to make your code dumb proof, not hack proof.
At the pinnacle of the insanity, when you have that bong affecting your normal senses, you may want to return that object from your beloved method which cannot be modified in anyway. You cannot even create a new object of the class that you are returning to have exclusive access to your return type. Anyways, whatever it can be used (?) for, it's here:
using System;
using System.Collections.ObjectModel;
using System.Security.Permissions;

public class ReadOnlyClassAsReturnValue
{
    private int sampleInt;
    // make regular property read-only by creating only 'get' call 
    public int SampleInt
    {
        get { return sampleInt; }
    }

    private bool[] sampleCollection;
    // Collections need to be made ReadOnlyCollection<T> . 
    //      NOTE: Though ReadOnlyCollection<T> can be casted as IList<T>, never 
    //          do that. It allows someone to to change collection in compilte
    //          time only to realized during runtime that it cannot be changed
    public ReadOnlyCollection<bool> SampleCollection
    {
        get { return Array.AsReadOnly(sampleCollection); } //gives ReadOnlyCollection<T>
    }

    // 'private' varialbes can only be assigned in contructor. If any variable need to be assigned after
    // object is created, it need to be made 'internal'.
    protected string sampleString = "x";
    public string SampleString
    {
        get { return sampleString; }
    }

    // Contructor is made 'protected' as this class is to be used by serious consumers only.
    // If ultimate (dumb) user is going to be outside your assembly, you can make it 
    // 'internal' instead of 'prorected' eliminating need to override this class and 
    // providing more protection than 'protected' (and 'proected internal').
    protected ReadOnlyClassAsReturnValue(int sampleInt, bool[] sampleCollection)
    {
        this.sampleInt = sampleInt;
        this.sampleCollection = sampleCollection;
    }
}

public class PrimaryUserClass
{
    // Inherit ReadOnlyClassAsReturnValue so internal methods/properties/variables can be assigned
    private class ReadOnlyObjectMaker : ReadOnlyClassAsReturnValue
    {
        public ReadOnlyObjectMaker(int sampleInt, bool[] sampleCollection)
            : base(sampleInt, sampleCollection)
        { }

        public string SampleString
        {
            set
            {
                this.sampleString = value;
            }
        }
    }

    //NO ONE CAN CHANGE THIS RETURN VALUE
    public ReadOnlyClassAsReturnValue ReadOnlyClassReturner()
    {
        ReadOnlyObjectMaker returnValue = new ReadOnlyObjectMaker(10, new bool[] { true, false });
        returnValue.SampleString = "I can assign this";
        return returnValue;
    }
}

class Program
{
    static void Main(string[] args)
    {
        var returnValue = new PrimaryUserClass().ReadOnlyClassReturner();
        //returnValue cannot be modified. Why? Just for fun. Someone has to hack it to change anythingin returnValue.
    }
}

Tuesday, February 18, 2014

Counting Number of REAL lines in Your C# Code

If you are here, it means one (or more) of the three things: 1. You are the boss, 2. Your boss told you to count number of lines 3. You are a serious programmer.
Anyways, let's come to the point. Each programer is different.  Problem with just literally counting the line has many issues which this piece of code attempts to resolve as mentioned below:
0. Comments of // and /* kind are ignored.
1. A statement written in multiple line is considered single line.
2. brackets are (i.e. '{') not considered lines.
3. 'using namespace' line are ignored.


        private int CountNumberOfLinesInCSFilesOfDirectory(string dirPath)
        {
            FileInfo[] csFiles = new DirectoryInfo(txtPath.Text.Trim())
                                        .GetFiles("*.cs", SearchOption.AllDirectories);

            int totalNumberOfLines = 0;
            Parallel.ForEach(csFiles, fo =>
            {
                Interlocked.Add(ref totalNumberOfLines, CountNumberOfLine(fo));
            });
            return totalNumberOfLines;
        }

        private int CountNumberOfLine(Object tc)
        {
            FileInfo fo = (FileInfo)tc;
            int count = 0;
            int inComment = 0;
            using (StreamReader sr = fo.OpenText())
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    if (IsRealCode(line.Trim(), ref inComment))
                        count++;
                }
            }
            return count;
        }

        private bool IsRealCode(string trimmed, ref int inComment)
        {
            if (trimmed.StartsWith("/*") && trimmed.EndsWith("*/"))
                return false;
            else if (trimmed.StartsWith("/*"))
            {
                inComment++;
                return false;
            }
            else if (trimmed.EndsWith("*/"))
            {
                inComment--;
                return false;
            }

            return
                   inComment == 0
                && !trimmed.StartsWith("//")
                && (trimmed.StartsWith("if")
                    || trimmed.StartsWith("else if")
                    || trimmed.StartsWith("using (")
                    || trimmed.StartsWith("else  if")
                    || trimmed.Contains(";")
                    || trimmed.StartsWith("public") //method signature
                    || trimmed.StartsWith("private") //method signature
                    || trimmed.StartsWith("protected") //method signature
                    );
        }


There is (literally) no solution for below (un)kind of programers:
1. Write multiple statements in single line.
2. Who declare useless variables.
3.Whose every other line is debug.write
4. Inappropriate use of compiler directive (directive itself is not considered as line btw)
5. Bulk copy programmers who suffer from schizophrenia and lives only in their set of classes. Don't trust or reuse anything else.
6. Who still likes to reinvent the wheel.

Apart from above six non-solvable issues, if you can improve my code, it will be helpful to everyone.

Thursday, April 04, 2013

Firefox 20 Feature - Click To Play

If you, my friend, are annoyed by zillions of flash advertisements running on your browser for which you haven't subscribed, have a look at the latest version of Firefox 20. It has a hidden lovable feature called "Click To Play". If enabled, it will deactivate all Flash (and Java) plugins except the one which you select to activate in a page. I've a netbook ('cheap laptop') which cannot handle the army of flash advertisements so I chose to activate this hidden Click_To_Play feature on my recently updated Firefox.

How to Enable Click_To_Play:

  1. Open a new firefox tab and type "about:config" in address bar.
  2. Click on button which says "I will be careful" etc..
  3. Now search for Click_To_Play in "Search" box.
  4. Double click preference named "plugins.click_to_play". It will change value of the preference to 'true'.
  5. You are done. Go to any web site and click only the plugin which you know you want to activate.  
Happy Browsing and don't let the Flash ads bite!

Friday, February 15, 2013

Auto Mouse Click and Drag Generator

If you are looking for a simple but powerful fast mouse clicker application, you are not alone. I am growing old and the software written by kids are too difficult for me to use, so I wrote a simple one called Auto Mouse Click and Drag Generator by myself which suite my work style - use keyboard when you automate mouse.

Here is from where you can try yourself: https://sourceforge.net/projects/superclick/files/latest/download?source=directory

Source Code can be found at project home: https://sourceforge.net/projects/superclick/?source=directory

Dedicated blog location is: http://automouseclicker.blogspot.in/
Below are the screens of the software as of today. Latest version may have more features and better UI. I don't intend to post update on this blog as the software has it's own dedicated blog.

Happy clicking and crashing others' software!



Unity3D and Vector Graphics

SVG is a popular vector graphics format. It has same limitation and advantages like any other vector graphics formats but SVG is very popular (and it's open format). Actually most modern browsers (and mobile devices) support it. Also, a graphics developer can create complex vector graphics using various tools available and need not to be programmed. They are not there to replace all raster format but in some specific cases, it may be more advantageous to use vector graphics. One of the major advantage of the vector graphics in general is that it looks crisp regardless of resolution, and they are less in size (well, not all the times). They may take same amount of memory and little more processing power but sometime they are worth it.

After thinking about all these advantages, I went look for unity plugin that can support direct rendering of SVG file or converts it to texture in runtime and show it and I found this:

http://wiki.unity3d.com/index.php?title=SVG

This one is still in its basic form and not yet practical to use. I hope something along the way come to make Unity use 2D vector rendering easier.

The other one that I stuck during my search was:
http://virtualplayground.d2.pl/?p=241
This one works with basic shapes and creates triangles out of vector forms which is more Unity friendly. It's nice thing but the implementation as of now has very limited features and most probably will not satisfy your needs.

Btw, as you may know, Flash already supports vector rendering (proprietary format) and this is the main reason why Flash games are very light in size and still looks crisp.

Tuesday, September 11, 2012

Extract BINK to PNG Sequence (with transparency)


BINK movies are widely  used in games because they provide transparency in the video from long back. Sometime you have to reverse engineer the BINK file and get the PNGs out of (not JPEG but PNG with  transparency) the movie. I've used FFMpeg to get PNGs out of BINK (.bik format) file as followed.

1. Download latest ffmpeg static build from http://ffmpeg.zeranoe.com/builds/
2. Extract the .7z build to any folder.
3. Give this command to convert “my.bik” to “output” folder and %d is sequence number in output image.  %d will auto increment by one, you have to just give %d in the command.
ffmpeg.exe -i D:\my.bik D:\output\my_%d.png
Note that this is a general command which applies to any type of movie format supported by ffmpeg. There are hell lot of more things you can with ffmpeg. For example, your popular VLC is built using ffmpeg library :) 

Tuesday, May 08, 2012

When your 'mktime' is slow as shit

While working on Vanilla Forum, I realized that dashboard was taking around 3 seconds to load on LAN. When looked further using Xdebug and cachegrind log, I realized that a method called "UnixTimestamp" (in Framework.Functions.php) was calling a stupid method called "mktime". What it does is just return the timestamp value (elapsed int value from a stupid start date of C which will create problem 2030). Not sure why on the earth this should happen, but this method is utter slow. So slow that when I wrote got the UTC time from my MySQL server (of local machine), it was 3 time fast.

Conclusion:
If your mktime is slow, throw that shit out and use stupider but faster method.. i.e. tell MySQL to do it for you. Here is what I wrote:

 
  $query = sprintf("SELECT UNIX_TIMESTAMP('%s') as UTS",
     mysql_real_escape_string($DateTime));
  $result = mysql_query($query);
  if($row = mysql_fetch_assoc($result)) {
     return $row['UTS'];
  }
May PHP god have mercy on you! Happy Coding!

Friday, March 16, 2012

SqlDateTime overflow Exception with NHibernate

SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.
Exception Stacktrace:
at System.Data.SqlTypes.SqlDateTime.FromTimeSpan(TimeSpan value)
at System.Data.SqlTypes.SqlDateTime.FromDateTime(DateTime value)
at System.Data.SqlClient.MetaType.FromDateTime(DateTime dateTime, Byte cb)
at System.Data.SqlClient.TdsParser.WriteValue(Object value, MetaType type, Byte scale, Int32 actualLength, Int32 encodingByteSize, Int32 offset, TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.TdsExecuteRPC(_SqlRPC[] rpcArray, Int32 timeout, Boolean inSchema, SqlNotificationRequest notificationRequest, TdsParserStateObject stateObj, Boolean isCommandProc)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at NHibernate.AdoNet.AbstractBatcher.ExecuteNonQuery(IDbCommand cmd)
at NHibernate.AdoNet.NonBatchingBatcher.AddToBatch(IExpectation expectation)
at NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object id, Object[] fields, Object[] oldFields, Object rowId, Boolean[] includeProperty, Int32 j, Object oldVersion, Object obj, SqlCommandInfo sql, ISessionImplementor session)
at NHibernate.Persister.Entity.AbstractEntityPersister.UpdateOrInsert(Object id, Object[] fields, Object[] oldFields, Object rowId, Boolean[] includeProperty, Int32 j, Object oldVersion, Object obj, SqlCommandInfo sql, ISessionImplementor session)
What is NOT a problem:
As always, I opned SQL Profiler and tried solve the issue but it misguided me by adding few extra zeros in millisecond part of datetime (I don't want to discuss it and it is not the problem).

What IS (or was in my case) problem:
I added an extra Datetime filed which was nullable in database. But I was mapping it to DateTime in my database. When NHibernate was converting my unset Datetime to SQLdatetime for mysterious reason, it was trying to set DateTime.Min in SQL datetime datatype which is not allowed (see exception).

Solution:
Look for any Datetime in your mappings that should be nullabe (according to database). And then use
type="System.Nullable`1[[System.DateTime, mscorlib]], mscorlib" in your mapping.

Thursday, December 01, 2011

Floating/Fixed Table Header in HTML Page or Inside DIV control

I’ve looked a lot for finding a solution were we can float a header of a table so that the header always appears on the top no matter how down do I scroll.
All the ‘floating table header’ solution ‘mostly’ worked were full HTML body is getting the scroll but none of them worked which can work inside scrolling div. None!
So I though I can create one. The concept was simple, get the ‘onScroll’ even of Div control and change CSS “top” property of table header to current scrolled position of Div. 
Unfortunately, there are many browser issues. Like mozilla doesn’t like when table cells change their position. Chrome rendering issues shows two headers rows when scrolled up.
At last, after storming through the problems, I arrived on this complex but simplified solution which is mentioned below. Copy the content in a blank HTML file and see the fun.

<html> 
    <head> 
        <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript" > </script> 
        <script  type="text/javascript"> 
            $(function() { 
                if($.browser.mozilla) 
                    //table row doean't float in firefox, div floats 
                    $(".floatingHeader" + " tr th div") 
                            .addClass("floatingStyle"); 
                else 
                    //table row can float in IE and Chrome 
                    $(".floatingHeader"+ " tr th") 
                            .addClass("floatingStyle"); 
            }); 
            function changeFloatingHeaderPosition(container, headerId) { 
                    if($.browser.webkit) //chrome rendering bug fix 
                        $("#"+headerId + " tr th") 
                            .css("visibility", "hidden"); 
                    if($.browser.mozilla) 
                        $("#"+headerId + " tr th div") 
                            .css("top", container.scrollTop); 
                    else 
                        $("#"+headerId + " tr th") 
                            .css("top", container.scrollTop); 
                    if($.browser.webkit) //chrome rendering bug fix 
                        $("#"+headerId + " tr th") 
                            .css("visibility", "visible"); 
            } 
     
      // Remove this method if you are using Jqeury earlier than 1.9
     (function() {
      var matched, browser;

      // Use of jQuery.browser is frowned upon.
      // More details: http://api.jquery.com/jQuery.browser
      // jQuery.uaMatch maintained for back-compat
      jQuery.uaMatch = function( ua ) {
   ua = ua.toLowerCase();

   var match = /(chrome)[ \/]([\w.]+)/.exec( ua ) ||
       /(webkit)[ \/]([\w.]+)/.exec( ua ) ||
       /(opera)(?:.*version|)[ \/]([\w.]+)/.exec( ua ) ||
       /(msie) ([\w.]+)/.exec( ua ) ||
       ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec( ua ) ||
       [];

   return {
       browser: match[ 1 ] || "",
       version: match[ 2 ] || "0"
   };
      };

      matched = jQuery.uaMatch( navigator.userAgent );
      browser = {};

      if ( matched.browser ) {
   browser[ matched.browser ] = true;
   browser.version = matched.version;
      }

      // Chrome is Webkit, but Webkit is also Safari.
      if ( browser.chrome ) {
   browser.webkit = true;
      } else if ( browser.webkit ) {
   browser.safari = true;
      }

      jQuery.browser = browser;
  })();
        </script> 
        <style> 
            .floatingStyle 
            { 
                position:relative; 
                background-color:#829DC0; 
                top:0px; 
            } 
        </style> 
    </head> 
<body> 
    <div class="floatingContainer"     onscroll="changeFloatingHeaderPosition(this, 'idHeader' );"      style="height:150px; width: 100px;overflow:auto;"> 
        <table cellspacing=0 cellpadding=0> 
            <thead class="floatingHeader" id="idHeader"> 
                <tr> 
                    <th><div>Col1</div></th>                     <th ><div>Col2<div></th> 
                    <th ><div>Col3<div></th> 
                </tr> 
            </thead> 
            <tbody> 
                <tr><td>first_row</td><td>first_row</td><td>first_row</td></tr> 
                <tr><td>data</td><td>data</td><td>data</td></tr> 
                <tr><td>data</td><td>data</td><td>data</td></tr> 
                <tr><td>data</td><td>data</td><td>data</td></tr> 
                <tr><td>data</td><td>data</td><td>data</td></tr> 
                <tr><td>data</td><td>data</td><td>data</td></tr> 
                <tr><td>data</td><td>data</td><td>data</td></tr> 
                <tr><td>data</td><td>data</td><td>data</td></tr>             </tbody> 
        </table> 
</div></body><html>
Note: Dan/Kenji has written plugging for the same which might be useful to you. I've not verified if it works in all case but worth having a look. You can find it: here: http://www.redkitetechnologies.com/2013/03/floatingsticky-headers-for-visualforce-pageblocktable/

Tuesday, November 15, 2011

Find Missing Foreign Key References

If you database column naming convention says all foreign key columns must end with 'ID' then this is the query for you to find missing foreign key reference. E.g. PRIZE_ID means Foreign Key reference to PRIZE table.
SELECT 
    colT.TABLE_NAME , 
    colT.COLUMN_NAME 
FROM 
    INFORMATION_SCHEMA.COLUMNS AS colT 
WHERE 
    COLUMN_NAME LIKE '%id'
    AND 
    (colT.TABLE_NAME + colT.COLUMN_NAME) NOT IN
     ( SELECT 
          KOM.TABLE_NAME + KOM.COLUMN_NAME 
      FROM 
          INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE AS KOM)

Monday, June 20, 2011

Amazon SimpleDB in Nutshell for those who know RDBMS Systems

Note: Latest Available Amazon SimpleDB version as of today, i.e. when I am writing this, is in Beta status with API Version: “2009-04-15”

Overview of SimpleDB
Amazon SimpleDB is a highly available, flexible, and scalable non-relational data store that offloads the work of database administration. Developers simply store and query data items via web services requests.[1]

Representation of Data with SimpleDB:

Domains— Domains are similar to tables that contain similar data.

You can execute queries against a domain, but cannot execute queries across different domains.

Attributes— They are similar to columns in RDBMS, attributes represent categories of data that can be assigned to items.

Items— Represented by rows, items represent individual objects that contain one or more attribute name-value pairs.

Values—Similar to column value, values represent instances of attributes for items. An attribute can have multiple values. There is no data typing supported for attribute and all data is treated as text data during query execution.

However, Amazon SimpleDB is not a relational database, and does not offer some features needed in certain applications, e.g. complex transactions or joins (i.e. execute queries across different domains).[1] You need to rely on duplicating the data to avoid such scenarios.[2]

Benefits of using SimpleDB

Highly Available
Amazon SimpleDB creates and manages multiple geographically distributed replicas of your data automatically to enable high availability and data durability.
Flexible
You can change your data model on the fly, and data is automatically indexed for you.
Scalability
You can access additional machine resources by spreading your data set and requests across multiple domains.

Support for Reporting
Sql Server Reporting Service (SSRS) is a popular platform to build and access reports. SSRS reports are build based on dataset that has set for a report. It is possible to build dataset from SimpleDB.[3] 

Limitation with SimpleDB

Domain (similar to table): 250 active domains per account. More can be requested by filling a form. Note that each attribute (similar to column) can hold multiple value.[4]

Attribute (similar to column) name-value pairs per item: 256[4]

Maximum response size for Select: 1 MB (large data like images can be stored separately into cloud as files and an Attribute can store URL for the resource).[4]

Maximum items per select: 2500[4]

Attribute value length: 1024 bytes[4]

No datatyping: text only. Integers and reals must be represented using leading zeros to ensure proper query comparisons.[5]

References:

[1] Amazon SimpleDB (beta)
http://aws.amazon.com/simpledb/

[2] How And Why Glue Is Using Amazon SimpleDB Instead Of A Relational Database
http://blog.getglue.com/?p=1145

[3] Accessing SimpleDB from SSRS
http://www.chrisumbel.com/article/simpledb_ssrs.aspx

[4] Amazon SimpleDB Limits, Amazon SimpleDB Developer Guide (API Latest version)
http://docs.amazonwebservices.com/AmazonSimpleDB/latest/DeveloperGuide/

[5] M/DB - A Free Open Source "plug-compatible" alternative to Amazon's SimpleDB database
http://gradvs1.mgateway.com/main/index.html?path=mdb