本文整理匯總了Python中pymongo.errors方法的典型用法代碼示例。如果您正苦於以下問題:Python pymongo.errors方法的具體用法?Python pymongo.errors怎麽用?Python pymongo.errors使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pymongo
的用法示例。
在下文中一共展示了pymongo.errors方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _check_write_command_response
# 需要導入模塊: import pymongo [as 別名]
# 或者: from pymongo import errors [as 別名]
def _check_write_command_response(results):
"""Backward compatibility helper for write command error handling.
"""
errors = [res for res in results
if "writeErrors" in res[1] or "writeConcernError" in res[1]]
if errors:
# If multiple batches had errors
# raise from the last batch.
offset, result = errors[-1]
# Prefer write errors over write concern errors
write_errors = result.get("writeErrors")
if write_errors:
# If the last batch had multiple errors only report
# the last error to emulate continue_on_error.
error = write_errors[-1]
error["index"] += offset
if error.get("code") == 11000:
raise DuplicateKeyError(error.get("errmsg"), 11000, error)
else:
error = result["writeConcernError"]
if "errInfo" in error and error["errInfo"].get('wtimeout'):
# Make sure we raise WTimeoutError
raise WTimeoutError(error.get("errmsg"),
error.get("code"), error)
raise OperationFailure(error.get("errmsg"), error.get("code"), error)
示例2: __init__
# 需要導入模塊: import pymongo [as 別名]
# 或者: from pymongo import errors [as 別名]
def __init__(self, **kwargs):
super().__init__(**kwargs)
from pymongo import MongoClient
from pymongo.errors import OperationFailure
self.database_uri = kwargs.get(
'database_uri', 'mongodb://localhost:27017/chatterbot-database'
)
# Use the default host and port
self.client = MongoClient(self.database_uri)
# Increase the sort buffer to 42M if possible
try:
self.client.admin.command({'setParameter': 1, 'internalQueryExecMaxBlockingSortBytes': 44040192})
except OperationFailure:
pass
# Specify the name of the database
self.database = self.client.get_database()
# The mongo collection of statement documents
self.statements = self.database['statements']
示例3: check_server_up
# 需要導入模塊: import pymongo [as 別名]
# 或者: from pymongo import errors [as 別名]
def check_server_up(self):
"""Test connection to the server."""
import pymongo
from pymongo.errors import AutoReconnect, ConnectionFailure
# Hostname must exist before continuing
# Some server class (e.g. Docker) will only allocate an IP after the
# container has started.
if not self.hostname:
return False
log.info("Connecting to Mongo at %s:%s" % (self.hostname, self.port))
try:
self.api = pymongo.MongoClient(self.hostname, self.port,
serverselectiontimeoutms=200)
self.api.list_database_names()
# Configure the client with default timeouts in case the server goes slow
self.api = pymongo.MongoClient(self.hostname, self.port)
return True
except (AutoReconnect, ConnectionFailure) as e:
pass
return False
示例4: create
# 需要導入模塊: import pymongo [as 別名]
# 或者: from pymongo import errors [as 別名]
def create(self):
"""Creates model in database."""
state = self.get_state()
state.pop("_id", None)
state["time"]["created"] = timeutils.current_unix_timestamp()
state["time"]["updated"] = state["time"]["created"]
state["update_marker"] = self.new_update_marker()
collection = self.collection()
insert_method = retryutils.mongo_retry()(collection.insert_one)
find_method = retryutils.mongo_retry()(collection.find_one)
try:
document = insert_method(state)
except pymongo.errors.DuplicateKeyError as exc:
raise exceptions.UniqueConstraintViolationError from exc
document = find_method({"_id": document.inserted_id})
self.set_state(document)
return self
示例5: library_exists
# 需要導入模塊: import pymongo [as 別名]
# 或者: from pymongo import errors [as 別名]
def library_exists(self, library):
"""
Check whether a given library exists.
Parameters
----------
library : `str`
The name of the library. e.g. 'library' or 'user.library'
Returns
-------
`bool`
True if the library with the given name already exists, False otherwise
"""
exists = False
try:
# This forces auth errors, and to fall back to the slower "list_collections"
ArcticLibraryBinding(self, library).get_library_type()
# This will obtain the library, if no exception thrown we have verified its existence
self.get_library(library)
exists = True
except OperationFailure:
exists = library in self.list_libraries()
except LibraryNotFoundException:
pass
return exists
示例6: _update_fw_pointers
# 需要導入模塊: import pymongo [as 別名]
# 或者: from pymongo import errors [as 別名]
def _update_fw_pointers(collection, symbol, version, previous_version, is_append, shas_to_add=None):
"""
This function will decide whether to update the version document with forward pointers to segments.
It detects cases where no prior writes/appends have been performed with FW pointers, and extracts the segment IDs.
It also sets the metadata which indicate the mode of operation at the time of the version creation.
"""
version[FW_POINTERS_CONFIG_KEY] = ARCTIC_FORWARD_POINTERS_CFG.name # get the str as enum is not BSON serializable
if ARCTIC_FORWARD_POINTERS_CFG is FwPointersCfg.DISABLED:
return
version_shas = set()
if is_append:
# Appends are tricky, as we extract the SHAs from the previous version (assuming it has FW pointers info)
prev_fw_cfg = get_fwptr_config(previous_version)
if prev_fw_cfg is FwPointersCfg.DISABLED.name:
version_shas.update(Binary(sha) for sha in collection.find(
{'symbol': symbol,
'parent': version_base_or_id(previous_version),
'segment': {'$lt': previous_version['up_to']}},
{'sha': 1}))
else:
version_shas.update(previous_version[FW_POINTERS_REFS_KEY])
# It is a write (we always get the all-inclusive set of SHAs), so no need to obtain previous SHAs
version_shas.update(shas_to_add)
# Verify here the number of seen segments vs expected ones
if len(version_shas) != version['segment_count']:
raise pymongo.errors.OperationFailure("Mismatched number of forward pointers to segments for {}: {} != {})"
"Is append: {}. Previous version: {}. "
"Gathered forward pointers segment shas: {}.".format(
symbol, len(version_shas), version['segment_count'], is_append, previous_version['_id'], version_shas))
version[FW_POINTERS_REFS_KEY] = list(version_shas)
示例7: _fw_pointers_convert_append_to_write
# 需要導入模塊: import pymongo [as 別名]
# 或者: from pymongo import errors [as 別名]
def _fw_pointers_convert_append_to_write(previous_version):
"""
This method decides whether to convert an append to a full write in order to avoid data integrity errors
"""
# Switching from ENABLED --> DISABLED/HYBRID when appending can cause integrity errors for subsequent reads:
# - Assume the last write was done with ENABLED (segments don't have parent references updated).
# - Subsequent appends were done in DISABLED/HYBRID (append segments have parent references).
# - Reading with DISABLED won't "see" the first write's segments.
prev_fw_config = get_fwptr_config(previous_version)
# Convert to a full-write, which force-updates all segments with parent references.
return prev_fw_config is FwPointersCfg.ENABLED and ARCTIC_FORWARD_POINTERS_CFG is not FwPointersCfg.ENABLED
示例8: check_written
# 需要導入模塊: import pymongo [as 別名]
# 或者: from pymongo import errors [as 別名]
def check_written(collection, symbol, version):
# Currently only called from methods which guarantee 'base_version_id' is not populated.
# Make it nonetheless safe for the general case.
parent_id = version_base_or_id(version)
# Check all the chunks are in place
if version.get(FW_POINTERS_CONFIG_KEY) == FwPointersCfg.DISABLED.name:
spec = {'symbol': symbol, 'parent': parent_id}
else:
spec = {'symbol': symbol, 'sha': {'$in': version[FW_POINTERS_REFS_KEY]}}
seen_chunks = mongo_count(collection, filter=spec)
if seen_chunks != version['segment_count']:
raise pymongo.errors.OperationFailure("Failed to write all the chunks. Saw %s expecting %s. "
"Parent: %s. Segments: %s" %
(seen_chunks, version['segment_count'], parent_id,
list(collection.find(spec, projection={'_id': 1, 'segment': 1}))))
if version.get(FW_POINTERS_CONFIG_KEY) == FwPointersCfg.HYBRID.name and ARCTIC_FORWARD_POINTERS_RECONCILE:
seen_chunks_reverse_pointers = mongo_count(collection, filter={'symbol': symbol, 'parent': parent_id})
if seen_chunks != seen_chunks_reverse_pointers:
raise pymongo.errors.OperationFailure("Failed to reconcile forward pointer chunks ({}). "
"Parent {}. "
"Reverse pointers segments #: {}. "
"Forward pointers segments #: {}.".format(
symbol, parent_id, seen_chunks_reverse_pointers, seen_chunks))
示例9: _insert_version
# 需要導入模塊: import pymongo [as 別名]
# 或者: from pymongo import errors [as 別名]
def _insert_version(self, version):
try:
# Keep here the mongo_retry to avoid incrementing versions and polluting the DB with garbage segments,
# upon intermittent Mongo errors
# If, however, we get a DuplicateKeyError, suppress it and raise OperationFailure, so that the method-scoped
# mongo_retry re-tries and creates a new version, to overcome the issue.
mongo_retry(self._versions.insert_one)(version)
except DuplicateKeyError as err:
logger.exception(err)
raise OperationFailure("A version with the same _id exists, force a clean retry")
示例10: dump_strings
# 需要導入模塊: import pymongo [as 別名]
# 或者: from pymongo import errors [as 別名]
def dump_strings(self, entries):
operations = []
for entry in entries:
operations.append(pymongo.UpdateOne({'_id': entry.value}, {'$inc': {'count': 1}}, upsert=True))
if len(operations) > 0:
try:
self.strings_collection.bulk_write(operations, ordered=False)
except pymongo.errors.BulkWriteError as bwe:
print(bwe.details)
# filter out "key too large to index" exceptions, which have error code 17280
# we don't care about them
filtered_errors = filter(lambda x: x['code'] != 17280, bwe.details['writeErrors'])
if len(list(filtered_errors)) > 0:
raise
示例11: __init__
# 需要導入模塊: import pymongo [as 別名]
# 或者: from pymongo import errors [as 別名]
def __init__(
self,
max_calls_before_failure=2,
exception_to_raise=pymongo.errors.AutoReconnect,
**kwargs
):
super().__init__(**kwargs)
self._max_calls_before_failure = max_calls_before_failure
self.exception_to_raise = exception_to_raise
self._exception_to_raise = exception_to_raise
示例12: insert_one
# 需要導入模塊: import pymongo [as 別名]
# 或者: from pymongo import errors [as 別名]
def insert_one(self, document, session=None):
self._calls += 1
if self._calls > self._max_calls_before_failure:
raise pymongo.errors.ConnectionFailure
else:
return super().insert_one(document)
示例13: update_one
# 需要導入模塊: import pymongo [as 別名]
# 或者: from pymongo import errors [as 別名]
def update_one(self, filter, update, upsert=False, session=None):
self._calls += 1
if self._calls > self._max_calls_before_failure:
raise pymongo.errors.ConnectionFailure
else:
return super().update_one(filter, update, upsert)
示例14: mongo_obs
# 需要導入模塊: import pymongo [as 別名]
# 或者: from pymongo import errors [as 別名]
def mongo_obs(monkeypatch):
client = ReconnectingMongoClient(
max_calls_before_reconnect=10,
max_calls_before_failure=1,
exception_to_raise=pymongo.errors.ServerSelectionTimeoutError,
)
fs = gridfs.GridFS(client.sacred)
monkeypatch.setattr(pymongo, "MongoClient", lambda *args, **kwargs: client)
monkeypatch.setattr(gridfs, "GridFS", lambda _: fs)
return QueuedMongoObserver(interval=0.01, retry_interval=0.01)
示例15: test_mongo_observer_failed_event_updates_run
# 需要導入模塊: import pymongo [as 別名]
# 或者: from pymongo import errors [as 別名]
def test_mongo_observer_failed_event_updates_run(mongo_obs, sample_run):
mongo_obs.started_event(**sample_run)
fail_trace = "lots of errors and\nso\non..."
mongo_obs.failed_event(fail_time=T2, fail_trace=fail_trace)
assert mongo_obs.runs.count_documents({}) == 1
db_run = mongo_obs.runs.find_one()
assert db_run["stop_time"] == T2
assert db_run["status"] == "FAILED"
assert db_run["fail_trace"] == fail_trace