Auto refactor Only: s/Account/Device/g

This commit is contained in:
Matt Corallo
2014-01-11 10:56:29 -10:00
parent 5a837d4481
commit bd6cf10402
20 changed files with 217 additions and 298 deletions

View File

@@ -58,15 +58,15 @@ public abstract class Accounts {
GCM_ID + ", " + APN_ID + ", " + SUPPORTS_SMS + ") " +
"VALUES (:number, :device_id, :auth_token, :salt, :signaling_key, :fetches_messages, :gcm_id, :apn_id, :supports_sms)")
@GetGeneratedKeys
abstract long insertStep(@AccountBinder Account account);
abstract long insertStep(@AccountBinder Device device);
@SqlQuery("SELECT " + DEVICE_ID + " FROM accounts WHERE " + NUMBER + " = :number ORDER BY " + DEVICE_ID + " DESC LIMIT 1 FOR UPDATE")
abstract long getHighestDeviceId(@Bind("number") String number);
@Transaction(TransactionIsolationLevel.SERIALIZABLE)
public long insert(@AccountBinder Account account) {
account.setDeviceId(getHighestDeviceId(account.getNumber()) + 1);
return insertStep(account);
public long insert(@AccountBinder Device device) {
device.setDeviceId(getHighestDeviceId(device.getNumber()) + 1);
return insertStep(device);
}
@SqlUpdate("DELETE FROM accounts WHERE " + NUMBER + " = :number RETURNING id")
@@ -76,41 +76,41 @@ public abstract class Accounts {
SIGNALING_KEY + " = :signaling_key, " + GCM_ID + " = :gcm_id, " + APN_ID + " = :apn_id, " +
FETCHES_MESSAGES + " = :fetches_messages, " + SUPPORTS_SMS + " = :supports_sms " +
"WHERE " + NUMBER + " = :number AND " + DEVICE_ID + " = :device_id")
abstract void update(@AccountBinder Account account);
abstract void update(@AccountBinder Device device);
@Mapper(AccountMapper.class)
@SqlQuery("SELECT * FROM accounts WHERE " + NUMBER + " = :number AND " + DEVICE_ID + " = :device_id")
abstract Account get(@Bind("number") String number, @Bind("device_id") long deviceId);
abstract Device get(@Bind("number") String number, @Bind("device_id") long deviceId);
@SqlQuery("SELECT COUNT(DISTINCT " + NUMBER + ") from accounts")
abstract long getNumberCount();
@Mapper(AccountMapper.class)
@SqlQuery("SELECT * FROM accounts WHERE " + DEVICE_ID + " = 1 OFFSET :offset LIMIT :limit")
abstract List<Account> getAllFirstAccounts(@Bind("offset") int offset, @Bind("limit") int length);
abstract List<Device> getAllFirstAccounts(@Bind("offset") int offset, @Bind("limit") int length);
@Mapper(AccountMapper.class)
@SqlQuery("SELECT * FROM accounts WHERE " + DEVICE_ID + " = 1")
public abstract Iterator<Account> getAllFirstAccounts();
public abstract Iterator<Device> getAllFirstAccounts();
@Mapper(AccountMapper.class)
@SqlQuery("SELECT * FROM accounts WHERE " + NUMBER + " = :number")
public abstract List<Account> getAllByNumber(@Bind("number") String number);
public abstract List<Device> getAllByNumber(@Bind("number") String number);
@Transaction(TransactionIsolationLevel.SERIALIZABLE)
public long insertClearingNumber(Account account) {
removeAccountsByNumber(account.getNumber());
account.setDeviceId(getHighestDeviceId(account.getNumber()) + 1);
return insertStep(account);
public long insertClearingNumber(Device device) {
removeAccountsByNumber(device.getNumber());
device.setDeviceId(getHighestDeviceId(device.getNumber()) + 1);
return insertStep(device);
}
public static class AccountMapper implements ResultSetMapper<Account> {
public static class AccountMapper implements ResultSetMapper<Device> {
@Override
public Account map(int i, ResultSet resultSet, StatementContext statementContext)
public Device map(int i, ResultSet resultSet, StatementContext statementContext)
throws SQLException
{
return new Account(resultSet.getLong(ID), resultSet.getString(NUMBER), resultSet.getLong(DEVICE_ID),
return new Device(resultSet.getLong(ID), resultSet.getString(NUMBER), resultSet.getLong(DEVICE_ID),
resultSet.getString(AUTH_TOKEN), resultSet.getString(SALT),
resultSet.getString(SIGNALING_KEY), resultSet.getString(GCM_ID),
resultSet.getString(APN_ID),
@@ -125,23 +125,23 @@ public abstract class Accounts {
public static class AccountBinderFactory implements BinderFactory {
@Override
public Binder build(Annotation annotation) {
return new Binder<AccountBinder, Account>() {
return new Binder<AccountBinder, Device>() {
@Override
public void bind(SQLStatement<?> sql,
AccountBinder accountBinder,
Account account)
Device device)
{
sql.bind(ID, account.getId());
sql.bind(NUMBER, account.getNumber());
sql.bind(DEVICE_ID, account.getDeviceId());
sql.bind(AUTH_TOKEN, account.getAuthenticationCredentials()
sql.bind(ID, device.getId());
sql.bind(NUMBER, device.getNumber());
sql.bind(DEVICE_ID, device.getDeviceId());
sql.bind(AUTH_TOKEN, device.getAuthenticationCredentials()
.getHashedAuthenticationToken());
sql.bind(SALT, account.getAuthenticationCredentials().getSalt());
sql.bind(SIGNALING_KEY, account.getSignalingKey());
sql.bind(GCM_ID, account.getGcmRegistrationId());
sql.bind(APN_ID, account.getApnRegistrationId());
sql.bind(SUPPORTS_SMS, account.getSupportsSms() ? 1 : 0);
sql.bind(FETCHES_MESSAGES, account.getFetchesMessages() ? 1 : 0);
sql.bind(SALT, device.getAuthenticationCredentials().getSalt());
sql.bind(SIGNALING_KEY, device.getSignalingKey());
sql.bind(GCM_ID, device.getGcmRegistrationId());
sql.bind(APN_ID, device.getApnRegistrationId());
sql.bind(SUPPORTS_SMS, device.getSupportsSms() ? 1 : 0);
sql.bind(FETCHES_MESSAGES, device.getFetchesMessages() ? 1 : 0);
}
};
}

View File

@@ -44,84 +44,84 @@ public class AccountsManager {
return accounts.getNumberCount();
}
public List<Account> getAllMasterAccounts(int offset, int length) {
public List<Device> getAllMasterAccounts(int offset, int length) {
return accounts.getAllFirstAccounts(offset, length);
}
public Iterator<Account> getAllMasterAccounts() {
public Iterator<Device> getAllMasterAccounts() {
return accounts.getAllFirstAccounts();
}
/** Creates a new Account and NumberData, clearing all existing accounts/data on the given number */
public void createResetNumber(Account account) {
long id = accounts.insertClearingNumber(account);
account.setId(id);
/** Creates a new Device and NumberData, clearing all existing accounts/data on the given number */
public void createResetNumber(Device device) {
long id = accounts.insertClearingNumber(device);
device.setId(id);
if (memcachedClient != null) {
memcachedClient.set(getKey(account.getNumber(), account.getDeviceId()), 0, account);
memcachedClient.set(getKey(device.getNumber(), device.getDeviceId()), 0, device);
}
updateDirectory(account);
updateDirectory(device);
}
/** Creates a new Account for an existing NumberData (setting the deviceId) */
public void createAccountOnExistingNumber(Account account) {
long id = accounts.insert(account);
account.setId(id);
/** Creates a new Device for an existing NumberData (setting the deviceId) */
public void createAccountOnExistingNumber(Device device) {
long id = accounts.insert(device);
device.setId(id);
if (memcachedClient != null) {
memcachedClient.set(getKey(account.getNumber(), account.getDeviceId()), 0, account);
memcachedClient.set(getKey(device.getNumber(), device.getDeviceId()), 0, device);
}
updateDirectory(account);
updateDirectory(device);
}
public void update(Account account) {
public void update(Device device) {
if (memcachedClient != null) {
memcachedClient.set(getKey(account.getNumber(), account.getDeviceId()), 0, account);
memcachedClient.set(getKey(device.getNumber(), device.getDeviceId()), 0, device);
}
accounts.update(account);
updateDirectory(account);
accounts.update(device);
updateDirectory(device);
}
public Optional<Account> get(String number, long deviceId) {
Account account = null;
public Optional<Device> get(String number, long deviceId) {
Device device = null;
if (memcachedClient != null) {
account = (Account)memcachedClient.get(getKey(number, deviceId));
device = (Device)memcachedClient.get(getKey(number, deviceId));
}
if (account == null) {
account = accounts.get(number, deviceId);
if (device == null) {
device = accounts.get(number, deviceId);
if (account != null && memcachedClient != null) {
memcachedClient.set(getKey(number, deviceId), 0, account);
if (device != null && memcachedClient != null) {
memcachedClient.set(getKey(number, deviceId), 0, device);
}
}
if (account != null) return Optional.of(account);
if (device != null) return Optional.of(device);
else return Optional.absent();
}
public List<Account> getAllByNumber(String number) {
public List<Device> getAllByNumber(String number) {
return accounts.getAllByNumber(number);
}
private void updateDirectory(Account account) {
if (account.getDeviceId() != 1)
private void updateDirectory(Device device) {
if (device.getDeviceId() != 1)
return;
if (account.isActive()) {
byte[] token = Util.getContactToken(account.getNumber());
ClientContact clientContact = new ClientContact(token, null, account.getSupportsSms());
if (device.isActive()) {
byte[] token = Util.getContactToken(device.getNumber());
ClientContact clientContact = new ClientContact(token, null, device.getSupportsSms());
directory.add(clientContact);
} else {
directory.remove(account.getNumber());
directory.remove(device.getNumber());
}
}
private String getKey(String number, long accountId) {
return Account.class.getSimpleName() + Account.MEMCACHE_VERION + number + accountId;
return Device.class.getSimpleName() + Device.MEMCACHE_VERION + number + accountId;
}
}

View File

@@ -22,7 +22,7 @@ import org.whispersystems.textsecuregcm.util.Util;
import java.io.Serializable;
public class Account implements Serializable {
public class Device implements Serializable {
public static final int MEMCACHE_VERION = 1;
@@ -42,11 +42,11 @@ public class Account implements Serializable {
private boolean supportsSms;
private boolean fetchesMessages;
public Account() {}
public Device() {}
public Account(long id, String number, long deviceId, String hashedAuthenticationToken, String salt,
String signalingKey, String gcmRegistrationId, String apnRegistrationId,
boolean supportsSms, boolean fetchesMessages)
public Device(long id, String number, long deviceId, String hashedAuthenticationToken, String salt,
String signalingKey, String gcmRegistrationId, String apnRegistrationId,
boolean supportsSms, boolean fetchesMessages)
{
this.id = id;
this.number = number;

View File

@@ -79,10 +79,10 @@ public abstract class Keys {
}
@Transaction(TransactionIsolationLevel.SERIALIZABLE)
public UnstructuredPreKeyList get(String number, List<Account> accounts) {
public UnstructuredPreKeyList get(String number, List<Device> devices) {
List<PreKey> preKeys = new LinkedList<>();
for (Account account : accounts) {
PreKey preKey = retrieveFirst(number, account.getDeviceId());
for (Device device : devices) {
PreKey preKey = retrieveFirst(number, device.getDeviceId());
if (preKey != null)
preKeys.add(preKey);
}

View File

@@ -27,11 +27,11 @@ public class StoredMessageManager {
this.storedMessages = storedMessages;
}
public void storeMessage(Account account, EncryptedOutgoingMessage outgoingMessage) throws IOException {
storedMessages.insert(account.getId(), outgoingMessage.serialize());
public void storeMessage(Device device, EncryptedOutgoingMessage outgoingMessage) throws IOException {
storedMessages.insert(device.getId(), outgoingMessage.serialize());
}
public List<String> getStoredMessage(Account account) {
return storedMessages.getMessagesForAccountId(account.getId());
public List<String> getStoredMessage(Device device) {
return storedMessages.getMessagesForAccountId(device.getId());
}
}