gouchstore

package module
v0.0.0-...-ebc3bea Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2015 License: Apache-2.0 Imports: 13 Imported by: 2

README

gouchstore

A native go library for working with couchstore files.

NOTE: This library is relatively new, use at your own risk.

Using

go get github.com/mschoch/gouchstore

Example

To open a database and fetch a key:

db, err := gouchstore.Open("database.couch")
handleError(err)
doc, err := db.DocumentById("docid")
handleError(err)

Documentation

See the full documentation

Utilities

There are also a few utility programs available in the utils directory.

  • gsdbinfo - prints the database info for a couchstore file

      $ gsdbinfo test/couchbase_beer_sample_vbucket.couch 
      {
        "fileName": "/Users/mschoch/go/src/github.com/mschoch/gouchstore/test/couchbase_beer_sample_vbucket.couch",
        "lastSeq": 101,
        "documentCount": 101,
        "deletedCount": 0,
        "spaceUsed": 43757,
        "FileSize": 233563,
        "HeaderPosition": 233472
      }
    
  • gsdblist - prints list of document info for documents with ids in the specified id range or the specified sequence range

      $ gsdblist -startId ab -endId ac test/couchbase_beer_sample_vbucket.couch
      {
        "id": "abita_brewing_company-s_o_s",
        "seq": 1,
        "rev": 1,
        "revMeta": "AAAEDEOgB8AAAAAAAAAAAA==",
        "contentMeta": 128,
        "deleted": false,
        "size": 314
      }
      Listed 1 documents
      $ gsdblist -startSeq 101 -endSeq 101 test/couchbase_beer_sample_vbucket.couch 
      {
        "id": "zea_rotisserie_and_brewery-clearview_light",
        "seq": 101,
        "rev": 1,
        "revMeta": "AAAEENA2njwAAAAAAAAAAA==",
        "contentMeta": 128,
        "deleted": false,
        "size": 167
      }
      Listed 1 documents
    
  • gsdbget - fetch individual documents and print the info and/or body

      $ gsdbget test/couchbase_beer_sample_vbucket.couch lion_brewery_ceylon_ltd
      Document Info:
      {
        "id": "lion_brewery_ceylon_ltd",
        "seq": 50,
        "rev": 1,
        "revMeta": "AAAEDr5gV/IAAAAAAAAAAA==",
        "contentMeta": 128,
        "deleted": false,
        "size": 310
      }
      Document Body:
      {"name":"Lion Brewery Ceylon Ltd.","city":"Colombo","state":"","code":"","country":"Sri Lanka","phone":"94-331535-42","website":"http://www.lionbeer.com/","type":"brewery","updated":"2010-07-22 20:00:20","description":"","address":["No-254, Colombo Road"],"geo":{"accuracy":"APPROXIMATE","lat":38.7548,"lon":-9.1883}}
    
  • gsdbcompact - compact a couchstore file

      $  gsdbcompact original.couch compacted.couch
    

Build Status

Build Status

Code Coverage

Coverage Status

Documentation

Overview

Package gouchstore implements native Go access to couchstore files. See https://github.com/couchbase/couchstore for more information about couchstore.

Usage:

db, err := gouchstore.Open("database.couch")

To fetch a document by ID:

doc, err := db.DocumentById("docid")

To list all keys, create a callback:

func callback(g *gouchstore.Gouchstore, docInfo *gouchstore.DocumentInfo, userContext interface{}) {
	fmt.Printf("ID: %s", docInfo.ID)
}

Then:

err = db.AllDocuments("", "", callback, nil)

Index

Constants

View Source
const (
	COMPACT_KEEP_ITEM int = 0
	COMPACT_DROP_ITEM int = 1
)
View Source
const (
	OPEN_CREATE int = 1
	OPEN_RDONLY int = 2
)
View Source
const (
	DOC_IS_COMPRESSED byte = 128
)
View Source
const ID_SORT_CHUNK_SIZE = (10 * 1024 * 1024) // 10MB. Make tuneable?

FIXME this isn't really MB right? its a count of items, not their size

Variables

This section is empty.

Functions

This section is empty.

Types

type BaseGouchOps

type BaseGouchOps struct{}

func NewBaseGouchOps

func NewBaseGouchOps() *BaseGouchOps

func (*BaseGouchOps) Close

func (g *BaseGouchOps) Close(f *os.File) error

func (*BaseGouchOps) CompactionTreeWriter

func (g *BaseGouchOps) CompactionTreeWriter(keyCompare btreeKeyComparator, reduce, rereduce reduceFunc, reduceContext interface{}) (TreeWriter, error)

func (*BaseGouchOps) GotoEOF

func (g *BaseGouchOps) GotoEOF(f *os.File) (ret int64, err error)

func (*BaseGouchOps) OpenFile

func (g *BaseGouchOps) OpenFile(name string, flag int, perm os.FileMode) (file *os.File, err error)

func (*BaseGouchOps) ReadAt

func (g *BaseGouchOps) ReadAt(f *os.File, b []byte, off int64) (n int, err error)

func (*BaseGouchOps) SnappyDecode

func (g *BaseGouchOps) SnappyDecode(dst, src []byte) ([]byte, error)

func (*BaseGouchOps) SnappyEncode

func (g *BaseGouchOps) SnappyEncode(dst, src []byte) []byte

func (*BaseGouchOps) Sync

func (g *BaseGouchOps) Sync(f *os.File) error

func (*BaseGouchOps) WriteAt

func (g *BaseGouchOps) WriteAt(f *os.File, b []byte, off int64) (n int, err error)

type BulkWriter

type BulkWriter interface {
	// Set a document.
	Set(*DocumentInfo, *Document)
	// Delete a document.
	Delete(*DocumentInfo)
	// Commit the current batch.
	Commit() error
	// Shut down this bulk interface.
	Close() error
}

type DatabaseInfo

type DatabaseInfo struct {
	FileName       string `json:"fileName"`       // filesystem path
	LastSeq        uint64 `json:"lastSeq"`        // last sequence number allocated
	DocumentCount  uint64 `json:"documentCount"`  // total number of (non-deleted) documents
	DeletedCount   uint64 `json:"deletedCount"`   // total number of deleted documents
	SpaceUsed      uint64 `json:"spaceUsed"`      // disk space actively used by docs
	FileSize       uint64 `json:"fileSize"`       // total disk space used by database
	HeaderPosition uint64 `json:"headerPosition"` // file offset of current header
}

DatabaseInfo describes the database as a whole.

type Document

type Document struct {
	ID   string
	Body []byte
}

Document represents a document stored in the database.

func NewDocument

func NewDocument(id string, value []byte) *Document

type DocumentInfo

type DocumentInfo struct {
	ID          string `json:"id"`          // document identifier
	Seq         uint64 `json:"seq"`         // sequence number in database
	Rev         uint64 `json:"rev"`         // revision number of document
	RevMeta     []byte `json:"revMeta"`     // additional revision meta-data (uninterpreted by Gouchstore)
	ContentMeta uint8  `json:"contentMeta"` // content meta-data flags
	Deleted     bool   `json:"deleted"`     // is the revision deleted?
	Size        uint64 `json:"size"`        // size of document data in bytes
	// contains filtered or unexported fields
}

DocumentInfo is document meta-data.

func NewDocumentInfo

func NewDocumentInfo(id string) *DocumentInfo

func (*DocumentInfo) String

func (di *DocumentInfo) String() string

func (*DocumentInfo) WriteIDTo

func (di *DocumentInfo) WriteIDTo(w io.Writer) (int, error)

type DocumentInfoCallback

type DocumentInfoCallback func(gouchstore *Gouchstore, documentInfo *DocumentInfo, userContext interface{}) error

DocumentInfoCallback is a function definition which is used for document iteration. For example, when iterating all documents with the AllDocuments() method, the provided callback will be invoked for each document in the database.

type DocumentWalkFun

type DocumentWalkFun func(db *Gouchstore, di *DocumentInfo, doc *Document) error

type GouchOps

type GouchOps interface {
	OpenFile(name string, flag int, perm os.FileMode) (file *os.File, err error)
	ReadAt(f *os.File, b []byte, off int64) (n int, err error)
	WriteAt(f *os.File, b []byte, off int64) (n int, err error)
	GotoEOF(f *os.File) (ret int64, err error)
	Sync(f *os.File) error
	CompactionTreeWriter(keyCompare btreeKeyComparator, reduce, rereduce reduceFunc, reduceContext interface{}) (TreeWriter, error)
	SnappyEncode(dst, src []byte) []byte
	SnappyDecode(dst, src []byte) ([]byte, error)
	Close(f *os.File) error
}

GouchOps an interface for plugging in differentl implementations of some common low-level operations

type Gouchstore

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

Gouchstore gives access to a couchstore database file.

func Open

func Open(filename string, options int) (*Gouchstore, error)

Open attemps to open an existing couchstore file.

All Gouchstore files successfully opened should be closed with the Close() method.

func OpenEx

func OpenEx(filename string, options int, ops GouchOps) (*Gouchstore, error)

func (*Gouchstore) AllDocuments

func (g *Gouchstore) AllDocuments(startId, endId string, cb DocumentInfoCallback, userContext interface{}) error

AllDocuments will iterate through all documents in the database in ascending ID order, from startId (inclusive) through endId (inclusive). For each document, the provided DocumentInfoCallback will be invoked. A user specified context can be included, and this will be passed to each invocation of the callback.

If startId is the empty string, the iteration will start with the first document.

If endId is the empty string, the iteration will continue to the last document.

func (*Gouchstore) Bulk

func (db *Gouchstore) Bulk() BulkWriter

Get a bulk writer.

You must call Close() on the bulk writer when you're done bulk writing.

func (*Gouchstore) ChangesSince

func (g *Gouchstore) ChangesSince(since uint64, till uint64, cb DocumentInfoCallback, userContext interface{}) error

ChangesSince will iterate through all documents in the database in ascending sequence number order, from since (inclusive) through till (inclusive). For each document, the provided DocumentInfoCallback will be invoked. A user specified context can be included, and this will be passed to each invocation of the callback.

If since is 0, the iteration will start with the first document.

If endId is 0, the iteration will continue to the last document.

func (*Gouchstore) Close

func (g *Gouchstore) Close() error

Close will close the underlying file handle and release any resources associated with the Gouchstore object.

func (*Gouchstore) Commit

func (g *Gouchstore) Commit() error

func (*Gouchstore) Compact

func (g *Gouchstore) Compact(targetFilename string) error

func (*Gouchstore) DatabaseInfo

func (g *Gouchstore) DatabaseInfo() (*DatabaseInfo, error)

DatabaseInfo returns information describing the database itself.

func (*Gouchstore) DebugAddress

func (g *Gouchstore) DebugAddress(w io.Writer, offsetAddress int64, printRawBytes, readLargeChunk bool, indexType int) error

func (*Gouchstore) DocumentBodyById

func (g *Gouchstore) DocumentBodyById(id string) ([]byte, error)

func (*Gouchstore) DocumentByDocumentInfo

func (g *Gouchstore) DocumentByDocumentInfo(docInfo *DocumentInfo) (*Document, error)

DocumentByDocumentInfo returns the Document using the provided DocumentInfo. The provided DocumentInfo should be valid, such as one received by one of the DocumentInfo*() methods, on the current couchstore file.

func (*Gouchstore) DocumentByDocumentInfoNoAlloc

func (g *Gouchstore) DocumentByDocumentInfoNoAlloc(docInfo *DocumentInfo, doc *Document) error

func (*Gouchstore) DocumentById

func (g *Gouchstore) DocumentById(id string) (*Document, error)

DocumentById returns the Document with the specified identifier.

func (*Gouchstore) DocumentByIdNoAlloc

func (g *Gouchstore) DocumentByIdNoAlloc(id string, doc *Document) error

func (*Gouchstore) DocumentInfoById

func (g *Gouchstore) DocumentInfoById(id string) (*DocumentInfo, error)

DocumentInfoById returns DocumentInfo for a single document with the specified ID.

func (*Gouchstore) DocumentInfoByIdNoAlloc

func (g *Gouchstore) DocumentInfoByIdNoAlloc(id string, docInfo *DocumentInfo) error

func (*Gouchstore) DocumentInfoBySeq

func (g *Gouchstore) DocumentInfoBySeq(seq uint64) (*DocumentInfo, error)

DocumentInfoBySeq returns DocumentInfo for a single document with the specified sequence number.

func (*Gouchstore) DocumentInfosByIds

func (g *Gouchstore) DocumentInfosByIds(identifiers []string) ([]*DocumentInfo, error)

DocumentInfosByIds returns DocumentInfo objects for the specified document IDs. This will be more efficient than making consecutive calls to DocumentInfoById().

NOTE: contents of the result slice will be in ascending ID order, not the order they appeared in the argument list.

func (*Gouchstore) DocumentInfosBySeqs

func (g *Gouchstore) DocumentInfosBySeqs(sequences []uint64) ([]*DocumentInfo, error)

DocumentInfosBySeqs returns DocumentInfo objects for the specified document sequence numbers. This will be more efficient than making consecutive calls to DocumentInfoBySeq().

NOTE: contents of the result slice will be in ascending sequence order, not the order they appeared in the argument list.

func (*Gouchstore) LocalDocumentById

func (g *Gouchstore) LocalDocumentById(id string) (*LocalDocument, error)

LocalDocumentById returns the LocalDocument with the specified identifier.

func (*Gouchstore) SaveDocument

func (g *Gouchstore) SaveDocument(doc *Document, docInfo *DocumentInfo) error

SaveDocument stores the document, if doc is nil, the document will be deleted

func (*Gouchstore) SaveDocuments

func (g *Gouchstore) SaveDocuments(docs []*Document, docInfos []*DocumentInfo) error

SaveDocuments stores multiple documents at a time

func (*Gouchstore) SaveLocalDocument

func (g *Gouchstore) SaveLocalDocument(localDoc *LocalDocument) error

SaveLocalDocument stores local documents in the database

func (*Gouchstore) WalkDocs

func (db *Gouchstore) WalkDocs(startkey, endkey string, callback DocumentWalkFun) error

Walk the DB from a specific location including the complete docs.

func (*Gouchstore) WalkIdTree

func (g *Gouchstore) WalkIdTree(startId, endId string, wtcb WalkTreeCallback, userContext interface{}) error

func (*Gouchstore) WalkLocalDocsTree

func (g *Gouchstore) WalkLocalDocsTree(startId, endId string, wtcb WalkTreeCallback, userContext interface{}) error

func (*Gouchstore) WalkSeqTree

func (g *Gouchstore) WalkSeqTree(since uint64, till uint64, wtcb WalkTreeCallback, userContext interface{}) error

type InMemoryTreeWriter

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

func NewInMemoryTreeWriter

func NewInMemoryTreeWriter(keyCompare btreeKeyComparator, reduce, rereduce reduceFunc, reduceContext interface{}) (*InMemoryTreeWriter, error)

func (*InMemoryTreeWriter) AddItem

func (imt *InMemoryTreeWriter) AddItem(key, value []byte) error

func (*InMemoryTreeWriter) Close

func (imt *InMemoryTreeWriter) Close() error

func (*InMemoryTreeWriter) Sort

func (imt *InMemoryTreeWriter) Sort() error

func (*InMemoryTreeWriter) Write

func (imt *InMemoryTreeWriter) Write(db *Gouchstore) (*nodePointer, error)

type LocalDocument

type LocalDocument struct {
	ID      string
	Body    []byte
	Deleted bool
}

LocalDocument represents a local (non-replicated) document.

type LogGouchOps

type LogGouchOps struct {
	*BaseGouchOps
}

func NewLogGouchOps

func NewLogGouchOps() *LogGouchOps

func (*LogGouchOps) Close

func (g *LogGouchOps) Close(f *os.File) error

func (*LogGouchOps) GotoEOF

func (g *LogGouchOps) GotoEOF(f *os.File) (ret int64, err error)

func (*LogGouchOps) OpenFile

func (g *LogGouchOps) OpenFile(name string, flag int, perm os.FileMode) (file *os.File, err error)

func (*LogGouchOps) ReadAt

func (g *LogGouchOps) ReadAt(f *os.File, b []byte, off int64) (n int, err error)

func (*LogGouchOps) Sync

func (g *LogGouchOps) Sync(f *os.File) error

func (*LogGouchOps) WriteAt

func (g *LogGouchOps) WriteAt(f *os.File, b []byte, off int64) (n int, err error)

type MemCompactGouchOps

type MemCompactGouchOps struct {
	*BaseGouchOps
}

func NewMemCompactGouchOps

func NewMemCompactGouchOps() *MemCompactGouchOps

func (*MemCompactGouchOps) CompactionTreeWriter

func (g *MemCompactGouchOps) CompactionTreeWriter(keyCompare btreeKeyComparator, reduce, rereduce reduceFunc, reduceContext interface{}) (TreeWriter, error)

type OnDiskTreeWriter

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

func NewOnDiskTreeWriter

func NewOnDiskTreeWriter(unsortedFilePath string, keyCompare btreeKeyComparator, reduce, rereduce reduceFunc, reduceContext interface{}) (*OnDiskTreeWriter, error)

func (*OnDiskTreeWriter) AddItem

func (imt *OnDiskTreeWriter) AddItem(key, value []byte) error

func (*OnDiskTreeWriter) Close

func (imt *OnDiskTreeWriter) Close() error

func (*OnDiskTreeWriter) Sort

func (imt *OnDiskTreeWriter) Sort() error

func (*OnDiskTreeWriter) Write

func (imt *OnDiskTreeWriter) Write(db *Gouchstore) (*nodePointer, error)

type TreeWriter

type TreeWriter interface {
	AddItem(key, value []byte) error
	Sort() error
	Write(db *Gouchstore) (*nodePointer, error)
	Close() error
}

type WalkTreeCallback

type WalkTreeCallback func(gouchstore *Gouchstore, depth int, documentInfo *DocumentInfo, key []byte, subTreeSize uint64, reducedValue []byte, userContext interface{}) error

WalkTreeCallback is a function definition which is used for tree walks.

Directories

Path Synopsis
utils

Jump to

Keyboard shortcuts

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