Friday, January 10, 2014

Opening File in exclusive or shared mode

Sometimes you need to read file that can be used by other processes as well and in that case you if you just want to read the content of the file and let the other process to write in it, you actually have to open a file in shared mode. Code below can allow you to open a file in shared mode.

"FileShare.ReadWrite" enum allow you to specify the level of lock you want on the file. Here it will allow other processes to read and write to the file/

using (Stream s = new FileStream(fullFilePath,
                                 FileMode.Open,
                                 FileAccess.Read,
                                 FileShare.ReadWrite))
{
  // here use the stream s
}

In case if you don't want other processes to interfere or read your file you can specify "FileShare.None" which will acquire exclusive access to the file.


using (Stream s = new FileStream(fullFilePath,
                                 FileMode.Open,
                                 FileAccess.Read,
                                 FileShare.None))
{
  // here use the stream s
}

 

Parallel Processing with Defined Degree of Parallelism using Task Parallelism

Sometimes you really want to achieve very simple task but when it comes to program it, you end up thinking about dead locks, thread sync, max threads, processing time etc. I have faced a similar situation where i want to process some items mutually exclusive with each other but i can't process more than 4 items at a time. so i end up using Task Parallelism which makes it really easy and understandable. Other way of doing this is by using ThreadPool and ManualRest locks that is you can say a retro fit for this problem.

To illustrate the problem, let assume you want to parse the html content of a list of urls on a site but you can't process more than 4 urls at a time to avoid overloading your web server and bandwidth. Here is how it can be easily achieved using Task Parallelism:


          //fetch all list of urls to scan.
           List<string> urlsToScan = GetUrlsToScan();
           
            //apply parallel foreach on every url but with Degree of Parallelism to 4 threads at a time.
            Parallel.ForEach(urlsToScan,
                new ParallelOptions()
                {
                    MaxDegreeOfParallelism = 4
                }, (s) =>
                {
                    Console.WriteLine("{0} - Processing url: {1}", DateTime.Now, s);
                    ScrapUrl(s);
                });

Hope this will help a lot to solve many such problems very easily. Happy Coding!!

Monday, April 22, 2013

Shrink Log file SQL Server

After prolong usage of heavy DB transactions and CURD operation MS SQL server DB log file starts getting large. This file is important for the DBA because it determine which recovery method your DB is using but in my case as a developer i don't really have to maintain recovery methods so i rather use to shrink my log file often. Here is the quick snippet to do so so this will help you guys:

USE Northwind
GO
Print 'Set Recovery Mode To Simple'
ALTER DATABASE Northwind SET RECOVERY SIMPLE;
Go
Print 'Set Single User Mode'
ALTER DATABASE Northwind SET SINGLE_USER WITH ROLLBACK IMMEDIATE
Go
print 'Shrink Log File'
Declare @FileName As Varchar(100)
Set @FileName=(SELECT name From sys.database_Files where type=1)
DBCC SHRINKFILE(@FileName, 1)
BACKUP LOG Northwind WITH TRUNCATE_ONLY
DBCC SHRINKFILE(@FileName, 1)
Go
Print 'Set Recovery Back To Full'
ALTER DATABASE Northwind SET RECOVERY FULL;
Go
Print 'Set User Mode To Multi'
ALTER DATABASE Northwind SET MULTI_USER WITH NO_WAIT
Go

Monday, January 7, 2013

A Simple Text File logger for .Net apps

Sometimes you just need a simple kick start while debugging application. Once such scenario is to add simple file based logging. I have created a very simple file based Logger just to add some info to text file and comes handy when you actually want to look at the trace of events specially in a multi-threaded environment that is a little difficult to debug at runtime.

public class Logger
    {
        public static void Log(string str)
        {
            var file = Path.Combine(HttpContext.Current.Server.MapPath(@"~\"), "log.txt");
            System.IO.File.AppendAllText(file, string.Format("{0}-{1}{2}",DateTime.Now, str, Environment.NewLine));
        }
    }

Friday, August 3, 2012

T-SQL script to find out which table is locked

This is a handy script and it is common in some scenarios that you get your tabled locked due to some deadlock condition and results in unavailability of such table in whole application. There are other ways to find out e.g: using SSMS can give you handful details of what you want but in case if you want some custom parameters with custom query you will have to understand which tables MS SQL server provides such information. There are tables in sys called dm_tran_locks that shows how many translations running in current system this tables gives us a lot of information but most of it is ids so to get descriptive values you will have to join these with other sys tables like 'objects', 'waiting_tasks' etc.. Hope this will help other as well.


SELECT t1.resource_type,
       t1.resource_database_id,
       t1.resource_associated_entity_id,
       t1.request_mode,
       t1.request_session_id,
       t2.blocking_session_id,
       o1.name         'object name',
       o1.type_desc    'object descr',
       p1.partition_id 'partition id',
       p1.rows         'partition/page rows',
       a1.type_desc    'index descr',
       a1.container_id 'index/page container_id'
FROM   sys.dm_tran_locks AS t1
       INNER JOIN sys.dm_os_waiting_tasks AS t2
               ON t1.lock_owner_address = t2.resource_address
       LEFT OUTER JOIN sys.objects o1
                    ON o1.object_id = t1.resource_associated_entity_id
       LEFT OUTER JOIN sys.partitions p1
                    ON p1.hobt_id = t1.resource_associated_entity_id
       LEFT OUTER JOIN sys.allocation_units a1
                    ON a1.allocation_unit_id = t1.resource_associated_entity_id 

Thursday, July 26, 2012

Easiest way to find how good is your software team

The Joel Test
  1. Do you use source control?
  2. Can you make a build in one step?
  3. Do you make daily builds?
  4. Do you have a bug database?
  5. Do you fix bugs before writing new code?
  6. Do you have an up-to-date schedule?
  7. Do you have a spec?
  8. Do programmers have quiet working conditions?
  9. Do you use the best tools money can buy?
  10. Do you have testers?
  11. Do new candidates write code during their interview?
  12. Do you do hallway usability testing?


The neat thing about The Joel Test is that it's easy to get a quick yes or no to each question. You don't have to figure out lines-of-code-per-day or average-bugs-per-inflection-point. Give your team 1 point for each "yes" answer. The bummer about The Joel Test is that you really shouldn't use it to make sure that your nuclear power plant software is safe.

A score of 12 is perfect, 11 is tolerable, but 10 or lower and you've got serious problems. The truth is that most software organizations are running with a score of 2 or 3, and they need serious help, because companies like Microsoft run at 12 full-time.

Of course, these are not the only factors that determine success or failure: in particular, if you have a great software team working on a product that nobody wants, well, people aren't going to want it. And it's possible to imagine a team of "gunslingers" that doesn't do any of this stuff that still manages to produce incredible software that changes the world. But, all else being equal, if you get these 12 things right, you'll have a disciplined team that can consistently deliver.

Thursday, July 19, 2012

T-SQL RANK,Row_Number,NTile and Dense_Rank

Rank():
Returns the rank of each row within the partition of a result set. The rank of a row is one plus the number of ranks that come before the row in question.
Usage: RANK ( )    OVER ( [ < partition_by_clause > ] < order_by_clause > ) 
Dense_Rank() :
Returns the rank of rows within the partition of a result set, without any gaps in the ranking. The rank of a row is one plus the number of distinct ranks that come before the row in question.
Usage: DENSE_RANK ( )    OVER ( [ < partition_by_clause > ] < order_by_clause > ) 
Ntile():
Distributes the rows in an ordered partition into a specified number of groups. The groups are numbered, starting at one. For each row, NTILE returns the number of the group to which the row belongs.
Usage: NTILE (integer_expression)    OVER ( [ <partition_by_clause> ] < order_by_clause > ) 
Row_Number():
Returns the sequential number of a row within a partition of a result set, starting at 1 for the first row in each partition.
ROW_NUMBER ( )     OVER ( [ <partition_by_clause> ] <order_by_clause> ) 
Example 

All Recordset :  
select * from finals 
RecordID Name Surname Course Point
1 Bugra Postaci Mathematics 89
2 Bugra Postaci Lecture 90
3 Dany Lowe Mathematics 75
4 Dany Lowe Lecture 85
5 Alice Marcel Mathematics 77
6 Alice Marcel Lecture 100
7 Simon Duru Mathematics 45
8 Simon Duru Lecture 58

select row_number() over(order by RecordID desc) as RowNumber ,* from finals 

RowNumber RecordID Name Surname Course Point
1 8 Simon Duru Lecture 58
2 7 Simon Duru Mathematics 45
3 6 Alice Marcel Lecture 100
4 5 Alice Marcel Mathematics 77
5 4 Dany Lowe Lecture 85
6 3 Dany Lowe Mathematics 75
7 2 Bugra Postaci Lecture 90
8 1 Bugra Postaci Mathematics 89

select ntile(2) over(order by RecordID desc) as [Ntile] ,* from finals 

Ntile RecordID Name Surname Course Point
1 8 Simon Duru Lecture 58
1 7 Simon Duru Mathematics 45
1 6 Alice Marcel Lecture 100
1 5 Alice Marcel Mathematics 77
2 4 Dany Lowe Lecture 85
2 3 Dany Lowe Mathematics 75
2 2 Bugra Postaci Lecture 90
2 1 Bugra Postaci Mathematics 89

as you see ntile function just divide the scope with given number 
select rank() over (order by Surname) as [Rank], * from finals 
Rank RecordID Name Surname Course Point
1 7 Simon Duru Mathematics 45
1 8 Simon Duru Lecture 58
3 3 Dany Lowe Mathematics 75
3 4 Dany Lowe Lecture 85
5 5 Alice Marcel Mathematics 77
5 6 Alice Marcel Lecture 100
7 1 Bugra Postaci Mathematics 89
7 2 Bugra Postaci Lecture 90

rank function is working like  match one ”Duru” as 1 match another “Duru” as 1 but total count as 2 , next surname matches “Lowe” as count +1 as 3 , another “Lowe” as 3  ; now total count is 4 matching next surname as “Marcel” is count + 1 as 5 and goes on …. 

select dense_rank() over (order by Surname) as [DenseRank], * from finals 

DenseRank RecordID Name Surname Course Point
1 7 Simon Duru Mathematics 45
1 8 Simon Duru Lecture 58
2 3 Dany Lowe Mathematics 75
2 4 Dany Lowe Lecture 85
3 5 Alice Marcel Mathematics 77
3 6 Alice Marcel Lecture 100
4 1 Bugra Postaci Mathematics 89
4 2 Bugra Postaci Lecture 90
 Dense rank its obvious like shown. 

if you want to make your denserank number reseting by groups there is a way use PARTITION BY
below example reseting ranks by using “partition by” by course column
select
dense_rank() over (Partition by course order by Surname) as [DenseRank], * from finals 
 

DenseRank RecordID Name Surname Course Point
1 8 Simon Duru Lecture 58
2 4 Dany Lowe Lecture 85
3 6 Alice Marcel Lecture 100
4 2 Bugra Postaci Lecture 90
1 7 Simon Duru Mathematics 45
2 3 Dany Lowe Mathematics 75
3 5 Alice Marcel Mathematics 77
4 1 Bugra Postaci Mathematics 89
Thats all folks…


Original Article: http://blog.bugrapostaci.com/2010/01/26/t-sql-rank-dense_rank-ntile-row_number/