不当之处,请雅正。

请自行下载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的更多相关文章

  1. 联系人数据库设计之AbstractContactsProvider

    个人见解,欢迎交流. 联系人数据库设计,源代码下载请自行去android官网下载. package com.android.providers.contacts; import android.con ...

  2. MySQL 数据库设计 笔记与总结(1)需求分析

    数据库设计的步骤 ① 需求分析 ② 逻辑设计 使用 ER 图对数据库进行逻辑建模 ③ 物理设计 ④ 维护优化 a. 新的需求进行建表 b. 索引优化 c. 大表拆分 [需求分析] ① 了解系统中所要存 ...

  3. 一、MySQL中的索引 二、MySQL中的函数 三、MySQL数据库的备份和恢复 四、数据库设计和优化(重点)

    一.MySQL中的索引###<1>索引的概念 索引就是一种数据结构(高效获取数据),在mysql中以文件的方式存在.存储建立了索引列的地址或者指向. 文件 :(以某种数据 结构存放) 存放 ...

  4. 电子商务(电销)平台中订单模块(Order)数据库设计明细

    电子商务(电销)平台中订单模块(Order)数据库设计明细 - sochishun - 博客园 http://www.cnblogs.com/sochishun/p/7040628.html 电子商务 ...

  5. 电子商务(电销)平台中内容模块(Content)数据库设计明细

    以下是自己在电子商务系统设计中的数据库设计经验总结,而今发表出来一起分享,如有不当,欢迎跟帖讨论~ 文章表 (article)|-- 自动编号|-- 文章标题 (title)|-- 文章类别编号 (c ...

  6. web-51job(前程无忧)-账户、简历-数据库设计

    ylbtech-DatabaseDesgin:web-51job(前程无忧)-账户.简历-数据库设计   1.A,数据库关系图 1.B,数据库设计脚本 /App_Data/1,Account.sql ...

  7. CMDB数据库设计

    title: CMDB 数据库设计 tags: Django --- CMDB数据库设计 具体的资产 服务器表和网卡.内存.硬盘是一对多的关系,一个服务器可以有多个网卡.多个内存.多个硬盘 hostn ...

  8. [SQL] 外卖系统数据库设计

    注意: 1.项目需求:小程序外卖系统,以美团,饿了么为参考. 2.表设计没有外键约束,设计是在程序中进行外键约束. 3.希望通过分享该数据库设计,获取大家的建议和讨论. SQL: CREATE DAT ...

  9. 数据库设计_ERMaster安装使用_PowerDesigner数据设计工具

    数据库设计 1. 说在前面 项目开发的流程包括哪些环节 需求调研[需求调研报告]-- 公司决策层 (1) 根据市场公司需求分析公司是否需要开发软件来辅助日常工作 (2) 公司高层市场考察,市场分析,决 ...

随机推荐

  1. java.lang.IllegalStateException: No data type for node: org.hibernate.hql.ast.tree.MethodNode(尼玛,蛋疼的错误)

    java.lang.IllegalStateException: No data type for node: org.hibernate.hql.ast.tree.MethodNode   \-[M ...

  2. docker学习笔记15:Dockerfile 指令 USER介绍

    USER指令用于指定容器执行程序的用户身份,默认是 root用户. 在docker run 中可以通过 -u 选项来覆盖USER指令的设置. 举例:docker run -i -t -u mysql ...

  3. Android常用动画alpha和rotate同时使用

    Android的动画可以是一种动画,也可以多种动画作用于一张图片上,如RotaeAnimation和AlphaAnimation同时放到一个配置文件中 alpha1.xml <?xml vers ...

  4. iOS:(接口适配器3)--iPhone适应不同型号 6/6plus 前

    对于不同的苹果设备.检查每个参数<iOS:机型參数.sdk.xcode各版本号>.        机型变化 坐标:表示屏幕物理尺寸大小,坐标变大了.表示机器屏幕尺寸变大了: 像素:表示屏幕 ...

  5. hdu1397(素数组和成偶数的个数 用 标记法)

    Problem Description Goldbach's Conjecture: For any even number n greater than or equal to 4, there e ...

  6. 05-IOSCore - 单例模式、KVO

    单例模式 是设计模式之一,使用频率高,让数据或对象在程序的各个地方都能访问,保持唯一 要素: 各个地方都能访问方法 + 静态消息 只要导入类 就能访问 保持唯一 1.在静态消息内限制对象的创建 2.外 ...

  7. 15-UIKit(view布局、Autoresizing)

    目录: 1. 纯代码布局 2. 在View中进行代码布局 3. Autoresizing 回到顶部 1. 纯代码布局 纯代码布局分VC下和V下 [MX1-layout-code] 在VC下覆盖view ...

  8. WCF技术剖析之二十七: 如何将一个服务发布成WSDL[编程篇]

    原文:WCF技术剖析之二十七: 如何将一个服务发布成WSDL[编程篇] 对于WCF服务端元数据架构体系来说,通过MetadataExporter将服务的终结点导出成MetadataSet(参考< ...

  9. html5新特性:利用history的pushState等方法来解决使用ajax导致页面后退和前进的问题

    一.背景 使用ajax,可以实现不需要刷新整个页面就可以进行局部页面的更新.这样可以开发交互性很强的富客户端程序,减少网络传输的内容.但长期以来存在一个问题,就是无法利用浏览器本身提供的前进和后退按钮 ...

  10. mysql服务无法启动

    可能是没有data文件夹,可以新建或拷贝以前的data文件夹到安装目录.