--[[----------------------------------------------------------------------------

RxTestAspectRatio.lua

--------------------------------------------------------------------------------

ADOBE CONFIDENTIAL
------------------
Copyright 2015 Adobe Systems Incorporated
All Rights Reserved.

NOTICE: All information contained herein is, and remain the property of
Adobe Systems Incorporated and its suppliers, if any. The intellectual and
technical concepts contained herein are proprietary to Adobe Systems
Incorporated and its suppliers and may be covered by U.S. and Foreign
Patents, patents in process, and are protected by trade secret or copyright
law.  Dissemination of this information or reproduction of this material is
strictly forbidden unless prior written permission is obtained from
Adobe Systems Incorporated.

------------------------------------------------------------------------------]]

	-- WF.sqlite.index_store
local WFRxIndexStoreSchema = require 'WFRxIndexStoreSchema'


--============================================================================--

local indexStoreSchema = WFRxIndexStoreSchema()
indexStoreSchema:addDatabaseInitRoutine( require 'WFSQLiteSetLogicImp'.registerSetModule )

--------------------------------------------------------------------------------

indexStoreSchema:addTable {

	sql = [[CREATE TABLE aspectRatio( aspectRatio )]],

	relevantTypeSubtypeTuples = {
		{ '*', '*' }
	},

	map = function( docValue, emit )

		local width = docValue.width
		local height = docValue.height

		if type( width ) == 'number' and width > 0
			and type( height ) == 'number' and height > 0
		then
			emit( height / width )
		end
	
	end,

	hasOneRowPerDocument = true,

	useLocalId = true,

}

--------------------------------------------------------------------------------

indexStoreSchema:addTable {

	sql = [[CREATE TABLE keyword( name )]],

	relevantTypeSubtypeTuples = {
		{ '*', '*' }
	},

	map = function( docValue, emit )

		local keyword = docValue.keyword
		
		if keyword == "< generate error >" then
			error( "throwing error for testing" )
		end

		if type( keyword ) == 'string' then
			emit( keyword )
		end
	
	end,

}

--------------------------------------------------------------------------------

indexStoreSchema:addTable {

	sql = [[CREATE TABLE bad_indexing( name )]],
	
	relevantTypeSubtypeTuples = {
		{ '*', '*' }
	},
	
	hasOneRowPerDocument = true,
	
	map = function( docValue, emit )

		if docValue.type ~= 'badIndexing' then 
			return 
		end

		emit( 'foo' )
		emit( 'foo' )
	
	end,

}

--------------------------------------------------------------------------------

local mapFnCounter = 0

indexStoreSchema:addTable {
	
	sql = [[CREATE TABLE doc_value_filter( count )]],
	
	useLocalId = true,
	
	hasOneRowPerDocument = true,
	
	relevantTypeSubtypeTuples = {
		{ '*', '*' }
	},
	
	docValueFilter = function( docValue )
		return docValue.type == 'ok_docvalue_type'
	end,
	
	map = function( docValue, emit )
		mapFnCounter = mapFnCounter + 1
		emit( mapFnCounter )
	end,
}

--------------------------------------------------------------------------------

local typeAndSubtypMapFnCounter = 0

indexStoreSchema:addTable {
	
	sql = [[CREATE TABLE asset_only_type_subtype_filter( count )]],
	
	useLocalId = true,
	
	hasOneRowPerDocument = true,
	
	relevantTypeSubtypeTuples = {
		{ 'asset', '*' }
	},
	
	map = function( docValue, emit )
		typeAndSubtypMapFnCounter = typeAndSubtypMapFnCounter + 1
		emit( typeAndSubtypMapFnCounter )
	end,
}

--------------------------------------------------------------------------------

indexStoreSchema:addTable {

	sql = [[CREATE TABLE space_docs( name )]],

	relevantTypeSubtypeTuples = {
		{ '*', '*' }
	},

	map = function( docValue, emit )

		local keyword = docValue.keyword
		
		if type( keyword ) == 'string' then
			emit( keyword )
		end
	
	end,
	
	useLocalId = true,
	
	includeDocumentsFromSource = WFRxIndexStoreSchema.sourceSpaceFeedDocuments,
	
}

--------------------------------------------------------------------------------

indexStoreSchema:addTable {

	sql = [[CREATE TABLE catalog_docs( name )]],

	relevantTypeSubtypeTuples = {
		{ '*', '*' }
	},

	map = function( docValue, emit )

		local keyword = docValue.keyword

		if type( keyword ) == 'string' then
			emit( keyword )
		end
	
	end,
	
	useLocalId = true,
	
	includeDocumentsFromSource = WFRxIndexStoreSchema.sourceCatalogDocuments,
	
}

--------------------------------------------------------------------------------

indexStoreSchema:addTable {

	sql = [[CREATE TABLE space_and_catalog_docs( name )]],

	relevantTypeSubtypeTuples = {
		{ '*', '*' }
	},

	map = function( docValue, emit )

		local keyword = docValue.keyword
		
		if type( keyword ) == 'string' then
			emit( keyword )
		end
	
	end,
	
	useLocalId = true,
	
	includeDocumentsFromSource = WFRxIndexStoreSchema.sourceAllDocuments,
	
}

--------------------------------------------------------------------------------

indexStoreSchema:addTable {

	sql = [[CREATE TABLE simple_datatypes( string, number, bool, nil )]],

	relevantTypeSubtypeTuples = {
		{ '*', '*' }
	},

	map = function( docValue, emit )

		emit( docValue.astring, docValue.anumber, docValue.abool, docValue.nonexistent )
	
	end,
	
	useLocalId = true,
	
	includeDocumentsFromSource = WFRxIndexStoreSchema.sourceAllDocuments,
	
}

--------------------------------------------------------------------------------

return {
	indexStoreSchema = indexStoreSchema,
}
