[原创][翻译]Java 企业级项目中应用Subversion的配置与管理[中英文对照]

    企业应用不仅限于对数据进行插入、读取、更新和删除操作,还希望存储数据更改的历史记录,要求存储关于是谁,在什么时候,因为什么原因,如何改变了数据等等诸如此类的信息,但这些内容仅仅使用传统的数据库已经不能满足要求了,还有其他的好办法吗?使用JavaSVN + Subversion跟踪数据变化历史,就是一个好的思路.

 

Java 企业级项目中应用Subversion的配置与管理

--JavaSVN + Subversion跟踪数据变化历史

05/03/2006

作者:Swaminathan Radhakrishnan

译者:陈海青(http://www.chq.name)

The most critical asset of any enterprise application is the data that it stores. Today's enterprise applications are often required to not just store data, but also keep track of all the changes that are made. This expectation also permeates into an associated set of requirements, such as tracking the reason for each change, the time of the change etc. In many cases, the data tracking requirements apply to data that applications store in the form of documents. Using Subversion can help satisfy these complicated, albeit common, requirements.

企业最重要的资产应该是数据信息,但现在的企业应用除了需要存储数据外,还经常要求跟踪数据变化整个过程,并会扩展到一系列相关的要求,如数据变化的原因、变化的时间等,而且在许多情况下是对以文档形式存储的数据进行跟踪。使用SubVersion可以满足这些貌似普通但实际上很复杂的要求

 

来自数据的挑战

Enterprise applications store critical pieces of data, and the requirements of such applications do not stop at just the create, read, update and delete (CRUD) operations on the data. Applications are expected to store the history of all the changes to such data. Furthermore, organizations have a variety of legal and regulatory compliance mandates that ultimately result in requirements to not just track every change to critical data assets, but also associated requirements such as who made the changes to the data, what time a change was made, what changes were made, why the change made, etc.

企业应用存储了关键数据,而且应用程序并不仅限于对数据进行插入、读取、更新和删除操作(即CRUD),应用程序还期望能够存储数据更改的历史记录。此外,企业按照一系列的业务或者规定的要求,不但要求存储数据资产更改结果的历史,而且要求存储是谁,在什么时候,因为什么原因,如何改变了数据等等诸如此类的跟踪信息。

Application data also varies widely in form and size. Applications are required to store data varying from simple forms, such as strings and numbers, to complex types, such as documents that are stored as blobs and clobs. A certain class of applications deals very heavily with data in the form of documents uploaded into the application. Tracking data in complex forms such as documents can become a nightmare if traditional approaches, such as history tables, are chosen.

应用数据的形式和尺寸也有很多变数,既有简单数据,如字符串和数字型,也有复杂的类型,如使用Blob或Cblob类型来存储文档。典型的应用程序要处理大量的上传给程序处理的以文档形式存储的数据,如果用传统的历史表等方式来跟踪诸如复杂类型的文档的变化,简直就是做一场恶梦。

 

使用历史表进行跟踪

Relational databases are generally the preferred choice for storing application data. They help organize, store, and retrieve the data in a very efficient manner. Since application data is stored in these relational databases, applications try to use these databases for tracking historical data as well. The most pervasive approach to storing historical data is to have a time-stamped history table for every table that stores important application entities. Updates made to the main table result in actions that push out the previous values of data to the history tables. This is either done through triggers or by the applications themselves.

关系数据库是存储数据的首选,可以高效地组织、存储、检索数据信息,由于应用程序将数据存放在关系数据库中,当然就顺理成章的尝试用它来存放历史跟踪数据,一般是使用带有时间戳的数据表来存放所有的重要数据表。在更新主表的时候会把旧数据推入历史表中,这个过程一般是通过触发器或由应用程序自己来完成。

There are several issues with storing the historical information in history tables.

Relational databases and the relational data modeling concentrate on efficient data storage and retrieval. The history tables do not model naturally with relational databases.

There is no support for versioning from the database. The application has to carefully store the entries into the history tables either through triggers or some other custom techniques.

It's up to the application to determine what changed between versions. The retrieval of historical information from the history tables is also specific to the history table storing the data.

 

The relational databases should still be the repository for storing and retrieving transactional data. They excel in managing these critical data assets. The shortcomings listed above are confined to storing multiple versions of data entities within the relational data store and tracking such entities over time.

使用历史表存储历史信息,会存在以下问题:

关系型数据库和关系模型会提高数据存储和检索的效率,但是历史表显然不适合使用关系型数据库。

数据库不支持版本控制。应用程序不得不使用触发器或其它定制的技术来仔细的存放数据(,以便实现版本控制功能)。

必须由应用程序亲自检测版本之间的变化,从历史表中检索历史数据进行互相比较。

 

关系数据库依旧是存储和检索业务数据的仓库,它们擅长于管理数据。以上列举的缺点仅限于用关系数据模型存储多个不同的版本的数据并进行历史数据跟踪的情况下。

 

Subversion 和 JavaSVN

Subversion is a version control system that's a replacement for CVS, an older version control system. Subversion organizes data as files and directories in the form of a tree called a repository. Subversion tracks all the changes that are made to any asset stored in this repository. Subversion can have a centrally located repository and allows multiple concurrent updates to the repository. It can access its repository over HTTP and HTTPS using the WebDAV protocol, which helps avoid firewall issues that often crop up during deployment. Subversion works on the concept of copy-modify-merge, which effectively means that there is no requirement to lock an object before making changes to it.

Subversion是一个可以代替CVS(一个传统的版本控制系统)的版本控制系统。Subversion使用称作仓库的树状结构来存储文件和目录。Subversion会跟踪对仓库中信息的所有改变,它具有一个中央仓库,允许进行并发更新,允许通过http或https使用WebDAV协议来访问仓库,可以避免使用过程中的防火墙的干扰。Subversion的理念就是“拷贝-编辑-合并”,这就意味着在修改时不需要锁定被修改的对象。

(译者注:关于WebDAV,是Web-based Distributed Authoring and Versioning的缩写,是一个标准HTTP协议的扩展,通过web技术把目录和文件作为可读些的对象进行共享读写,把web变成一个可读写的媒体。RFCs2518和3253描述了WebDAV/DeltaV 对于HTTP的扩展,网址http://www.webdav.org/。)

JavaSVN is a pure-Java Subversion client library. It offers APIs for Java applications to interact with Subversion. JavaSVN offers low-level APIs that can directly interact with a repository or high-level APIs to manage working copies checked out from a repository.

JavaSVN是一个纯Java的Subversion客户端类库,提供与Subversion交互的基于Java程序的应用程序接口(API), JavaSVN既提供了进行直接读取Subversion仓库的底层接口,也提供了从Subversion仓库检出工作拷贝的高层接口。

Applications can use a combination of relational databases and Subversion to satisfy data management and data tracking requirements. Any updates that are made to the critical data assets present in the relational database would be accompanied with a commit into Subversion. Subversion would be the primary data source for use-cases for tracking, while the relational database would be used for all other purposes. An additional advantage is that due to Subversion's copy-modify-merge concept, there is no requirement to lock an object every time its retrieved from the relational database.

现在,应用程序可以使用结合了关系型数据库和Subversion的方式来满足数据存储和变化跟踪的需求了,对数据库的更新同时会将变化情况提交到Subversion中,Subversion将是记录变化的主要数据源,关系数据库则用于除此以外所有的其他存储。这样做还有一个优势,由于Subversion使用“拷贝-编辑-合并”模式,这样每次从关系数据库中检索数据时不再要求锁定目标表了。

 

实例学习

Now that we've stated the problem and proposed a potential solution, let's use an example to illustrate the usage of Subversion and JavaSVN to solve the problem. We will store a sample domain object into Subversion using the JavaSVN API. We will also retrieve previous versions and display differences between two versions. Our sample domain object is the LoanData object as shown below. The complete code used in this article can be found in the Resources section at the end of this article.

现在让我们来确定一下要解决的问题和解决方案,并使用实例来说明如何使用Subversion 和JavaSVN。我们使用JavaSVN将一个简单的领域对象存储到Subversion 中,检索以前的版本,并显示两个版本的差异。我们示例的领域对象是以下所示的贷款数据。在本文结尾的“资源”部分有完整的源代码链接。

public class LoanData extends BaseTrackingObject {

private String loanId;

private double loanAmount;

private float loanRate;

private int loanTerm;

......

......

}

An abstract BaseTrackingObject class defines the commonly required tracking information, such as modifiedUser, modificationDate, and modificationReason. It defines the abstract methods to set and retrieve the objectId that can acts as the primary key for the domain object. It also defines a utility method called getXmlRepresentation that we will use to convert the object representation to an XML format. This format will be used to store and retrieve data from Subversion.

在这里,使用抽象的BaseTrackingObject类来定义通用的跟踪数据,如修改用户、修改日期、修改原因等。其中定义了设置和取得objectId抽象方法,把它用作主键来访问领域对象;定义了一个命名为getXmlRepresentation的工具方法,用于把对象转换成XML格式,进而用于在Subversion中存储和检索数据。

 

初始化JavaSVN

The class SVNManager is our gateway to Subversion. It uses the low-level JavaSVN APIs to directly interact with the repository since we do not deal with working copies. We initialize the JavaSVN library to either interact with the repository over HTTP(S) or SVN(S). We have chosen to interact with the repository using HTTP (WebDAV), since this will lead to fewer deployment/firewall issues.

SVNManager类是通向Subversion的路由,用于在不使用工作拷贝的情况下,通过底层JavaSVN接口直接访问Subversion仓库,通过初始化JavaSVN类库来可以使用HTTP(S)或SVN(S)与Subversion仓库进行交互。在这里,我们选择使用HTTP (WebDAV),因为可以减少在处理防火墙方面的工作。

The library is initialized using the call DAVRepositoryFactory.setup(). The class SVNRepository has all the required methods to directly interact with the Subversion repository. This class is initialized using the SVNRepositoryFactory class by providing the root of the tree hierarchy in the repository. The class ISVNAuthenticationManager is used to provide the authentication information required for SVNRepository to interact with the repository.

库的初始化工作要首先调用的是方法DAVRepositoryFactory.setup()。SVNRepository类包含了所有直接访问Subversion仓库的方法,将Subversion仓库树状结构的根路径提供给SVNRepositoryFactory类后,就完成了这个类的初始化,而ISVNAuthenticationManager类的作用是向SVNRepository提供访问Subversion仓库的授权信息。

public void initRepository() {

//initialize the system to work over http

DAVRepositoryFactory.setup();

............

//point to the root folder of repository

SVNURL svnUrl = SVNURL.parseURIEncoded

("http://localhost/repos/");

//initialize the SVNRepository

theRepository = SVNRepositoryFactory.

create(svnUrl);

//Creates the Auth manager with our user

//and password credentials

ISVNAuthenticationManager authManager =

new BasicAuthenticationManager

(name, password);

//provides the credentials to the

//SVNRepository

theManager.setAuthenticationManager

(authManager);

........

}

 

在Subversion中存储数据

Subversion stores data in a hierarchical format, meaning we have to come up with a standard for storing our domain entities. For the sake of this example, we will store all the domain objects under the folder DomainObjects. The class of the domain object will determine the sub-directory under which all the domain objects of that type will be stored. The individual domain objects are stored as XML files with the name of their primary keys.

Subversion需要使用层次结构存储数据,这样我们先要设定一下领域实体的层次结构,这里使用一个命名为“DomainObjects”的文件夹来存放领域数据。领域对象类将会检测这个目录下存放领域对象的所有子目录,而每个独立的域对象被以XML格式进行存储,并以其主键进行命名。

To store the LoanData domain object, we will invoke the checkInObject method of our SVNManager object. An implementation of the ISVNEditor obtained through the SVNRepository object is used to create or update revisions of our domain object in the repository. All the operations performed on the editor will be committed only when the closeEdit method is called. The SVNDeltaGenerator class is used to obtain differences between the version currently in the repository with one that is being updated. Subversion receives updates for new versions in the form of such deltas rather than the entire object to improve network performance.

为存储LoanData域对象,我们先要执行SVNManager对象的checkInObject方法,通过SVNRepository 执行的ISVNEditor对象来在Subversion仓库中的建立和更新域对象的版本,但只有在closeEdit被调用后,才会提交所有的操作。SVNDeltaGenerator类用于获取当前版本与被更新版本之间的差异,Subversion通过存储版本间差异部分的形式存放新的版本,这样会使提高网络效率。

public SVNResult checkInObject(

BaseTrackingObject obj){

.....

//Obtain the editor handle from the

//repository

ISVNEditor editor = theRepository.

getCommitEditor(obj.

getModificationReason(), null);

....

//create the file at the specified path

editor.addFile(path, null, -1);

}

else {

//file is already present, so open

//the file in the repository

editor.openFile(path, -1);

}

....

String checksum = deltaGenerator.

sendDelta(path,

new ByteArrayInputStream(

obj.getXmlRepresentation().

getBytes()),

editor, true);

.....

editor.closeEdit();

...

}

 

检索变化历史

To retrieve the history of versions for a particular domain object, we will call SVNManager's getRevisionLog method. The latest revision number of the repository can be obtained using SVNRepository's getLatestRevision() method. The SVNManager.log method helps obtain the log entries associated with each revision. The log entries contain information such as when was the revision created, who created the revision, what was the revision number, etc. A call to the SVNManager.getFile method helps retrieve a particular revision from the repository.

为检索指定领域对象的历史版本,需要调用SVNManager类的getRevisionLog方法; SVNRepository类的getLatestRevision方法可以得到当前版本号;SVNManager.log方法可以检索每个版本的日志,日志可以包含版本修订的日期、修改人、修改的内容等信息;SVNManager.getFile方法可以从Subversion仓库中取得领域对象指定版本的所有内容。

public List getRevisionLog(BaseTrackingObject

obj, long startRevision,

long endRevision) {

.....

if(endRevision==-1) {

//obtain the latest revision from

//the repository

endRevision =

theRepository.getLatestRevision();

}

//retrieve the various log entries for

//the particular object path

Collection revisionLogs = theRepository.

log(new String[] {path}, null,

startRevision, endRevision,

false, true);

....

//Obtain the contents of the object at

//the specified revision

theRepository.getFile(path, revisionInfo.

getRevisionNumber(),

new Properties(),

xmlFileContentsStream);

....

}

 

检索版本间的差异

To determine the differences that exist between two revisions, the SVNManager.showDifferences method is used. The JavaSVN class SVNDiffManager has the required implementation for getting the differences between two versions. A handle to this class can be obtained using the SVNClientManager class. The SVNDiffManager's doDiff method has a default implementation for returning the differences in a fixed format into the OutputStream passed in as a parameter. We can have custom implementations for getting the differences between two versions through the ISVNEditor class, but we will go with the default implementation for this example.

SVNManager.showDifferences方法用来检测两个版本之间的差异,是通过调用JavaSVN 的SVNDiffManager类来去的差异,也可以通过SVNClientManager来引用并执行这个类,SVNDiffManager的doDiff方法有一个默认的实现,可以通过参数指定输出流参数的形式取得固定格式的差异结果,我们也可以使用ISVNEditor来实现一个自己的差异比较方法。在这个例子里,我们使用默认的实现。

public String showDifferences(

BaseTrackingObject obj,long revision1,

long revision2) {

....

//Create an instance of SVNClientManager

//providing authentication

SVNClientManager ourClientManager =

SVNClientManager.newInstance(

options, "name", "password");

//Obtain the handle to DiffClient

//from the client manager

SVNDiffClient theDiff = ourClientManager

.getDiffClient();

....

theDiff.doDiff(svnUrl, SVNRevision.

create(revision1), svnUrl,

SVNRevision.create(revision2),

false, false, diffStream);

....

}

 

结论

Enterprise applications deal with requirements to not just store and retrieve critical data assets, but also track the historical changes to such data. The traditional approaches to solve these requirements using relational databases do not present an elegant solution. Subversion, a version tracking system offers the required amount of support for tracking our sample LoanData object over time. The JavaSVN APIs were used for tasks such as storing the loan object, retrieving the object, retrieving the log of changes that are made to the object, and also showing the differences between two versions of the object.

We have examined only a basic set of features but there is plenty of support from Subversion for far more sophisticated requirements that are not uncommon for an enterprise-level application. Happy exploring!

在企业级应用里,不但要完成数据的存储和检索,还要实现对数据变化历史的跟踪。传统方法是使用关系数据库来完成这个工作,但是这不是一个“优雅”的方案。在我们的贷款数据处理的例子里,Subversion提供了跟踪数据变化的支持。JavaSVN的API用来完成数据存储、检索、获取版本间差异和日志等任务。

我们的例子只是一个简单的性能演示,Subversion提供了丰富的功能支持,完全可以应用于企业级应用。祝你探索地更开心!

Resources

资源

本文的范例代码和相关的安装介绍

Subversion 站点可以提供相关信息

Version Control with Subversion 一书也可以从网上获得online

JavaSVN 的示例代码

www.chq.name也可以得到部分相关资源和信息

 

关于作者

Swaminathan Radhakrishnan 本文作者,Infosys技术有限公司的资深技术架构师

陈海青(joson),本文译者,生活在中国的山东省烟台市,先后从事软件开发、数据库管理、系统管理等工作,2001年获得高级程序员资格。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值