<commit_before>'use strict';

/**
 * super-orm
 *
 * @author <NAME> <<EMAIL>>
 */

const assert = require('assert');
const {
  createPromiseCallback, isConnectionInstance, isCacheInstance, isUpdateSQL,
} = require('./utils');
const Schema = require('./schema');
const QueryBuilder = require('./query');


class Model {

  /**
   * 创建 Model
   *
   * @param {Object} options
   *   - {Object} connection
   *   - {Object} cache
   *   - {String} table
   *   - {Object} fields 格式为 { name: info }
   *   - {String|Array} primary
   *   - {Boolean} autoIncrement
   */
  constructor(options) {
    options = Object.assign({}, options || {});

    assert.ok(isConnectionInstance(options.connection), `connection must be an Connection instance`);
    this.connection = options.connection;

    assert.ok(isCacheInstance(options.cache), `cache must be an Cache instance`);
    this.cache = options.cache;

    assert.ok(options.table, `must provide table name`);
    assert.ok(typeof options.table === 'string', `table name must be a string`);
    this.tableName = options.table;

    if (options.primary) {
      assert.ok(typeof options.primary === 'string' || Array.isArray(options.primary), `primary must be a string or array`);
      if (Array.isArray(options.primary)) {
        options.primary.forEach(name => {
          assert.ok(typeof name === 'string', `every item of primary must be a string`);
        });
        // 包装 key 是按顺序排列的
        this.primaryKey = options.primary.slice().sort();
      } else {
        this.primaryKey = [ options.primary ];
      }
    } else {
      this.primaryKey = null;
    }

    if (options.autoIncrement) {
      assert.equal(this.primaryKey.length, 1, `invalid primary key number when autoIncrement=true`);
      this.primaryKeyAutoIncrement = true;
    } else {
      this.primaryKeyAutoIncrement = false;
    }

    this.schema = new Schema(options);
  }

  /**
   * 从一行数据中获取主键标志符
   *
   * @param {Object} data
   * @param {Boolean} strict
   * @return {String}
   */
  getPrimaryCacheKey(data, strict) {
    assert.ok(this.primaryKey, `table "${ this.tableName }" does not have primary key`);
    const key = this.primaryKey.map(name => {
      if (strict) {
        assert.ok(name in data, `missing primary key "${ name }" in this data row`);
      }
      return `${ name }_${ data[name] }`;
    }).join('&');
    return `${ this.tableName }:r:${ key }`;
  }

  /**
   * 从一行数据中保留主键的数据
   *
   * @param {Object} data
   * @return {Object}
   */
  keepPrimaryFields(data) {
    assert.ok(this.primaryKey, `table "${ this.tableName }" does not have primary key`);
    const ret = {};<commit_msg>更改 cache key 生成方法<commit_after>'use strict';

/**
 * super-orm
 *
 * @author <NAME> <<EMAIL>>
 */

const assert = require('assert');
const {
  createPromiseCallback, isConnectionInstance, isCacheInstance, isUpdateSQL,
} = require('./utils');
const Schema = require('./schema');
const QueryBuilder = require('./query');


class Model {

  /**
   * 创建 Model
   *
   * @param {Object} options
   *   - {Object} connection
   *   - {Object} cache
   *   - {String} table
   *   - {Object} fields 格式为 { name: info }
   *   - {String|Array} primary
   *   - {Boolean} autoIncrement
   */
  constructor(options) {
    options = Object.assign({}, options || {});

    assert.ok(isConnectionInstance(options.connection), `connection must be an Connection instance`);
    this.connection = options.connection;

    assert.ok(isCacheInstance(options.cache), `cache must be an Cache instance`);
    this.cache = options.cache;

    assert.ok(options.table, `must provide table name`);
    assert.ok(typeof options.table === 'string', `table name must be a string`);
    this.tableName = options.table;

    if (options.primary) {
      assert.ok(typeof options.primary === 'string' || Array.isArray(options.primary), `primary must be a string or array`);
      if (Array.isArray(options.primary)) {
        options.primary.forEach(name => {
          assert.ok(typeof name === 'string', `every item of primary must be a string`);
        });
        // 包装 key 是按顺序排列的
        this.primaryKey = options.primary.slice().sort();
      } else {
        this.primaryKey = [ options.primary ];
      }
    } else {
      this.primaryKey = null;
    }

    if (options.autoIncrement) {
      assert.equal(this.primaryKey.length, 1, `invalid primary key number when autoIncrement=true`);
      this.primaryKeyAutoIncrement = true;
    } else {
      this.primaryKeyAutoIncrement = false;
    }

    this.schema = new Schema(options);
  }

  /**
   * 从一行数据中获取主键标志符
   *
   * @param {Object} data
   * @param {Boolean} strict
   * @return {String}
   */
  getPrimaryCacheKey(data, strict) {
    assert.ok(this.primaryKey, `table "${ this.tableName }" does not have primary key`);
    const key = this.primaryKey.map(name => {
      if (strict) {
        assert.ok(name in data, `missing primary key "${ name }" in this data row`);
      }
      return `${ name }_${ data[name] }`;
    }).join(':');
    return `${ this.tableName }:r:${ key }`;
  }

  /**
   * 从一行数据中保留主键的数据
   *
   * @param {Object} data
   * @return {Object}
   */
  keepPrimaryFields(data) {
    assert.ok(this.primaryKey, `table "${ this.tableName }" does not have primary key`);
    const ret = {};
{'fraction_non_alphanumeric': 0.15236294896030245, 'fraction_numerical': 0.0003780718336483932, 'mean_word_length': 2.834057971014493, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, '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': '3653115', 'n_tokens_mistral': 1669, 'n_tokens_neox': 1701, 'n_words': 397}