-
Notifications
You must be signed in to change notification settings - Fork 32
/
doc.go
82 lines (82 loc) · 3.25 KB
/
doc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Package bigqueue provides embedded, fast and persistent queue
// written in pure Go using memory mapped file. bigqueue is
// currently not thread safe. To use bigqueue in parallel context,
// a Write lock needs to be acquired (even for Read APIs).
//
// Create or open a bigqueue:
//
// bq, err := bigqueue.NewQueue("path/to/queue")
// defer bq.Close()
//
// bigqueue persists the data of the queue in multiple Arenas.
// Each Arena is a file on disk that is mapped into memory (RAM)
// using mmap syscall. Default size of each Arena is set to 128MB.
// It is possible to create a bigqueue with custom Arena size:
//
// bq, err := bigqueue.NewQueue("path/to/queue", bigqueue.SetArenaSize(4*1024))
// defer bq.Close()
//
// bigqueue also allows setting up the maximum possible memory that it
// can use. By default, the maximum memory is set to [3 x Arena Size].
//
// bq, err := bigqueue.NewQueue("path/to/queue", bigqueue.SetArenaSize(4*1024),
// bigqueue.SetMaxInMemArenas(10))
// defer bq.Close()
//
// In this case, bigqueue will never allocate more memory than `4KB*10=40KB`. This
// memory is above and beyond the memory used in buffers for copying data.
//
// Bigqueue allows to set periodic flush based on either elapsed time or number
// of mutate (enqueue/dequeue) operations. Flush syncs the in memory changes of all
// memory mapped files with disk. *This is a best effort flush*. Elapsed time and
// number of mutate operations are only checked upon an enqueue/dequeue.
//
// This is how we can set these options:
//
// bq, err := bigqueue.NewQueue("path/to/queue", bigqueue.SetPeriodicFlushOps(2))
//
// In this case, a flush is done after every two mutate operations.
//
// bq, err := bigqueue.NewQueue("path/to/queue", bigqueue.SetPeriodicFlushDuration(time.Minute))
//
// In this case, a flush is done after one minute elapses and an Enqueue/Dequeue is called.
//
// Write to bigqueue:
//
// err := bq.Enqueue([]byte("elem")) // size = 1
//
// bigqueue allows writing string data directly, avoiding conversion to `[]byte`:
//
// err := bq.EnqueueString("elem") // size = 2
//
// Read from bigqueue:
//
// elem, err := bq.Dequeue()
//
// we can also read string data from bigqueue:
//
// elem, err := bq.DequeueString()
//
// Check whether bigqueue has non zero elements:
//
// isEmpty := bq.IsEmpty()
//
// bigqueue allows reading data from bigqueue using consumers similar to Kafka. This allows
// multiple consumers from reading data at different offsets (not in thread safe manner yet).
// The offsets of each consumer are persisted on disk and can be retrieved by creating a
// consumer with the same name. Data will be read from the same offset where it was left off.
//
// We can create a new consumer as follows. The offsets of a new consumer are set at the
// start of the queue wherever the first non-deleted element is.
// consumer, err := bq.NewConsumer("consumer")
//
// We can also copy an existing consumer. This will create a consumer that will have the
// same offsets into the queue as that of the existing consumer.
// copyConsumer, err := bq.FromConsumer("copyConsumer", consumer)
//
// Now, read operations can be performed on the consumer:
// isEmpty := consumer.IsEmpty()
// elem, err := consumer.Dequeue()
// elem, err := consumer.DequeueString()
//
package bigqueue