生成insertall BulkWrite对象
@access public
@param array $dataSet 数据集
@param array $options 参数
@return BulkWrite
760761
public function insertAll($dataSet, $options = [])
    {
        $bulk = new BulkWrite;
        foreach ($dataSet as $data) {
            // 分析并处理数据
            $data = $this->parseData($data, $options);
            if ($insertId = $bulk->insert($data)) {
                $this->insertId[] = $insertId;
            }
        }
        $this->log('insert', $dataSet, $options);
        return $bulk;
    }
760249
protected function _normalize($dataset, $options) {
    // create quads and map bnodes to their associated quads
    $quads = array();
    $bnodes = new stdClass();
    foreach($dataset as $graph_name => $triples) {
      if($graph_name === '@default') {
        $graph_name = null;
      }
      foreach($triples as $triple) {
        $quad = $triple;
        if($graph_name !== null) {
          if(strpos($graph_name, '_:') === 0) {
            $quad->name = (object)array(
              'type' => 'blank node', 'value' => $graph_name);
          } else {
            $quad->name = (object)array(
              'type' => 'IRI', 'value' => $graph_name);
          }
        }
        $quads[] = $quad;

        foreach(array('subject', 'object', 'name') as $attr) {
          if(property_exists($quad, $attr) &&
            $quad->{$attr}->type === 'blank node') {
            $id = $quad->{$attr}->value;
            if(property_exists($bnodes, $id)) {
              $bnodes->{$id}->quads[] = $quad;
            } else {
              $bnodes->{$id} = (object)array('quads' => array($quad));
            }
          }
        }
      }
    }

    // mapping complete, start canonical naming
    $namer = new UniqueNamer('_:c14n');

    // continue to hash bnode quads while bnodes are assigned names
    $unnamed = null;
    $nextUnnamed = array_keys((array)$bnodes);
    $duplicates = null;
    do {
      $unnamed = $nextUnnamed;
      $nextUnnamed = array();
      $duplicates = new stdClass();
      $unique = new stdClass();
      foreach($unnamed as $bnode) {
        // hash quads for each unnamed bnode
        $hash = $this->_hashQuads($bnode, $bnodes, $namer);

        // store hash as unique or a duplicate
        if(property_exists($duplicates, $hash)) {
          $duplicates->{$hash}[] = $bnode;
          $nextUnnamed[] = $bnode;
        } else if(property_exists($unique, $hash)) {
          $duplicates->{$hash} = array($unique->{$hash}, $bnode);
          $nextUnnamed[] = $unique->{$hash};
          $nextUnnamed[] = $bnode;
          unset($unique->{$hash});
        } else {
          $unique->{$hash} = $bnode;
        }
      }

      // name unique bnodes in sorted hash order
      $hashes = array_keys((array)$unique);
      sort($hashes);
      foreach($hashes as $hash) {
        $namer->getName($unique->{$hash});
      }
    }
    while(count($unnamed) > count($nextUnnamed));

    // enumerate duplicate hash groups in sorted order
    $hashes = array_keys((array)$duplicates);
    sort($hashes);
    foreach($hashes as $hash) {
      // process group
      $group = $duplicates->{$hash};
      $results = array();
      foreach($group as $bnode) {
        // skip already-named bnodes
        if($namer->isNamed($bnode)) {
          continue;
        }

        // hash bnode paths
        $path_namer = new UniqueNamer('_:b');
        $path_namer->getName($bnode);
        $results[] = $this->_hashPaths($bnode, $bnodes, $namer, $path_namer);
      }

      // name bnodes in hash order
      usort($results, function($a, $b) {
        $a = $a->hash;
        $b = $b->hash;
        return ($a < $b) ? -1 : (($a > $b) ? 1 : 0);
      });
      foreach($results as $result) {
        // name all bnodes in path namer in key-entry order
        foreach($result->pathNamer->order as $bnode) {
          $namer->getName($bnode);
        }
      }
    }

    // create normalized array
    $normalized = array();

    /* Note: At this point all bnodes in the set of RDF quads have been
     assigned canonical names, which have been stored in the 'namer' object.
     Here each quad is updated by assigning each of its bnodes its new name
     via the 'namer' object. */

    // update bnode names in each quad and serialize
    foreach($quads as $quad) {
      foreach(array('subject', 'object', 'name') as $attr) {
        if(property_exists($quad, $attr) &&
          $quad->{$attr}->type === 'blank node' &&
          strpos($quad->{$attr}->value, '_:c14n') !== 0) {
          $quad->{$attr}->value = $namer->getName($quad->{$attr}->value);
        }
      }
      $normalized[] = $this->toNQuad($quad, property_exists($quad, 'name') ?
        $quad->name->value : null);
    }

    // sort normalized output
    sort($normalized);

    // handle output format
    if(isset($options['format']) && $options['format']) {
      if($options['format'] === 'application/nquads') {
        return implode($normalized);
      }
      throw new JsonLdException(
        'Unknown output format.',
        'jsonld.UnknownFormat', null, array('format' => $options['format']));
    }

    // return RDF dataset
    return $this->parseNQuads(implode($normalized));
  }
695191
protected function _fromRDF($dataset, $options) {
    $default_graph = new stdClass();
    $graph_map = (object)array('@default' => $default_graph);
    $referenced_once = (object)array();

    foreach($dataset as $name => $graph) {
      if(!property_exists($graph_map, $name)) {
        $graph_map->{$name} = new stdClass();
      }
      if($name !== '@default' && !property_exists($default_graph, $name)) {
        $default_graph->{$name} = (object)array('@id' => $name);
      }
      $node_map = $graph_map->{$name};
      foreach($graph as $triple) {
        // get subject, predicate, object
        $s = $triple->subject->value;
        $p = $triple->predicate->value;
        $o = $triple->object;

        if(!property_exists($node_map, $s)) {
          $node_map->{$s} = (object)array('@id' => $s);
        }
        $node = $node_map->{$s};

        $object_is_id = ($o->type === 'IRI' || $o->type === 'blank node');
        if($object_is_id && !property_exists($node_map, $o->value)) {
          $node_map->{$o->value} = (object)array('@id' => $o->value);
        }

        if($p === self::RDF_TYPE && !$options['useRdfType'] && $object_is_id) {
          self::addValue(
            $node, '@type', $o->value, array('propertyIsArray' => true));
          continue;
        }

        $value = self::_RDFToObject($o, $options['useNativeTypes']);
        self::addValue($node, $p, $value, array('propertyIsArray' => true));

        // object may be an RDF list/partial list node but we can't know
        // easily until all triples are read
        if($object_is_id) {
          if($o->value === self::RDF_NIL) {
            $object = $node_map->{$o->value};
            if(!property_exists($object, 'usages')) {
              $object->usages = array();
            }
            $object->usages[] = (object)array(
              'node' => $node,
              'property' => $p,
              'value' => $value);
          } else if(property_exists($referenced_once, $o->value)) {
            // object referenced more than once
            $referenced_once->{$o->value} = false;
          } else {
            // track single reference
            $referenced_once->{$o->value} = (object)array(
              'node' => $node,
              'property' => $p,
              'value' => $value);
          }
        }
      }
    }

    // convert linked lists to @list arrays
    foreach($graph_map as $name => $graph_object) {
      // no @lists to be converted, continue
      if(!property_exists($graph_object, self::RDF_NIL)) {
        continue;
      }

      // iterate backwards through each RDF list
      $nil = $graph_object->{self::RDF_NIL};
      foreach($nil->usages as $usage) {
        $node = $usage->node;
        $property = $usage->property;
        $head = $usage->value;
        $list = array();
        $list_nodes = array();

        // ensure node is a well-formed list node; it must:
        // 1. Be referenced only once.
        // 2. Have an array for rdf:first that has 1 item.
        // 3. Have an array for rdf:rest that has 1 item.
        // 4. Have no keys other than: @id, rdf:first, rdf:rest, and,
        //   optionally, @type where the value is rdf:List.
        $node_key_count = count(array_keys((array)$node));
        while($property === self::RDF_REST &&
          property_exists($referenced_once, $node->{'@id'}) &&
          is_object($referenced_once->{$node->{'@id'}}) &&
          property_exists($node, self::RDF_FIRST) &&
          property_exists($node, self::RDF_REST) &&
          is_array($node->{self::RDF_FIRST}) &&
          is_array($node->{self::RDF_REST}) &&
          count($node->{self::RDF_FIRST}) === 1 &&
          count($node->{self::RDF_REST}) === 1 &&
          ($node_key_count === 3 || ($node_key_count === 4 &&
            property_exists($node, '@type') && is_array($node->{'@type'}) &&
            count($node->{'@type'}) === 1 &&
            $node->{'@type'}[0] === self::RDF_LIST))) {
          $list[] = $node->{self::RDF_FIRST}[0];
          $list_nodes[] = $node->{'@id'};

          // get next node, moving backwards through list
          $usage = $referenced_once->{$node->{'@id'}};
          $node = $usage->node;
          $property = $usage->property;
          $head = $usage->value;
          $node_key_count = count(array_keys((array)$node));

          // if node is not a blank node, then list head found
          if(strpos($node->{'@id'}, '_:') !== 0) {
            break;
          }
        }

        // list is nested in another list
        if($property === self::RDF_FIRST) {
          // empty list
          if($node->{'@id'} === self::RDF_NIL) {
            // can't convert rdf:nil to a @list object because it would
            // result in a list of lists which isn't supported
            continue;
          }

          // preserve list head
          $head = $graph_object->{$head->{'@id'}}->{self::RDF_REST}[0];
          array_pop($list);
          array_pop($list_nodes);
        }

        // transform list into @list object
        unset($head->{'@id'});
        $head->{'@list'} = array_reverse($list);
        foreach($list_nodes as $list_node) {
          unset($graph_object->{$list_node});
        }
      }

      unset($nil->usages);
    }

    $result = array();
    $subjects = array_keys((array)$default_graph);
    sort($subjects);
    foreach($subjects as $subject) {
      $node = $default_graph->{$subject};
      if(property_exists($graph_map, $subject)) {
        $node->{'@graph'} = array();
        $graph_object = $graph_map->{$subject};
        $subjects_ = array_keys((array)$graph_object);
        sort($subjects_);
        foreach($subjects_ as $subject_) {
          $node_ = $graph_object->{$subject_};
          // only add full subjects to top-level
          if(!self::_isSubjectReference($node_)) {
            $node->{'@graph'}[] = $node_;
          }
        }
      }
      // only add full subjects to top-level
      if(!self::_isSubjectReference($node)) {
        $result[] = $node;
      }
    }

    return $result;
  }
1069877
public function insertAll(Query $query, array $dataSet)
    {
        // 分析查询表达式
        $options = $query->getOptions();

        if (!is_array(reset($dataSet))) {
            return false;
        }

        // 生成bulkWrite对象
        $bulk         = $this->builder->insertAll($query, $dataSet);
        $writeConcern = isset($options['writeConcern']) ? $options['writeConcern'] : null;
        $writeResult  = $this->execute($options['table'], $bulk, $writeConcern);

        return $writeResult->getInsertedCount();
    }
505563
public function executeInserts(array $options = []) : void
    {
        if (! $this->queuedInserts) {
            return;
        }

        $inserts = [];
        $options = $this->getWriteOptions($options);
        foreach ($this->queuedInserts as $oid => $document) {
            $data = $this->pb->prepareInsertData($document);

            // Set the initial version for each insert
            if ($this->class->isVersioned) {
                $versionMapping = $this->class->fieldMappings[$this->class->versionField];
                $nextVersion    = null;
                if ($versionMapping['type'] === 'int') {
                    $nextVersion = max(1, (int) $this->class->reflFields[$this->class->versionField]->getValue($document));
                    $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion);
                } elseif ($versionMapping['type'] === 'date') {
                    $nextVersionDateTime = new DateTime();
                    $nextVersion         = Type::convertPHPToDatabaseValue($nextVersionDateTime);
                    $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersionDateTime);
                }
                $data[$versionMapping['name']] = $nextVersion;
            }

            $inserts[] = $data;
        }

        if ($inserts) {
            try {
                assert($this->collection instanceof Collection);
                $this->collection->insertMany($inserts, $options);
            } catch (DriverException $e) {
                $this->queuedInserts = [];
                throw $e;
            }
        }

        /* All collections except for ones using addToSet have already been
         * saved. We have left these to be handled separately to avoid checking
         * collection for uniqueness on PHP side.
         */
        foreach ($this->queuedInserts as $document) {
            $this->handleCollections($document, $options);
        }

        $this->queuedInserts = [];
    }
497081
public function blindWrite($datasetId, Google_BlindWriteRequest $postBody, $optParams = array()) {
      $params = array('datasetId' => $datasetId, 'postBody' => $postBody);
      $params = array_merge($params, $optParams);
      $data = $this->__call('blindWrite', array($params));
      if ($this->useObjects()) {
        return new Google_BlindWriteResponse($data);
      } else {
        return $data;
      }
    }
34990
public function insert($model, array $options = [])
    {
        $values = $this->convertValues->convertToMongoValues(get_values($model), []);

        $result = $this->collection->insertOne($values, $options);
        if (false == $result->isAcknowledged()) {
            throw new \LogicException('Operation is not acknowledged');
        }

        set_object_id($model, new ObjectID((string) $result->getInsertedId()));
        $this->changesCollector->register($model, $values);

        return $result;
    }
471000
public function insert(array &$a, array $options = [])
    {
        if ($this->eventManager->hasListeners(Events::preInsert)) {
            $eventArgs = new EventArgs($this, $a, $options);
            $this->eventManager->dispatchEvent(Events::preInsert, $eventArgs);
            $a = $eventArgs->getData();
            $options = $eventArgs->getOptions();
        }

        $result = $this->doInsert($a, $options);

        if ($this->eventManager->hasListeners(Events::postInsert)) {
            $eventArgs = new MutableEventArgs($this, $result);
            $this->eventManager->dispatchEvent(Events::postInsert, $eventArgs);
            $result = $eventArgs->getData();
        }
        return $result;
    }
849958
public function insertOne($document, $options = [])
    {
        $objectDatas = $this->hydrator->unhydrate($document);

        $stream = $objectDatas["stream"];
        unset($objectDatas["stream"]);

        if (!isset($objectDatas["filename"])) {
            $filename = stream_get_meta_data($stream)["uri"];
        } else {
            $filename = $objectDatas["filename"];
        }

        unset($objectDatas["filename"]);

        $id = $this->bucket->uploadFromStream($filename, $stream, $objectDatas);
        $data['_id'] = $id;

        $data["stream"] = $this->bucket->openDownloadStream($data['_id']);
        $this->hydrator->hydrate($document, $data, true);

        $this->documentManager->setObjectState($document, ObjectManager::OBJ_MANAGED);
        $this->cacheObject($document);

        return true;
    }
527311
protected function _createNewContent($options = array()) {

        /**
         * Import variables into the current symbol table from an array
         */
        extract($options);

        /**
         * Create new entity
         */
        $contentTable = TableRegistry::get('CmsContent');
        $content = $contentTable->newEntity();
        $content->parent = isset($parent) ? intval($parent) : 0;
        $content->name = $this->_randomString();
        $content->content_title = isset($content_title) ? h($content_title) : '';
        $content->content_description = isset($content_description) ? h($content_description) : '';
        $content->content_excerpt = isset($content_excerpt) ? h($content_excerpt) : '';
        $content->content_status = isset($content_status) ? trim($content_status) : 'draft';
        $content->content_type = isset($content_type) ? trim($content_type) : 'page';
        $content->content_path = isset($content_path) ? trim($content_path) : '';
        $content->menu_order = isset($menu_order) ? trim($menu_order) : 0;
        $content->publish_start = isset($publish_start) ? trim($publish_start) : date('Y-m-d H:i:s');
        $content->publish_end = isset($publish_end) ? trim($publish_end) : '0000-00-00 00:00:00';
        $content->author_id = isset($author_id) ? intval($author_id) : 1;
        $content->created = date('Y-m-d H:i:s');
        $content->created_user = isset($created_user) ? intval($created_user) : 1;
        $content->modified = date('Y-m-d H:i:s');
        $content->modified_user = isset($modified_user) ? intval($modified_user) : 1;

        /**
         * Save and set name of Content
         */
        if ($contentTable->save($content)) :
            $content->name = (!isset($name)) ? $content->id : $this->_permittedContentName($content->id, $name);
            if ($contentTable->save($content))
                return $content->id;
        endif;

        /**
         * If not save Content return FALSE
         */
        return FALSE;
    }
1030921
public function saveAll($dataSet, $replace = true)
    {
        $result = [];

        $db = $this->db(false);
        $db->startTrans();

        try {
            $pk = $this->getPk();

            if (is_string($pk) && $replace) {
                $auto = true;
            }

            foreach ($dataSet as $key => $data) {
                if (!empty($auto) && isset($data[$pk])) {
                    $result[$key] = self::update($data, [], $this->field);
                } else {
                    $result[$key] = self::create($data, $this->field, $this->replace);
                }
            }

            $db->commit();

            return $this->toCollection($result);
        } catch (\Exception $e) {
            $db->rollback();
            throw $e;
        }
    }
20452
protected function mergeDataSources($dataset = null)
    {
        $sources = $this->dataSources();
        foreach ($sources as $sourceIdent) {
            $filter = $this->dataSourceFilter($sourceIdent);
            $getter = $this->camelize('data_from_'.$sourceIdent);
            $method = [ $this, $getter ];

            if (is_callable($method)) {
                $data = call_user_func($method);

                if ($data) {
                    if ($filter && $dataset) {
                        $data = call_user_func($filter, $data, $dataset);
                    }

                    parent::setData($data);
                }
            }
        }

        return $this;
    }
507581
public function insert(array $data, $options = [], $replace = false)
    {
        // 分析并处理数据
        $data = $this->parseData($data, $options);
        if (empty($data)) {
            return 0;
        }
        $fields = array_keys($data);
        $values = array_values($data);

        $sql = str_replace(
            ['%INSERT%', '%TABLE%', '%FIELD%', '%DATA%', '%COMMENT%'],
            [
                $replace ? 'REPLACE' : 'INSERT',
                $this->parseTable($options['table'], $options),
                implode(' , ', $fields),
                implode(' , ', $values),
                $this->parseComment($options['comment']),
            ], $this->insertSql);

        return $sql;
    }
884259
public function insertMany(array $models, array $options = [])
    {
        $data = [];
        foreach ($models as $key => $model) {
            $data[$key] = get_values($model, false);
        }

        $result = $this->collection->insertMany($data, $options);
        if (false == $result->isAcknowledged()) {
            throw new \LogicException('Operation is not acknowledged');
        }

        foreach ($result->getInsertedIds() as $key => $modelId) {
            $this->hydrator->hydrate($data[$key], $models[$key]);
            set_object_id($models[$key], $modelId);

            $this->changesCollector->register($models[$key], $data[$key]);
        }

        return $result;
    }
27849
public static function split(Dataset $dataset) : self
    {
        $column = rand(0, $dataset->numColumns() - 1);

        $sample = $dataset[rand(0, count($dataset) - 1)];

        $value = $sample[$column];

        $groups = $dataset->partition($column, $value);

        return new self($column, $value, $groups);
    }
104021
public static function stack(array $datasets) : self
    {
        $samples = $labels = [];

        foreach ($datasets as $dataset) {
            if (!$dataset instanceof self) {
                throw new InvalidArgumentException('Dataset must be'
                    . ' an instance of Labeled, ' . get_class($dataset)
                    . ' given.');
            }

            $samples = array_merge($samples, $dataset->samples());
            $labels = array_merge($labels, $dataset->labels());
        }

        return self::quick($samples, $labels);
    }
989033
private function create($options = [])
	{
		$this->before_create();

		$statement = 'INSERT INTO ' . static::$table_name . ' (';

		if (isset($options['include_primary_key'])) {
			$statement .= implode(', ', $this->_keys(false, true)) . ') VALUES (';
			$values = $this->_values(false);
		} else {
			$statement .= implode(', ', $this->_keys(true, true)) . ') VALUES (';
			$values = $this->_values();
		}

		$value_set = [];

		foreach ($values as $value) {
			$value_set[] = '?';
		}

		$statement .= implode(', ', $value_set) . ')';

		$result = $this->db->query($statement, $values, 'last_id');

		/* NOTE: PDO's lastInsertId does not work with PostgreSQL unless we supply
		 *       the sequence name. This is a hack to circumvent this.
		 *
		 *       Also: it would be nice to handle this in the DB wrapper, but
		 *       unfortunately we don't have any information about the query or table
		 *       there, so the hack has to stay here for now.
		 */
		if ('pgsql' === $this->db->type()) {
			$result = $this->db->lastInsertId(static::$table_name . '_id_seq');
		}

		$this->stored = true;
		$this->updated = false;
		$this->id = $result;

		$this->after_create();

		return true;
	}