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

WFRxPlatformIndexStore.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2020 Adobe
All Rights Reserved.

NOTICE: All information contained herein is, and remains
the property of Adobe and its suppliers, if any. The intellectual
and technical concepts contained herein are proprietary to Adobe
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.

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

	-- Rx
local Rx = require 'Rx'

	-- WF.core.threading
local WFWorkerThread = require 'WFWorkerThread'

	-- WF.core.rxlua
local WFRxRunUntilDone = require 'WFRxRunUntilDone'

	-- WF.core.logging
local WFLogger = require 'WFLogger'

	-- WF.core.utils
local WFArguments = require 'WFArguments'
local WFCloseSupport = require 'WFCloseSupport'

--------------------------------------------------------------------------------
local log = WFLogger( "WFRxPlatformIndexStore" )
		--:setLevel 'debug'
		
local check = assert( WFArguments.check, "Cannot find WFArguments.check" )

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

local function dispatchToWorkerThread( self, selector, ... )
	if not self._workerThread then
		return
	end
	
	return self._workerThread:request( selector, ... )
end

--============================================================================--
--[[!
	\class WFRxPlatformIndexStore
	\brief Platform based read-only instance corresponding to a WFRxIndexStore.
--]]

local WFRxPlatformIndexStore = {

	_workerThread = false,
		-- the workerthread instance corresponding to imp
	
	WFCloseSupport.addCloseSupportMethods,

}

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

--[[!
	\brief Create a new read only platform specific index store.

	\param docStorePath (\c string) path to doc store to observe
	\param indexStoreSchema (\c WFRxIndexStoreSchema) index store schema
	
	The parameters should match the ones needed for WFRxIndexStoreImp.lua
--]]

function WFRxPlatformIndexStore:__init( docStorePath, indexSpecScript )

	check( 'WFRxPlatformIndexStore', '+string', '+string' )
	
	if not( docStorePath:sub( 1, 8 ) == ':memory:' or docStorePath:sub( 1, 6 ) == ':temp:' ) then
	
		-- Do not create platform based indexstore for temp and memory dbs.

		local initArgs = string.format( '%s=%s', indexSpecScript, docStorePath )
		self._workerThread = WFWorkerThread( 'WFRxPlatformIndexStoreImp.lua', initArgs )
		dispatchToWorkerThread( self, "init" )
			:tap( function()
				log:debugf("WFRxPlatformIndexStore init done" )
			end )
			:call( WFRxRunUntilDone )

	end

end

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

function WFRxPlatformIndexStore:close()
	
	if self._workerThread then
	
		dispatchToWorkerThread( self, "close" )
		self._workerThread:close()
		self._workerThread = nil
	end
end

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

return Rx.Class 'WFRxPlatformIndexStore'( WFRxPlatformIndexStore )
