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

WFRxPlatformIndexStore.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2020 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.

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

	-- 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 )
