code stringlengths 73 34.1k | label stringclasses 1 value |
|---|---|
public static<A, B, Z> Function2<A, B, Z> lift(Func2<A, B, Z> f) {
return bridge.lift(f);
} | java |
public static<A, B, C, Z> Function3<A, B, C, Z> lift(Func3<A, B, C, Z> f) {
return bridge.lift(f);
} | java |
public static<A, B, C, D, Z> Function4<A, B, C, D, Z> lift(Func4<A, B, C, D, Z> f) {
return bridge.lift(f);
} | java |
public static<Z> Function0<Z> lift(Callable<Z> f) {
return bridge.lift(f);
} | java |
public static CssSel sel(String selector, String value) {
return j.sel(selector, value);
} | java |
public static<T> Vendor<T> vendor(Func0<T> f) {
return j.vendor(f);
} | java |
public static<T> Vendor<T> vendor(Callable<T> f) {
return j.vendor(f);
} | java |
public static<T> SessionVar<T> vendSessionVar(T defValue) {
return (new VarsJBridge()).vendSessionVar(defValue, new Exception());
} | java |
public static<T> SessionVar<T> vendSessionVar(Callable<T> defFunc) {
return (new VarsJBridge()).vendSessionVar(defFunc, new Exception());
} | java |
protected <T1> void dispatchCommand(final BiConsumer<P, T1> action, final T1 routable1) {
routingFor(routable1)
.routees()
.forEach(routee -> routee.receiveCommand(action, routable1));
} | java |
@Nullable
public ResultT first() {
final CoreRemoteMongoCursor<ResultT> cursor = iterator();
if (!cursor.hasNext()) {
return null;
}
return cursor.next();
} | java |
public <U> CoreRemoteMongoIterable<U> map(final Function<ResultT, U> mapper) {
return new CoreRemoteMappingIterable<>(this, mapper);
} | java |
public <A extends Collection<? super ResultT>> A into(final A target) {
forEach(new Block<ResultT>() {
@Override
public void apply(@Nonnull final ResultT t) {
target.add(t);
}
});
return target;
} | java |
public AwsServiceClient withCodecRegistry(@Nonnull final CodecRegistry codecRegistry) {
return new AwsServiceClientImpl(proxy.withCodecRegistry(codecRegistry), dispatcher);
} | java |
public void callFunction(
final String name,
final List<?> args,
final @Nullable Long requestTimeout) {
this.functionService.callFunction(name, args, requestTimeout);
} | java |
public <T> T callFunction(
final String name,
final List<?> args,
final @Nullable Long requestTimeout,
final Class<T> resultClass,
final CodecRegistry codecRegistry
) {
return this.functionService
.withCodecRegistry(codecRegistry)
.callFunction(name, args, requestTimeout, resultClass);
} | java |
public void start() {
instanceLock.writeLock().lock();
try {
for (final Map.Entry<MongoNamespace, NamespaceChangeStreamListener> streamerEntry :
nsStreamers.entrySet()) {
streamerEntry.getValue().start();
}
} finally {
instanceLock.writeLock().unlock();
}
} | java |
public void stop() {
instanceLock.writeLock().lock();
try {
for (final NamespaceChangeStreamListener streamer : nsStreamers.values()) {
streamer.stop();
}
} finally {
instanceLock.writeLock().unlock();
}
} | java |
public void addNamespace(final MongoNamespace namespace) {
this.instanceLock.writeLock().lock();
try {
if (this.nsStreamers.containsKey(namespace)) {
return;
}
final NamespaceChangeStreamListener streamer =
new NamespaceChangeStreamListener(
namespace,
instanceConfig.getNamespaceConfig(namespace),
service,
networkMonitor,
authMonitor,
getLockForNamespace(namespace));
this.nsStreamers.put(namespace, streamer);
} finally {
this.instanceLock.writeLock().unlock();
}
} | java |
@Override
public void removeNamespace(final MongoNamespace namespace) {
this.instanceLock.writeLock().lock();
try {
if (!this.nsStreamers.containsKey(namespace)) {
return;
}
final NamespaceChangeStreamListener streamer = this.nsStreamers.get(namespace);
streamer.stop();
this.nsStreamers.remove(namespace);
} finally {
this.instanceLock.writeLock().unlock();
}
} | java |
public Map<BsonValue, ChangeEvent<BsonDocument>> getEventsForNamespace(
final MongoNamespace namespace
) {
this.instanceLock.readLock().lock();
final NamespaceChangeStreamListener streamer;
try {
streamer = nsStreamers.get(namespace);
} finally {
this.instanceLock.readLock().unlock();
}
if (streamer == null) {
return new HashMap<>();
}
return streamer.getEvents();
} | java |
public @Nullable ChangeEvent<BsonDocument> getUnprocessedEventForDocumentId(
final MongoNamespace namespace,
final BsonValue documentId
) {
this.instanceLock.readLock().lock();
final NamespaceChangeStreamListener streamer;
try {
streamer = nsStreamers.get(namespace);
} finally {
this.instanceLock.readLock().unlock();
}
if (streamer == null) {
return null;
}
return streamer.getUnprocessedEventForDocumentId(documentId);
} | java |
void setPaused(final boolean isPaused) {
docLock.writeLock().lock();
try {
docsColl.updateOne(
getDocFilter(namespace, documentId),
new BsonDocument("$set",
new BsonDocument(
ConfigCodec.Fields.IS_PAUSED,
new BsonBoolean(isPaused))));
this.isPaused = isPaused;
} catch (IllegalStateException e) {
// eat this
} finally {
docLock.writeLock().unlock();
}
} | java |
public void setSomePendingWritesAndSave(
final long atTime,
final ChangeEvent<BsonDocument> changeEvent
) {
docLock.writeLock().lock();
try {
// if we were frozen
if (isPaused) {
// unfreeze the document due to the local write
setPaused(false);
// and now the unfrozen document is now stale
setStale(true);
}
this.lastUncommittedChangeEvent =
coalesceChangeEvents(this.lastUncommittedChangeEvent, changeEvent);
this.lastResolution = atTime;
docsColl.replaceOne(
getDocFilter(namespace, documentId),
this);
} finally {
docLock.writeLock().unlock();
}
} | java |
private static ChangeEvent<BsonDocument> coalesceChangeEvents(
final ChangeEvent<BsonDocument> lastUncommittedChangeEvent,
final ChangeEvent<BsonDocument> newestChangeEvent
) {
if (lastUncommittedChangeEvent == null) {
return newestChangeEvent;
}
switch (lastUncommittedChangeEvent.getOperationType()) {
case INSERT:
switch (newestChangeEvent.getOperationType()) {
// Coalesce replaces/updates to inserts since we believe at some point a document did not
// exist remotely and that this replace or update should really be an insert if we are
// still in an uncommitted state.
case REPLACE:
case UPDATE:
return new ChangeEvent<>(
newestChangeEvent.getId(),
OperationType.INSERT,
newestChangeEvent.getFullDocument(),
newestChangeEvent.getNamespace(),
newestChangeEvent.getDocumentKey(),
null,
newestChangeEvent.hasUncommittedWrites()
);
default:
break;
}
break;
case DELETE:
switch (newestChangeEvent.getOperationType()) {
// Coalesce inserts to replaces since we believe at some point a document existed
// remotely and that this insert should really be an replace if we are still in an
// uncommitted state.
case INSERT:
return new ChangeEvent<>(
newestChangeEvent.getId(),
OperationType.REPLACE,
newestChangeEvent.getFullDocument(),
newestChangeEvent.getNamespace(),
newestChangeEvent.getDocumentKey(),
null,
newestChangeEvent.hasUncommittedWrites()
);
default:
break;
}
break;
case UPDATE:
switch (newestChangeEvent.getOperationType()) {
case UPDATE:
return new ChangeEvent<>(
newestChangeEvent.getId(),
OperationType.UPDATE,
newestChangeEvent.getFullDocument(),
newestChangeEvent.getNamespace(),
newestChangeEvent.getDocumentKey(),
lastUncommittedChangeEvent.getUpdateDescription() != null
? lastUncommittedChangeEvent
.getUpdateDescription()
.merge(newestChangeEvent.getUpdateDescription())
: newestChangeEvent.getUpdateDescription(),
newestChangeEvent.hasUncommittedWrites()
);
case REPLACE:
return new ChangeEvent<>(
newestChangeEvent.getId(),
OperationType.REPLACE,
newestChangeEvent.getFullDocument(),
newestChangeEvent.getNamespace(),
newestChangeEvent.getDocumentKey(),
null,
newestChangeEvent.hasUncommittedWrites()
);
default:
break;
}
break;
case REPLACE:
switch (newestChangeEvent.getOperationType()) {
case UPDATE:
return new ChangeEvent<>(
newestChangeEvent.getId(),
OperationType.REPLACE,
newestChangeEvent.getFullDocument(),
newestChangeEvent.getNamespace(),
newestChangeEvent.getDocumentKey(),
null,
newestChangeEvent.hasUncommittedWrites()
);
default:
break;
}
break;
default:
break;
}
return newestChangeEvent;
} | java |
static DocumentVersionInfo getRemoteVersionInfo(final BsonDocument remoteDocument) {
final BsonDocument version = getDocumentVersionDoc(remoteDocument);
return new DocumentVersionInfo(
version,
remoteDocument != null
? BsonUtils.getDocumentId(remoteDocument) : null
);
} | java |
static BsonDocument getFreshVersionDocument() {
final BsonDocument versionDoc = new BsonDocument();
versionDoc.append(Fields.SYNC_PROTOCOL_VERSION_FIELD, new BsonInt32(1));
versionDoc.append(Fields.INSTANCE_ID_FIELD, new BsonString(UUID.randomUUID().toString()));
versionDoc.append(Fields.VERSION_COUNTER_FIELD, new BsonInt64(0L));
return versionDoc;
} | java |
static BsonDocument getDocumentVersionDoc(final BsonDocument document) {
if (document == null || !document.containsKey(DOCUMENT_VERSION_FIELD)) {
return null;
}
return document.getDocument(DOCUMENT_VERSION_FIELD, null);
} | java |
static BsonDocument getVersionedFilter(
@Nonnull final BsonValue documentId,
@Nullable final BsonValue version
) {
final BsonDocument filter = new BsonDocument("_id", documentId);
if (version == null) {
filter.put(DOCUMENT_VERSION_FIELD, new BsonDocument("$exists", BsonBoolean.FALSE));
} else {
filter.put(DOCUMENT_VERSION_FIELD, version);
}
return filter;
} | java |
BsonDocument getNextVersion() {
if (!this.hasVersion() || this.getVersionDoc() == null) {
return getFreshVersionDocument();
}
final BsonDocument nextVersion = BsonUtils.copyOfDocument(this.getVersionDoc());
nextVersion.put(
Fields.VERSION_COUNTER_FIELD,
new BsonInt64(this.getVersion().getVersionCounter() + 1));
return nextVersion;
} | java |
public RemoteMongoCollection<Document> getCollection(final String collectionName) {
return new RemoteMongoCollectionImpl<>(proxy.getCollection(collectionName), dispatcher);
} | java |
static <T> StitchEvent<T> fromEvent(final Event event,
final Decoder<T> decoder) {
return new StitchEvent<>(event.getEventName(), event.getData(), decoder);
} | java |
protected final Event processEvent() throws IOException {
while (true) {
String line;
try {
line = readLine();
} catch (final EOFException ex) {
if (doneOnce) {
throw ex;
}
doneOnce = true;
line = "";
}
// If the line is empty (a blank line), Dispatch the event, as defined below.
if (line.isEmpty()) {
// If the data buffer is an empty string, set the data buffer and the event name buffer to
// the empty string and abort these steps.
if (dataBuffer.length() == 0) {
eventName = "";
continue;
}
// If the event name buffer is not the empty string but is also not a valid NCName,
// set the data buffer and the event name buffer to the empty string and abort these steps.
// NOT IMPLEMENTED
final Event.Builder eventBuilder = new Event.Builder();
eventBuilder.withEventName(eventName.isEmpty() ? Event.MESSAGE_EVENT : eventName);
eventBuilder.withData(dataBuffer.toString());
// Set the data buffer and the event name buffer to the empty string.
dataBuffer = new StringBuilder();
eventName = "";
return eventBuilder.build();
// If the line starts with a U+003A COLON character (':')
} else if (line.startsWith(":")) {
// ignore the line
// If the line contains a U+003A COLON character (':') character
} else if (line.contains(":")) {
// Collect the characters on the line before the first U+003A COLON character (':'),
// and let field be that string.
final int colonIdx = line.indexOf(":");
final String field = line.substring(0, colonIdx);
// Collect the characters on the line after the first U+003A COLON character (':'),
// and let value be that string.
// If value starts with a single U+0020 SPACE character, remove it from value.
String value = line.substring(colonIdx + 1);
value = value.startsWith(" ") ? value.substring(1) : value;
processField(field, value);
// Otherwise, the string is not empty but does not contain a U+003A COLON character (':')
// character
} else {
processField(line, "");
}
}
} | java |
private static String handleRichError(final Response response, final String body) {
if (!response.getHeaders().containsKey(Headers.CONTENT_TYPE)
|| !response.getHeaders().get(Headers.CONTENT_TYPE).equals(ContentTypes.APPLICATION_JSON)) {
return body;
}
final Document doc;
try {
doc = BsonUtils.parseValue(body, Document.class);
} catch (Exception e) {
return body;
}
if (!doc.containsKey(Fields.ERROR)) {
return body;
}
final String errorMsg = doc.getString(Fields.ERROR);
if (!doc.containsKey(Fields.ERROR_CODE)) {
return errorMsg;
}
final String errorCode = doc.getString(Fields.ERROR_CODE);
throw new StitchServiceException(errorMsg, StitchServiceErrorCode.fromCodeName(errorCode));
} | java |
public static void initialize(final Context context) {
if (!initialized.compareAndSet(false, true)) {
return;
}
applicationContext = context.getApplicationContext();
final String packageName = applicationContext.getPackageName();
localAppName = packageName;
final PackageManager manager = applicationContext.getPackageManager();
try {
final PackageInfo pkgInfo = manager.getPackageInfo(packageName, 0);
localAppVersion = pkgInfo.versionName;
} catch (final NameNotFoundException e) {
Log.d(TAG, "Failed to get version of application, will not send in device info.");
}
Log.d(TAG, "Initialized android SDK");
} | java |
public static StitchAppClient getAppClient(
@Nonnull final String clientAppId
) {
ensureInitialized();
synchronized (Stitch.class) {
if (!appClients.containsKey(clientAppId)) {
throw new IllegalStateException(
String.format("client for app '%s' has not yet been initialized", clientAppId));
}
return appClients.get(clientAppId);
}
} | java |
public void start() {
nsLock.writeLock().lock();
try {
if (runnerThread != null) {
return;
}
runnerThread =
new Thread(new NamespaceChangeStreamRunner(
new WeakReference<>(this), networkMonitor, logger));
runnerThread.start();
} finally {
nsLock.writeLock().unlock();
}
} | java |
public void stop() {
if (runnerThread == null) {
return;
}
runnerThread.interrupt();
nsLock.writeLock().lock();
try {
if (runnerThread == null) {
return;
}
this.cancel();
this.close();
while (runnerThread.isAlive()) {
runnerThread.interrupt();
try {
runnerThread.join(1000);
} catch (final Exception e) {
e.printStackTrace();
return;
}
}
runnerThread = null;
} catch (Exception e) {
e.printStackTrace();
} finally {
nsLock.writeLock().unlock();
}
} | java |
boolean openStream() throws InterruptedException, IOException {
logger.info("stream START");
final boolean isOpen;
final Set<BsonValue> idsToWatch = nsConfig.getSynchronizedDocumentIds();
if (!networkMonitor.isConnected()) {
logger.info("stream END - Network disconnected");
return false;
}
if (idsToWatch.isEmpty()) {
logger.info("stream END - No synchronized documents");
return false;
}
nsLock.writeLock().lockInterruptibly();
try {
if (!authMonitor.isLoggedIn()) {
logger.info("stream END - Logged out");
return false;
}
final Document args = new Document();
args.put("database", namespace.getDatabaseName());
args.put("collection", namespace.getCollectionName());
args.put("ids", idsToWatch);
currentStream =
service.streamFunction(
"watch",
Collections.singletonList(args),
ResultDecoders.changeEventDecoder(BSON_DOCUMENT_CODEC));
if (currentStream != null && currentStream.isOpen()) {
this.nsConfig.setStale(true);
isOpen = true;
} else {
isOpen = false;
}
} finally {
nsLock.writeLock().unlock();
}
return isOpen;
} | java |
@SuppressWarnings("unchecked")
public Map<BsonValue, ChangeEvent<BsonDocument>> getEvents() {
nsLock.readLock().lock();
final Map<BsonValue, ChangeEvent<BsonDocument>> events;
try {
events = new HashMap<>(this.events);
} finally {
nsLock.readLock().unlock();
}
nsLock.writeLock().lock();
try {
this.events.clear();
return events;
} finally {
nsLock.writeLock().unlock();
}
} | java |
public @Nullable ChangeEvent<BsonDocument> getUnprocessedEventForDocumentId(
final BsonValue documentId
) {
final ChangeEvent<BsonDocument> event;
nsLock.readLock().lock();
try {
event = this.events.get(documentId);
} finally {
nsLock.readLock().unlock();
}
nsLock.writeLock().lock();
try {
this.events.remove(documentId);
return event;
} finally {
nsLock.writeLock().unlock();
}
} | java |
public static long hash(final BsonDocument doc) {
if (doc == null) {
return 0L;
}
final byte[] docBytes = toBytes(doc);
long hashValue = FNV_64BIT_OFFSET_BASIS;
for (int offset = 0; offset < docBytes.length; offset++) {
hashValue ^= (0xFF & docBytes[offset]);
hashValue *= FNV_64BIT_PRIME;
}
return hashValue;
} | java |
public static boolean deleteDatabase(final StitchAppClientInfo appInfo,
final String serviceName,
final EmbeddedMongoClientFactory clientFactory,
final String userId) {
final String dataDir = appInfo.getDataDirectory();
if (dataDir == null) {
throw new IllegalArgumentException("StitchAppClient not configured with a data directory");
}
final String instanceKey = String.format(
"%s-%s_sync_%s_%s", appInfo.getClientAppId(), dataDir, serviceName, userId);
final String dbPath = String.format(
"%s/%s/sync_mongodb_%s/%s/0/", dataDir, appInfo.getClientAppId(), serviceName, userId);
final MongoClient client =
clientFactory.getClient(instanceKey, dbPath, appInfo.getCodecRegistry());
for (final String listDatabaseName : client.listDatabaseNames()) {
try {
client.getDatabase(listDatabaseName).drop();
} catch (Exception e) {
// do nothing
}
}
client.close();
clientFactory.removeClient(instanceKey);
return new File(dbPath).delete();
} | java |
public Task<Long> count() {
return dispatcher.dispatchTask(new Callable<Long>() {
@Override
public Long call() {
return proxy.count();
}
});
} | java |
public Task<RemoteInsertOneResult> insertOne(final DocumentT document) {
return dispatcher.dispatchTask(new Callable<RemoteInsertOneResult>() {
@Override
public RemoteInsertOneResult call() {
return proxy.insertOne(document);
}
});
} | java |
public Task<Void> registerWithEmail(@NonNull final String email, @NonNull final String password) {
return dispatcher.dispatchTask(
new Callable<Void>() {
@Override
public Void call() {
registerWithEmailInternal(email, password);
return null;
}
});
} | java |
public Task<Void> confirmUser(@NonNull final String token, @NonNull final String tokenId) {
return dispatcher.dispatchTask(
new Callable<Void>() {
@Override
public Void call() {
confirmUserInternal(token, tokenId);
return null;
}
});
} | java |
public Task<Void> resendConfirmationEmail(@NonNull final String email) {
return dispatcher.dispatchTask(
new Callable<Void>() {
@Override
public Void call() {
resendConfirmationEmailInternal(email);
return null;
}
});
} | java |
public Task<Void> sendResetPasswordEmail(@NonNull final String email) {
return dispatcher.dispatchTask(
new Callable<Void>() {
@Override
public Void call() {
sendResetPasswordEmailInternal(email);
return null;
}
});
} | java |
@Override
public boolean commit() {
final CoreRemoteMongoCollection<DocumentT> collection = getCollection();
final List<WriteModel<DocumentT>> writeModels = getBulkWriteModels();
// define success as any one operation succeeding for now
boolean success = true;
for (final WriteModel<DocumentT> write : writeModels) {
if (write instanceof ReplaceOneModel) {
final ReplaceOneModel<DocumentT> replaceModel = ((ReplaceOneModel) write);
final RemoteUpdateResult result =
collection.updateOne(replaceModel.getFilter(), (Bson) replaceModel.getReplacement());
success = success
&& (result != null && result.getModifiedCount() == result.getMatchedCount());
} else if (write instanceof UpdateOneModel) {
final UpdateOneModel<DocumentT> updateModel = ((UpdateOneModel) write);
final RemoteUpdateResult result =
collection.updateOne(updateModel.getFilter(), updateModel.getUpdate());
success = success
&& (result != null && result.getModifiedCount() == result.getMatchedCount());
} else if (write instanceof UpdateManyModel) {
final UpdateManyModel<DocumentT> updateModel = ((UpdateManyModel) write);
final RemoteUpdateResult result =
collection.updateMany(updateModel.getFilter(), updateModel.getUpdate());
success = success
&& (result != null && result.getModifiedCount() == result.getMatchedCount());
}
}
return success;
} | java |
@Override
public Response doRequest(final StitchRequest stitchReq) {
initAppMetadata(clientAppId);
return super.doRequestUrl(stitchReq, getHostname());
} | java |
@Override
public EventStream doStreamRequest(final StitchRequest stitchReq) {
initAppMetadata(clientAppId);
return super.doStreamRequestUrl(stitchReq, getHostname());
} | java |
public BsonDocument toUpdateDocument() {
final List<BsonElement> unsets = new ArrayList<>();
for (final String removedField : this.removedFields) {
unsets.add(new BsonElement(removedField, new BsonBoolean(true)));
}
final BsonDocument updateDocument = new BsonDocument();
if (this.updatedFields.size() > 0) {
updateDocument.append("$set", this.updatedFields);
}
if (unsets.size() > 0) {
updateDocument.append("$unset", new BsonDocument(unsets));
}
return updateDocument;
} | java |
public BsonDocument toBsonDocument() {
final BsonDocument updateDescDoc = new BsonDocument();
updateDescDoc.put(
Fields.UPDATED_FIELDS_FIELD,
this.getUpdatedFields());
final BsonArray removedFields = new BsonArray();
for (final String field : this.getRemovedFields()) {
removedFields.add(new BsonString(field));
}
updateDescDoc.put(
Fields.REMOVED_FIELDS_FIELD,
removedFields);
return updateDescDoc;
} | java |
public static UpdateDescription fromBsonDocument(final BsonDocument document) {
keyPresent(Fields.UPDATED_FIELDS_FIELD, document);
keyPresent(Fields.REMOVED_FIELDS_FIELD, document);
final BsonArray removedFieldsArr =
document.getArray(Fields.REMOVED_FIELDS_FIELD);
final Set<String> removedFields = new HashSet<>(removedFieldsArr.size());
for (final BsonValue field : removedFieldsArr) {
removedFields.add(field.asString().getValue());
}
return new UpdateDescription(document.getDocument(Fields.UPDATED_FIELDS_FIELD), removedFields);
} | java |
public UpdateDescription merge(@Nullable final UpdateDescription otherDescription) {
if (otherDescription != null) {
for (final Map.Entry<String, BsonValue> entry : this.updatedFields.entrySet()) {
if (otherDescription.removedFields.contains(entry.getKey())) {
this.updatedFields.remove(entry.getKey());
}
}
for (final String removedField : this.removedFields) {
if (otherDescription.updatedFields.containsKey(removedField)) {
this.removedFields.remove(removedField);
}
}
this.removedFields.addAll(otherDescription.removedFields);
this.updatedFields.putAll(otherDescription.updatedFields);
}
return this;
} | java |
@Override
@SuppressWarnings("unchecked")
public Stream<ChangeEvent<DocumentT>> watch(final BsonValue... ids)
throws InterruptedException, IOException {
return operations.watch(
new HashSet<>(Arrays.asList(ids)),
false,
documentClass
).execute(service);
} | java |
public Task<Void> updateSyncFrequency(@NonNull final SyncFrequency syncFrequency) {
return this.dispatcher.dispatchTask(new Callable<Void>() {
@Override
public Void call() throws Exception {
SyncImpl.this.proxy.updateSyncFrequency(syncFrequency);
return null;
}
});
} | java |
public void emitEvent(
final NamespaceSynchronizationConfig nsConfig,
final ChangeEvent<BsonDocument> event) {
listenersLock.lock();
try {
if (nsConfig.getNamespaceListenerConfig() == null) {
return;
}
final NamespaceListenerConfig namespaceListener =
nsConfig.getNamespaceListenerConfig();
eventDispatcher.dispatch(() -> {
try {
if (namespaceListener.getEventListener() != null) {
namespaceListener.getEventListener().onEvent(
BsonUtils.getDocumentId(event.getDocumentKey()),
ChangeEvents.transformChangeEventForUser(
event, namespaceListener.getDocumentCodec()));
}
} catch (final Exception ex) {
logger.error(String.format(
Locale.US,
"emitEvent ns=%s documentId=%s emit exception: %s",
event.getNamespace(),
BsonUtils.getDocumentId(event.getDocumentKey()),
ex), ex);
}
return null;
});
} finally {
listenersLock.unlock();
}
} | java |
static ChangeEvent<BsonDocument> changeEventForLocalInsert(
final MongoNamespace namespace,
final BsonDocument document,
final boolean writePending
) {
final BsonValue docId = BsonUtils.getDocumentId(document);
return new ChangeEvent<>(
new BsonDocument(),
OperationType.INSERT,
document,
namespace,
new BsonDocument("_id", docId),
null,
writePending);
} | java |
static ChangeEvent<BsonDocument> changeEventForLocalUpdate(
final MongoNamespace namespace,
final BsonValue documentId,
final UpdateDescription update,
final BsonDocument fullDocumentAfterUpdate,
final boolean writePending
) {
return new ChangeEvent<>(
new BsonDocument(),
OperationType.UPDATE,
fullDocumentAfterUpdate,
namespace,
new BsonDocument("_id", documentId),
update,
writePending);
} | java |
static ChangeEvent<BsonDocument> changeEventForLocalReplace(
final MongoNamespace namespace,
final BsonValue documentId,
final BsonDocument document,
final boolean writePending
) {
return new ChangeEvent<>(
new BsonDocument(),
OperationType.REPLACE,
document,
namespace,
new BsonDocument("_id", documentId),
null,
writePending);
} | java |
static ChangeEvent<BsonDocument> changeEventForLocalDelete(
final MongoNamespace namespace,
final BsonValue documentId,
final boolean writePending
) {
return new ChangeEvent<>(
new BsonDocument(),
OperationType.DELETE,
null,
namespace,
new BsonDocument("_id", documentId),
null,
writePending);
} | java |
public static <T> void notNull(final String name, final T value) {
if (value == null) {
throw new IllegalArgumentException(name + " can not be null");
}
} | java |
public static void keyPresent(final String key, final Map<String, ?> map) {
if (!map.containsKey(key)) {
throw new IllegalStateException(
String.format("expected %s to be present", key));
}
} | java |
public static BsonDocument copyOfDocument(final BsonDocument document) {
final BsonDocument newDocument = new BsonDocument();
for (final Map.Entry<String, BsonValue> kv : document.entrySet()) {
newDocument.put(kv.getKey(), kv.getValue());
}
return newDocument;
} | java |
public static <T> ConflictHandler<T> localWins() {
return new ConflictHandler<T>() {
@Override
public T resolveConflict(
final BsonValue documentId,
final ChangeEvent<T> localEvent,
final ChangeEvent<T> remoteEvent
) {
return localEvent.getFullDocument();
}
};
} | java |
public static boolean isTodoItem(final Document todoItemDoc) {
return todoItemDoc.containsKey(ID_KEY)
&& todoItemDoc.containsKey(TASK_KEY)
&& todoItemDoc.containsKey(CHECKED_KEY);
} | java |
void recover() {
final List<NamespaceSynchronizationConfig> nsConfigs = new ArrayList<>();
for (final MongoNamespace ns : this.syncConfig.getSynchronizedNamespaces()) {
nsConfigs.add(this.syncConfig.getNamespaceConfig(ns));
}
for (final NamespaceSynchronizationConfig nsConfig : nsConfigs) {
nsConfig.getLock().writeLock().lock();
}
try {
for (final NamespaceSynchronizationConfig nsConfig : nsConfigs) {
nsConfig.getLock().writeLock().lock();
try {
recoverNamespace(nsConfig);
} finally {
nsConfig.getLock().writeLock().unlock();
}
}
} finally {
for (final NamespaceSynchronizationConfig nsConfig : nsConfigs) {
nsConfig.getLock().writeLock().unlock();
}
}
} | java |
private void recoverNamespace(final NamespaceSynchronizationConfig nsConfig) {
final MongoCollection<BsonDocument> undoCollection =
getUndoCollection(nsConfig.getNamespace());
final MongoCollection<BsonDocument> localCollection =
getLocalCollection(nsConfig.getNamespace());
final List<BsonDocument> undoDocs = undoCollection.find().into(new ArrayList<>());
final Set<BsonValue> recoveredIds = new HashSet<>();
// Replace local docs with undo docs. Presence of an undo doc implies we had a system failure
// during a write. This covers updates and deletes.
for (final BsonDocument undoDoc : undoDocs) {
final BsonValue documentId = BsonUtils.getDocumentId(undoDoc);
final BsonDocument filter = getDocumentIdFilter(documentId);
localCollection.findOneAndReplace(
filter, undoDoc, new FindOneAndReplaceOptions().upsert(true));
recoveredIds.add(documentId);
}
// If we recovered a document, but its pending writes are set to do something else, then the
// failure occurred after the pending writes were set, but before the undo document was
// deleted. In this case, we should restore the document to the state that the pending
// write indicates. There is a possibility that the pending write is from before the failed
// operation, but in that case, the findOneAndReplace or delete is a no-op since restoring
// the document to the state of the change event would be the same as recovering the undo
// document.
for (final CoreDocumentSynchronizationConfig docConfig : nsConfig.getSynchronizedDocuments()) {
final BsonValue documentId = docConfig.getDocumentId();
final BsonDocument filter = getDocumentIdFilter(documentId);
if (recoveredIds.contains(docConfig.getDocumentId())) {
final ChangeEvent<BsonDocument> pendingWrite = docConfig.getLastUncommittedChangeEvent();
if (pendingWrite != null) {
switch (pendingWrite.getOperationType()) {
case INSERT:
case UPDATE:
case REPLACE:
localCollection.findOneAndReplace(
filter,
pendingWrite.getFullDocument(),
new FindOneAndReplaceOptions().upsert(true)
);
break;
case DELETE:
localCollection.deleteOne(filter);
break;
default:
// There should never be pending writes with an unknown event type, but if someone
// is messing with the config collection we want to stop the synchronizer to prevent
// further data corruption.
throw new IllegalStateException(
"there should not be a pending write with an unknown event type"
);
}
}
}
}
// Delete all of our undo documents. If we've reached this point, we've recovered the local
// collection to the state we want with respect to all of our undo documents. If we fail before
// these deletes or while carrying out the deletes, but after recovering the documents to
// their desired state, that's okay because the next recovery pass will be effectively a no-op
// up to this point.
for (final BsonValue recoveredId : recoveredIds) {
undoCollection.deleteOne(getDocumentIdFilter(recoveredId));
}
// Find local documents for which there are no document configs and delete them. This covers
// inserts, upserts, and desync deletes. This will occur on any recovery pass regardless of
// the documents in the undo collection, so it's fine that we do this after deleting the undo
// documents.
localCollection.deleteMany(new BsonDocument(
"_id",
new BsonDocument(
"$nin",
new BsonArray(new ArrayList<>(
this.syncConfig.getSynchronizedDocumentIds(nsConfig.getNamespace()))))));
} | java |
public void wipeInMemorySettings() {
this.waitUntilInitialized();
syncLock.lock();
try {
this.instanceChangeStreamListener.stop();
if (instancesColl.find().first() == null) {
throw new IllegalStateException("expected to find instance configuration");
}
this.syncConfig = new InstanceSynchronizationConfig(configDb);
this.instanceChangeStreamListener = new InstanceChangeStreamListenerImpl(
syncConfig,
service,
networkMonitor,
authMonitor
);
this.isConfigured = false;
this.stop();
} finally {
syncLock.unlock();
}
} | java |
public void start() {
syncLock.lock();
try {
if (!this.isConfigured) {
return;
}
instanceChangeStreamListener.stop();
if (listenersEnabled) {
instanceChangeStreamListener.start();
}
if (syncThread == null) {
syncThread = new Thread(
new DataSynchronizerRunner(
new WeakReference<>(this),
networkMonitor,
logger
),
"dataSynchronizerRunnerThread"
);
}
if (syncThreadEnabled && !isRunning) {
syncThread.start();
isRunning = true;
}
} finally {
syncLock.unlock();
}
} | java |
public void stop() {
syncLock.lock();
try {
if (syncThread == null) {
return;
}
instanceChangeStreamListener.stop();
syncThread.interrupt();
try {
syncThread.join();
} catch (final InterruptedException e) {
return;
}
syncThread = null;
isRunning = false;
} finally {
syncLock.unlock();
}
} | java |
public void close() {
this.waitUntilInitialized();
this.ongoingOperationsGroup.blockAndWait();
syncLock.lock();
try {
if (this.networkMonitor != null) {
this.networkMonitor.removeNetworkStateListener(this);
}
this.dispatcher.close();
stop();
this.localClient.close();
} finally {
syncLock.unlock();
}
} | java |
public boolean doSyncPass() {
if (!this.isConfigured || !syncLock.tryLock()) {
return false;
}
try {
if (logicalT == Long.MAX_VALUE) {
if (logger.isInfoEnabled()) {
logger.info("reached max logical time; resetting back to 0");
}
logicalT = 0;
}
logicalT++;
if (logger.isInfoEnabled()) {
logger.info(String.format(
Locale.US,
"t='%d': doSyncPass START",
logicalT));
}
if (networkMonitor == null || !networkMonitor.isConnected()) {
if (logger.isInfoEnabled()) {
logger.info(String.format(
Locale.US,
"t='%d': doSyncPass END - Network disconnected",
logicalT));
}
return false;
}
if (authMonitor == null || !authMonitor.tryIsLoggedIn()) {
if (logger.isInfoEnabled()) {
logger.info(String.format(
Locale.US,
"t='%d': doSyncPass END - Logged out",
logicalT));
}
return false;
}
syncRemoteToLocal();
syncLocalToRemote();
if (logger.isInfoEnabled()) {
logger.info(String.format(
Locale.US,
"t='%d': doSyncPass END",
logicalT));
}
} catch (InterruptedException e) {
if (logger.isInfoEnabled()) {
logger.info(String.format(
Locale.US,
"t='%d': doSyncPass INTERRUPTED",
logicalT));
}
return false;
} finally {
syncLock.unlock();
}
return true;
} | java |
@CheckReturnValue
private LocalSyncWriteModelContainer resolveConflict(
final NamespaceSynchronizationConfig nsConfig,
final CoreDocumentSynchronizationConfig docConfig,
final ChangeEvent<BsonDocument> remoteEvent
) {
return resolveConflict(nsConfig, docConfig, docConfig.getLastUncommittedChangeEvent(),
remoteEvent);
} | java |
@SuppressWarnings("unchecked")
private static Object resolveConflictWithResolver(
final ConflictHandler conflictResolver,
final BsonValue documentId,
final ChangeEvent localEvent,
final ChangeEvent remoteEvent
) {
return conflictResolver.resolveConflict(
documentId,
localEvent,
remoteEvent);
} | java |
public void addWatcher(final MongoNamespace namespace,
final Callback<ChangeEvent<BsonDocument>, Object> watcher) {
instanceChangeStreamListener.addWatcher(namespace, watcher);
} | java |
public Set<CoreDocumentSynchronizationConfig> getSynchronizedDocuments(
final MongoNamespace namespace
) {
this.waitUntilInitialized();
try {
ongoingOperationsGroup.enter();
return this.syncConfig.getSynchronizedDocuments(namespace);
} finally {
ongoingOperationsGroup.exit();
}
} | java |
public Set<BsonValue> getPausedDocumentIds(final MongoNamespace namespace) {
this.waitUntilInitialized();
try {
ongoingOperationsGroup.enter();
final Set<BsonValue> pausedDocumentIds = new HashSet<>();
for (final CoreDocumentSynchronizationConfig config :
this.syncConfig.getSynchronizedDocuments(namespace)) {
if (config.isPaused()) {
pausedDocumentIds.add(config.getDocumentId());
}
}
return pausedDocumentIds;
} finally {
ongoingOperationsGroup.exit();
}
} | java |
boolean resumeSyncForDocument(
final MongoNamespace namespace,
final BsonValue documentId
) {
if (namespace == null || documentId == null) {
return false;
}
final NamespaceSynchronizationConfig namespaceSynchronizationConfig;
final CoreDocumentSynchronizationConfig config;
if ((namespaceSynchronizationConfig = syncConfig.getNamespaceConfig(namespace)) == null
|| (config = namespaceSynchronizationConfig.getSynchronizedDocument(documentId)) == null) {
return false;
}
config.setPaused(false);
return !config.isPaused();
} | java |
void insertOne(final MongoNamespace namespace, final BsonDocument document) {
this.waitUntilInitialized();
try {
ongoingOperationsGroup.enter();
// Remove forbidden fields from the document before inserting it into the local collection.
final BsonDocument docForStorage = sanitizeDocument(document);
final NamespaceSynchronizationConfig nsConfig =
this.syncConfig.getNamespaceConfig(namespace);
final Lock lock = nsConfig.getLock().writeLock();
lock.lock();
final ChangeEvent<BsonDocument> event;
final BsonValue documentId;
try {
getLocalCollection(namespace).insertOne(docForStorage);
documentId = BsonUtils.getDocumentId(docForStorage);
event = ChangeEvents.changeEventForLocalInsert(namespace, docForStorage, true);
final CoreDocumentSynchronizationConfig config = syncConfig.addAndGetSynchronizedDocument(
namespace,
documentId
);
config.setSomePendingWritesAndSave(logicalT, event);
} finally {
lock.unlock();
}
checkAndInsertNamespaceListener(namespace);
eventDispatcher.emitEvent(nsConfig, event);
} finally {
ongoingOperationsGroup.exit();
}
} | java |
DeleteResult deleteMany(final MongoNamespace namespace,
final Bson filter) {
this.waitUntilInitialized();
try {
ongoingOperationsGroup.enter();
final List<ChangeEvent<BsonDocument>> eventsToEmit = new ArrayList<>();
final DeleteResult result;
final NamespaceSynchronizationConfig nsConfig = this.syncConfig.getNamespaceConfig(namespace);
final Lock lock = nsConfig.getLock().writeLock();
lock.lock();
try {
final MongoCollection<BsonDocument> localCollection = getLocalCollection(namespace);
final MongoCollection<BsonDocument> undoCollection = getUndoCollection(namespace);
final Set<BsonValue> idsToDelete =
localCollection
.find(filter)
.map(new Function<BsonDocument, BsonValue>() {
@Override
@NonNull
public BsonValue apply(@NonNull final BsonDocument bsonDocument) {
undoCollection.insertOne(bsonDocument);
return BsonUtils.getDocumentId(bsonDocument);
}
}).into(new HashSet<>());
result = localCollection.deleteMany(filter);
for (final BsonValue documentId : idsToDelete) {
final CoreDocumentSynchronizationConfig config =
syncConfig.getSynchronizedDocument(namespace, documentId);
if (config == null) {
continue;
}
final ChangeEvent<BsonDocument> event =
ChangeEvents.changeEventForLocalDelete(namespace, documentId, true);
// this block is to trigger coalescence for a delete after insert
if (config.getLastUncommittedChangeEvent() != null
&& config.getLastUncommittedChangeEvent().getOperationType()
== OperationType.INSERT) {
desyncDocumentsFromRemote(nsConfig, config.getDocumentId())
.commitAndClear();
undoCollection.deleteOne(getDocumentIdFilter(documentId));
continue;
}
config.setSomePendingWritesAndSave(logicalT, event);
undoCollection.deleteOne(getDocumentIdFilter(documentId));
eventsToEmit.add(event);
}
checkAndDeleteNamespaceListener(namespace);
} finally {
lock.unlock();
}
for (final ChangeEvent<BsonDocument> event : eventsToEmit) {
eventDispatcher.emitEvent(nsConfig, event);
}
return result;
} finally {
ongoingOperationsGroup.exit();
}
} | java |
MongoCollection<BsonDocument> getUndoCollection(final MongoNamespace namespace) {
return localClient
.getDatabase(String.format("sync_undo_%s", namespace.getDatabaseName()))
.getCollection(namespace.getCollectionName(), BsonDocument.class)
.withCodecRegistry(MongoClientSettings.getDefaultCodecRegistry());
} | java |
private <T> MongoCollection<T> getLocalCollection(
final MongoNamespace namespace,
final Class<T> resultClass,
final CodecRegistry codecRegistry
) {
return localClient
.getDatabase(String.format("sync_user_%s", namespace.getDatabaseName()))
.getCollection(namespace.getCollectionName(), resultClass)
.withCodecRegistry(codecRegistry);
} | java |
MongoCollection<BsonDocument> getLocalCollection(final MongoNamespace namespace) {
return getLocalCollection(
namespace,
BsonDocument.class,
MongoClientSettings.getDefaultCodecRegistry());
} | java |
private <T> CoreRemoteMongoCollection<T> getRemoteCollection(
final MongoNamespace namespace,
final Class<T> resultClass
) {
return remoteClient
.getDatabase(namespace.getDatabaseName())
.getCollection(namespace.getCollectionName(), resultClass);
} | java |
static BsonDocument sanitizeDocument(final BsonDocument document) {
if (document == null) {
return null;
}
if (document.containsKey(DOCUMENT_VERSION_FIELD)) {
final BsonDocument clonedDoc = document.clone();
clonedDoc.remove(DOCUMENT_VERSION_FIELD);
return clonedDoc;
}
return document;
} | java |
private static BsonDocument withNewVersion(
final BsonDocument document,
final BsonDocument newVersion
) {
final BsonDocument newDocument = BsonUtils.copyOfDocument(document);
newDocument.put(DOCUMENT_VERSION_FIELD, newVersion);
return newDocument;
} | java |
public static void clearallLocalDBs() {
for (final Map.Entry<MongoClient, Boolean> entry : localInstances.entrySet()) {
for (final String dbName : entry.getKey().listDatabaseNames()) {
entry.getKey().getDatabase(dbName).drop();
}
}
} | java |
public StitchObjectMapper withCodecRegistry(final CodecRegistry codecRegistry) {
// We can't detect if their codecRegistry has any duplicate providers. There's also a chance
// that putting ours first may prevent decoding of some of their classes if for example they
// have their own way of decoding an Integer.
final CodecRegistry newReg =
CodecRegistries.fromRegistries(BsonUtils.DEFAULT_CODEC_REGISTRY, codecRegistry);
return new StitchObjectMapper(this, newReg);
} | java |
public StitchEvent<T> nextEvent() throws IOException {
final Event nextEvent = eventStream.nextEvent();
if (nextEvent == null) {
return null;
}
return StitchEvent.fromEvent(nextEvent, this.decoder);
} | java |
@Nullable
public StitchUserT getUser() {
authLock.readLock().lock();
try {
return activeUser;
} finally {
authLock.readLock().unlock();
}
} | java |
private synchronized Response doAuthenticatedRequest(
final StitchAuthRequest stitchReq,
final AuthInfo authInfo
) {
try {
return requestClient.doRequest(prepareAuthRequest(stitchReq, authInfo));
} catch (final StitchServiceException ex) {
return handleAuthFailure(ex, stitchReq);
}
} | java |
private void tryRefreshAccessToken(final Long reqStartedAt) {
authLock.writeLock().lock();
try {
if (!isLoggedIn()) {
throw new StitchClientException(StitchClientErrorCode.LOGGED_OUT_DURING_REQUEST);
}
try {
final Jwt jwt = Jwt.fromEncoded(getAuthInfo().getAccessToken());
if (jwt.getIssuedAt() >= reqStartedAt) {
return;
}
} catch (final IOException e) {
// Swallow
}
// retry
refreshAccessToken();
} finally {
authLock.writeLock().unlock();
}
} | java |
private StitchUserT doLogin(final StitchCredential credential, final boolean asLinkRequest) {
final Response response = doLoginRequest(credential, asLinkRequest);
final StitchUserT previousUser = activeUser;
final StitchUserT user = processLoginResponse(credential, response, asLinkRequest);
if (asLinkRequest) {
onUserLinked(user);
} else {
onUserLoggedIn(user);
onActiveUserChanged(activeUser, previousUser);
}
return user;
} | java |
private static Interval parseStartExtended(CharSequence startStr, CharSequence endStr) {
Instant start = Instant.parse(startStr);
if (endStr.length() > 0) {
char c = endStr.charAt(0);
if (c == 'P' || c == 'p') {
PeriodDuration amount = PeriodDuration.parse(endStr);
// addition of PeriodDuration only supported by OffsetDateTime,
// but to make that work need to move point being added to closer to EPOCH
long move = start.isBefore(Instant.EPOCH) ? 1000 * 86400 : -1000 * 86400;
Instant end = start.plusSeconds(move).atOffset(ZoneOffset.UTC).plus(amount).toInstant().minusSeconds(move);
return Interval.of(start, end);
}
}
// infer offset from start if not specified by end
return parseEndDateTime(start, ZoneOffset.UTC, endStr);
} | java |
private static Interval parseEndDateTime(Instant start, ZoneOffset offset, CharSequence endStr) {
try {
TemporalAccessor temporal = DateTimeFormatter.ISO_DATE_TIME.parseBest(endStr, OffsetDateTime::from, LocalDateTime::from);
if (temporal instanceof OffsetDateTime) {
OffsetDateTime odt = (OffsetDateTime) temporal;
return Interval.of(start, odt.toInstant());
} else {
// infer offset from start if not specified by end
LocalDateTime ldt = (LocalDateTime) temporal;
return Interval.of(start, ldt.toInstant(offset));
}
} catch (DateTimeParseException ex) {
Instant end = Instant.parse(endStr);
return Interval.of(start, end);
}
} | java |
@Override
public AccountingDate date(int prolepticYear, int month, int dayOfMonth) {
return AccountingDate.of(this, prolepticYear, month, dayOfMonth);
} | java |
@Override
public AccountingDate dateYearDay(Era era, int yearOfEra, int dayOfYear) {
return dateYearDay(prolepticYear(era, yearOfEra), dayOfYear);
} | java |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.