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

NmbProfilesIndexDefinitions.lua

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

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

NOTICE: All information contained herein is, and remains 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 are protected by all applicable
intellectual property laws, including trade secret and copyright laws.
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 KSTableUtils = require 'KSTableUtils'

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

local NmbProfilesIndexDefinitions = {}

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

local tableDefinitions = {}
local indexDefinitions = {}

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

local sqlitePageSize = 4096

local useSchemaVersionList = false
	-- Starting soon, the catalog-based index store uses an explicit schema-version
	-- list for forced reindexing. The profiles-based index store will continue to
	-- use the older hash-based approach for forced reindexing; this is okay because
	-- profile reindexes are rare and do not trigger the house keeping dialog.
	
local indexStoreSchema = WFRxIndexStoreSchema( sqlitePageSize, useSchemaVersionList )

indexStoreSchema:addDatabaseInitRoutine( require 'WFSQLiteSetLogicImp'.registerSetModule )

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

tableDefinitions.profiles = {
	
	sql = [[CREATE TABLE profiles( digest, filename, href, name, profile_type, model, cameraProfileDigest )]],
	
	useLocalId = true,
	
	relevantTypeSubtypeTuples = {
		{ 'asset', 'lens_profile' },
		{ 'asset', 'camera_profile' },
	},
	
	map = function( docValue, emit )
	
		if docValue.subtype == 'lens_profile' or docValue.subtype == 'camera_profile' then
			
			local digest = docValue.resolvedDigest
			local filename = docValue.fileName
			local name = docValue.name
			local href = KSTableUtils.derefOpt( docValue, '_serverProvidedMetadata', 'links', '/rels/master', 'href' )
			local profileType = docValue.subtype
			local model = docValue.model
			local cameraProfileDigest = docValue.cameraProfileDigest
			
			if digest and filename and href then
				emit( digest, filename, href, name, profileType, model, cameraProfileDigest )
			end
		
		end
	
	end,
	
	hasOneRowPerDocument = true,
	
}

indexDefinitions.profile_idx = [[CREATE INDEX profile_idx ON profiles( digest )]]
indexDefinitions.profile_type_idx = [[CREATE INDEX profile_type_idx ON profiles( profile_type, name )]]
indexDefinitions.filename_type_idx = [[CREATE INDEX filename_idx ON profiles( filename )]]

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

for k, definition in pairs( tableDefinitions ) do
	indexStoreSchema:addTable( definition )
end

for k, definition in pairs( indexDefinitions ) do
	indexStoreSchema:addIndex( definition )
end

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

NmbProfilesIndexDefinitions.indexStoreSchema = indexStoreSchema

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

return NmbProfilesIndexDefinitions
