<commit_before>package com.yammer.guava.collections.backup.azure;

// TODO implement
public class AzureBackupTableFactory {
}<commit_msg>added azure wiring to the backup solution<commit_after>package com.yammer.guava.collections.backup.azure;

import com.google.common.base.Throwables;
import com.google.common.collect.Table;
import com.microsoft.windowsazure.services.core.storage.StorageException;
import com.microsoft.windowsazure.services.table.client.CloudTable;
import com.microsoft.windowsazure.services.table.client.CloudTableClient;
import com.yammer.guava.collections.azure.AzureTable;
import com.yammer.guava.collections.azure.StringAzureTable;
import com.yammer.guava.collections.backup.lib.BackupTableFactory;

import java.net.URISyntaxException;
import java.util.Date;

import static com.yammer.guava.collections.backup.lib.Backup.BackupStatus;

public class AzureBackupTableFactory implements BackupTableFactory {
    private static final String BACKUP_LIST_TABLE_NAME = "com_yammer_guava_collections_backup_azure_backup_list_table_name";
    private static final String BACKUP_TABLE_NAME_TEMPLATE = "com_yammer_guava_collections_backup___NAME_%s___DATE_%s";
    private static final AzureTable.Marshaller<String, String> IDENTITY_MARSHALLER = new AzureTable.Marshaller<String, String>() {
        @Override
        public String marshal(String unmarshalled) {
            return unmarshalled;
        }

        @Override
        public String unmarshal(String marshalled) {
            return marshalled;
        }

        @Override
        public Class<String> getType() {
            return String.class;
        }
    };
    private static final AzureTable.Marshaller<Date, String> DATE_MARSHALLER = new AzureTable.Marshaller<Date, String>() {
        @Override
        public String marshal(Date unmarshalled) {
            return Long.toString(unmarshalled.getTime());
        }

        @Override
        public Date unmarshal(String marshalled) {
            return new Date(Long.parseLong(marshalled));
        }

        @Override
        public Class<Date> getType() {
            return Date.class;
        }
    };
    private static final AzureTable.Marshaller<BackupStatus, String> BACKUP_STATUS_MARSHALLER = new AzureTable.Marshaller<BackupStatus, String>() {

        @Override
        public String marshal(BackupStatus unmarshalled) {
            return unmarshalled.toString();
        }

        @Override
        public BackupStatus unmarshal(String marshalled) {
            return BackupStatus.valueOf(marshalled);
        }

        @Override
        public Class<BackupStatus> getType() {
            return BackupStatus.class;
        }
    };
    private final CloudTableClient cloudTableClient;

    public AzureBackupTableFactory(CloudTableClient cloudTableClient) {
        this.cloudTableClient = cloudTableClient;
    }

    @Override
    public Table<String, Date, BackupStatus> getBackupListTable() {
        try {
            return new AzureTable<>(
                    IDENTITY_MARSHALLER,
                    DATE_MARSHALLER,
                    BACKUP_STATUS_MARSHALLER,
                    getOrCreateTable(BACKUP_LIST_TABLE_NAME)
            );
        } catch (Exception e) {
            throw Throwables.propagate(e);
        }
    }

    @Override
    public Table<String, String, String> createBackupTable(Date backupDate, String backupName) {
        try {
            final String backupTableName = createBackupTableName(backupDate, backupName);
            return getOrCreateTable(backupTableName);
        } catch (Exception e) {
            throw Throwables.propagate(e);
        }
    }

    @Override
    public void removeTable(Date backupDate, String backupName) {
        final String backupTableName = createBackupTableName(backupDate, backupName);
        try {
            CloudTable cloudTable = cloudTableClient.getTableReference(backupTableName);
            cloudTable.deleteIfExists();
        } catch (Exception e) {
            throw Throwables.propagate(e);
        }
    }

    @Override
    public Table<String, String, String> getBackupTable(Date backupDate, String backupName) {
        try {
            final String backupTableName = createBackupTableName(backupDate, backupName);
            return new StringAzureTable(backupTableName, cloudTableClient);
        } catch (Exception e) {
            throw Throwables.propagate(e);
        }
    }

    private String createBackupTableName(Date backupDate, String backupName) {
        return String.format(BACKUP_TABLE_NAME_TEMPLATE, backupName, backupDate.getTime());
    }

    private StringAzureTable getOrCreateTable(String tableName) throws URISyntaxException, StorageException {
        CloudTable table = cloudTableClient.getTableReference(tableName);
        table.createIfNotExist();
        return new StringAzureTable(tableName, cloudTableClient);
    }

}
{'fraction_non_alphanumeric': 0.0765940110002037, 'fraction_numerical': 0.0, 'mean_word_length': 2.946945337620579, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4235895', 'n_tokens_mistral': 1342, 'n_tokens_neox': 1312, 'n_words': 264}