@@ -23,7 +23,6 @@ import (
23
23
"flag"
24
24
"fmt"
25
25
"io/ioutil"
26
- "log"
27
26
"math"
28
27
"math/rand"
29
28
"os"
@@ -1796,70 +1795,6 @@ func TestMain(m *testing.M) {
1796
1795
os .Exit (m .Run ())
1797
1796
}
1798
1797
1799
- func ExampleDB_Subscribe () {
1800
- prefix := []byte {'a' }
1801
-
1802
- // This key should be printed, since it matches the prefix.
1803
- aKey := []byte ("a-key" )
1804
- aValue := []byte ("a-value" )
1805
-
1806
- // This key should not be printed.
1807
- bKey := []byte ("b-key" )
1808
- bValue := []byte ("b-value" )
1809
-
1810
- // Open the DB.
1811
- dir , err := ioutil .TempDir ("" , "badger-test" )
1812
- if err != nil {
1813
- panic (err )
1814
- }
1815
- defer removeDir (dir )
1816
- db , err := Open (DefaultOptions (dir ))
1817
- if err != nil {
1818
- panic (err )
1819
- }
1820
- defer db .Close ()
1821
-
1822
- // Create the context here so we can cancel it after sending the writes.
1823
- ctx , cancel := context .WithCancel (context .Background ())
1824
- defer cancel ()
1825
-
1826
- // Use the WaitGroup to make sure we wait for the subscription to stop before continuing.
1827
- var wg sync.WaitGroup
1828
- wg .Add (1 )
1829
- go func () {
1830
- defer wg .Done ()
1831
- cb := func (kvs * KVList ) error {
1832
- for _ , kv := range kvs .Kv {
1833
- fmt .Printf ("%s is now set to %s\n " , kv .Key , kv .Value )
1834
- }
1835
- return nil
1836
- }
1837
- if err := db .Subscribe (ctx , cb , prefix ); err != nil && err != context .Canceled {
1838
- panic (err )
1839
- }
1840
- log .Printf ("subscription closed" )
1841
- }()
1842
-
1843
- // Wait for the above go routine to be scheduled.
1844
- time .Sleep (time .Second )
1845
- // Write both keys, but only one should be printed in the Output.
1846
- err = db .Update (func (txn * Txn ) error { return txn .Set (aKey , aValue ) })
1847
- if err != nil {
1848
- panic (err )
1849
- }
1850
- err = db .Update (func (txn * Txn ) error { return txn .Set (bKey , bValue ) })
1851
- if err != nil {
1852
- panic (err )
1853
- }
1854
-
1855
- log .Printf ("stopping subscription" )
1856
- cancel ()
1857
- log .Printf ("waiting for subscription to close" )
1858
- wg .Wait ()
1859
- // Output:
1860
- // a-key is now set to a-value
1861
- }
1862
-
1863
1798
func removeDir (dir string ) {
1864
1799
if err := os .RemoveAll (dir ); err != nil {
1865
1800
panic (err )
0 commit comments