Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(mongo): Remove useless ensureIdSegment #620

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,4 @@ public interface IdSegmentCollection {
String COLLECTION_NAME = "cosid";

long incrementAndGet(String namespacedName, long step);

boolean ensureIdSegment(String segmentName, long offset);

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@

package me.ahoo.cosid.mongo;

import static me.ahoo.cosid.mongo.IdSegmentOperates.ensureIdSegmentDocument;
import static me.ahoo.cosid.mongo.IdSegmentOperates.incrementAndGetUpdates;

import com.google.common.base.Preconditions;
import com.mongodb.MongoWriteException;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Filters;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -45,22 +43,4 @@ public long incrementAndGet(String namespacedName, long step) {
Long lastMaxId = afterDoc.getLong(IdSegmentOperates.LAST_MAX_ID_FIELD);
return Objects.requireNonNull(lastMaxId);
}

@Deprecated
@Override
public boolean ensureIdSegment(String segmentName, long offset) {
if (log.isInfoEnabled()) {
log.info("Ensure IdSegment:[{}]", segmentName);
}
try {
Document document = ensureIdSegmentDocument(segmentName, offset);
cosidCollection.insertOne(document);
return true;
} catch (MongoWriteException mongoWriteException) {
if (log.isInfoEnabled()) {
log.info("Ensure IdSegment:[{}] Failed", segmentName, mongoWriteException);
}
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@

package me.ahoo.cosid.mongo.reactive;

import static me.ahoo.cosid.mongo.IdSegmentOperates.ensureIdSegmentDocument;
import static me.ahoo.cosid.mongo.IdSegmentOperates.incrementAndGetUpdates;

import me.ahoo.cosid.mongo.Documents;
import me.ahoo.cosid.mongo.IdSegmentCollection;
import me.ahoo.cosid.mongo.IdSegmentOperates;

import com.google.common.base.Preconditions;
import com.mongodb.MongoWriteException;
import com.mongodb.client.model.Filters;
import com.mongodb.client.result.InsertOneResult;
import com.mongodb.reactivestreams.client.MongoCollection;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;
Expand Down Expand Up @@ -52,22 +49,4 @@ public long incrementAndGet(String namespacedName, long step) {
Long lastMaxId = afterDoc.getLong(IdSegmentOperates.LAST_MAX_ID_FIELD);
return Objects.requireNonNull(lastMaxId);
}

@Override
public boolean ensureIdSegment(String segmentName, long offset) {
if (log.isInfoEnabled()) {
log.info("Ensure IdSegment:[{}]", segmentName);
}
try {
Document document = ensureIdSegmentDocument(segmentName, offset);
Publisher<InsertOneResult> publisher = cosidCollection.insertOne(document);
BlockingAdapter.block(publisher);
return true;
} catch (MongoWriteException mongoWriteException) {
if (log.isInfoEnabled()) {
log.info("Ensure IdSegment:[{}] Failed:[{}]", segmentName, mongoWriteException.getMessage());
}
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,10 @@

package me.ahoo.cosid.mongo;

import static org.hamcrest.MatcherAssert.assertThat;

import me.ahoo.cosid.test.MockIdGenerator;
import me.ahoo.cosid.test.container.MongoLauncher;

import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoDatabase;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -40,13 +36,4 @@ void setup() {
void ensureCosIdCollection() {
idSegmentInitializer.ensureCosIdCollection();
}

@Test
void ensureIdSegment() {
String namespace = MockIdGenerator.INSTANCE.generateAsString();
boolean actual = cosIdSegmentCollection.ensureIdSegment(namespace, 0);
assertThat(actual, Matchers.equalTo(true));
actual = cosIdSegmentCollection.ensureIdSegment(namespace, 0);
assertThat(actual, Matchers.equalTo(false));
}
}
Loading