Path: ...!2.eu.feeder.erje.net!feeder.erje.net!weretis.net!feeder8.news.weretis.net!feeder1.feed.usenet.farm!feed.usenet.farm!peer01.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!fx12.ams4.POSTED!not-for-mail MIME-Version: 1.0 User-Agent: NoZilla/3.11 (Hackint; Unicorn; rv:0.8.15) go-while/19720229 NewsRW/4.2.0 Subject: New Readme: github.com/go-while/nntp-mongodb-storage Content-Language: en-US Newsgroups: news.software.nntp References: <8BTyM.407874$8uM.372258@fx11.ams4> From: "Billy G. (go-while)" Organization: github.com/go-while In-Reply-To: <8BTyM.407874$8uM.372258@fx11.ams4> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Lines: 577 Message-ID: <8aDzM.299994$6Li6.167647@fx12.ams4> X-Complaints-To: abuse@blocknews.net NNTP-Posting-Date: Sun, 06 Aug 2023 02:01:40 UTC Date: Sun, 6 Aug 2023 04:50:19 +0200 X-Received-Bytes: 23876 Bytes: 24113 https://github.com/go-while/nntp-mongodb-storage nntp-mongodb-storage MongoDB Storage Package for Usenet Articles The mongostorage package offers a set of convenient functions for interacting with a MongoDB database and managing Usenet articles. It provides efficient and reliable methods for inserting, deleting, and retrieving Usenet articles in a MongoDB collection. With the mongostorage package, you can easily store, manage, and query Usenet articles in a MongoDB database, making it a valuable tool for any application that deals with Usenet data. Key Functions: mongostorage.Func(...) InsertOneArticle: Inserts a single article into the MongoDB collection. InsertManyArticles: Performs a bulk insert of multiple articles into the MongoDB collection. IsDup: Checks if an error is a duplicate key error in MongoDB. DeleteManyArticles: Deletes multiple articles from the MongoDB collection based on a list of MessageIDHashes. DeleteArticlesByMessageIDHash: Deletes an article from the MongoDB collection by its MessageIDHash. RetrieveArticleByMessageIDHash: Retrieves an article from the MongoDB collection by its MessageIDHash. RetrieveArticlesByMessageIDHashes: Retrieves articles from the MongoDB collection based on a list of MessageIDHashes. RetrieveHeadByMessageIDHash: Retrieves the "Head" data of an article based on its MessageIDHash. RetrieveBodyByMessageIDHash: Retrieves the "Body" data of an article based on its MessageIDHash. CheckIfArticleExistsByMessageIDHash: Checks if an article exists in the MongoDB collection based on its MessageIDHash. Using External Context The functions in the mongostorage package accept a ctx context.Context and mongo options, allowing you to provide your own MongoDB context from the main program. In Go, the context.Context is used to manage the lifecycle of operations and carry deadlines, cancellation signals, and other request-scoped values across API boundaries. It ensures the graceful termination of operations and handling of long-running tasks. To use these functions with an external context, you can create your MongoDB client and collection in the main program and pass the context.Context to the functions. This allows the MongoDB operations to be context-aware and respect the context's timeout and cancellation signals. Extending Context Timeout If you need to extend the timeout of the context for specific operations, you can use the ExtendContextTimeout function provided by the package. Here's an example of how to use it: // Extending the context timeout for a specific operation newCtx, newCancel := mongostorage.ExtendContextTimeout(ctx, cancel, 20) // defer newCancel() <- defer is only needed if you dont call ExtendContextTimeout again. pass `cancel` to ExtendContextTimeout to cancel there. // Now use the newCtx for the MongoDB operation Here's an example of how you can use the mongostorage functions with an external context: package main import ( "context" "log" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" "yourproject/mongostorage" ) func main() { // Create a MongoDB client with a timeout for connecting to the server client, err := mongo.Connect(context.Background(), options.Client().ApplyURI("mongodb://localhost:27017").SetConnectTimeout(5*time.Second)) if err != nil { log.Fatalf("Error connecting to MongoDB: %v", err) } defer client.Disconnect(context.Background()) // Get a reference to the MongoDB collection collection := client.Database("yourdbname").Collection("yourcollectionname") // Create a context with a timeout and pass it to the functions in the mongostorage package ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() // Example of inserting a single article article := &mongostorage.MongoArticle{ MessageIDHash: strPtr("hash1"), MessageID: strPtr("msgid1"), Newsgroups: []string{"group1", "group2"}, Head: []byte("This is the head of article 1"), Headsize: 100, Body: []byte("This is the body of article 1"), Bodysize: 200, Enc: 0, // not compressed } err = mongostorage.InsertOneArticle(ctx, collection, article) if err != nil { log.Printf("Error inserting article: %v", err) } // Example of inserting multiple articles in bulk articles := []*mongostorage.MongoArticle{ { MessageIDHash: strPtr("hash1"), MessageID: strPtr("msgid1"), Newsgroups: []string{"group1", "group2"}, Head: []byte("This is the head of article 1"), Headsize: 100, Body: []byte("This is the body of article 1"), Bodysize: 200, Enc: 0, // not compressed }, { MessageIDHash: strPtr("hash1"), // will generate a duplicate error MessageID: strPtr("msgid1"), Newsgroups: []string{"group1", "group2"}, Head: []byte("This is the head of article 1"), Headsize: 100, Body: []byte("This is the body of article 1"), Bodysize: 200, Enc: 1, // indicator: compressed with GZIP (sender has to apply de/compression) }, { MessageIDHash: strPtr("hash2"), MessageID: strPtr("msgid2"), Newsgroups: []string{"group3", "group4"}, Head: []byte("This is the head of article 2"), Headsize: 150, Body: []byte("This is the body of article 2"), Bodysize: 300, Enc: 2, // indicator: compressed with ZLIB (sender has to apply de/compression) }, // Add more articles as needed } err = mongostorage.InsertManyArticles(ctx, collection, articles) if err != nil { log.Printf("Error inserting articles: %v", err) } else { log.Println("Articles inserted successfully.") } // Check if the error is a duplicate key error using the IsDup function. if IsDup(err) { log.Println("Duplicate key error: The document already exists.") // Handle the duplicate key error here, if needed. } else { log.Println("Other error occurred:", err) ========== REMAINDER OF ARTICLE TRUNCATED ==========