icefiredb_ipfs_log

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 23, 2024 License: Apache-2.0 Imports: 38 Imported by: 2

README

icefiredb-ipfs-log

icefiredb-ipfs-log is a distributed immutable, operation-based conflict-free replication data structure that relies on ipfs to store data and merges each peer node data based on pubsub conflict-free. You can easily implement custom data structures such as kv, event, nosql, etc. based on icefiredb-ipfs-log.

Conflict-free log replication model
           Log A                Log B
             |                    |
     logA.append("one")   logB.append("hello")
             |                    |
             v                    v
          +-----+             +-------+
          |"one"|             |"hello"|
          +-----+             +-------+
             |                    |
     logA.append("two")   logB.append("world")
             |                    |
             v                    v
       +-----------+       +---------------+
       |"one","two"|       |"hello","world"|
       +-----------+       +---------------+
             |                    |
             |                    |
       logA.join(logB) <----------+
             |
             v
+---------------------------+
|"one","hello","two","world"|
+---------------------------+
Example of building a key-value database using icefiredb-ipfs-log
Use of key-value databases

Detailed usage example reference

func main() {
    ctx := context.TODO()
    // disk cache directory
    rootPath := "./kvdb"
    node, api, err := iflog.CreateNode(ctx, rootPath)
    if err != nil {
        panic(err)
    }
	
    hostAddr, _ := ma.NewMultiaddr(fmt.Sprintf("/ipfs/%s", node.PeerHost.ID().Pretty()))
    for _, a := range node.PeerHost.Addrs() {
        fmt.Println(a.Encapsulate(hostAddr).String())
    }
    
    log := zap.NewNop()
    dbname := "iflog-event-kv"
    ev, err := iflog.NewIpfsLog(ctx, api, dbname, &iflog.EventOptions{
        Directory: rootPath,
        Logger:    log,
    })
    if err != nil {
        panic(err)
    }
	
    if err := ev.AnnounceConnect(ctx, node); err != nil {
        panic(err)
    }
    kvdb, err := kv.NewKeyValueDB(ctx, ev, log)
    if err != nil {
        panic(err)
    }
    // Load old data from disk
    if err := ev.LoadDisk(ctx); err != nil {
        panic(err)
    }
    kvdb.Put(ctx, "one", "one")
    kvdb.Get("one")
    kvdb.Delete(ctx, "one")
}
Some code reference sources

License

icefiredb-ipfs-log is under the Apache 2.0 license. See the LICENSE directory for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateNode

func CreateNode(ctx context.Context, repoPath string) (*core.IpfsNode, ifGoIpfsCore.CoreAPI, error)

func SetupPlugins

func SetupPlugins(path string) error

Types

type DataLogEvent

type DataLogEvent struct {
	Payload []byte
}

func NewDataLogEvent

func NewDataLogEvent(b []byte) DataLogEvent

type EventOptions

type EventOptions struct {
	Directory     string
	Keystore      keystore.Interface
	CloseKeystore func() error
	Logger        *zap.Logger
	Identity      *idp.Identity
	IpfsLog       ipfslog.Log
	Pubsub        pubsub.PubSubInterface
	IO            ipfslog.IO
	Cache         datastore.Datastore
}

type IpfsLog

type IpfsLog struct {
	// contains filtered or unexported fields
}

func NewIpfsLog

func NewIpfsLog(ctx context.Context, ipfs iface.CoreAPI, dbname string, options *EventOptions) (*IpfsLog, error)

func (*IpfsLog) AnnounceConnect

func (ev *IpfsLog) AnnounceConnect(ctx context.Context, node *ipfscore.IpfsNode) error

func (*IpfsLog) Append

func (ev *IpfsLog) Append(ctx context.Context, payload []byte) (cid.Cid, error)

func (*IpfsLog) Close

func (ev *IpfsLog) Close() error

func (*IpfsLog) Get

func (ev *IpfsLog) Get(cid cid.Cid) []byte

func (*IpfsLog) List

func (ev *IpfsLog) List() [][]byte

func (*IpfsLog) LoadDisk

func (ev *IpfsLog) LoadDisk(ctx context.Context) error

Load disk cache data

func (*IpfsLog) WatchWriteEvent

func (ev *IpfsLog) WatchWriteEvent(ctx context.Context) (<-chan []byte, error)

type LocalWrite

type LocalWrite struct {
	Heads []*entry.Entry
}

func NewLocalWrite

func NewLocalWrite(heads []*entry.Entry) LocalWrite

func (*LocalWrite) Marshal

func (m *LocalWrite) Marshal() []byte

type RemoteWrite

type RemoteWrite struct {
	From  core.PeerID
	Heads []*entry.Entry
}

func NewRemoteWrite

func NewRemoteWrite(from core.PeerID, heads []*entry.Entry) RemoteWrite

func (*RemoteWrite) Marshal

func (m *RemoteWrite) Marshal() []byte

Directories

Path Synopsis
example
stores
kv

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL