Skip to content

Commit

Permalink
feat(mongo): Remove useless ensureIdSegment (#620)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahoo-Wang authored Aug 22, 2024
1 parent e01c034 commit 08673da
Show file tree
Hide file tree
Showing 4 changed files with 0 additions and 57 deletions.
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));
}
}

0 comments on commit 08673da

Please sign in to comment.