<commit_before>/* ***** BEGIN LICENSE BLOCK *****
 *
 * This file is part of Weave.
 *
 * The Initial Developer of Weave is the Institute for Visualization
 * and Perception Research at the University of Massachusetts Lowell.
 * Portions created by the Initial Developer are Copyright (C) 2008-2015
 * the Initial Developer. All Rights Reserved.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * ***** END LICENSE BLOCK ***** */

namespace weavejs.data
{
	import WeaveAPI = weavejs.WeaveAPI;
	import ILinkableHashMap = weavejs.api.core.ILinkableHashMap;
	import ColumnMetadata = weavejs.api.data.ColumnMetadata;
	import DataType = weavejs.api.data.DataType;
	import IAttributeColumn = weavejs.api.data.IAttributeColumn;
	import IAttributeColumnCache = weavejs.api.data.IAttributeColumnCache;
	import IBaseColumn = weavejs.api.data.IBaseColumn;
	import IDataSource = weavejs.api.data.IDataSource;
	import IQualifiedKey = weavejs.api.data.IQualifiedKey;
	import DateColumn = weavejs.data.column.DateColumn;
	import GeometryColumn = weavejs.data.column.GeometryColumn;
	import NumberColumn = weavejs.data.column.NumberColumn;
	import StringColumn = weavejs.data.column.StringColumn;
	import QKeyManager = weavejs.data.key.QKeyManager;
	import CachedDataSource = weavejs.data.source.CachedDataSource;
	import GeneralizedGeometry = weavejs.geom.GeneralizedGeometry;
	import Dictionary2D = weavejs.util.Dictionary2D;
	import WeavePromise = weavejs.util.WeavePromise;

	export declare type CachedColumnData = [string, string, {[property:string]:string}, string[], any[]][];
	export class AttributeColumnCache implements IAttributeColumnCache
	{
		public getColumn(dataSource:IDataSource, metadata:{[key:string]:string}):IAttributeColumn
		{
			// null means no column
			if (dataSource == null || metadata == null)
				return null;

			// Get the column pointer associated with the hash value.
			var hashCode = Weave.stringify(metadata);
			var column = this.d2d_dataSource_metadataHash_column.get(dataSource, hashCode);
			if (!column)
			{
				// if this is the first time we've seen this data source, add dispose callback
				if (!this.d2d_dataSource_metadataHash_column.map.has(dataSource))
					Weave.getCallbacks(dataSource).addDisposeCallback(this, function():void {
						this.d2d_dataSource_metadataHash_column.map['delete'](dataSource);
					});

				// If no column is associated with this hash value, request the
				// column from its data source and save the column pointer.
				column = dataSource.generateNewAttributeColumn(metadata);
				if (column)
					column.addDisposeCallback(this, function():void {
						this.d2d_dataSource_metadataHash_column.remove(dataSource, hashCode);
					});
				this.d2d_dataSource_metadataHash_column.set(dataSource, hashCode, column);
			}
			return column;
		}

		private d2d_dataSource_metadataHash_column = new Dictionary2D<IDataSource, string, IAttributeColumn>();

		// TEMPORARY SOLUTION for WeaveArchive to access this cache data
		public /* readonly */ map_root_saveCache:WeakMap<ILinkableHashMap, CachedColumnData> = new WeakMap();

		/**
		 * Creates a cache dump and modifies the session state so data sources are non-functional.
		 * @return A WeavePromise that returns a cache dump that can later be passed to restoreCache();
		 */
		public convertToCachedDataSources(root:ILinkableHashMap):WeavePromise<CachedColumnData>
		{
			var promise = new WeavePromise(root).setResult(root);
			var dispose = () => promise.dispose();
			var promiseThen = promise
				.then(function(root:ILinkableHashMap):ILinkableHashMap {
					// request data from every column
					var column:IAttributeColumn;
					var columns = Weave.getDescendants(root, IAttributeColumn);
					for (column of columns)
					{
						// simply requesting the keys will cause the data to be requested
						if (column.keys.length)
							column.getValueFromKey(column.keys[0]);
						// wait for the column to finish any async tasks
						promise.depend(column);
					}
					return root;
				})
				.then(this._convertToCachedDataSources);
			promiseThen.then(dispose, dispose);
			return promiseThen;
		}

		private _convertToCachedDataSources(root:ILinkableHashMap):CachedColumnData
		{
			//cache data from AttributeColumnCache
			var output:CachedColumnData = [];
			var dataSource:IDataSource;
			var dataSources:IDataSource[] = this.d2d_dataSource_metadataHash_column.primaryKeys();
			for (var dataSource of dataSources)
			{
				// skip local data sources
				if (dataSource.isLocal)
					continue;
<commit_msg>fix bound this error.<commit_after>/* ***** BEGIN LICENSE BLOCK *****
 *
 * This file is part of Weave.
 *
 * The Initial Developer of Weave is the Institute for Visualization
 * and Perception Research at the University of Massachusetts Lowell.
 * Portions created by the Initial Developer are Copyright (C) 2008-2015
 * the Initial Developer. All Rights Reserved.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * ***** END LICENSE BLOCK ***** */

namespace weavejs.data
{
	import WeaveAPI = weavejs.WeaveAPI;
	import ILinkableHashMap = weavejs.api.core.ILinkableHashMap;
	import ColumnMetadata = weavejs.api.data.ColumnMetadata;
	import DataType = weavejs.api.data.DataType;
	import IAttributeColumn = weavejs.api.data.IAttributeColumn;
	import IAttributeColumnCache = weavejs.api.data.IAttributeColumnCache;
	import IBaseColumn = weavejs.api.data.IBaseColumn;
	import IDataSource = weavejs.api.data.IDataSource;
	import IQualifiedKey = weavejs.api.data.IQualifiedKey;
	import DateColumn = weavejs.data.column.DateColumn;
	import GeometryColumn = weavejs.data.column.GeometryColumn;
	import NumberColumn = weavejs.data.column.NumberColumn;
	import StringColumn = weavejs.data.column.StringColumn;
	import QKeyManager = weavejs.data.key.QKeyManager;
	import CachedDataSource = weavejs.data.source.CachedDataSource;
	import GeneralizedGeometry = weavejs.geom.GeneralizedGeometry;
	import Dictionary2D = weavejs.util.Dictionary2D;
	import WeavePromise = weavejs.util.WeavePromise;

	export declare type CachedColumnData = [string, string, {[property:string]:string}, string[], any[]][];
	export class AttributeColumnCache implements IAttributeColumnCache
	{
		public getColumn(dataSource:IDataSource, metadata:{[key:string]:string}):IAttributeColumn
		{
			// null means no column
			if (dataSource == null || metadata == null)
				return null;

			// Get the column pointer associated with the hash value.
			var hashCode = Weave.stringify(metadata);
			var column = this.d2d_dataSource_metadataHash_column.get(dataSource, hashCode);
			if (!column)
			{
				// if this is the first time we've seen this data source, add dispose callback
				if (!this.d2d_dataSource_metadataHash_column.map.has(dataSource))
					Weave.getCallbacks(dataSource).addDisposeCallback(this, function():void {
						this.d2d_dataSource_metadataHash_column.map['delete'](dataSource);
					});

				// If no column is associated with this hash value, request the
				// column from its data source and save the column pointer.
				column = dataSource.generateNewAttributeColumn(metadata);
				if (column)
					column.addDisposeCallback(this, function():void {
						this.d2d_dataSource_metadataHash_column.remove(dataSource, hashCode);
					});
				this.d2d_dataSource_metadataHash_column.set(dataSource, hashCode, column);
			}
			return column;
		}

		private d2d_dataSource_metadataHash_column = new Dictionary2D<IDataSource, string, IAttributeColumn>();

		// TEMPORARY SOLUTION for WeaveArchive to access this cache data
		public /* readonly */ map_root_saveCache:WeakMap<ILinkableHashMap, CachedColumnData> = new WeakMap();

		/**
		 * Creates a cache dump and modifies the session state so data sources are non-functional.
		 * @return A WeavePromise that returns a cache dump that can later be passed to restoreCache();
		 */
		public convertToCachedDataSources(root:ILinkableHashMap):WeavePromise<CachedColumnData>
		{
			var promise = new WeavePromise(root).setResult(root);
			var dispose = () => promise.dispose();
			var promiseThen = promise
				.then(function(root:ILinkableHashMap):ILinkableHashMap {
					// request data from every column
					var column:IAttributeColumn;
					var columns = Weave.getDescendants(root, IAttributeColumn);
					for (column of columns)
					{
						// simply requesting the keys will cause the data to be requested
						if (column.keys.length)
							column.getValueFromKey(column.keys[0]);
						// wait for the column to finish any async tasks
						promise.depend(column);
					}
					return root;
				})
				.then((root:ILinkableHashMap) => this._convertToCachedDataSources(root));
			promiseThen.then(dispose, dispose);
			return promiseThen;
		}

		private _convertToCachedDataSources(root:ILinkableHashMap):CachedColumnData
		{
			//cache data from AttributeColumnCache
			var output:CachedColumnData = [];
			var dataSource:IDataSource;
			var dataSources:IDataSource[] = this.d2d_dataSource_metadataHash_column.primaryKeys();
			for (var dataSource of dataSources)
			{
				// skip local data sources
				if (dataSource.isLocal)
					continue;

{'fraction_non_alphanumeric': 0.09219031471866058, 'fraction_numerical': 0.004874430433400445, 'mean_word_length': 4.353374929098128, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5707279', 'n_tokens_mistral': 3168, 'n_tokens_neox': 2750, 'n_words': 746}