<commit_before>package de.butterworks.awscommons.dynamo;


import com.amazonaws.services.dynamodbv2.document.*;
import com.amazonaws.services.dynamodbv2.document.spec.ScanSpec;
import com.amazonaws.services.dynamodbv2.model.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

public abstract class AbstractDynamoTable<T extends Identifyable> implements CrudInterface<T> {

    private final String tableName;
    private final long readCapacityUnits;
    private final long writeCapacityUnits;
    private final Table table;
    private final DynamoConverter<T> converter;
    private final List<String> secondaryIndexNames;

    private static final Logger logger = LoggerFactory.getLogger(AbstractDynamoTable.class);

    public String getTableName() {
        return tableName;
    }

    public long getReadCapacityUnits() {
        return readCapacityUnits;
    }

    public long getWriteCapacityUnits() {
        return writeCapacityUnits;
    }

    public Table getTable() {
        return table;
    }

    public List<String> getSecondaryIndexNames() {
        return secondaryIndexNames;
    }

    public DynamoConverter<T> getConverter() {
        return converter;
    }

    protected AbstractDynamoTable(final String tableName,
                                  final long readCapacityUnits,
                                  final long writeCapacityUnits,
                                  final DynamoConverter<T> converter,
                                  final boolean checkTableExistence,
                                  final List<String> secondaryIndexNames) {

        this.tableName = tableName;
        this.readCapacityUnits = readCapacityUnits;
        this.writeCapacityUnits = writeCapacityUnits;
        this.converter = converter;
        this.secondaryIndexNames = secondaryIndexNames;

        logger.debug(String.format("Initializing table %s with R/W capacity %s / %s", tableName, readCapacityUnits, writeCapacityUnits));

        if (checkTableExistence) {
            if (!DynamoCommons.getInstance().tableExists(tableName)) {
                logger.info("Table " + tableName + " does not exist. Creating...");
                createTable();
            }
        }
        table = DynamoCommons.getInstance().getDb().getTable(tableName);
    }

    protected AbstractDynamoTable(final String tableName, final long readCapacityUnits, final long writeCapacityUnits, final DynamoConverter<T> converter) {
        this(tableName, readCapacityUnits, writeCapacityUnits, converter, false, new ArrayList<>());
    }

    private void createTable() {
        try {
            final ArrayList<KeySchemaElement> keySchema = new ArrayList<>();
            keySchema.add(new KeySchemaElement()
                    .withAttributeName("id")
                    .withKeyType(KeyType.HASH));

            final ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<>();
            attributeDefinitions.add(new AttributeDefinition()
                    .withAttributeName("id")
                    .withAttributeType("S"));

            final ArrayList<GlobalSecondaryIndex> globalSecondaryIndices = new ArrayList<>();
            for (final String indexName : secondaryIndexNames) {
                attributeDefinitions.add(new AttributeDefinition()
                        .withAttributeName(indexName)
                        .withAttributeType("S"));

                globalSecondaryIndices.add(new GlobalSecondaryIndex()
                        .withIndexName(indexName + "-index")
                        .withKeySchema(new KeySchemaElement().withAttributeName(indexName).withKeyType(KeyType.HASH))
                        .withProjection(new Projection().withProjectionType(ProjectionType.ALL))
                        .withProvisionedThroughput(new ProvisionedThroughput()
                                .withReadCapacityUnits(readCapacityUnits)
                                .withWriteCapacityUnits(writeCapacityUnits)));
            }

            final CreateTableRequest request = new CreateTableRequest()
                    .withTableName(tableName)
                    .withKeySchema(keySchema)
                    .withGlobalSecondaryIndexes(globalSecondaryIndices)
                    .withProvisionedThroughput(new ProvisionedThroughput()
                            .withReadCapacityUnits(readCapacityUnits)
                            .withWriteCapacityUnits(writeCapacityUnits));
<commit_msg>Fix 2ndary index NPE<commit_after>package de.butterworks.awscommons.dynamo;


import com.amazonaws.services.dynamodbv2.document.*;
import com.amazonaws.services.dynamodbv2.document.spec.ScanSpec;
import com.amazonaws.services.dynamodbv2.model.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

public abstract class AbstractDynamoTable<T extends Identifyable> implements CrudInterface<T> {

    private final String tableName;
    private final long readCapacityUnits;
    private final long writeCapacityUnits;
    private final Table table;
    private final DynamoConverter<T> converter;
    private final List<String> secondaryIndexNames;

    private static final Logger logger = LoggerFactory.getLogger(AbstractDynamoTable.class);

    public String getTableName() {
        return tableName;
    }

    public long getReadCapacityUnits() {
        return readCapacityUnits;
    }

    public long getWriteCapacityUnits() {
        return writeCapacityUnits;
    }

    public Table getTable() {
        return table;
    }

    public List<String> getSecondaryIndexNames() {
        return secondaryIndexNames;
    }

    public DynamoConverter<T> getConverter() {
        return converter;
    }

    protected AbstractDynamoTable(final String tableName,
                                  final long readCapacityUnits,
                                  final long writeCapacityUnits,
                                  final DynamoConverter<T> converter,
                                  final boolean checkTableExistence,
                                  final List<String> secondaryIndexNames) {

        this.tableName = tableName;
        this.readCapacityUnits = readCapacityUnits;
        this.writeCapacityUnits = writeCapacityUnits;
        this.converter = converter;
        this.secondaryIndexNames = secondaryIndexNames;

        logger.debug(String.format("Initializing table %s with R/W capacity %s / %s", tableName, readCapacityUnits, writeCapacityUnits));

        if (checkTableExistence) {
            if (!DynamoCommons.getInstance().tableExists(tableName)) {
                logger.info("Table " + tableName + " does not exist. Creating...");
                createTable();
            }
        }
        table = DynamoCommons.getInstance().getDb().getTable(tableName);
    }

    protected AbstractDynamoTable(final String tableName, final long readCapacityUnits, final long writeCapacityUnits, final DynamoConverter<T> converter) {
        this(tableName, readCapacityUnits, writeCapacityUnits, converter, false, new ArrayList<>());
    }

    private void createTable() {
        try {
            final ArrayList<KeySchemaElement> keySchema = new ArrayList<>();
            keySchema.add(new KeySchemaElement()
                    .withAttributeName("id")
                    .withKeyType(KeyType.HASH));

            final ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<>();
            attributeDefinitions.add(new AttributeDefinition()
                    .withAttributeName("id")
                    .withAttributeType("S"));

            final ArrayList<GlobalSecondaryIndex> globalSecondaryIndices = new ArrayList<>();

            if(secondaryIndexNames != null) {
                for (final String indexName : secondaryIndexNames) {
                    attributeDefinitions.add(new AttributeDefinition()
                            .withAttributeName(indexName)
                            .withAttributeType("S"));

                    globalSecondaryIndices.add(new GlobalSecondaryIndex()
                            .withIndexName(indexName + "-index")
                            .withKeySchema(new KeySchemaElement().withAttributeName(indexName).withKeyType(KeyType.HASH))
                            .withProjection(new Projection().withProjectionType(ProjectionType.ALL))
                            .withProvisionedThroughput(new ProvisionedThroughput()
                                    .withReadCapacityUnits(readCapacityUnits)
                                    .withWriteCapacityUnits(writeCapacityUnits)));
                }
            }

            final CreateTableRequest request = new CreateTableRequest()
                    .withTableName(tableName)
                    .withKeySchema(keySchema)
                    .withGlobalSecondaryIndexes(globalSecondaryIndices)
                    .withProvisionedThroughput(new ProvisionedThroughput()
                            .withReadCapacityUnits(readCapacityUnits)
                            .withWriteCapacityUnits(writeCapacityUnits));

{'fraction_non_alphanumeric': 0.07788836001730852, 'fraction_numerical': 0.0011899610558199914, 'mean_word_length': 2.2644774011299433, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3753825', 'n_tokens_mistral': 2233, 'n_tokens_neox': 2152, 'n_words': 443}