/**
* Copyright (C) 2015 MongoDB Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the GNU Affero General Public License in all respects for
* all of the code used other than as permitted herein. If you modify file(s)
* with this exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version. If you delete this
* exception statement from all source files in the program, then also delete
* it in the license file.
*/
#pragma once
#include "mongo/base/disallow_copying.h"
#include "mongo/base/status.h"
#include "mongo/base/status_with.h"
#include "mongo/base/string_data.h"
#include "mongo/bson/bsonobj.h"
#include "mongo/bson/timestamp.h"
#include "mongo/db/namespace_string.h"
#include "mongo/db/repl/storage_interface.h"
#include "mongo/stdx/mutex.h"
namespace mongo {
namespace repl {
struct CollectionMockStats {
bool initCalled = false;
int insertCount = 0;
bool commitCalled = false;
};
class CollectionBulkLoaderMock : public CollectionBulkLoader {
MONGO_DISALLOW_COPYING(CollectionBulkLoaderMock);
public:
CollectionBulkLoaderMock(CollectionMockStats* collStats) : stats(collStats){};
virtual ~CollectionBulkLoaderMock() = default;
virtual Status init(const std::vector& secondaryIndexSpecs) override;
virtual Status insertDocuments(const std::vector::const_iterator begin,
const std::vector::const_iterator end) override;
virtual Status commit() override;
std::string toString() const override {
return toBSON().toString();
};
BSONObj toBSON() const override {
return BSONObj();
};
CollectionMockStats* stats;
// Override functions.
stdx::function::const_iterator begin,
const std::vector::const_iterator end)>
insertDocsFn = [](const std::vector::const_iterator,
const std::vector::const_iterator) { return Status::OK(); };
stdx::function abortFn = []() { return Status::OK(); };
stdx::function commitFn = []() { return Status::OK(); };
};
class StorageInterfaceMock : public StorageInterface {
MONGO_DISALLOW_COPYING(StorageInterfaceMock);
public:
// Used for testing.
using CreateCollectionForBulkFn =
stdx::function>(
const NamespaceString& nss,
const CollectionOptions& options,
const BSONObj idIndexSpec,
const std::vector& secondaryIndexSpecs)>;
using InsertDocumentFn = stdx::function;
using InsertDocumentsFn = stdx::function& docs)>;
using DropUserDatabasesFn = stdx::function;
using CreateOplogFn =
stdx::function;
using CreateCollectionFn = stdx::function;
using TruncateCollectionFn =
stdx::function;
using DropCollectionFn =
stdx::function;
using FindDocumentsFn =
stdx::function>(OperationContext* opCtx,
const NamespaceString& nss,
boost::optional indexName,
ScanDirection scanDirection,
const BSONObj& startKey,
BoundInclusion boundInclusion,
std::size_t limit)>;
using DeleteDocumentsFn =
stdx::function>(OperationContext* opCtx,
const NamespaceString& nss,
boost::optional indexName,
ScanDirection scanDirection,
const BSONObj& startKey,
BoundInclusion boundInclusion,
std::size_t limit)>;
using IsAdminDbValidFn = stdx::function;
using GetCollectionUUIDFn = stdx::function(
OperationContext* opCtx, const NamespaceString& nss)>;
StorageInterfaceMock() = default;
StatusWith getRollbackID(OperationContext* opCtx) override;
StatusWith initializeRollbackID(OperationContext* opCtx) override;
StatusWith incrementRollbackID(OperationContext* opCtx) override;
StatusWith> createCollectionForBulkLoading(
const NamespaceString& nss,
const CollectionOptions& options,
const BSONObj idIndexSpec,
const std::vector& secondaryIndexSpecs) override {
return createCollectionForBulkFn(nss, options, idIndexSpec, secondaryIndexSpecs);
};
Status insertDocument(OperationContext* opCtx,
const NamespaceStringOrUUID& nsOrUUID,
const TimestampedBSONObj& doc,
long long term) override {
return insertDocumentFn(opCtx, nsOrUUID, doc, term);
};
Status insertDocuments(OperationContext* opCtx,
const NamespaceStringOrUUID& nsOrUUID,
const std::vector& docs) override {
return insertDocumentsFn(opCtx, nsOrUUID, docs);
}
Status dropReplicatedDatabases(OperationContext* opCtx) override {
return dropUserDBsFn(opCtx);
};
Status createOplog(OperationContext* opCtx, const NamespaceString& nss) override {
return createOplogFn(opCtx, nss);
};
StatusWith getOplogMaxSize(OperationContext* opCtx,
const NamespaceString& nss) override {
return 1024 * 1024 * 1024;
}
Status createCollection(OperationContext* opCtx,
const NamespaceString& nss,
const CollectionOptions& options) override {
return createCollFn(opCtx, nss, options);
}
Status dropCollection(OperationContext* opCtx, const NamespaceString& nss) override {
return dropCollFn(opCtx, nss);
};
Status truncateCollection(OperationContext* opCtx, const NamespaceString& nss) override {
return truncateCollFn(opCtx, nss);
}
Status renameCollection(OperationContext* opCtx,
const NamespaceString& fromNS,
const NamespaceString& toNS,
bool stayTemp) override {
return Status{ErrorCodes::IllegalOperation, "renameCollection not implemented."};
}
Status setIndexIsMultikey(OperationContext* opCtx,
const NamespaceString& nss,
const std::string& indexName,
const MultikeyPaths& paths,
Timestamp ts) override {
return Status{ErrorCodes::IllegalOperation, "setIndexIsMultikey not implemented."};
}
StatusWith> findDocuments(OperationContext* opCtx,
const NamespaceString& nss,
boost::optional indexName,
ScanDirection scanDirection,
const BSONObj& startKey,
BoundInclusion boundInclusion,
std::size_t limit) override {
return findDocumentsFn(
opCtx, nss, indexName, scanDirection, startKey, boundInclusion, limit);
}
StatusWith> deleteDocuments(OperationContext* opCtx,
const NamespaceString& nss,
boost::optional indexName,
ScanDirection scanDirection,
const BSONObj& startKey,
BoundInclusion boundInclusion,
std::size_t limit) override {
return deleteDocumentsFn(
opCtx, nss, indexName, scanDirection, startKey, boundInclusion, limit);
}
StatusWith findSingleton(OperationContext* opCtx,
const NamespaceString& nss) override {
return Status{ErrorCodes::IllegalOperation, "findSingleton not implemented."};
}
Status putSingleton(OperationContext* opCtx,
const NamespaceString& nss,
const TimestampedBSONObj& update) override {
return Status{ErrorCodes::IllegalOperation, "putSingleton not implemented."};
}
Status updateSingleton(OperationContext* opCtx,
const NamespaceString& nss,
const BSONObj& query,
const TimestampedBSONObj& update) override {
return Status{ErrorCodes::IllegalOperation, "updateSingleton not implemented."};
}
StatusWith findById(OperationContext* opCtx,
const NamespaceStringOrUUID&,
const BSONElement& idKey) override {
return Status{ErrorCodes::IllegalOperation, "findById not implemented."};
}
StatusWith deleteById(OperationContext* opCtx,
const NamespaceStringOrUUID&,
const BSONElement& idKey) override {
return Status{ErrorCodes::IllegalOperation, "deleteById not implemented."};
}
Status upsertById(OperationContext* opCtx,
const NamespaceStringOrUUID& nsOrUUID,
const BSONElement& idKey,
const BSONObj& update) override {
return Status{ErrorCodes::IllegalOperation, "upsertById not implemented."};
}
Status deleteByFilter(OperationContext* opCtx,
const NamespaceString& nss,
const BSONObj& filter) override {
return Status{ErrorCodes::IllegalOperation, "deleteByFilter not implemented."};
}
StatusWith getCollectionSize(
OperationContext* opCtx, const NamespaceString& nss) override {
return 0;
}
StatusWith getCollectionCount(
OperationContext* opCtx, const NamespaceStringOrUUID& nsOrUUID) override {
return 0;
}
Status setCollectionCount(OperationContext* opCtx,
const NamespaceStringOrUUID& nsOrUUID,
long long newCount) override {
return Status{ErrorCodes::IllegalOperation, "setCollectionCount not implemented."};
}
StatusWith getCollectionUUID(OperationContext* opCtx,
const NamespaceString& nss) override {
return getCollectionUUIDFn(opCtx, nss);
}
void setStableTimestamp(ServiceContext* serviceCtx, Timestamp snapshotName) override;
void setInitialDataTimestamp(ServiceContext* serviceCtx, Timestamp snapshotName) override;
Timestamp getStableTimestamp() const;
Timestamp getInitialDataTimestamp() const;
StatusWith recoverToStableTimestamp(OperationContext* opCtx) override {
return Status{ErrorCodes::IllegalOperation, "recoverToStableTimestamp not implemented."};
}
bool supportsRecoverToStableTimestamp(ServiceContext* serviceCtx) const override {
return false;
}
boost::optional getRecoveryTimestamp(ServiceContext* serviceCtx) const override {
return boost::none;
}
Status isAdminDbValid(OperationContext* opCtx) override {
return isAdminDbValidFn(opCtx);
};
void waitForAllEarlierOplogWritesToBeVisible(OperationContext* opCtx) override {
return;
}
void oplogDiskLocRegister(OperationContext* opCtx,
const Timestamp& ts,
bool orderedCommit) override {
return;
}
boost::optional getLastStableCheckpointTimestamp(
ServiceContext* serviceCtx) const override {
return boost::none;
}
// Testing functions.
CreateCollectionForBulkFn createCollectionForBulkFn =
[](const NamespaceString& nss,
const CollectionOptions& options,
const BSONObj idIndexSpec,
const std::vector&
secondaryIndexSpecs) -> StatusWith> {
return Status{ErrorCodes::IllegalOperation, "CreateCollectionForBulkFn not implemented."};
};
InsertDocumentFn insertDocumentFn = [](OperationContext* opCtx,
const NamespaceStringOrUUID& nsOrUUID,
const TimestampedBSONObj& doc,
long long term) {
return Status{ErrorCodes::IllegalOperation, "InsertDocumentFn not implemented."};
};
InsertDocumentsFn insertDocumentsFn = [](OperationContext* opCtx,
const NamespaceStringOrUUID& nsOrUUID,
const std::vector& docs) {
return Status{ErrorCodes::IllegalOperation, "InsertDocumentsFn not implemented."};
};
DropUserDatabasesFn dropUserDBsFn = [](OperationContext* opCtx) {
return Status{ErrorCodes::IllegalOperation, "DropUserDatabasesFn not implemented."};
};
CreateOplogFn createOplogFn = [](OperationContext* opCtx, const NamespaceString& nss) {
return Status{ErrorCodes::IllegalOperation, "CreateOplogFn not implemented."};
};
CreateCollectionFn createCollFn =
[](OperationContext* opCtx, const NamespaceString& nss, const CollectionOptions& options) {
return Status{ErrorCodes::IllegalOperation, "CreateCollectionFn not implemented."};
};
TruncateCollectionFn truncateCollFn = [](OperationContext* opCtx, const NamespaceString& nss) {
return Status{ErrorCodes::IllegalOperation, "TruncateCollectionFn not implemented."};
};
DropCollectionFn dropCollFn = [](OperationContext* opCtx, const NamespaceString& nss) {
return Status{ErrorCodes::IllegalOperation, "DropCollectionFn not implemented."};
};
FindDocumentsFn findDocumentsFn = [](OperationContext* opCtx,
const NamespaceString& nss,
boost::optional indexName,
ScanDirection scanDirection,
const BSONObj& startKey,
BoundInclusion boundInclusion,
std::size_t limit) {
return Status{ErrorCodes::IllegalOperation, "FindOneFn not implemented."};
};
DeleteDocumentsFn deleteDocumentsFn = [](OperationContext* opCtx,
const NamespaceString& nss,
boost::optional indexName,
ScanDirection scanDirection,
const BSONObj& startKey,
BoundInclusion boundInclusion,
std::size_t limit) {
return Status{ErrorCodes::IllegalOperation, "DeleteOneFn not implemented."};
};
IsAdminDbValidFn isAdminDbValidFn = [](OperationContext*) {
return Status{ErrorCodes::IllegalOperation, "IsAdminDbValidFn not implemented."};
};
GetCollectionUUIDFn getCollectionUUIDFn = [](
OperationContext* opCtx, const NamespaceString& nss) -> StatusWith {
return Status{ErrorCodes::IllegalOperation, "GetCollectionUUIDFn not implemented."};
};
private:
mutable stdx::mutex _mutex;
int _rbid;
bool _rbidInitialized = false;
Timestamp _stableTimestamp = Timestamp::min();
Timestamp _initialDataTimestamp = Timestamp::min();
OptionalCollectionUUID _uuid;
bool _schemaUpgraded;
};
} // namespace repl
} // namespace mongo