SharpFileDB - a file database for small apps

本文中文版在此处

I'm not an expert of database. Please feel free to corect my mistakes.

This article (http://www.cnblogs.com/gaochundong/archive/2013/04/24/csharp_file_database.html) helpes a lot. Thank you!

目标(Goal)

I've decided to write a micro database library that spports 10 thousand level applications.

It's better not to rely on other drives or tools in case of high difficulty in deployment. After all, it's a small database for a small application.

It's a DLL from totally C# codes. It's easy for reading and using.

It supports CRUD of course.

It doesn't use any SQL. I'm not familiar with SQL and I don't like it. And it's not necessary to use SQL here.

It saves data via plain text files or binary files. It's convenient to use plain text files when developing and debugging codes. And it's safer to use binary files at deployment time.

So, simply, it's a micro database library that uses no SQL, files as storage form and totally C# to implement a CRUD system. Let's name the library SharpFileDB.

I've put the newest project on Github. All codes in the library are noted in both chinese and engilsh. I hope that will help to communicate.

设计草图(sketch)

使用场景(User Scene)

The typical user scene of SharpFileDB is as follows.

                 // common cases to use SharpFileDB.
FileDBContext db = new FileDBContext(); Cat cat = new Cat();
cat.Name = "xiao xiao bai";
db.Create(cat); Predicate<Cat> pre = new Predicate<Cat>(x => x.Name == "xiao xiao bai");
IList<Cat> cats = db.Retrieve(pre); cat.Name = "xiao bai";
db.Update(cat); db.Delete(cat);

This routine contains lines that create a database and uses CRUD operations.

Let's start the first version of SharpFileDB according to this user scene.

表vs类型(Table vs Type)

Let's take the type 'Cat' as an example.

     /// <summary>
/// demo file object
/// </summary>
public class Cat : FileObject
{
public string Name { get; set; }
public int Legs { get; set; } public override string ToString()
{
return string.Format("{0}, Name: {1}, Legs: {2}", base.ToString(), Name, Legs);
}
}

The type 'Cat' is equivalent to a 'Table' in a relational database.

An instance of 'Cat' is equivalent to a record of a 'Table'.

Lets' call types like 'Cat' a table-type.

全局唯一的主键(global unique main key)

We need a global unique main key for every instance of a table-type to diffentiate them. So let's do this in an abstract class.

     /// <summary>
/// 可在文件数据库中使用CRUD操作的所有类型的基类。
/// Base class for all classed that can use CRUD in SharpFileDB.
/// </summary>
[Serializable]
public abstract class FileObject
{
/// <summary>
/// 主键.
/// main key.
/// </summary>
public Guid Id { get; set; } /// <summary>
/// 创建一个文件对象,并自动为其生成一个全局唯一的Id。
/// <para>Create a <see cref="FileObject"/> and generate a global unique id for it.</para>
/// </summary>
public FileObject()
{
this.Id = Guid.NewGuid();
} public override string ToString()
{
return string.Format("Id: {0}", this.Id);
}
}

数据库(FileDBContext)

All table-types' CRUD operations are done in a FileDBContext.

     /// <summary>
/// 文件数据库。
/// Represents a file database.
/// </summary>
public class FileDBContext
{
#region Fields /// <summary>
/// 文件数据库操作锁
/// <para>database operation lock.</para>
/// </summary>
protected static readonly object operationLock = new object(); /// <summary>
/// 文件数据库
/// <para>Represents a file database.</para>
/// </summary>
/// <param name="directory">数据库文件所在目录<para>Directory for all files of database.</para></param>
public FileDBContext(string directory = null)
{
if (directory == null)
{
this.Directory = Environment.CurrentDirectory;
}
else
{
Directory = directory;
}
} #endregion public override string ToString()
{
return string.Format("@: {0}", Directory);
} #region Properties /// <summary>
/// 数据库文件所在目录
/// <para>Directory of database files.</para>
/// </summary>
public virtual string Directory { get; protected set; } #endregion protected string Serialize(FileObject item)
{
using (StringWriterWithEncoding sw = new StringWriterWithEncoding(Encoding.UTF8))
{
XmlSerializer serializer = new XmlSerializer(item.GetType());
serializer.Serialize(sw, item);
string serializedString = sw.ToString(); return serializedString;
}
} /// <summary>
/// 将字符串反序列化成文档对象
/// </summary>
/// <typeparam name="TDocument">文档类型</typeparam>
/// <param name="serializedFileObject">字符串</param>
/// <returns>
/// 文档对象
/// </returns>
protected TFileObject Deserialize<TFileObject>(string serializedFileObject)
where TFileObject : FileObject
{
if (string.IsNullOrEmpty(serializedFileObject))
throw new ArgumentNullException("data"); using (StringReader sr = new StringReader(serializedFileObject))
{
XmlSerializer serializer = new XmlSerializer(typeof(TFileObject));
object deserializedObj = serializer.Deserialize(sr);
TFileObject fileObject = deserializedObj as TFileObject;
return fileObject;
}
} protected string GenerateFileFullPath(FileObject item)
{
string path = GenerateFilePath(item.GetType());
string name = item.GenerateFileName();
string fullname = Path.Combine(path, name);
return fullname;
} /// <summary>
/// 生成文件路径
/// </summary>
/// <typeparam name="TDocument">文档类型</typeparam>
/// <returns>文件路径</returns>
protected string GenerateFilePath(Type type)
{
string path = Path.Combine(this.Directory, type.Name);
return path;
} #region CRUD /// <summary>
/// 增加一个<see cref="FileObject"/>到数据库。这实际上创建了一个文件。
/// <para>Create a new <see cref="FileObject"/> into database. This operation will create a new file.</para>
/// </summary>
/// <param name="item"></param>
public virtual void Create(FileObject item)
{
string fileName = GenerateFileFullPath(item);
string output = Serialize(item); lock (operationLock)
{
System.IO.FileInfo info = new System.IO.FileInfo(fileName);
System.IO.Directory.CreateDirectory(info.Directory.FullName);
System.IO.File.WriteAllText(fileName, output);
}
} /// <summary>
/// 检索符合给定条件的所有<paramref name="TFileObject"/>。
/// <para>Retrives all <paramref name="TFileObject"/> that satisfies the specified condition.</para>
/// </summary>
/// <typeparam name="TFileObject"></typeparam>
/// <param name="predicate">检索出的对象应满足的条件。<para>THe condition that should be satisfied by retrived object.</para></param>
/// <returns></returns>
public virtual IList<TFileObject> Retrieve<TFileObject>(Predicate<TFileObject> predicate)
where TFileObject : FileObject
{
IList<TFileObject> result = new List<TFileObject>();
if (predicate != null)
{
string path = GenerateFilePath(typeof(TFileObject));
string[] files = System.IO.Directory.GetFiles(path, "*.xml", SearchOption.AllDirectories);
foreach (var item in files)
{
string fileContent = File.ReadAllText(item);
TFileObject deserializedFileObject = Deserialize<TFileObject>(fileContent);
if (predicate(deserializedFileObject))
{
result.Add(deserializedFileObject);
}
}
} return result;
} /// <summary>
/// 更新给定的对象。
/// <para>Update specified <paramref name="FileObject"/>.</para>
/// </summary>
/// <param name="item">要被更新的对象。<para>The object to be updated.</para></param>
public virtual void Update(FileObject item)
{
string fileName = GenerateFileFullPath(item);
string output = Serialize(item); lock (operationLock)
{
System.IO.FileInfo info = new System.IO.FileInfo(fileName);
System.IO.Directory.CreateDirectory(info.Directory.FullName);
System.IO.File.WriteAllText(fileName, output);
}
} /// <summary>
/// 删除指定的对象。
/// <para>Delete specified <paramref name="FileObject"/>.</para>
/// </summary>
/// <param name="item">要被删除的对象。<para>The object to be deleted.</para></param>
public virtual void Delete(FileObject item)
{
if (item == null)
{
throw new ArgumentNullException(item.ToString());
} string filename = GenerateFileFullPath(item);
if (File.Exists(filename))
{
lock (operationLock)
{
File.Delete(filename);
}
}
} #endregion CRUD }

FileDBContext

文件存储方式(Way to store files)

SharpFileDB creates a directory for every table-types in the database's folder. Every instance of a table-type are stored in its respective directory as a single XML file. The file's name is same with the instance's Id.

下载(Download)

I've put the project on github(https://github.com/bitzhuwei/SharpFileDB/) and you are welcom to try, create issues and fork it.

PS: I think this small file database library can help some people. It's where we are that needs us.

SharpFileDB - a file database for small apps的更多相关文章

  1. 小型文件数据库 (a file database for small apps) SharpFileDB

    小型文件数据库 (a file database for small apps) SharpFileDB For english version of this article, please cli ...

  2. BIT祝威博客汇总(Blog Index)

    +BIT祝威+悄悄在此留下版了个权的信息说: 关于硬件(Hardware) <穿越计算机的迷雾>笔记 继电器是如何成为CPU的(1) 继电器是如何成为CPU的(2) 关于操作系统(Oper ...

  3. EBS R12 修改 apps 密码[Z]

    注意:修改密码时应保证所有用户已退出, 最好是关闭应用实例.不用关闭数据库.在修改密码之前一定要改备下数据库中的FND_ORACLE_USERID和FND_USER表.FNDCPASS工具会自动把AP ...

  4. File System Programming --- (二)

    File System Basics The file systems in OS X and iOS handle the persistent storage of data files, app ...

  5. swat主流域文件(file.cio)参数详解——引自http://blog.sciencenet.cn/blog-922140-710636.html

    % file.clo,即主流域文件用于文件管理,包括与模型选项.气候输入.数据库和输出控制相关的信息. Master Watershed File: file.cio Project Descript ...

  6. Database(Mysql)发版控制二

    author:skate time:2014/08/18 Database(Mysql)发版控制 The Liquibase Tool related Database 一.Installation ...

  7. Inno Setup connection to the database and create

    原文 Inno Setup connection to the database and create Description: the first half of this program in I ...

  8. DISQLite3 - A self-contained, embeddable, zero-configuration SQL database engine for Delphi

    DISQLite3 implements a self-contained, embeddable, zero-configuration SQL database engine for Delphi ...

  9. How to get the MD5 checksum for a file: md5sum, digest, csum, fciv

    LINUX: md5sum fileName In Linux, the md5sum utility can be used: aemtux1:/ % md5sum binary.file 0c46 ...

随机推荐

  1. 关于P,V操作理解的突破,关于并发设计与并行

    今天又找了一篇博客研究P,V操作.. 发现..它有一个变量没有声明.. 我就换了篇博客..http://c.biancheng.net/cpp/html/2600.html 然后就看懂了.. 关键突破 ...

  2. Drools 函数学习

    Drools 函数学习 函数是定义在规则文件当中一代码块,作用是将在规则文件当中若干个规则都会用到的业务操作封装起来,实现业务代码的复用,减少规则编写的工作量.函数的编写位置可以是规则文件当中 pac ...

  3. [spring源码学习]单元测试演化

    1.使用main方法 最早的测试方法一般是在类中增加main方法,然后在main方法中增加对每个方法的测试代码,如果要测其中一个,就屏蔽掉其他的测试代码,执行后,根据log的打印来判断测试是否成功 2 ...

  4. Maven打包生成可运行bat/sh脚本文件

    利用Maven的appassembler-maven-plugin插件,就可以实现自动打包可运行的脚本,还可以跨平台.   <plugin>      <groupId>org ...

  5. Maven 配置远程仓库

    最近要用到一个包:spark-1.6.0-cdh5.8.2,在https://mvnrepository.com网站上搜到该包的pom.xml语句.但是看到下面有这样一句话: 该包在图中所述repos ...

  6. linux菜鸟日记(4)

    使用一个简单的for循环和if判断语句实现某个网段内所有ping所有客户机的shell程序: ..} do >&; then echo " ${i}通" else e ...

  7. python 编码问题

    参考原文:http://www.crifan.com/eclipse_pydev_console_messy_char_for_console_is_utf8/ 通用 rq = urllib.urlo ...

  8. 20161022 NOIP模拟赛 解题报告

     好元素 [问题描述] 小A一直认为,如果在一个由N个整数组成的数列{An}中,存在以下情况: Am+An+Ap = Ai (1 <= m, n, p < i <= N ,  m,n ...

  9. ASP.NET中获取Repeater模板列中LinkButton按钮事件中获取ID等

    前台页面中: <asp:Repeater ID="repComment" runat="server">            <ItemTe ...

  10. Java 应该跨四个平台

    编程语言从属于操作系统,要统一,就要在根本处统一,要统一的是操作系统,而不是编程语言.你认为是苹果决定苹果树,还是苹果树决定苹果? 编程语言跨操作系统是错误的道路,你见过苹果长在桔子树上的吗?苹果长得 ...