Text database --.Net industry a wonderful work of the future

Text storage, this is the one I rarely go to the implementation of specific areas of application before.

 

How will talk about text database from Part I: Technical Principle Autumn Park QBlog: performance optimization articles: separate read and write text database (XVIII)  , a seen out of the application, and therefore have the urge to dash.

 

Of course, from the above, it can be seen: the basic text-based application write simple, gently and strictly speaking a little "Database" not the edge state is not enough.

 

I thought "text database", no matter how simple, they have it should have the following:

 

1 : a memory structure [Table Structure]

2 : primary key ID, not when the GUID, ye have to right across the self-energizing ID (application habit everyone).

3 : Can add, modify, delete data.

4 : The best can query, sorting, paging, as the grouping may require a high point.

5 : There is preferably further concurrency control.

 

Look back Autumn Park QBlog  current applications are:

 

1 : Less contents of the text stored: a table is not stored text, basically one line (occasionally multiple lines).

2 : 1 basis point, can only be applied to read the text, and "database" has nothing to do.

 

From the above it is the current application, not entirely up to the realm of text database applications.

 

Of course, with text-based applications, and therefore the logical extension of a text "Text Database" to generate a little interest and impulse and very natural.

 

So, I search the Internet a bit, "the text database" and found almost no .net community in its presence, but added php community had let EXTRAORDINARY SPLENDOR large, this is why?

 

Why is this? Presumably to the following:

 

When the basic advantages of using text databases:

 

1 : Simple text manipulation faster than database:

have to explain, I can only say: everyone is saved disk, direct deposit a certain amount of certainty around the storage structure of the rules after the deposit faster than the database, of course, the more backward the more complex , not easy to say.

2 : simple access operation:

do what ADO.NET, direct System.IO.File can handle, easy multi-ah!

 

When the basic disadvantage with text databases:

 

1 : not suitable for large amounts of data.

2 : Concurrent does not seem good

3 : delete and modify operation is not good

4 : To transform into a "text database" There is a lot to deal with something important:

A: This is an increase from ID loud noise out ah?

b: To find this Zezheng ah?

c: To sort this Zezheng ah?

 

Basic application scenarios with the applicable text databases:

 

1 : small applications, text-oriented database:

single (table) small amount of text data: the following data 100,000, 10M size of the following (the disposable loading operation into memory, it would be a database memory, speed crashed) .

2 : large-scale application, supplemented text database:

by some database dispersed fragmented text, by reducing the pressure in the primary database.

3 : medium-sized applications, text-oriented database:

this is not to say that bad, not that good, need to have some strong technical support team. 

 

 

In order to better play "text database" initiative, I spent some time its a little research and thinking, and everyone to share the following experience:

 

1: storage structure

 

I am from this Autumn park  QBlog  resulting text application tips, save as json, very good ah, this is not what json are popular, read directly spread to the front, doing love doing!

So here is the point of technology: How to read json and json output resolution.

Here: CYQ.Data  . V3
0  frame open source version ( downloaded ) in a JsonHelper, can generate data Json, json string can be parsed into KeyValue key value, may be of interest to study.

So, basic reading and taking solved.

 

2: Data Insert

 

Use: System.IO.File.AppendAllText can be easily added to the end of the line json text.

 

3: update, delete

 

These two operations are almost the same, in two ways individuals think of:

One way: simply type [this actually quite good, so from the point of view text database application scenarios, the basic requirements are not too high]

will re-export the entire table json , once again rewrite the text on it.

Second way: Complex [This is a little more performance considerations for text database pursuit had some too little, because if it's too complicated, why not look for other databases, text does not plan a simple what]

the more painful, there is also gives the idea of a personal idea:

1 : when a given table structure, must be a good length of each field, so you fix a total maximum line length.

2 : When you write a line of data, the total length of time is not enough, back up empty (usually written like \ 0).

3 : when updated or deleted according to (ID-1) * total length of line, positioned to the starting position of the writing, and then rewritten to a line, if the full row is deleted when the write null (\ 0).

In fact, here is space for time, and when data is deleted, the text size did not change, is not it a bit like access it?

 

Two pins in the above manner, the point here is sample code:

 

                FS the FileStream  =  File.Open (file path);
                fs.Seek (
start position location to be written , SeekOrigin.Current);
                fs.Write (content written .. ...);
                fs.Close ();

 

4: increment ID gnaw out

 

Increase the ID is relatively easy, read the value of the first field of the last line of text + 1 came out, of course, only suitable for digital type, and then the global cache, each read ++ .

 

The following sample code is given a reference:

 

  /// <Summary> ///  next increase from ID /// </ Summary> Private int  the NextID         { GET             { Lock  (lockNextIDobj)                 { IF  (maxID  > 0 )                     {                         maxID ++ ;                     } the else IF  (DataType.GetGroupID (Table.Columns [ 0 ] .SqlType)  == . 1 ) // increment int ID only valid                     { IF  (Table.Rows.Count  > 0 )  
        

        
 
          

            


                


                    
 



                    
   

                        
 
                        {
                            
int  lastIndex  =  _Table.Rows.Count  -   1 ;
                            
do
                            {
                                
if  (lastIndex  >=   0 )
                                {
                                    
if  (_Table.Rows[lastIndex][ 0 ].IsNull)
                                    {
                                        lastIndex
-- ;
                                    }
                                    
else
                                    {
                                        maxID 
=  Convert.ToInt32(_Table.Rows[lastIndex][ 0 ].Value)  +   1 ;
                                    }
                                }
                                
else
                                {
                                    maxID 
=   1 ;
                                }
                            }
                            
while  (maxID  ==   0 );

                        }
                        
else
                        {
                            maxID 
=   1 ;
                        }
                    }
                    
else
                    {
                        
throw   new  Exception( " Increment id only use for int type " );
                    }
                }
                
return  maxID;
            }
        }

 

5: Query how to do

 

After the fact, a good run, will be reduced to parse json array list, there is an array FindAll method, the search point tutorial look at it, to query the array, the garden is still a lot article describes.

 

6: Sorting how to do

 

This is also very good to do the same after parsing json reduced to an array of lists, arrays have a Sort method, search the same point tutorial on it.

 

7: Single-process concurrency control how

 

This fact is also easy to handle, plus lock lock on the line.

 

8: multi-process concurrency control how

 

When IIS application pool recycling or enable multiple exe program, multi-process situation may arise while manipulating text database, and here I have to think for a long time, how to control?

Finally, I want to be out of the way: in the process of preparing to change the text, read text last modified time for comparison, thus achieving a relatively controlled.

 

to sum up:

 

After personally feel completely solve the above problem, basically forming a simple text database also, of course, you can also continue to pursue up.

However, a text database, toss did not need too complicated, after all text database, or to simply dominated.

If NoSql will be popular, why not let text database also .net community also out of the limelight, to grow into a .Net world a wonderful work!

 

Reproduced in: https: //my.oschina.net/secyaher/blog/274218

Guess you like

Origin blog.csdn.net/weixin_34223655/article/details/91967124