text:
'use strict';
/**
 * Created by Adrian on 07-Apr-16.
 * This will register the CREATE, READ, UPDATE, DELETE, FIND
 * actions for the given modelObj.
 * All models that will pass through the crudify process, will have a .fromCrudify=true field added to their instances.
 *
 * FILTERS THAT CAN BE INTERCEPTED:
 *  create.before(intentObj, newInstanceObj)
 *  create.after(intentObj, savedInstanceObj)
 *  create.send(intentObj)
 *  create.error(intentObj)
 *  read.before(intentObj, findQuery)
 *  read.after(intentObj, entityObj)
 *  read.send(intentObj)
 *  read.error(intentObj)
 *  find.before(intentObj, findQuery)
 *  find.count(intentObj, countQry)
 *  find.after(intentObj, results[])
 *  find.send(intentObj)
 *  find.error(intentObj)
 *  update.before(intentObj, findQuery)
 *  update.save(intentObj, entityObj)
 *  update.after(intentObj, entityObj)
 *  update.send(intentObj)
 *  update.error(intentObj)
 *  delete.before(intentObj, findQuery)
 *  delete.destroy(intentObj, entityObj)
 *  delete.after(intentObj, entityObj)
 *  delete.send(intentObj)
 *  delete.error(intentObj)
 */
module.exports = function (thorin) {

  const actions = {},
    dispatcher = thorin.dispatcher,
    ACTION_NAMESPACE = "";

  /* Returns an array of aliases for the given action */
  function getAliases(actionObj, defaultVerb, defaultPath, hasAlias) {
    if (hasAlias === false) return [];
    if (actionObj.aliases.length !== 0) {
      return actionObj.aliases; // some custom aliases.
    }
    var rootPath = actionObj.rootPath;
    if (rootPath.charAt(0) !== '/') rootPath = '/' + rootPath;
    if (rootPath.charAt(rootPath.length - 1) === '/') rootPath = rootPath.substr(0, rootPath.length - 1);
    rootPath += defaultPath;
    return [[defaultVerb, rootPath]];
  }

  /* Attach the use() and middleware() for the action */
  function attachUses(crudifyAction, dbAction) {
    crudifyAction.uses.forEach((item) => {
      if (typeof dbAction[item.fn] !== 'function') return;
      dbAction[item.fn].apply(dbAction, item.args);
    });
  }

  /* Attach the stuff to the new action */
  function attach(crudifyAction, dbAction, aliases) {
    if (!crudifyAction.templates) return;
    crudifyAction.templates.forEach((t) => {
      dbAction.template(t);
    });
    delete crudifyAction.templates;
    // attach all middlewares.
    crudifyAction.handlers.forEach((item) => {
      if (typeof dbAction[item.fn] !== 'function') return;
      dbAction[item.fn].apply(dbAction, item.args);
    });
    delete crudifyAction.handlers;
    // attach aliases.
    aliases.forEach((args) => {
      dbAction.alias.apply(dbAction, args);
    });
  }

  /* Attaches a validator to the input */
  function addValidator(fieldValidator, fieldName, field) {
    fieldValidator[fieldName] = dispatcher.validate(field.type.key);
    if (typeof field.defaultValue !== 'undefined') {
      fieldValidator[fieldName].default(field.defaultValue);
    }
    if (field.type.values) {
      fieldValidator[fieldName].options(field.type.values);
    }
    fieldValidator[fieldName].fieldName = fieldName;
    return fieldValidator[fieldName];
  }

  /* Attaches a foreign validator to the input */
  function addForeignValidator(fieldValidator, relation, _ignoreCallback) {
    let relObj = this.model(relation.name, true),
      relPrimary = relObj.getPrimary(),
      relName = relation.options.foreignKey.name;
    if (relPrimary) {
      addValidator(fieldValidator, relName, relPrimary);
    }
    fieldValidator[relName].fieldName = relName;
    if (relation.options.foreignKey.allowNull) {
      fieldValidator[relName].default(null);
    }
    if (_ignoreCallback === true) return fieldValidator[relName];
    // query the model when an incoming request containing the id comes.
    fieldValidator[relName].callback((val, done) => {
      if (val == null) return done();
      if (typeof val === 'object' && typeof val.get === 'function') return done(null, val); // it was previously queried and fetched.
      let RelModel = this.model(relation.name);
      let where = {};
      if (typeof val === 'function') val = val();
      where[relPrimary.fieldName] = val;
      RelModel.find({
        where: where,
        attributes: [relPrimary.fieldName]  // we just check for existance.
      }).then((rObj) => {
        if (!rObj) {
          return done(thorin.error('INPUT.NOT_VALID', 'Invalid reference for ' + relName, 404));
        }
        done(null, rObj.get(relPrimary.fieldName));  //return with the id
      }).catch((e) => {
        done(thorin.error(e));
      });
    });
    return fieldValidator[relName];
  }

  /*
   * Error parser for create and update.
   * */
  function parseChangeError(e, modelObj) {
    if (e && e.name.indexOf('Thorin') === 0) return e;
    let wrapError = thorin.error('INPUT.NOT_VALID', 'Invalid value for entity.', 400),
      fields = [];
    if (e instanceof Error) {
      switch (e.name) {
        case 'SequelizeValidationError':
          wrapError.message = e.errors[0].message;
          e.errors.forEach((err) => {
            if (err.path.indexOf('thorinValidate') === -1) { // field validator.
              fields.push(err.path);
            }
          });
          break;
        case 'SequelizeUniqueConstraintError':
          wrapError.code = 'INPUT.EXISTS';
          wrapError.message = "An entry with the same " + e.errors[0].path.substr(modelObj.tableName.length + 1) + ' already exists.';
          e.errors.forEach((err) => {
            fields.push(err.path.substr(modelObj.tableName.length + 1));
          });
          break;
        default:
          e = thorin.error(e);  // default error.
      }
    }
    if (fields.length === 1) {
      wrapError.data = {
        field: fields[0]
      }
    } else if (fields.length > 1) {
      wrapError.data = {
        fields: fields
      };
    }
    return wrapError;
  }

  /*
   * Handles the CREATEion of a sql model.
   * Action id will be:
   *   db.create.{modelName}
   * HTTP alias will be: POST {root}/{modelName}
   * OPTIONS
   *    - delay -> if set to true, we will delay input() only after the action's uses
   * FILTERS:
   *  create.before(intentObj) -> right before we call the save()
   *  create.after(intentObj, instanceObj)  -> right after we call save() and before we send the intent.
   * */
  actions.create = function (actionObj, opt) {
    let modelObj = actionObj.model,
      aliases = getAliases(actionObj, 'POST', '/' + opt.name, opt.alias),
      namespace = (typeof opt.namespace === 'undefined' ? ACTION_NAMESPACE : opt.namespace);
    if (namespace !== '') namespace = namespace + '.';
    let actionId = (typeof opt.action === 'undefined' ? namespace + opt.name + '.create' : opt.action);
    var dbAction = new thorin.Action(actionId);
    attach(actionObj, dbAction, aliases);
    let fieldValidator = {},
      primaryKey = modelObj.getPrimary();

    // place static ones
    Object.keys(modelObj.fields).forEach((fieldName) => {
      if (fieldName === 'created_at') return;
      let field = modelObj.fields[fieldName];
      if (field.primaryKey || field.private || field.create === false) return;
      addValidator(fieldValidator, fieldName, field);
    });
    // check if we have any relationships.
    modelObj.relations.forEach((relation) => {
      if (relation.options) {
        if (relation.options.private) return;
        if (relation.options.create === false) return;
      }
      // belongsTo relations have a foreign key in the current model that we have to check.
      if (relation.type === 'belongsTo') {
        addForeignValidator.call(this, fieldValidator, relation);
      }
    });
    if (opt.delay === true) {
      attachUses(actionObj, dbAction);
      dbAction.input(fieldValidator);
    } else {
      dbAction.input(fieldValidator);
      attachUses(actionObj, dbAction);
    }
    dbAction.use((intentObj) => {
      let DbModel = this.model(modelObj.code),
        inputData = intentObj.input(),
        newInstance = DbModel.build({});
      newInstance.fromCrudify = true;
      newInstance.fromRestify = true; // backwards compatibility
      // at this point, we map the information, so that it has access to the .fromCrudify
      Object.keys(inputData).forEach((keyName) => {
        if (primaryKey && primaryKey.fieldName === keyName) return;
        newInstance.set(keyName, inputData[keyName]);
      });
      try {
        actionObj._runFilters('create.before', intentObj, newInstance);
      } catch (e) {
        intentObj.error(thorin.error(e));
        actionObj._runSend('create.error', intentObj);
        return null;
      }
      newInstance.save().then(() => {
        try {
          actionObj._runFilters('create.after', intentObj, newInstance);
        } catch (e) {
          intentObj.error(thorin.error(e));
          actionObj._runSend('create.error', intentObj);
          return null;
        }
        intentObj.result(newInstance);
        actionObj._runSend('create.send', intentObj);
        return null;
      }, (e) => {
        let wrapError = parseChangeError(e, modelObj);
        intentObj.error(wrapError);
        actionObj._runSend('create.error', intentObj);
        return null;
      }).catch((e) => {
        intentObj.error(thorin.error(e));
        actionObj._runSend('create.error', intentObj);
        return null;
      });
    });
    return dbAction;
  };

  /*
   * Handles the "FindByID" of a SQL model.
   *  OPTIONS
   *    - input = input/filter (where to place the validations)
   *    - delay -> if set to true, we will delay filter applying only after the action's ones.
   *  - forceQuery -> if set to true, we will not use data(modelObj.code)  *
   * FILTERS:
   *  read.before(intentObj, findQuery) -> right before we call the .find()
   *  read.after(intentObj, entityObj)  -> right after we've found the iten and before we send it to the client.
   * */
  actions.read = function (actionObj, opt) {
    let modelObj = actionObj.model,
      primaryKey = modelObj.getPrimary();
    if (!primaryKey) {
      console.error('Thorin.sql.crudify: Model ' + modelObj.code + " does not contain a primary key for the READ crudify action.");
      return false;
    }
    let aliases = getAliases(actionObj, 'GET', '/' + opt.name + '/:' + primaryKey.fieldName, opt.alias),
      namespace = (typeof opt.namespace === 'undefined' ? ACTION_NAMESPACE : opt.namespace);
    if (namespace !== '') namespace = namespace + '.';
    let actionId = (typeof opt.action === 'undefined' ? namespace + opt.name + '.read' : opt.action);
    let dbAction = new thorin.Action(actionId);
    attach(actionObj, dbAction, aliases);
    let fieldValidator = {};
    fieldValidator[primaryKey.fieldName] = dispatcher.validate(primaryKey.type.key);
    fieldValidator[primaryKey.fieldName].default(null);
    let inputType = opt.input === 'filter' ? 'filter' : 'input';
    if (opt.delay === true) {
      attachUses(actionObj, dbAction);
      dbAction[inputType](fieldValidator);
    } else {
      dbAction[inputType](fieldValidator);
      attachUses(actionObj, dbAction);
    }
    dbAction.use((intentObj) => {
      let DbModel = this.model(modelObj.code),
        primaryId = intentObj[inputType](primaryKey.fieldName),
        findQuery = {
          where: {}
        };
      findQuery.where[primaryKey.fieldName] = intentObj.input(primaryKey.fieldName);
      // update the findQuery
      try {
        actionObj._runFilters('read.before', intentObj, findQuery);
      } catch (e) {
        intentObj.error(thorin.error(e));
        actionObj._runSend('read.error', intentObj);
        return null;
      }

      function doResObj(resObj) {
        if (!resObj) { // not found.
          intentObj.error(thorin.error('ENTRY.NOT_FOUND', 'The requested entity was not found', 404))
          actionObj._runSend('read.error', intentObj);
          return null;
        }
        resObj.fromRestify = true; // backwards compatibility
        resObj.fromCrudify = true;
        try {
          actionObj._runFilters('read.after', intentObj, resObj);
        } catch (e) {
          intentObj.error(thorin.error(e));
          actionObj._runSend('read.error', intentObj);
          return null;
        }
        intentObj.result(resObj);
        actionObj._runSend('read.send', intentObj);
        return null;
      }

      let tmpObj = intentObj.data(modelObj.code);
      // IF we already have the modelObj in the intent data, we use it.
      if (tmpObj && !opt.forceQuery) {
        return doResObj(tmpObj);
      }
      if (primaryId == null) {
        intentObj.error(thorin.error('INPUT.NOT_VALID', 'Invalid or missing ' + primaryKey.fieldName));
        actionObj._runSend('read.error', intentObj);
        return null;
      }
      DbModel.find(findQuery).then(doResObj).catch((e) => {
        intentObj.error(thorin.error(e));
        actionObj._runSend('read.error', intentObj);
        return null;
      });
    });
    return dbAction;
  };

  /*
   * Handles the "FindAll" of a SQL Model.
   * OPTIONS:
   *  - maxLimit -> the maximum limit. If this is set to false, we disable pagination
   *  - defaultLimit -> the default limit
   *  - defaultOrder -> the default order (asc/desc)
   *  - defaultOrderBy -> the default orderBy (fieldName)
   *  - allowDate -> should we allow start/end date filtering?
   *  - allowAll -> if we receive a limit_disable=true, do not apply limitation
   *  - filters -> if set to false, will disable any filters
   *  - input -> defaults to "input", can be specified as "filter"
   *  - delay -> if set, we will place the filter with process.nextTick
   * The default functionality will include:
   * - sorting with: sort=asc|desc&sortBy=fieldName
   * - paging with ?page=2&limit=10
   * - date filtering with ?start_date&end_date
   *
   * FILTERS:
   *  find.before(intentObj, findQuery) -> right before we call the findAll() with the query.
   *  find.count(intentObj, countQry) -> right before we call the count() with the query
   *  find.after(intentObj, results[])  -> right after we've queried and before we send them.
   * */
  actions.find = function (actionObj, opt) {
    let modelObj = actionObj.model;
    let aliases = getAliases(actionObj, 'GET', '/' + opt.name, opt.alias),
      namespace = (typeof opt.namespace === 'undefined' ? ACTION_NAMESPACE : opt.namespace);
    if (namespace !== '') namespace = namespace + '.';
    let actionId = (typeof opt.action === 'undefined' ? namespace + opt.name + '.find' : opt.action);
    let dbAction = new thorin.Action(actionId);
    attach(actionObj, dbAction, aliases);
    let fieldValidator = {},
      attributeFilters = [];
    // add the created_at or updated_at field.
    if (modelObj.options.createdAt) {
      attributeFilters.push(modelObj.options.createdAt);
    }
    if (modelObj.options.updatedAt) {
      attributeFilters.push(modelObj.options.updatedAt);
    }
    let primaryKey = modelObj.getPrimary();
    // for each field in the model, we check if it is searchable or not.
    if (opt.filters !== false) {
      Object.keys(modelObj.fields).forEach((fieldName) => {
        let field = modelObj.fields[fieldName];
        // IF the field is private, has filtering disabled or is primary, we skip.
        if (field.primaryKey && field.autoIncrement) return;
        if (field.private && field.filter !== true) return;
        if (field.filter === false || field.find === false) return;
        attributeFilters.push(fieldName);
        addValidator(fieldValidator, fieldName, field);
        fieldValidator[fieldName].default(null);
      });
      // then, do the createdAt or updatedAt.
      if (modelObj.options.createdAt && opt.allowDate !== false) {
        fieldValidator[modelObj.options.createdAt] = dispatcher.validate('DATE').default(null);
      }
      if (modelObj.options.updatedAt && opt.allowDate !== false) {
        fieldValidator[modelObj.options.updatedAt] = dispatcher.validate('DATE').default(null);
      }
      // then, we do the foreign validators.
      modelObj.relations.forEach((relation) => {
        if (relation.options.private && relation.options.filter !== true) return;
        if (relation.options.filter === false || relation.options.find === false) return;
        if (relation.type === 'belongsTo') {
          let valObj = addForeignValidator.call(this, fieldValidator, relation, true);
          valObj.default(null);
          let name = (typeof relation.options.foreignKey === 'string' ? relation.options.foreignKey : relation.options.foreignKey.name);
          if (name) {
            attributeFilters.push(name);
          }
        }
      });
      // Add the "START_DATE" and "END_DATE" validators, if the model has a createdAt.
      if (modelObj.options.createdAt && opt.allowDate !== false) {
        fieldValidator['start_date'] = dispatcher.validate('DATE').default(null);
        fieldValidator['end_date'] = dispatcher.validate('DATE').default(null);
      }
    }

    // Add the "LIMIT" validator
    if (opt.maxLimit !== false) {
      fieldValidator['limit'] = dispatcher.validate('NUMBER', {
        min: 1,
        max: opt.maxLimit || 100
      }).default(opt.defaultLimit || 10);
      // add the "PAGE" validator
      fieldValidator['page'] = dispatcher.validate('NUMBER', {
        min: 1
      }).default(1);
    }
    // Allow limit: false, to fetch all entries
    if (opt.allowAll) {
      fieldValidator['limit_disable'] = dispatcher.validate('BOOLEAN').default(false);
    }
    // add the "ORDER" validator
    fieldValidator['order'] = dispatcher.validate('ENUM', ['asc', 'desc']).default(opt.defaultOrder || 'desc');
    // add the "ORDER_BY" validator
    let orderByDefault = opt.defaultOrderBy || null;
    if(!orderByDefault) {
      if (modelObj.options.createdAt) {
        orderByDefault = modelObj.options.createdAt;
      } else {  // default the primary key.
        if (primaryKey) {
          orderByDefault = primaryKey.fieldName;
        }
      }
    }
    fieldValidator['order_by'] = dispatcher.validate('string').default(orderByDefault);
    // attach the validator.s
    let inputType = opt.input === 'filter' ? 'filter' : 'input';
    if (opt.delay === true) {
      attachUses(actionObj, dbAction);
      dbAction[inputType](fieldValidator);
    } else {
      dbAction[inputType](fieldValidator);
      attachUses(actionObj, dbAction);
    }
    dbAction.use((intentObj) => {
      // first, we parse the input, to see what isn't null.
      let DbModel = this.model(modelObj.code),
        findQuery = {
          where: {},
          order: []
        },
        input = intentObj[inputType]();
      if (input.limit_disable === true) {
        input.limit = null;
      }
      // validate the incoming keys
      attributeFilters.forEach((name) => {
        let value = input[name];
        if (value == null || typeof value === 'undefined') return;
        // type safe.
        if (value instanceof Array) {
          let safe = [];
          for (let i = 0; i < value.length; i++) {
            if (typeof value[i] === 'string' || typeof value[i] === 'number' || typeof value[i] === 'boolean') {
              safe.push(value[i]);
            }
          }
          if (safe.length === 0) return;
          findQuery.where[name] = safe;
        } else if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
          findQuery.where[name] = value;
        }
      });
      // set the limit
      if (opt.maxLimit !== false && input.limit !== null) {
        findQuery.limit = input.limit;
        if (input.page > 1) {  // we have pagination
          findQuery.offset = (input.page - 1 ) * input.limit;
        }
      }
      // set the order and orderBy
      let orders = input.order_by.split(','),
        hasOrder = false;
      for (let i = 0; i < orders.length; i++) {
        let orderBy = orders[i];
        if (attributeFilters.indexOf(orderBy) !== -1
          || orderBy === modelObj.options.createdAt
          || orderBy === modelObj.options.updatedAt
          || (primaryKey && primaryKey.fieldName === orderBy)) {
          findQuery.order.push([orderBy, input.order]);
          hasOrder = true;
        }
      }
      if (!hasOrder) {
        findQuery.order.push([orderByDefault, input.order]);
      }
      // set the start_date and end_date if we have em under start_date and end_date
      if ((input.start_date || input.end_date) && !findQuery.where[modelObj.options.createdAt]) {
        if (input.start_date && input.end_date) {
          if (input.start_date > input.end_date) {
            let err = thorin.error('INPUT.NOT_VALID', 'Start date must be before end date.', 400);
            err.data = {
              field: 'start_date'
            };
            intentObj.error(err);
            actionObj._runSend('find.error', intentObj);
            return null;
          }
          if (input.end_date < input.start_date) {
            let err = thorin.error('INPUT.NOT_VALID', 'End date must be after start date.', 400);
            err.data = {
              field: 'end_date'
            };
            intentObj.error(err);
            actionObj._runSend('find.error', intentObj);
            return null;
          }
          if (input.start_date.toString() === input.end_date.toString()) {
            findQuery.where[modelObj.options.createdAt] = input.start_date;
          }
        }
        if (!findQuery.where[modelObj.options.createdAt]) {
          findQuery.where[modelObj.options.createdAt] = {};
          if (input.start_date) {
            findQuery.where[modelObj.options.createdAt]['$gte'] = input.start_date;
          }
          if (input.end_date) {
            findQuery.where[modelObj.options.createdAt]['$lte'] = input.end_date;
          }
        }
      }
      // run the filters.
      try {
        actionObj._runFilters('find.before', intentObj, findQuery);
      } catch (e) {
        intentObj.error(thorin.error(e));
        actionObj._runSend('find.error', intentObj);
        return null;
      }
      let calls = [],
        totalCount,
        results = [];
      if (opt.maxLimit !== false) {
        // step one, count() all of them.
        calls.push(() => {
          // run the filters.
          let countQuery = thorin.util.extend(findQuery);
          try {
            actionObj._runFilters('find.count', intentObj, countQuery);
          } catch (e) {
            intentObj.error(thorin.error(e));
            actionObj._runSend('find.error', intentObj);
            return null;
          }
          delete countQuery.limit;
          delete countQuery.offset;
          delete countQuery.order;
          delete countQuery.attributes;
          return DbModel.count(countQuery).then((cnt) => {
            totalCount = cnt;
            return null;
          });
        });
      }
      // step two, if there are any counts, query em.
      calls.push(() => {
        if (totalCount === 0) return;
        return DbModel.findAll(findQuery).then((_res) => {
          results = _res;
          for (let i = 0; i < results.length; i++) {
            results[i].fromRestify = true;
            results[i].fromCrudify = true;
          }
          return null;
        });
      });
      thorin.series(calls, (err) => {
        if (err) {
          intentObj.error(thorin.error(err));
          actionObj._runSend('find.error', intentObj);
          return null;
        }
        try {
          actionObj._runFilters('find.after', intentObj, results);
        } catch (e) {
          intentObj.error(thorin.error(e));
          actionObj._runSend('find.error', intentObj);
          return null;
        }
        intentObj.result(results);
        // TODO: transform the pagination data into a thorin.Pagination
        if (opt.maxLimit !== false) {
          let totalPages = 1;
          if (typeof input.limit === 'number' && typeof totalCount === 'number') {
            totalPages = Math.ceil(totalCount / input.limit);
          }
          let paginationData = {
            total_count: totalCount,
            page_count: totalPages,
            current_count: results.length,
            current_page: input.page
          };
          intentObj.setMeta(paginationData);
        }
        actionObj._runSend('find.send', intentObj);
        return null;
      });
    });

    return dbAction;
  };

  /*
  * Handles the "Update" of a SQL Model. We can only handle UPDATE
  * for models that have one primary key defined. If a model has no such ting,
  * we cannot perform the update.
  * OPTIONS:
  *  - delay -> if set, we will delay filtering only after the input()
  *  - fields -> an array of allowed updatable fields.
  *  - forceQuery -> if set to true, we will not use data(modelObj.code)  *
  * FILTERS:
  *  update.before(intentObj, findQuery) -> right before we call the find() for the entity.
  *  update.save(intentObj, entityObj) -> right before we call the save() for the entity
  *  update.after(intentObj, entityObj) -> right after we called the save() for the entity.
  * */
  actions.update = function (actionObj, opt) {
    let modelObj = actionObj.model;
    let primaryKey = modelObj.getPrimary();
    if (!primaryKey) {
      console.warn('Thorin.sql.crudify: model ' + modelObj.code + ' does not have a primary key for "update" crudify');
      return;
    }
    let aliases = getAliases(actionObj, 'POST', '/' + opt.name + '/:' + primaryKey.fieldName, opt.alias),
      namespace = (typeof opt.namespace === 'undefined' ? ACTION_NAMESPACE : opt.namespace);
    if (namespace !== '') namespace = namespace + '.';
    let actionId = (typeof opt.action === 'undefined' ? namespace + opt.name + '.update' : opt.action);
    let dbAction = new thorin.Action(actionId);
    attach(actionObj, dbAction, aliases);
    let fieldValidator = {},
      foreignKeys = [];
    let allowedFields = opt.fields || false;
    // place the model's fields as input fields
    Object.keys(modelObj.fields).forEach((fieldName) => {
      if (fieldName === 'created_at') return;
      if (allowedFields && allowedFields.indexOf(fieldName) === -1) return;
      let field = modelObj.fields[fieldName];
      if (field.private || field.update === false) return;
      let proxyField = {
        type: {
          key: field.type.key,
          values: field.type.values
        },
        defaultValue: null
      };
      addValidator(fieldValidator, fieldName, proxyField);
    });
    // place any relation foreign keys
    modelObj.relations.forEach((relation) => {
      let fieldName = relation.options.foreignKey.name;
      if (allowedFields && allowedFields.indexOf(fieldName) === -1) return;
      if (relation.options) {
        if (relation.options.private === true) return; //skip private fields.
        if (relation.options.update === false) return; // skip non updatable fields.
      }

      if (relation.type === 'belongsTo') {
        let valObj = addForeignValidator.call(this, fieldValidator, relation);
        valObj.default(undefined);
        foreignKeys.push(valObj.fieldName);
      }
    });
    if (opt.delay === true) {
      attachUses(actionObj, dbAction);
      dbAction.input(fieldValidator);
    } else {
      dbAction.input(fieldValidator);
      attachUses(actionObj, dbAction);
    }
    dbAction.use((intentObj) => {
      let DbModel = this.model(modelObj.code),
        rawInput = intentObj.rawInput,
        input = intentObj.input();
      let calls = [],
        resObj,
        primaryId = intentObj.input(primaryKey.fieldName),
        hasUpdates = false,
        findQry = {
          where: {}
        };
      findQry.where[primaryKey.fieldName] = primaryId;
      // run the action filters
      // IF we already have the entity in the intent, we skip the query
      let tmpObj = intentObj.data(modelObj.code);
      if (tmpObj && !opt.forceQuery) {
        resObj = tmpObj;
      } else {
        if (!primaryId) {
          intentObj.error(thorin.error('INPUT.NOT_VALID', 'Invalid or missing ' + primaryKey.fieldName));
          actionObj._runSend('update.error', intentObj);
          return null;
        }
        try {
          actionObj._runFilters('update.before', intentObj, findQry);
        } catch (e) {
          intentObj.error(thorin.error(e));
          actionObj._runSend('update.error', intentObj);
          return null;
        }
        // step one, read the model.
        calls.push((stop) => {
          return DbModel.find(findQry).then((res) => {
            if (!res) {
              stop(thorin.error('ENTRY.NOT_FOUND', 'The requested entity was not found', 404));
              return null;
            }
            resObj = res;
            resObj.fromRestify = true;
            resObj.fromCrudify = true;
            return null;
          });
        });
      }
      // step two, update the result's fields. and save it.
      calls.push((stop) => {
        // step one, for each rawInput, we fetch its input, so that we only update what we receive.
        let updateKeys = Object.keys(input),
          toUpdate = {};
        for (let i = 0; i < updateKeys.length; i++) {
          let keyName = updateKeys[i],
            keyValue = input[keyName];
          if (keyName === primaryKey.fieldName) continue;
          if (typeof rawInput[keyName] === 'undefined') continue;
          let shouldUpdateKey = false;
          // check if we have a foreign key
          if (foreignKeys.indexOf(keyName) !== -1) {
            if (rawInput[keyName] == null) {
              keyValue = null;  //remove the reference.
              shouldUpdateKey = true;
            } else if (input[keyName] != null && typeof input[keyName] !== 'undefined') {
              shouldUpdateKey = true; // update teh reference
            }
          } else {
            if (input[keyName] != null && typeof input[keyName] !== 'undefined') {
              shouldUpdateKey = true;
            }
          }
          if (!shouldUpdateKey) continue;
          toUpdate[keyName] = keyValue;
          if (!hasUpdates) hasUpdates = true;
        }
        if (!hasUpdates) return;
        let hasAfterFilters = false;
        Object.keys(toUpdate).forEach((keyName) => {
          if (!hasAfterFilters) hasAfterFilters = true;
          let val = toUpdate[keyName];
          resObj.set(keyName, val);
        });
        if (!hasAfterFilters) return;  // filter removed updates.
        // call the filters to possibly alter the updates.
        try {
          actionObj._runFilters('update.save', intentObj, resObj);
        } catch (e) {
          stop(e);
          return null;
        }
        return resObj.save();
      });
      thorin.series(calls, (e) => {
        if (e) {
          let wrapError = parseChangeError(e, modelObj);
          intentObj.error(wrapError);
          actionObj._runSend('update.error', intentObj);
          return null;
        }
        try {
          actionObj._runFilters('update.after', intentObj, resObj);
        } catch (e) {
          intentObj.error(thorin.error(e));
          actionObj._runSend('update.error', intentObj);
          return null;
        }
        let hasChanged = (Object.keys(resObj._changed).length > 0);
        intentObj.setMeta({
          changed: hasChanged
        });
        intentObj.result(resObj);
        actionObj._runSend('update.send', intentObj);
        return null;
      });
    });
    return dbAction;
  };

  /*
   * Handles the "Delete" of a SQL Model.
   * FILTERS:
   *  delete.before(intentObj, findQuery) -> right before we call the find() for the entity
   *  delete.destroy(intentObj, entityObj) -> right before we call the destroy() for the entity
   *  delete.after(intentObj, entityObj)  -> right after we called the destroy() and the entity was deleted.
   *
   *  OPTIONS:
   *    - delay -> if set to true, we will delay input to after uses'
   *    - useField (string) if specified, we will not destroy() the entity ,but rather set the given field to false and save.
   *      - forceQuery -> if set to true, we will not use data(modelObj.code)  *
   * */
  actions.delete = function (actionObj, opt) {
    let modelObj = actionObj.model,
      primaryKey = modelObj.getPrimary();
    if (!primaryKey) {
      console.warn('Thorin.sql.resitfy: model ' + modelObj.code + ' does not have a primary key. Skipping');
      return;
    }
    let aliases = getAliases(actionObj, 'DELETE', '/' + opt.name + '/:' + primaryKey.fieldName, opt.alias),
      namespace = (typeof opt.namespace === 'undefined' ? ACTION_NAMESPACE : opt.namespace);
    if (namespace !== '') namespace = namespace + '.';
    let actionId = (typeof opt.action === 'undefined' ? namespace + opt.name + '.delete' : opt.action);
    let dbAction = new thorin.Action(actionId);
    attach(actionObj, dbAction, aliases);
    // the only input we have is the primary key.
    let fieldValidator = {};
    fieldValidator[primaryKey.fieldName] = dispatcher.validate(primaryKey.type.key);
    fieldValidator[primaryKey.fieldName].default(null);
    if (opt.delay === true) {
      attachUses(actionObj, dbAction);
      dbAction.input(fieldValidator);
    } else {
      dbAction.input(fieldValidator);
      attachUses(actionObj, dbAction);
    }
    dbAction.use((intentObj) => {
      let DbModel = this.model(modelObj.code),
        resObj,
        primaryId = intentObj.input(primaryKey.fieldName),
        findQuery = {
          where: {}
        };
      findQuery.where[primaryKey.fieldName] = primaryId;
      // IF we had a useField option specify, that MUST be true
      if (opt.useField) {
        findQuery.where[opt.useField] = true;
      }
      // update the find query
      // IF we already have the entity in the intent, we skip the query.
      let tmpObj = intentObj.data(modelObj.code);
      let calls = [];
      if (tmpObj && !opt.forceQuery) {
        resObj = tmpObj;
      } else {
        if (primaryId == null) {
          return intentObj.error(thorin.error('INPUT.NOT_VALID', 'Invalid or missing ' + primaryKey.fieldName)).send();
        }
        // IF we don't have the entity, we query for it.
        try {
          actionObj._runFilters('delete.before', intentObj, findQuery);
        } catch (e) {
          // if we have any filter error, we stop.
          intentObj.error(thorin.error(e));
          actionObj._runSend('delete.error', intentObj);
          return null;
        }
        // step one, read the model.
        calls.push((stop) => {
          return DbModel.find(findQuery).then((res) => {
            if (!res) {
              stop(thorin.error('ENTRY.NOT_FOUND', 'The requested entity was not found', 404));
              return null;
            }
            resObj = res;
            resObj.fromRestify = true;
            resObj.fromCrudify = true;
            return null;
          });
        });
      }
      // step two, delete it.
      calls.push((stop) => {
        if (typeof resObj.canDelete === 'function') {
          let canDelete = true;
          try {
            canDelete = resObj.canDelete();
            if (!canDelete) {
              stop();
              return null;
            }
          } catch (e) {
            stop(thorin.error(e));
            return null;
          }
          if (canDelete === false) {
            return stop(thorin.error('ENTRY.DELETE', 'The requested entity cannot be deleted.', 400));
          }
        }
        try {
          actionObj._runFilters('delete.destroy', intentObj, resObj);
        } catch (e) {
          // if we have any filter error, we stop.
          stop(e);
          return null;
        }
        /* We check if we want to simply set a given field to false or destroy.*/
        if (!opt.useField) {
          return resObj.destroy();
        }
        resObj.set(opt.useField, false);
        resObj.set('deleted_at', Date.now());
        return resObj.save();
      });

      thorin.series(calls, (e) => {
        if (e) {
          intentObj.error(e);
          actionObj._runSend('delete.error', intentObj);
          return null;
        }
        try {
          actionObj._runFilters('delete.after', intentObj, resObj);
        } catch (e) {
          // if we have any filter error, we stop.
          intentObj.error(thorin.error(e));
          actionObj._runSend('delete.error', intentObj);
          return null;
        }
        intentObj.setMeta({
          deleted: true
        });
        intentObj.data(modelObj.code, resObj);
        actionObj._runSend('delete.send', intentObj);
        return null;
      });
    });
    return dbAction;
  };

  return actions;
};


text:
Kozai is a Python package to evolve hierarchical triples in the secular
approximation.  Hierarchical triples may be evolved either using the
Delaunay formalism (Naoz et al. 2013b) or the vectorial formalism (Katz et
al. 2011).  The quadrupole, octupole, and hexadecapole terms of the
Hamiltonian may be toggled to be included (or not) in the equations of
motion.  Post-Newtonian terms may also be toggled to include both
relativistic precession (PN 1) and gravitational radiation (PN 2.5) using
terms from Blaes et al. (2002).

The package provides a TripleDelaunay object which may be integrated using
the Delaunay orbital elements and a TripleVectorial which may be integrated
using the eccentricity and angular momentum vectors.  This allows the
integration to occur within the context of an external Python program.

The underlying integrator is from the SciPy ODE package.  By default this
package uses VODE as its integration algorithm, but the algorithm may be
changed to any of the other integration algorithms supported by the SciPy
ODE package.

## Installation

The Kozai package is available on PyPI and can be installed with pip:

```
pip install kozai
```

Or, if you do not have the permissions to run the above:

```
pip install --user kozai
```

## Tutorial

An IPython notebook tutorial is in the docs folder.  The tutorial can be
also be accessed online [here.][1]

Note that you will need to separately install `matplotlib` to run the tutorial
if you don't have it installed already.  `matplotlib` will be installed if you
install the `requirements-dev.txt` file described below.

## Development

If you want to do any development on the `kozai` package it can help to install
the dependencies in the `requirements-dev.txt` file.  If you are in the root
directory of the `kozai` repository you can do this as follows:

```sh
pip install -r requirements-dev.txt
```

## References

- [Antognini, 2015, ArXiv 1504.05957][5]
- [Blaes, O., Lee, M.H., & Socrates, A., 2002, ApJ, 578, 775][2]
- [Katz, B., Dong, S., & Malhotra, R., 2011, PhRvL, 107, 181101][3]
- [Naoz, S., Farr, W.M., Lithwick, Y., Rasio, F.A., & Teyssandier, J., 2013b,
  MNRAS, 431, 2155][4]

[1]: http://nbviewer.ipython.org/url/www.astronomy.ohio-state.edu/~antognini/kozai_tutorial.ipynb
[2]: http://adsabs.harvard.edu/abs/2002ApJ...578..775B
[3]: http://adsabs.harvard.edu/abs/2011PhRvL.107r1101K
[4]: http://adsabs.harvard.edu/abs/2013MNRAS.431.2155N
[5]: http://arxiv.org/abs/1504.05957

redpajama_set_name: RedPajamaGithub


text:
module RPM
  class File
    # @return [String] file path
    attr_accessor :path
    # @return [String] md5sum as string
    attr_accessor :md5sum
    # @return [String] Path to the destination if the file is a symbolic link
    # @note
    #   This path is sometimes relative. To convert an absolute path from relative path:
    #   File.expand_path (file.link_to, File.dirname (file.path))
    attr_accessor :link_to
    # @return [Number] File size
    attr_accessor :size
    # @return [Time] File modification time.
    attr_accessor :mtime
    # @return [String] File owner. Nil may be returned.
    attr_accessor :owner
    # @return [String] Group that owns the file. Nil may be returned.
    attr_accessor :group
    # @return [Number] Device type of the file
    attr_accessor :mode

    attr_accessor :attr
    attr_accessor :state
    attr_accessor :rdev

    # @return [Boolean] True if the file is a symbolic link
    def symlink?
      !@link_to.nil?
    end

    # @return [Boolean] True if the file is marked as a configuration file
    def config?
      !(@attr & RPM::C::FileAttrs[:config]).zero?
    end

    # @return [Boolean] True if the file is marked as documentation
    def doc?
      !(@attr & RPM::C::FileAttrs[:doc]).zero?
    end

    # @return [Boolean] True if the file is marked as do not use
    # @deprecated RPMFILE_DONOTUSE was removed in recent versions of RPM.
    def donotuse?
      msg = 'RPMFILE_DONOTUSE was removed in recent versions of RPM.'
      warn "#{Kernel.caller.first} #{msg}"
      raise NotImplementedError
    end

    # @return [Boolean] True if the file is marked that can be missing on disk
    #
    # This modifier is used for files or links that are created during the %post scripts
    # but will need to be removed if the package is removed
    def is_missingok?
      !(@attr & RPM::C::FileAttrs[:missingok]).zero?
    end

    # @return [Boolean] True if the file is marked as configuration not to be replaced
    #
    # This flag is used to protect local modifications.
    # If used, the file will not overwrite an existing file that has been modified.
    # If the file has not been modified on disk, the rpm command will overwrite the file. But,
    #   if the file has been modified on disk, the rpm command will copy the new file with an extra
    #   file-name extension of .rpmnew.
    def is_noreplace?
      !(@attr & RPM::C::FileAttrs[:noreplace]).zero?
    end

    # @return [Boolean] True if the file is marked as a spec file
    def is_specfile?
      !(@attr & RPM::C::FileAttrs[:specfile]).zero?
    end

    # @return [Boolean] True if the file is marked as ghost
    #
    # This flag indicates the file should not be included in the package.
    # It can be used to name the needed attributes for a file that the program, when installed,
    #   will create.
    # For example, you may want to ensure that a program's log file has certain attributes.
    def ghost?
      !(@attr & RPM::C::FileAttrs[:ghost]).zero?
    end

    # @return [Boolean] True if the file is a license
    def license?
      !(@attr & RPM::C::FileAttrs[:license]).zero?
    end

    # @return [Boolean] True if the file is a README
    def readme?
      !(@attr & RPM::C::FileAttrs[:readme]).zero?
    end

    # @raise NotImplementedError
    # @deprecated RPMFILE_EXCLUDE was removed in recent versions of RPM.
    def exclude?
      msg = 'RPMFILE_EXCLUDE was removed in recent versions of RPM.'
      warn "#{Kernel.caller.first} #{msg}"
      raise NotImplementedError
    end

    # @return [Boolean] True if the file is replaced during installation
    def replaced?
      !(@attr & RPM::C::FileState[:replaced]).zero?
    end

    # @return [Boolean] True if the file is not installed
    def notinstalled?
      !(@attr & RPM::C::FileState[:notinstalled]).zero?
    end

    # @return [Boolean] True if the file is shared over the network
    def netshared?
      !(@attr & RPM::C::FileState[:netshared]).zero?
    end

    def initialize(path, md5sum, link_to, size, mtime, owner, group, rdev, mode, attr, state)
      @path = path
      @md5sum = md5sum
      # If link_to is "" save it as nil
      @link_to = (link_to && link_to.empty? ? nil : link_to)
      @size = size
      @mtime = mtime
      @owner = owner
      @group = group
      @rdev = rdev
      @mode = mode
      @attr = attr
      @state = state
    end
  end
end


text:
using System;
using System.Collections.Generic;
using System.Linq;
using StructureMap.Graph;
using StructureMap.TypeRules;

namespace StructureMap.Query
{
    public class Model : IModel
    {
        private readonly IPipelineGraph _graph;

        internal Model(IPipelineGraph graph, PluginGraph pluginGraph)
        {
            _graph = graph;
            PluginGraph = pluginGraph;
        }

        public IPipelineGraph Pipeline
        {
            get { return _graph; }
        }

        private IEnumerable<IPluginTypeConfiguration> pluginTypes
        {
            get
            {
                foreach (var family in _graph.Instances.UniqueFamilies())
                {
                    if (family.IsGenericTemplate)
                    {
                        yield return new GenericFamilyConfiguration(family, _graph);
                    }
                    else
                    {
                        yield return new ClosedPluginTypeConfiguration(family, _graph);
                    }
                }
            }
        }

        public bool HasDefaultImplementationFor(Type pluginType)
        {
            return _graph.Instances.HasDefaultForPluginType(pluginType);
        }

        public bool HasDefaultImplementationFor<T>()
        {
            return HasDefaultImplementationFor(typeof (T));
        }

        public Type DefaultTypeFor<T>()
        {
            return DefaultTypeFor(typeof (T));
        }

        public Type DefaultTypeFor(Type pluginType)
        {
            return findForFamily(pluginType, f => f.Default == null ? null : f.Default.ReturnedType);
        }

        public IEnumerable<IPluginTypeConfiguration> PluginTypes
        {
            get { return pluginTypes; }
        }

        public PluginGraph PluginGraph { get; private set; }

        /// <summary>
        /// Retrieves the configuration for the given type
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public IPluginTypeConfiguration For<T>()
        {
            return For(typeof (T));
        }

        /// <summary>
        /// Retrieves the configuration for the given type
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public IPluginTypeConfiguration For(Type type)
        {
            return pluginTypes.FirstOrDefault(x => x.PluginType == type) ?? new EmptyConfiguration(type);
        }

        /// <summary>
        /// Eject all objects, configuration, and Plugin Types matching this filter
        /// </summary>
        /// <param name="filter"></param>
        public void EjectAndRemoveTypes(Func<Type, bool> filter)
        {
            // first pass hits Plugin types
            EjectAndRemovePluginTypes(filter);

            // second pass to hit instances
            pluginTypes.Each(x => { x.EjectAndRemove(i => filter(i.ReturnedType)); });
        }

        /// <summary>
        /// Eject all objects and configuration for any Plugin Type that matches this filter
        /// </summary>
        /// <param name="filter"></param>
        public void EjectAndRemovePluginTypes(Func<Type, bool> filter)
        {
            _graph.Ejector.RemoveCompletely(filter);
        }

        /// <summary>
        /// Eject all objects and Instance configuration for this PluginType
        /// </summary>
        /// <param name="pluginType"></param>
        public void EjectAndRemove(Type pluginType)
        {
            _graph.Ejector.RemoveCompletely(pluginType);
        }

        public void EjectAndRemove<TPluginType>()
        {
            EjectAndRemove(typeof (TPluginType));
        }

        /// <summary>
        /// Get each and every configured instance that could possibly
        /// be cast to T
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public IEnumerable<T> GetAllPossible<T>() where T : class
        {
            var targetType = typeof (T);
            return AllInstances
                .Where(x => x.ReturnedType.CanBeCastTo(targetType))
                .Select(x => x.Get<T>())
                .Where(x => x != null);
        }

        public InstanceRef Find<TPluginType>(string name)
        {
            return For<TPluginType>().Find(name);
        }

        public IEnumerable<InstanceRef> InstancesOf(Type pluginType)
        {
            return findForFamily(pluginType, x => x.Instances, new InstanceRef[0]);
        }

        public IEnumerable<InstanceRef> InstancesOf<T>()
        {
            return InstancesOf(typeof (T));
        }

        public bool HasImplementationsFor(Type pluginType)
        {
            return findForFamily(pluginType, x => x.HasImplementations());
        }

        public bool HasImplementationsFor<T>()
        {
            return HasImplementationsFor(typeof (T));
        }

        public IEnumerable<InstanceRef> AllInstances
        {
            get { return PluginTypes.SelectMany(x => x.Instances); }
        }

        private T findForFamily<T>(Type pluginType, Func<IPluginTypeConfiguration, T> func, T defaultValue)
        {
            var family = PluginTypes.FirstOrDefault(x => x.PluginType == pluginType);
            return family == null ? defaultValue : func(family);
        }

        private T findForFamily<T>(Type pluginType, Func<IPluginTypeConfiguration, T> func)
        {
            return findForFamily(pluginType, func, default(T));
        }
    }
}


text:
package ashwani.in.volleyeventbus.events.success;

public class GithubUserSuccessEvent extends BaseSuccessEvent {

}


text:
import os
import tempfile
from unittest import mock
from cobwebs.config import get_config, import_plugin
import pytest

@pytest.fixture
def tmp_config_file(tmpdir):
    fn = tmpdir.mkdir('data').join('conf.yaml')
    fn.write(str(config_test))
    print(fn)
    return str(fn)

def test_get_config(tmp_config_file):
    global_config = get_config(confname=tmp_config_file)
    assert isinstance(global_config, dict)
    assert "main" in global_config
    assert "templates" in global_config['main']
    assert "plugins" in global_config['main']
    assert "mq_driver" in global_config['main']
    assert "logging" in global_config

def test_import_plugin(tmp_config_file):
    global_config = get_config(tmp_config_file)
    plugins = import_plugin(global_config)
    assert isinstance(plugins, dict)

config_test = """main:
  templates: "src/apiviewer/static/templates/"
  plugins: "src/spider/spider/plugins/"
  mq_driver: cobwebs.mq.backends.rabbitmq
  mq_host: localhost

logging:
  version: 1

  formatters:
    brief:
      format: "%(levelname)s %(name)s %(message)-30s"
    custom:
      format: "%(asctime)-15s %(levelname)s %(name)s %(message)s"

  handlers:
    console:
      class : logging.StreamHandler
      formatter: brief
      level   : INFO
      stream  : ext://sys.stdout
    file:
      class : logging.handlers.RotatingFileHandler
      formatter: custom
      level   : DEBUG
      filename: /tmp/logconfig.log
      maxBytes: 1048576
      backupCount: 3

  loggers:
    spider:
      level: DEBUG
      handlers: [file]
      propagate: no

  root:
    level: INFO
    handlers: [file]
"""


text:
/* ###
 * IP: GHIDRA
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ghidra.pcode.exec.trace.data;

import java.nio.ByteBuffer;

import ghidra.program.model.address.*;
import ghidra.program.model.lang.Language;
import ghidra.trace.model.Lifespan;
import ghidra.trace.model.TraceTimeViewport;
import ghidra.trace.model.guest.TracePlatform;
import ghidra.trace.model.memory.*;

/**
 * An abstract data-access shim, for either memory or registers
 */
public abstract class AbstractPcodeTraceDataAccess implements InternalPcodeTraceDataAccess {
	protected final TracePlatform platform;
	protected final long snap;
	protected final TraceTimeViewport viewport;

	protected final TraceMemoryManager mm;

	/**
	 * Construct a shim
	 *
	 * @param platform the associated platform
	 * @param snap the associated snap
	 * @param viewport the viewport, set to the same snapshot
	 */
	public AbstractPcodeTraceDataAccess(TracePlatform platform, long snap,
			TraceTimeViewport viewport) {
		this.platform = platform;
		this.snap = snap;
		this.viewport = viewport;

		this.mm = platform.getTrace().getMemoryManager();
	}

	@Override
	public TraceTimeViewport getViewport() {
		return viewport;
	}

	@Override
	public Language getLanguage() {
		return platform.getLanguage();
	}

	@Override
	public TracePlatform getPlatform() {
		return platform;
	}

	@Override
	public long getSnap() {
		return snap;
	}

	/**
	 * Get the interface for accessing trace memory or registers
	 *
	 * @param createIfAbsent in the case of registers, whether to create the missing space
	 * @return the operations, or null
	 */
	protected abstract TraceMemoryOperations getMemoryOps(boolean createIfAbsent);

	/**
	 * If this shim is associated with a (register) overlay space, translate the given address into
	 * it
	 *
	 * @param address the physical (register) address
	 * @return the overlay address
	 */
	protected abstract Address toOverlay(Address address);

	/**
	 * @see #toOverlay(Address)
	 * @param range the physical range
	 * @return the overlay range
	 */
	protected abstract AddressRange toOverlay(AddressRange range);

	/**
	 * @see #toOverlay(Address)
	 * @param set
	 * @return
	 */
	protected abstract AddressSetView toOverlay(AddressSetView set);

	@Override
	public void setState(AddressRange guestRange, TraceMemoryState state) {
		AddressRange hostRange = platform.mapGuestToHost(guestRange);
		if (hostRange == null) {
			return;
		}
		getMemoryOps(true).setState(snap, toOverlay(hostRange), state);
	}

	@Override
	public TraceMemoryState getViewportState(AddressRange guestRange) {
		TraceMemoryOperations ops = getMemoryOps(false);
		if (ops == null) {
			return TraceMemoryState.UNKNOWN;
		}

		AddressRange hostRange = platform.mapGuestToHost(guestRange);
		if (hostRange == null) {
			return TraceMemoryState.UNKNOWN;
		}

		AddressSet hostSet = new AddressSet(toOverlay(hostRange));
		for (long snap : viewport.getOrderedSnaps()) {
			hostSet.delete(
				ops.getAddressesWithState(snap, hostSet, s -> s == TraceMemoryState.KNOWN));
		}
		return hostSet.isEmpty() ? TraceMemoryState.KNOWN : TraceMemoryState.UNKNOWN;
	}

	protected AddressSetView doGetKnown(Lifespan span) {
		TraceMemoryOperations ops = getMemoryOps(false);
		if (ops == null) {
			return new AddressSet();
		}
		return platform.mapHostToGuest(ops.getAddressesWithState(span,
			s -> s == TraceMemoryState.KNOWN));
	}

	@Override
	public AddressSetView getKnownNow() {
		return doGetKnown(Lifespan.at(snap));
	}

	@Override
	public AddressSetView getKnownBefore() {
		return doGetKnown(Lifespan.since(snap));
	}

	@Override
	public AddressSetView intersectUnknown(AddressSetView guestView) {
		TraceMemoryOperations ops = getMemoryOps(false);
		if (ops == null) {
			return guestView;
		}

		AddressSetView hostView = toOverlay(platform.mapGuestToHost(guestView));
		AddressSetView hostKnown = ops.getAddressesWithState(snap, hostView,
			s -> s != null && s != TraceMemoryState.UNKNOWN);
		AddressSetView hostResult = hostView.subtract(hostKnown);
		return platform.mapHostToGuest(hostResult);
	}

	@Override
	public int putBytes(Address start, ByteBuffer buf) {
		// TODO: Truncate or verify range?
		Address hostStart = platform.mapGuestToHost(start);
		if (hostStart == null) {
			return 0;
		}
		return getMemoryOps(true).putBytes(snap, toOverlay(hostStart), buf);
	}

	@Override
	public int getBytes(Address start, ByteBuffer buf) {
		// TODO: Truncate or verify range?
		Address hostStart = platform.mapGuestToHost(start);
		if (hostStart == null) {
			return 0;
		}
		TraceMemoryOperations ops = getMemoryOps(false);
		if (ops == null) {
			// TODO: Write 0s?
			int length = buf.remaining();
			buf.position(buf.position() + length);
			return length;
		}
		return ops.getViewBytes(snap, toOverlay(hostStart), buf);
	}

	@Override
	public Address translate(Address address) {
		Address host = platform.mapGuestToHost(address);
		if (host == null) {
			return null;
		}
		return toOverlay(host);
	}

	@Override
	public <T> PcodeTracePropertyAccess<T> getPropertyAccess(String name, Class<T> type) {
		return new DefaultPcodeTracePropertyAccess<>(this, name, type);
	}
}


text:
/**
 * Support and FAQ: visit Atmel Support
 */
#ifndef _CONF_SD_MMC_SPI_H_
#define _CONF_SD_MMC_SPI_H_

#include "board.h"
#include "conf_access.h"

#if SD_MMC_SPI_MEM == DISABLE
  #error conf_sd_mmc_spi.h is #included although SD_MMC_SPI_MEM is disabled
#endif

#include "sd_mmc_spi.h"

//_____ D E F I N I T I O N S ______________________________________________

//! SPI master speed in Hz.
#define SD_MMC_SPI_MASTER_SPEED     12000000

//! Number of bits in each SPI transfer.
#define SD_MMC_SPI_BITS             8

#if !defined(SD_MMC_SPI)
//! Set SD_MMC_SPI, default SPI register address if this is a user board
#warning "Using a default SD_MMC_SPI define value. Edit the conf_sd_mmc_spi.h file to modify that define value according to the current board."
#if (UC3L || UC3B || UC3D)
#define SD_MMC_SPI                 (&AVR32_SPI)
#else
#define SD_MMC_SPI                 (&AVR32_SPI0)
#endif
#endif

#if !defined(SD_MMC_SPI_NPCS)
//! Set SD_MMC_SPI_NPCS, default chip select if this is a user board
#warning "Using a default SD_MMC_SPI_NPCS define value. Edit the conf_sd_mmc_spi.h file to modify that define value according to the current board."
#define SD_MMC_SPI_NPCS            0
#endif

#endif  // _CONF_SD_MMC_SPI_H_


text:
@interface RMTopping : NSObject

@property (nonatomic, strong) NSString* id;
@property (nonatomic, strong) NSString* type;

@end


text:
module DynamicImage
  module Model
    # = DynamicImage Model Dimensions
    #
    module Dimensions
      # Returns the crop gravity.
      #
      # DynamicImage will try to keep the pixel represented by
      # crop_gravity as close to the center as possible when cropping
      # images.
      #
      # It is relative to 0,0 on the original image.
      #
      # Unless crop_gravity has been explicitely set, it defaults to
      # the center of the cropped image.
      def crop_gravity
        if crop_gravity?
          vector(crop_gravity_x, crop_gravity_y)
        elsif cropped?
          crop_start + (crop_size / 2)
        elsif size?
          size / 2
        end
      end

      # Returns true if crop gravity has been explicitely set.
      def crop_gravity?
        crop_gravity_x.present? && crop_gravity_y.present?
      end

      # Returns the crop size, or nil if no cropping is applied.
      def crop_size
        vector(crop_width, crop_height) if crop_size?
      end

      # Returns true if crop size has been set.
      def crop_size?
        crop_width? && crop_height?
      end

      # Returns the crop start if set, or Vector2d(0, 0) if not.
      def crop_start
        if crop_start?
          vector(crop_start_x, crop_start_y)
        else
          vector(0, 0)
        end
      end

      # Returns true if crop start has been set.
      def crop_start?
        crop_start_x.present? && crop_start_y.present?
      end

      # Returns true if the image is cropped.
      def cropped?
        crop_size? && real_size? && crop_size != real_size
      end

      # Returns the real size of the image, without any cropping applied.
      def real_size
        vector(real_width, real_height) if real_size?
      end

      # Returns true if the size has been set.
      def real_size?
        real_width? && real_height?
      end

      # Returns the cropped size if the image has been cropped. If not,
      # it returns the actual size.
      def size
        crop_size || real_size
      end

      # Returns true if the image has size set.
      def size?
        size ? true : false
      end

      private

      def vector(width, height)
        Vector2d.new(width, height)
      end
    end
  end
end


text:
<?php

namespace ModuleGenerator\Module\Backend\Actions;

use ModuleGenerator\PhpGenerator\ClassName\ClassNameType;
use ModuleGenerator\PhpGenerator\ModuleName\ModuleNameType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class BackendActionType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add(
                'moduleName',
                ModuleNameType::class
            )
            ->add(
                'entityClassName',
                ClassNameType::class,
                [
                    'label' => 'Entity class name',
                    'name_label' => 'Entity class name',
                    'namespace_label' => 'Entity namespace (i.e.: Backend\\Modules\MyModule\Domain\MyEntity)',
                ]
            );
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(
            [
                'data_class' => BackendActionDataTransferObject::class,
            ]
        );
    }
}


text:
from __future__ import annotations

import logging

from doc2dash.parsers.types import Parser

from .docsets import DocSet
from .parsers.patcher import patch_anchors

log = logging.getLogger(__name__)

def convert_docs(
    *,
    parser: Parser,
    docset: DocSet,
    quiet: bool,
) -> None:
    """
    User *parser* to parse, index, and patch *docset*.
    """
    log.info("Parsing documentation...")
    with docset.db_conn:
        toc = patch_anchors(parser, docset.docs, show_progressbar=not quiet)
        next(toc)

        for entry in parser.parse():
            docset.db_conn.execute(
                "INSERT INTO searchIndex VALUES (NULL, ?, ?, ?)",
                entry.as_tuple(),
            )
            toc.send(entry)

        count = docset.db_conn.execute(
            "SELECT COUNT(1) FROM searchIndex"
        ).fetchone()[0]

    color = "green" if count > 0 else "red"
    log.info(f"Added [{color}]{count:,}[/{color}] index entries.")

    # Now patch for TOCs.
    toc.close()


text:
#ifndef I915_CONTEXT_H
#define I915_CONTEXT_H

#include "pipe/p_context.h"
#include "pipe/p_defines.h"
#include "pipe/p_state.h"

#include "draw/draw_vertex.h"

#include "tgsi/tgsi_scan.h"

#include "util/u_slab.h"
#include "util/u_blitter.h"

struct i915_winsys;
struct i915_winsys_buffer;
struct i915_winsys_batchbuffer;

#define I915_TEX_UNITS 8

#define I915_DYNAMIC_MODES4       0
#define I915_DYNAMIC_DEPTHSCALE_0 1 /* just the header */
#define I915_DYNAMIC_DEPTHSCALE_1 2
#define I915_DYNAMIC_IAB          3
#define I915_DYNAMIC_BC_0         4 /* just the header */
#define I915_DYNAMIC_BC_1         5
#define I915_DYNAMIC_BFO_0        6
#define I915_DYNAMIC_BFO_1        7
#define I915_DYNAMIC_STP_0        8
#define I915_DYNAMIC_STP_1        9
#define I915_DYNAMIC_SC_ENA_0     10
#define I915_DYNAMIC_SC_RECT_0    11
#define I915_DYNAMIC_SC_RECT_1    12
#define I915_DYNAMIC_SC_RECT_2    13
#define I915_MAX_DYNAMIC          14

#define I915_IMMEDIATE_S0         0
#define I915_IMMEDIATE_S1         1
#define I915_IMMEDIATE_S2         2
#define I915_IMMEDIATE_S3         3
#define I915_IMMEDIATE_S4         4
#define I915_IMMEDIATE_S5         5
#define I915_IMMEDIATE_S6         6
#define I915_IMMEDIATE_S7         7
#define I915_MAX_IMMEDIATE        8

/* These must mach the order of LI0_STATE_* bits, as they will be used
 * to generate hardware packets:
 */
#define I915_CACHE_STATIC         0
#define I915_CACHE_DYNAMIC        1 /* handled specially */
#define I915_CACHE_SAMPLER        2
#define I915_CACHE_MAP            3
#define I915_CACHE_PROGRAM        4
#define I915_CACHE_CONSTANTS      5
#define I915_MAX_CACHE            6

#define I915_MAX_CONSTANT  32

/** See constant_flags[] below */
#define I915_CONSTFLAG_USER 0x1f

/**
 * Subclass of pipe_shader_state
 */
struct i915_fragment_shader
{
   struct pipe_shader_state state;

   struct tgsi_shader_info info;

   struct draw_fragment_shader *draw_data;

   uint *decl;
   uint decl_len;

   uint *program;
   uint program_len;

   /**
    * constants introduced during translation.
    * These are placed at the end of the constant buffer and grow toward
    * the beginning (eg: slot 31, 30 29, ...)
    * User-provided constants start at 0.
    * This allows both types of constants to co-exist (until there's too many)
    * and doesn't require regenerating/changing the fragment program to
    * shuffle constants around.
    */
   uint num_constants;
   float constants[I915_MAX_CONSTANT][4];

   /**
    * Status of each constant
    * if I915_CONSTFLAG_PARAM, the value must be taken from the corresponding
    * slot of the user's constant buffer. (set by pipe->set_constant_buffer())
    * Else, the bitmask indicates which components are occupied by immediates.
    */
   ubyte constant_flags[I915_MAX_CONSTANT];

   /**
    * The mapping between generics and hw texture coords.
    * We need to share this between the vertex and fragment stages.
    **/
   int generic_mapping[I915_TEX_UNITS];
};

struct i915_cache_context;

/* Use to calculate differences between state emitted to hardware and
 * current driver-calculated state.
 */
struct i915_state
{
   unsigned immediate[I915_MAX_IMMEDIATE];
   unsigned dynamic[I915_MAX_DYNAMIC];

   /** number of constants passed in through a constant buffer */
   uint num_user_constants[PIPE_SHADER_TYPES];

   /* texture sampler state */
   unsigned sampler[I915_TEX_UNITS][3];
   unsigned sampler_enable_flags;
   unsigned sampler_enable_nr;
   boolean sampler_srgb[I915_TEX_UNITS];
   int srgb_const_offset;

   /* texture image buffers */
   unsigned texbuffer[I915_TEX_UNITS][2];

   /** Describes the current hardware vertex layout */
   struct vertex_info vertex_info;

   /* static state (dst/depth buffer state) */
   struct i915_winsys_buffer *cbuf_bo;
   unsigned cbuf_flags;
   struct i915_winsys_buffer *depth_bo;
   unsigned depth_flags;
   unsigned dst_buf_vars;
   uint32_t draw_offset;
   uint32_t draw_size;
   uint32_t target_fixup_format;
   uint32_t fixup_swizzle;

   unsigned id;			/* track lost context events */
};

struct i915_blend_state {
   unsigned iab;
   unsigned modes4;
   unsigned LIS5;
   unsigned LIS6;
};

struct i915_depth_stencil_state {
   unsigned stencil_modes4;
   unsigned bfo[2];
   unsigned stencil_LIS5;
   unsigned depth_LIS6;
};

struct i915_rasterizer_state {
   struct pipe_rasterizer_state templ;

   unsigned light_twoside : 1;
   unsigned st;
   enum interp_mode color_interp;

   unsigned LIS4;
   unsigned LIS7;
   unsigned sc[1];

   union { float f; unsigned u; } ds[2];
};

struct i915_sampler_state {
   struct pipe_sampler_state templ;
   unsigned state[3];
   unsigned minlod;
   unsigned maxlod;
};

struct i915_velems_state {
   unsigned count;
   struct pipe_vertex_element velem[PIPE_MAX_ATTRIBS];
};

struct i915_context {
   struct pipe_context base;

   struct i915_winsys *iws;

   struct draw_context *draw;

   /* The most recent drawing state as set by the driver:
    */
   const struct i915_blend_state           *blend;
   const struct i915_sampler_state         *sampler[PIPE_MAX_SAMPLERS];
   struct pipe_sampler_state *vertex_samplers[PIPE_MAX_SAMPLERS];
   const struct i915_depth_stencil_state   *depth_stencil;
   const struct i915_rasterizer_state      *rasterizer;

   struct i915_fragment_shader *fs;

   struct pipe_blend_color blend_color;
   struct pipe_stencil_ref stencil_ref;
   struct pipe_clip_state clip;
   struct pipe_resource *constants[PIPE_SHADER_TYPES];
   struct pipe_framebuffer_state framebuffer;
   struct pipe_poly_stipple poly_stipple;
   struct pipe_scissor_state scissor;
   struct pipe_sampler_view *fragment_sampler_views[PIPE_MAX_SAMPLERS];
   struct pipe_sampler_view *vertex_sampler_views[PIPE_MAX_SAMPLERS];
   struct pipe_viewport_state viewport;
   struct pipe_index_buffer index_buffer;

   unsigned dirty;

   struct pipe_resource *mapped_vs_tex[PIPE_MAX_SAMPLERS];
   struct i915_winsys_buffer* mapped_vs_tex_buffer[PIPE_MAX_SAMPLERS];

   unsigned num_samplers;
   unsigned num_fragment_sampler_views;
   unsigned num_vertex_samplers;
   unsigned num_vertex_sampler_views;

   struct i915_winsys_batchbuffer *batch;

   /** Vertex buffer */
   struct i915_winsys_buffer *vbo;
   size_t vbo_offset;
   unsigned vbo_flushed;

   struct i915_state current;
   unsigned hardware_dirty;
   unsigned immediate_dirty : I915_MAX_IMMEDIATE;
   unsigned dynamic_dirty : I915_MAX_DYNAMIC;
   unsigned static_dirty : 4;
   unsigned flush_dirty : 2;

   struct i915_winsys_buffer *validation_buffers[2 + 1 + I915_TEX_UNITS];
   int num_validation_buffers;

   struct util_slab_mempool transfer_pool;
   struct util_slab_mempool texture_transfer_pool;

   /* state for tracking flushes */
   int last_fired_vertices;
   int fired_vertices;
   int queued_vertices;

   /** blitter/hw-clear */
   struct blitter_context* blitter;

   /** State tracking needed by u_blitter for save/restore. */
   void *saved_fs;
   void (*saved_bind_fs_state)(struct pipe_context *pipe, void *shader);
   void *saved_vs;
   struct pipe_clip_state saved_clip;
   struct i915_velems_state *saved_velems;
   unsigned saved_nr_vertex_buffers;
   struct pipe_vertex_buffer saved_vertex_buffers[PIPE_MAX_ATTRIBS];
   unsigned saved_nr_samplers;
   void *saved_samplers[PIPE_MAX_SAMPLERS];
   void (*saved_bind_sampler_states)(struct pipe_context *pipe,
                                     unsigned num, void **sampler);
   unsigned saved_nr_sampler_views;
   struct pipe_sampler_view *saved_sampler_views[PIPE_MAX_SAMPLERS];
   void (*saved_set_sampler_views)(struct pipe_context *pipe,
                                   unsigned num, struct pipe_sampler_view **views);
};

/* A flag for each state_tracker state object:
 */
#define I915_NEW_VIEWPORT      0x1
#define I915_NEW_RASTERIZER    0x2
#define I915_NEW_FS            0x4
#define I915_NEW_BLEND         0x8
#define I915_NEW_CLIP          0x10
#define I915_NEW_SCISSOR       0x20
#define I915_NEW_STIPPLE       0x40
#define I915_NEW_FRAMEBUFFER   0x80
#define I915_NEW_ALPHA_TEST    0x100
#define I915_NEW_DEPTH_STENCIL 0x200
#define I915_NEW_SAMPLER       0x400
#define I915_NEW_SAMPLER_VIEW  0x800
#define I915_NEW_VS_CONSTANTS  0x1000
#define I915_NEW_FS_CONSTANTS  0x2000
#define I915_NEW_GS_CONSTANTS  0x4000
#define I915_NEW_VBO           0x8000
#define I915_NEW_VS            0x10000

/* Driver's internally generated state flags:
 */
#define I915_NEW_VERTEX_FORMAT    0x10000

/* Dirty flags for hardware emit
 */
#define I915_HW_STATIC            (1<<I915_CACHE_STATIC)
#define I915_HW_DYNAMIC           (1<<I915_CACHE_DYNAMIC)
#define I915_HW_SAMPLER           (1<<I915_CACHE_SAMPLER)
#define I915_HW_MAP               (1<<I915_CACHE_MAP)
#define I915_HW_PROGRAM           (1<<I915_CACHE_PROGRAM)
#define I915_HW_CONSTANTS         (1<<I915_CACHE_CONSTANTS)
#define I915_HW_IMMEDIATE         (1<<(I915_MAX_CACHE+0))
#define I915_HW_INVARIANT         (1<<(I915_MAX_CACHE+1))
#define I915_HW_FLUSH             (1<<(I915_MAX_CACHE+1))

/* hw flush handling */
#define I915_FLUSH_CACHE		1
#define I915_PIPELINE_FLUSH		2

/* split up static state */
#define I915_DST_BUF_COLOR              1
#define I915_DST_BUF_DEPTH              2
#define I915_DST_VARS                   4
#define I915_DST_RECT                   8

static INLINE
void i915_set_flush_dirty(struct i915_context *i915, unsigned flush)
{
   i915->hardware_dirty |= I915_HW_FLUSH;
   i915->flush_dirty |= flush;
}

/***********************************************************************
 * i915_prim_emit.c:
 */
struct draw_stage *i915_draw_render_stage( struct i915_context *i915 );

/***********************************************************************
 * i915_prim_vbuf.c:
 */
struct draw_stage *i915_draw_vbuf_stage( struct i915_context *i915 );

/***********************************************************************
 * i915_state.c:
 */
void i915_prepare_vertex_sampling(struct i915_context *i915);
void i915_cleanup_vertex_sampling(struct i915_context *i915);

/***********************************************************************
 * i915_state_emit.c:
 */
void i915_emit_hardware_state(struct i915_context *i915 );

/***********************************************************************
 * i915_clear.c:
 */
void i915_clear_blitter(struct pipe_context *pipe, unsigned buffers,
                        const union pipe_color_union *color,
                        double depth, unsigned stencil);
void i915_clear_render(struct pipe_context *pipe, unsigned buffers,
                       const union pipe_color_union *color,
                       double depth, unsigned stencil);
void i915_clear_emit(struct pipe_context *pipe, unsigned buffers,
                     const union pipe_color_union *color,
                     double depth, unsigned stencil,
                     unsigned destx, unsigned desty, unsigned width, unsigned height);

/***********************************************************************
 *
 */
void i915_init_state_functions( struct i915_context *i915 );
void i915_init_fixup_state_functions( struct i915_context *i915 );
void i915_init_flush_functions( struct i915_context *i915 );
void i915_init_string_functions( struct i915_context *i915 );

/************************************************************************
 * i915_context.c
 */
struct pipe_context *i915_create_context(struct pipe_screen *screen,
					 void *priv);

/***********************************************************************
 * Inline conversion functions.  These are better-typed than the
 * macros used previously:
 */
static INLINE struct i915_context *
i915_context( struct pipe_context *pipe )
{
   return (struct i915_context *)pipe;
}

#endif


text:
nohup dstat -tcm --output /home/panfengfeng/trace_log/in-memory/movies/readrandom_multi_rocksdb_256_3G 2 > nohup.out &

file=/datainssd/publicdata/movies/movies.txt
record_num=7911684
read_num=7911684
dirname=/mnt/datamemory
writebuffer=268435456
#cachesize=268435456
cachesize=3110962490

rm -rf $dirname/*
echo "####Now, running rocksdb benchmark"
echo 3 > /proc/sys/vm/drop_caches
free -m
date
../../db_movies_rocksdb --benchmarks=fillrandom --num=$record_num --write_buffer_size=$writebuffer --bloom_bits=5 --db=$dirname --resource_data=$file
date
echo "####rocksdb benchmark finish"
du -s -b $dirname

echo "####Now, running rocksdb benchmark"
echo 3 > /proc/sys/vm/drop_caches
free -m
date
../../db_movies_rocksdb --benchmarks=readrandom --num=$record_num --reads=$read_num --write_buffer_size=$writebuffer --cache_size=$cachesize --bloom_bits=5 --db=$dirname --use_existing_db=1 --resource_data=$file
free -m
date
echo "####rocksdb benchmark finish"
du -s -b $dirname

echo "####Now, running rocksdb benchmark"
echo 3 > /proc/sys/vm/drop_caches
free -m
date
../../db_movies_rocksdb --benchmarks=readrandom --num=$record_num --reads=$read_num --write_buffer_size=$writebuffer --cache_size=$cachesize --bloom_bits=5 --db=$dirname --use_existing_db=1 --threads=8 --resource_data=$file
free -m
date
echo "####rocksdb benchmark finish"
du -s -b $dirname

echo "####Now, running rocksdb benchmark"
echo 3 > /proc/sys/vm/drop_caches
free -m
date
../../db_movies_rocksdb --benchmarks=readrandom --num=$record_num --reads=$read_num --write_buffer_size=$writebuffer --cache_size=$cachesize --bloom_bits=5 --db=$dirname --use_existing_db=1 --threads=16 --resource_data=$file
free -m
date
echo "####rocksdb benchmark finish"
du -s -b $dirname

dstatpid=`ps aux | grep dstat | awk '{if($0 !~ "grep"){print $2}}'`
for i in $dstatpid
do
        kill -9 $i
done


text:
/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.trino.spi.connector;

public final class TableProcedureExecutionMode
{
    private final boolean readsData;
    private final boolean supportsFilter;

    private TableProcedureExecutionMode(boolean readsData, boolean supportsFilter)
    {
        if (!readsData) {
            if (supportsFilter) {
                throw new IllegalArgumentException("filtering not supported if table data is not processed");
            }
        }
        this.readsData = readsData;
        this.supportsFilter = supportsFilter;
    }

    public boolean isReadsData()
    {
        return readsData;
    }

    public boolean supportsFilter()
    {
        return supportsFilter;
    }

    /**
     * Table procedure that does not read any table data and only executes on the coordinator.
     * Such procedures are useful for custom DDL-type operations.
     */
    public static TableProcedureExecutionMode coordinatorOnly()
    {
        return new TableProcedureExecutionMode(false, false);
    }

    public static TableProcedureExecutionMode distributedWithFilteringAndRepartitioning()
    {
        return new TableProcedureExecutionMode(true, true);
    }
}


text:
package dataworks_public

//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
//http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
//
// Code generated by Alibaba Cloud SDK Code Generator.
// Changes may cause incorrect behavior and will be lost if the code is regenerated.

import (
	"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
	"github.com/aliyun/alibaba-cloud-sdk-go/sdk/responses"
)

// GetMetaTableIntroWiki invokes the dataworks_public.GetMetaTableIntroWiki API synchronously
func (client *Client) GetMetaTableIntroWiki(request *GetMetaTableIntroWikiRequest) (response *GetMetaTableIntroWikiResponse, err error) {
	response = CreateGetMetaTableIntroWikiResponse()
	err = client.DoAction(request, response)
	return
}

// GetMetaTableIntroWikiWithChan invokes the dataworks_public.GetMetaTableIntroWiki API asynchronously
func (client *Client) GetMetaTableIntroWikiWithChan(request *GetMetaTableIntroWikiRequest) (<-chan *GetMetaTableIntroWikiResponse, <-chan error) {
	responseChan := make(chan *GetMetaTableIntroWikiResponse, 1)
	errChan := make(chan error, 1)
	err := client.AddAsyncTask(func() {
		defer close(responseChan)
		defer close(errChan)
		response, err := client.GetMetaTableIntroWiki(request)
		if err != nil {
			errChan <- err
		} else {
			responseChan <- response
		}
	})
	if err != nil {
		errChan <- err
		close(responseChan)
		close(errChan)
	}
	return responseChan, errChan
}

// GetMetaTableIntroWikiWithCallback invokes the dataworks_public.GetMetaTableIntroWiki API asynchronously
func (client *Client) GetMetaTableIntroWikiWithCallback(request *GetMetaTableIntroWikiRequest, callback func(response *GetMetaTableIntroWikiResponse, err error)) <-chan int {
	result := make(chan int, 1)
	err := client.AddAsyncTask(func() {
		var response *GetMetaTableIntroWikiResponse
		var err error
		defer close(result)
		response, err = client.GetMetaTableIntroWiki(request)
		callback(response, err)
		result <- 1
	})
	if err != nil {
		defer close(result)
		callback(nil, err)
		result <- 0
	}
	return result
}

// GetMetaTableIntroWikiRequest is the request struct for api GetMetaTableIntroWiki
type GetMetaTableIntroWikiRequest struct {
	*requests.RpcRequest
	WikiVersion requests.Integer `position:"Query" name:"WikiVersion"`
	TableGuid   string           `position:"Query" name:"TableGuid"`
}

// GetMetaTableIntroWikiResponse is the response struct for api GetMetaTableIntroWiki
type GetMetaTableIntroWikiResponse struct {
	*responses.BaseResponse
	HttpStatusCode int    `json:"HttpStatusCode" xml:"HttpStatusCode"`
	ErrorMessage   string `json:"ErrorMessage" xml:"ErrorMessage"`
	RequestId      string `json:"RequestId" xml:"RequestId"`
	Success        bool   `json:"Success" xml:"Success"`
	ErrorCode      string `json:"ErrorCode" xml:"ErrorCode"`
	Data           Data   `json:"Data" xml:"Data"`
}

// CreateGetMetaTableIntroWikiRequest creates a request to invoke GetMetaTableIntroWiki API
func CreateGetMetaTableIntroWikiRequest() (request *GetMetaTableIntroWikiRequest) {
	request = &GetMetaTableIntroWikiRequest{
		RpcRequest: &requests.RpcRequest{},
	}
	request.InitWithApiInfo("dataworks-public", "2020-05-18", "GetMetaTableIntroWiki", "", "")
	request.Method = requests.POST
	return
}

// CreateGetMetaTableIntroWikiResponse creates a response to parse from GetMetaTableIntroWiki response
func CreateGetMetaTableIntroWikiResponse() (response *GetMetaTableIntroWikiResponse) {
	response = &GetMetaTableIntroWikiResponse{
		BaseResponse: &responses.BaseResponse{},
	}
	return
}


text:
package org.mifos.reports.cashconfirmationreport.persistence;

import static org.mifos.framework.util.helpers.MoneyUtils.zero;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

import junit.framework.Assert;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.mifos.accounts.productdefinition.persistence.PrdOfferingPersistence;
import org.mifos.accounts.util.helpers.AccountTypes;
import org.mifos.application.master.business.MifosCurrency;
import org.mifos.framework.TestUtils;
import org.mifos.framework.hibernate.helper.StaticHibernateUtil;
import org.mifos.framework.util.AssertionUtils;
import org.mifos.framework.util.CollectionUtils;
import org.mifos.framework.util.helpers.DateUtils;
import org.mifos.reports.business.service.BranchReportIntegrationTestCase;
import org.mifos.reports.cashconfirmationreport.BranchCashConfirmationCenterRecoveryBO;
import org.mifos.reports.cashconfirmationreport.BranchCashConfirmationDisbursementBO;
import org.mifos.reports.cashconfirmationreport.BranchCashConfirmationInfoBO;
import org.mifos.reports.cashconfirmationreport.BranchCashConfirmationIssueBO;
import org.mifos.reports.cashconfirmationreport.BranchCashConfirmationReportBO;

public class BranchCashConfirmationReportPersistenceIntegrationTest extends BranchReportIntegrationTestCase {
    public BranchCashConfirmationReportPersistenceIntegrationTest() throws Exception {
        super();
    }

    private static final int DIGITS_AFTER_DECIMAL_FOR_AMOUNT_IN_DB = 3;
    private static final List<Short> DISBURSEMENT_PRODUCT_OFFERING_IDS = Arrays.asList(new Short[] { 1, 2 });
    private Session session;
    private Transaction transaction;
    private BranchCashConfirmationReportPersistence persistence;
    private BranchCashConfirmationReportBO reportBO;
    private MifosCurrency currency;
    private BranchCashConfirmationReportBO firstJanReportBO;

    public void testRetrievesTheReportFromDB() throws Exception {
        session.save(firstJanReportBO);
        List<BranchCashConfirmationReportBO> fetchedReports = persistence
                .getBranchCashConfirmationReportsForDate(FIRST_JAN_2008);
        Assert.assertNotNull(fetchedReports);
       Assert.assertEquals(1, fetchedReports.size());
       Assert.assertEquals(firstJanReportBO, fetchedReports.get(0));
    }

    public void testReturnsEmptyListIfReportForGivenDateAndBranchDoesNotExist() throws Exception {
        List<BranchCashConfirmationReportBO> report = persistence.getBranchCashConfirmationReportsForDateAndBranch(
                Short.valueOf("3"), FIRST_JAN_2008);
        Assert.assertNotNull(report);
       Assert.assertTrue(report.isEmpty());
    }

    public void testReturnsReportListIfReportForGivenDateAndBranchExist() throws Exception {
        session.save(firstJanReportBO);
        List<BranchCashConfirmationReportBO> report = persistence.getBranchCashConfirmationReportsForDateAndBranch(
                BRANCH_ID_SHORT, FIRST_JAN_2008);
        Assert.assertNotNull(report);
       Assert.assertEquals(1, report.size());
    }

    public void testRemovesTheReportFromDB() throws Exception {
        BranchCashConfirmationReportBO newReport = new BranchCashConfirmationReportBO(Short.valueOf("1"), RUN_DATE);
        session.save(newReport);
        persistence.delete(newReport);
        List<BranchCashConfirmationReportBO> fetchedReports = persistence
                .getBranchCashConfirmationReportsForDate(RUN_DATE);
        Assert.assertNotNull(fetchedReports);
       Assert.assertTrue(fetchedReports.isEmpty());
    }

    public void testRetrieveIssueReportForGivenDateAndBranch() throws Exception {
        BranchCashConfirmationInfoBO issueBO = new BranchCashConfirmationIssueBO("SOME PRODUCT", zero());
        reportBO.addCenterIssue(issueBO);
        BranchCashConfirmationInfoBO anotherIssue = new BranchCashConfirmationIssueBO("SOMEMORE", zero());
        reportBO.addCenterIssue(anotherIssue);
        session.save(reportBO);
        List<BranchCashConfirmationInfoBO> retrievedIssues = persistence.getCenterIssues(BRANCH_ID_SHORT, RUN_DATE);
        Assert.assertNotNull(retrievedIssues);
       Assert.assertEquals(2, retrievedIssues.size());
       Assert.assertTrue(retrievedIssues.contains(issueBO));
       Assert.assertTrue(retrievedIssues.contains(anotherIssue));
    }

    public void testRetrievesCenterRecoveryReport() throws Exception {
        BranchCashConfirmationCenterRecoveryBO recoveryReport = new BranchCashConfirmationCenterRecoveryBO("PRDOFF1",
                zero(), zero(), zero());
        reportBO.addCenterRecovery(recoveryReport);
        session.save(reportBO);
        List<BranchCashConfirmationCenterRecoveryBO> retrievedRecoveryReport = persistence.getCenterRecoveries(
                BRANCH_ID_SHORT, RUN_DATE);
        Assert.assertNotNull(retrievedRecoveryReport);
       Assert.assertEquals(1, retrievedRecoveryReport.size());
       Assert.assertEquals(recoveryReport, retrievedRecoveryReport.get(0));
    }

    public void testExtractDisbursementReturnsEmptyListIfNoneExists() throws Exception {
        List<Object[]> disbursements = persistence.extractDisbursements(currency, AccountTypes.LOAN_ACCOUNT,
                DISBURSEMENT_PRODUCT_OFFERING_IDS, DateUtils.currentDate());
        Assert.assertNotNull(disbursements);
       Assert.assertTrue("disbursements should be empty", disbursements.isEmpty());
    }

    // TODO TW Test depends on inserting data into db breaking existing tests
    public void xtestExtractDisbursementsForGivenProductsAndDate() throws Exception {
        List<Object[]> disbursements = persistence.extractDisbursements(currency, AccountTypes.LOAN_ACCOUNT,
                DISBURSEMENT_PRODUCT_OFFERING_IDS, FIRST_JAN_2008);
        Assert.assertNotNull(disbursements);
        Assert.assertFalse("disbursements should not be empty", disbursements.isEmpty());
        AssertionUtils
                .assertSameCollections(Arrays.asList(new Object[] { Short.valueOf("3"), "TESTPRDOFFERING1",
                        BigDecimal.ZERO.setScale(DIGITS_AFTER_DECIMAL_FOR_AMOUNT_IN_DB) }), Arrays.asList(disbursements
                        .get(0)));
    }

    public void testRetrievesDisbursementsReturnsEmptyListIfNoneExists() throws Exception {
        List<BranchCashConfirmationDisbursementBO> disbursements = persistence.getDisbursements(BRANCH_ID_SHORT,
                RUN_DATE);
        Assert.assertNotNull("retrieved disbursements should not be null", disbursements);
       Assert.assertTrue("retrieved disbursements should be empty", disbursements.isEmpty());
    }

    public void testRetrievesDisbursementsForGivenDateAndBranch() throws Exception {
        BranchCashConfirmationDisbursementBO disbursement = new BranchCashConfirmationDisbursementBO("SOME PRODUCT",
                zero());
        BranchCashConfirmationDisbursementBO anotherDisbursement = new BranchCashConfirmationDisbursementBO(
                "ANOTHER PRODUCT", zero());
        reportBO.addDisbursement(disbursement);
        reportBO.addDisbursement(anotherDisbursement);
        session.save(reportBO);
        List<BranchCashConfirmationDisbursementBO> retrievedDisbursements = persistence.getDisbursements(
                BRANCH_ID_SHORT, RUN_DATE);
        Assert.assertNotNull("retrieved disbursements should not be null", retrievedDisbursements);
        Assert.assertFalse("retrieved disbursements should not be empty", retrievedDisbursements.isEmpty());
        AssertionUtils.assertSameCollections(CollectionUtils.asList(disbursement, anotherDisbursement),
                retrievedDisbursements);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        session = StaticHibernateUtil.getSessionTL();
        transaction = session.beginTransaction();
        persistence = new BranchCashConfirmationReportPersistence(new PrdOfferingPersistence());
        reportBO = new BranchCashConfirmationReportBO(BRANCH_ID_SHORT, RUN_DATE);
        firstJanReportBO = new BranchCashConfirmationReportBO(BRANCH_ID_SHORT, FIRST_JAN_2008);
        currency = TestUtils.RUPEE;
    }

    @Override
    protected void tearDown() throws Exception {
        transaction.rollback();
        super.tearDown();
    }
}