berty / go-orbit-db

Go version of P2P Database on IPFS
https://berty.tech
Apache License 2.0
422 stars 57 forks source link

Add an example, and link it from the README #5

Open moul opened 5 years ago

jryebread commented 3 years ago

+1, just came across this awesome package and would be great to see an example with how joins work.

diyism commented 2 years ago

A complete but not so simple example: https://github.com/mrusme/superhighway84/blob/master/database/database.go

GitHub
superhighway84/database.go at master · mrusme/superhighway84
USENET-inspired, uncensorable, decentralized internet discussion system running on IPFS & OrbitDB - superhighway84/database.go at master · mrusme/superhighway84
diyism commented 2 years ago

I'm trying to figure out a simplest go-orbit-db example code (test.go):

package main

import
(   "context"
    "time"
    "fmt"
    goOrbitDb "berty.tech/go-orbit-db"
    ifGoIpfsCore "github.com/ipfs/interface-go-ipfs-core"
    //"go.uber.org/zap"
    "berty.tech/go-orbit-db/stores/documentstore"
    "berty.tech/go-orbit-db/accesscontroller"
    "berty.tech/go-orbit-db/iface"
    "github.com/ipfs/go-ipfs/core"
    "github.com/ipfs/go-ipfs/repo/fsrepo"
    "github.com/ipfs/go-ipfs/core/node/libp2p"
    "github.com/ipfs/go-ipfs/core/coreapi"
)

func main()
{   CachePath:="./db_cache"
    storetype:="docstore"
    ac:=&accesscontroller.CreateAccessControllerOptions{Access: map[string][]string{"write": {"*"}}}

    //var Logger *zap.Logger
    var ConnectionString string="/orbitdb/bafyreifdpagppa7ve45odxuvudz5snbzcybwyfer777huckl4li4zbc5k4/superhighway84"

    ctx,cancel := context.WithTimeout(context.Background(), 600*time.Second)
    defer cancel()

    _,ifIpfs,err := createNode(ctx, "./")

    //Logger.Debug("initializing NewOrbitDB ...")
    OrbitDB,err := goOrbitDb.NewOrbitDB(    ctx,
                                            ifIpfs,
                                            &goOrbitDb.NewOrbitDBOptions{   Directory:&CachePath},
                                        )
    if err!=nil {   fmt.Printf("%+v", err); return}

    //Logger.Debug("initializing OrbitDB.Docs ...")
    Store,err := OrbitDB.Docs(  ctx,
                                ConnectionString,
                                &goOrbitDb.CreateDBOptions{ AccessController: ac,
                                                            StoreType: &storetype,
                                                            StoreSpecificOpts: documentstore.DefaultStoreOptsForMap("id"),
                                                            Timeout: time.Second*600,
                                                        },
                            )
    if err != nil { fmt.Printf("%+v", err); return}

    doc1 := map[string]interface{}{"_id": "doc1", "hello": "world"}
    _, err = Store.Put(ctx, doc1)
    fmt.Printf("%+v", "jack")

    docs, err := Store.Get(ctx, "doc1", &iface.DocumentStoreGetOptions{CaseInsensitive: false})
    fmt.Printf("%+v", docs)
}

func createNode(ctx context.Context, repoPath string) (*core.IpfsNode, ifGoIpfsCore.CoreAPI, error) {
  repo, err := fsrepo.Open(repoPath)
  if err != nil {
    return nil, nil, err
  }

  nodeOptions := &core.BuildCfg{
    Online:  true,
    Routing: libp2p.DHTClientOption, // DHTOption
    Repo: repo,
    ExtraOpts: map[string]bool{
      "pubsub": true,
    },
  }

  node, err := core.NewNode(ctx, nodeOptions)
  if err != nil {
    return nil, nil, err
  }

  coreAPI, err := coreapi.NewCoreAPI(node)
  if err != nil {
    return nil, nil, err
  }

  return node, coreAPI, nil
}

// the go.mod file:

module test
go 1.18
require (
    github.com/ipfs/interface-go-ipfs-core v0.5.2
)
require (
    github.com/lucas-clemente/quic-go v0.26.0 // indirect
)

// must lock the interface-go-ipfs-core version, or else show error ""undefined: blockstore.ErrNotFound"" $ go mod tidy $ export IPFS_PATH=./ $ ipfs init $ go run test.go

No luck , it shows:

unknown datastore type: flatfs
mp-netis commented 2 years ago

@diyism I had the same issue until I added IPFS Plugins. It seems flatfs is implemented as a builtin plugin, so you need to include plugin support. Any luck creating a working example?

flowpoint commented 2 years ago

based on the above, i bolted together a dirty example. no guarantees. https://gist.github.com/flowpoint/0643f7239920387e0de8e335fdbd18dd

Gist
dirty example to use go-orbitdb eventlog
dirty example to use go-orbitdb eventlog. GitHub Gist: instantly share code, notes, and snippets.
jefft0 commented 1 year ago

When I run the example in the gist, it prints:

Computed default go-libp2p Resource Manager limits based on:
    - 'Swarm.ResourceMgr.MaxMemory': "8.6 GB"
    - 'Swarm.ResourceMgr.MaxFileDescriptors': 30720

Theses can be inspected with 'ipfs swarm resources'.

bafyreigotslg6nydukp3eqhybnhnkottgtjgrhqyd6hklz23j6qdn4w5cu&{Key:<nil> Op:ADD Value:[1 2] Docs:[] Entry:0x1400210c780}%
jefft0 commented 9 months ago

Keep this open as a tracking issue for adding examples. Put in backlog until we have more time to work on it.