<commit_before>import * as Q from 'q';
import GetPartitionsCodec = require('./codec/GetPartitionsCodec');
import ClientMessage = require('./ClientMessage');
import Address = require('./Address');
import HazelcastClient from './HazelcastClient';

class PartitionService {

    private client: HazelcastClient;
    private partitionMap: {[partitionId: number]: Address};
    private partitionCount: number;

    constructor(client: HazelcastClient) {
        this.client = client;
    }

    initialize(): Q.Promise<PartitionService> {
        var deferred = Q.defer<PartitionService>();
        this.refresh().then(() => {
            deferred.resolve(this);
        });

        return deferred.promise;
    }

    /**
     * Refreshes the internal partition table.
     * @returns {Q.Promise<U>}
     */
    refresh(): Q.Promise<void> {
        var ownerConnection = this.client.getClusterService().getOwnerConnection();
        var clientMessage: ClientMessage = GetPartitionsCodec.encodeRequest();

        return this.client.getInvocationService()
            .invokeOnConnection(ownerConnection, clientMessage)
            .then((clientMessage: ClientMessage) => {
                this.partitionMap = GetPartitionsCodec.decodeResponse(clientMessage);
                this.partitionCount = Object.keys(this.partitionMap).length;
            });
    };

    /**
     * Returns the {@link Address} of the node which owns given partition id.
     * @param partitionId
     * @returns the address of the node.
     */
    getAddressForPartition(partitionId: number): Address {
        return this.partitionMap[partitionId];
    }

    /**
     * Computes the partition id for a given key.
     * @param key
     * @returns the partition id.
     */
    getPartitionId(key: any) {
        var partitionHash = this.client.getSerializationService().toData(key).getPartitionHash();
        return Math.abs(partitionHash) % this.partitionCount;
    }
}

export = PartitionService;<commit_msg>fix PartitionService so it does not re-calculate partition hash when a Data object is passed to it<commit_after>import * as Q from 'q';
import GetPartitionsCodec = require('./codec/GetPartitionsCodec');
import ClientMessage = require('./ClientMessage');
import Address = require('./Address');
import HazelcastClient from './HazelcastClient';

class PartitionService {

    private client: HazelcastClient;
    private partitionMap: {[partitionId: number]: Address};
    private partitionCount: number;

    constructor(client: HazelcastClient) {
        this.client = client;
    }

    initialize(): Q.Promise<PartitionService> {
        var deferred = Q.defer<PartitionService>();
        this.refresh().then(() => {
            deferred.resolve(this);
        });

        return deferred.promise;
    }

    /**
     * Refreshes the internal partition table.
     * @returns {Q.Promise<U>}
     */
    refresh(): Q.Promise<void> {
        var ownerConnection = this.client.getClusterService().getOwnerConnection();
        var clientMessage: ClientMessage = GetPartitionsCodec.encodeRequest();

        return this.client.getInvocationService()
            .invokeOnConnection(ownerConnection, clientMessage)
            .then((clientMessage: ClientMessage) => {
                this.partitionMap = GetPartitionsCodec.decodeResponse(clientMessage);
                this.partitionCount = Object.keys(this.partitionMap).length;
            });
    };

    /**
     * Returns the {@link Address} of the node which owns given partition id.
     * @param partitionId
     * @returns the address of the node.
     */
    getAddressForPartition(partitionId: number): Address {
        return this.partitionMap[partitionId];
    }

    /**
     * Computes the partition id for a given key.
     * @param key
     * @returns the partition id.
     */
    getPartitionId(key: any) {
        var partitionHash: number;
        if ('getPartitionHash' in key) {
            partitionHash = key.getPartitionHash();
        } else {
            partitionHash = this.client.getSerializationService().toData(key).getPartitionHash();
        }
        return Math.abs(partitionHash) % this.partitionCount;
    }
}

export = PartitionService;
{'fraction_non_alphanumeric': 0.11105837683910774, 'fraction_numerical': 0.0, 'mean_word_length': 2.8283378746594003, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, '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': '2724171', 'n_tokens_mistral': 1098, 'n_tokens_neox': 1082, 'n_words': 262}