# -*- mode: python -*-

Import("env")

env.SConscript(
    dirs=[
        'catalog',
        'client',
        'commands',
        'query',
    ],
)

# Functionality for initializing global sharding state
env.Library(
    target='sharding_initialization',
    source=[
        'sharding_initialization.cpp',
    ],
    LIBDEPS=[
        '$BUILD_DIR/mongo/db/repl/replication_executor', # TODO: change to thread pool executor
        '$BUILD_DIR/mongo/executor/network_interface_factory',
        '$BUILD_DIR/mongo/s/catalog/legacy/catalog_manager_legacy',
        '$BUILD_DIR/mongo/s/catalog/replset/catalog_manager_replica_set',
    ]
)

# Functionality shared between mongod and mongos
env.Library(
    target='common',
    source=[
        'chunk_diff.cpp',
    ],
    LIBDEPS=[
        'catalog/catalog_types',
    ]
)

env.Library(
    target='shard_util',
    source=[
        'shard_util.cpp',
    ],
    LIBDEPS=[
        'client/sharding_client',
    ]
)

env.CppUnitTest(
    target='chunk_version_test',
    source=[
        'chunk_version_test.cpp',
    ],
    LIBDEPS=[
        '$BUILD_DIR/mongo/db/common',
    ]
)

env.CppUnitTest(
    target='chunk_diff_test',
    source=[
        'chunk_diff_test.cpp',
    ],
    LIBDEPS=[
        'common',
    ]
)

#
# Write Operations
#

# Types for write operations
# TODO: Push upward into shared types library between mongos/mongod
env.Library(
    target='batch_write_types',
    source=[
        'write_ops/batched_command_request.cpp',
        'write_ops/batched_command_response.cpp',
        'write_ops/batched_delete_request.cpp',
        'write_ops/batched_delete_document.cpp',
        'write_ops/batched_insert_request.cpp',
        'write_ops/batched_request_metadata.cpp',
        'write_ops/batched_update_request.cpp',
        'write_ops/batched_update_document.cpp',
        'write_ops/batched_upsert_detail.cpp',
        'write_ops/wc_error_detail.cpp',
        'write_ops/write_error_detail.cpp',
    ],
    LIBDEPS=[
        '$BUILD_DIR/mongo/base/base',
        '$BUILD_DIR/mongo/bson/bson',
    ],
)

env.CppUnitTest(
    target='batch_write_types_test',
    source=[
        'write_ops/batched_command_request_test.cpp',
        'write_ops/batched_command_response_test.cpp',
        'write_ops/batched_delete_request_test.cpp',
        'write_ops/batched_insert_request_test.cpp',
        'write_ops/batched_request_metadata_test.cpp',
        'write_ops/batched_update_request_test.cpp',
    ],
    LIBDEPS=[
        'batch_write_types',
        '$BUILD_DIR/mongo/db/common',
    ]
)

#
# State and execution of operations across multiple hosts
#
# This functionality is self-contained and independent of any network or system-level
# code.
#
env.Library(
    target='cluster_ops',
    source=[
        'write_ops/write_op.cpp',
        'write_ops/batch_write_op.cpp',
        'write_ops/batch_write_exec.cpp',
    ],
    LIBDEPS=[
        '$BUILD_DIR/mongo/bson/bson',
        '$BUILD_DIR/mongo/client/clientdriver',
        'batch_write_types',
        '$BUILD_DIR/mongo/util/concurrency/synchronization'
    ],
)

env.CppUnitTest(
    target='cluster_ops_test',
    source=[
        'write_ops/write_op_test.cpp',
        'write_ops/batch_write_op_test.cpp',
        'write_ops/batch_write_exec_test.cpp',
    ],
    LIBDEPS=[
        'cluster_ops',
        '$BUILD_DIR/mongo/db/common',
        '$BUILD_DIR/mongo/db/range_arithmetic',
    ]
)

# Upconvert/Downconvert write functionality for mongos
env.Library(
    target='cluster_write_op_conversion',
    source=[
        'write_ops/batch_upconvert.cpp',
        'write_ops/batch_downconvert.cpp',
    ],
    LIBDEPS=[
        '$BUILD_DIR/mongo/bson/bson',
        '$BUILD_DIR/mongo/db/common', # for Message
        '$BUILD_DIR/mongo/db/lasterror',
        'cluster_ops',
    ],
)

env.CppUnitTest(
    target='cluster_write_op_conversion_test',
    source=[
        'write_ops/batch_upconvert_test.cpp',
        'write_ops/batch_downconvert_test.cpp',
    ],
    LIBDEPS=[
        'cluster_ops',
        'cluster_write_op_conversion',
        '$BUILD_DIR/mongo/db/common',
        '$BUILD_DIR/mongo/client/clientdriver',
    ]
)

#
# Implementations of components to perform cluster operations in mongos
#
# This is the glue code implementing the interfaces required by cluster ops
# in particular environments.
#
env.Library(
    target='cluster_ops_impl',
    source=[
        'chunk_manager_targeter.cpp',
        'cluster_explain.cpp',
        'cluster_write.cpp',
        'dbclient_shard_resolver.cpp',
    ],
    LIBDEPS=[
        '$BUILD_DIR/mongo/bson/bson',
        'cluster_ops',
        'cluster_write_op_conversion',
        'client/sharding_client',
    ],
)

env.CppUnitTest(
    target='chunk_manager_targeter_test',
    source=[
        'chunk_manager_targeter_test.cpp',
    ],
    LIBDEPS=[
        'coreshard',
        'mongoscore',
        '$BUILD_DIR/mongo/db/coredb',
        '$BUILD_DIR/mongo/db/auth/authorization_manager_mock_init',
    ]
)

# This library contains sharding functionality used by both mongod and mongos. Certain tests,
# which exercise this functionality also link against it.
env.Library(
    target='coreshard',
    source=[
        # This is only here temporarily for auto-split logic in chunk.cpp.
        'balancer_policy.cpp',
        'chunk.cpp',
        'chunk_manager.cpp',
        'config.cpp',
        'grid.cpp',
        'shard_key_pattern.cpp',
        'version_manager.cpp',
    ],
    LIBDEPS=[
        'catalog/catalog_manager',
        'catalog/catalog_types',
        'client/sharding_client',
        'cluster_ops_impl',
        'common',
        'shard_util',
    ]
)

# This library is only used by the mongos execuable and any tests which require mongos runtime
# objects, such as the request processing pipeline or the balancer.
env.Library(
    target='mongoscore',
    source=[
        'balance.cpp',
        'cluster_last_error_info.cpp',
        'cursors.cpp',
        'request.cpp',
        's_only.cpp',
        'strategy.cpp',
        'version_mongos.cpp',
    ],
    LIBDEPS=[
        '$BUILD_DIR/mongo/db/auth/authmongos',
        '$BUILD_DIR/mongo/client/parallel',
        '$BUILD_DIR/mongo/db/fts/ftsmongos',
        '$BUILD_DIR/mongo/db/stats/counters',
        '$BUILD_DIR/mongo/db/query/explain_common',
        '$BUILD_DIR/mongo/db/query/lite_parsed_query',
        '$BUILD_DIR/mongo/s/query/cluster_query',
        '$BUILD_DIR/mongo/util/concurrency/task',
        'cluster_ops',
        'cluster_write_op_conversion',
    ]
)

env.CppUnitTest(
    target='mongoscore_test',
    source=[
        'balancer_policy_tests.cpp',
        'shard_key_pattern_test.cpp',
    ],
    LIBDEPS=[
        '$BUILD_DIR/mongo/db/coredb',
        "$BUILD_DIR/mongo/db/auth/authorization_manager_mock_init",
        'coreshard',
        'mongoscore',
    ]
)

env.Library(
    target='serveronly',
    source=[
        "d_merge.cpp",
        "d_migrate.cpp",
        "d_split.cpp",
        "d_state.cpp",
        "distlock_test.cpp",
    ],
    LIBDEPS=[
        "$BUILD_DIR/mongo/executor/network_interface_factory"
    ],
)
