github.com/bhojpur/cache@v0.0.4/pkg/temporal/temporal.go (about) 1 package temporal 2 3 // Copyright (c) 2018 Bhojpur Consulting Private Limited, India. All rights reserved. 4 5 // Permission is hereby granted, free of charge, to any person obtaining a copy 6 // of this software and associated documentation files (the "Software"), to deal 7 // in the Software without restriction, including without limitation the rights 8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 // copies of the Software, and to permit persons to whom the Software is 10 // furnished to do so, subject to the following conditions: 11 12 // The above copyright notice and this permission notice shall be included in 13 // all copies or substantial portions of the Software. 14 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 // THE SOFTWARE. 22 23 import ( 24 "fmt" 25 "os" 26 "sync" 27 ) 28 29 const ( 30 ASC = "asc" 31 DESC = "desc" 32 ) 33 34 type MemCacheConfig struct { 35 Path string 36 Mode os.FileMode 37 } 38 39 type TimeEntry struct { 40 Time int64 41 Value []byte 42 } 43 44 type Query struct { 45 Series string 46 47 From int64 48 To int64 49 // Sorting order: 50 // Possible values are ASC and DESC 51 // ASC : The time Series will have the oldest data first 52 // DESC: The time Series will have the latest data first. 53 Sort string 54 55 // Number of entries to be returned per page. This is used for pagination. 56 // The next sequence is found out using NextEntry variable of a query response. 57 MaxEntries int 58 } 59 60 type TimeSeries []TimeEntry 61 62 type Database interface { 63 64 // Create a new bucket 65 Create(name string) error 66 67 // This function adds the records 68 Add(name string, timeseries TimeSeries) error 69 70 // Get the records 71 Query(q Query) (timeSeries TimeSeries, nextEntry *int64, err error) 72 73 QueryOnChannel(q Query) (timeseries <-chan TimeEntry, nextEntry chan *int64, err chan error) 74 // Get the total pages for a particular query. 75 // This helps for any client to call multiple queries 76 GetPages(q Query) (seriesList []int64, count int, err error) 77 78 // Get the records 79 Get(series string) (timeSeries TimeSeries, err error) 80 // Returns two channels, one for Time entries and one for error. 81 // This avoids the usage of an extra buffer by the database 82 // Caution: first read the channel and then read the error. Error channel shall be written only after the timeseries channel is closed 83 GetOnChannel(series string) (timeseries <-chan TimeEntry, err chan error) 84 85 // Delete a complete Series 86 Delete(series string) error 87 88 // Close the database 89 Close() error 90 } 91 92 var ds Database // It will be used as a singleton DB object 93 var once sync.Once // make thread safe singleton 94 95 func Open(config interface{}) (Database, error) { 96 switch config.(type) { 97 case MemCacheConfig: 98 retDB := new(MemCacheDB) 99 err := retDB.open(config.(MemCacheConfig)) 100 return retDB, err 101 default: 102 return nil, fmt.Errorf("Unsupported storage configuration") 103 } 104 }