联系人数据库设计之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) 公司高层市场考察,市场分析,决 ...
随机推荐
- 基于visual Studio2013解决C语言竞赛题之0601判断素数函数
题目 解决代码及点评 //编写一函数判断一个数是否为素数 #include<stdio.h> #include <stdlib.h> # ...
- Android新增API之AudioEffect中文API与应用实例
在Android2.3中增加了对音频混响的支持,这些API包含在android.media.audiofx包中. 一.概述 AudioEffect是android audio framework(an ...
- PopupWindow的使用以及ArrayAdatper.notifyDataSetChanged()无效详解
Android的对话框有两种:PopupWindow和AlertDialog.它们的不同点在于: AlertDialog的位置固定,而PopupWindow的位置可以随意 AlertDialog是非阻 ...
- 开源框架ViewPagerIndicator的使用——TabPageIndicator
1.导入Android-ViewPagerIndicator库文件 下载地址:https://github.com/JakeWharton/ViewPagerIndicator 2.布局文件 ...
- 自定义Log4cpp的日志输出格式
// 1. 实例化一个PatternLayout对象 log4cpp::PatternLayout* pLayout = new log4cpp::PatternLayout(); // 2. 实例化 ...
- BZOJ 3477: [Usaco2014 Mar]Sabotage( 二分答案 )
先二分答案m, 然后对于原序列 A[i] = A[i] - m, 然后O(n)找最大连续子序列和, 那么此时序列由 L + mx + R组成. L + mx + R = sum - n * m, s ...
- android4.0移植,拨号异常
D/dalvikvm( 2274): GC_CONCURRENT freed 206K, 12% free 6571K/7431K, paused 2ms+3ms D/dalvikvm( 2274): ...
- pyfits例子
下面是一个读入fits文件,画积分强度图,再把某个星表里的天体画到图上的python程序. ====================================================== ...
- JavaScript DOM省市自适配select菜单
<html> <head> <meta charset="UTF-8"> <meta name="Generator" ...
- 12-UIKit(View绘制、绘制曲线、绘制文字、贴图)
目录: 1. View绘制 2. 绘制曲线 3. 绘制文字 4. 贴图 回到顶部 1. View绘制 1.1 做出自己的视图对象 TRCell : UITableViewCell : UIView U ...