Coder Social home page Coder Social logo

sharding about lifti HOT 3 CLOSED

Jacknq avatar Jacknq commented on July 24, 2024
sharding

from lifti.

Comments (3)

Jacknq avatar Jacknq commented on July 24, 2024 1

Ok, thanks for hints, interesting stuff!

from lifti.

mikegoatly avatar mikegoatly commented on July 24, 2024

@Jacknq There's nothing built into the index implicitly and no current plans - it's not something I'd considered to be honest.

Off the top of my head, the most pragmatic way to do this would be with some sort of wrapper around a Dictionary of indexes, each key being a different partition - that's probably something that could be managed outside of the framework itself in the short term.

from lifti.

mikegoatly avatar mikegoatly commented on July 24, 2024

Something like this (completely untested!)

using Lifti;
using Lifti.Serialization.Binary;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

public class ShardedIndex
        {
            private static readonly BinarySerializer<int> serializer = new BinarySerializer<int>();
            private readonly Dictionary<string, FullTextIndex<int>> indexShards = new Dictionary<string, FullTextIndex<int>>();
            private readonly SemaphoreSlim syncObject = new SemaphoreSlim(1);

            public async Task<FullTextIndex<int>> GetIndexAsync(string partitionKey, CancellationToken cancellationToken = default)
            {
                if (!await syncObject.WaitAsync(TimeSpan.FromSeconds(1), cancellationToken))
                {
                    throw new Exception("Timeout waiting for lock");
                }

                try
                {
                    if (!indexShards.TryGetValue(partitionKey, out var index))
                    {
                        // Create the index for this shard
                        index = new FullTextIndexBuilder<int>()
                                // e.g. shard peristance using the partition key as the file name
                                .WithIndexModificationAction(async (idx) =>
                                {
                                    using (var fileStream = File.OpenWrite($"{partitionKey}.dat"))
                                    {
                                        await serializer.SerializeAsync(idx, fileStream);
                                    }
                                })
                                .Build();

                        // Deserialize initial state, if appropriate
                        var serializedFile = new FileInfo($"{partitionKey}.dat");
                        if (serializedFile.Exists)
                        {
                            using (var stream = serializedFile.OpenRead())
                            {
                                await serializer.DeserializeAsync(index, stream);
                            }
                        }

                        // Store the index for the next access
                        indexShards[partitionKey] = index;
                    }

                    return index;
                }
                finally
                {
                    syncObject.Release();
                }
            }
        }

There may be a more optimal solution - just food for thought, more than anything.

from lifti.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.