联系人数据库设计之ContactsTransaction
不当之处,请雅正。
请自行下载android源代码
package com.android.providers.contacts; import com.google.android.collect.Lists;
import com.google.android.collect.Maps; import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteTransactionListener; import java.util.List;
import java.util.Map; /**
* A transaction for interacting with a Contacts provider. This is used to pass state around
* throughout the operations comprising the transaction, including which databases the overall
* transaction is involved in, and whether the operation being performed is a batch operation.
*
* 和ContactProvider交互的事务。在包含事务(Transaction)的整个过程中用来传递状态信息,例如:全部的事务涉及那个数据库以及当前
* 正在处理的数据库是否为批处理等。
*
* 本事务用于管理在事务操作中涉及到的数据库。加入到集合,从集合中删除等。
*/
public class ContactsTransaction { /**
* Whether this transaction is encompassing a batch of operations. If we're in batch mode,
* transactional operations from non-batch callers are ignored.
* 当前的事务是否包含一个批处理操作,如果我们当前处在批处理模式,非批处理事务性请求会忽略。
*/
private final boolean mBatch; /**
* The list of databases that have been enlisted in this transaction.
* 在当前事务中还没有建立的数据库列表
*/
private List<SQLiteDatabase> mDatabasesForTransaction; /**
* The mapping of tags to databases involved in this transaction.
* 事务中涉及到的数据库tags的map集合
*/
private Map<String, SQLiteDatabase> mDatabaseTagMap; /**
* Whether any actual changes have been made successfully in this transaction.
* 在当前事务中,是否发生了任何实际的变化(数据是否发生改变)
*/
private boolean mIsDirty; /**
* Whether a yield operation failed with an exception. If this occurred, we may not have a
* lock on one of the databases that we started the transaction with (the yield code cleans
* that up itself), so we should do an extra check before ending transactions.
* 是否一个yield操作由于异常而失败。如果发生了这种情况,yield code会自己
* 处理,不会在事务(Transaction)要处理的数据库上加锁。
* 但我们应该在终止事务之前额外检查一下。
*/
private boolean mYieldFailed; /**
* Creates a new transaction object, optionally marked as a batch transaction.
* @param batch Whether the transaction is in batch mode.
* 创建一个事务。参数:是否标记为批处理
*/
public ContactsTransaction(boolean batch) {
mBatch = batch;
mDatabasesForTransaction = Lists.newArrayList();
mDatabaseTagMap = Maps.newHashMap();
mIsDirty = false;
} public boolean isBatch() {
return mBatch;
} public boolean isDirty() {
return mIsDirty;
} public void markDirty() {
mIsDirty = true;
} public void markYieldFailed() {
mYieldFailed = true;
} /**
* If the given database has not already been enlisted in this transaction, adds it to our
* list of affected databases and starts a transaction on it. If we already have the given
* database in this transaction, this is a no-op.
* @param db The database to start a transaction on, if necessary.
* @param tag A constant that can be used to retrieve the DB instance in this transaction.
* @param listener A transaction listener to attach to this transaction. May be null.
* 如果事务作用的数据库没有建立,将数据库增加到(作用列表中)并且对数据库启动事务。
* 如果数据库建立了,这是一个无效的操作。
*/
public void startTransactionForDb(SQLiteDatabase db, String tag,
SQLiteTransactionListener listener) {
if (!hasDbInTransaction(tag)) {
mDatabasesForTransaction.add(db);
mDatabaseTagMap.put(tag, db);
if (listener != null) {
db.beginTransactionWithListener(listener);
} else {
db.beginTransaction();
}
}
} /**
* Returns whether DB corresponding to the given tag is currently enlisted in this transaction.
* 返回被事务作用的数据库(DB)是否建立。建立返回true。否则false;
*/
public boolean hasDbInTransaction(String tag) {
return mDatabaseTagMap.containsKey(tag);
} /**
* Retrieves the database enlisted in the transaction corresponding to the given tag.
* @param tag The tag of the database to look up.
* @return The database corresponding to the tag, or null if no database with that tag has been
* enlisted in this transaction.
* 返回 根据tag在列表中查找数据库。列表由事务所涉及到的数据库组成。
* 返回数据库 或者null
*/
public SQLiteDatabase getDbForTag(String tag) {
return mDatabaseTagMap.get(tag);
} /**
* Removes the database corresponding to the given tag from this transaction. It is now the
* caller's responsibility to do whatever needs to happen with this database - it is no longer
* a part of this transaction.
* @param tag The tag of the database to remove.
* @return The database corresponding to the tag, or null if no database with that tag has been
* enlisted in this transaction.
* 根据tag从集合中移除数据库并返回。
* 前期我们构建了事务涉及的数据库集合,现在是用户的指责去操作这些数据库,在数据库上可以做任何用户想做的事情。
* 移除的数据库不再属于本事务。
*/
public SQLiteDatabase removeDbForTag(String tag) {
SQLiteDatabase db = mDatabaseTagMap.get(tag);
mDatabaseTagMap.remove(tag);
mDatabasesForTransaction.remove(db);
return db;
} /**
* Marks all active DB transactions as successful.
* @param callerIsBatch Whether this is being performed in the context of a batch operation.
* If it is not, and the transaction is marked as batch, this call is a no-op.
* 将所有的数据库标记为成功。
* 参数:是否由批处理引发的,如果不是,并且当前事务为批处理(batch标记为true),那么此调用为无用调用。
*/
public void markSuccessful(boolean callerIsBatch) {
if (!mBatch || callerIsBatch) {
for (SQLiteDatabase db : mDatabasesForTransaction) {
db.setTransactionSuccessful();
}
}
} /**
* Completes the transaction, ending the DB transactions for all associated databases.
* @param callerIsBatch Whether this is being performed in the context of a batch operation.
* If it is not, and the transaction is marked as batch, this call is a no-op.
* 终止与此事务相关的数据库上的所有事务。
* 根据mYieldFaild决定是否在finish前终止数据库上的事务(db.endTransaction())
* 参数:是否由批处理引发的,如果不是,并且当前事务为批处理(batch标记为true),那么此调用为无用调用。
*/
public void finish(boolean callerIsBatch) {
if (!mBatch || callerIsBatch) {
for (SQLiteDatabase db : mDatabasesForTransaction) {
// If an exception was thrown while yielding, it's possible that we no longer have
// a lock on this database, so we need to check before attempting to end its
// transaction. Otherwise, we should always expect to be in a transaction (and will
// throw an exception if this is not the case).
if (mYieldFailed && !db.isDbLockedByCurrentThread()) {
// We no longer hold the lock, so don't do anything with this database.
continue;
}
db.endTransaction();
}
mDatabasesForTransaction.clear();
mDatabaseTagMap.clear();
mIsDirty = false;
}
}
}
联系人数据库设计之ContactsTransaction的更多相关文章
- 联系人数据库设计之AbstractContactsProvider
个人见解,欢迎交流. 联系人数据库设计,源代码下载请自行去android官网下载. package com.android.providers.contacts; import android.con ...
- MySQL 数据库设计 笔记与总结(1)需求分析
数据库设计的步骤 ① 需求分析 ② 逻辑设计 使用 ER 图对数据库进行逻辑建模 ③ 物理设计 ④ 维护优化 a. 新的需求进行建表 b. 索引优化 c. 大表拆分 [需求分析] ① 了解系统中所要存 ...
- 一、MySQL中的索引 二、MySQL中的函数 三、MySQL数据库的备份和恢复 四、数据库设计和优化(重点)
一.MySQL中的索引###<1>索引的概念 索引就是一种数据结构(高效获取数据),在mysql中以文件的方式存在.存储建立了索引列的地址或者指向. 文件 :(以某种数据 结构存放) 存放 ...
- 电子商务(电销)平台中订单模块(Order)数据库设计明细
电子商务(电销)平台中订单模块(Order)数据库设计明细 - sochishun - 博客园 http://www.cnblogs.com/sochishun/p/7040628.html 电子商务 ...
- 电子商务(电销)平台中内容模块(Content)数据库设计明细
以下是自己在电子商务系统设计中的数据库设计经验总结,而今发表出来一起分享,如有不当,欢迎跟帖讨论~ 文章表 (article)|-- 自动编号|-- 文章标题 (title)|-- 文章类别编号 (c ...
- web-51job(前程无忧)-账户、简历-数据库设计
ylbtech-DatabaseDesgin:web-51job(前程无忧)-账户.简历-数据库设计 1.A,数据库关系图 1.B,数据库设计脚本 /App_Data/1,Account.sql ...
- CMDB数据库设计
title: CMDB 数据库设计 tags: Django --- CMDB数据库设计 具体的资产 服务器表和网卡.内存.硬盘是一对多的关系,一个服务器可以有多个网卡.多个内存.多个硬盘 hostn ...
- [SQL] 外卖系统数据库设计
注意: 1.项目需求:小程序外卖系统,以美团,饿了么为参考. 2.表设计没有外键约束,设计是在程序中进行外键约束. 3.希望通过分享该数据库设计,获取大家的建议和讨论. SQL: CREATE DAT ...
- 数据库设计_ERMaster安装使用_PowerDesigner数据设计工具
数据库设计 1. 说在前面 项目开发的流程包括哪些环节 需求调研[需求调研报告]-- 公司决策层 (1) 根据市场公司需求分析公司是否需要开发软件来辅助日常工作 (2) 公司高层市场考察,市场分析,决 ...
随机推荐
- django学习之Model(三)QuerySet
接下来主要学习Models中的Making queries 写好models.py后,django会自动提供一个数据库的抽象API,来实现CRUD(create, retrieve, update, ...
- java入门(1) 程序运行机制及运行过程
首先我们来看一下java程序在底层是怎么工作的: JAVA有两种核心机制: Java虚拟机(Java Virtual Machine): 1.java虚拟机可以理解成一个以字节码为机器指令的CPU. ...
- 同步机制Lock初学(转)
前段时间review同事的代码,发现可以简化成如下的模型: Thread 1 Thread 2 lock.lock(); condition.await(); lock.unlock() lock.l ...
- Fiddler 教程(转)
阅读目录 Fiddler的基本介绍 Fiddler的工作原理 同类的其它工具 Fiddler如何捕获Firefox的会话 Fiddler如何捕获HTTPS会话 Fiddler的基本界面 Fiddler ...
- 使用ant的jar任务打jar包
<?xml version="1.0" encoding="UTF-8"?> <project name="javaTest&quo ...
- (读书笔记).NET大局观-.NET框架类库概观
.NET框架类库概况 构建在.NET框架上所有的软件,都会用到通用语言进行时,即使基于最简单的CLR程序,也需要用到一部分.NET框架类库,更精致复杂的软件则使用这个类库提供的更多服务. .NET框架 ...
- JAVA类(上)
package test; public class staticAccess { public int age; public staticAccess grow() { age++; return ...
- P65
#include<stdio.h> #define N 6 main() { char c[N]; int i=0; for(;i<N;c[i]=getchar(),i++); pr ...
- YT工作日志-0911
上午 在导师的帮助下,帮我从svn上下载了项目.但是因为项目太大(不算jar包有730M),很多模块不是我工作中涉及的,但是运行的时候会报错,所以导师还帮我找了很多错误.把那些不需要的东西注释了.就这 ...
- PHP - 验证用户名
/** * * 函数名:_check_username($user_str,$min_num,$max_num); * 作用:检测用户名是否符合格式 * 参数: * 1:用户名 * 2:不得小于多少位 ...