定位

本文适合于不愿意手工编写而想自动化生成DAO单元测试的筒鞋。成果是不能照搬的,但其中的"创建模板、填充内容、自动生成"思想是可以复用的。读完本文,可以了解 Python 读取配置文件、替换字符串相关的知识点。

在使用 jtester+unitils+testng 做数据库接口的单元测试框架中,
常常需要编写一些 wiki 及 DAOTest java 文件,  比如:

public class XXXDefaultDAOTest extends BaseRegionDbDAOTestCase {

    @SpringBeanByName
private XXXDefaultDAO XXXDefaultDAO; @Test
@DbFit(when="XXXDefaultDAOTest.initBlank.when.wiki", then="XXXDefaultDAOTest.queryOneRecord.then.wiki")
public void testInsertXXXDefaultDO() {
XXXDefaultDO XXXDefaultDO = new XXXDefaultDO();
XXXDefaultDO.setId(1L);
XXXDefaultDO.setCidrBlock("192.168.10.10");
XXXDefaultDO.setIpProtocol("tcp");
XXXDefaultDO.setPortRange("3000:4000");
XXXDefaultDO.setPolicy(Policy.POLICY_ACCEPT);
XXXDefaultDO.setNic(Nic.INTRANET);
XXXDefaultDO.setPriority(65533L);
XXXDefaultDO.setType(1L);
XXXDefaultDO.setIsDeleted(0L);
XXXDefaultDO.setDescription("test1");
XXXDefaultDO.setGmtCreate(new Date());
XXXDefaultDO.setGmtModify(new Date());
XXXDefaultDAO.insertXXXDefaultDO(XXXDefaultDO);
} @Test
@DbFit(when="XXXDefaultDAOTest.initRecords.when.wiki")
public void testCountXXXDefaultDOByExample() {
XXXDefaultDO XXXDefaultDO = new XXXDefaultDO();
Assert.assertTrue(XXXDefaultDAO.countXXXDefaultDOByExample(XXXDefaultDO) == 1);
} @Test
@DbFit(when="XXXDefaultDAOTest.initRecords.when.wiki", then="XXXDefaultDAOTest.testUpdate.then.wiki")
public void testUpdateXXXDefaultDO() {
XXXDefaultDO found = XXXDefaultDAO.findXXXDefaultDOByPrimaryKey(6L);
found.setIpProtocol("udp");
found.setNic(Nic.INTERNET);
found.setDescription("desc");
XXXDefaultDAO.updateXXXDefaultDO(found);
} @Test
@DbFit(when="XXXDefaultDAOTest.initRecords.when.wiki")
public void testFindListByExample() {
String cidrBlock = "10.152.126.83";
Policy policy = Policy.POLICY_ACCEPT;
XXXDefaultDO XXXDefault = new XXXDefaultDO();
XXXDefault.setCidrBlock(cidrBlock);
XXXDefault.setPolicy(policy);
List<XXXDefaultDO> list = XXXDefaultDAO.findListByExample(XXXDefault);
Assert.assertEquals(list.size(), 1);
for (XXXDefaultDO XXXDefaultDO: list) {
Assert.assertEquals(XXXDefaultDO.getCidrBlock(), cidrBlock);
Assert.assertEquals(XXXDefaultDO.getPolicy(), policy);
}
} @Test
@DbFit(when="XXXDefaultDAOTest.initRecords.when.wiki")
public void testFindXXXDefaultDOByPrimaryKey() {
XXXDefaultDO found = XXXDefaultDAO.findXXXDefaultDOByPrimaryKey(6L);
Assert.assertEquals(found.getCidrBlock(), "10.152.126.83");
Assert.assertEquals(found.getIpProtocol(), "all");
Assert.assertEquals(found.getPortRange(), "");
Assert.assertEquals(found.getPolicy(), Policy.POLICY_ACCEPT);
Assert.assertEquals(found.getNic(), Nic.BOTH);
Assert.assertEquals(found.getPriority().longValue(),1L);
Assert.assertEquals(found.getType().intValue(), 1);
Assert.assertEquals(found.getIsDeleted().intValue(), 0);
Assert.assertEquals(found.getDescription(), "bie dong");
} @Test
@DbFit(when="XXXDefaultDAOTest.initRecords.when.wiki", then="")
public void testDeleteXXXDefaultDOByPrimaryKey() {
Integer count = XXXDefaultDAO.deleteXXXDefaultDOByPrimaryKey(6L);
Assert.assertEquals(count.intValue(), 1); Integer nodelete = XXXDefaultDAO.deleteXXXDefaultDOByPrimaryKey(6L);
Assert.assertEquals(nodelete.intValue(), 0);
} }

其中, 数据准备文件在 *.when.wiki 中, 数据验证文件在 *.then.wiki 中, 数据库中只需要保证正确的表结构即可。 每次单元测试都是自动化可重复的。

XXXDefaultDAOTest.initBlank.when.wiki
|connect|
|clean table|xxx_default|

XXXDefaultDAOTest.initRecords.when.wiki
|connect|
|clean table|xxx_default|
|clean table|xxx|
|insert|xxx_default|
| id | gmt_create | gmt_modify | cidr_block | ip_protocol | port_range | policy | nic | priority | type | is_deleted | description |
| 6 | 2014-04-08 20:18:04 | 2014-04-08 20:18:04 | 10.152.126.83 | all | | accept | 3 | 1 | 1 | 0 | bie dong | XXXDefaultDAOTest.queryOneRecord.then.wiki
|connect|
|query|select cidr_block, ip_protocol, port_range, policy, nic, priority, type, is_deleted, description from xxx_default |
|cidr_block | ip_protocol | port_range | policy | nic | priority | type | is_deleted | description |
|192.168.10.10 | tcp | 3000:4000 | accept | 2 | 65533 | 1 | 0 | test1 | XXXDefaultDAOTest.testUpdate.then.wiki
|connect|
|query|select cidr_block, ip_protocol, port_range, policy, nic, priority, type, is_deleted, description from xxx_default|
| cidr_block | ip_protocol | port_range | policy | nic | priority | type | is_deleted | description |
| 10.152.126.83 | udp | | accept | 1 | 1 | 1 | 0 | desc |

  

  显然, 如果每个 DAO 测试类都写这些 WIKI  及 DAO 类(set/get 字段很耗体力), 那会是比较大的工作量。 这时候, 最好能够自动生成这些文件或文件模板, 减少手工的劳动量。

因此, 编写了一个 python 程序, 在指定配置下, 可以自动生成相关的测试文件模板文件。

  readcfg.py :  读取DAO测试类信息的配置文件

from ConfigParser import ConfigParser

config = ConfigParser()
config.read("daotest.conf") def getAllDAOTestInfo():
allDAOTest = {}
secs = config.sections()
for sec in secs:
allDAOTest[sec] = getDAOTestInfo(sec)
return allDAOTest def getDAOTestInfo(daoTestName) :
daoTestInfo = {
'DaoTestName': config.get(daoTestName, 'DaoTestName') ,
'TableName': config.get(daoTestName, 'TableName'),
'FieldArray': config.get(daoTestName, 'FieldArray'),
'NumTypeFields': config.get(daoTestName, 'NumTypeFields'),
}
return daoTestInfo  

create_daotest_wiki.py : 生成 dao 测试的测试文件模板:

import readcfg
import time
import re def gene_daotest(daoTestInfo): daoTestName = daoTestInfo['DaoTestName']
tableName = daoTestInfo['TableName']
fieldArray = re.split('\s*,\s*', daoTestInfo['FieldArray'])
numTypeFields = set(re.split('\s*,\s*', daoTestInfo['NumTypeFields'])) print ' *** ', daoTestName , ' start...\n' startTime = time.clock()
gene_daotest_wiki_really(daoTestName, tableName, fieldArray, numTypeFields)
gene_daotest_java(daoTestName, tableName, fieldArray, numTypeFields)
endTime = time.clock() print ' *** ', daoTestName, ' finished.\n'
print 'time cost: ', str((endTime - startTime)*1000) + 'ms.\n' def gene_daotest_wiki_really(daoTestName, tableName, fieldArray, numTypeFields): '''
generate the wikies used for DAO test java file
''' conn = '|connect|'
clean_table = '|clean table|' + tableName + '|'
insert_table = '|insert|' + tableName + '|'
all_fields = '|' + getfieldsWithSep(fieldArray, 0, '|') + '|'
query_stmt = '|query|' + 'select ' + getfieldsWithSep(fieldArray, 0, ', ', filterTimeAndIdFieldFunc) + ' from ' + tableName + '|'
query_fields = '|' + getfieldsWithSep(fieldArray, 0, '|', filterTimeAndIdFieldFunc) + '|'
all_fields_default_values = '|' + getfieldValuesWithSep(fieldArray, numTypeFields, 0, '|') + '|'
query_fields_default_values = '|' + getfieldValuesWithSep(fieldArray, numTypeFields, 0, '|', filterTimeAndIdFieldFunc) + '|' # create DaoTestName.initBlank.when.wiki
f_initBlank = open(daoTestName+".initBlank.when.wiki", 'w')
f_initBlank.write('\n'.join([conn, clean_table]));
f_initBlank.close # create DaoTestName.initRecords.when.wiki
f_initRecs = open(daoTestName+".initRecords.when.wiki", 'w')
f_initRecs.write('\n'.join([conn, clean_table, insert_table, all_fields, all_fields_default_values]))
f_initRecs.close # create DaoTestName.queryOneRecord.then.wiki
f_qor = open(daoTestName+".queryOneRecord.then.wiki", 'w')
f_qor.write('\n'.join([conn, query_stmt, query_fields, query_fields_default_values]))
f_qor.close # create DaoTestName.testUpdate.then.wiki
f_update = open(daoTestName+".testUpdate.then.wiki", 'w')
f_update.write('\n'.join([conn, query_stmt, query_fields, query_fields_default_values]))
f_update.close def gene_daotest_java(daoTestName, tableName, fieldArray, numTypeFields): f_daotest_java = open(daoTestName+'.java', 'w')
f_daotest_tmpl = open('TemplateDefaultDAOTest.java')
content = ''
for line in f_daotest_tmpl:
content += line
daoPrefixIndex = daoTestName.find('DAOTest')
daoPrefix = daoTestName[0: daoPrefixIndex]
XXXReplacer = daoPrefix
YYYReplacer = firstLowerCase(XXXReplacer)
filteredFieldArray = getFilteredFields(fieldArray, filterTimeFieldFunc)
contentReplaced = content.replace('XXX', XXXReplacer).replace('YYY', YYYReplacer) \
.replace('$setFields', geneSetFields(filteredFieldArray, numTypeFields, YYYReplacer)) \
.replace('$AssertGetValues', geneAssertGetValues(filteredFieldArray, numTypeFields, YYYReplacer))
f_daotest_java.write(contentReplaced) def geneAssertGetValues(fieldArray, numTypeFields, YYYReplacer):
content = ''
for field in fieldArray:
quoteStr = '' if field in numTypeFields else '"'
content += 'Assert.assertEquals(%s.get%s(), %s%s%s);\n%s' % \
(YYYReplacer, transformField(field), quoteStr, getDefaultValueForField(field, numTypeFields), quoteStr, indentTimes(2))
return content def geneSetFields(fieldArray, numTypeFields, YYYReplacer):
content = ''
for field in fieldArray:
quoteStr = '' if field in numTypeFields else '"'
content += '%s.set%s(%s%s%s);\n%s' % \
(YYYReplacer, transformField(field), quoteStr, getDefaultValueForField(field, numTypeFields), quoteStr, indentTimes(2))
return content def transformField(field):
'''
convert field with UnderLine form to Camel Form
eg. cidr_block ==> CidrBlock
''' parts = field.split('_')
content = ''
for part in parts:
content += firstSuperCase(part)
return content def indentTimes(num):
indent = '';
while num > 0 :
indent += '\t'
num -= 1
return indent def firstLowerCase(input):
'''
the first letter lowered. eg. NcDAOTest ==> ncDAOTest
'''
return input[0].lower() + input[1:] def firstSuperCase(input):
'''
the first letter uppered. eg. ncDAOTest ==> NcDAOTest
'''
return input[0].upper() + input[1:] def nopFunc(field):
return True def currTime():
return time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) def getDefaultValueForField(field, numTypeFields):
'''
get the default value of field and return a value
if you want to set more proper value , do it here.
'''
if field == 'id' or field.find('_id') != -1:
return ''
elif field.find('gmt_') != -1:
return currTime()
elif field.find('ip') != -1 or field.find('addr') != -1:
return '172.16.0.1'
elif field.find('cidr') != -1:
return '172.16.0.0/22'
elif field in numTypeFields:
return ''
else :
return 'test-'+field def getfieldsWithSep(fieldArray, index=0, sep='|', filterFunc=nopFunc):
if index < 0 or index > len(fieldArray):
raise Exception('index ' + index + ' invalid: must be in [0,' + len(fieldArray) + ']')
fieldFilteredArray = getFilteredFields(fieldArray, filterFunc)
return sep.join(fieldFilteredArray[index:]) def getfieldValuesWithSep(fieldArray, numTypeFields, index=0, sep='|', filterFunc=nopFunc):
if index < 0 or index > len(fieldArray):
raise Exception('index ' + index + ' invalid: must be in [0,' + len(fieldArray) + ']')
fieldFilteredArray = getFilteredFields(fieldArray, filterFunc)
fieldDefaultValues = []
for field in fieldFilteredArray:
fieldDefaultValues.append(getDefaultValueForField(field, numTypeFields))
return sep.join(fieldDefaultValues) def filterTimeAndIdFieldFunc(field):
return field.find('gmt_') == -1 and field != 'id' def filterTimeFieldFunc(field):
return field.find('gmt_') == -1 def getFilteredFields(fieldArray, filterFunc):
return filter(filterFunc, fieldArray) if __name__ == '__main__':
allDAOTest = readcfg.getAllDAOTestInfo()
for daoTestName, daoTestInfo in allDAOTest.iteritems():
gene_daotest(daoTestInfo)

daotest.conf:  配置文件  

[VmDAOTest]
DaoTestName=VmDAOTest
TableName=vm
FieldArray=id,gmt_create,gmt_modify,vm_name,cores,mem,disk,status,nc_id,is_deleted
NumTypeFields=id,cores,mem,disk,status,nc_id,is_deleted [NcDAOTest]
DaoTestName=NcDAOTest
TableName=nc
FieldArray=id,gmt_create,gmt_modify,hostname,ip,avail_cpu, avail_mem, avail_disk
NumTypeFields=id,avail_cpu, avail_mem, avail_disk

DAO java 文件模板: 

package xxx.dao.regiondb.impl;

import java.util.Date;
import java.util.List; import org.jtester.unitils.dbfit.DbFit;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.unitils.spring.annotation.SpringBeanByName; import xxx.BaseRegionDbDAOTestCase;
import xxx.constant.group.Nic;
import xxx.constant.group.Policy;
import xxx.dao.regiondb.XXXDAO;
import xxx.model.db.XXXDO; public class XXXDAOTest extends BaseRegionDbDAOTestCase { @SpringBeanByName
private XXXDAO YYYDAO; @Test
@DbFit(when="XXXDAOTest.initBlank.when.wiki", then="XXXDAOTest.queryOneRecord.then.wiki")
public void testInsertXXXDO() {
XXXDO YYY = new XXXDO();
$setFields
YYY.setGmtCreate(new Date());
YYY.setGmtModify(new Date());
YYYDAO.insertXXXDO(YYY);
} @Test
@DbFit(when="XXXDAOTest.initRecords.when.wiki")
public void testCountXXXDOByExample() {
XXXDO YYY = new XXXDO();
Assert.assertTrue(YYYDAO.countXXXDOByExample(YYY).intValue() == 1);
} @Test
@DbFit(when="XXXDAOTest.initRecords.when.wiki", then="XXXDAOTest.testUpdate.then.wiki")
public void testUpdateXXXDO() {
XXXDO YYY = YYYDAO.findXXXDOByPrimaryKey();
$setFields
YYYDAO.updateXXXDO(YYY);
} @Test
@DbFit(when="XXXDAOTest.initRecords.when.wiki")
public void testFindListByExample() {
XXXDO YYY = new XXXDO();
$setFields
List<XXXDO> list = YYYDAO.findListByExample(YYY);
Assert.assertEquals(list.size(), 1);
for (XXXDO YYYDO: list) {
$AssertGetValues
}
} @Test
@DbFit(when="XXXDAOTest.initRecords.when.wiki")
public void testFindXXXDOByPrimaryKey() {
XXXDO YYY = YYYDAO.findXXXDOByPrimaryKey(1L);
$AssertGetValues
} @Test
@DbFit(when="XXXDAOTest.initRecords.when.wiki")
public void testDeleteXXXDOByPrimaryKey() {
Integer count = YYYDAO.deleteXXXDOByPrimaryKey(1L);
Assert.assertEquals(count.intValue(), 1); Integer nodelete = YYYDAO.deleteXXXDOByPrimaryKey(1L);
Assert.assertEquals(nodelete.intValue(), 0);
} }

运行: $ python create_daotest_wiki.py

生成以下文件:

其中:

VmDAOTest.initBlank.when.wiki  

|connect|
|clean table|vm| VmDAOTest.initRecords.when.wiki |connect|
|clean table|vm|
|insert|vm|
|id|gmt_create|gmt_modify|vm_name|cores|mem|disk|status|nc_id|is_deleted|
|1|2014-05-22 12:51:38|2014-05-22 12:51:38|test-vm_name|0|0|0|0|1|0| VmDAOTest.queryOneRecord.when.wiki / VmDAOTest.testUpdate.when.wiki |connect|
|query|select vm_name, cores, mem, disk, status, nc_id, is_deleted from vm|
|vm_name|cores|mem|disk|status|nc_id|is_deleted|
|test-vm_name|0|0|0|0|1|0| 

生成的DAOTEST Java 文件:

package xxx.dao.regiondb.impl;

import java.util.Date;
import java.util.List; import org.jtester.unitils.dbfit.DbFit;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.unitils.spring.annotation.SpringBeanByName; import xxx.BaseRegionDbDAOTestCase;
import xxx.constant.group.Nic;
import xxx.constant.group.Policy;
import xxx.dao.regiondb.VmDAO;
import xxx.model.db.VmDO; public class VmDAOTest extends BaseRegionDbDAOTestCase { @SpringBeanByName
private VmDAO vmDAO; @Test
@DbFit(when="VmDAOTest.initBlank.when.wiki", then="VmDAOTest.queryOneRecord.then.wiki")
public void testInsertVmDO() {
VmDO vm = new VmDO();
vm.setId(1);
vm.setVmName("test-vm_name");
vm.setCores(0);
vm.setMem(0);
vm.setDisk(0);
vm.setStatus(0);
vm.setNcId(1);
vm.setIsDeleted(0); vm.setGmtCreate(new Date());
vm.setGmtModify(new Date());
vmDAO.insertVmDO(vm);
} @Test
@DbFit(when="VmDAOTest.initRecords.when.wiki")
public void testCountVmDOByExample() {
VmDO vm = new VmDO();
Assert.assertTrue(vmDAO.countVmDOByExample(vm).intValue() == 1);
} @Test
@DbFit(when="VmDAOTest.initRecords.when.wiki", then="VmDAOTest.testUpdate.then.wiki")
public void testUpdateVmDO() {
VmDO vm = vmDAO.findVmDOByPrimaryKey();
vm.setId(1);
vm.setVmName("test-vm_name");
vm.setCores(0);
vm.setMem(0);
vm.setDisk(0);
vm.setStatus(0);
vm.setNcId(1);
vm.setIsDeleted(0); vmDAO.updateVmDO(vm);
} @Test
@DbFit(when="VmDAOTest.initRecords.when.wiki")
public void testFindListByExample() {
VmDO vm = new VmDO();
vm.setId(1);
vm.setVmName("test-vm_name");
vm.setCores(0);
vm.setMem(0);
vm.setDisk(0);
vm.setStatus(0);
vm.setNcId(1);
vm.setIsDeleted(0); List<VmDO> list = vmDAO.findListByExample(vm);
Assert.assertEquals(list.size(), 1);
for (VmDO vmDO: list) {
Assert.assertEquals(vm.getId(), 1)
Assert.assertEquals(vm.getVmName(), "test-vm_name")
Assert.assertEquals(vm.getCores(), 0)
Assert.assertEquals(vm.getMem(), 0)
Assert.assertEquals(vm.getDisk(), 0)
Assert.assertEquals(vm.getStatus(), 0)
Assert.assertEquals(vm.getNcId(), 1)
Assert.assertEquals(vm.getIsDeleted(), 0) }
} @Test
@DbFit(when="VmDAOTest.initRecords.when.wiki")
public void testFindVmDOByPrimaryKey() {
VmDO vm = vmDAO.findVmDOByPrimaryKey(1L);
Assert.assertEquals(vm.getId(), 1)
Assert.assertEquals(vm.getVmName(), "test-vm_name")
Assert.assertEquals(vm.getCores(), 0)
Assert.assertEquals(vm.getMem(), 0)
Assert.assertEquals(vm.getDisk(), 0)
Assert.assertEquals(vm.getStatus(), 0)
Assert.assertEquals(vm.getNcId(), 1)
Assert.assertEquals(vm.getIsDeleted(), 0) } @Test
@DbFit(when="VmDAOTest.initRecords.when.wiki")
public void testDeleteVmDOByPrimaryKey() {
Integer count = vmDAO.deleteVmDOByPrimaryKey(1L);
Assert.assertEquals(count.intValue(), 1); Integer nodelete = vmDAO.deleteVmDOByPrimaryKey(1L);
Assert.assertEquals(nodelete.intValue(), 0);
} }

结语:

只要是手工劳动, 尽可能自动化。而要做到自动化, 第一是规范标准化, 第二是要发现一些规律性的模式。

Jtester+unitils+testng:DAO单元测试文件模板自动生成的更多相关文章

  1. T4 模板自动生成带注释的实体类文件

    T4 模板自动生成带注释的实体类文件 - 只需要一个 SqlSugar.dll 生成实体就是这么简单,只要建一个T4文件和 文件夹里面放一个DLL. 使用T4模板教程 步骤1 创建T4模板 如果你没有 ...

  2. Eclipse中R文件不能自动生成

       R文件不能自动生成主要是因为编译有错误,这时你想什么办法都是没有用的,clean, fix properties,都不是从根上解决问题.    R文件主要是自动生成资源文件的id的,里边静态子类 ...

  3. 用shell脚本新建shell文件并自动生成头说明信息

    目标: 新建文件后,直接给文件写入下图信息 代码实现: [root@localhost test]# vi AutoHead.sh #!/bin/bash#此程序的功能是新建shell文件并自动生成头 ...

  4. C++解析头文件-Qt自动生成信号声明

    目录 一.瞎白话 二.背景 三.思路分析 四.代码讲解 1.类图 2.内存结构声明 3.QtHeaderDescription 4.私有函数讲解 五.分析结果 六.下载 一.瞎白话 时间过的ZTMK, ...

  5. C++解析头文件-Qt自动生成信号定义

    目录 一.概述 二.实现思路 三.代码讲解 1.类图 2.QtCppDescription 3.测试 四.源代码 一.概述 上一篇文章C++解析头文件-Qt自动生成信号声明我们主要讲解了怎么去解析C+ ...

  6. 安装Ruby、Sass在WebStrom添加Watcher实现编辑scss文件时自动生成.map和压缩后的.css文件

    前言 这段时间一直在看Bootstrap,V3官方直接提供了Less版本的源码,就先将Less学完了,很简单的语法,学习写Demo都是在Webstorm里写的,配置了Watcher自动编译(详见< ...

  7. R.java文件无法自动生成的问题

    如果出现R.java文件无法自动生成的问题,同时Console窗口提示下列信息: Android requires compiler compliance level 5.0 or 6.0. Foun ...

  8. webstorm创建js文件时自动生成js注释

    设置webstorm创建js文件时自动生成js注释 settings--Editor--File and Code Temlates 黑色框框里的内容自己填写上去,以下是参考的代码块: /** * @ ...

  9. 用shell脚本新建文件并自动生成头说明信息

    目标: 新建文件后,直接给文件写入下图信息 代码实现: [root@localhost test]# vi AutoHead.sh #!/bin/bash #此程序的功能是新建shell文件并自动生成 ...

随机推荐

  1. Windows运行python脚本文件

    开始学习python就是听说这个语言写脚本文件特别方便,简单使用.学了一段时间,但是直到现在我才直到直到怎么在Windows的cmd上运行脚本文件. 之前一直都是在pycharm上运行,并不实用. 百 ...

  2. JPA的锁机制

    JPA 各种实体锁模式的区别 字数2084 阅读304 评论0 喜欢4 为了能够同步访问实体,JPA提供了2种锁机制.这两种机制都可以避免两个事务中的其中一个,在不知情的情况下覆盖另一个事务的数据. ...

  3. Nginx限制IP访问及获取客户端realip实战

    做网站时经常会用到remote_addr和x_forwarded_for这两个头信息来获取客户端的IP,然而当有反向代理或者CDN的情况下,这两个值就不够准确了,需要调整一些配置.Nginx作为web ...

  4. ubuntu16.04下安装Sophus

    git clone https://github.com/strasdat/Sophus.git 下载完成后 cd Sophus git checkout a621ffmkdir buildcd bu ...

  5. ZOJ 3993 - Safest Buildings - [数学题]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3993 题意: 给出n幢建筑,每个都以一个点表示,给出点坐标. 有 ...

  6. CodeForces - 665D Simple Subset 想法题

    //题意:给你n个数(可能有重复),问你最多可以取出多少个数使得任意两个数之和为质数.//题解:以为是个C(2,n)复杂度,结果手摸几组,发现从奇偶性考虑,只有两种情况:有1,可以取出所有的1,并可以 ...

  7. queue hardware os

    Computer Science An Overview 11th Edition Queues are often used as the underlying structure of a buf ...

  8. sklearn学习总结(超全面)

    https://blog.csdn.net/fuqiuai/article/details/79495865 前言sklearn想必不用我多介绍了,一句话,她是机器学习领域中最知名的python模块之 ...

  9. 我读过的最好的epoll讲解(转)

    原文:http://zhihu.com/question/20122137/answer/14049112 作者:蓝形参来源:知乎 首先我们来定义流的概念,一个流可以是文件,socket,pipe等等 ...

  10. python 使用set对列表去重,并保持列表原来顺序

    # python 使用set对列表去重,并保持列表原来顺序 list1 = ['cc', 'bbbb', 'afa', 'sss', 'bbbb', 'cc', 'shafa'] for item i ...